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