IPSDK  4_1_0_2
IPSDK : Image Processing Software Development Kit
Angles3d.h
Go to the documentation of this file.
1 // Angles3d.h:
3 // -----------
4 //
14 
15 #ifndef __IPSDKMATH_ANGLES3D_H__
16 #define __IPSDKMATH_ANGLES3D_H__
17 
23 
24 namespace ipsdk {
25 namespace math {
26 
29 
34 template <typename TOut, typename TIn>
35 IPSDK_FORCEINLINE TOut
36 sphericalToXCartesian(const TIn rho,
37  const TIn cosTheta, const TIn sinTheta,
38  const TIn cosPhi, const TIn sinPhi)
39 {
40  return math::round<TOut>(rho * sinTheta * cosPhi);
41 }
42 template <typename TOut, typename TIn>
43 IPSDK_FORCEINLINE TOut
44 sphericalToYCartesian(const TIn rho,
45  const TIn cosTheta, const TIn sinTheta,
46  const TIn cosPhi, const TIn sinPhi)
47 {
48  return math::round<TOut>(rho * sinTheta * sinPhi);
49 }
50 template <typename TOut, typename TIn>
51 IPSDK_FORCEINLINE TOut
52 sphericalToZCartesian(const TIn rho,
53  const TIn cosTheta, const TIn sinTheta,
54  const TIn cosPhi, const TIn sinPhi)
55 {
56  return math::round<TOut>(rho * cosTheta);
57 }
58 template <typename TOut, typename TIn>
59 IPSDK_FORCEINLINE void
60 sphericalToCartesian(const TIn rho, const TIn cosTheta, const TIn sinTheta,
61  const TIn cosPhi, const TIn sinPhi, TOut& x, TOut& y, TOut& z)
62 {
63  x = sphericalToXCartesian<TOut>(rho, cosTheta, sinTheta, cosPhi, sinPhi);
64  y = sphericalToYCartesian<TOut>(rho, cosTheta, sinTheta, cosPhi, sinPhi);
65  z = sphericalToZCartesian<TOut>(rho, cosTheta, sinTheta, cosPhi, sinPhi);
66 }
67 template <typename TOut, typename TIn>
68 IPSDK_FORCEINLINE void
69 sphericalToCartesian(const TIn rho, const TIn theta, const TIn phi,
70  TOut& x, TOut& y, TOut& z)
71 {
72  sphericalToCartesian(rho, std::cos(theta), std::sin(theta),
73  std::cos(phi), std::sin(phi), x, y, z);
74 }
76 
80 template <typename TOut, typename TIn>
81 IPSDK_FORCEINLINE void
82 cartesianToSpherical(const TIn x, const TIn y, const TIn z,
83  TOut& rho, TOut& theta, TOut& phi)
84 {
85  rho = math::round<TOut>(normL2(x, y, z));
86  if (rho != 0)
87  theta = std::acos(z / rho);
88  else
89  theta = 0;
90  phi = std::atan2(y, x);
91 }
92 
97 template <typename T>
98 IPSDK_FORCEINLINE void
99 eulerToMatrix(const T cosChi, const T sinChi,
100  const T cosBeta, const T sinBeta,
101  const T cosAlpha, const T sinAlpha,
102  T& rxx, T& rxy, T& rxz,
103  T& ryx, T& ryy, T& ryz,
104  T& rzx, T& rzy, T& rzz)
105 {
106  rxx = cosChi*cosBeta;
107  rxy = cosChi*sinBeta*sinAlpha - sinChi*cosAlpha;
108  rxz = cosChi*sinBeta*cosAlpha + sinChi*sinAlpha;
109  ryx = sinChi*cosBeta;
110  ryy = sinChi*sinBeta*sinAlpha + cosChi*cosAlpha;
111  ryz = sinChi*sinBeta*cosAlpha - cosChi*sinAlpha;
112  rzx = -sinBeta;
113  rzy = cosBeta*sinAlpha;
114  rzz = cosBeta*cosAlpha;
115 }
116 template <typename T>
117 IPSDK_FORCEINLINE void
118 eulerToMatrix(const T chi, const T beta, const T alpha,
119  T& rxx, T& rxy, T& rxz,
120  T& ryx, T& ryy, T& ryz,
121  T& rzx, T& rzy, T& rzz)
122 {
123  eulerToMatrix(std::cos(chi), std::sin(chi),
124  std::cos(beta), std::sin(beta),
125  std::cos(alpha), std::sin(alpha),
126  rxx, rxy, rxz,
127  ryx, ryy, ryz,
128  rzx, rzy, rzz);
129 }
130 IPSDK_FORCEINLINE Matrix3d
131 eulerToMatrix(const ipReal64 chi, const ipReal64 beta, const ipReal64 alpha)
132 {
133  Matrix3d matRot;
134  eulerToMatrix(chi, beta, alpha,
135  matRot(0, 0), matRot(0, 1), matRot(0, 2),
136  matRot(1, 0), matRot(1, 1), matRot(1, 2),
137  matRot(2, 0), matRot(2, 1), matRot(2, 2));
138 
139  return matRot;
140 }
142 
149 template <typename T>
150 IPSDK_FORCEINLINE void
151 matrixToEuler(const T rxx, const T rxy, const T rxz,
152  const T ryx, const T ryy, const T ryz,
153  const T rzx, const T rzy, const T rzz,
154  T& chi, T& beta, T& alpha)
155 {
156  // retrieve epsilon value
157  const T epsilonValue = NumericLimits<T>::sqrt_epsilon();
158 
159  // handle degenerate case
160  if ((1-std::abs(rzx)) > epsilonValue) {
161 
162  beta = -std::asin(rzx);
163  const T cosBeta = cos(beta);
164  chi = std::atan2(ryx/cosBeta, rxx/cosBeta);
165 
166  alpha = std::atan2(rzy/cosBeta, rzz/cosBeta);
167  }
168  else { // degenerate case
169 
170  chi = 0;
171  if (rzx < 0) {
172  beta = static_cast<T>(M_PI / 2);
173  alpha = std::atan2(rxy, rxz);
174  }
175  else {
176  beta = static_cast<T>(-M_PI / 2);
177  alpha = std::atan2(-rxy, -rxz);
178  }
179  }
180 }
181 IPSDK_FORCEINLINE void
182 matrixToEuler(const Matrix3d& matRot,
183  ipReal64& chi, ipReal64& beta, ipReal64& alpha)
184 {
185  matrixToEuler(matRot(0, 0), matRot(0, 1), matRot(0, 2),
186  matRot(1, 0), matRot(1, 1), matRot(1, 2),
187  matRot(2, 0), matRot(2, 1), matRot(2, 2),
188  chi, beta, alpha);
189 }
191 
196 IPSDKMATH_API void
197 eulerToQuaternion(const ipReal64 chi, const ipReal64 beta, const ipReal64 alpha,
198  ipReal64& q0, ipReal64& q1, ipReal64& q2, ipReal64& q3);
199 
204 IPSDKMATH_API void
205 quaternionToEuler(const ipReal64 q0, const ipReal64 q1, const ipReal64 q2, const ipReal64 q3,
206  ipReal64& chi, ipReal64& beta, ipReal64& alpha);
207 
213 template <typename T>
214 IPSDK_FORCEINLINE void
215 rotate3d(const T xIn, const T yIn, const T zIn,
216  const ipReal64 rxx, const ipReal64 rxy, const ipReal64 rxz,
217  const ipReal64 ryx, const ipReal64 ryy, const ipReal64 ryz,
218  const ipReal64 rzx, const ipReal64 rzy, const ipReal64 rzz,
219  T& xOut, T& yOut, T& zOut)
220 {
221  xOut = math::round<T>(rxx * xIn + rxy * yIn + rxz * zIn);
222  yOut = math::round<T>(ryx * xIn + ryy * yIn + ryz * zIn);
223  zOut = math::round<T>(rzx * xIn + rzy * yIn + rzz * zIn);
224 }
225 template <typename T>
226 IPSDK_FORCEINLINE void
227 rotate3dInSitu(const ipReal64 rxx, const ipReal64 rxy, const ipReal64 rxz,
228  const ipReal64 ryx, const ipReal64 ryy, const ipReal64 ryz,
229  const ipReal64 rzx, const ipReal64 rzy, const ipReal64 rzz,
230  T& x, T& y, T& z)
231 {
232  T xOut, yOut, zOut;
233  rotate3d(x, y, z,
234  rxx, rxy, rxz,
235  ryx, ryy, ryz,
236  rzx, rzy, rzz,
237  xOut, yOut, zOut);
238  x = xOut;
239  y = yOut;
240  z = zOut;
241 }
242 template <typename T>
243 IPSDK_FORCEINLINE void
244 rotate3d(const T xIn, const T yIn, const T zIn,
245  const ipReal64 cosChi, const ipReal64 sinChi,
246  const ipReal64 cosBeta, const ipReal64 sinBeta,
247  const ipReal64 cosAlpha, const ipReal64 sinAlpha,
248  T& xOut, T& yOut, T& zOut)
249 {
250  ipReal64 rxx, rxy, rxz, ryx, ryy, ryz, rzx, rzy, rzz;
251  eulerToMatrix(cosChi, sinChi, cosBeta, sinBeta, cosAlpha, sinAlpha,
252  rxx, rxy, rxz, ryx, ryy, ryz, rzx, rzy, rzz);
253  rotate3d(xIn, yIn, zIn,
254  rxx, rxy, rxz,
255  ryx, ryy, ryz,
256  rzx, rzy, rzz,
257  xOut, yOut, zOut);
258 }
259 template <typename T>
260 IPSDK_FORCEINLINE void
261 rotate3dInSitu(const ipReal64 cosChi, const ipReal64 sinChi,
262  const ipReal64 cosBeta, const ipReal64 sinBeta,
263  const ipReal64 cosAlpha, const ipReal64 sinAlpha,
264  T& x, T& y, T& z)
265 {
266  T xOut, yOut, zOut;
267  rotate3d(x, y, z,
268  cosChi, sinChi,
269  cosBeta, sinBeta,
270  cosAlpha, sinAlpha,
271  xOut, yOut, zOut);
272  x = xOut;
273  y = yOut;
274  z = zOut;
275 }
276 template <typename T>
277 IPSDK_FORCEINLINE void
278 rotate3d(const T xIn, const T yIn, const T zIn,
279  const ipReal64 chi, const ipReal64 beta, const ipReal64 alpha,
280  T& xOut, T& yOut, T& zOut)
281 {
282  rotate3d(xIn, yIn, zIn,
283  std::cos(chi), std::sin(chi),
284  std::cos(beta), std::sin(beta),
285  std::cos(alpha), std::sin(alpha),
286  xOut, yOut, zOut);
287 }
289 
292 
293 } // end of namespace math
294 } // end of namespace ipsdk
295 
296 #endif // __IPSDKMATH_ANGLES3D_H__
Definition of import/export macro for library.
Main namespace for IPSDK library.
Definition: AlgorithmFunctionEfficiency.h:22
IPSDKMATH_API void quaternionToEuler(const ipReal64 q0, const ipReal64 q1, const ipReal64 q2, const ipReal64 q3, ipReal64 &chi, ipReal64 &beta, ipReal64 &alpha)
Convertion from quaternion representation to euler angles.
IPSDK_FORCEINLINE void eulerToMatrix(const T cosChi, const T sinChi, const T cosBeta, const T sinBeta, const T cosAlpha, const T sinAlpha, T &rxx, T &rxy, T &rxz, T &ryx, T &ryy, T &ryz, T &rzx, T &rzy, T &rzz)
Convertion from euler angles to rotation matrix.
Definition: Angles3d.h:99
Predefined types for linear algebra management.
double ipReal64
Base types definition.
Definition: BaseTypes.h:57
IPSDKMATH_API void eulerToQuaternion(const ipReal64 chi, const ipReal64 beta, const ipReal64 alpha, ipReal64 &q0, ipReal64 &q1, ipReal64 &q2, ipReal64 &q3)
Convertion from euler angles to a quaternion representation.
Definition: NumericLimits.h:27
IPSDK_FORCEINLINE TOut sphericalToXCartesian(const TIn rho, const TIn cosTheta, const TIn sinTheta, const TIn cosPhi, const TIn sinPhi)
spherical to cartesian convertion
Definition: Angles3d.h:36
#define IPSDKMATH_API
Import/Export macro for library IPSDKMath.
Definition: IPSDKMathExports.h:27
Utility functions for angle management.
IPSDK_FORCEINLINE void sphericalToCartesian(const TIn rho, const TIn cosTheta, const TIn sinTheta, const TIn cosPhi, const TIn sinPhi, TOut &x, TOut &y, TOut &z)
spherical to cartesian convertion
Definition: Angles3d.h:60
IPSDK_FORCEINLINE void matrixToEuler(const T rxx, const T rxy, const T rxz, const T ryx, const T ryy, const T ryz, const T rzx, const T rzy, const T rzz, T &chi, T &beta, T &alpha)
Convertion from rotation matrix to euler angles.
Definition: Angles3d.h:151
Utility functions for ipsdk math library.
Utility functions used to handle points 3d.
boost::numeric::ublas::bounded_matrix< ipReal64, 3, 3 > Matrix3d
3d matrix (3x3) type associated to library
Definition: LinearAlgebraTypes.h:54
IPSDK_FORCEINLINE void rotate3d(const T xIn, const T yIn, const T zIn, const ipReal64 rxx, const ipReal64 rxy, const ipReal64 rxz, const ipReal64 ryx, const ipReal64 ryy, const ipReal64 ryz, const ipReal64 rzx, const ipReal64 rzy, const ipReal64 rzz, T &xOut, T &yOut, T &zOut)
rotation of a point using a rotation angle (rotation around origin)
Definition: Angles3d.h:215
IPSDK_FORCEINLINE void cartesianToSpherical(const TIn x, const TIn y, const TIn z, TOut &rho, TOut &theta, TOut &phi)
cartesian to spherical convertion
Definition: Angles3d.h:82
IPSDK_FORCEINLINE TOut sphericalToYCartesian(const TIn rho, const TIn cosTheta, const TIn sinTheta, const TIn cosPhi, const TIn sinPhi)
spherical to cartesian convertion
Definition: Angles3d.h:44
IPSDK_FORCEINLINE void rotate3dInSitu(const ipReal64 rxx, const ipReal64 rxy, const ipReal64 rxz, const ipReal64 ryx, const ipReal64 ryy, const ipReal64 ryz, const ipReal64 rzx, const ipReal64 rzy, const ipReal64 rzz, T &x, T &y, T &z)
rotation of a point using a rotation angle (rotation around origin)
Definition: Angles3d.h:227
IPSDK_FORCEINLINE PackT abs(const PackT &in)
returns the absolute value of a pack
Definition: abs.h:41
IPSDK_FORCEINLINE TOut sphericalToZCartesian(const TIn rho, const TIn cosTheta, const TIn sinTheta, const TIn cosPhi, const TIn sinPhi)
spherical to cartesian convertion
Definition: Angles3d.h:52
IPSDK_FORCEINLINE ipReal64 normL2(const T x, const T y)
L2 norm of a 2d vector.
Definition: Vector2d.h:32