Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkNodeDisplacementFilter.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 
18 
19 
20 
22  : m_SelectedInput(-1)
23  {
24  }
25 
27  {
28  }
29 
31  {
32  // Consistency Checks
33  if (node.IsNull())
34  {
35  MITK_WARN("NodeDisplacementFilter")
36  << "Null Node passed to NodeDisplacementFilter. Ignoring Node....";
37  return false;
38  }
39  if (node->GetData() == 0)
40  {
41  MITK_WARN("NodeDisplacementFilter")
42  << "Empty Node passed to NodeDisplacementFilter. Ignoring Node....";
43  return false;
44  }
45  if(m_SelectedInput == -1)
46  {
47  MITK_ERROR("NodeDisplacementFilter")
48  << "Cannot add nodes before input Stream was selected";
49  mitkThrow() << "Cannot add nodes before input Stream was selected";
50  }
51 
52  this->Update(); // make sure we are working on current data
54  if (m_InitialReferencePose.IsNotNull()) //if there is a given reference pose use it
55  {reference = m_InitialReferencePose;}
56  else //else use the current pose of the given input
57  {reference = this->GetOutput(m_SelectedInput);}
58 
59  if (! reference->IsDataValid())
60  {
61  MITK_WARN("NodeDisplacementFilter")
62  << "Cannot add node while selected tool is not tracked. Ignoring Node....";
63  return false;
64  }
65 
66  // find transformation and add node
68  if ( ! reference->GetAffineTransform3D()->GetInverse(inverseAffineTransform) )
69  {
70  MITK_ERROR("NodeDisplacementFilter")
71  << "Could not get the inverse transformation of the navigation data transformation.";
72  mitkThrow() << "Could not get the inverse transformation of the navigation data transformation.";
73  }
74 
75  inverseAffineTransform->Compose(node->GetData()->GetGeometry()->GetIndexToWorldTransform(), true);
76  m_Transforms.push_back(inverseAffineTransform);
77  m_Nodes.push_back(node);
78 
79  return true;
80  }
81 
83  {
84  if ( i >= m_Nodes.size() ) { return false; }
85 
86  m_Nodes.erase(m_Nodes.begin()+i);
87  m_Transforms.erase(m_Transforms.begin()+i);
88  return true;
89  }
90 
92  {
93  return m_Nodes.size();
94  }
95 
97  {
98  if (i < m_Nodes.size() ) { return m_Nodes.at(i); }
99  else { return NULL; }
100  }
101 
102  std::vector< mitk::DataNode::Pointer > mitk::NodeDisplacementFilter::GetNodes()
103  {
104  return m_Nodes;
105  }
106 
108  {
109  if (i < 0) { mitkThrow() << "Negative Input selected in NodeDisplacementFilter"; }
110  if (! (static_cast<unsigned int>(i) < this->GetInputs().size()))
111  {
112  MITK_ERROR("NodeDisplacementFilter")
113  << "Selected input index is larger than actual number of inputs.";
114  mitkThrow() << "Selected input index is larger than actual number of inputs in NodeDisplacementFilter";
115  }
116  m_SelectedInput = i;
117  }
118 
120  {
122  if((m_Nodes.size()>i) && (m_Nodes.at(i).IsNotNull()))
123  {
124  try
125  {
126  returnValue = mitk::NavigationData::New(m_Nodes.at(i)->GetData()->GetGeometry()->GetIndexToWorldTransform());
127  }
128  catch (mitk::Exception& e)
129  {
130  returnValue->SetDataValid(false);
131  MITK_WARN << "Excetion while returning navigation data: " << e.GetDescription();
132  }
133  }
134  else
135  {
136  returnValue->SetDataValid(false);
137  MITK_WARN << "Node Nr. " << i << " does not exist!";
138  }
139 
140  return returnValue;
141  }
142 
144  {
145  // copy the navigation data from the inputs to the outputs
147 
148  // if no reference has been set yet, warn and abort
149  if (m_SelectedInput == -1)
150  {
151  MITK_INFO("NodeDisplacementFilter")
152  << "No input has been selected. Only forwarding NavigationData...";
153  return;
154  }
155 
156  // cancel, if selected tool is currently not being tracked
157  if ( ! this->GetInput(m_SelectedInput)->IsDataValid() ) { return; }
158 
159  // outputs have been updated, now to transform the nodes
160  // 1) Generate Pseudo-Geometry for Reference
161  mitk::Geometry3D::Pointer refGeom = this->TransformToGeometry(
162  this->GetInput(m_SelectedInput)->GetAffineTransform3D());
163 
164  // 2) For each node, calculate new position
165  for (unsigned int index=0; index < m_Nodes.size(); index++)
166  {
167  mitk::Geometry3D::Pointer transformGeometry = refGeom->Clone();
168 
169  // create transformation to the reference position and from there to
170  // the node position (node has fixed transformation from reference position)
171  transformGeometry->Compose(m_Transforms.at(index), true);
172 
173  m_Nodes.at(index)->GetData()->SetGeometry(transformGeometry);
174  }
175  }
176 
178  {
179  m_Nodes.clear();
180  m_Transforms.clear();
181  }
182 
185  g3d->SetIndexToWorldTransform(transform);
186  //g3d->TransferItkToVtkTransform(); // update VTK Transform for rendering too //Included in SetIndexToWorldTransform
187  g3d->Modified();
188  return g3d;
189 }
static Pointer New()
virtual mitk::DataNode::Pointer GetNode(unsigned int i=0)
Returns the nth node that was added to this filter.
itk::SmartPointer< Self > Pointer
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
virtual void GenerateData() override
Passes navigation data from all inputs to all outputs. If a subclass wants to implement its own versi...
bool AddNode(mitk::DataNode::Pointer node)
Adds a node to the filter. The position of which will then be continously update relatively to the se...
static void Update(vtkPolyData *)
Definition: mitkSurface.cpp:35
virtual void ResetNodes()
Removes all added Nodes from the Filter but leaves all other configuration intact.
static Pointer New()
bool RemoveNode(unsigned int i)
Removes a node from the filter.
mitk::Geometry3D::Pointer TransformToGeometry(mitk::AffineTransform3D::Pointer transform)
Creates an Geometry 3D Object from an AffineTransformation.
#define MITK_WARN
Definition: mitkLogMacros.h:23
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
virtual int GetNumberOfNodes()
Returns the number of nodes that were added to this filter.
#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 std::vector< mitk::DataNode::Pointer > GetNodes()
Returns a vector containing all nodes that have been added to this filter.
virtual mitk::NavigationData::Pointer GetRawDisplacementNavigationData(unsigned int i=0)
virtual void SelectInput(int i)
Selects an input stream as the reference stream.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.