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