Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkContourModelSetGLMapper2D.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 
18 
19 #include "mitkColorProperty.h"
20 #include "mitkContourModelSet.h"
21 #include "mitkPlaneGeometry.h"
22 #include "mitkProperties.h"
23 #include <vtkLinearTransform.h>
24 
25 #include "mitkGL.h"
26 
28 {
29 }
30 
32 {
33 }
34 
36 {
37  BaseLocalStorage *ls = m_LSH.GetLocalStorage(renderer);
38 
39  mitk::DataNode *dataNode = this->GetDataNode();
40  bool visible = true;
41  dataNode->GetVisibility(visible, renderer, "visible");
42 
43  if (!visible)
44  return;
45 
46  mitk::ContourModelSet *input = this->GetInput();
47 
49 
51 
52  while (it != end)
53  {
54  this->DrawContour(it->GetPointer(), renderer);
55  ++it;
56  }
57 
58  if (input->GetSize() < 1)
59  return;
60 
62 }
63 
64 mitk::ContourModelSet *mitk::ContourModelSetGLMapper2D::GetInput(void)
65 {
66  return const_cast<mitk::ContourModelSet *>(static_cast<const mitk::ContourModelSet *>(GetDataNode()->GetData()));
67 }
68 
70  mitk::BaseRenderer *renderer)
71 {
72  if (!renderingContour)
73  return;
74 
75  mitk::DataNode *dataNode = this->GetDataNode();
76 
77  renderingContour->UpdateOutputInformation();
78 
79  unsigned int timestep = renderer->GetTimeStep();
80 
81  if (!renderingContour->IsEmptyTimeStep(timestep))
82  {
83  // apply color and opacity read from the PropertyList
84  ApplyColorAndOpacityProperties(renderer);
85 
87  dynamic_cast<mitk::ColorProperty *>(dataNode->GetProperty("contour.color", renderer));
88  float opacity = 0.5;
89  dataNode->GetFloatProperty("opacity", opacity, renderer);
90 
91  if (colorprop)
92  {
93  // set the color of the contour
94  double red = colorprop->GetColor().GetRed();
95  double green = colorprop->GetColor().GetGreen();
96  double blue = colorprop->GetColor().GetBlue();
97  glColor4f(red, green, blue, opacity);
98  }
99 
100  mitk::ColorProperty::Pointer selectedcolor =
101  dynamic_cast<mitk::ColorProperty *>(dataNode->GetProperty("contour.points.color", renderer));
102  if (!selectedcolor)
103  {
104  selectedcolor = mitk::ColorProperty::New(1.0, 0.0, 0.1);
105  }
106 
107  vtkLinearTransform *transform = dataNode->GetVtkTransform();
108 
109  // ContourModel::OutputType point;
110  mitk::Point3D point;
111 
112  mitk::Point3D p;
113  float vtkp[3];
114  float lineWidth = 3.0;
115 
116  bool drawit = false;
117 
118  bool isHovering = false;
119  dataNode->GetBoolProperty("contour.hovering", isHovering);
120 
121  if (isHovering)
122  dataNode->GetFloatProperty("contour.hovering.width", lineWidth);
123  else
124  dataNode->GetFloatProperty("contour.width", lineWidth);
125 
126  bool showSegments = false;
127  dataNode->GetBoolProperty("contour.segments.show", showSegments);
128 
129  bool showControlPoints = false;
130  dataNode->GetBoolProperty("contour.controlpoints.show", showControlPoints);
131 
132  bool showPoints = false;
133  dataNode->GetBoolProperty("contour.points.show", showPoints);
134 
135  bool showPointsNumbers = false;
136  dataNode->GetBoolProperty("contour.points.text", showPointsNumbers);
137 
138  bool showControlPointsNumbers = false;
139  dataNode->GetBoolProperty("contour.controlpoints.text", showControlPointsNumbers);
140 
141  bool projectmode = false;
142  dataNode->GetVisibility(projectmode, renderer, "contour.project-onto-plane");
143 
144  mitk::ContourModel::VertexIterator pointsIt = renderingContour->IteratorBegin(timestep);
145 
146  Point2D pt2d; // projected_p in display coordinates
147  Point2D lastPt2d;
148 
149  int index = 0;
150 
151  mitk::ScalarType maxDiff = 0.25;
152 
153  while (pointsIt != renderingContour->IteratorEnd(timestep))
154  {
155  lastPt2d = pt2d;
156 
157  point = (*pointsIt)->Coordinates;
158 
159  itk2vtk(point, vtkp);
160  transform->TransformPoint(vtkp, vtkp);
161  vtk2itk(vtkp, p);
162 
163  renderer->WorldToDisplay(p, pt2d);
164 
165  ScalarType scalardiff = fabs(renderer->GetCurrentWorldPlaneGeometry()->SignedDistance(p));
166 
167  // project to plane
168  if (projectmode)
169  {
170  drawit = true;
171  }
172  else if (scalardiff < maxDiff) // point is close enough to be drawn
173  {
174  drawit = true;
175  }
176  else
177  {
178  drawit = false;
179  }
180 
181  // draw line
182  if (drawit)
183  {
184  if (showSegments)
185  {
186  // lastPt2d is not valid in first step
187  if (!(pointsIt == renderingContour->IteratorBegin(timestep)))
188  {
189  glLineWidth(lineWidth);
190  glBegin(GL_LINES);
191  glVertex2f(pt2d[0], pt2d[1]);
192  glVertex2f(lastPt2d[0], lastPt2d[1]);
193  glEnd();
194  glLineWidth(1);
195  }
196  }
197 
198  if (showControlPoints)
199  {
200  // draw ontrol points
201  if ((*pointsIt)->IsControlPoint)
202  {
203  float pointsize = 4;
204  Point2D tmp;
205 
206  Vector2D horz, vert;
207  horz[1] = 0;
208  vert[0] = 0;
209  horz[0] = pointsize;
210  vert[1] = pointsize;
211  glColor3f(selectedcolor->GetColor().GetRed(),
212  selectedcolor->GetColor().GetBlue(),
213  selectedcolor->GetColor().GetGreen());
214  glLineWidth(1);
215  // a rectangle around the point with the selected color
216  glBegin(GL_LINE_LOOP);
217  tmp = pt2d - horz;
218  glVertex2dv(&tmp[0]);
219  tmp = pt2d + vert;
220  glVertex2dv(&tmp[0]);
221  tmp = pt2d + horz;
222  glVertex2dv(&tmp[0]);
223  tmp = pt2d - vert;
224  glVertex2dv(&tmp[0]);
225  glEnd();
226  glLineWidth(1);
227  // the actual point in the specified color to see the usual color of the point
228  glColor3f(
229  colorprop->GetColor().GetRed(), colorprop->GetColor().GetGreen(), colorprop->GetColor().GetBlue());
230  glPointSize(1);
231  glBegin(GL_POINTS);
232  tmp = pt2d;
233  glVertex2dv(&tmp[0]);
234  glEnd();
235  }
236  }
237 
238  if (showPoints)
239  {
240  float pointsize = 3;
241  Point2D tmp;
242 
243  Vector2D horz, vert;
244  horz[1] = 0;
245  vert[0] = 0;
246  horz[0] = pointsize;
247  vert[1] = pointsize;
248  glColor3f(0.0, 0.0, 0.0);
249  glLineWidth(1);
250  // a rectangle around the point with the selected color
251  glBegin(GL_LINE_LOOP);
252  tmp = pt2d - horz;
253  glVertex2dv(&tmp[0]);
254  tmp = pt2d + vert;
255  glVertex2dv(&tmp[0]);
256  tmp = pt2d + horz;
257  glVertex2dv(&tmp[0]);
258  tmp = pt2d - vert;
259  glVertex2dv(&tmp[0]);
260  glEnd();
261  glLineWidth(1);
262  // the actual point in the specified color to see the usual color of the point
263  glColor3f(colorprop->GetColor().GetRed(), colorprop->GetColor().GetGreen(), colorprop->GetColor().GetBlue());
264  glPointSize(1);
265  glBegin(GL_POINTS);
266  tmp = pt2d;
267  glVertex2dv(&tmp[0]);
268  glEnd();
269  }
270 
271  if (showPointsNumbers)
272  {
273  std::string l;
274  std::stringstream ss;
275  ss << index;
276  l.append(ss.str());
277 
278  float rgb[3];
279  rgb[0] = 0.0;
280  rgb[1] = 0.0;
281  rgb[2] = 0.0;
282 
283  WriteTextWithOverlay(m_PointNumbersOverlay, l.c_str(), rgb, pt2d, renderer);
284  }
285 
286  if (showControlPointsNumbers && (*pointsIt)->IsControlPoint)
287  {
288  std::string l;
289  std::stringstream ss;
290  ss << index;
291  l.append(ss.str());
292 
293  float rgb[3];
294  rgb[0] = 1.0;
295  rgb[1] = 1.0;
296  rgb[2] = 0.0;
297 
298  WriteTextWithOverlay(m_ControlPointNumbersOverlay, l.c_str(), rgb, pt2d, renderer);
299  }
300 
301  index++;
302  }
303 
304  pointsIt++;
305  } // end while iterate over controlpoints
306 
307  // close contour if necessary
308  if (renderingContour->IsClosed(timestep) && drawit && showSegments)
309  {
310  lastPt2d = pt2d;
311  point = renderingContour->GetVertexAt(0, timestep)->Coordinates;
312  itk2vtk(point, vtkp);
313  transform->TransformPoint(vtkp, vtkp);
314  vtk2itk(vtkp, p);
315  renderer->WorldToDisplay(p, pt2d);
316 
317  glLineWidth(lineWidth);
318  glBegin(GL_LINES);
319  glVertex2f(lastPt2d[0], lastPt2d[1]);
320  glVertex2f(pt2d[0], pt2d[1]);
321  glEnd();
322  glLineWidth(1);
323  }
324 
325  // draw selected vertex if exists
326  if (renderingContour->GetSelectedVertex())
327  {
328  // transform selected vertex
329  point = renderingContour->GetSelectedVertex()->Coordinates;
330 
331  itk2vtk(point, vtkp);
332  transform->TransformPoint(vtkp, vtkp);
333  vtk2itk(vtkp, p);
334 
335  renderer->WorldToDisplay(p, pt2d);
336 
337  ScalarType scalardiff = fabs(renderer->GetCurrentWorldPlaneGeometry()->SignedDistance(p));
338  //----------------------------------
339 
340  // draw point if close to plane
341  if (scalardiff < maxDiff)
342  {
343  float pointsize = 5;
344  Point2D tmp;
345  glColor3f(0.0, 1.0, 0.0);
346  glLineWidth(1);
347  // a diamond around the point
348  glBegin(GL_LINE_LOOP);
349  // begin from upper left corner and paint clockwise
350  tmp[0] = pt2d[0] - pointsize;
351  tmp[1] = pt2d[1] + pointsize;
352  glVertex2dv(&tmp[0]);
353  tmp[0] = pt2d[0] + pointsize;
354  tmp[1] = pt2d[1] + pointsize;
355  glVertex2dv(&tmp[0]);
356  tmp[0] = pt2d[0] + pointsize;
357  tmp[1] = pt2d[1] - pointsize;
358  glVertex2dv(&tmp[0]);
359  tmp[0] = pt2d[0] - pointsize;
360  tmp[1] = pt2d[1] - pointsize;
361  glVertex2dv(&tmp[0]);
362  glEnd();
363  }
364  //------------------------------------
365  }
366  }
367 }
368 
370  mitk::BaseRenderer *renderer,
371  bool overwrite)
372 {
373  node->AddProperty("contour.color", ColorProperty::New(0.9, 1.0, 0.1), renderer, overwrite);
374  node->AddProperty("contour.points.color", ColorProperty::New(1.0, 0.0, 0.1), renderer, overwrite);
375  node->AddProperty("contour.points.show", mitk::BoolProperty::New(false), renderer, overwrite);
376  node->AddProperty("contour.segments.show", mitk::BoolProperty::New(true), renderer, overwrite);
377  node->AddProperty("contour.controlpoints.show", mitk::BoolProperty::New(false), renderer, overwrite);
378  node->AddProperty("contour.width", mitk::FloatProperty::New(1.0), renderer, overwrite);
379  node->AddProperty("contour.hovering.width", mitk::FloatProperty::New(3.0), renderer, overwrite);
380  node->AddProperty("contour.hovering", mitk::BoolProperty::New(false), renderer, overwrite);
381  node->AddProperty("contour.points.text", mitk::BoolProperty::New(false), renderer, overwrite);
382  node->AddProperty("contour.controlpoints.text", mitk::BoolProperty::New(false), renderer, overwrite);
383 
384  node->AddProperty("contour.project-onto-plane", mitk::BoolProperty::New(false), renderer, overwrite);
385 
386  node->AddProperty("opacity", mitk::FloatProperty::New(1.0f), renderer, overwrite);
387 
388  Superclass::SetDefaultProperties(node, renderer, overwrite);
389 }
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
Base class for mapper specific rendering ressources.
Definition: mitkMapper.h:200
virtual void UpdateOutputInformation() override
Update the OutputInformation of a ContourModel object.
mitk::Point3D Coordinates
Coordinates in 3D space.
double ScalarType
static Pointer New()
Organizes the rendering process.
virtual ScalarType SignedDistance(const Point3D &pt3d_mm) const
bool GetBoolProperty(const char *propertyKey, bool &boolValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for bool properties (instances of BoolProperty)
mitk::ContourElement::VertexIterator VertexIterator
virtual const PlaneGeometry * GetCurrentWorldPlaneGeometry()
Get the current 2D-worldgeometry (m_CurrentWorldPlaneGeometry) used for 2D-rendering.
virtual ContourModelSetIterator Begin()
Return an iterator a the front.
virtual ContourModelSetIterator End()
Return an iterator a the front.
VertexIterator IteratorBegin(int timestep=0) const
Returns a const VertexIterator at the start element of the contour.
mitk::BaseProperty * GetProperty(const char *propertyKey, const mitk::BaseRenderer *renderer=nullptr) const
Get the property (instance of BaseProperty) with key propertyKey from the PropertyList of the rendere...
virtual int GetSize() const
Returns the number of contained contours.
ContourModelListType::iterator ContourModelSetIterator
static void SetDefaultProperties(mitk::DataNode *node, mitk::BaseRenderer *renderer=NULL, bool overwrite=false)
static Pointer New()
The ColorProperty class RGB color property.
VertexType * GetSelectedVertex()
Get the current selected vertex.
void WorldToDisplay(const Point3D &worldIndex, Point2D &displayPoint) const
This method converts a 3D world index to the display point using the geometry of the renderWindow...
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 bool IsEmptyTimeStep(unsigned int t) const override
Check if there isn't something at this timestep.
bool GetFloatProperty(const char *propertyKey, float &floatValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for float properties (instances of FloatProperty)
void InternalDrawContour(mitk::ContourModel *contour, mitk::BaseRenderer *renderer) override
void vtk2itk(const Tin &in, Tout &out)
vtkLinearTransform * GetVtkTransform(int t=0) const
Get the transformation applied prior to displaying the data as a vtkTransform.
virtual unsigned int GetTimeStep() const
static Pointer New()
void itk2vtk(const Tin &in, Tout &out)
bool IsClosed(int timestep=0) const
Return if the contour is closed or not.
virtual void Paint(BaseRenderer *renderer) override
VertexIterator IteratorEnd(int timestep=0) const
Returns a const VertexIterator at the end element of the contour.
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:413
Class for nodes of the DataTree.
Definition: mitkDataNode.h:66
virtual const VertexType * GetVertexAt(int index, int timestep=0) const
Returns the vertex at the index position within the container.