Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
mitkContourModelGLMapper2DBase.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 "mitkBaseRenderer.h"
26 #include "mitkContourModel.h"
27 #include "mitkOverlayManager.h"
28 #include "mitkTextOverlay2D.h"
29 
30 #include "mitkGL.h"
31 
33 {
36 }
37 
39 {
40 }
41 
43 {
44  if (std::find(m_RendererList.begin(), m_RendererList.end(), renderer) == m_RendererList.end())
45  {
46  m_RendererList.push_back(renderer);
47  }
48 
49  renderer->GetOverlayManager()->AddOverlay(m_PointNumbersOverlay.GetPointer(), renderer);
50  m_PointNumbersOverlay->SetVisibility(false, renderer);
51 
52  renderer->GetOverlayManager()->AddOverlay(m_ControlPointNumbersOverlay.GetPointer(), renderer);
53  m_ControlPointNumbersOverlay->SetVisibility(false, renderer);
54 
55  InternalDrawContour(renderingContour, renderer);
56 }
57 
59  mitk::BaseRenderer *renderer)
60 {
61  if (!renderingContour)
62  return;
63 
64  mitk::DataNode *dataNode = this->GetDataNode();
65 
66  renderingContour->UpdateOutputInformation();
67 
68  unsigned int timestep = renderer->GetTimeStep();
69 
70  if (!renderingContour->IsEmptyTimeStep(timestep))
71  {
72  // apply color and opacity read from the PropertyList
73  ApplyColorAndOpacityProperties(renderer);
74 
76  dynamic_cast<mitk::ColorProperty *>(dataNode->GetProperty("contour.color", renderer));
77  float opacity = 0.5;
78  dataNode->GetFloatProperty("opacity", opacity, renderer);
79 
80  if (colorprop)
81  {
82  // set the color of the contour
83  double red = colorprop->GetColor().GetRed();
84  double green = colorprop->GetColor().GetGreen();
85  double blue = colorprop->GetColor().GetBlue();
86  glColor4f(red, green, blue, opacity);
87  }
88 
89  mitk::ColorProperty::Pointer selectedcolor =
90  dynamic_cast<mitk::ColorProperty *>(dataNode->GetProperty("contour.points.color", renderer));
91  if (!selectedcolor)
92  {
93  selectedcolor = mitk::ColorProperty::New(1.0, 0.0, 0.1);
94  }
95 
96  vtkLinearTransform *transform = dataNode->GetVtkTransform();
97 
98  // ContourModel::OutputType point;
99  mitk::Point3D point;
100 
101  mitk::Point3D p;
102  float vtkp[3];
103  float lineWidth = 3.0;
104 
105  bool drawit = false;
106 
107  bool isHovering = false;
108  dataNode->GetBoolProperty("contour.hovering", isHovering);
109 
110  if (isHovering)
111  dataNode->GetFloatProperty("contour.hovering.width", lineWidth);
112  else
113  dataNode->GetFloatProperty("contour.width", lineWidth);
114 
115  bool showSegments = false;
116  dataNode->GetBoolProperty("contour.segments.show", showSegments);
117 
118  bool showControlPoints = false;
119  dataNode->GetBoolProperty("contour.controlpoints.show", showControlPoints);
120 
121  bool showPoints = false;
122  dataNode->GetBoolProperty("contour.points.show", showPoints);
123 
124  bool showPointsNumbers = false;
125  dataNode->GetBoolProperty("contour.points.text", showPointsNumbers);
126 
127  bool showControlPointsNumbers = false;
128  dataNode->GetBoolProperty("contour.controlpoints.text", showControlPointsNumbers);
129 
130  bool projectmode = false;
131  dataNode->GetVisibility(projectmode, renderer, "contour.project-onto-plane");
132 
133  mitk::ContourModel::VertexIterator pointsIt = renderingContour->IteratorBegin(timestep);
134 
135  Point2D pt2d; // projected_p in display coordinates
136  Point2D lastPt2d;
137 
138  int index = 0;
139 
140  mitk::ScalarType maxDiff = 0.25;
141 
142  while (pointsIt != renderingContour->IteratorEnd(timestep))
143  {
144  lastPt2d = pt2d;
145 
146  point = (*pointsIt)->Coordinates;
147 
148  itk2vtk(point, vtkp);
149  transform->TransformPoint(vtkp, vtkp);
150  vtk2itk(vtkp, p);
151 
152  renderer->WorldToDisplay(p, pt2d);
153 
154  ScalarType scalardiff = fabs(renderer->GetCurrentWorldPlaneGeometry()->SignedDistance(p));
155 
156  // project to plane
157  if (projectmode)
158  {
159  drawit = true;
160  }
161  else if (scalardiff < maxDiff) // point is close enough to be drawn
162  {
163  drawit = true;
164  }
165  else
166  {
167  drawit = false;
168  }
169 
170  // draw line
171  if (drawit)
172  {
173  if (showSegments)
174  {
175  // lastPt2d is not valid in first step
176  if (!(pointsIt == renderingContour->IteratorBegin(timestep)))
177  {
178  glLineWidth(lineWidth);
179  glBegin(GL_LINES);
180  glVertex2f(pt2d[0], pt2d[1]);
181  glVertex2f(lastPt2d[0], lastPt2d[1]);
182  glEnd();
183  glLineWidth(1);
184  }
185  }
186 
187  if (showControlPoints)
188  {
189  // draw ontrol points
190  if ((*pointsIt)->IsControlPoint)
191  {
192  float pointsize = 4;
193  Point2D tmp;
194 
195  Vector2D horz, vert;
196  horz[1] = 0;
197  vert[0] = 0;
198  horz[0] = pointsize;
199  vert[1] = pointsize;
200  glColor3f(selectedcolor->GetColor().GetRed(),
201  selectedcolor->GetColor().GetBlue(),
202  selectedcolor->GetColor().GetGreen());
203  glLineWidth(1);
204  // a rectangle around the point with the selected color
205  glBegin(GL_LINE_LOOP);
206  tmp = pt2d - horz;
207  glVertex2dv(&tmp[0]);
208  tmp = pt2d + vert;
209  glVertex2dv(&tmp[0]);
210  tmp = pt2d + horz;
211  glVertex2dv(&tmp[0]);
212  tmp = pt2d - vert;
213  glVertex2dv(&tmp[0]);
214  glEnd();
215  glLineWidth(1);
216  // the actual point in the specified color to see the usual color of the point
217  glColor3f(
218  colorprop->GetColor().GetRed(), colorprop->GetColor().GetGreen(), colorprop->GetColor().GetBlue());
219  glPointSize(1);
220  glBegin(GL_POINTS);
221  tmp = pt2d;
222  glVertex2dv(&tmp[0]);
223  glEnd();
224  }
225  }
226 
227  if (showPoints)
228  {
229  float pointsize = 3;
230  Point2D tmp;
231 
232  Vector2D horz, vert;
233  horz[1] = 0;
234  vert[0] = 0;
235  horz[0] = pointsize;
236  vert[1] = pointsize;
237  glColor3f(0.0, 0.0, 0.0);
238  glLineWidth(1);
239  // a rectangle around the point with the selected color
240  glBegin(GL_LINE_LOOP);
241  tmp = pt2d - horz;
242  glVertex2dv(&tmp[0]);
243  tmp = pt2d + vert;
244  glVertex2dv(&tmp[0]);
245  tmp = pt2d + horz;
246  glVertex2dv(&tmp[0]);
247  tmp = pt2d - vert;
248  glVertex2dv(&tmp[0]);
249  glEnd();
250  glLineWidth(1);
251  // the actual point in the specified color to see the usual color of the point
252  glColor3f(colorprop->GetColor().GetRed(), colorprop->GetColor().GetGreen(), colorprop->GetColor().GetBlue());
253  glPointSize(1);
254  glBegin(GL_POINTS);
255  tmp = pt2d;
256  glVertex2dv(&tmp[0]);
257  glEnd();
258  }
259 
260  if (showPointsNumbers)
261  {
262  std::string l;
263  std::stringstream ss;
264  ss << index;
265  l.append(ss.str());
266 
267  float rgb[3];
268  rgb[0] = 0.0;
269  rgb[1] = 0.0;
270  rgb[2] = 0.0;
271 
272  WriteTextWithOverlay(m_PointNumbersOverlay, l.c_str(), rgb, pt2d, renderer);
273  }
274 
275  if (showControlPointsNumbers && (*pointsIt)->IsControlPoint)
276  {
277  std::string l;
278  std::stringstream ss;
279  ss << index;
280  l.append(ss.str());
281 
282  float rgb[3];
283  rgb[0] = 1.0;
284  rgb[1] = 1.0;
285  rgb[2] = 0.0;
286 
287  WriteTextWithOverlay(m_ControlPointNumbersOverlay, l.c_str(), rgb, pt2d, renderer);
288  }
289 
290  index++;
291  }
292 
293  pointsIt++;
294  } // end while iterate over controlpoints
295 
296  // close contour if necessary
297  if (renderingContour->IsClosed(timestep) && drawit && showSegments)
298  {
299  lastPt2d = pt2d;
300  point = renderingContour->GetVertexAt(0, timestep)->Coordinates;
301  itk2vtk(point, vtkp);
302  transform->TransformPoint(vtkp, vtkp);
303  vtk2itk(vtkp, p);
304  renderer->WorldToDisplay(p, pt2d);
305 
306  glLineWidth(lineWidth);
307  glBegin(GL_LINES);
308  glVertex2f(lastPt2d[0], lastPt2d[1]);
309  glVertex2f(pt2d[0], pt2d[1]);
310  glEnd();
311  glLineWidth(1);
312  }
313 
314  // draw selected vertex if exists
315  if (renderingContour->GetSelectedVertex())
316  {
317  // transform selected vertex
318  point = renderingContour->GetSelectedVertex()->Coordinates;
319 
320  itk2vtk(point, vtkp);
321  transform->TransformPoint(vtkp, vtkp);
322  vtk2itk(vtkp, p);
323 
324  renderer->WorldToDisplay(p, pt2d);
325 
326  ScalarType scalardiff = fabs(renderer->GetCurrentWorldPlaneGeometry()->SignedDistance(p));
327  //----------------------------------
328 
329  // draw point if close to plane
330  if (scalardiff < maxDiff)
331  {
332  float pointsize = 5;
333  Point2D tmp;
334  glColor3f(0.0, 1.0, 0.0);
335  glLineWidth(1);
336  // a diamond around the point
337  glBegin(GL_LINE_LOOP);
338  // begin from upper left corner and paint clockwise
339  tmp[0] = pt2d[0] - pointsize;
340  tmp[1] = pt2d[1] + pointsize;
341  glVertex2dv(&tmp[0]);
342  tmp[0] = pt2d[0] + pointsize;
343  tmp[1] = pt2d[1] + pointsize;
344  glVertex2dv(&tmp[0]);
345  tmp[0] = pt2d[0] + pointsize;
346  tmp[1] = pt2d[1] - pointsize;
347  glVertex2dv(&tmp[0]);
348  tmp[0] = pt2d[0] - pointsize;
349  tmp[1] = pt2d[1] - pointsize;
350  glVertex2dv(&tmp[0]);
351  glEnd();
352  }
353  //------------------------------------
354  }
355  }
356 }
357 
359  TextOverlayPointerType textOverlay, const char *text, float rgb[3], Point2D /*pt2d*/, mitk::BaseRenderer *renderer)
360 {
361  textOverlay->SetText(text);
362  textOverlay->SetColor(rgb);
363  textOverlay->SetOpacity(1);
364  textOverlay->SetFontSize(16);
365  textOverlay->SetBoolProperty("drawShadow", false);
366  textOverlay->SetVisibility(true, renderer);
367 }
ContourModel is a structure of linked vertices defining a contour in 3D space. The vertices are store...
virtual void UpdateOutputInformation() override
Update the OutputInformation of a ContourModel object.
mitk::Point3D Coordinates
Coordinates in 3D space.
double ScalarType
static Pointer New()
virtual void InternalDrawContour(mitk::ContourModel *renderingContour, mitk::BaseRenderer *renderer)
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.
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...
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...
virtual bool IsEmptyTimeStep(unsigned int t) const override
Check if there isn't something at this timestep.
void DrawContour(mitk::ContourModel *contour, mitk::BaseRenderer *renderer)
bool GetFloatProperty(const char *propertyKey, float &floatValue, const mitk::BaseRenderer *renderer=nullptr) const
Convenience access method for float properties (instances of FloatProperty)
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
itk::SmartPointer< OverlayManager > GetOverlayManager()
Get the OverlayManager registered with this renderer if none was set, it will be created at this poin...
void itk2vtk(const Tin &in, Tout &out)
static Pointer New()
bool IsClosed(int timestep=0) const
Return if the contour is closed or not.
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
void WriteTextWithOverlay(TextOverlayPointerType textOverlay, const char *text, float rgb[3], Point2D pt2d, mitk::BaseRenderer *renderer)
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.