fw4spl
VectorFunctions.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwMath/VectorFunctions.hpp"
8 
9 #include <glm/glm.hpp>
10 #include <glm/gtc/type_ptr.hpp>
11 
12 namespace fwMath
13 {
14 
15 //------------------------------------------------------------------------------
16 
17 double normalize(fwVec3d& vec)
18 {
19  const ::glm::dvec3 vecGlm = ::glm::make_vec3<double>(vec.data());
20  const double length = vecLength(vec);
21  const ::glm::dvec3 normalizedVec = ::glm::normalize(vecGlm);
22  vec[0] = normalizedVec[0];
23  vec[1] = normalizedVec[1];
24  vec[2] = normalizedVec[2];
25  return length;
26 }
27 
28 //------------------------------------------------------------------------------
29 
30 fwVec3d normalized(const fwVec3d& vec)
31 {
32  fwVec3d normalized = vec;
33  normalize(normalized);
34  return normalized;
35 }
36 
37 //------------------------------------------------------------------------------
38 
39 double dot(const fwVec3d& v1, const fwVec3d& v2)
40 {
41  // Using GLM here is slower, especially in debug, so keep it simple
42  return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
43 }
44 
45 //------------------------------------------------------------------------------
46 
47 fwVec3d cross(const fwVec3d& v1, const fwVec3d& v2)
48 {
49  const ::glm::dvec3 vecGlm1 = ::glm::make_vec3<double>(v1.data());
50  const ::glm::dvec3 vecGlm2 = ::glm::make_vec3<double>(v2.data());
51 
52  ::glm::dvec3 res = ::glm::cross(vecGlm1, vecGlm2);
53 
54  return {{res[0], res[1], res[2]}};
55 }
56 
57 //------------------------------------------------------------------------------
58 
59 double vecLength(const fwVec3d &vec)
60 {
61  ::glm::dvec3 vecGLM (vec[0], vec[1], vec[2]);
62  return ::glm::length(vecGLM);
63 }
64 
65 //------------------------------------------------------------------------------
66 
67 void negate(fwVec3d &vec)
68 {
69  vec[0] = -vec[0];
70  vec[1] = -vec[1];
71  vec[2] = -vec[2];
72 }
73 } //namespace fwMath
74 
75 //------------------------------------------------------------------------------
76 
77 fwVec3d& operator*=(fwVec3d& vec1, double val)
78 {
79  ::glm::dvec3 vecGlm1(vec1[0], vec1[1], vec1[2]);
80  vecGlm1 *= val;
81  vec1 = {{vecGlm1[0], vecGlm1[1], vecGlm1[2]}};
82 
83  return vec1;
84 }
85 
86 //------------------------------------------------------------------------------
87 
88 fwVec3d& operator/=(fwVec3d& vec, double val )
89 {
90  FW_RAISE_IF("Division by 0 not possible.", val == 0.0);
91 
92  ::glm::dvec3 vecGlm1 = ::glm::make_vec3<double>(vec.data());
93  vecGlm1 /= val;
94  vec = {{vecGlm1[0], vecGlm1[1], vecGlm1[2]}};
95 
96  return vec;
97 }
98 
99 //------------------------------------------------------------------------------
100 
101 fwVec3d& operator+=(fwVec3d& vec1, const fwVec3d& vec2)
102 {
103  ::glm::dvec3 vecGlm1 = ::glm::make_vec3<double>(vec1.data());
104  ::glm::dvec3 vecGlm2 = ::glm::make_vec3<double>(vec2.data());
105  vecGlm1 += vecGlm2;
106  vec1 = {{vecGlm1[0], vecGlm1[1], vecGlm1[2]}};
107 
108  return vec1;
109 }
110 
111 //------------------------------------------------------------------------------
112 
113 fwVec3d& operator-=(fwVec3d& vec1, const fwVec3d& vec2)
114 {
115  ::glm::dvec3 vecGlm1 = ::glm::make_vec3<double>(vec1.data());
116  ::glm::dvec3 vecGlm2 = ::glm::make_vec3<double>(vec2.data());
117  vecGlm1 -= vecGlm2;
118  vec1 = {{vecGlm1[0], vecGlm1[1], vecGlm1[2]}};
119 
120  return vec1;
121 }
122 
123 //------------------------------------------------------------------------------
124 
125 fwVec3d operator*(const fwVec3d& vec, double val)
126 {
127  ::glm::dvec3 vecGlm1(vec[0], vec[1], vec[2]);
128  ::glm::dvec3 vecGlm = vecGlm1 * val;
129 
130  return {{vecGlm[0], vecGlm[1], vecGlm[2]}};
131 }
132 
133 //------------------------------------------------------------------------------
134 
135 fwVec3d operator*(const double val, const fwVec3d& vec)
136 {
137  return vec * val;
138 }
139 
140 //------------------------------------------------------------------------------
141 
142 fwVec3d operator/(const fwVec3d& vec, double val)
143 {
144  FW_RAISE_IF("Division by 0 not possible.", val == 0.0);
145 
146  ::glm::dvec3 vecGlm1(vec[0], vec[1], vec[2]);
147  ::glm::dvec3 vecGlm = vecGlm1 / val;
148 
149  return {{vecGlm[0], vecGlm[1], vecGlm[2]}};
150 }
151 
152 //------------------------------------------------------------------------------
153 
154 fwVec3d operator+(const fwVec3d& vec1, const fwVec3d& vec2)
155 {
156  const ::glm::dvec3 vecGlm1 = ::glm::make_vec3<double>(vec1.data());
157  const ::glm::dvec3 vecGlm2 = ::glm::make_vec3<double>(vec2.data());
158  ::glm::dvec3 vecGlm = vecGlm1 + vecGlm2;
159 
160  return {{vecGlm[0], vecGlm[1], vecGlm[2]}};
161 }
162 
163 //------------------------------------------------------------------------------
164 
165 fwVec3d operator-(const fwVec3d& vec1, const fwVec3d& vec2)
166 {
167  const ::glm::dvec3 vecGlm1 = ::glm::make_vec3<double>(vec1.data());
168  const ::glm::dvec3 vecGlm2 = ::glm::make_vec3<double>(vec2.data());
169  ::glm::dvec3 vecGlm = vecGlm1 - vecGlm2;
170 
171  return {{vecGlm[0], vecGlm[1], vecGlm[2]}};
172 }
173 
174 //------------------------------------------------------------------------------
175 
176 int operator==(const fwVec3d& vec1, const fwVec3d& vec2)
177 {
178  const ::glm::dvec3 vecGlm1 = ::glm::make_vec3<double>(vec1.data());
179  const ::glm::dvec3 vecGlm2 = ::glm::make_vec3<double>(vec2.data());
180  ::glm::bvec3 res = ::glm::equal(vecGlm1, vecGlm2);
181 
182  return res[0] && res[1] && res[2];
183 }
184 
185 //------------------------------------------------------------------------------
186 
187 int operator!=(const fwVec3d& vec1, const fwVec3d& vec2)
188 {
189  return !(vec1 == vec2);
190 }
191 
FWMATH_API double vecLength(const fwVec3d &_vec)
Compute the length of the vector.
FWMATH_API fwVec3d normalized(const fwVec3d &vec)
Return a normalized vector.
FWMATH_API fwVec3d cross(const fwVec3d &v1, const fwVec3d &v2)
Compute the Cross product between two vectors.
The namespace fwMath contains classes which provide the implementation of several mathematic function...
Definition: Compare.hpp:12
FWMATH_API void negate(fwVec3d &_vec)
Compute the negate of the vector.
FWMATH_API double dot(const fwVec3d &v1, const fwVec3d &v2)
Compute the Dot product between two vectors.
FWMATH_API double normalize(fwVec3d &vec)
Normalize a vector.