Fraxinus  17.12-rc4
An IGT application
cxSliceAutoViewportCalculator.cpp
Go to the documentation of this file.
2 
3 #include "cxUtilHelpers.h"
4 #include "cxLogger.h"
5 
6 
7 namespace cx
8 {
9 
11 {
12  ReturnType retval;
13 // retval.center_shift_s = Vector3D::Zero();
14 // retval.zoom = 0;
15 // CX_LOG_CHANNEL_DEBUG("CA") << "rois" << mROI_s;
16 
18  {
19 
20  // find the smallest viewport fitting the roi, by changing zoom and center
21  if (this->isAutoZoom())
22  return this->calculateZoomCenter_SmallestFitROI();
23  // move center only, in order to keep the tool visible.
24  else
25  return this->calculateCenter_FitToolIntoViewport();
26  }
27  else
28  {
29  // find the smallest viewport fitting the roi, by changing zoom only
30  if (this->isAutoZoom())
31  return this->calculateZoom_FitROIKeepCenter();
32  }
33 
34  return retval;
35 }
36 
37 bool SliceAutoViewportCalculator::isAutoZoom() const
38 {
40 }
41 
42 Vector3D SliceAutoViewportCalculator::findCenterShift_s()
43 {
44  Vector3D shift = Vector3D::Zero();
45 
46  Vector3D pt_s = this->findVirtualTooltip_s();
47  DoubleBoundingBox3D BB_s = this->findStaticBox();
48  shift = this->findShiftFromBoxToTool_s(BB_s, pt_s);
49 
50 // if (!similar(shift, Vector3D::Zero()))
51 // {
52 // std::cout << "type: " << enum2string(mSliceProxy->getComputer().getPlaneType()) << std::endl;
53 // std::cout << "mBB_s: " << mBB_s << std::endl;
54 // std::cout << "BB_s: " << BB_s << std::endl;
55 // std::cout << "pt_s: " << pt_s << std::endl;
56 // Vector3D pt_r = rMpr * prMt.coord(Vector3D(0,0,tool->getTooltipOffset()));
57 // std::cout << "pt_r: " << pt_r << std::endl;
58 // std::cout << "shift: " << shift << std::endl;
59 // }
60 
61  return shift;
62 }
63 
64 Vector3D SliceAutoViewportCalculator::findVirtualTooltip_s()
65 {
66  Vector3D pt_s = mTooltip_s;
67  pt_s[2] = 0; // project into plane
68  return pt_s;
69 }
70 
71 DoubleBoundingBox3D SliceAutoViewportCalculator::findStaticBox()
72 {
73  double followTooltipBoundary = mFollowTooltipBoundary;
74  followTooltipBoundary = constrainValue(followTooltipBoundary, 0.0, 0.5);
75  Transform3D S = createTransformScale(Vector3D::Ones()*(1.0-2.0*followTooltipBoundary));
77  DoubleBoundingBox3D BB_s = transform(T*S*T.inv(), mBB_s);
78  return BB_s;
79 }
80 
84 Vector3D SliceAutoViewportCalculator::findShiftFromBoxToTool_s(DoubleBoundingBox3D BB_s, Vector3D pt_s)
85 {
86  Vector3D shift = Vector3D::Zero();
87 
88  for (unsigned i=0; i<2; ++i) // loop over two first dimensions, check if pt outside of bb
89  {
90  if (pt_s[i] < BB_s[2*i])
91  shift[i] += pt_s[i] - BB_s[2*i];
92  if (pt_s[i] > BB_s[2*i+1])
93  shift[i] += pt_s[i] - BB_s[2*i+1];
94  }
95 
96  return shift;
97 }
98 
102 Vector3D SliceAutoViewportCalculator::findShiftFromBoxToROI(DoubleBoundingBox3D bb, DoubleBoundingBox3D roi)
103 {
104  Vector3D shift = Vector3D::Zero();
105 
106  for (unsigned i=0; i<2; ++i) // loop over two first dimensions, check if roi outside of bb
107  {
108  if (roi[2*i ] < bb[2*i ])
109  shift[i] += roi[2*i ] - bb[2*i ];
110  if (roi[2*i+1] > bb[2*i+1])
111  shift[i] += roi[2*i+1] - bb[2*i+1];
112  }
113 
114  return shift;
115 }
116 
117 SliceAutoViewportCalculator::ReturnType SliceAutoViewportCalculator::calculateZoomCenter_SmallestFitROI()
118 {
119  // zoom to centered box
120  // shift pos to box
121 
122  // roi: bb defining region of interest
123  // box: viewable section
124  //
125  // find centered roi
126  // find zoom needed to see entire centered roi
127  // apply zoom to box and emit zoom
128  // find shift required to see entire roi
129  // set center based on shift
130 
131  ReturnType retval;
132  DoubleBoundingBox3D roi_s = mROI_s;
134  DoubleBoundingBox3D roi_sc = transform(T, roi_s);
135  // CX_LOG_CHANNEL_DEBUG("CA") << "roi_s " << roi_s;
136  // CX_LOG_CHANNEL_DEBUG("CA") << "roi_sc " << roi_sc;
137  // CX_LOG_CHANNEL_DEBUG("CA") << "mBB_s " << mBB_s;
138 
139  double zoom = this->findZoomRequiredToIncludeRoi(mBB_s, roi_sc);
140  // CX_LOG_CHANNEL_DEBUG("CA") << "autozoom zoom " << zoom;
141  Transform3D S = createTransformScale(Vector3D::Ones()*zoom);
142  DoubleBoundingBox3D bb_zoomed = transform(S, mBB_s);
143  // CX_LOG_CHANNEL_DEBUG("CA") << "bb_zoomed " << bb_zoomed;
144 
145  // find shift
146  Vector3D shift = this->findShiftFromBoxToROI(bb_zoomed, roi_s);
147  // CX_LOG_CHANNEL_DEBUG("CA") << "autozoom shift " << shift;
148 
149  retval.center_shift_s = shift;
150  retval.zoom = 1.0/zoom;
151 
152  return retval;
153 }
154 
155 SliceAutoViewportCalculator::ReturnType SliceAutoViewportCalculator::calculateCenter_FitToolIntoViewport()
156 {
157  ReturnType retval;
158  if (mFollowTooltip)
159  {
160  retval.center_shift_s = this->findCenterShift_s();
161  }
162  return retval;
163 }
164 
165 SliceAutoViewportCalculator::ReturnType SliceAutoViewportCalculator::calculateZoom_FitROIKeepCenter()
166 {
167  ReturnType retval;
168 
169  DoubleBoundingBox3D roi_s = mROI_s;
170  // zoom to actual box
171 
172  // find zoom needed to see entire box
173  // emit zoom
174  double zoom = this->findZoomRequiredToIncludeRoi(mBB_s, roi_s);
175 // CX_LOG_CHANNEL_DEBUG("CA") << "autozoom zoom only " << zoom;
176  retval.zoom = 1.0/zoom; //???? check inversion
177 
178  return retval;
179 }
180 
181 //SliceAutoViewportCalculator::ReturnType SliceAutoViewportCalculator::autoZoom()
182 //{
183 // ReturnType retval;
184 // retval.center_shift_s = Vector3D::Zero();
185 // retval.zoom = 0;
186 
187 // DoubleBoundingBox3D roi_s = mROI_s;
188 // //DoubleBoundingBox3D roi_s = this->getROI_BB_s();
189 // // autozoom
190 // // if orthogonal: zoom to centered box
191 // // shift pos to box
192 // //
193 // // if oblique: zoom to actual box
194 // //
195 // if (mFollowType==ftFIXED_CENTER)
196 // {
197 // // roi: bb defining region of interest
198 // // box: viewable section
199 // //
200 // // find centered roi
201 // // find zoom needed to see entire centered roi
202 // // apply zoom to box and emit zoom
203 // // find shift required to see entire roi
204 // // set center based on shift
205 
207 
208 // Transform3D T = createTransformTranslate(-roi_s.center());
209 // DoubleBoundingBox3D roi_sc = transform(T, roi_s);
213 
214 // double zoom = this->findZoomRequiredToIncludeRoi(mBB_s, roi_sc);
216 // Transform3D S = createTransformScale(Vector3D::Ones()*zoom);
217 // DoubleBoundingBox3D bb_zoomed = transform(S, mBB_s);
219 
220 // // find shift
221 // Vector3D shift = this->findShiftFromBoxToROI(bb_zoomed, roi_s);
223 
225 
226 // retval.center_shift_s = shift;
227 // retval.zoom = 1.0/zoom;
228 
239 // }
240 // else // ftFOLLOW_TOOL
241 // {
242 // // find zoom needed to see entire box
243 // // emit zoom
244 // double zoom = this->findZoomRequiredToIncludeRoi(mBB_s, roi_s);
245 // CX_LOG_CHANNEL_DEBUG("CA") << "autozoom zoom " << zoom;
247 // retval.zoom = 1.0/zoom; //???? check inversion
248 // }
249 
250 // return retval;
251 //}
252 
253 double SliceAutoViewportCalculator::findZoomRequiredToIncludeRoi(DoubleBoundingBox3D base, DoubleBoundingBox3D roi)
254 {
255  double scale = 0;
256  // find zoom in x and y
257  for (int i=0; i<2; ++i)
258  {
259  double base_max = fabs(std::max(base[2*i], base[2*i+1]));
260  double roi_max = fabs(std::max(roi[2*i], roi[2*i+1]));
261  scale = std::max(scale, roi_max/base_max);
262  }
263 
264  return scale;
265 }
266 
267 
268 } // namespace cx
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
ftFIXED_CENTER
center is set.
Definition: cxDefinitions.h:68
Transform3D createTransformScale(const Vector3D &scale_)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
double constrainValue(double val, double min, double max)
static DoubleBoundingBox3D zero()
Transform3D createTransformTranslate(const Vector3D &translation)
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
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
SliceAutoViewportCalculator::ReturnType calculate()
Namespace for all CustusX production code.