Fraxinus  18.10
An IGT application
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) 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 "cxVector3D.h"
13 #include <vtkMath.h>
14 #include <vtkSmartPointer.h>
15 #include "cxUtilHelpers.h"
16 #include "cxTypeConversions.h"
17 #include "vtkForwardDeclarations.h"
18 #include <cmath>
19 #include <QString>
20 
21 // --------------------------------------------------------
22 namespace cx
23 {
24 
25 // --------------------------------------------------------
26 bool similar(double a, double b, double tol)
27 {
28  return fabs(b - a) < tol;
29 }
30 
32 {
33  return a.array() * b.array();
34 }
35 
37 {
38  return a.array() / b.array();
39 }
40 
41 Vector3D cross(const Vector3D& a, const Vector3D& b)
42 {
43  return a.cross(b);
44 }
45 
46 double dot(const Vector3D& a, const Vector3D& b)
47 {
48  return a.dot(b);
49 }
50 
51 bool similar(const Vector3D& a, const Vector3D& b, double tol)
52 {
53  return (b - a).length() < tol;
54 }
55 
56 Vector3D unitVector(double thetaXY, double thetaZ)
57 {
58  Vector3D e;
59  e[0] = cos(thetaXY) * cos(thetaZ);
60  e[1] = sin(thetaXY) * cos(thetaZ);
61  e[2] = sin(thetaZ);
62  return e;
63 }
64 
66 {
67  return atan2(k[1], k[0]);
68 }
69 
71 {
72  return atan2(k[2], sqrt(k[0] * k[0] + k[1] * k[1]));
73 }
74 
76 {
77  Vector3D retval;
78  for (int i = 0; i < 3; ++i)
79  retval[i] = (int) (a[i] + 0.5);
80 
81  return retval;
82 }
83 
85 {
86  Vector3D retval;
87  for (int i = 0; i < 3; ++i)
88  retval[i] = std::ceil(a[i]);
89 
90  return retval;
91 }
92 
93 bool similar(const Eigen::Array3i& a, const Eigen::Array3i& b)
94 {
95  return (b - a).abs().maxCoeff() < 10E-6;;
96 }
97 
98 QString prettyFormat(Vector3D val, int decimals, int fieldWidth)
99 {
100  return QString("%1 %2 %3")
101  .arg(val[0], fieldWidth, 'f', decimals)
102  .arg(val[1], fieldWidth, 'f', decimals)
103  .arg(val[2], fieldWidth, 'f', decimals);
104 }
105 
106 Eigen::Vector2d fromString(const QString& text)
107 {
108  std::vector<double> raw = convertQString2DoubleVector(text);
109  if (raw.size() != 2)
110  return Eigen::Vector2d(0, 0);
111  return Eigen::Vector2d((double*) &(*raw.begin()));
112 }
113 
114 } // namespace cx
115 // --------------------------------------------------------
Vector3D ceil(const Vector3D &a)
Definition: cxVector3D.cpp:84
Vector3D divide_elems(const Vector3D &a, const Vector3D &b)
perform element-wise division of a and b.
Definition: cxVector3D.cpp:36
Eigen::Vector2d fromString(const QString &text)
Definition: cxVector3D.cpp:106
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:56
QString prettyFormat(Vector3D val, int decimals, int fieldWidth)
Definition: cxVector3D.cpp:98
double getThetaXY(Vector3D k)
get thetaXY, meaning the angle of v projected onto the xy plane
Definition: cxVector3D.cpp:65
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
Definition: cxVector3D.cpp:41
double getThetaZ(Vector3D k)
get thetaZ, z meaning the elevation from the xy plane
Definition: cxVector3D.cpp:70
double dot(const Vector3D &a, const Vector3D &b)
compute inner product (or dot product) of a and b.
Definition: cxVector3D.cpp:46
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
Vector3D multiply_elems(const Vector3D &a, const Vector3D &b)
perform element-wise multiplication of a and b.
Definition: cxVector3D.cpp:31
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
std::vector< double > convertQString2DoubleVector(const QString &input, bool *ok)
Vector3D round(const Vector3D &a)
Definition: cxVector3D.cpp:75
Namespace for all CustusX production code.