CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxGraphicalAxes3D.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 "cxGraphicalAxes3D.h"
13 
14 #include "boost/bind.hpp"
15 #include <vtkAxesActor.h>
16 #include <vtkRenderer.h>
17 #include <vtkMatrix4x4.h>
18 #include <vtkCaptionActor2D.h>
19 #include <vtkTextProperty.h>
20 #include <vtkAssembly.h>
21 
22 #include "cxTypeConversions.h"
23 #include "cxView.h"
24 #include "cxGraphicalPrimitives.h"
25 
26 namespace cx
27 {
28 
29 GraphicalAxes3D::GraphicalAxes3D(vtkRendererPtr renderer) : m_vtkAxisLength(100)
30 {
32  mViewportListener->setCallback(boost::bind(&GraphicalAxes3D::rescale, this));
33 
34  mActor = vtkAxesActorPtr::New();
35  mActor->SetAxisLabels(false);
37 
38  this->setAxisLength(0.2);
39 
40  this->setShowAxesLabels(true);
41  setTransform(Transform3D::Identity());
42  setFontSize(0.04);
43 
44  this->setRenderer(renderer);
45 }
46 
48 {
49  if (mRenderer)
50  {
51  mRenderer->RemoveActor(mActor);
52  for (unsigned i=0; i<mCaption.size(); ++i)
53  mRenderer->RemoveActor(mCaption[i]);
54  mViewportListener->stopListen();
55  }
56 
57  mRenderer = renderer;
58 
59  if (mRenderer)
60  {
61  mRenderer->AddActor(mActor);
62  for (unsigned i=0; i<mCaption.size(); ++i)
63  mRenderer->AddActor(mCaption[i]);
64  mViewportListener->startListen(mRenderer);
65  this->rescale();
66  }
67 }
68 
70 {
71  this->setRenderer(NULL);
72 }
73 
75 {
76  mActor->SetVisibility(on);
77  this->resetAxesLabels();
78 }
79 
81 {
82  mShowLabels = on;
83  this->resetAxesLabels();
84 }
85 
87 {
88  if (mRenderer)
89  {
90  for (unsigned i=0; i<mCaption.size(); ++i)
91  mRenderer->RemoveActor(mCaption[i]);
92  }
93  mCaption.clear();
94  mCaptionPos.clear();
95 
96  if (!mShowLabels)
97  return;
98 
99  this->addCaption("x", Vector3D(1,0,0), Vector3D(1,0,0));
100  this->addCaption("y", Vector3D(0,1,0), Vector3D(0,1,0));
101  this->addCaption("z", Vector3D(0,0,1), Vector3D(0,0,1));
102 
103  if (mRenderer)
104  {
105  for (unsigned i=0; i<mCaption.size(); ++i)
106  mRenderer->AddActor(mCaption[i]);
107  }
108 }
109 
110 void GraphicalAxes3D::setCaption(const QString& caption, const Vector3D& color)
111 {
112  this->addCaption(caption, Vector3D(0,0,0), color);
113 }
114 
119 {
120  mFontSize = size;
121  this->resetAxesLabels();
122 }
123 
128 {
129  mSize = length;
130  this->rescale();
131 }
132 
137 {
138  m_rMt = rMt;
139  this->rescale();
140 }
141 
143 {
144  if (!mViewportListener->isListening())
145  return;
146 
147  double size = mViewportListener->getVpnZoom();
148  double axisSize = mSize/size;
149  double scale = axisSize / m_vtkAxisLength;
150 
151  // NOTE: vtkAxesActor dislikes small values for SetTotalLength, thus we
152  // keep that value constant at m_vtkAxisLength and instead scale the transform.
153  Transform3D rMq = m_rMt * createTransformScale(Vector3D(scale,scale,scale));
154 
155  mActor->SetUserMatrix(rMq.getVtkMatrix());
156 
157  for (unsigned i=0; i<mCaption.size(); ++i)
158  {
159  Vector3D pos = rMq.coord(axisSize*mCaptionPos[i]);
160  mCaption[i]->SetAttachmentPoint(pos.begin());
161  }
162 }
163 
164 void GraphicalAxes3D::addCaption(const QString& label, Vector3D pos, Vector3D color)
165 {
166  vtkCaptionActor2DPtr cap = vtkCaptionActor2DPtr::New();
167  cap->SetCaption(cstring_cast(label));
168  cap->GetCaptionTextProperty()->SetColor(color.begin());
169  cap->LeaderOff();
170  cap->BorderOff();
171  cap->GetCaptionTextProperty()->ShadowOff();
172  cap->SetHeight(mFontSize);
173  cap->SetVisibility(mActor->GetVisibility());
174  mCaption.push_back(cap);
175  mCaptionPos.push_back(pos);
176 }
177 
178 
179 } // namespace cx
Transform3D createTransformScale(const Vector3D &scale_)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
void setFontSize(double size)
cstring_cast_Placeholder cstring_cast(const T &val)
vtkRendererPtr mRenderer
Listens to changes in viewport and camera matrix.
vtkAxesActorPtr mActor
GraphicalAxes3D(vtkRendererPtr renderer=vtkRendererPtr())
vtkSmartPointer< class vtkRenderer > vtkRendererPtr
void setAxisLength(double length)
void addCaption(const QString &label, Vector3D pos, Vector3D color)
std::vector< vtkCaptionActor2DPtr > mCaption
vtkSmartPointer< class vtkCaptionActor2D > vtkCaptionActor2DPtr
std::vector< Vector3D > mCaptionPos
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
void setCaption(const QString &caption, const Vector3D &color)
RealScalar length() const
const double m_vtkAxisLength
ViewportListenerPtr mViewportListener
void setRenderer(vtkRendererPtr renderer=vtkRendererPtr())
void setTransform(Transform3D rMt)
void setShowAxesLabels(bool on)
Namespace for all CustusX production code.