Fraxinus  17.12-rc4
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxGraphicalAxes3D.h"
34 
35 #include "boost/bind.hpp"
36 #include <vtkAxesActor.h>
37 #include <vtkRenderer.h>
38 #include <vtkMatrix4x4.h>
39 #include <vtkCaptionActor2D.h>
40 #include <vtkTextProperty.h>
41 #include <vtkAssembly.h>
42 
43 #include "cxTypeConversions.h"
44 #include "cxView.h"
45 #include "cxGraphicalPrimitives.h"
46 
47 namespace cx
48 {
49 
50 GraphicalAxes3D::GraphicalAxes3D(vtkRendererPtr renderer) : m_vtkAxisLength(100)
51 {
53  mViewportListener->setCallback(boost::bind(&GraphicalAxes3D::rescale, this));
54 
55  mActor = vtkAxesActorPtr::New();
56  mActor->SetAxisLabels(false);
58 
59  this->setAxisLength(0.2);
60 
61  this->setShowAxesLabels(true);
62  setTransform(Transform3D::Identity());
63  setFontSize(0.04);
64 
65  this->setRenderer(renderer);
66 }
67 
69 {
70  if (mRenderer)
71  {
72  mRenderer->RemoveActor(mActor);
73  for (unsigned i=0; i<mCaption.size(); ++i)
74  mRenderer->RemoveActor(mCaption[i]);
75  mViewportListener->stopListen();
76  }
77 
78  mRenderer = renderer;
79 
80  if (mRenderer)
81  {
82  mRenderer->AddActor(mActor);
83  for (unsigned i=0; i<mCaption.size(); ++i)
84  mRenderer->AddActor(mCaption[i]);
85  mViewportListener->startListen(mRenderer);
86  this->rescale();
87  }
88 }
89 
91 {
92  this->setRenderer(NULL);
93 }
94 
96 {
97  mActor->SetVisibility(on);
98  this->resetAxesLabels();
99 }
100 
102 {
103  mShowLabels = on;
104  this->resetAxesLabels();
105 }
106 
108 {
109  if (mRenderer)
110  {
111  for (unsigned i=0; i<mCaption.size(); ++i)
112  mRenderer->RemoveActor(mCaption[i]);
113  }
114  mCaption.clear();
115  mCaptionPos.clear();
116 
117  if (!mShowLabels)
118  return;
119 
120  this->addCaption("x", Vector3D(1,0,0), Vector3D(1,0,0));
121  this->addCaption("y", Vector3D(0,1,0), Vector3D(0,1,0));
122  this->addCaption("z", Vector3D(0,0,1), Vector3D(0,0,1));
123 
124  if (mRenderer)
125  {
126  for (unsigned i=0; i<mCaption.size(); ++i)
127  mRenderer->AddActor(mCaption[i]);
128  }
129 }
130 
131 void GraphicalAxes3D::setCaption(const QString& caption, const Vector3D& color)
132 {
133  this->addCaption(caption, Vector3D(0,0,0), color);
134 }
135 
140 {
141  mFontSize = size;
142  this->resetAxesLabels();
143 }
144 
149 {
150  mSize = length;
151  this->rescale();
152 }
153 
158 {
159  m_rMt = rMt;
160  this->rescale();
161 }
162 
164 {
165  if (!mViewportListener->isListening())
166  return;
167 
168  double size = mViewportListener->getVpnZoom();
169  double axisSize = mSize/size;
170  double scale = axisSize / m_vtkAxisLength;
171 
172  // NOTE: vtkAxesActor dislikes small values for SetTotalLength, thus we
173  // keep that value constant at m_vtkAxisLength and instead scale the transform.
174  Transform3D rMq = m_rMt * createTransformScale(Vector3D(scale,scale,scale));
175 
176  mActor->SetUserMatrix(rMq.getVtkMatrix());
177 
178  for (unsigned i=0; i<mCaption.size(); ++i)
179  {
180  Vector3D pos = rMq.coord(axisSize*mCaptionPos[i]);
181  mCaption[i]->SetAttachmentPoint(pos.begin());
182  }
183 }
184 
185 void GraphicalAxes3D::addCaption(const QString& label, Vector3D pos, Vector3D color)
186 {
187  vtkCaptionActor2DPtr cap = vtkCaptionActor2DPtr::New();
188  cap->SetCaption(cstring_cast(label));
189  cap->GetCaptionTextProperty()->SetColor(color.begin());
190  cap->LeaderOff();
191  cap->BorderOff();
192  cap->GetCaptionTextProperty()->ShadowOff();
193  cap->SetHeight(mFontSize);
194  cap->SetVisibility(mActor->GetVisibility());
195  mCaption.push_back(cap);
196  mCaptionPos.push_back(pos);
197 }
198 
199 
200 } // 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:63
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.