Fraxinus  18.10
An IGT application
cxMathUtils.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 "cxMathUtils.h"
13 
14 #include "cxVector3D.h"
15 
16 // For debugging:
17 #include "cxLogger.h"
18 
19 
20 namespace cx {
21 double roundAwayFromZero(double val)
22 {
23  if(val >= 0)
24  return int(val+0.5);
25  else
26  return int(val-0.5);
27 }
28 
29 unsigned int roundUnsigned(double val)
30 {
31  return int(val+0.5);
32 }
33 
34 Eigen::ArrayXd matrixToQuaternion(Transform3D Tx)
35 // Converts a 4x4 transformation matrix to quaternion (7 elements)
36 {
37  Eigen::ArrayXd qArray = Eigen::ArrayXd::Zero(7);
38  Eigen::Quaterniond qA;
39 
40  qArray.segment<3>(4) = Tx.matrix().block<3, 1>(0,3); // Translation part
41  qA = Eigen::Quaterniond(Tx.matrix().block<3, 3>(0,0)); //Convert rot to quaternions
42  qArray.segment<4>(0) = qA.coeffs(); //Rotation parameters
43 
44  return qArray;
45 }
46 
47 Transform3D quaternionToMatrix(Eigen::ArrayXd qArray)
48 // Converts a quaternion (7 elements) to a 4x4 transformation matrix
49 {
50  Transform3D Tx;
51  Eigen::Quaterniond qA;
52 
53  qA.coeffs() = qArray.segment<4>(0);
54  Tx.matrix().block<3, 3>(0,0) = qA.toRotationMatrix();
55  Tx.matrix().block<3, 1>(0,3) = qArray.segment<3>(4);
56  Tx.matrix()(3,3) = 1;
57 
58  return Tx;
59 }
60 
61 }
Transform3D quaternionToMatrix(Eigen::ArrayXd qArray)
Definition: cxMathUtils.cpp:47
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Eigen::ArrayXd matrixToQuaternion(Transform3D Tx)
Definition: cxMathUtils.cpp:34
double roundAwayFromZero(double val)
Definition: cxMathUtils.cpp:21
unsigned int roundUnsigned(double val)
Definition: cxMathUtils.cpp:29
Namespace for all CustusX production code.