Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkNavigationDataObjectVisualizationFilter.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 ============================================================================*/
13 
14 #include "mitkDataStorage.h"
15 
16 
19 m_RepresentationList(), m_TransformPosition(), m_TransformOrientation(), m_RotationMode(RotationStandard)
20 {
21 
22 }
23 
24 
26 {
27  m_RepresentationList.clear();
28  m_OffsetList.clear();
29 }
30 
31 
33 {
34  RepresentationPointerMap::const_iterator iter = m_RepresentationList.find(idx);
35  if (iter != m_RepresentationList.end())
36  return iter->second;
37 
38  return nullptr;
39 }
40 
41 mitk::AffineTransform3D::Pointer mitk::NavigationDataObjectVisualizationFilter::GetOffset(int index)
42 {
43  OffsetPointerMap::const_iterator iter = m_OffsetList.find(index);
44  if (iter != m_OffsetList.end())
45  return iter->second;
46  return nullptr;
47 }
48 
49 
51 {
53 }
54 
55 void mitk::NavigationDataObjectVisualizationFilter::SetOffset(int index, mitk::AffineTransform3D::Pointer offset)
56 {
57 m_OffsetList[index] = offset;
58 }
59 
60 
62 {
63  m_RotationMode = r;
64 }
65 
66 
68 {
69  /*get each input, lookup the associated BaseData and transfer the data*/
70  DataObjectPointerArray inputs = this->GetInputs(); //get all inputs
71  for (unsigned int index=0; index < inputs.size(); index++)
72  {
73  //get the needed variables
74  const mitk::NavigationData* nd = this->GetInput(index);
75  assert(nd);
76 
77  mitk::NavigationData* output = this->GetOutput(index);
78  assert(output);
79 
80  //check if the data is valid
81  if (!nd->IsDataValid())
82  {
83  output->SetDataValid(false);
84  continue;
85  }
86  output->Graft(nd); // copy all information from input to output
87  const mitk::BaseData* data = this->GetRepresentationObject(index);
88  if (data == nullptr)
89  {
90  MITK_WARN << "No BaseData associated with input " << index;
91  continue;
92  }
93 
94  //get the transform from data
95  mitk::AffineTransform3D::Pointer affineTransform = data->GetGeometry()->GetIndexToWorldTransform();
96  if (affineTransform.IsNull())
97  {
98  MITK_WARN << "AffineTransform IndexToWorldTransform not initialized!";
99  continue;
100  }
101 
102  //check for offset
103  mitk::AffineTransform3D::Pointer offset = this->GetOffset(index);
104 
105  //store the current scaling to set it after transformation
106  mitk::Vector3D spacing = data->GetGeometry()->GetSpacing();
107  //clear spacing of data to be able to set it again afterwards
108  ScalarType scale[] = {1.0, 1.0, 1.0};
109  data->GetGeometry()->SetSpacing(scale);
110 
111  /*now bring quaternion to affineTransform by using vnl_Quaternion*/
112  affineTransform->SetIdentity();
113 
114  if (this->GetTransformOrientation(index) == true)
115  {
117 
118  /* because of an itk bug, the transform can not be calculated with float data type.
119  To use it in the mitk geometry classes, it has to be transfered to mitk::ScalarType which is float */
120  static AffineTransform3D::MatrixType m;
121 
122  //convert quaternion to rotation matrix depending on the rotation mode
123  if(m_RotationMode == RotationStandard)
124  {
125  //calculate the transform from the quaternions
126  static itk::QuaternionRigidTransform<double>::Pointer quatTransform = itk::QuaternionRigidTransform<double>::New();
127  // convert mitk::ScalarType quaternion to double quaternion because of itk bug
128  vnl_quaternion<double> doubleQuaternion(orientation.x(), orientation.y(), orientation.z(), orientation.r());
129  quatTransform->SetIdentity();
130  quatTransform->SetRotation(doubleQuaternion);
131  quatTransform->Modified();
132  mitk::TransferMatrix(quatTransform->GetMatrix(), m);
133  }
134 
135  else if(m_RotationMode == RotationTransposed)
136  {
137  vnl_matrix_fixed<mitk::ScalarType,3,3> rot = orientation.rotation_matrix_transpose();
138  for(int i=0; i<3; i++) for (int j=0; j<3; j++) m[i][j] = rot[i][j];
139  }
140  affineTransform->SetMatrix(m);
141 
142  }
143  if (this->GetTransformPosition(index) == true)
144  {
146  mitk::Vector3D pos;
147  pos.SetVnlVector(nd->GetPosition().GetVnlVector());
148  affineTransform->SetOffset(pos);
149  }
150  affineTransform->Modified();
151 
152 
153  //set the transform to data
154  if(offset.IsNotNull()) //first use offset if there is one.
155  {
156  mitk::AffineTransform3D::Pointer overallTransform = mitk::AffineTransform3D::New();
157  overallTransform->SetIdentity();
158  overallTransform->Compose(offset);
159  overallTransform->Compose(affineTransform);
160  data->GetGeometry()->SetIndexToWorldTransform(overallTransform);
161  }
162  else
163  {
164  data->GetGeometry()->SetIndexToWorldTransform(affineTransform);
165  }
166 
167  //set the original spacing to keep scaling of the geometrical object
168  data->GetGeometry()->SetSpacing(spacing);
169  data->GetGeometry()->Modified();
170  data->Modified();
171  output->SetDataValid(true); // operation was successful, therefore data of output is valid.
172  }
173 }
174 
175 
176 void mitk::NavigationDataObjectVisualizationFilter::SetTransformPosition( unsigned int index, bool applyTransform )
177 {
178  itkDebugMacro("setting TransformPosition for index " << index << " to " << applyTransform);
179  BooleanInputMap::const_iterator it = this->m_TransformPosition.find(index);
180  if ((it != this->m_TransformPosition.end()) && (it->second == applyTransform))
181  return;
182 
183  this->m_TransformPosition[index] = applyTransform;
184  this->Modified();
185 }
186 
187 
189 {
190  itkDebugMacro("returning TransformPosition for index " << index);
191  BooleanInputMap::const_iterator it = this->m_TransformPosition.find(index);
192  if (it != this->m_TransformPosition.end())
193  return it->second;
194  else
195  return true; // default to true
196 }
197 
198 
200 {
201  this->SetTransformPosition(index, true);
202 }
203 
204 
206 {
207  this->SetTransformPosition(index, false);
208 }
209 
210 
211 void mitk::NavigationDataObjectVisualizationFilter::SetTransformOrientation( unsigned int index, bool applyTransform )
212 {
213  itkDebugMacro("setting TransformOrientation for index " << index << " to " << applyTransform);
214  BooleanInputMap::const_iterator it = this->m_TransformOrientation.find(index);
215  if ((it != this->m_TransformOrientation.end()) && (it->second == applyTransform))
216  return;
217 
218  this->m_TransformOrientation[index] = applyTransform;
219  this->Modified(); \
220 }
221 
222 
224 {
225  itkDebugMacro("returning TransformOrientation for index " << index);
226  BooleanInputMap::const_iterator it = this->m_TransformOrientation.find(index);
227  if (it != this->m_TransformOrientation.end())
228  return it->second;
229  else
230  return true; // default to true
231 }
232 
233 
235 {
236  this->SetTransformOrientation(index, true);
237 }
238 
239 
241 {
242  this->SetTransformOrientation(index, false);
243 }
void SetSpacing(const mitk::Vector3D &aSpacing, bool enforceSetSpacing=false)
Set the spacing (m_Spacing).
NavigationData * GetOutput(void)
return the output (output with id 0) of the filter
void SetIndexToWorldTransform(mitk::AffineTransform3D *transform)
Base of all data objects.
Definition: mitkBaseData.h:37
void SetRepresentationObject(unsigned int index, BaseData *data)
Set the representation object of the input.
virtual void SetTransformOrientation(unsigned int index, bool applyTransform)
if set to true, the filter will use the orientation part of the input navigation data at the given in...
virtual void TransformOrientationOn(unsigned int index)
sets the TransformOrientation flag to true for the given index
NavigationDataToNavigationDataFilter is the base class of all filters that receive NavigationDatas as...
double ScalarType
Navigation Data.
BooleanInputMap m_TransformPosition
if set to true, the filter will use the position part of the input navigation data at the given index...
BaseData::ConstPointer RepresentationPointer
Smart Pointer type to a BaseData.
virtual void SetDataValid(bool _arg)
sets the dataValid flag of the NavigationData object indicating if the object contains valid data ...
mitk::Quaternion OrientationType
Type that holds the orientation part of the tracking data.
virtual PositionType GetPosition() const
returns position of the NavigationData object
const NavigationData * GetInput(void) const
Get the input of this filter.
virtual void TransformPositionOff(unsigned int index)
sets the TransformPosition flag to false for the given index
virtual bool GetTransformPosition(unsigned int index) const
returns whether position part of the input navigation data at the given index is used for the transfo...
static Vector3D offset
#define MITK_WARN
Definition: mitkLogMacros.h:19
const BaseData * GetRepresentationObject(unsigned int idx)
Get the representation object associated with the index idx.
void Modified() const override
Overload of function Modified() to prohibit several calls of Modified() using the ModifiedLock class...
BooleanInputMap m_TransformOrientation
if set to true, the filter will use the orientation part of the input navigation data at the given in...
virtual void TransformOrientationOff(unsigned int index)
sets the TransformOrientation flag to false for the given index
RepresentationPointerMap m_RepresentationList
An array of the BaseData which represent the tools.
void TransferMatrix(const itk::Matrix< U, NRows, NColumns > &in, itk::Matrix< V, NRows, NColumns > &out)
void Graft(const DataObject *data) override
Graft the data and information from one NavigationData to another.
virtual void SetTransformPosition(unsigned int index, bool applyTransform)
if set to true, the filter will use the position part of the input navigation data at the given index...
const mitk::Vector3D GetSpacing() const
Get the spacing (size of a pixel).
virtual bool GetTransformOrientation(unsigned int index) const
returns whether orientation part of the input navigation data at the given index is used for the tran...
virtual void TransformPositionOn(unsigned int index)
sets the TransformPosition flag to true for the given index
virtual OrientationType GetOrientation() const
returns the orientation of the NavigationData object
void SetOffset(int index, mitk::AffineTransform3D::Pointer offset)
Defines an offset for a representation object. This offset is applied before the object is visualized...
virtual bool IsDataValid() const
returns true if the object contains valid data
mitk::BaseGeometry * GetGeometry(int t=0) const
Return the geometry, which is a TimeGeometry, of the data as non-const pointer.
Definition: mitkBaseData.h:138
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.