Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkUnstructuredGridVtkMapper3D.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 #include "mitkColorProperty.h"
19 #include "mitkDataNode.h"
20 #include "mitkProperties.h"
22 //#include "mitkLookupTableProperty.h"
27 
28 #include "mitkDataStorage.h"
29 
30 #include "mitkSurfaceVtkMapper3D.h"
31 
32 #include <vtkProperty.h>
33 #include <vtkUnstructuredGrid.h>
34 #include <vtkVolume.h>
35 #include <vtkVolumeProperty.h>
36 
37 #include <vtkPlanes.h>
38 
40 {
41  return static_cast<const mitk::UnstructuredGrid *>(GetDataNode()->GetData());
42 }
43 
45 {
46  m_VtkTriangleFilter = vtkDataSetTriangleFilter::New();
47 
48  m_Assembly = vtkAssembly::New();
49 
50  m_Volume = vtkVolume::New();
51  m_Actor = vtkActor::New();
52  m_ActorWireframe = vtkActor::New();
53 
54  m_VtkDataSetMapper = vtkUnstructuredGridMapper::New();
55  m_VtkDataSetMapper->SetResolveCoincidentTopologyToPolygonOffset();
56  m_VtkDataSetMapper->SetResolveCoincidentTopologyPolygonOffsetParameters(0, 1);
57  m_Actor->SetMapper(m_VtkDataSetMapper);
58 
59  m_VtkDataSetMapper2 = vtkUnstructuredGridMapper::New();
60  m_VtkDataSetMapper2->SetResolveCoincidentTopologyToPolygonOffset();
61  m_VtkDataSetMapper2->SetResolveCoincidentTopologyPolygonOffsetParameters(1, 1);
62  m_ActorWireframe->SetMapper(m_VtkDataSetMapper2);
63  m_ActorWireframe->GetProperty()->SetRepresentationToWireframe();
64 
65  m_Assembly->AddPart(m_Actor);
66  m_Assembly->AddPart(m_ActorWireframe);
67  m_Assembly->AddPart(m_Volume);
68 
69  m_VtkVolumeRayCastMapper = 0;
70  m_VtkPTMapper = 0;
71  m_VtkVolumeZSweepMapper = 0;
72 
73  // m_GenerateNormals = false;
74 }
75 
77 {
78  if (m_VtkTriangleFilter != 0)
79  m_VtkTriangleFilter->Delete();
80 
81  if (m_VtkVolumeRayCastMapper != 0)
82  m_VtkVolumeRayCastMapper->Delete();
83 
84  if (m_VtkVolumeZSweepMapper != 0)
85  m_VtkVolumeZSweepMapper->Delete();
86 
87  if (m_VtkPTMapper != 0)
88  m_VtkPTMapper->Delete();
89 
90  if (m_VtkDataSetMapper != 0)
91  m_VtkDataSetMapper->Delete();
92 
93  if (m_VtkDataSetMapper2 != 0)
94  m_VtkDataSetMapper2->Delete();
95 
96  if (m_Assembly != 0)
97  m_Assembly->Delete();
98 
99  if (m_Actor != 0)
100  m_Actor->Delete();
101 
102  if (m_ActorWireframe != 0)
103  m_ActorWireframe->Delete();
104 
105  if (m_Volume != 0)
106  m_Volume->Delete();
107 }
108 
110 {
111  return m_Assembly;
112 }
113 
115 {
116  mitk::DataNode::ConstPointer node = this->GetDataNode();
117 
118  BaseLocalStorage *ls = m_LSH.GetLocalStorage(renderer);
119  bool needGenerateData = ls->IsGenerateDataRequired(renderer, this, GetDataNode());
120 
121  if (needGenerateData)
122  {
124 
125  m_Assembly->VisibilityOn();
126 
127  m_ActorWireframe->GetProperty()->SetAmbient(1.0);
128  m_ActorWireframe->GetProperty()->SetDiffuse(0.0);
129  m_ActorWireframe->GetProperty()->SetSpecular(0.0);
130 
132  if (node->GetProperty(transferFuncProp, "TransferFunction"))
133  {
134  mitk::TransferFunction::Pointer transferFunction = transferFuncProp->GetValue();
135  if (transferFunction->GetColorTransferFunction()->GetSize() < 2)
136  {
137  mitk::UnstructuredGrid::Pointer input = const_cast<mitk::UnstructuredGrid *>(this->GetInput());
138  if (input.IsNull())
139  return;
140 
141  vtkUnstructuredGrid *grid = input->GetVtkUnstructuredGrid(this->GetTimestep());
142  if (grid == 0)
143  return;
144 
145  double *scalarRange = grid->GetScalarRange();
146  vtkColorTransferFunction *colorFunc = transferFunction->GetColorTransferFunction();
147  colorFunc->RemoveAllPoints();
148  colorFunc->AddRGBPoint(scalarRange[0], 1, 0, 0);
149  colorFunc->AddRGBPoint((scalarRange[0] + scalarRange[1]) / 2.0, 0, 1, 0);
150  colorFunc->AddRGBPoint(scalarRange[1], 0, 0, 1);
151  }
152  }
153  }
154 
155  bool visible = true;
156  GetDataNode()->GetVisibility(visible, renderer, "visible");
157 
158  if (!visible)
159  {
160  m_Assembly->VisibilityOff();
161  return;
162  }
163 
164  //
165  // get the TimeGeometry of the input object
166  //
167  mitk::UnstructuredGrid::Pointer input = const_cast<mitk::UnstructuredGrid *>(this->GetInput());
168 
169  //
170  // set the input-object at time t for the mapper
171  //
172  vtkUnstructuredGrid *grid = input->GetVtkUnstructuredGrid(this->GetTimestep());
173  if (grid == 0)
174  {
175  m_Assembly->VisibilityOff();
176  return;
177  }
178 
179  m_Assembly->VisibilityOn();
180 
181  m_VtkTriangleFilter->SetInputData(grid);
182  m_VtkDataSetMapper->SetInput(grid);
183  m_VtkDataSetMapper2->SetInput(grid);
184 
185  bool clip = false;
186  node->GetBoolProperty("enable clipping", clip);
187  mitk::DataNode::Pointer bbNode = renderer->GetDataStorage()->GetNamedDerivedNode("Clipping Bounding Object", node);
188  if (clip && bbNode.IsNotNull())
189  {
190  m_VtkDataSetMapper->SetBoundingObject(dynamic_cast<mitk::BoundingObject *>(bbNode->GetData()));
191  m_VtkDataSetMapper2->SetBoundingObject(dynamic_cast<mitk::BoundingObject *>(bbNode->GetData()));
192  }
193  else
194  {
195  m_VtkDataSetMapper->SetBoundingObject(0);
196  m_VtkDataSetMapper2->SetBoundingObject(0);
197  }
198 
199  //
200  // apply properties read from the PropertyList
201  //
202  ApplyProperties(0, renderer);
203 }
204 
206 {
207  m_Assembly->VisibilityOff();
208 }
209 
211 {
212  mitk::DataNode::Pointer node = this->GetDataNode();
213  ApplyColorAndOpacityProperties(renderer, m_Actor);
214  ApplyColorAndOpacityProperties(renderer, m_ActorWireframe);
215 
216  vtkVolumeProperty *volProp = m_Volume->GetProperty();
217  vtkProperty *property = m_Actor->GetProperty();
218  vtkProperty *wireframeProp = m_ActorWireframe->GetProperty();
219 
221  mitk::SurfaceVtkMapper3D::ApplyMitkPropertiesToVtkProperty(node, wireframeProp, renderer);
222 
224  if (node->GetProperty(transferFuncProp, "TransferFunction", renderer))
225  {
226  mitk::TransferFunction::Pointer transferFunction = transferFuncProp->GetValue();
227 
228  volProp->SetColor(transferFunction->GetColorTransferFunction());
229  volProp->SetScalarOpacity(transferFunction->GetScalarOpacityFunction());
230  volProp->SetGradientOpacity(transferFunction->GetGradientOpacityFunction());
231 
232  m_VtkDataSetMapper->SetLookupTable(transferFunction->GetColorTransferFunction());
233  m_VtkDataSetMapper2->SetLookupTable(transferFunction->GetColorTransferFunction());
234  }
235 
236  bool isVolumeRenderingOn = false;
237  node->GetBoolProperty("volumerendering", isVolumeRenderingOn, renderer);
238 
239  if (isVolumeRenderingOn)
240  {
241  m_Assembly->RemovePart(m_Actor);
242  m_Assembly->RemovePart(m_ActorWireframe);
243  m_Assembly->AddPart(m_Volume);
244 
246  if (node->GetProperty(mapperProp, "volumerendering.mapper", renderer))
247  {
248  mitk::GridVolumeMapperProperty::IdType type = mapperProp->GetValueAsId();
249  switch (type)
250  {
252  if (m_VtkVolumeRayCastMapper == 0)
253  {
254  m_VtkVolumeRayCastMapper = vtkUnstructuredGridVolumeRayCastMapper::New();
255  m_VtkVolumeRayCastMapper->SetInputConnection(m_VtkTriangleFilter->GetOutputPort());
256  }
257  m_Volume->SetMapper(m_VtkVolumeRayCastMapper);
258  break;
260  if (m_VtkPTMapper == 0)
261  {
262  m_VtkPTMapper = vtkProjectedTetrahedraMapper::New();
263  m_VtkPTMapper->SetInputConnection(m_VtkTriangleFilter->GetOutputPort());
264  }
265  m_Volume->SetMapper(m_VtkPTMapper);
266  break;
268  if (m_VtkVolumeZSweepMapper == 0)
269  {
270  m_VtkVolumeZSweepMapper = vtkUnstructuredGridVolumeZSweepMapper::New();
271  m_VtkVolumeZSweepMapper->SetInputConnection(m_VtkTriangleFilter->GetOutputPort());
272  }
273  m_Volume->SetMapper(m_VtkVolumeZSweepMapper);
274  break;
275  }
276  }
277  }
278  else
279  {
280  m_Assembly->RemovePart(m_Volume);
281  m_Assembly->AddPart(m_Actor);
282  m_Assembly->RemovePart(m_ActorWireframe);
283 
285  if (node->GetProperty(gridRepProp, "grid representation", renderer))
286  {
287  mitk::GridRepresentationProperty::IdType type = gridRepProp->GetValueAsId();
288  switch (type)
289  {
291  property->SetRepresentationToPoints();
292  break;
294  property->SetRepresentationToWireframe();
295  break;
297  property->SetRepresentationToSurface();
298  break;
299  }
300 
301  // if (type == mitk::GridRepresentationProperty::WIREFRAME_SURFACE)
302  // {
303  // m_Assembly->AddPart(m_ActorWireframe);
304  // }
305  }
306  }
307 
308  // mitk::LevelWindow levelWindow;
309  // if(node->GetLevelWindow(levelWindow, renderer, "levelWindow"))
310  // {
311  // m_VtkVolumeRayCastMapper->SetScalarRange(levelWindow.GetMin(),levelWindow.GetMax());
312  // }
313  // else
314  // if(node->GetLevelWindow(levelWindow, renderer))
315  // {
316  // m_VtkVolumeRayCastMapper->SetScalarRange(levelWindow.GetMin(),levelWindow.GetMax());
317  // }
318  //
319  // mitk::VtkRepresentationProperty* representationProperty;
320  // node->GetProperty(representationProperty, "material.representation", renderer);
321  // if ( representationProperty != NULL )
322  // m_Volume->GetProperty()->SetRepresentation( representationProperty->GetVtkRepresentation() );
323  //
324  // mitk::VtkInterpolationProperty* interpolationProperty;
325  // node->GetProperty(interpolationProperty, "material.interpolation", renderer);
326  // if ( interpolationProperty != NULL )
327  // m_Volume->GetProperty()->SetInterpolation( interpolationProperty->GetVtkInterpolation() );
328  //
329 
330  mitk::VtkScalarModeProperty *scalarMode = 0;
331  if (node->GetProperty(scalarMode, "scalar mode", renderer))
332  {
333  if (m_VtkVolumeRayCastMapper)
334  m_VtkVolumeRayCastMapper->SetScalarMode(scalarMode->GetVtkScalarMode());
335  if (m_VtkPTMapper)
336  m_VtkPTMapper->SetScalarMode(scalarMode->GetVtkScalarMode());
337  if (m_VtkVolumeZSweepMapper)
338  m_VtkVolumeZSweepMapper->SetScalarMode(scalarMode->GetVtkScalarMode());
339 
340  m_VtkDataSetMapper->SetScalarMode(scalarMode->GetVtkScalarMode());
341  m_VtkDataSetMapper2->SetScalarMode(scalarMode->GetVtkScalarMode());
342  }
343  else
344  {
345  if (m_VtkVolumeRayCastMapper)
346  m_VtkVolumeRayCastMapper->SetScalarModeToDefault();
347  if (m_VtkPTMapper)
348  m_VtkPTMapper->SetScalarModeToDefault();
349  if (m_VtkVolumeZSweepMapper)
350  m_VtkVolumeZSweepMapper->SetScalarModeToDefault();
351 
352  m_VtkDataSetMapper->SetScalarModeToDefault();
353  m_VtkDataSetMapper2->SetScalarModeToDefault();
354  }
355 
356  bool scalarVisibility = true;
357  node->GetBoolProperty("scalar visibility", scalarVisibility, renderer);
358  m_VtkDataSetMapper->SetScalarVisibility(scalarVisibility ? 1 : 0);
359  m_VtkDataSetMapper2->SetScalarVisibility(scalarVisibility ? 1 : 0);
360 
361  // double scalarRangeLower = std::numeric_limits<double>::min();
362  // double scalarRangeUpper = std::numeric_limits<double>::max();
363  // mitk::DoubleProperty* lowerRange = 0;
364  // if (node->GetProperty(lowerRange, "scalar range min", renderer))
365  // {
366  // scalarRangeLower = lowerRange->GetValue();
367  // }
368  // mitk::DoubleProperty* upperRange = 0;
369  // if (node->GetProperty(upperRange, "scalar range max", renderer))
370  // {
371  // scalarRangeUpper = upperRange->GetValue();
372  // }
373  // m_VtkDataSetMapper->SetScalarRange(scalarRangeLower, scalarRangeUpper);
374  // m_VtkDataSetMapper2->SetScalarRange(scalarRangeLower, scalarRangeUpper);
375 
376  // bool colorMode = false;
377  // node->GetBoolProperty("color mode", colorMode);
378  // m_VtkVolumeRayCastMapper->SetColorMode( (colorMode ? 1 : 0) );
379 
380  // double scalarsMin = 0;
381  // node->GetDoubleProperty("ScalarsRangeMinimum", scalarsMin, renderer);
382 
383  // double scalarsMax = 1.0;
384  // node->GetProperty("ScalarsRangeMaximum", scalarsMax, renderer);
385 
386  // m_VtkVolumeRayCastMapper->SetScalarRange(scalarsMin,scalarsMax);
387 }
388 
390  mitk::BaseRenderer *renderer,
391  bool overwrite)
392 {
394 
395  node->AddProperty("grid representation", GridRepresentationProperty::New(), renderer, overwrite);
396  node->AddProperty("volumerendering", BoolProperty::New(false), renderer, overwrite);
397  node->AddProperty("volumerendering.mapper", GridVolumeMapperProperty::New(), renderer, overwrite);
398  node->AddProperty("scalar mode", VtkScalarModeProperty::New(0), renderer, overwrite);
399  node->AddProperty("scalar visibility", BoolProperty::New(true), renderer, overwrite);
400  // node->AddProperty("scalar range min", DoubleProperty::New(std::numeric_limits<double>::min()), renderer,
401  // overwrite);
402  // node->AddProperty("scalar range max", DoubleProperty::New(std::numeric_limits<double>::max()), renderer,
403  // overwrite);
404  node->AddProperty("outline polygons", BoolProperty::New(false), renderer, overwrite);
405  node->AddProperty("color", ColorProperty::New(1.0f, 1.0f, 1.0f), renderer, overwrite);
406  node->AddProperty("line width", IntProperty::New(1), renderer, overwrite);
407 
408  if (overwrite || node->GetProperty("TransferFunction", renderer) == 0)
409  {
410  // add a default transfer function
412  // tf->GetColorTransferFunction()->RemoveAllPoints();
413  node->SetProperty("TransferFunction", mitk::TransferFunctionProperty::New(tf.GetPointer()));
414  }
415 
416  Superclass::SetDefaultProperties(node, renderer, overwrite);
417 }
Base class for mapper specific rendering ressources.
Definition: mitkMapper.h:200
static void SetDefaultProperties(mitk::DataNode *node, mitk::BaseRenderer *renderer=NULL, bool overwrite=false)
static Pointer New()
Organizes the rendering process.
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
mitk::BaseProperty * GetProperty(const char *propertyKey, const mitk::BaseRenderer *renderer=nullptr) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList of the rendere...
virtual DataStorage::Pointer GetDataStorage() const
static Pointer New()
void AddProperty(const char *propertyKey, BaseProperty *property, const mitk::BaseRenderer *renderer=nullptr, bool overwrite=false)
Add the property (instance of BaseProperty) if it does not exist (or always ifoverwrite istrue) with ...
bool IsGenerateDataRequired(mitk::BaseRenderer *renderer, mitk::Mapper *mapper, mitk::DataNode *dataNode) const
Definition: mitkMapper.cpp:129
void ApplyProperties(vtkActor *, mitk::BaseRenderer *renderer) override
Apply color and opacity properties read from the PropertyList.
virtual vtkProp * GetVtkProp(mitk::BaseRenderer *renderer) override
static void ApplyMitkPropertiesToVtkProperty(mitk::DataNode *node, vtkProperty *property, mitk::BaseRenderer *renderer)
virtual const mitk::UnstructuredGrid * GetInput()
static Pointer New()
static void SetDefaultPropertiesForVtkProperty(mitk::DataNode *node, mitk::BaseRenderer *renderer, bool overwrite)
virtual void ResetMapper(BaseRenderer *) override
Reset the mapper (i.e., make sure that nothing is displayed) if no valid data is present. In most cases the reimplemented function disables the according actors (toggling visibility off)
static Pointer New()
static vtkUnstructuredGridMapper * New()
virtual void GenerateDataForRenderer(mitk::BaseRenderer *renderer) override
Generate the data needed for rendering into renderer.
Class for storing unstructured grids (vtkUnstructuredGrid)
void SetProperty(const char *propertyKey, BaseProperty *property, const mitk::BaseRenderer *renderer=nullptr)
Set the property (instance of BaseProperty) with key propertyKey in the PropertyList of the renderer ...
virtual DataNode * GetDataNode() const
Get the DataNode containing the data to map. Method only returns valid DataNode Pointer if the mapper...
Definition: mitkMapper.cpp:36
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.