Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkNeedleProjectionFilter.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 // MITK
19 #include <mitkPlaneGeometry.h>
20 
21 // VTK
22 #include <vtkPlane.h>
23 
24 
26  : m_Projection(mitk::PointSet::New()),
27  m_OriginalPoints(mitk::PointSet::New()),
28  m_SelectedInput(-1)
29 {
30  // Tool Coordinates: First point - Tip of Needle, Second Point - 40 cm distance from needle
31  for (int i = 0; i < 2; i++)
32  {
33  mitk::Point3D point;
34  point.SetElement(0,0);
35  point.SetElement(1,0);
36  point.SetElement(2, i * 400);
37  m_OriginalPoints->InsertPoint(i, point);
38  }
39 }
40 
42 {
43 }
44 
45 
47 {
48  if (i < 0) mitkThrow() << "Negative Input selected in NeedleProjectionFilter";
49  if (! (static_cast<unsigned int>(i) < this->GetInputs().size())) mitkThrow() << "Selected input index is larger than actual number of inputs in NeedleProjectionFilter";
50  m_SelectedInput = i;
51 }
52 
54 {
55  // copy the navigation data from the inputs to the outputs
57 
58  // If no reference has been set yet, warn and abort
59  if (m_SelectedInput == -1)
60  {
61  MITK_INFO << "No input has been selected in NeedleProjection Filter. Only forwarding NavigationData...";
62  return;
63  }
64 
65  // Cancel, if selected tool is currently not being tracked
66  if (! GetInput(m_SelectedInput)->IsDataValid()) return;
67 
68  // Outputs have been updated, now to calculate the Projection
69  // 1) Generate Pseudo-Geometry for Input
70  mitk::AffineTransform3D::Pointer refTrans = this->NavigationDataToTransform(this->GetInput(m_SelectedInput));
71  mitk::Geometry3D::Pointer refGeom = this->TransformToGeometry(refTrans);
72  // 2) Transform Original Pointset
73  m_OriginalPoints->SetGeometry(refGeom);
74  // Update Projection (We do not clone, since we want to keep properties alive)
75  m_Projection->SetPoint(0, m_OriginalPoints->GetPoint(0));
76  m_Projection->SetPoint(1, m_OriginalPoints->GetPoint(1));
77  // 3a) If no target Plane has been set, then leave it at that
78  if (this->m_TargetPlane.IsNull())
79  return;
80 
81  // 3b) else, calculate intersection with plane
83  plane->SetIndexToWorldTransform(m_TargetPlane);
84  //plane->TransferItkToVtkTransform(); //included in SetIndexToWorldTransform
85 
86  double t;
87  double x[3];
88  // Points that define the needle vector
89  double p1[3] = {m_OriginalPoints->GetPoint(0)[0], m_OriginalPoints->GetPoint(0)[1], m_OriginalPoints->GetPoint(0)[2]};
90  double p2[3] = {m_OriginalPoints->GetPoint(1)[0], m_OriginalPoints->GetPoint(1)[1], m_OriginalPoints->GetPoint(1)[2]};
91  // Center of image plane and it's normal
92  double center[3] = {plane->GetCenter()[0], plane->GetCenter()[1], plane->GetCenter()[2]};
93  double normal[3] = {plane->GetNormal()[0], plane->GetNormal()[1], plane->GetNormal()[2]};
94 
95  vtkPlane::IntersectWithLine(p1, p2, normal, center, t, x);
96 
97  // change (cut) needle path only if the needle points to the image plane;
98  // otherwise the needle path direction would be changed pointing to the image plane
99  if ( t >= 0 )
100  {
101  // Convert vtk to itk
102  mitk::Point3D intersection;
103  intersection[0] = x[0];
104  intersection[1] = x[1];
105  intersection[2] = x[2];
106 
107  // Replace distant point with image intersection
108  m_Projection->SetPoint(1, intersection);
109  }
110 }
111 
113 {
115  affineTransform->SetIdentity();
116 
117  //calculate the transform from the quaternions
119 
121  // convert mitk::ScalarType quaternion to double quaternion because of itk bug
122  vnl_quaternion<double> doubleQuaternion(orientation.x(), orientation.y(), orientation.z(), orientation.r());
123  quatTransform->SetIdentity();
124  quatTransform->SetRotation(doubleQuaternion);
125  quatTransform->Modified();
126 
127  /* because of an itk bug, the transform can not be calculated with float data type.
128  To use it in the mitk geometry classes, it has to be transfered to mitk::ScalarType which is float */
129  static AffineTransform3D::MatrixType m;
130  mitk::TransferMatrix(quatTransform->GetMatrix(), m);
131  affineTransform->SetMatrix(m);
132 
133  /*set the offset by convert from itkPoint to itkVector and setting offset of transform*/
134  mitk::Vector3D pos;
135  pos.SetVnlVector(nd->GetPosition().GetVnlVector());
136  affineTransform->SetOffset(pos);
137 
138  affineTransform->Modified();
139  return affineTransform;
140 }
141 
144  mitk::ScalarType scale[] = {1.0, 1.0, 1.0};
145  g3d->SetSpacing(scale);
146  g3d->SetIndexToWorldTransform(transform);
147  //g3d->TransferItkToVtkTransform(); // update VTK Transform for rendering too //included in SetIndexToWorldTransform
148  g3d->Modified();
149  return g3d;
150 }
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
double ScalarType
Navigation Data.
DataCollection - Class to facilitate loading/accessing structured data.
virtual OrientationType GetOrientation() const
returns the orientation of the NavigationData object
static Pointer New()
mitk::Quaternion OrientationType
Type that holds the orientation part of the tracking data.
mitk::Geometry3D::Pointer TransformToGeometry(mitk::AffineTransform3D::Pointer transform)
Creates an Geometry 3D Object from an AffineTransformation.
Data structure which stores a set of points. Superclass of mitk::Mesh.
Definition: mitkPointSet.h:79
#define mitkThrow()
virtual void GenerateData() override
Passes navigation data from all inputs to all outputs. If a subclass wants to implement its own versi...
virtual void GenerateData() override
Passes navigation data from all inputs to all outputs. If a subclass wants to implement its own versi...
void TransferMatrix(const itk::Matrix< U, NRows, NColumns > &in, itk::Matrix< V, NRows, NColumns > &out)
mitk::PointSet::Pointer m_OriginalPoints
virtual PositionType GetPosition() const
returns position of the NavigationData object
static Pointer New()
mitk::AffineTransform3D::Pointer NavigationDataToTransform(const mitk::NavigationData *nd)
Creates an Affine Transformation from a Navigation Data Object.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.