Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkContourModelSetMapper3D.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 ===================================================================*/
17 
18 #include "mitkSurface.h"
19 #include <vtkCellArray.h>
20 #include <vtkPoints.h>
21 #include <vtkPolyLine.h>
22 #include <vtkProperty.h>
23 
25 {
26 }
27 
29 {
30 }
31 
33 {
34  // convient way to get the data from the dataNode
35  return static_cast<const mitk::ContourModelSet *>(GetDataNode()->GetData());
36 }
37 
39 {
40  // return the actor corresponding to the renderer
41  return m_LSH.GetLocalStorage(renderer)->m_Assembly;
42 }
43 
45 {
46  /* First convert the contourModel to vtkPolyData, then tube filter it and
47  * set it input for our mapper
48  */
49 
50  LocalStorage *localStorage = m_LSH.GetLocalStorage(renderer);
51 
52  ContourModelSet *contourModelSet = dynamic_cast<ContourModelSet *>(this->GetDataNode()->GetData());
53 
54  if (contourModelSet != NULL)
55  {
56  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
57  vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New();
58  vtkIdType baseIndex = 0;
59 
60  ContourModelSet::ContourModelSetIterator it = contourModelSet->Begin();
61  ContourModelSet::ContourModelSetIterator end = contourModelSet->End();
62 
63  while (it != end)
64  {
65  ContourModel *contourModel = it->GetPointer();
66 
67  ContourModel::VertexIterator vertIt = contourModel->Begin();
68  ContourModel::VertexIterator vertEnd = contourModel->End();
69 
70  while (vertIt != vertEnd)
71  {
72  points->InsertNextPoint((*vertIt)->Coordinates[0], (*vertIt)->Coordinates[1], (*vertIt)->Coordinates[2]);
73  ++vertIt;
74  }
75 
76  vtkSmartPointer<vtkPolyLine> line = vtkSmartPointer<vtkPolyLine>::New();
77  vtkIdList *pointIds = line->GetPointIds();
78 
79  vtkIdType numPoints = contourModel->GetNumberOfVertices();
80  pointIds->SetNumberOfIds(numPoints + 1);
81 
82  for (vtkIdType i = 0; i < numPoints; ++i)
83  pointIds->SetId(i, baseIndex + i);
84 
85  pointIds->SetId(numPoints, baseIndex);
86 
87  cells->InsertNextCell(line);
88 
89  baseIndex += numPoints;
90 
91  ++it;
92  }
93 
94  vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
95  polyData->SetPoints(points);
96  polyData->SetLines(cells);
97 
98  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
99  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
100  actor->SetMapper(mapper);
101 
102  mapper->SetInputData(polyData);
103 
104  localStorage->m_Assembly->AddPart(actor);
105  }
106  this->ApplyContourProperties(renderer);
107  this->ApplyContourModelSetProperties(renderer);
108 }
109 
111 {
112  bool visible = true;
113  GetDataNode()->GetVisibility(visible, renderer, "visible");
114 
115  mitk::ContourModel *data = static_cast<mitk::ContourModel *>(GetDataNode()->GetData());
116  if (data == NULL)
117  {
118  return;
119  }
120 
121  // Calculate time step of the input data for the specified renderer (integer value)
122  this->CalculateTimeStep(renderer);
123 
124  LocalStorage *localStorage = m_LSH.GetLocalStorage(renderer);
125 
126  if (this->GetTimestep() == -1)
127  {
128  return;
129  }
130 
131  const DataNode *node = this->GetDataNode();
132  data->UpdateOutputInformation();
133 
134  // check if something important has changed and we need to rerender
135  if ((localStorage->m_LastUpdateTime < node->GetMTime()) // was the node modified?
136  ||
137  (localStorage->m_LastUpdateTime < data->GetPipelineMTime()) // Was the data modified?
138  ||
139  (localStorage->m_LastUpdateTime <
140  renderer->GetCurrentWorldPlaneGeometryUpdateTime()) // was the geometry modified?
141  ||
142  (localStorage->m_LastUpdateTime < renderer->GetCurrentWorldPlaneGeometry()->GetMTime()) ||
143  (localStorage->m_LastUpdateTime < node->GetPropertyList()->GetMTime()) // was a property modified?
144  ||
145  (localStorage->m_LastUpdateTime < node->GetPropertyList(renderer)->GetMTime()))
146  {
147  this->GenerateDataForRenderer(renderer);
148  }
149 
150  // since we have checked that nothing important has changed, we can set
151  // m_LastUpdateTime to the current time
152  localStorage->m_LastUpdateTime.Modified();
153 }
154 
156  mitk::ContourModel *inputContour, mitk::BaseRenderer *renderer)
157 {
158  unsigned int timestep = this->GetTimestep();
159 
160  LocalStorage *localStorage = m_LSH.GetLocalStorage(renderer);
161 
162  localStorage->m_contourToPolyData->SetInput(inputContour);
163  localStorage->m_contourToPolyData->Update();
164 
165  vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
166  polyData = localStorage->m_contourToPolyData->GetOutput()->GetVtkPolyData(timestep);
167 
168  return polyData;
169 }
170 
172 {
173  LocalStorage *localStorage = m_LSH.GetLocalStorage(renderer);
174  DataNode *dataNode = this->GetDataNode();
175 
176  if (dataNode != NULL)
177  {
178  float lineWidth = 1;
179  dataNode->GetFloatProperty("contour.3D.width", lineWidth, renderer);
180 
181  vtkSmartPointer<vtkPropCollection> collection = vtkSmartPointer<vtkPropCollection>::New();
182  localStorage->m_Assembly->GetActors(collection);
183  collection->InitTraversal();
184  for (vtkIdType i = 0; i < collection->GetNumberOfItems(); i++)
185  {
186  vtkActor::SafeDownCast(collection->GetNextProp())->GetProperty()->SetLineWidth(lineWidth);
187  }
188  }
189 }
190 
192 {
193  LocalStorage *localStorage = m_LSH.GetLocalStorage(renderer);
194 
195  mitk::ColorProperty::Pointer colorprop =
196  dynamic_cast<mitk::ColorProperty *>(GetDataNode()->GetProperty("contour.color", renderer));
197  if (colorprop)
198  {
199  // set the color of the contour
200  double red = colorprop->GetColor().GetRed();
201  double green = colorprop->GetColor().GetGreen();
202  double blue = colorprop->GetColor().GetBlue();
203 
204  vtkSmartPointer<vtkPropCollection> collection = vtkSmartPointer<vtkPropCollection>::New();
205  localStorage->m_Assembly->GetActors(collection);
206  collection->InitTraversal();
207  for (vtkIdType i = 0; i < collection->GetNumberOfItems(); i++)
208  {
209  vtkActor::SafeDownCast(collection->GetNextProp())->GetProperty()->SetColor(red, green, blue);
210  }
211  }
212 }
213 
214 /*+++++++++++++++++++ LocalStorage part +++++++++++++++++++++++++*/
215 
217  mitk::BaseRenderer *renderer)
218 {
219  return m_LSH.GetLocalStorage(renderer);
220 }
221 
223 {
224  m_Assembly = vtkSmartPointer<vtkAssembly>::New();
225  m_contourToPolyData = mitk::ContourModelToSurfaceFilter::New();
226 }
227 
229  mitk::BaseRenderer *renderer,
230  bool overwrite)
231 {
232  node->AddProperty("color", ColorProperty::New(1.0, 0.0, 0.0), renderer, overwrite);
233  node->AddProperty("contour.3D.width", mitk::FloatProperty::New(0.5), renderer, overwrite);
234 
235  Superclass::SetDefaultProperties(node, renderer, overwrite);
236 }
LocalStorage()
Default constructor of the local storage.
mitk::PropertyList * GetPropertyList(const mitk::BaseRenderer *renderer=nullptr) const
Get the PropertyList of the renderer. If renderer is NULL, the BaseRenderer-independent PropertyList ...
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
static char * line
Definition: svm.cpp:2884
virtual void UpdateOutputInformation() override
Update the OutputInformation of a ContourModel object.
virtual void ApplyContourProperties(mitk::BaseRenderer *renderer)
LocalStorage * GetLocalStorage(mitk::BaseRenderer *renderer)
Get the LocalStorage corresponding to the current renderer.
virtual vtkProp * GetVtkProp(mitk::BaseRenderer *renderer) override
virtual unsigned long GetMTime() const override
Get the timestamp of the last change of the map or the last change of one of the properties store in ...
VertexIterator End(int timestep=0) const
Returns a const VertexIterator at the end element of the contour.
static Pointer New()
Organizes the rendering process.
mitk::ContourElement::VertexIterator VertexIterator
virtual const PlaneGeometry * GetCurrentWorldPlaneGeometry()
Get the current 2D-worldgeometry (m_CurrentWorldPlaneGeometry) used for 2D-rendering.
virtual vtkSmartPointer< vtkPolyData > CreateVtkPolyDataFromContour(mitk::ContourModel *inputContour, mitk::BaseRenderer *renderer)
virtual ContourModelSetIterator Begin()
Return an iterator a the front.
virtual ContourModelSetIterator End()
Return an iterator a the front.
virtual unsigned long GetMTime() const override
Get the timestamp of the last change of the contents of this node or the referenced BaseData...
ContourModelListType::iterator ContourModelSetIterator
void GenerateDataForRenderer(mitk::BaseRenderer *renderer) override
Generate the data needed for rendering into renderer.
The ColorProperty class RGB color property.
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 ...
const mitk::ContourModelSet * GetInput(void)
bool GetFloatProperty(const char *propertyKey, float &floatValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for float properties (instances of FloatProperty)
virtual void Update(mitk::BaseRenderer *renderer) override
Checks whether this mapper needs to update itself and generate data.
static Pointer New()
static void SetDefaultProperties(mitk::DataNode *node, mitk::BaseRenderer *renderer=NULL, bool overwrite=false)
Set the default properties for general image rendering.
VertexIterator Begin(int timestep=0) const
Returns a const VertexIterator at the start element of the contour.
mitk::ContourModelToSurfaceFilter::Pointer m_contourToPolyData
unsigned long GetCurrentWorldPlaneGeometryUpdateTime()
Get timestamp of last call of SetCurrentWorldPlaneGeometry.
virtual void ApplyContourModelSetProperties(BaseRenderer *renderer)
vtkSmartPointer< vtkAssembly > m_Assembly
Assembly of contours.
itk::TimeStamp m_LastUpdateTime
Timestamp of last update of stored data.
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
int GetNumberOfVertices(int timestep=0) const
Returns the number of vertices at a given timestep.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.