<commit_before>#include "mitkLineInteractor.h"
#include "mitkStatusBar.h"
#include <mitkInteractionConst.h>
#include <mitkLineOperation.h>
#include <mitkPointOperation.h>
#include <mitkPositionEvent.h>
#include <mitkStateTransitionOperation.h>
#include <mitkDataTreeNode.h>
#include <mitkMesh.h>
#include <mitkAction.h>
#include <mitkEventMapper.h>
#include <mitkLineOperation.h>
#include <mitkProperties.h>
#include <vtkTransform.h>
#include <mitkLine.h>
#include <mitkPointSet.h>


mitk::LineInteractor::LineInteractor(const char * type, DataTreeNode* dataTreeNode)
  : HierarchicalInteractor(type, dataTreeNode), m_CurrentLineId(0), m_CurrentCellId(0)
{
    m_PointInteractor = new mitk::PointInteractor("pointinteractor",dataTreeNode);
    this->AddInteractor((Interactor::Pointer)m_PointInteractor);
    m_LastPoint.Fill(0);
}

mitk::LineInteractor::~LineInteractor()
{
    //delete m_PointInteractor;
}

void mitk::LineInteractor::DeselectAllLines()
{
  mitk::Mesh* mesh = dynamic_cast<mitk::Mesh*>(m_DataTreeNode->GetData());
	if (mesh == NULL)
		return;

  //find the selected cell
  mitk::Mesh::DataType *itkMesh = mesh->GetMesh(); 
  mitk::Mesh::CellDataIterator cellDataIt;
  mitk::Mesh::CellDataIterator cellDataEnd;
  cellDataIt = itkMesh->GetCellData()->Begin();
  cellDataEnd = itkMesh->GetCellData()->End();
  while (cellDataIt != cellDataEnd)
  {
    if(cellDataIt->Value().selected)
      break;
    ++cellDataIt;
  }
  //then deselect all selected lines
  if (cellDataIt != cellDataEnd)
  {
    mitk::PointSet::SelectedLinesType selectedLines = cellDataIt->Value().selectedLines;
    for (unsigned int i = 0; i < selectedLines.size(); i++)
		{
      mitk::LineOperation* doOp = new mitk::LineOperation(OpDESELECTLINE, cellDataIt->Index(), -1, -1, selectedLines[i]);
			if (m_UndoEnabled)
			{
				mitk::LineOperation* undoOp = new mitk::LineOperation(OpSELECTLINE, cellDataIt->Index(), -1, -1, selectedLines[i]);
				OperationEvent *operationEvent = new OperationEvent(mesh, doOp, undoOp);
				m_UndoController->SetOperationEvent(operationEvent);
			}
			mesh->ExecuteOperation(doOp);
		}
  }
}

float mitk::LineInteractor::CalculateJurisdiction(StateEvent const* stateEvent) const
//go through all lines and check, if the given Point lies near a line
{
  float returnValue = 0;
  
  mitk::PositionEvent const  *posEvent = dynamic_cast <const mitk::PositionEvent *> (stateEvent->GetEvent());
  //checking if a keyevent can be handled:
  if (posEvent == NULL)
  {
    //check, if the current state has a transition waiting for that key event.
    if (this->GetCurrentState()->GetTransition(stateEvent->GetId())!=NULL)
    {
      return 0.5;
    }
    else
    {
      return 0;
    }
  }

  //Mouse event handling:
  //on MouseMove do nothing! reimplement if needed differently
  if (stateEvent->GetEvent()->GetType() == mitk::Type_MouseMove)
  {
    return 0;
  }

  //check on the right data-type
  mitk::Mesh* mesh = dynamic_cast<mitk::Mesh*>(m_DataTreeNode->GetData());
	if (mesh == NULL)
		return 0;


  //since we now have 3D picking in GlobalInteraction and all events send are DisplayEvents with 3D information,
  //we concentrate on 3D coordinates
  mitk::Point3D worldPoint = posEvent->GetWorldPosition();
  float p[3];
  itk2vtk(worldPoint, p);
  //transforming the Worldposition to local coordinatesystem
  m_DataTreeNode->GetData()->GetGeometry()->GetVtkTransform()->GetInverse()->TransformPoint(p, p);
  vtk2itk(p, worldPoint);

  //now check if the point lies on a line;
  unsigned long lineId = 0;
  unsigned long cellId = 0;

  float distance = 5;
  bool found =  mesh->SearchLine(worldPoint, distance, lineId, cellId);
  if (found)
    //how far away is the line from the point?
  {
    //get the cell
    Mesh::CellAutoPointer cellAutoPointer;
    if (!mesh->GetMesh()->GetCell(cellId,cellAutoPointer))
    {
      itkWarningMacro("Cell could not be found!Check LineInteractor");
      return 0;
    }
    //get the two points according to the recieved lineId
    mitk::PointSet::PointType pointA, pointB;
    mitk::Mesh::PointIdConstIterator pointIdIt = cellAutoPointer.GetPointer()->PointIdsBegin();
    mitk::Mesh::PointIdConstIterator pointIdEnd = cellAutoPointer.GetPointer()->PointIdsEnd();
    //get to the desired index of pointIndexes according to lineId
    int counter = 0;
    while (pointIdIt != pointIdEnd)
    {
      if (counter == lineId)
        break;
      ++counter;
      ++pointIdIt;
    }

    if ( ! mesh->GetMesh()->GetPoint((*pointIdIt), &pointA) )//get the PointA
      return 0;
    ++pointIdIt;
    if (pointIdIt == pointIdEnd)//then it was the last and the first point
      pointIdIt = cellAutoPointer.GetPointer()->PointIdsBegin();
    if ( ! mesh->GetMesh()->GetPoint((*pointIdIt), &pointB) )//get the PointB
      return 0;

    //now we have the points and so we can calculate the distance
    Line<PointSet::CoordinateType> *line = new Line<PointSet::CoordinateType>();
    line->SetPoints(pointA, pointB);
    
    double pointDistance = line->distance(worldPoint);
    
    //because we searched for a line in the distance, we now can set it according to distance between 0 and 1
    returnValue = 1 - ( pointDistance / distance );
    
    //and now between 0,5 and 1
    returnValue = 0.5 + (returnValue / 2);
  }
  else //not found
  {
    returnValue = 0;
  }


  //now check all lower statemachines:
  float lowerJurisdiction = this->CalculateLowerJurisdiction(stateEvent);
  
  if (returnValue<lowerJurisdiction)
    returnValue = lowerJurisdiction;

  return returnValue;
}


