NorMIT-nav  22.09
An IGT application
cxTransform3D.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 #include "cxTransform3D.h"
13 
14 #include <sstream>
15 #include <vector>
16 #include <vtkMatrix4x4.h>
17 #include <vtkTransform.h>
18 #include "cxTypeConversions.h"
19 #include "cxBoundingBox3D.h"
20 #include "vtkForwardDeclarations.h"
21 
22 // --------------------------------------------------------
24 {
25 
26 boost::array<double, 16> flatten(const Eigen::Affine3d* self)
27 {
28  boost::array<double, 16> retval;
29  boost::array<double, 16>::iterator raw = retval.begin();
30 
31  for (int r = 0; r < 4; ++r)
32  for (int c = 0; c < 4; ++c)
33  *raw++ = (*self)(r, c);
34 
35  return retval;
36 }
37 
38 void fill(Eigen::Affine3d* self, vtkMatrix4x4Ptr m)
39 {
40  if (!m)
41  return;
42  for (int r = 0; r < 4; ++r)
43  for (int c = 0; c < 4; ++c)
44  (*self)(r, c) = m->GetElement(r, c);
45 }
46 
47 void fill(Eigen::Affine3d* self, float m[4][4])
48 {
49  if (!m)
50  return;
51  for (int r = 0; r < 4; ++r)
52  for (int c = 0; c < 4; ++c)
53  (*self)(r, c) = m[r][c];
54 }
55 
56 
60 void fill(Eigen::Affine3d* self, const double* raw)
61 {
62  for (int r = 0; r < 4; ++r)
63  for (int c = 0; c < 4; ++c)
64  (*self)(r, c) = *raw++;
65 }
66 
67 vtkMatrix4x4Ptr getVtkMatrix(const Eigen::Affine3d* self)
68 {
69  vtkMatrix4x4Ptr m = vtkMatrix4x4Ptr::New();
70 
71  for (int r = 0; r < 4; ++r)
72  for (int c = 0; c < 4; ++c)
73  m->SetElement(r, c, (*self)(r, c));
74  ;
75 
76  return m;
77 }
78 
79 vtkTransformPtr getVtkTransform(const Eigen::Affine3d* self)
80 {
81  vtkTransformPtr retval = vtkTransform::New();
82  retval->SetMatrix(self->getVtkMatrix());
83  retval->Update();
84  return retval;
85 }
86 
87 std::ostream& put(const Eigen::Affine3d* self, std::ostream& s, int indent, char newline)
88 {
89  QString ind(indent, ' ');
90 
91  std::ostringstream ss; // avoid changing state of input stream
92  ss << setprecision(3) << std::fixed;
93 
94  for (unsigned i = 0; i < 4; ++i)
95  {
96  ss << ind;
97  for (unsigned j = 0; j < 4; ++j)
98  {
99  ss << setw(10) << (*self)(i, j) << " ";
100  }
101  if (i != 3)
102  {
103  ss << newline;
104  }
105  }
106 
107  s << ss.str();
108 
109  return s;
110 }
111 
112 Eigen::Affine3d fromString(const QString& text, bool* _ok)
113 {
114  bool okval = false; // if input _ok is null, we still need a flag
115  bool* ok = &okval;
116  if (_ok)
117  ok = _ok;
118 
119  std::vector<double> raw = convertQString2DoubleVector(text, ok);
120  if (raw.size() != 16)
121  *ok = false;
122  if (!*ok)
123  return Eigen::Affine3d();
124 
125  Eigen::Affine3d retval;
126  fill(&retval, &*raw.begin());
127  return retval;
128 }
129 
130 } // namespace cx_transform3D_internal
131 
132 namespace cx
133 {
134 
135 bool similar(const Transform3D& a, const Transform3D& b, double tol)
136 {
137  boost::array<double, 16> m = a.flatten();
138  boost::array<double, 16> n = b.flatten();
139  for (int j = 0; j < 16; ++j)
140  {
141  if (!similar(n[j], m[j], tol))
142  {
143  return false;
144  }
145  }
146  return true;
147 }
148 // --------------------------------------------------------
149 
151 {
152  Vector3D a = m.coord(bb.bottomLeft());
153  Vector3D b = m.coord(bb.topRight());
154  return DoubleBoundingBox3D(a, b);
155 }
156 
158 {
159  Transform3D retval = Transform3D::Identity();
160  retval.scale(scale_);
161  return retval;
162 }
163 
165 {
166  Transform3D retval = Transform3D::Identity();
167  retval.translate(translation);
168  return retval;
169 }
170 
172 {
173  Transform3D retval = Transform3D::Identity();
174  retval.rotate(Eigen::AngleAxisd(angle, Vector3D::UnitX()));
175  return retval;
176 }
177 
179 {
180  Transform3D retval = Transform3D::Identity();
181  retval.rotate(Eigen::AngleAxisd(angle, Vector3D::UnitY()));
182  return retval;
183 }
184 
186 {
187  Transform3D retval = Transform3D::Identity();
188  retval.rotate(Eigen::AngleAxisd(angle, Vector3D::UnitZ()));
189  return retval;
190 }
191 
193 {
194  Vector3D k = cross(from, to);
195 
196 
197  // handle special cases
198  if (similar(k.length(), 0.0))
199  {
200  // dot==1 -> point in the same direction
201  if (similar(dot(from, to), 1.0))
202  {
203  return Transform3D::Identity();
204  }
205 
206  // dot==-1 ->point in opposite directions, cross product will fail.
207  // Find an arbitrary vector perpendicular to from, rotate 180 around that one.
208  if (similar(dot(from, to), -1.0))
209  {
210  Vector3D e_x = Vector3D::UnitX();
211  Vector3D kk = cross(from, e_x);
212  if (similar(kk.length(), 0.0))
213  {
214  Vector3D e_y = Vector3D::UnitY();
215  kk = cross(from, e_y);
216  }
217 
218  Transform3D retval = Transform3D::Identity();
219  retval.rotate(Eigen::AngleAxisd(M_PI, kk));
220  return retval;
221  }
222  }
223 
224  double dotnormal = dot(from, to)/from.length()/to.length();
225  double angle = acos(dotnormal);
226 
227  Transform3D retval = Transform3D::Identity();
228  retval.rotate(Eigen::AngleAxisd(angle, k.normal()));
229  return retval;
230 }
231 
232 
234 {
235  // translate input bottomleft to origin, scale, translate back to output bottomleft.
237  Vector3D inrange = in.range();
238  Vector3D outrange = out.range();
239  Vector3D scale;
240 
241  // check for zero input dimensions
242  for (unsigned i = 0; i < scale.size(); ++i)
243  {
244  if (fabs(inrange[i]) < 1.0E-5)
245  scale[i] = 0;
246  else
247  scale[i] = outrange[i] / inrange[i];
248  }
251  Transform3D M = T1 * S * T0;
252  return M;
253 }
254 
255 Transform3D createTransformIJC(const Vector3D& ivec, const Vector3D& jvec, const Vector3D& center)
256 {
257  Transform3D t = Transform3D::Identity();
258  t.matrix().col(0).head(3) = ivec;
259  t.matrix().col(1).head(3) = jvec;
260  t.matrix().col(2).head(3) = cross(ivec, jvec);
261  t.matrix().col(3).head(3) = center;
262  return t;
263 }
264 
265 
267 {
269 }
270 
271 cxResource_EXPORT Transform3D createTransformFromReferenceToExternal(PATIENT_COORDINATE_SYSTEM external)
272 {
273  if (external==pcsRAS)
274  return createTransformLPS2RAS();
275  else
276  return Transform3D::Identity();
277 }
278 
280 {
281  Eigen::IOFormat singleLineFormatting(Eigen::StreamPrecision, Eigen::DontAlignCols, ", ", " | ", "", "", "", "");
282  std::ostringstream stream;
283  stream << transform.matrix().format(singleLineFormatting);
284 
285  return stream.str();
286 }
287 
288 
289 } // namespace cx
290 // --------------------------------------------------------
291 
pcsRAS
pcsRAS
Right-Anterior-Superior, used by Slicer3D, ITK-Snap, nifti, MINC.
Definition: cxDefinitions.h:105
vtkMatrix4x4Ptr
vtkSmartPointer< class vtkMatrix4x4 > vtkMatrix4x4Ptr
Definition: cxMathBase.h:37
cx::createTransformRotateZ
Transform3D createTransformRotateZ(const double angle)
Definition: cxTransform3D.cpp:185
cx::DoubleBoundingBox3D
Representation of a floating-point bounding box in 3D. The data are stored as {xmin,...
Definition: cxBoundingBox3D.h:63
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::createTransformScale
Transform3D createTransformScale(const Vector3D &scale_)
Definition: cxTransform3D.cpp:157
cx::DoubleBoundingBox3D::topRight
Vector3D topRight() const
Definition: cxBoundingBox3D.cpp:192
cx::createTransformLPS2RAS
cxResource_EXPORT Transform3D createTransformLPS2RAS()
Definition: cxTransform3D.cpp:266
cx_transform3D_internal::fill
void fill(Eigen::Affine3d *self, vtkMatrix4x4Ptr m)
Definition: cxTransform3D.cpp:38
cx_transform3D_internal::getVtkTransform
vtkTransformPtr getVtkTransform(const Eigen::Affine3d *self)
Definition: cxTransform3D.cpp:79
cx::createTransformIJC
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D &center)
Definition: cxTransform3D.cpp:255
cx_transform3D_internal::fromString
Eigen::Affine3d fromString(const QString &text, bool *_ok)
Definition: cxTransform3D.cpp:112
convertQString2DoubleVector
std::vector< double > convertQString2DoubleVector(const QString &input, bool *ok)
Definition: cxTypeConversions.cpp:34
cxBoundingBox3D.h
cx_transform3D_internal::put
std::ostream & put(const Eigen::Affine3d *self, std::ostream &s, int indent, char newline)
Definition: cxTransform3D.cpp:87
cx::matrixAsSingleLineString
std::string matrixAsSingleLineString(cx::Transform3D transform)
Definition: cxTransform3D.cpp:279
cx::cross
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:41
cx_transform3D_internal
Definition: cxTransform3D.cpp:23
cx::Transform3D
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Definition: cxLandmarkPatientRegistrationWidget.h:33
cxTypeConversions.h
vtkForwardDeclarations.h
cx::createTransformRotateX
Transform3D createTransformRotateX(const double angle)
Definition: cxTransform3D.cpp:171
vtkTransformPtr
vtkSmartPointer< class vtkTransform > vtkTransformPtr
Definition: cxMathBase.h:41
cx::DoubleBoundingBox3D::range
Vector3D range() const
Definition: cxBoundingBox3D.cpp:200
M_PI
#define M_PI
Definition: cxReconstructParams.cpp:25
cx_transform3D_internal::getVtkMatrix
vtkMatrix4x4Ptr getVtkMatrix(const Eigen::Affine3d *self)
Definition: cxTransform3D.cpp:67
cx::createTransformRotationBetweenVectors
Transform3D createTransformRotationBetweenVectors(Vector3D from, Vector3D to)
Definition: cxTransform3D.cpp:192
cx::createTransformTranslate
Transform3D createTransformTranslate(const Vector3D &translation)
Definition: cxTransform3D.cpp:164
cx::createTransformNormalize
Transform3D createTransformNormalize(const DoubleBoundingBox3D &in, const DoubleBoundingBox3D &out)
Definition: cxTransform3D.cpp:233
cx::transform
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
Definition: cxTransform3D.cpp:150
cx::createTransformRotateY
Transform3D createTransformRotateY(const double angle)
Definition: cxTransform3D.cpp:178
cxTransform3D.h
cx::dot
double dot(const Vector3D &a, const Vector3D &b)
compute inner product (or dot product) of a and b.
Definition: cxVector3D.cpp:46
cx_transform3D_internal::flatten
boost::array< double, 16 > flatten(const Eigen::Affine3d *self)
Definition: cxTransform3D.cpp:26
cx::similar
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
Definition: cxCameraStyleForView.cpp:506
cx::DoubleBoundingBox3D::bottomLeft
Vector3D bottomLeft() const
Definition: cxBoundingBox3D.cpp:188
cx::Vector3D
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
cx::createTransformFromReferenceToExternal
cxResource_EXPORT Transform3D createTransformFromReferenceToExternal(PATIENT_COORDINATE_SYSTEM external)
Definition: cxTransform3D.cpp:271