CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 "cxTransform3D.h"
34 
35 #include <sstream>
36 #include <vector>
37 #include <vtkMatrix4x4.h>
38 #include <vtkTransform.h>
39 #include "cxTypeConversions.h"
40 #include "cxBoundingBox3D.h"
41 #include "vtkForwardDeclarations.h"
42 
43 // --------------------------------------------------------
44 namespace cx_transform3D_internal
45 {
46 
50 boost::array<double, 16> flatten(const Eigen::Affine3d* self)
51 {
52  boost::array<double, 16> retval;
53  boost::array<double, 16>::iterator raw = retval.begin();
54 
55  for (int r = 0; r < 4; ++r)
56  for (int c = 0; c < 4; ++c)
57  *raw++ = (*self)(r, c);
58 
59  return retval;
60 }
61 
62 void fill(Eigen::Affine3d* self, vtkMatrix4x4Ptr m)
63 {
64  if (!m)
65  return;
66  for (int r = 0; r < 4; ++r)
67  for (int c = 0; c < 4; ++c)
68  (*self)(r, c) = m->GetElement(r, c);
69 }
70 
74 void fill(Eigen::Affine3d* self, const double* raw)
75 {
76  for (int r = 0; r < 4; ++r)
77  for (int c = 0; c < 4; ++c)
78  (*self)(r, c) = *raw++;
79 }
80 
81 vtkMatrix4x4Ptr getVtkMatrix(const Eigen::Affine3d* self)
82 {
83  vtkMatrix4x4Ptr m = vtkMatrix4x4Ptr::New();
84 
85  for (int r = 0; r < 4; ++r)
86  for (int c = 0; c < 4; ++c)
87  m->SetElement(r, c, (*self)(r, c));
88  ;
89 
90  return m;
91 }
92 
93 vtkTransformPtr getVtkTransform(const Eigen::Affine3d* self)
94 {
95  vtkTransformPtr retval = vtkTransform::New();
96  retval->SetMatrix(self->getVtkMatrix());
97  retval->Update();
98  return retval;
99 }
100 
101 std::ostream& put(const Eigen::Affine3d* self, std::ostream& s, int indent, char newline)
102 {
103  QString ind(indent, ' ');
104 
105  std::ostringstream ss; // avoid changing state of input stream
106  ss << setprecision(3) << std::fixed;
107 
108  for (unsigned i = 0; i < 4; ++i)
109  {
110  ss << ind;
111  for (unsigned j = 0; j < 4; ++j)
112  {
113  ss << setw(10) << (*self)(i, j) << " ";
114  }
115  if (i != 3)
116  {
117  ss << newline;
118  }
119  }
120 
121  s << ss.str();
122 
123  return s;
124 }
125 
126 Eigen::Affine3d fromString(const QString& text, bool* _ok)
127 {
128  bool okval = false; // if input _ok is null, we still need a flag
129  bool* ok = &okval;
130  if (_ok)
131  ok = _ok;
132 
133  std::vector<double> raw = convertQString2DoubleVector(text, ok);
134  if (raw.size() != 16)
135  *ok = false;
136  if (!*ok)
137  return Eigen::Affine3d();
138 
139  Eigen::Affine3d retval;
140  fill(&retval, &*raw.begin());
141  return retval;
142 }
143 
144 } // namespace cx_transform3D_internal
145 
146 namespace cx
147 {
148 
149 bool similar(const Transform3D& a, const Transform3D& b, double tol)
150 {
151  boost::array<double, 16> m = a.flatten();
152  boost::array<double, 16> n = b.flatten();
153  for (int j = 0; j < 16; ++j)
154  {
155  if (!similar(n[j], m[j], tol))
156  {
157  return false;
158  }
159  }
160  return true;
161 }
162 // --------------------------------------------------------
163 
168 {
169  Vector3D a = m.coord(bb.bottomLeft());
170  Vector3D b = m.coord(bb.topRight());
171  return DoubleBoundingBox3D(a, b);
172 }
173 
177 {
178  Transform3D retval = Transform3D::Identity();
179  retval.scale(scale_);
180  return retval;
181 }
182 
186 {
187  Transform3D retval = Transform3D::Identity();
188  retval.translate(translation);
189  return retval;
190 }
191 
195 {
196  Transform3D retval = Transform3D::Identity();
197  retval.rotate(Eigen::AngleAxisd(angle, Vector3D::UnitX()));
198  return retval;
199 }
200 
204 {
205  Transform3D retval = Transform3D::Identity();
206  retval.rotate(Eigen::AngleAxisd(angle, Vector3D::UnitY()));
207  return retval;
208 }
209 
213 {
214  Transform3D retval = Transform3D::Identity();
215  retval.rotate(Eigen::AngleAxisd(angle, Vector3D::UnitZ()));
216  return retval;
217 }
218 
225 {
226  // translate input bottomleft to origin, scale, translate back to output bottomleft.
228  Vector3D inrange = in.range();
229  Vector3D outrange = out.range();
230  Vector3D scale;
231 
232  // check for zero input dimensions
233  for (unsigned i = 0; i < scale.size(); ++i)
234  {
235  if (fabs(inrange[i]) < 1.0E-5)
236  scale[i] = 0;
237  else
238  scale[i] = outrange[i] / inrange[i];
239  }
242  Transform3D M = T1 * S * T0;
243  return M;
244 }
245 
252 Transform3D createTransformIJC(const Vector3D& ivec, const Vector3D& jvec, const Vector3D& center)
253 {
254  Transform3D t = Transform3D::Identity();
255  t.matrix().col(0).head(3) = ivec;
256  t.matrix().col(1).head(3) = jvec;
257  t.matrix().col(2).head(3) = cross(ivec, jvec);
258  t.matrix().col(3).head(3) = center;
259  return t;
260 }
261 
262 
263 } // namespace cx
264 // --------------------------------------------------------
265 
vtkSmartPointer< class vtkMatrix4x4 > vtkMatrix4x4Ptr
Definition: cxMathBase.h:58
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
Transform3D createTransformRotateY(const double angle)
Vector3D topRight() const
std::ostream & put(const Eigen::Affine3d *self, std::ostream &s, int indent, char newline)
Transform3D createTransformScale(const Vector3D &scale_)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::array< double, 16 > flatten(const Eigen::Affine3d *self)
Vector3D bottomLeft() const
vtkSmartPointer< class vtkTransform > vtkTransformPtr
Definition: cxMathBase.h:62
Transform3D createTransformNormalize(const DoubleBoundingBox3D &in, const DoubleBoundingBox3D &out)
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:62
vtkMatrix4x4Ptr getVtkMatrix(const Eigen::Affine3d *self)
Transform3D createTransformIJC(const Vector3D &ivec, const Vector3D &jvec, const Vector3D &center)
Eigen::Affine3d fromString(const QString &text, bool *_ok)
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
void fill(Eigen::Affine3d *self, vtkMatrix4x4Ptr m)
Transform3D createTransformRotateZ(const double angle)
vtkTransformPtr getVtkTransform(const Eigen::Affine3d *self)
Transform3D createTransformRotateX(const double angle)
std::vector< double > convertQString2DoubleVector(const QString &input, bool *ok)