Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkMeshVtkMapper3D.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 #include "mitkMeshVtkMapper3D.h"
14 #include "mitkDataNode.h"
15 #include "mitkProperties.h"
16 #include "mitkVtkPropRenderer.h"
17 
18 #ifndef VCL_VC60
19 #include "mitkMeshUtil.h"
20 #endif
21 
22 #include <vtkActor.h>
23 #include <vtkAssembly.h>
24 #include <vtkFollower.h>
25 #include <vtkProp3DCollection.h>
26 #include <vtkPropAssembly.h>
27 #include <vtkRenderer.h>
28 
29 #include <cstdlib>
30 #include <vtkPolyDataMapper.h>
31 #include <vtkProperty.h>
32 
34 {
35  return static_cast<const mitk::Mesh *>(GetDataNode()->GetData());
36 }
37 
39 {
40  return m_PropAssembly;
41 }
42 
44 {
45  vtkLinearTransform *vtktransform = this->GetDataNode()->GetVtkTransform(this->GetTimestep());
46 
47  m_SpheresActor->SetUserTransform(vtktransform);
48  m_ContourActor->SetUserTransform(vtktransform);
49 }
50 
52 {
53  m_Spheres = vtkAppendPolyData::New();
54  m_Contour = vtkPolyData::New();
55 
56  m_SpheresActor = vtkActor::New();
57  m_SpheresMapper = vtkPolyDataMapper::New();
58  m_SpheresActor->SetMapper(m_SpheresMapper);
59 
60  m_ContourActor = vtkActor::New();
61  m_ContourMapper = vtkPolyDataMapper::New();
62  m_ContourActor->SetMapper(m_ContourMapper);
63  m_ContourActor->GetProperty()->SetAmbient(1.0);
64 
65  m_PropAssembly = vtkPropAssembly::New();
66 
67  // a vtkPropAssembly is not a sub-class of vtkProp3D, so
68  // we cannot use m_Prop3D.
69 }
70 
72 {
73  m_ContourActor->Delete();
74  m_SpheresActor->Delete();
75  m_ContourMapper->Delete();
76  m_SpheresMapper->Delete();
77  m_PropAssembly->Delete();
78  m_Spheres->Delete();
79  m_Contour->Delete();
80 }
81 
83 {
85  bool needGenerateData = ls->IsGenerateDataRequired(renderer, this, GetDataNode());
86 
87  if (needGenerateData)
88  {
90 
91  m_PropAssembly->VisibilityOn();
92 
93  if (m_PropAssembly->GetParts()->IsItemPresent(m_SpheresActor))
94  m_PropAssembly->RemovePart(m_SpheresActor);
95  if (m_PropAssembly->GetParts()->IsItemPresent(m_ContourActor))
96  m_PropAssembly->RemovePart(m_ContourActor);
97 
98  m_Spheres->RemoveAllInputs();
99  m_Contour->Initialize();
100 
101  mitk::Mesh::Pointer input = const_cast<mitk::Mesh *>(this->GetInput());
102  input->Update();
103 
104  mitk::Mesh::DataType::Pointer itkMesh = input->GetMesh(this->GetTimestep());
105 
106  if (itkMesh.GetPointer() == nullptr)
107  {
108  m_PropAssembly->VisibilityOff();
109  return;
110  }
111 
112  mitk::Mesh::PointsContainer::Iterator i;
113 
114  int j;
115 
116  float floatRgba[4] = {1.0f, 1.0f, 1.0f, 1.0f};
117  double doubleRgba[4] = {1.0f, 1.0f, 1.0f, 1.0f};
118  mitk::Color tmpColor;
119 
120  // check for color prop and use it for rendering if it exists
121  m_DataNode->GetColor(floatRgba, nullptr);
122 
123  if (dynamic_cast<mitk::ColorProperty *>(this->GetDataNode()->GetProperty("unselectedcolor")) != nullptr)
124  {
125  tmpColor = dynamic_cast<mitk::ColorProperty *>(this->GetDataNode()->GetProperty("unselectedcolor"))->GetValue();
126  floatRgba[0] = tmpColor[0];
127  floatRgba[1] = tmpColor[1];
128  floatRgba[2] = tmpColor[2];
129  floatRgba[3] = 1.0f;
130  doubleRgba[0] = floatRgba[0];
131  doubleRgba[1] = floatRgba[1];
132  doubleRgba[2] = floatRgba[2];
133  doubleRgba[3] = floatRgba[3];
134  }
135 
136  if (itkMesh->GetNumberOfPoints() > 0)
137  {
138  // build m_Spheres->GetOutput() vtkPolyData
139  float pointSize = 2.0;
140  mitk::FloatProperty::Pointer pointSizeProp =
141  dynamic_cast<mitk::FloatProperty *>(this->GetDataNode()->GetProperty("pointsize"));
142  if (pointSizeProp.IsNotNull())
143  pointSize = pointSizeProp->GetValue();
144 
145  for (j = 0, i = itkMesh->GetPoints()->Begin(); i != itkMesh->GetPoints()->End(); i++, j++)
146  {
147  vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
148 
149  sphere->SetRadius(pointSize);
150  sphere->SetCenter(i.Value()[0], i.Value()[1], i.Value()[2]);
151 
152  m_Spheres->AddInputConnection(sphere->GetOutputPort());
153  sphere->Delete();
154  }
155 
156  // setup mapper, actor and add to assembly
157  m_SpheresMapper->SetInputConnection(m_Spheres->GetOutputPort());
158  m_SpheresActor->GetProperty()->SetColor(doubleRgba);
159  m_PropAssembly->AddPart(m_SpheresActor);
160  }
161 
162  if (itkMesh->GetNumberOfCells() > 0)
163  {
164 // build m_Contour vtkPolyData
165 #ifdef VCL_VC60
166  itkExceptionMacro(<< "MeshVtkMapper3D currently not working for MS Visual C++ 6.0, because MeshUtils are "
167  "currently not supported.");
168 #else
169  m_Contour =
170  MeshUtil<mitk::Mesh::MeshType>::MeshToPolyData(itkMesh.GetPointer(), false, false, 0, nullptr, m_Contour);
171 #endif
172 
173  if (m_Contour->GetNumberOfCells() > 0)
174  {
175  // setup mapper, actor and add to assembly
176  m_ContourMapper->SetInputData(m_Contour);
177  bool wireframe = true;
178  GetDataNode()->GetVisibility(wireframe, nullptr, "wireframe");
179  if (wireframe)
180  m_ContourActor->GetProperty()->SetRepresentationToWireframe();
181  else
182  m_ContourActor->GetProperty()->SetRepresentationToSurface();
183  m_ContourActor->GetProperty()->SetColor(doubleRgba);
184  m_PropAssembly->AddPart(m_ContourActor);
185  }
186  }
187  }
188 
189  bool visible = true;
190  GetDataNode()->GetVisibility(visible, renderer, "visible");
191 
192  if (!visible)
193  {
194  m_SpheresActor->VisibilityOff();
195  m_ContourActor->VisibilityOff();
196  return;
197  }
198 
199  bool makeContour = false;
200  this->GetDataNode()->GetBoolProperty("show contour", makeContour);
201 
202  if (makeContour)
203  {
204  m_ContourActor->VisibilityOn();
205  }
206  else
207  {
208  m_ContourActor->VisibilityOff();
209  }
210 
211  bool showPoints = true;
212  this->GetDataNode()->GetBoolProperty("show points", showPoints);
213  if (showPoints)
214  {
215  m_SpheresActor->VisibilityOn();
216  }
217  else
218  {
219  m_SpheresActor->VisibilityOff();
220  }
221 }
222 
224 {
225  m_PropAssembly->VisibilityOff();
226 }
#define ls
Definition: MitkMCxyz.cpp:57
mitk::BaseProperty * GetProperty(const char *propertyKey, const mitk::BaseRenderer *renderer=nullptr, bool fallBackOnDataProperties=true) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList of the rendere...
void UpdateVtkTransform(mitk::BaseRenderer *renderer) override
Set the vtkTransform of the m_Prop3D for the current time step of renderer.
L * GetLocalStorage(mitk::BaseRenderer *forRenderer)
Retrieves a LocalStorage for a specific BaseRenderer.
void ResetMapper(BaseRenderer *renderer) 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)
Base class for mapper specific rendering ressources.
Definition: mitkMapper.h:193
vtkProp * GetVtkProp(mitk::BaseRenderer *renderer) override
vtkPropAssembly * m_PropAssembly
bool IsGenerateDataRequired(mitk::BaseRenderer *renderer, mitk::Mapper *mapper, mitk::DataNode *dataNode) const
Definition: mitkMapper.cpp:119
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
Organizes the rendering process.
bool GetBoolProperty(const char *propertyKey, bool &boolValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for bool properties (instances of BoolProperty)
virtual const mitk::Mesh * GetInput()
The ColorProperty class RGB color property.
vtkPolyDataMapper * m_ContourMapper
BaseData * GetData() const
Get the data object (instance of BaseData, e.g., an Image) managed by this DataNode.
bool GetColor(float rgb[3], const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="color") const
Convenience access method for color properties (instances of ColorProperty)
DataStructure which stores a set of points (incl. pointdata) where each point can be associated to an...
Definition: mitkMesh.h:50
static vtkPolyData * MeshToPolyData(MeshType *mesh, bool onlyTriangles=false, bool useScalarAccessor=false, unsigned int pointDataType=0, mitk::BaseGeometry *geometryFrame=nullptr, vtkPolyData *polydata=nullptr)
mitk::DataNode * m_DataNode
Definition: mitkMapper.h:175
bool GetVisibility(bool &visible, const mitk::BaseRenderer *renderer, const char *propertyKey="visible") const
Convenience access method for visibility properties (instances of BoolProperty with property-key "vis...
Definition: mitkDataNode.h:422
vtkLinearTransform * GetVtkTransform(int t=0) const
Get the transformation applied prior to displaying the data as a vtkTransform.
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
vtkAppendPolyData * m_Spheres
int GetTimestep() const
Returns the current time step as calculated from the renderer.
Definition: mitkMapper.h:147
LocalStorageHandler< BaseLocalStorage > m_LSH
vtkPolyDataMapper * m_SpheresMapper
void GenerateDataForRenderer(mitk::BaseRenderer *renderer) override
Generate the data needed for rendering into renderer.