bool mitk::LineInteractor::ExecuteAction(Action* action, mitk::StateEvent const* stateEvent)
{
  bool ok = false;//for return type bool

  //checking corresponding Data; has to be a PointSet or a subclass
	mitk::Mesh* mesh = dynamic_cast<mitk::Mesh*>(m_DataTreeNode->GetData());
	if (mesh == NULL)
		return false;

  //for reading on the points, Id's etc
  mitk::Mesh::DataType *itkpointSet = mesh->GetMesh();
  mitk::Mesh::PointsContainer *points = itkpointSet->GetPoints();
  
  switch (action->GetActionId())
	{
  case AcADDLINE:
    {
      Operation* doOp = new mitk::Operation(OpADDLINE);
		  if (m_UndoEnabled)
		  {
		  	Operation* undoOp = new mitk::Operation(OpDELETELINE);<commit_msg>removed warnings: signed/unsigned and unused var : ----------------------------------------------------------------------<commit_after>#include "mitkLineInteractor.h"
#include "mitkStatusBar.h"
#include <mitkInteractionConst.h>
#include <mitkLineOperation.h>
#include <mitkPointOperation.h>
#include <mitkPositionEvent.h>
#include <mitkStateTransitionOperation.h>
#include <mitkDataTreeNode.h>
#include <mitkMesh.h>
#include <mitkAction.h>
#include <mitkEventMapper.h>
#include <mitkLineOperation.h>
#include <mitkProperties.h>
#include <vtkTransform.h>
#include <mitkLine.h>
#include <mitkPointSet.h>


mitk::LineInteractor::LineInteractor(const char * type, DataTreeNode* dataTreeNode)
  : HierarchicalInteractor(type, dataTreeNode), m_CurrentLineId(0), m_CurrentCellId(0)
{
    m_PointInteractor = new mitk::PointInteractor("pointinteractor",dataTreeNode);
    this->AddInteractor((Interactor::Pointer)m_PointInteractor);
    m_LastPoint.Fill(0);
}

mitk::LineInteractor::~LineInteractor()
{
    //delete m_PointInteractor;
}

void mitk::LineInteractor::DeselectAllLines()
{
  mitk::Mesh* mesh = dynamic_cast<mitk::Mesh*>(m_DataTreeNode->GetData());
	if (mesh == NULL)
		return;

  //find the selected cell
  mitk::Mesh::DataType *itkMesh = mesh->GetMesh(); 
  mitk::Mesh::CellDataIterator cellDataIt;
  mitk::Mesh::CellDataIterator cellDataEnd;
  cellDataIt = itkMesh->GetCellData()->Begin();
  cellDataEnd = itkMesh->GetCellData()->End();
  while (cellDataIt != cellDataEnd)
  {
    if(cellDataIt->Value().selected)
      break;
    ++cellDataIt;
  }
  //then deselect all selected lines
  if (cellDataIt != cellDataEnd)
  {
    mitk::PointSet::SelectedLinesType selectedLines = cellDataIt->Value().selectedLines;
    for (unsigned int i = 0; i < selectedLines.size(); i++)
		{
      mitk::LineOperation* doOp = new mitk::LineOperation(OpDESELECTLINE, cellDataIt->Index(), -1, -1, selectedLines[i]);
			if (m_UndoEnabled)
			{
				mitk::LineOperation* undoOp = new mitk::LineOperation(OpSELECTLINE, cellDataIt->Index(), -1, -1, selectedLines[i]);
				OperationEvent *operationEvent = new OperationEvent(mesh, doOp, undoOp);
				m_UndoController->SetOperationEvent(operationEvent);
			}
			mesh->ExecuteOperation(doOp);
		}
  }
}

float mitk::LineInteractor::CalculateJurisdiction(StateEvent const* stateEvent) const
//go through all lines and check, if the given Point lies near a line
{
  float returnValue = 0;
  
  mitk::PositionEvent const  *posEvent = dynamic_cast <const mitk::PositionEvent *> (stateEvent->GetEvent());
  //checking if a keyevent can be handled:
  if (posEvent == NULL)
  {
    //check, if the current state has a transition waiting for that key event.
    if (this->GetCurrentState()->GetTransition(stateEvent->GetId())!=NULL)
    {
      return 0.5;
    }
    else
    {
      return 0;
    }
  }

  //Mouse event handling:
  //on MouseMove do nothing! reimplement if needed differently
  if (stateEvent->GetEvent()->GetType() == mitk::Type_MouseMove)
  {
    return 0;
  }

  //check on the right data-type
  mitk::Mesh* mesh = dynamic_cast<mitk::Mesh*>(m_DataTreeNode->GetData());
	if (mesh == NULL)
		return 0;


  //since we now have 3D picking in GlobalInteraction and all events send are DisplayEvents with 3D information,
  //we concentrate on 3D coordinates
  mitk::Point3D worldPoint = posEvent->GetWorldPosition();
  float p[3];
  itk2vtk(worldPoint, p);
  //transforming the Worldposition to local coordinatesystem
  m_DataTreeNode->GetData()->GetGeometry()->GetVtkTransform()->GetInverse()->TransformPoint(p, p);
  vtk2itk(p, worldPoint);

  //now check if the point lies on a line;
  unsigned long lineId = 0;
  unsigned long cellId = 0;

  float distance = 5;
  bool found =  mesh->SearchLine(worldPoint, distance, lineId, cellId);
  if (found)
    //how far away is the line from the point?
  {
    //get the cell
    Mesh::CellAutoPointer cellAutoPointer;
    if (!mesh->GetMesh()->GetCell(cellId,cellAutoPointer))
    {
      itkWarningMacro("Cell could not be found!Check LineInteractor");
      return 0;
    }
    //get the two points according to the recieved lineId
    mitk::PointSet::PointType pointA, pointB;
    mitk::Mesh::PointIdConstIterator pointIdIt = cellAutoPointer.GetPointer()->PointIdsBegin();
    mitk::Mesh::PointIdConstIterator pointIdEnd = cellAutoPointer.GetPointer()->PointIdsEnd();
    //get to the desired index of pointIndexes according to lineId
    unsigned int counter = 0;
    while (pointIdIt != pointIdEnd)
    {
      if (counter == lineId)
        break;
      ++counter;
      ++pointIdIt;
    }

    if ( ! mesh->GetMesh()->GetPoint((*pointIdIt), &pointA) )//get the PointA
      return 0;
    ++pointIdIt;
    if (pointIdIt == pointIdEnd)//then it was the last and the first point
      pointIdIt = cellAutoPointer.GetPointer()->PointIdsBegin();
    if ( ! mesh->GetMesh()->GetPoint((*pointIdIt), &pointB) )//get the PointB
      return 0;

    //now we have the points and so we can calculate the distance
    Line<PointSet::CoordinateType> *line = new Line<PointSet::CoordinateType>();
    line->SetPoints(pointA, pointB);
    
    double pointDistance = line->distance(worldPoint);
    
    //because we searched for a line in the distance, we now can set it according to distance between 0 and 1
    returnValue = 1 - ( pointDistance / distance );
    
    //and now between 0,5 and 1
    returnValue = 0.5 + (returnValue / 2);
  }
  else //not found
  {
    returnValue = 0;
  }


  //now check all lower statemachines:
  float lowerJurisdiction = this->CalculateLowerJurisdiction(stateEvent);
  
  if (returnValue<lowerJurisdiction)
    returnValue = lowerJurisdiction;

  return returnValue;
}


bool mitk::LineInteractor::ExecuteAction(Action* action, mitk::StateEvent const* stateEvent)
{
  bool ok = false;//for return type bool

  //checking corresponding Data; has to be a PointSet or a subclass
	mitk::Mesh* mesh = dynamic_cast<mitk::Mesh*>(m_DataTreeNode->GetData());
	if (mesh == NULL)
		return false;

  //for reading on the points, Id's etc
  mitk::Mesh::DataType *itkpointSet = mesh->GetMesh();
  //mitk::Mesh::PointsContainer *points = itkpointSet->GetPoints();
  
  switch (action->GetActionId())
	{
  case AcADDLINE:
    {
      Operation* doOp = new mitk::Operation(OpADDLINE);
		  if (m_UndoEnabled)
		  {
		  	Operation* undoOp = new mitk::Operation(OpDELETELINE);
{'fraction_non_alphanumeric': 0.13073085755008315, 'fraction_numerical': 0.0060178953202945605, 'mean_word_length': 4.011904761904762, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 129, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4028340', 'n_tokens_mistral': 4151, 'n_tokens_neox': 3987, 'n_words': 1018}