Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxUSReconstructInputDataAlgoritms.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 
34 
35 namespace cx
36 {
37 
39 {
40  // Transform from image coordinate syst with origin in upper left corner
41  // to t (tool) space. TODO check is u is ul corner or ll corner.
43 
44  //mPos is prMt
45  for (unsigned i = 0; i < data->mPositions.size(); i++)
46  {
47  Transform3D prMt = data->mPositions[i].mPos;
48  data->mPositions[i].mPos = prMt * tMu;
49  }
50  //mPos is prMu
51 }
52 
54 {
55  Transform3D rMpr = data->rMpr;
56  // Transform from image coordinate syst with origin in upper left corner to t (tool) space.
58 
59  //mFrames is prMt
60  for (unsigned i = 0; i < data->mFrames.size(); i++)
61  {
62  Transform3D prMt = data->mFrames[i].mPos;
63  data->mFrames[i].mPos = rMpr * prMt * tMv;
64  }
65  //mFrames is rMu
66 }
67 
69 {
70  std::vector<double> error(data->mFrames.size(), 1000);
71 
72  if (data->mPositions.empty())
73  return error;
74 
75  for (unsigned i_frame = 0; i_frame < data->mFrames.size(); i_frame++)
76  {
77  std::vector<TimedPosition>::iterator posIter;
78  posIter = lower_bound(data->mPositions.begin(), data->mPositions.end(), data->mFrames[i_frame]);
79 
80  unsigned i_pos = distance(data->mPositions.begin(), posIter);
81  if (i_pos != 0)
82  i_pos--;
83 
84  if (i_pos >= data->mPositions.size() - 1)
85  i_pos = data->mPositions.size() - 2;
86 
87  double diff1 = fabs(data->mFrames[i_frame].mTime - data->mPositions[i_pos].mTime);
88  double diff2 = fabs(data->mFrames[i_frame].mTime - data->mPositions[i_pos + 1].mTime);
89  double diff = std::max(diff1, diff2);
90 
91  double t_delta_tracking = data->mPositions[i_pos + 1].mTime - data->mPositions[i_pos].mTime;
92  double t = 0;
93  if (!similar(t_delta_tracking, 0))
94  t = (data->mFrames[i_frame].mTime - data->mPositions[i_pos].mTime) / t_delta_tracking;
95  data->mFrames[i_frame].mPos = slerpInterpolate(data->mPositions[i_pos].mPos, data->mPositions[i_pos + 1].mPos, t);
96  error[i_frame] = diff;
97  }
98 
99  return error;
100 }
101 
103 {
104  //Convert input transforms to Quaternions
105  Eigen::Quaterniond aq = Eigen::Quaterniond(a.matrix().block<3, 3>(0,0));
106  Eigen::Quaterniond bq = Eigen::Quaterniond(b.matrix().block<3, 3>(0,0));
107 
108  Eigen::Quaterniond cq = aq.slerp(t, bq);
109 
110  Transform3D c;
111  c.matrix().block<3, 3>(0, 0) = Eigen::Matrix3d(cq);
112 
113  for (int i = 0; i < 4; i++)
114  c(i, 3) = (1 - t) * a(i, 3) + t * b(i, 3);
115  return c;
116 }
117 
119 {
120  Transform3D c;
121  for (int i = 0; i < 4; i++)
122  for (int j = 0; j < 4; j++)
123  c(i, j) = (1 - t) * a(i, j) + t * b(i, j);
124  return c;
125 }
126 
127 
128 } // namespace cx
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
std::vector< TimedPosition > mFrames
static void transformTrackingPositionsTo_prMu(USReconstructInputData *data)
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
static std::vector< double > interpolateFramePositionsFromTracking(USReconstructInputData *data)
Transform3D interpolate(const Transform3D &a, const Transform3D &b, double t)
Transform3D rMpr
patient registration
Transform3D get_uMv() const
get transform from inverted image space v (origin in ul corner) to image space u. ...
Transform3D get_tMu() const
get transform from image space u to probe tool space t.
std::vector< TimedPosition > mPositions
static Transform3D slerpInterpolate(const Transform3D &a, const Transform3D &b, double t)
static void transformFramePositionsTo_rMu(USReconstructInputData *data)