Medical Imaging Interaction Toolkit  2018.4.99-c0f884b2
Medical Imaging Interaction Toolkit
mitkContourModelMapper3D.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 <vtkCellArray.h>
15 #include <vtkPoints.h>
16 #include <vtkProperty.h>
17 
19 {
20 }
21 
23 {
24 }
25 
27 {
28  // convient way to get the data from the dataNode
29  return static_cast<const mitk::ContourModel *>(GetDataNode()->GetData());
30 }
31 
33 {
34  // return the actor corresponding to the renderer
35  return m_LSH.GetLocalStorage(renderer)->m_Actor;
36 }
37 
39 {
40  /* First convert the contourModel to vtkPolyData, then tube filter it and
41  * set it input for our mapper
42  */
43 
44  LocalStorage *localStorage = m_LSH.GetLocalStorage(renderer);
45 
46  auto *inputContour = static_cast<mitk::ContourModel *>(GetDataNode()->GetData());
47 
48  localStorage->m_OutlinePolyData = this->CreateVtkPolyDataFromContour(inputContour);
49 
50  this->ApplyContourProperties(renderer);
51 
52  // tube filter the polyData
53  localStorage->m_TubeFilter->SetInputData(localStorage->m_OutlinePolyData);
54 
55  float lineWidth(1.0);
56  if (this->GetDataNode()->GetFloatProperty("contour.3D.width", lineWidth, renderer))
57  {
58  localStorage->m_TubeFilter->SetRadius(lineWidth);
59  }
60  else
61  {
62  localStorage->m_TubeFilter->SetRadius(0.5);
63  }
64  localStorage->m_TubeFilter->CappingOn();
65  localStorage->m_TubeFilter->SetNumberOfSides(10);
66  localStorage->m_TubeFilter->Update();
67  localStorage->m_Mapper->SetInputConnection(localStorage->m_TubeFilter->GetOutputPort());
68 }
69 
71 {
72  bool visible = true;
73  GetDataNode()->GetVisibility(visible, renderer, "visible");
74 
75  auto *data = static_cast<mitk::ContourModel *>(GetDataNode()->GetData());
76  if (data == nullptr)
77  {
78  return;
79  }
80 
81  // Calculate time step of the input data for the specified renderer (integer value)
82  this->CalculateTimeStep(renderer);
83 
84  LocalStorage *localStorage = m_LSH.GetLocalStorage(renderer);
85  // Check if time step is valid
86  const TimeGeometry *dataTimeGeometry = data->GetTimeGeometry();
87  if ((dataTimeGeometry == nullptr) || (dataTimeGeometry->CountTimeSteps() == 0) ||
88  (!dataTimeGeometry->IsValidTimeStep(renderer->GetTimeStep())) || (this->GetTimestep() == -1))
89  {
90  // clear the rendered polydata
91  localStorage->m_Mapper->SetInputData(vtkSmartPointer<vtkPolyData>::New());
92  return;
93  }
94 
95  const DataNode *node = this->GetDataNode();
96  data->UpdateOutputInformation();
97 
98  // check if something important has changed and we need to rerender
99  if ((localStorage->m_LastUpdateTime < node->GetMTime()) // was the node modified?
100  ||
101  (localStorage->m_LastUpdateTime < data->GetPipelineMTime()) // Was the data modified?
102  ||
103  (localStorage->m_LastUpdateTime <
104  renderer->GetCurrentWorldPlaneGeometryUpdateTime()) // was the geometry modified?
105  ||
106  (localStorage->m_LastUpdateTime < renderer->GetCurrentWorldPlaneGeometry()->GetMTime()) ||
107  (localStorage->m_LastUpdateTime < node->GetPropertyList()->GetMTime()) // was a property modified?
108  ||
109  (localStorage->m_LastUpdateTime < node->GetPropertyList(renderer)->GetMTime()))
110  {
111  this->GenerateDataForRenderer(renderer);
112  }
113 
114  // since we have checked that nothing important has changed, we can set
115  // m_LastUpdateTime to the current time
116  localStorage->m_LastUpdateTime.Modified();
117 }
118 
120 {
121  unsigned int timestep = this->GetTimestep();
122 
123  // the points to draw
124  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
125  // the lines to connect the points
126  vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
127  // Create a polydata to store everything in
128  vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
129 
130  // iterate over the control points
131  auto current = inputContour->IteratorBegin(timestep);
132  auto next = inputContour->IteratorBegin(timestep);
133  if (next != inputContour->IteratorEnd(timestep))
134  {
135  next++;
136 
137  auto end = inputContour->IteratorEnd(timestep);
138 
139  while (next != end)
140  {
141  mitk::ContourModel::VertexType *currentControlPoint = *current;
142  mitk::ContourModel::VertexType *nextControlPoint = *next;
143 
144  if (!(currentControlPoint->Coordinates[0] == nextControlPoint->Coordinates[0] &&
145  currentControlPoint->Coordinates[1] == nextControlPoint->Coordinates[1] &&
146  currentControlPoint->Coordinates[2] == nextControlPoint->Coordinates[2]))
147  {
148  vtkIdType p1 = points->InsertNextPoint(currentControlPoint->Coordinates[0],
149  currentControlPoint->Coordinates[1],
150  currentControlPoint->Coordinates[2]);
151  vtkIdType p2 = points->InsertNextPoint(
152  nextControlPoint->Coordinates[0], nextControlPoint->Coordinates[1], nextControlPoint->Coordinates[2]);
153  // add the line between both contorlPoints
154  lines->InsertNextCell(2);
155  lines->InsertCellPoint(p1);
156  lines->InsertCellPoint(p2);
157  }
158  current++;
159  next++;
160  }
161 
162  if (inputContour->IsClosed(timestep))
163  {
164  // If the contour is closed add a line from the last to the first control point
165  mitk::ContourModel::VertexType *firstControlPoint = *(inputContour->IteratorBegin(timestep));
166  mitk::ContourModel::VertexType *lastControlPoint = *(--(inputContour->IteratorEnd(timestep)));
167  if (lastControlPoint->Coordinates[0] != firstControlPoint->Coordinates[0] ||
168  lastControlPoint->Coordinates[1] != firstControlPoint->Coordinates[1] ||
169  lastControlPoint->Coordinates[2] != firstControlPoint->Coordinates[2])
170  {
171  vtkIdType p2 = points->InsertNextPoint(
172  lastControlPoint->Coordinates[0], lastControlPoint->Coordinates[1], lastControlPoint->Coordinates[2]);
173  vtkIdType p1 = points->InsertNextPoint(
174  firstControlPoint->Coordinates[0], firstControlPoint->Coordinates[1], firstControlPoint->Coordinates[2]);
175 
176  // add the line to the cellArray
177  lines->InsertNextCell(2);
178  lines->InsertCellPoint(p1);
179  lines->InsertCellPoint(p2);
180  }
181  }
182 
183  // Add the points to the dataset
184  polyData->SetPoints(points);
185  // Add the lines to the dataset
186  polyData->SetLines(lines);
187  }
188  return polyData;
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  localStorage->m_Actor->GetProperty()->SetColor(red, green, blue);
204  }
205 }
206 
207 /*+++++++++++++++++++ LocalStorage part +++++++++++++++++++++++++*/
208 
210 {
211  return m_LSH.GetLocalStorage(renderer);
212 }
213 
215 {
216  m_Mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
217  m_Actor = vtkSmartPointer<vtkActor>::New();
218  m_OutlinePolyData = vtkSmartPointer<vtkPolyData>::New();
219  m_TubeFilter = vtkSmartPointer<vtkTubeFilter>::New();
220 
221  // set the mapper for the actor
222  m_Actor->SetMapper(m_Mapper);
223 }
224 
226  mitk::BaseRenderer *renderer,
227  bool overwrite)
228 {
229  node->AddProperty("color", ColorProperty::New(1.0, 0.0, 0.0), renderer, overwrite);
230  node->AddProperty("contour.3D.width", mitk::FloatProperty::New(0.5), renderer, overwrite);
231 
232  Superclass::SetDefaultProperties(node, renderer, overwrite);
233 }
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...
LocalStorage()
Default constructor of the local storage.
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...
virtual TimeStepType CountTimeSteps() const =0
Returns the number of time steps.
mitk::Point3D Coordinates
Coordinates in 3D space.
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()
Organizes the rendering process.
vtkSmartPointer< vtkTubeFilter > m_TubeFilter
virtual const PlaneGeometry * GetCurrentWorldPlaneGeometry()
Get the current 2D-worldgeometry (m_CurrentWorldPlaneGeometry) used for 2D-rendering.
virtual void ApplyContourProperties(mitk::BaseRenderer *renderer)
vtkSmartPointer< vtkPolyDataMapper > m_Mapper
Mapper of a 2D render window.
unsigned long GetMTime() const override
Get the timestamp of the last change of the contents of this node or the referenced BaseData...
void Update(mitk::BaseRenderer *renderer) override
Checks whether this mapper needs to update itself and generate data.
virtual vtkSmartPointer< vtkPolyData > CreateVtkPolyDataFromContour(mitk::ContourModel *inputContour)
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 ...
virtual unsigned int GetTimeStep() const
LocalStorage * GetLocalStorage(mitk::BaseRenderer *renderer)
Get the LocalStorage corresponding to the current renderer.
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
VertexIterator IteratorEnd(int timestep=0) const
Returns a const VertexIterator at the end element of the contour.
virtual void CalculateTimeStep(BaseRenderer *renderer)
Updates the time step, which is sometimes needed in subclasses.
Definition: mitkMapper.cpp:79
bool IsClosed(int timestep=0) const
Return if the contour is closed or not.
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...
vtkSmartPointer< vtkPolyData > m_OutlinePolyData
const mitk::ContourModel * GetInput(void)
int GetTimestep() const
Returns the current time step as calculated from the renderer.
Definition: mitkMapper.h:147
vtkProp * GetVtkProp(mitk::BaseRenderer *renderer) override
itk::TimeStamp m_LastUpdateTime
Timestamp of last update of stored data.
unsigned long GetCurrentWorldPlaneGeometryUpdateTime()
Get timestamp of last call of SetCurrentWorldPlaneGeometry.
VertexIterator IteratorBegin(int timestep=0) const
Returns a const VertexIterator at the start 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.
mitk::LocalStorageHandler< LocalStorage > m_LSH
The LocalStorageHandler holds all (three) LocalStorages for the three 2D render windows.
Represents a single vertex of contour.
vtkSmartPointer< vtkActor > m_Actor
Actor of a 2D render window.
virtual bool IsValidTimeStep(TimeStepType timeStep) const =0
Test for the given time step if a geometry is availible.
void GenerateDataForRenderer(mitk::BaseRenderer *renderer) override
Generate the data needed for rendering into renderer.
Class for nodes of the DataTree.
Definition: mitkDataNode.h:57