Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxVector3D.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 "cxVector3D.h"
34 #include <vtkMath.h>
35 #include <vtkSmartPointer.h>
36 #include "cxUtilHelpers.h"
37 #include "cxTypeConversions.h"
38 #include "vtkForwardDeclarations.h"
39 #include <cmath>
40 #include <QString>
41 
42 // --------------------------------------------------------
43 namespace cx
44 {
45 
46 // --------------------------------------------------------
47 bool similar(double a, double b, double tol)
48 {
49  return fabs(b - a) < tol;
50 }
51 
53 {
54  return a.array() * b.array();
55 }
56 
58 {
59  return a.array() / b.array();
60 }
61 
62 Vector3D cross(const Vector3D& a, const Vector3D& b)
63 {
64  return a.cross(b);
65 }
66 
67 double dot(const Vector3D& a, const Vector3D& b)
68 {
69  return a.dot(b);
70 }
71 
72 bool similar(const Vector3D& a, const Vector3D& b, double tol)
73 {
74  return (b - a).length() < tol;
75 }
76 
77 Vector3D unitVector(double thetaXY, double thetaZ)
78 {
79  Vector3D e;
80  e[0] = cos(thetaXY) * cos(thetaZ);
81  e[1] = sin(thetaXY) * cos(thetaZ);
82  e[2] = sin(thetaZ);
83  return e;
84 }
85 
87 {
88  return atan2(k[1], k[0]);
89 }
90 
92 {
93  return atan2(k[2], sqrt(k[0] * k[0] + k[1] * k[1]));
94 }
95 
97 {
98  Vector3D retval;
99  for (int i = 0; i < 3; ++i)
100  retval[i] = (int) (a[i] + 0.5);
101 
102  return retval;
103 }
104 
106 {
107  Vector3D retval;
108  for (int i = 0; i < 3; ++i)
109  retval[i] = std::ceil(a[i]);
110 
111  return retval;
112 }
113 
114 bool similar(const Eigen::Array3i& a, const Eigen::Array3i& b)
115 {
116  return (b - a).abs().maxCoeff() < 10E-6;;
117 }
118 
119 QString prettyFormat(Vector3D val, int decimals, int fieldWidth)
120 {
121  return QString("%1 %2 %3")
122  .arg(val[0], fieldWidth, 'f', decimals)
123  .arg(val[1], fieldWidth, 'f', decimals)
124  .arg(val[2], fieldWidth, 'f', decimals);
125 }
126 
127 Eigen::Vector2d fromString(const QString& text)
128 {
129  std::vector<double> raw = convertQString2DoubleVector(text);
130  if (raw.size() != 2)
131  return Eigen::Vector2d(0, 0);
132  return Eigen::Vector2d((double*) &(*raw.begin()));
133 }
134 
135 } // namespace cx
136 // --------------------------------------------------------
Vector3D ceil(const Vector3D &a)
Definition: cxVector3D.cpp:105
Vector3D divide_elems(const Vector3D &a, const Vector3D &b)
perform element-wise division of a and b.
Definition: cxVector3D.cpp:57
Eigen::Vector2d fromString(const QString &text)
Definition: cxVector3D.cpp:127
Vector3D unitVector(double thetaXY, double thetaZ)
compute a unit vector given angles xy in the xy plane and z meaning the elevation from the xy plane...
Definition: cxVector3D.cpp:77
QString prettyFormat(Vector3D val, int decimals, int fieldWidth)
Definition: cxVector3D.cpp:119
double getThetaXY(Vector3D k)
get thetaXY, meaning the angle of v projected onto the xy plane
Definition: cxVector3D.cpp:86
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
double getThetaZ(Vector3D k)
get thetaZ, z meaning the elevation from the xy plane
Definition: cxVector3D.cpp:91
double dot(const Vector3D &a, const Vector3D &b)
compute inner product (or dot product) of a and b.
Definition: cxVector3D.cpp:67
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
Vector3D multiply_elems(const Vector3D &a, const Vector3D &b)
perform element-wise multiplication of a and b.
Definition: cxVector3D.cpp:52
std::vector< double > convertQString2DoubleVector(const QString &input, bool *ok)
Vector3D round(const Vector3D &a)
Definition: cxVector3D.cpp:96