Medical Imaging Interaction Toolkit  2018.4.99-3e3f1a6e
Medical Imaging Interaction Toolkit
mitkRegistrationWrapperMapperBase.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 
13 
14 #include <vtkPropAssembly.h>
15 #include <vtkPointData.h>
16 #include <vtkProperty.h>
17 #include <vtkCellArray.h>
18 #include <vtkColorTransferFunction.h>
19 #include <vtkPolyDataMapper.h>
20 #include <vtkPolyData.h>
21 #include <vtkActor.h>
22 
23 #include <mitkProperties.h>
24 #include <mitkExceptionMacro.h>
25 #include <mitkException.h>
26 
30 #include "mitkRegVisHelper.h"
31 #include "mitkRegVisPropertyTags.h"
32 
34 {
35 
36 }
37 
38 
40 {
41 
42 }
43 
45 {
46  mitk::DataNode::Pointer node = this->GetDataNode();
47 
48  if (node.IsNull())
49  return;
50 
51  bool isVisible = true;
52  node->GetVisibility(isVisible, renderer);
53 
54  if (!isVisible)
55  return;
56 
57  RegWrapperLocalStorage *localStorage = m_LSH.GetLocalStorage(renderer);
58 
59  //check if updates occured in the node or on the display
60  bool outdatedRendererGeometry = RendererGeometryIsOutdated(renderer,localStorage->m_LastUpdateTime);
61 
62  if ( (localStorage->m_LastUpdateTime < node->GetMTime())
63  || (localStorage->m_LastUpdateTime < node->GetPropertyList()->GetMTime()) //was a property modified?
64  || (localStorage->m_LastUpdateTime < node->GetPropertyList(renderer)->GetMTime())
65  || outdatedRendererGeometry)
66  {
67  MITK_DEBUG << "UPDATE NEEDED FOR _ " << renderer->GetName();
68 
69  bool isGridActive = false;
70  node->GetBoolProperty(mitk::nodeProp_RegVisGrid,isGridActive);
71 
72  bool isGlyphActive = false;
73  node->GetBoolProperty(mitk::nodeProp_RegVisGlyph,isGlyphActive);
74 
75  bool isPointsActive = false;
76  node->GetBoolProperty(mitk::nodeProp_RegVisPoints,isPointsActive);
77 
78  bool showStartGrid = false;
79  node->GetBoolProperty(mitk::nodeProp_RegVisGridShowStart,showStartGrid);
80 
81  bool isGridActiveOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisGrid,localStorage->m_LastUpdateTime);
82  bool isGlyphActiveOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisGlyph,localStorage->m_LastUpdateTime);
83  bool isPointsActiveOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisPoints,localStorage->m_LastUpdateTime);
84  bool showStartGridOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisGridShowStart,localStorage->m_LastUpdateTime);
85 
86  mitk::BaseData::Pointer baseData = node->GetData();
87 
88  if (baseData.IsNull())
89  return;
90 
91  const mitk::MAPRegistrationWrapper* regWrapper = dynamic_cast<const mitk::MAPRegistrationWrapper*>(baseData.GetPointer());
92  if (regWrapper == nullptr)
93  return;
94 
96  //1. Check the FOV and presentation styles
97  bool outdatedFOV = mitk::GridIsOutdated(node,localStorage->m_LastUpdateTime);
98  if (outdatedFOV ||isGridActiveOutdated || isGlyphActiveOutdated || isPointsActiveOutdated || outdatedRendererGeometry)
99  { // we need to generate the grids/presentation again
100  const map::core::RegistrationKernelBase<3,3>* regKernel= mitk::GetRelevantRegKernelOfNode(node);
101 
102  if(!regKernel)
103  {
104  mitkThrow() << "No reg kernel for visualization";
105  }
106 
108  unsigned int gridFrequ =5;
109 
110  if (!GetGeometryDescription(renderer,gridDesc, gridFrequ))
111  {
112  return;
113  };
114 
115  if(isGridActive)
116  {
117  localStorage->m_DeformedGridData = mitk::Generate3DDeformationGrid(gridDesc, gridFrequ, regKernel);
118  localStorage->m_StartGridData = mitk::Generate3DDeformationGrid(gridDesc,gridFrequ);
119  localStorage->m_DeformedGridMapper->SetInputData(localStorage->m_DeformedGridData);
120  localStorage->m_StartGridMapper->SetInputData(localStorage->m_StartGridData);
121  }
122  else if (isGlyphActive)
123  {
124  localStorage->m_DeformedGridData = mitk::Generate3DDeformationGlyph(gridDesc, regKernel);
125  localStorage->m_StartGridData = nullptr;
126  localStorage->m_DeformedGridMapper->SetInputData(localStorage->m_DeformedGridData);
127  }
128  else
129  {
130  mitkThrow() << "No reg kernel visualization style activated.";
131  }
132  }
133 
135  //2.Check if the mappers or actors must be modified
136  bool isColorStyleOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisColorStyle,localStorage->m_LastUpdateTime);
137  bool isColorUniOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisColorUni,localStorage->m_LastUpdateTime);
138  bool isColor1Outdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisColor1Value,localStorage->m_LastUpdateTime);
139  bool isColor2Outdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisColor2Value,localStorage->m_LastUpdateTime);
140  bool isColor3Outdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisColor3Value,localStorage->m_LastUpdateTime);
141  bool isColor4Outdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisColor4Value,localStorage->m_LastUpdateTime);
142  bool isColor2MagOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisColor2Magnitude,localStorage->m_LastUpdateTime);
143  bool isColor3MagOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisColor3Magnitude,localStorage->m_LastUpdateTime);
144  bool isColor4MagOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisColor4Magnitude,localStorage->m_LastUpdateTime);
145  bool isColorInterpolateOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisColorInterpolate,localStorage->m_LastUpdateTime);
146 
147  if(isColorStyleOutdated || isColorUniOutdated || isColor1Outdated ||
148  isColor2Outdated || isColor2MagOutdated || isColor3Outdated || isColor3MagOutdated ||
149  isColor4Outdated || isColor4MagOutdated || isColorInterpolateOutdated)
150  {
151  localStorage->m_DeformedGridMapper->ScalarVisibilityOn();
152  localStorage->m_DeformedGridMapper->SetScalarModeToUsePointData();
153  localStorage->m_DeformedGridMapper->SelectColorArray( "VectorMagnitude" );
154 
155  mitk::RegVisColorStyleProperty* colorStyleProp = nullptr;
156  node->GetProperty(colorStyleProp, mitk::nodeProp_RegVisColorStyle);
157 
158  float color1[3] = {0.0,0.0,0.0};
159  node->GetColor( color1, nullptr, mitk::nodeProp_RegVisColor1Value );
160  float color2[3] = {0.25,0.25,0.25};
161  node->GetColor( color2, nullptr, mitk::nodeProp_RegVisColor2Value );
162  float color3[3] = {0.5,0.5,0.5};
163  node->GetColor( color3, nullptr, mitk::nodeProp_RegVisColor3Value );
164  float color4[3] = {1.0,1.0,1.0};
165  node->GetColor( color4, nullptr, mitk::nodeProp_RegVisColor4Value );
166 
167  double mag2 = 0;
168  node->GetPropertyValue(mitk::nodeProp_RegVisColor2Magnitude, mag2);
169  double mag3 = 0;
170  node->GetPropertyValue(mitk::nodeProp_RegVisColor3Magnitude, mag3);
171  double mag4 = 0;
172  node->GetPropertyValue(mitk::nodeProp_RegVisColor4Magnitude, mag4);
173 
174  bool interpolate = true;
175  node->GetBoolProperty(mitk::nodeProp_RegVisColorInterpolate,interpolate);
176 
177  //default :color by vector magnitude
178  localStorage->m_DeformedGridMapper->SelectColorArray( "VectorMagnitude" );
179  localStorage->m_DeformedGridMapper->SetUseLookupTableScalarRange(true);
180 
181  localStorage->m_LUT = vtkSmartPointer<vtkColorTransferFunction>::New();
182 
183  if (!colorStyleProp || colorStyleProp->GetValueAsId()==0)
184  { //uni color mode
185  float temprgb[3] = {1.0,1.0,1.0};
186  node->GetColor( temprgb, nullptr, mitk::nodeProp_RegVisColorUni );
187  localStorage->m_LUT->AddRGBSegment(0.0,temprgb[0],temprgb[1],temprgb[2],1.0,temprgb[0],temprgb[1],temprgb[2]);
188  localStorage->m_LUT->Build();
189  localStorage->m_DeformedGridMapper->SetLookupTable(localStorage->m_LUT);
190  }
191  else
192  {
193  localStorage->m_LUT->AddRGBPoint(0.0,color1[0],color1[1],color1[2]);
194  localStorage->m_LUT->AddRGBPoint(mag2,color2[0],color2[1],color2[2]);
195  localStorage->m_LUT->AddRGBPoint(mag3,color3[0],color3[1],color3[2]);
196  localStorage->m_LUT->AddRGBPoint(mag4,color4[0],color4[1],color4[2]);
197  if (!interpolate)
198  {
199  localStorage->m_LUT->AddRGBPoint(0.99*mag2,color1[0],color1[1],color1[2]);
200  localStorage->m_LUT->AddRGBPoint(0.99*mag3,color2[0],color2[1],color2[2]);
201  localStorage->m_LUT->AddRGBPoint(0.99*mag4,color3[0],color3[1],color3[2]);
202  };
203  }
204 
205  localStorage->m_LUT->Build();
206  localStorage->m_DeformedGridMapper->SetLookupTable(localStorage->m_LUT);
207  localStorage->m_DeformedGridMapper->Update();
208  }
209 
210  bool isGridStartColorOutdated = mitk::PropertyIsOutdated(node,mitk::nodeProp_RegVisGridStartColor,localStorage->m_LastUpdateTime);
211 
212  if(isGridStartColorOutdated)
213  {
214  localStorage->m_StartGridMapper->ScalarVisibilityOn();
215  localStorage->m_StartGridMapper->SetScalarModeToUsePointFieldData();
216 
217  float temprgb[3];
218  if (node->GetColor( temprgb, nullptr, mitk::nodeProp_RegVisGridStartColor ))
219  {
220  double trgb[3] = { (double) temprgb[0], (double) temprgb[1], (double) temprgb[2] };
221  localStorage->m_StartGridActor->GetProperty()->SetColor(trgb);
222  }
223  }
224 
226  //3. Check if Assembly must be updated
227  if(isGridActiveOutdated||isGlyphActiveOutdated||isPointsActiveOutdated||showStartGridOutdated)
228  {
229  localStorage->m_RegAssembly = vtkSmartPointer<vtkPropAssembly>::New();
230 
231  if (isGridActive)
232  {
233  localStorage->m_RegAssembly->AddPart(localStorage->m_DeformedGridActor);
234  if (showStartGrid)
235  {
236  localStorage->m_RegAssembly->AddPart(localStorage->m_StartGridActor);
237  }
238  }
239  else if (isGlyphActive)
240  {
241  localStorage->m_RegAssembly->AddPart(localStorage->m_DeformedGridActor);
242  }
243  }
244 
245  localStorage->m_LastUpdateTime.Modified();
246 
247  }
248 }
249 
250 
252 {
253  Superclass::SetDefaultProperties(node, renderer, overwrite);
254 }
255 
257 {
258  return m_LSH.GetLocalStorage(renderer)->m_RegAssembly;
259 }
260 
262 {
263  m_DeformedGridActor = vtkSmartPointer<vtkActor>::New();
264  m_DeformedGridMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
265  m_DeformedGridActor->SetMapper(m_DeformedGridMapper);
266 
267  m_StartGridActor = vtkSmartPointer<vtkActor>::New();
268  m_StartGridMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
269  m_StartGridActor->SetMapper(m_StartGridMapper);
270 
271  m_RegAssembly = vtkSmartPointer<vtkPropAssembly>::New();
272 
273  m_LUT = vtkSmartPointer<vtkColorTransferFunction>::New();
274 
275  m_DeformedGridData = nullptr;
276  m_StartGridData = nullptr;
277 }
278 
virtual IdType GetValueAsId() const
const char *const nodeProp_RegVisGrid
virtual DataNode * GetDataNode() const
Get the DataNode containing the data to map. Method only returns valid DataNode Pointer if the mapper...
Definition: mitkMapper.cpp:31
void GenerateDataForRenderer(mitk::BaseRenderer *renderer) override
Generate the data needed for rendering into renderer.
Organizes the rendering process.
const char *const nodeProp_RegVisColorInterpolate
#define MITK_DEBUG
Definition: mitkLogMacros.h:22
MAPRegistrationWrapper Wrapper class to allow the handling of MatchPoint registration objects as mitk...
mitk::LocalStorageHandler< RegWrapperLocalStorage > m_LSH
This member holds all three LocalStorages for the 3D render window(s).
virtual bool RendererGeometryIsOutdated(mitk::BaseRenderer *renderer, const itk::TimeStamp &time) const =0
const char *const nodeProp_RegVisColorStyle
vtkSmartPointer< vtkPolyData > MITKMATCHPOINTREGISTRATION_EXPORT Generate3DDeformationGrid(const mitk::BaseGeometry *gridDesc, unsigned int gridFrequence, const map::core::RegistrationKernelBase< 3, 3 > *regKernel)
const char *const nodeProp_RegVisGridStartColor
const char *const nodeProp_RegVisColor3Value
#define mitkThrow()
bool PropertyIsOutdated(const mitk::DataNode *regNode, const std::string &propName, const itk::TimeStamp &reference)
bool GridIsOutdated(const mitk::DataNode *regNode, const itk::TimeStamp &reference)
const char *const nodeProp_RegVisPoints
vtkSmartPointer< vtkPolyData > Generate3DDeformationGlyph(const mitk::BaseGeometry *gridDesc, const map::core::RegistrationKernelBase< 3, 3 > *regKernel)
const char *const nodeProp_RegVisColor1Value
const char *const nodeProp_RegVisColor2Value
const char *const nodeProp_RegVisColor4Magnitude
const char *const nodeProp_RegVisColor2Magnitude
const char *const nodeProp_RegVisGridShowStart
const char *const nodeProp_RegVisGlyph
RegWrapperLocalStorage()
Constructor of the local storage. Do as much actions as possible in here to avoid double executions...
const char *const nodeProp_RegVisColor4Value
const char *const nodeProp_RegVisColor3Magnitude
const char *const nodeProp_RegVisColorUni
itk::TimeStamp m_LastUpdateTime
Timestamp of last update of stored data.
static void SetDefaultProperties(DataNode *node, BaseRenderer *renderer=nullptr, bool overwrite=false)
vtkProp * GetVtkProp(mitk::BaseRenderer *renderer) override
virtual bool GetGeometryDescription(mitk::BaseRenderer *renderer, mitk::BaseGeometry::ConstPointer &gridDesc, unsigned int &gridFrequ) const =0
const char * GetName() const
get the name of the Renderer
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57
const map::core::RegistrationKernelBase< 3, 3 > * GetRelevantRegKernelOfNode(const mitk::DataNode *regNode)