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