CustusX  22.04-rc5
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 }
ViewPtr getView() const
Definition: cxRepImpl.cpp:83
vtkRendererPtr getRenderer()
Definition: cxRepImpl.cpp:88
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:62
static PlaneMetricRepPtr New(const QString &uid="")
Data class representing a plane.
Definition: cxPlaneMetric.h:48
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:41
Helper for rendering an arrow in 3D.
virtual void onModifiedStartRender()
boost::shared_ptr< class PlaneMetric > PlaneMetricPtr
Definition: cxPlaneMetric.h:34
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D &center)
virtual void clear()
virtual void addRepActorsToViewRenderer(ViewPtr view)
void addRepActorsToViewRenderer(ViewPtr view)
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.
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
void removeRepActorsFromViewRenderer(ViewPtr view)
RealScalar length() const
boost::shared_ptr< class PlaneMetricRep > PlaneMetricRepPtr
DataMetricPtr mMetric
Helper for drawing a rectangle in 3D.
Namespace for all CustusX production code.