NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxViewFollower.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 "cxViewFollower.h"
34 
35 #include "cxSliceProxy.h"
36 #include "cxSettings.h"
37 #include "cxSliceComputer.h"
38 #include "cxTool.h"
39 #include "cxUtilHelpers.h"
40 #include "cxDefinitionStrings.h"
41 
42 #include "cxPatientModelService.h"
43 
44 
45 namespace cx
46 {
47 
49 {
50  return ViewFollowerPtr(new ViewFollower(dataManager));
51 }
52 
53 ViewFollower::ViewFollower(PatientModelServicePtr dataManager) :
54  mDataManager(dataManager)
55 {
56 
57 }
58 
59 
61 {
62  if (mSliceProxy)
63  {
64  disconnect(mSliceProxy.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(ensureCenterWithinView()));
65  }
66 
67  mSliceProxy = sliceProxy;
68 
69  if (mSliceProxy)
70  {
71  connect(mSliceProxy.get(), SIGNAL(toolTransformAndTimestamp(Transform3D, double)), this, SLOT(ensureCenterWithinView()));
72  }
73 }
74 
76 {
77  mBB_s = bb_s;
78  this->ensureCenterWithinView();
79 }
80 
81 void ViewFollower::ensureCenterWithinView()
82 {
83  if (!mSliceProxy)
84  return;
85  if (!mSliceProxy->getTool())
86  return;
87 
88  bool followTooltip = settings()->value("Navigation/followTooltip").value<bool>();
89  if (!followTooltip)
90  return;
91 
92  // this applies only to orthogonal views: oblique follows tool anyway
93  if (mSliceProxy->getComputer().getOrientationType()!=otORTHOGONAL)
94  return;
95 
96  Vector3D shift_s = this->findCenterShift_s();
97  this->applyShiftToCenter(shift_s);
98 }
99 
100 Vector3D ViewFollower::findCenterShift_s()
101 {
102  Vector3D shift = Vector3D::Zero();
103 
104  Vector3D pt_s = this->findVirtualTooltip_s();
105  DoubleBoundingBox3D BB_s = this->findStaticBox();
106  shift = this->findShiftFromBoxToTool_s(BB_s, pt_s);
107 
108 // if (!similar(shift, Vector3D::Zero()))
109 // {
110 // std::cout << "type: " << enum2string(mSliceProxy->getComputer().getPlaneType()) << std::endl;
111 // std::cout << "mBB_s: " << mBB_s << std::endl;
112 // std::cout << "BB_s: " << BB_s << std::endl;
113 // std::cout << "pt_s: " << pt_s << std::endl;
114 // Vector3D pt_r = rMpr * prMt.coord(Vector3D(0,0,tool->getTooltipOffset()));
115 // std::cout << "pt_r: " << pt_r << std::endl;
116 // std::cout << "shift: " << shift << std::endl;
117 // }
118 
119  return shift;
120 }
121 
122 Vector3D ViewFollower::findVirtualTooltip_s()
123 {
124  ToolPtr tool = mSliceProxy->getTool();
125  Transform3D sMr = mSliceProxy->get_sMr();
126  Transform3D rMpr = mDataManager->get_rMpr();
127  Transform3D prMt = tool->get_prMt();
128  Vector3D pt_s = sMr * rMpr * prMt.coord(Vector3D(0,0,tool->getTooltipOffset()));
129  pt_s[2] = 0; // project into plane
130  return pt_s;
131 }
132 
133 DoubleBoundingBox3D ViewFollower::findStaticBox()
134 {
135  double followTooltipBoundary = settings()->value("Navigation/followTooltipBoundary").toDouble();
136  followTooltipBoundary = constrainValue(followTooltipBoundary, 0.0, 0.5);
137  Transform3D S = createTransformScale(Vector3D::Ones()*(1.0-2.0*followTooltipBoundary));
139  DoubleBoundingBox3D BB_s = transform(T*S*T.inv(), mBB_s);
140  return BB_s;
141 }
142 
143 Vector3D ViewFollower::findShiftFromBoxToTool_s(DoubleBoundingBox3D BB_s, Vector3D pt_s)
144 {
145  Vector3D shift = Vector3D::Zero();
146 
147  for (unsigned i=0; i<2; ++i) // loop over two first dimensions, check if pt outside of bb
148  {
149  if (pt_s[i] < BB_s[2*i])
150  shift[i] += pt_s[i] - BB_s[2*i];
151  if (pt_s[i] > BB_s[2*i+1])
152  shift[i] += pt_s[i] - BB_s[2*i+1];
153  }
154 
155  return shift;
156 }
157 
158 void ViewFollower::applyShiftToCenter(Vector3D shift_s)
159 {
160  Transform3D sMr = mSliceProxy->get_sMr();
161  Vector3D c_s = sMr.coord(mDataManager->getCenter());
162  Vector3D newcenter_s = c_s + shift_s;
163  Vector3D newcenter_r = sMr.inv().coord(newcenter_s);
164  mDataManager->setCenter(newcenter_r);
165 }
166 
167 } // namespace cx
168 
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
boost::shared_ptr< class ViewFollower > ViewFollowerPtr
void setView(DoubleBoundingBox3D bb_s)
Transform3D createTransformScale(const Vector3D &scale_)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class SliceProxy > SliceProxyPtr
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:87
void setSliceProxy(SliceProxyPtr sliceProxy)
double constrainValue(double val, double min, double max)
otORTHOGONAL
orient planes relative to the image/reference space.
Definition: cxDefinitions.h:50
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
Transform3D createTransformTranslate(const Vector3D &translation)
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
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:63
Vector3D center() const
static ViewFollowerPtr create(PatientModelServicePtr dataManager)
boost::shared_ptr< class Tool > ToolPtr