NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxPlaneMetricRep.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 
34 #include "cxPlaneMetricRep.h"
35 
36 #include "cxView.h"
37 #include <vtkCamera.h>
38 #include <vtkRenderer.h>
39 #include "boost/bind.hpp"
40 #include "cxBoundingBox3D.h"
41 
42 namespace cx
43 {
44 
46 {
47  return wrap_new(new PlaneMetricRep(), uid);
48 }
49 
50 PlaneMetricRep::PlaneMetricRep()
51 {
52  mViewportListener.reset(new ViewportListener);
53  mViewportListener->setCallback(boost::bind(&PlaneMetricRep::rescale, this));
54 }
55 
57 {
58  mGraphicalPoint.reset();
59  mNormal.reset();
60 }
61 
63 {
64  mViewportListener->startListen(view->getRenderer());
66 }
67 
69 {
70  mViewportListener->stopListen();
72 }
73 
74 PlaneMetricPtr PlaneMetricRep::getPlaneMetric()
75 {
76  return boost::dynamic_pointer_cast<PlaneMetric>(mMetric);
77 }
78 
80 {
81  PlaneMetricPtr planeMetric = this->getPlaneMetric();
82  if (!planeMetric)
83  return;
84 
85  if (!mGraphicalPoint && this->getView() && mMetric)
86  {
87  mGraphicalPoint.reset(new GraphicalPoint3D(this->getRenderer()));
88  mNormal.reset(new GraphicalArrow3D(this->getRenderer()));
89  mRect.reset(new Rect3D(this->getRenderer(), mMetric->getColor()));
90  mRect->setLine(true, 1);
91  }
92 
93  if (!mGraphicalPoint)
94  return;
95 
96  mGraphicalPoint->setColor(mMetric->getColor());
97  mNormal->setColor(mMetric->getColor());
98  mRect->setColor(mMetric->getColor());
99 
100  this->rescale();
101 }
102 
109 void PlaneMetricRep::rescale()
110 {
111  if (!mGraphicalPoint)
112  return;
113 
114  PlaneMetricPtr planeMetric = this->getPlaneMetric();
115 
116  Vector3D p0_r = planeMetric->getRefCoord();
117  Vector3D n_r = planeMetric->getRefNormal();
118 
119  double size = mViewportListener->getVpnZoom();
120  double sphereSize = mGraphicsSize / 100 / size;
121 
122  mGraphicalPoint->setValue(p0_r);
123  mGraphicalPoint->setRadius(sphereSize);
124 
125  mNormal->setValue(p0_r, n_r, sphereSize * 8);
126 
127  this->drawRectangleForPlane(p0_r, n_r, size);
128 
129  this->drawText();
130 }
131 
132 void PlaneMetricRep::drawRectangleForPlane(Vector3D p0_r, Vector3D n_r, double size)
133 {
134  if (!mRect)
135  return;
136  // draw a rectangle showing the plane:
137  Vector3D e_z = n_r;
138  Vector3D k1(1,0,0);
139  Vector3D k2(0,1,0);
140  Vector3D e_x;
141  if (cross(k2,e_z).length() > cross(k1,e_z).length())
142  e_x = cross(k2,e_z)/cross(k2,e_z).length();
143  else
144  e_x = cross(k1,e_z)/cross(k1,e_z).length();
145 
146  Vector3D e_y = cross(e_z,e_x);
147  Transform3D rMb = createTransformIJC(e_x, e_y, p0_r);
148  double bb_size = 0.10/size;
149  DoubleBoundingBox3D bb(-bb_size,bb_size,-bb_size,bb_size,0,0);
150 
151  mRect->updatePosition(bb, rMb);
152 }
153 
154 }
ViewPtr getView() const
Definition: cxRepImpl.cpp:104
vtkRendererPtr getRenderer()
Definition: cxRepImpl.cpp:109
virtual void removeRepActorsFromViewRenderer(ViewPtr view)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Rep for visualizing a PlaneMetric.
static boost::shared_ptr< REP > wrap_new(REP *object, QString uid)
Definition: cxRepImpl.h:83
static PlaneMetricRepPtr New(const QString &uid="")
Data class representing a plane.
Definition: cxPlaneMetric.h:69
Helper for rendering a point in 3D.
boost::shared_ptr< class View > ViewPtr
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:62
Helper for rendering an arrow in 3D.
virtual void onModifiedStartRender()
boost::shared_ptr< class PlaneMetric > PlaneMetricPtr
Definition: cxPlaneMetric.h:55
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D &center)
virtual void clear()
virtual void addRepActorsToViewRenderer(ViewPtr view)
void addRepActorsToViewRenderer(ViewPtr view)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
void removeRepActorsFromViewRenderer(ViewPtr view)
RealScalar length() const
boost::shared_ptr< class PlaneMetricRep > PlaneMetricRepPtr
DataMetricPtr mMetric
Helper for drawing a rectangle in 3D.