Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
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 (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 
16 
18  : m_SelectedInput(-1)
19  {
20  }
21 
23  {
24  }
25 
27  {
28  // Consistency Checks
29  if (node.IsNull())
30  {
31  MITK_WARN("NodeDisplacementFilter")
32  << "Null Node passed to NodeDisplacementFilter. Ignoring Node....";
33  return false;
34  }
35  if (node->GetData() == nullptr)
36  {
37  MITK_WARN("NodeDisplacementFilter")
38  << "Empty Node passed to NodeDisplacementFilter. Ignoring Node....";
39  return false;
40  }
41  if(m_SelectedInput == -1)
42  {
43  MITK_ERROR("NodeDisplacementFilter")
44  << "Cannot add nodes before input Stream was selected";
45  mitkThrow() << "Cannot add nodes before input Stream was selected";
46  }
47 
48  this->Update(); // make sure we are working on current data
49  mitk::NavigationData::Pointer reference;
50  if (m_InitialReferencePose.IsNotNull()) //if there is a given reference pose use it
51  {reference = m_InitialReferencePose;}
52  else //else use the current pose of the given input
53  {reference = this->GetOutput(m_SelectedInput);}
54 
55  if (! reference->IsDataValid())
56  {
57  MITK_WARN("NodeDisplacementFilter")
58  << "Cannot add node while selected tool is not tracked. Ignoring Node....";
59  return false;
60  }
61 
62  // find transformation and add node
63  mitk::AffineTransform3D::Pointer inverseAffineTransform = mitk::AffineTransform3D::New();
64  if ( ! reference->GetAffineTransform3D()->GetInverse(inverseAffineTransform) )
65  {
66  MITK_ERROR("NodeDisplacementFilter")
67  << "Could not get the inverse transformation of the navigation data transformation.";
68  mitkThrow() << "Could not get the inverse transformation of the navigation data transformation.";
69  }
70 
71  inverseAffineTransform->Compose(node->GetData()->GetGeometry()->GetIndexToWorldTransform(), true);
72  m_Transforms.push_back(inverseAffineTransform);
73  m_Nodes.push_back(node);
74 
75  return true;
76  }
77 
79  {
80  if ( i >= m_Nodes.size() ) { return false; }
81 
82  m_Nodes.erase(m_Nodes.begin()+i);
83  m_Transforms.erase(m_Transforms.begin()+i);
84  return true;
85  }
86 
88  {
89  return m_Nodes.size();
90  }
91 
93  {
94  if (i < m_Nodes.size() ) { return m_Nodes.at(i); }
95  else { return nullptr; }
96  }
97 
98  std::vector< mitk::DataNode::Pointer > mitk::NodeDisplacementFilter::GetNodes()
99  {
100  return m_Nodes;
101  }
102 
104  {
105  if (i < 0) { mitkThrow() << "Negative Input selected in NodeDisplacementFilter"; }
106  if (! (static_cast<unsigned int>(i) < this->GetInputs().size()))
107  {
108  MITK_ERROR("NodeDisplacementFilter")
109  << "Selected input index is larger than actual number of inputs.";
110  mitkThrow() << "Selected input index is larger than actual number of inputs in NodeDisplacementFilter";
111  }
112  m_SelectedInput = i;
113  }
114 
115  mitk::NavigationData::Pointer mitk::NodeDisplacementFilter::GetRawDisplacementNavigationData(unsigned int i)
116  {
117  mitk::NavigationData::Pointer returnValue = mitk::NavigationData::New();
118  if((m_Nodes.size()>i) && (m_Nodes.at(i).IsNotNull()))
119  {
120  try
121  {
122  returnValue = mitk::NavigationData::New(m_Nodes.at(i)->GetData()->GetGeometry()->GetIndexToWorldTransform());
123  }
124  catch (mitk::Exception& e)
125  {
126  returnValue->SetDataValid(false);
127  MITK_WARN << "Excetion while returning navigation data: " << e.GetDescription();
128  }
129  }
130  else
131  {
132  returnValue->SetDataValid(false);
133  MITK_WARN << "Node Nr. " << i << " does not exist!";
134  }
135 
136  return returnValue;
137  }
138 
140  {
141  // copy the navigation data from the inputs to the outputs
143 
144  // if no reference has been set yet, warn and abort
145  if (m_SelectedInput == -1)
146  {
147  MITK_INFO("NodeDisplacementFilter")
148  << "No input has been selected. Only forwarding NavigationData...";
149  return;
150  }
151 
152  // cancel, if selected tool is currently not being tracked
153  if ( ! this->GetInput(m_SelectedInput)->IsDataValid() ) { return; }
154 
155  // outputs have been updated, now to transform the nodes
156  // 1) Generate Pseudo-Geometry for Reference
158  this->GetInput(m_SelectedInput)->GetAffineTransform3D());
159 
160  // 2) For each node, calculate new position
161  for (unsigned int index=0; index < m_Nodes.size(); index++)
162  {
163  mitk::Geometry3D::Pointer transformGeometry = refGeom->Clone();
164 
165  // create transformation to the reference position and from there to
166  // the node position (node has fixed transformation from reference position)
167  transformGeometry->Compose(m_Transforms.at(index), true);
168 
169  m_Nodes.at(index)->GetData()->SetGeometry(transformGeometry);
170  }
171  }
172 
174  {
175  m_Nodes.clear();
176  m_Transforms.clear();
177  }
178 
181  g3d->SetIndexToWorldTransform(transform);
182  //g3d->TransferItkToVtkTransform(); // update VTK Transform for rendering too //Included in SetIndexToWorldTransform
183  g3d->Modified();
184  return g3d;
185 }
static Pointer New()
virtual mitk::DataNode::Pointer GetNode(unsigned int i=0)
Returns the nth node that was added to this filter.
int m_SelectedInput
The Input that is used as a reference to orient the managed nodes.
NavigationData * GetOutput(void)
return the output (output with id 0) of the filter
#define MITK_INFO
Definition: mitkLogMacros.h:18
#define MITK_ERROR
Definition: mitkLogMacros.h:20
mitk::NavigationData::Pointer m_InitialReferencePose
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:31
virtual void ResetNodes()
Removes all added Nodes from the Filter but leaves all other configuration intact.
static Pointer New()
const NavigationData * GetInput(void) const
Get the input of this filter.
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:19
std::vector< mitk::DataNode::Pointer > m_Nodes
All Nodes that are being managed by this Filter.
An object of this class represents an exception of MITK. Please don&#39;t instantiate exceptions manually...
Definition: mitkException.h:45
virtual int GetNumberOfNodes()
Returns the number of nodes that were added to this filter.
#define mitkThrow()
void GenerateData() override
Passes navigation data from all inputs to all outputs. If a subclass wants to implement its own versi...
std::vector< mitk::AffineTransform3D::Pointer > m_Transforms
The transformation that each node has to be reached from the selected navigation tool. The indexes correspond to indexes in the node vector.
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.