Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkSplineVtkMapper3D.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 "mitkSplineVtkMapper3D.h"
14 #include <mitkPointSet.h>
15 #include <mitkProperties.h>
16 #include <vtkActor.h>
17 #include <vtkCardinalSpline.h>
18 #include <vtkCellArray.h>
19 #include <vtkPoints.h>
20 #include <vtkPolyData.h>
21 #include <vtkPolyDataMapper.h>
22 #include <vtkProp.h>
23 #include <vtkPropAssembly.h>
24 #include <vtkPropCollection.h>
25 #include <vtkProperty.h>
26 #include <vtkTubeFilter.h>
27 
28 mitk::SplineVtkMapper3D::SplineVtkMapper3D() : m_SplinesAvailable(false), m_SplinesAddedToAssembly(false)
29 {
30  m_SplinesActor = vtkActor::New();
31  m_SplineAssembly = vtkPropAssembly::New();
32  m_SplineResolution = 500;
33 }
34 
36 {
37  m_SplinesActor->Delete();
38  m_SplineAssembly->Delete();
39 }
40 
42 {
43  return m_SplineAssembly;
44 }
45 
47 {
48  vtkLinearTransform *vtktransform = this->GetDataNode()->GetVtkTransform(this->GetTimestep());
49 
50  m_SplinesActor->SetUserTransform(vtktransform);
51 }
52 
54 {
56 
57  // only update spline if UpdateSpline has not been called from
58  // external, e.g. by the SplineMapper2D. But call it the first time when m_SplineUpdateTime = 0 and m_LastUpdateTime =
59  // 0.
60  if (m_SplineUpdateTime < ls->GetLastGenerateDataTime() || m_SplineUpdateTime == 0)
61  {
62  this->UpdateSpline();
63  this->ApplyAllProperties(renderer, m_SplinesActor);
64  }
65 
67  {
69  {
72  }
73  }
74  else
75  {
77  {
78  m_SplineAssembly->RemovePart(m_SplinesActor);
80  }
81  }
82 
83  bool visible = true;
84  GetDataNode()->GetVisibility(visible, renderer, "visible");
85 
86  if (!visible)
87  {
88  m_SplinesActor->VisibilityOff();
89  m_SplineAssembly->VisibilityOff();
90  }
91  else
92  {
93  m_SplinesActor->VisibilityOn();
94  m_SplineAssembly->VisibilityOn();
95 
96  // remove the PointsAssembly if it was added in superclass. No need to display points and spline!
97  if (m_SplineAssembly->GetParts()->IsItemPresent(m_PointsAssembly))
99  }
100  // if the properties have been changed, then refresh the properties
101  if ((m_SplineUpdateTime < this->m_DataNode->GetPropertyList()->GetMTime()) ||
102  (m_SplineUpdateTime < this->m_DataNode->GetPropertyList(renderer)->GetMTime()))
103  this->ApplyAllProperties(renderer, m_SplinesActor);
104 }
105 
107 {
108  Superclass::ApplyColorAndOpacityProperties(renderer, actor);
109 
110  // vtk changed the type of rgba during releases. Due to that, the following convert is done
111  double rgba[4] = {1.0f, 1.0f, 1.0f, 1.0f}; // white
112 
113  // getting the color from DataNode
114  float temprgba[4];
115  this->GetDataNode()->GetColor(&temprgba[0], nullptr);
116  // convert to rgba, what ever type it has!
117  rgba[0] = temprgba[0];
118  rgba[1] = temprgba[1];
119  rgba[2] = temprgba[2];
120  rgba[3] = temprgba[3];
121  // finaly set the color inside the actor
122  m_SplinesActor->GetProperty()->SetColor(rgba);
123 
124  float lineWidth;
125  if (dynamic_cast<mitk::IntProperty *>(this->GetDataNode()->GetProperty("line width")) == nullptr)
126  lineWidth = 1.0;
127  else
128  lineWidth = dynamic_cast<mitk::IntProperty *>(this->GetDataNode()->GetProperty("line width"))->GetValue();
129  m_SplinesActor->GetProperty()->SetLineWidth(lineWidth);
130 
131  m_SplineUpdateTime.Modified();
132 }
133 
135 {
136  return m_SplinesAvailable;
137 }
138 
140 {
141  if (m_SplinesAvailable)
142  return (dynamic_cast<vtkPolyDataMapper *>(m_SplinesActor->GetMapper()))->GetInput();
143  else
144  return nullptr;
145 }
146 
148 {
149  if (m_SplinesAvailable)
150  return m_SplinesActor;
151  else
152  return vtkActor::New();
153 }
154 
156 {
157  auto input = this->GetInput();
158  // input->Update();//already done in superclass
159 
160  // Number of points on the spline
161  unsigned int numberOfOutputPoints = m_SplineResolution;
162  unsigned int numberOfInputPoints = input->GetSize();
163 
164  if (numberOfInputPoints >= 2)
165  {
166  m_SplinesAvailable = true;
167  vtkCardinalSpline *splineX = vtkCardinalSpline::New();
168  vtkCardinalSpline *splineY = vtkCardinalSpline::New();
169  vtkCardinalSpline *splineZ = vtkCardinalSpline::New();
170  unsigned int index = 0;
171  mitk::PointSet::DataType::PointsContainer::Pointer pointsContainer = input->GetPointSet()->GetPoints();
172  for (mitk::PointSet::DataType::PointsContainer::Iterator it = pointsContainer->Begin();
173  it != pointsContainer->End();
174  ++it, ++index)
175  // for ( unsigned int i = 0 ; i < numberOfInputPoints; ++i )
176  {
177  mitk::PointSet::PointType point = it->Value();
178  splineX->AddPoint(index, point[0]);
179  splineY->AddPoint(index, point[1]);
180  splineZ->AddPoint(index, point[2]);
181  }
182  vtkPoints *points = vtkPoints::New();
183  vtkPolyData *profileData = vtkPolyData::New();
184 
185  // Interpolate x, y and z by using the three spline filters and
186  // create new points
187  double t = 0.0f;
188  for (unsigned int i = 0; i < numberOfOutputPoints; ++i)
189  {
190  t = ((((double)numberOfInputPoints) - 1.0f) / (((double)numberOfOutputPoints) - 1.0f)) * ((double)i);
191  points->InsertPoint(i, splineX->Evaluate(t), splineY->Evaluate(t), splineZ->Evaluate(t));
192  }
193 
194  // Create the polyline.
195  vtkCellArray *lines = vtkCellArray::New();
196  lines->InsertNextCell(numberOfOutputPoints);
197  for (unsigned int i = 0; i < numberOfOutputPoints; ++i)
198  lines->InsertCellPoint(i);
199 
200  profileData->SetPoints(points);
201  profileData->SetLines(lines);
202 
203  // Add thickness to the resulting line.
204  // vtkTubeFilter* profileTubes = vtkTubeFilter::New();
205  // profileTubes->SetNumberOfSides(8);
206  // profileTubes->SetInput(profileData);
207  // profileTubes->SetRadius(.005);
208 
209  vtkPolyDataMapper *profileMapper = vtkPolyDataMapper::New();
210  profileMapper->SetInputData(profileData);
211 
212  m_SplinesActor->SetMapper(profileMapper);
213  }
214  else
215  {
216  m_SplinesAvailable = false;
217  }
218  m_SplineUpdateTime.Modified();
219 }
#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...
vtkSmartPointer< vtkPropAssembly > m_PointsAssembly
L * GetLocalStorage(mitk::BaseRenderer *forRenderer)
Retrieves a LocalStorage for a specific BaseRenderer.
Base class for mapper specific rendering ressources.
Definition: mitkMapper.h:193
LocalStorageHandler< BaseLocalStorage > m_LSH
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
vtkPropAssembly * m_SplineAssembly
Organizes the rendering process.
virtual const mitk::PointSet * GetInput()
bool GetColor(float rgb[3], const mitk::BaseRenderer *renderer=nullptr, const char *propertyKey="color") const
Convenience access method for color properties (instances of ColorProperty)
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.
void GenerateDataForRenderer(mitk::BaseRenderer *renderer) override
Generate the data needed for rendering into renderer.
void ApplyAllProperties(BaseRenderer *renderer, vtkActor *actor) override
mitk::PropertyList * GetPropertyList(const mitk::BaseRenderer *renderer=nullptr) const
Get the PropertyList of the renderer. If renderer is nullptr, the BaseRenderer-independent PropertyLi...
int GetTimestep() const
Returns the current time step as calculated from the renderer.
Definition: mitkMapper.h:147
void UpdateVtkTransform(mitk::BaseRenderer *renderer) override
Set the vtkTransform of the m_Prop3D for the current time step of renderer.
vtkProp * GetVtkProp(mitk::BaseRenderer *renderer) override