NorMIT-nav  2023.01.05-dev+develop.0da12
An IGT application
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) 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 
13 #include "cxPlaneMetricRep.h"
14 
15 #include "cxView.h"
16 #include <vtkCamera.h>
17 #include <vtkRenderer.h>
18 #include "boost/bind.hpp"
19 #include "cxBoundingBox3D.h"
20 
21 namespace cx
22 {
23 
25 {
26  return wrap_new(new PlaneMetricRep(), uid);
27 }
28 
29 PlaneMetricRep::PlaneMetricRep()
30 {
31  mViewportListener.reset(new ViewportListener);
32  mViewportListener->setCallback(boost::bind(&PlaneMetricRep::rescale, this));
33 }
34 
36 {
37  mGraphicalPoint.reset();
38  mNormal.reset();
39 }
40 
42 {
43  mViewportListener->startListen(view->getRenderer());
45 }
46 
48 {
49  mViewportListener->stopListen();
51 }
52 
53 PlaneMetricPtr PlaneMetricRep::getPlaneMetric()
54 {
55  return boost::dynamic_pointer_cast<PlaneMetric>(mMetric);
56 }
57 
59 {
60  PlaneMetricPtr planeMetric = this->getPlaneMetric();
61  if (!planeMetric)
62  return;
63 
64  if (!mGraphicalPoint && this->getView() && mMetric)
65  {
66  mGraphicalPoint.reset(new GraphicalPoint3D(this->getRenderer()));
67  mNormal.reset(new GraphicalArrow3D(this->getRenderer()));
68  mRect.reset(new Rect3D(this->getRenderer(), mMetric->getColor()));
69  mRect->setLine(true, 1);
70  }
71 
72  if (!mGraphicalPoint)
73  return;
74 
75  mGraphicalPoint->setColor(mMetric->getColor());
76  mNormal->setColor(mMetric->getColor());
77  mRect->setColor(mMetric->getColor());
78 
79  this->rescale();
80 }
81 
88 void PlaneMetricRep::rescale()
89 {
90  if (!mGraphicalPoint)
91  return;
92 
93  PlaneMetricPtr planeMetric = this->getPlaneMetric();
94 
95  Vector3D p0_r = planeMetric->getRefCoord();
96  Vector3D n_r = planeMetric->getRefNormal();
97 
98  double size = mViewportListener->getVpnZoom(p0_r);
99  double sphereSize = mGraphicsSize / 100 / size;
100 
101  mGraphicalPoint->setValue(p0_r);
102  mGraphicalPoint->setRadius(sphereSize);
103 
104  mNormal->setValue(p0_r, n_r, sphereSize * 8);
105 
106  this->drawRectangleForPlane(p0_r, n_r, size);
107 
108  this->drawText();
109 }
110 
111 void PlaneMetricRep::drawRectangleForPlane(Vector3D p0_r, Vector3D n_r, double size)
112 {
113  if (!mRect)
114  return;
115  // draw a rectangle showing the plane:
116  Vector3D e_z = n_r;
117  Vector3D k1(1,0,0);
118  Vector3D k2(0,1,0);
119  Vector3D e_x;
120  if (cross(k2,e_z).length() > cross(k1,e_z).length())
121  e_x = cross(k2,e_z)/cross(k2,e_z).length();
122  else
123  e_x = cross(k1,e_z)/cross(k1,e_z).length();
124 
125  Vector3D e_y = cross(e_z,e_x);
126  Transform3D rMb = createTransformIJC(e_x, e_y, p0_r);
127  double bb_size = 0.10/size;
128  DoubleBoundingBox3D bb(-bb_size,bb_size,-bb_size,bb_size,0,0);
129 
130  mRect->updatePosition(bb, rMb);
131 }
132 
133 }
cx::PlaneMetricRepPtr
boost::shared_ptr< class PlaneMetricRep > PlaneMetricRepPtr
Definition: cxPlaneMetricRep.h:26
cxPlaneMetricRep.h
cx::DataMetricRep::addRepActorsToViewRenderer
void addRepActorsToViewRenderer(ViewPtr view)
Definition: cxDataMetricRep.cpp:89
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::ViewportListener
Listens to changes in viewport and camera matrix.
Definition: cxViewportListener.h:67
cx::RepImpl::getRenderer
vtkRendererPtr getRenderer()
Definition: cxRepImpl.cpp:88
cx::PlaneMetricRep
Rep for visualizing a PlaneMetric.
Definition: cxPlaneMetricRep.h:36
cx::createTransformIJC
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D &center)
Definition: cxTransform3D.cpp:255
cx::RepImpl::getView
ViewPtr getView() const
Definition: cxRepImpl.cpp:83
cxBoundingBox3D.h
length
RealScalar length() const
Definition: cxMatrixBaseEigenAddons.h:35
cx::DataMetricRep::removeRepActorsFromViewRenderer
void removeRepActorsFromViewRenderer(ViewPtr view)
Definition: cxDataMetricRep.cpp:100
cx::cross
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:41
cx::Rect3D
Helper for drawing a rectangle in 3D.
Definition: cxGraphicalPrimitives.h:205
cx::GraphicalArrow3D
Helper for rendering an arrow in 3D.
Definition: cxGraphicalPrimitives.h:187
cx::Transform3D
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Definition: cxLandmarkPatientRegistrationWidget.h:33
cxView.h
cx::DataMetricRep::drawText
void drawText()
Definition: cxDataMetricRep.cpp:109
cx::ViewPtr
boost::shared_ptr< class View > ViewPtr
Definition: cxForwardDeclarations.h:110
cx::PlaneMetricPtr
boost::shared_ptr< class PlaneMetric > PlaneMetricPtr
Definition: cxPlaneMetric.h:34
cx::GraphicalPoint3D
Helper for rendering a point in 3D.
Definition: cxGraphicalPrimitives.h:122
cx::PlaneMetricRep::removeRepActorsFromViewRenderer
virtual void removeRepActorsFromViewRenderer(ViewPtr view)
Definition: cxPlaneMetricRep.cpp:47
cx::PlaneMetricRep::addRepActorsToViewRenderer
virtual void addRepActorsToViewRenderer(ViewPtr view)
Definition: cxPlaneMetricRep.cpp:41
cx::PlaneMetricRep::clear
virtual void clear()
Definition: cxPlaneMetricRep.cpp:35
cx::DataMetricRep::mGraphicsSize
double mGraphicsSize
Definition: cxDataMetricRep.h:62
cx::DataMetricRep::mMetric
DataMetricPtr mMetric
Definition: cxDataMetricRep.h:68
cx::RepImpl::wrap_new
static boost::shared_ptr< REP > wrap_new(REP *object, QString uid)
Definition: cxRepImpl.h:62
cx::Vector3D
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
cx::PlaneMetricRep::New
static PlaneMetricRepPtr New(const QString &uid="")
Definition: cxPlaneMetricRep.cpp:24
cx::PlaneMetricRep::onModifiedStartRender
virtual void onModifiedStartRender()
Definition: cxPlaneMetricRep.cpp:58