Skip to content

Commit

Permalink
ENH: Move ResliceWithRuler to vtkSlicerPathExplorerLogic from widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunderlandkyl committed Jan 31, 2023
1 parent 050c88d commit b6fad7f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 12 deletions.
99 changes: 91 additions & 8 deletions PathExplorer/Logic/vtkSlicerPathExplorerLogic.cxx
Expand Up @@ -19,9 +19,10 @@
#include "vtkSlicerPathExplorerLogic.h"

// MRML includes
#include "vtkMRMLPathPlannerTrajectoryNode.h"
#include "vtkMRMLMarkupsLineNode.h"
#include "vtkMRMLSubjectHierarchyNode.h"
#include <vtkMRMLPathPlannerTrajectoryNode.h>
#include <vtkMRMLMarkupsLineNode.h>
#include <vtkMRMLSubjectHierarchyNode.h>
#include <vtkMRMLSliceNode.h>

// VTK includes
#include <vtkNew.h>
Expand Down Expand Up @@ -50,7 +51,7 @@ void vtkSlicerPathExplorerLogic::PrintSelf(ostream& os, vtkIndent indent)
}

//---------------------------------------------------------------------------
void vtkSlicerPathExplorerLogic::SetMRMLSceneInternal(vtkMRMLScene * newScene)
void vtkSlicerPathExplorerLogic::SetMRMLSceneInternal(vtkMRMLScene* newScene)
{
vtkNew<vtkIntArray> events;
events->InsertNextValue(vtkMRMLScene::NodeAddedEvent);
Expand Down Expand Up @@ -84,16 +85,22 @@ void vtkSlicerPathExplorerLogic::UpdateTrajectory(vtkMRMLPathPlannerTrajectoryNo
vtkMRMLMarkupsLineNode* pathLineNode = trajectoryNode->GetNthPathLineNode(pathIndex);
vtkMRMLMarkupsNode* entryPoints = trajectoryNode->GetEntryPointsNode();
int entryPointIndex = (entryPoints ? trajectoryNode->GetNthEntryPointIndex(pathIndex) : -1);
vtkMRMLMarkupsNode* targetPoints = trajectoryNode->GetTargetPointsNode();
vtkMRMLMarkupsNode* targetPoints = trajectoryNode->GetTargetPointsNode();
int targetPointIndex = (targetPoints ? trajectoryNode->GetNthTargetPointIndex(pathIndex) : -1);
if (entryPointIndex<0 || targetPointIndex<0)
int entryStatus = entryPoints->GetNthControlPointPositionStatus(entryPointIndex);
int targetStatus = targetPoints->GetNthControlPointPositionStatus(entryPointIndex);

if (entryPointIndex < 0 || targetPointIndex < 0
|| (targetStatus != vtkMRMLMarkupsNode::PositionPreview && targetStatus != vtkMRMLMarkupsNode::PositionDefined)
|| (entryStatus != vtkMRMLMarkupsNode::PositionPreview && entryStatus != vtkMRMLMarkupsNode::PositionDefined))
{
if (pathLineNode)
{
{
pathLineNode->RemoveAllControlPoints();
}
}
continue;
}

// We have a valid path, make sure there is a line node for it
// Make sure we have a valid path node
if (!pathLineNode)
Expand All @@ -112,6 +119,9 @@ void vtkSlicerPathExplorerLogic::UpdateTrajectory(vtkMRMLPathPlannerTrajectoryNo
// failed to get/create line node
continue;
}



double entryPointPositionWorld[3] = { 0.0 };
entryPoints->GetNthControlPointPositionWorld(entryPointIndex, entryPointPositionWorld);
double targetPointPositionWorld[3] = { 0.0 };
Expand Down Expand Up @@ -199,3 +209,76 @@ void vtkSlicerPathExplorerLogic::ProcessMRMLNodesEvents(vtkObject* caller, unsig
}
}

//-----------------------------------------------------------------------------
void vtkSlicerPathExplorerLogic::ResliceWithRuler(vtkMRMLMarkupsLineNode* ruler, vtkMRMLSliceNode* viewer, bool perpendicular, double resliceValue)
{
if (!ruler || !viewer)
{
return;
}

// Get ruler points
double point1[4] = { 0,0,0,0 };
double point2[4] = { 0,0,0,0 };
ruler->GetNthControlPointPositionWorld(0, point1);
ruler->GetNthControlPointPositionWorld(1, point2);

// Compute vectors
double t[3];
double n[3];
double pos[3];
if (perpendicular)
{
// Ruler vector is normal vector
n[0] = point2[0] - point1[0];
n[1] = point2[1] - point1[1];
n[2] = point2[2] - point1[2];

// Reslice at chosen position
pos[0] = point1[0] + n[0] * resliceValue / 100;
pos[1] = point1[1] + n[1] * resliceValue / 100;
pos[2] = point1[2] + n[2] * resliceValue / 100;

// Normalize
double nlen = vtkMath::Normalize(n);
n[0] /= nlen;
n[1] /= nlen;
n[2] /= nlen;

// angle in radian
vtkMath::Perpendiculars(n, t, NULL, 0);
}
else
{
// Ruler vector is transverse vector
t[0] = point2[0] - point1[0];
t[1] = point2[1] - point1[1];
t[2] = point2[2] - point1[2];

// Reslice at target position
pos[0] = point2[0];
pos[1] = point2[1];
pos[2] = point2[2];

// Normalize
double tlen = vtkMath::Normalize(t);
t[0] /= tlen;
t[1] /= tlen;
t[2] /= tlen;

// angle in radian
vtkMath::Perpendiculars(t, n, NULL, resliceValue * vtkMath::Pi() / 180);
}

double nx = n[0];
double ny = n[1];
double nz = n[2];
double tx = t[0];
double ty = t[1];
double tz = t[2];
double px = pos[0];
double py = pos[1];
double pz = pos[2];

viewer->SetSliceToRASByNTP(nx, ny, nz, tx, ty, tz, px, py, pz, 0);
}
4 changes: 4 additions & 0 deletions PathExplorer/Logic/vtkSlicerPathExplorerLogic.h
Expand Up @@ -36,6 +36,8 @@
#include "vtkSlicerPathExplorerModuleLogicExport.h"

class vtkMRMLPathPlannerTrajectoryNode;
class vtkMRMLMarkupsLineNode;
class vtkMRMLSliceNode;

/// \ingroup Slicer_QtModules_ExtensionTemplate
class VTK_SLICER_PATHEXPLORER_MODULE_LOGIC_EXPORT vtkSlicerPathExplorerLogic :
Expand All @@ -51,6 +53,8 @@ class VTK_SLICER_PATHEXPLORER_MODULE_LOGIC_EXPORT vtkSlicerPathExplorerLogic :

void ProcessMRMLNodesEvents(vtkObject* caller, unsigned long event, void* callData) override;

static void ResliceWithRuler(vtkMRMLMarkupsLineNode* ruler, vtkMRMLSliceNode* viewer, bool perpendicular, double resliceValue);

protected:
vtkSlicerPathExplorerLogic();
virtual ~vtkSlicerPathExplorerLogic();
Expand Down
4 changes: 0 additions & 4 deletions PathExplorer/Widgets/qSlicerPathExplorerReslicingWidget.h
Expand Up @@ -60,10 +60,6 @@ class Q_SLICER_MODULE_PATHEXPLORER_WIDGETS_EXPORT qSlicerPathExplorerReslicingWi
void onPerpendicularToggled(bool status);
void onResliceValueChanged(int resliceValue);
void onRulerModified();
void resliceWithRuler(vtkMRMLMarkupsLineNode* ruler,
vtkMRMLSliceNode* viewer,
bool perpendicular,
double resliceValue);

protected:
QScopedPointer<qSlicerPathExplorerReslicingWidgetPrivate> d_ptr;
Expand Down

0 comments on commit b6fad7f

Please sign in to comment.