Fraxinus  18.10
An IGT application
cxGraphicalPrimitives.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxGraphicalPrimitives.h"
13 
14 #include "boost/bind.hpp"
15 #include <vtkSphereSource.h>
16 #include <vtkLineSource.h>
17 #include <vtkArcSource.h>
18 #include <vtkPolyDataMapper.h>
19 #include <vtkActor.h>
20 #include <vtkCellArray.h>
21 #include <vtkProperty.h>
22 #include <vtkRenderer.h>
23 #include <vtkCommand.h>
24 #include <vtkFollower.h>
25 #include <vtkVectorText.h>
26 #include <vtkCamera.h>
27 #include "cxTypeConversions.h"
28 #include "cxBoundingBox3D.h"
29 #include "vtkArrowSource.h"
30 #include "vtkMatrix4x4.h"
31 #include "vtkCaptionActor2D.h"
32 #include "vtkTextProperty.h"
33 #include "cxVtkHelperClasses.h"
34 #include "vtkPolyDataNormals.h"
35 
36 namespace cx
37 {
38 
39 
41 {
42  mProperty = vtkPropertyPtr::New();
43  mActor = vtkActorPtr::New();
44  mActor->SetProperty(mProperty);
45 }
46 
48 {
49  this->setRenderer(NULL);
50 }
51 
53 {
55  mSource = source;
56 
57  if (mSource)
58  getMapper()->SetInputConnection(mSource->GetOutputPort());
59  else
60  getMapper()->SetInputConnection(NULL);
61 }
62 
64 {
65  if (mRenderer)
66  mRenderer->RemoveActor(mActor);
67 
68  mRenderer = renderer;
69 
70  if (mRenderer)
71  mRenderer->AddActor(mActor);
72 }
73 
75 {
76  mActor->SetVisibility(visible);
77 }
78 
79 
81 {
82  mActor->GetProperty()->SetBackfaceCulling(val);
83 }
84 
86 {
87  mActor->GetProperty()->SetFrontfaceCulling(val);
88 }
89 
90 void GraphicalGeometricBase::setColor(double red, double green, double blue)
91 {
92  mActor->GetProperty()->SetColor(red, green, blue);
93 }
94 
96 {
97  mActor->GetProperty()->SetColor(color.begin());
98 }
99 
101 {
102  mActor->SetPosition(point.begin());
103 }
104 
106 {
107  mActor->GetProperty()->SetOpacity(val);
108 }
109 
110 void GraphicalGeometricBase::setUserMatrix(vtkMatrix4x4 *matrix)
111 {
112  mActor->SetUserMatrix(matrix);
113 }
114 
116 {
117  if(pointSize<=0)
118  return;
119  mProperty->SetPointSize(pointSize);
120 }
121 
123 {
124  getMapper()->SetScalarVisibility(show);
125 }
126 
128 {
129  return Vector3D(mActor->GetPosition());
130 }
131 
133 {
134  return mActor;
135 }
136 
138 {
139  return mProperty;
140 }
141 
143 {
144  if (mSource)
145  return mSource->GetOutput();
146  else
147  return mData;
148 }
149 
151 {
152  return mSource;
153 }
154 
155 //--------------------------------------------------------
156 //--------------------------------------------------------
157 //-------------------------------------------------------
158 
160  GraphicalGeometricBase(source,renderer)
161 {
162  mMapper = vtkPolyDataMapperPtr::New();
163 
164  mActor->SetMapper(mMapper);
165  setSource(source);
166  setRenderer(renderer);
167 }
168 
170 {
171  mActor->GetProperty()->SetRepresentationToWireframe();
172 }
173 
175 {
177  mData = data;
178 
179  mMapper->SetInputData(mData);
180 }
181 
183 {
184  mActor->SetTexture(texture);
185 }
186 
187 
189 {
190  return mMapper;
191 }
192 
193 //--------------------------------------------------------
194 //--------------------------------------------------------
195 //-------------------------------------------------------
196 
198  GraphicalGeometricBase(source,renderer)
199 {
200  mMapper = vtkSmartPointer<vtkGlyph3DMapper>::New();
201  vtkSmartPointer<vtkArrowSource> arrowSource = vtkSmartPointer<vtkArrowSource>::New();
202  mMapper->SetSourceConnection(arrowSource->GetOutputPort());
203  mMapper->ScalarVisibilityOn();
204  mMapper->SetUseLookupTableScalarRange(1);
205  mMapper->SetScalarMode(VTK_SCALAR_MODE_USE_POINT_FIELD_DATA);
206  mActor->SetMapper(mMapper);
207  setSource(source);
208  setRenderer(renderer);
209 }
210 
211 
213 {
214  mData = data;
215  if (data)
216  mMapper->SetInputData(mData);
217 }
218 
219 void GraphicalGlyph3DData::setOrientationArray(const char* orientationArray)
220 {
221  mMapper->SetOrientationArray(orientationArray);
222 }
223 
224 void GraphicalGlyph3DData::setColorArray(const char* colorArray)
225 {
226  if(strlen(colorArray)>0)
227  {
228  setScalarVisibility(true);
229  }else
230  {
231  setScalarVisibility(false);
232  }
233  mMapper->SelectColorArray(colorArray);
234 }
235 
236 
237 void GraphicalGlyph3DData::setLUT(const char* lut)
238 {
239  vtkSmartPointer<vtkColorSeries> colorSeries = vtkSmartPointer<vtkColorSeries>::New();
240  vtkSmartPointer<vtkLookupTable> table = vtkLookupTable::New();
241  colorSeries->SetColorSchemeByName(lut);
242  colorSeries->BuildLookupTable(table , vtkColorSeries::ORDINAL);
243  mMapper->SetLookupTable(table);
244 }
245 
246 
247 void GraphicalGlyph3DData::setScaleFactor(double scaleFactor)
248 {
249  if(scaleFactor<=0) return;
250  mMapper->SetScaleFactor(scaleFactor);
251 }
252 
254 {
255  return mMapper;
256 }
257 
258 //--------------------------------------------------------
259 //--------------------------------------------------------
260 //--------------------------------------------------------
261 
263 {
264  // mRenderer = renderer;
265  source = vtkSphereSourcePtr::New();
266  source->SetRadius(4);
267  // default:
268  // source->SetThetaResolution(8);
269  // source->SetPhiResolution(8);
270  // 24*16 = 384, 8*8=64, 16*12=192
271  source->SetThetaResolution(16);
272  source->SetPhiResolution(12);
273  source->LatLongTessellationOn();
274 
275  vtkPolyDataNormalsPtr normals = vtkPolyDataNormalsPtr::New();
276  normals->SetInputConnection(source->GetOutputPort());
277  normals->Update();
278 
279  mapper = vtkPolyDataMapperPtr::New();
280  mapper->SetInputConnection(normals->GetOutputPort());
281 
282  actor = vtkActorPtr::New();
283  actor->SetMapper(mapper);
284 
285  this->setRenderer(renderer);
286 }
287 
289 {
290  this->setRenderer(NULL);
291 }
292 
294 {
295  if (mRenderer)
296  {
297  mRenderer->RemoveActor(actor);
298  }
299 
300  mRenderer = renderer;
301 
302  if (mRenderer)
303  {
304  mRenderer->AddActor(actor);
305  }
306 }
307 
308 void GraphicalPoint3D::setRadius(double radius)
309 {
310  source->SetRadius(radius);
311 }
312 
313 void GraphicalPoint3D::setColor(QColor color)
314 {
315  setColorAndOpacity(actor->GetProperty(), color);
316 }
317 
319 {
320  actor->SetPosition(point.begin());
321 }
322 
324 {
325  return Vector3D(actor->GetPosition());
326 }
327 
329 {
330  return actor;
331 }
332 
334 {
335  return source->GetOutput();
336 }
337 
338 //--------------------------------------------------------
339 //--------------------------------------------------------
340 //--------------------------------------------------------
341 
343 {
344  mRenderer = renderer;
345  source = vtkLineSourcePtr::New();
346  mapper = vtkPolyDataMapperPtr::New() ;
347  actor = vtkActorPtr::New() ;
348 
349  mapper->SetInputConnection( source->GetOutputPort() );
350  actor->SetMapper (mapper );
351  this->setRenderer(renderer);
352 }
353 
355 {
356  this->setRenderer(NULL);
357 }
358 
360 {
361  if (mRenderer)
362  {
363  mRenderer->RemoveActor(actor);
364  }
365 
366  mRenderer = renderer;
367 
368  if (mRenderer)
369  {
370  mRenderer->AddActor(actor);
371  }
372 }
373 
374 void GraphicalLine3D::setColor(QColor color)
375 {
376  setColorAndOpacity(actor->GetProperty(), color);
377 }
378 
380 {
381  source->SetPoint1(point1.begin());
382  source->SetPoint2(point2.begin());
383 }
384 
386 {
387  actor->GetProperty()->SetLineStipplePattern(stipple);
388 }
389 
391 {
392  return actor;
393 }
394 
395 //--------------------------------------------------------
396 //--------------------------------------------------------
397 //--------------------------------------------------------
398 
400 {
401  mRenderer = renderer;
402  source = vtkArcSourcePtr::New();
403  source->SetResolution(20);
404  mapper = vtkPolyDataMapperPtr::New() ;
405  actor = vtkActorPtr::New() ;
406 
407  mapper->SetInputConnection( source->GetOutputPort() );
408  actor->SetMapper (mapper );
409  if (mRenderer)
410  mRenderer->AddActor(actor);
411 }
412 
414 {
415  if (mRenderer)
416  mRenderer->RemoveActor(actor);
417 }
418 
419 void GraphicalArc3D::setColor(QColor color)
420 {
421  setColorAndOpacity(actor->GetProperty(), color);
422 }
423 
425 {
426  source->SetPoint1(point1.begin());
427  source->SetPoint2(point2.begin());
428  source->SetCenter(center.begin());
429 }
430 
431 void GraphicalArc3D::setStipple(int stipple)
432 {
433  actor->GetProperty()->SetLineStipplePattern(stipple);
434 }
435 
437 {
438  return actor;
439 }
440 
441 //--------------------------------------------------------
442 //--------------------------------------------------------
443 //--------------------------------------------------------
444 
446 {
447  mRenderer = renderer;
448  source = vtkArrowSourcePtr::New();
449  source->SetTipResolution(24);
450  source->SetShaftResolution(24);
451  mapper = vtkPolyDataMapperPtr::New() ;
452  actor = vtkActorPtr::New() ;
453 
454  mapper->SetInputConnection( source->GetOutputPort() );
455  actor->SetMapper (mapper );
456  if (mRenderer)
457  mRenderer->AddActor(actor);
458 }
459 
461 {
462  if (mRenderer)
463  mRenderer->RemoveActor(actor);
464 }
465 
466 void GraphicalArrow3D::setColor(QColor color)
467 {
468  setColorAndOpacity(actor->GetProperty(), color);
469 }
470 
472 {
473  // find an arbitrary vector k perpendicular to normal:
474  Vector3D k = cross(Vector3D(1,0,0), normal);
475  if (similar(k, Vector3D(0,0,0)))
476  k = cross(Vector3D(0,1,0), normal);
477  k = k.normalized();
478  Transform3D M = createTransformIJC(normal, k, base);
479 
480  // std::cout << "GraphicalArrow3D::setValue " << base << " - " << normal << std::endl;
481  Transform3D S = createTransformScale(Vector3D(length,1,1));
482  M = M * S;
483  // let arrow shape increase slowly with length:
484  // source->SetTipLength(0.35/sqrt(length));
485  // source->SetTipRadius(0.1*sqrt(length));
486  // source->SetShaftRadius(0.03*sqrt(length));
487  source->SetTipLength(0.35);
488  source->SetTipRadius(0.1*(length));
489  source->SetShaftRadius(0.03*(length));
490  actor->SetUserMatrix(M.getVtkMatrix());
491 }
492 
493 //--------------------------------------------------------
494 //--------------------------------------------------------
495 //--------------------------------------------------------
496 
497 Rect3D::Rect3D(vtkRendererPtr renderer, QColor color)
498 {
499  mRenderer = renderer;
500  mapper = vtkPolyDataMapperPtr::New();
501  actor = vtkActorPtr::New();
502  setColorAndOpacity(actor->GetProperty(), color);
503  actor->SetMapper(mapper);
504  if (mRenderer)
505  mRenderer->AddActor(actor);
506 
507  mPolyData = vtkPolyDataPtr::New();
508  mPoints = vtkPointsPtr::New();
509  mSide = vtkCellArrayPtr::New();
510 
511  vtkIdType cells[5] = { 0,1,2,3,0 };
512  mSide->InsertNextCell(5, cells);
513 
514  mPolyData->SetPoints(mPoints);
515  mapper->SetInputData(mPolyData);
516 }
517 
518 void Rect3D::setLine(bool on, int width)
519 {
520  if (on)
521  {
522  mPolyData->SetLines(mSide);
523  actor->GetProperty()->SetLineWidth(width);
524  }
525  else
526  {
527  mPolyData->SetLines(NULL);
528  }
529 }
530 
531 void Rect3D::setSurface(bool on)
532 {
533  if (on)
534  {
535  mPolyData->SetPolys(mSide);
536  actor->GetProperty()->SetOpacity(1.0); // transparent planes dont work well together with texture volume. Use 1.0
537  }
538  else
539  {
540  mPolyData->SetPolys(NULL);
541  }
542 }
543 
545 {
546  if (mRenderer)
547  mRenderer->RemoveActor(actor);
548 }
549 
550 void Rect3D::setColor(QColor color)
551 {
552  setColorAndOpacity(actor->GetProperty(), color);
553 }
554 
556 {
557  mPoints = vtkPointsPtr::New();
558  mPoints->InsertPoint(0, M.coord(bb.corner(0,0,0)).begin());
559  mPoints->InsertPoint(1, M.coord(bb.corner(0,1,0)).begin());
560  mPoints->InsertPoint(2, M.coord(bb.corner(1,1,0)).begin());
561  mPoints->InsertPoint(3, M.coord(bb.corner(1,0,0)).begin());
562  mPolyData->SetPoints(mPoints);
563 }
564 
565 //--------------------------------------------------------
566 //--------------------------------------------------------
567 //--------------------------------------------------------
568 
570 {
571  mText = vtkVectorTextPtr::New();
572  vtkPolyDataMapperPtr mapper = vtkPolyDataMapperPtr::New();
573  mapper->SetInputConnection(mText->GetOutputPort());
574  mFollower = vtkFollowerPtr::New();
575  mFollower->SetMapper(mapper);
576  Vector3D mTextScale(2,2,2);
577  mFollower->SetScale(mTextScale.begin());
578 
579  this->setSizeInNormalizedViewport(true, 0.025);
580  this->setRenderer(renderer);
581 }
582 
584 {
585  if (mRenderer)
586  {
587  mRenderer->RemoveActor(mFollower);
588  mViewportListener->stopListen();
589  }
590 
591  mRenderer = renderer;
592 
593  if (mRenderer)
594  {
595  mRenderer->AddActor(mFollower);
596  mFollower->SetCamera(mRenderer->GetActiveCamera());
597  if (mViewportListener)
598  mViewportListener->startListen(mRenderer);
599  }
600 }
601 
603 {
604  if (mRenderer)
605  mRenderer->RemoveActor(mFollower);
606 }
607 
608 void FollowerText3D::setSize(double val)
609 {
610  mSize = val;
611  this->scaleText();
612 }
613 
615 {
616  if (on)
617  {
618  if (!mViewportListener)
619  {
620  mViewportListener.reset(new ViewportListener);
621  mViewportListener->setCallback(boost::bind(&FollowerText3D::scaleText, this));
622  }
623  }
624  else
625  {
626  mViewportListener.reset();
627  }
628 
629  this->setSize(size);
630 }
631 
632 void FollowerText3D::setColor(QColor color)
633 {
634  setColorAndOpacity(mFollower->GetProperty(), color);
635 }
636 
637 void FollowerText3D::setText(QString text)
638 {
639  mText->SetText(cstring_cast(text));
640 }
641 
643 {
644  mFollower->SetPosition(pos.begin());
645 }
646 
648 {
649  return mFollower;
650 }
651 
659 {
660  if (!mViewportListener || !mViewportListener->isListening())
661  {
662  mFollower->SetScale(Vector3D(mSize,mSize,mSize).begin());
663  return;
664  }
665 
666  double size = mViewportListener->getVpnZoom(Vector3D(mFollower->GetPosition()));
667 
668  double scale = mSize/size;
669  // std::cout << "s= " << size << " ,scale= " << scale << std::endl;
670  Vector3D mTextScale(scale,scale,scale);
671  if (mFollower)
672  mFollower->SetScale(mTextScale.begin());
673 }
674 
675 //--------------------------------------------------------
676 //--------------------------------------------------------
677 //--------------------------------------------------------
678 
680 {
681  mText = vtkCaptionActor2DPtr::New();
682  mText->BorderOff();
683  mText->LeaderOff();
684 
685  mText->GetCaptionTextProperty()->BoldOff();
686  mText->GetCaptionTextProperty()->ItalicOff();
687  mText->GetCaptionTextProperty()->ShadowOff();
688  mText->SetWidth(10);
689  mText->SetHeight(0.03);
690 
691  this->setRenderer(renderer);
692 }
693 
695 {
696  mText->SetPosition(-15, -30);
697  mText->SetPosition2(15, -10);
698 }
699 
701 {
702  mText->SetPosition(-15, 2);
703 }
704 
706 {
707  mText->SetVisibility(visible);
708 }
709 
711 {
712  if (mRenderer)
713  {
714  mRenderer->RemoveActor(mText);
715  }
716 
717  mRenderer = renderer;
718 
719  if (mRenderer)
720  {
721  mRenderer->AddActor(mText);
722  }
723 }
724 
726 {
727  if (mRenderer)
728  mRenderer->RemoveActor(mText);
729 }
730 
731 void CaptionText3D::setSize(double val)
732 {
733  mText->SetHeight(val);
734 }
735 
736 void CaptionText3D::setColor(QColor color)
737 {
738  mText->GetCaptionTextProperty()->SetColor(getColorAsVector3D(color).begin());
739 }
740 
741 void CaptionText3D::setText(QString text)
742 {
743  mText->SetCaption(cstring_cast(text));
744 }
745 
747 {
748  mPos = pos;
749  mText->SetAttachmentPoint(pos.begin());
750 }
751 
753 {
754  return mPos;
755 }
756 
758 {
759  return mText;
760 }
761 
762 //--------------------------------------------------------
763 //--------------------------------------------------------
764 //--------------------------------------------------------
765 
766 }
void setRenderer(vtkRendererPtr renderer=vtkRendererPtr())
void setScaleFactor(double scaleFactor)
vtkSmartPointer< class vtkTexture > vtkTexturePtr
void setValue(Vector3D point1, Vector3D point2, Vector3D center)
CaptionText3D(vtkRendererPtr renderer=vtkRendererPtr())
GraphicalGlyph3DData(vtkPolyDataAlgorithmPtr source=vtkPolyDataAlgorithmPtr(), vtkRendererPtr renderer=vtkRendererPtr())
vtkSmartPointer< class vtkActor > vtkActorPtr
void setRenderer(vtkRendererPtr renderer=vtkRendererPtr())
Vector3D corner(int x, int y, int z) const
void setRenderer(vtkRendererPtr renderer=vtkRendererPtr())
Vector3D getColorAsVector3D(QColor color)
Scalar * begin()
PlainObject normal() const
void setData(vtkPolyDataPtr data)
vtkSmartPointer< class vtkPolyDataMapper > vtkPolyDataMapperPtr
void setValue(Vector3D point)
Vector3D getPosition() const
GraphicalPoint3D(vtkRendererPtr renderer=vtkRendererPtr())
vtkCaptionActor2DPtr getActor()
Transform3D createTransformScale(const Vector3D &scale_)
vtkSmartPointer< class vtkProperty > vtkPropertyPtr
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
void setOrientationArray(const char *orientationArray)
void setPosition(Vector3D pos)
void setText(QString text)
GraphicalArc3D(vtkRendererPtr renderer=vtkRendererPtr())
void setColor(QColor color)
cstring_cast_Placeholder cstring_cast(const T &val)
virtual vtkMapperPtr getMapper()=0
Rect3D(vtkRendererPtr renderer, QColor color)
void setColorArray(const char *colorArray)
GraphicalLine3D(vtkRendererPtr renderer=vtkRendererPtr())
void setData(vtkPolyDataPtr data)
Listens to changes in viewport and camera matrix.
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:41
void setStipple(int stipple)
GraphicalGeometricBase(vtkPolyDataAlgorithmPtr source=vtkPolyDataAlgorithmPtr(), vtkRendererPtr renderer=vtkRendererPtr())
void setRenderer(vtkRendererPtr renderer=vtkRendererPtr())
void setTexture(vtkTexturePtr texture)
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D &center)
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
vtkSmartPointer< class vtkFollower > vtkFollowerPtr
void setColor(double red, double green, double blue)
void scaleText()
internal method
vtkSmartPointer< class vtkMapper > vtkMapperPtr
void setPosition(Vector3D pos)
void setLine(bool on, int width)
GraphicalPolyData3D(vtkPolyDataAlgorithmPtr source=vtkPolyDataAlgorithmPtr(), vtkRendererPtr renderer=vtkRendererPtr())
vtkSmartPointer< class vtkPolyDataNormals > vtkPolyDataNormalsPtr
FollowerText3D(vtkRendererPtr renderer=vtkRendererPtr())
void setRenderer(vtkRendererPtr renderer=vtkRendererPtr())
void setColor(QColor color)
void setUserMatrix(vtkMatrix4x4 *matrix)
vtkSmartPointer< class vtkCaptionActor2D > vtkCaptionActor2DPtr
void setColor(QColor color)
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,xmax,ymin,ymax,zmin,zmax}, in order to simplify communication with vtk.
void setSizeInNormalizedViewport(bool on, double size)
vtkPolyDataAlgorithmPtr getSource()
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
void updatePosition(const DoubleBoundingBox3D bb, const Transform3D &M)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
void setSurface(bool on)
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
vtkSmartPointer< class vtkPolyDataAlgorithm > vtkPolyDataAlgorithmPtr
Definition: cxViewWrapper.h:29
RealScalar length() const
void setVisibility(bool visible)
void setSource(vtkPolyDataAlgorithmPtr source)
void setStipple(int stipple)
void setColor(QColor color)
void setValue(Vector3D base, Vector3D normal, double length)
GraphicalArrow3D(vtkRendererPtr renderer=vtkRendererPtr())
void setRadius(double radius)
void setColor(QColor color)
void setText(QString text)
vtkPolyDataAlgorithmPtr mSource
void setValue(Vector3D point1, Vector3D point2)
void setColorAndOpacity(vtkPropertyOrProperty2DPtr property, QColor color)
Namespace for all CustusX production code.