File: itkVersor.h

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (360 lines) | stat: -rw-r--r-- 10,652 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#ifndef itkVersor_h
#define itkVersor_h

#include "itkMatrix.h"
#include "vnl/vnl_quaternion.h"
#include "vnl/vnl_vector_fixed.h"

namespace itk
{
/** \class Versor
 * \brief A templated class holding a unit quaternion.
 *
 * Versor is a templated class that holds a unit quaternion.
 * The difference between versors and quaternions is that quaternions
 * can represent rotations and scale changes while versors are limited
 * to rotations.
 *
 * This class only implements the operations that maintain versors as
 * a group, that is, any operations between versors result in another
 * versor. For this reason, addition is not defined in this class, even
 * though it is a valid operation between quaternions.
 *
 * \ingroup Geometry
 * \ingroup DataRepresentation
 *
 * \sa Vector
 * \sa Point
 * \sa CovariantVector
 * \sa Matrix
 * \ingroup ITKCommon
 */
template <typename T>
class ITK_TEMPLATE_EXPORT Versor
{
public:
  /** Standard class type aliases. */
  using Self = Versor;

  /** ValueType can be used to declare a variable that is the same type
   * as a data element held in a Versor.   */
  using ValueType = T;

  /** Type used for computations on the versor components */
  using RealType = typename NumericTraits<ValueType>::RealType;

  /** Vector type used to represent the axis. */
  using VectorType = Vector<T, 3>;

  /** Point type.  */
  using PointType = Point<T, 3>;

  /** CovariantVector type.  */
  using CovariantVectorType = CovariantVector<T, 3>;

  /** Vnl Vector type.  */
  using VnlVectorType = vnl_vector_fixed<T, 3>;

  /** Vnl Quaternion type.  */
  using VnlQuaternionType = vnl_quaternion<T>;

  /** Type of the rotation matrix equivalent to the Versor */
  using MatrixType = Matrix<T, 3, 3>;

  /** Get a vnl_quaternion with a copy of the internal memory block. */
  vnl_quaternion<T>
  GetVnlQuaternion() const;

  /** Set the Versor from a Quaternion
   \warning After assignment, the corresponding quaternion will
            be normalized in order to get a consistent Versor.  */
  void
  Set(const VnlQuaternionType &);

  /** Set the Versor from Quaternion components.
   \warning After assignment, the corresponding quaternion will be normalized
   in order to get a consistent Versor.  Also, if the "w" component is
   negative, the four components will be negated in order to produce a
   quaternion where "w" is positive, since this is implicitly assumed in other
   sections of the code, in particular when "w" is computed from (x,y,z) via
   normalization. The reason why it is valid to negate all the components is
   that the rotation by angle \f$\theta\f$, is represented by \f$\sin(\frac{\theta}{2})\f$
   in the (x,y,z) components and by \f$\cos(\frac{\theta}{2})\f$ in the "w"
   component. The rotation by any \f$\theta\f$ should be equivalent to a rotation by
   \f$\theta + n \times \pi\f$, therefore we should be able to replace
   \f$\sin(\frac{\theta}{2})\f$ with \f$\sin(\frac{\theta}{2} + n \times \pi )\f$ and
   \f$\cos(\frac{\theta}{2})\f$ with \f$\cos(\frac{\theta}{2} + n \times \pi )\f$.
   Considering that \f$\cos( n \times \pi ) = (-1)^{n}\f$ we can conclude that if we
   simultaneously change the signs of all the Versor components, the rotation
   that it represents remains unchanged.
   */
  void
  Set(T x, T y, T z, T w);

  /** Default constructor creates a null versor
   * (representing 0 degrees  rotation). */
  Versor() = default;

  /** Copy constructor.  */
  Versor(const Self & v);

  /** Assignment operator =.  Copy the versor argument. */
  Self &
  operator=(const Self & v);

  /** Composition operator *=.  Compose the current versor
   * with the operand and store the result in the current
   * versor. */
  const Self &
  operator*=(const Self & v);

  /** Division operator /=.  Divide the current versor
   * with the operand and store the result in the current
   * versor. This is equivalent to compose the Versor with
   * the reciprocal of the operand \sa GetReciprocal */
  const Self &
  operator/=(const Self & v);

  /** Get Tensor part of the Versor.
   * Given that Versors are normalized quaternions this value
   * is expected to be 1.0 always  */
  ValueType
  GetTensor() const;

  /** Normalize the Versor.
   * Given that Versors are normalized quaternions this method
   * is provided only for convenience when it is suspected that
   * a versor could be out of the unit sphere.   */
  void
  Normalize();

  /** Get Conjugate versor.  Returns the versor that produce
   * a rotation by the same angle but in opposite direction. */
  Self
  GetConjugate() const;

  /** Get Reciprocal versor.  Returns the versor that composed
   * with this one will result in a scalar operator equals to 1.
   * It is also equivalent to 1/this. */
  Self
  GetReciprocal() const;

  /** Versor operator*.  Performs the composition of two versors.
   * this operation is NOT commutative. */
  Self operator*(const Self & v) const;

  /** Versor operator/.  Performs the division of two versors. */
  Self
  operator/(const Self & v) const;

  /** Versor operator==  Performs the comparison between two versors.
   * this operation uses an arbitrary threshold for the comparison.  */
  bool
  operator==(const Self & v) const;

  ITK_UNEQUAL_OPERATOR_MEMBER_FUNCTION(Self);

  /** Returns the Scalar part. */
  ValueType
  GetScalar() const;

  /** Returns the X component. */
  ValueType
  GetX() const
  {
    return m_X;
  }

  /** Returns the Y component. */
  ValueType
  GetY() const
  {
    return m_Y;
  }

  /** Returns the Z component. */
  ValueType
  GetZ() const
  {
    return m_Z;
  }

  /** Returns the W component. */
  ValueType
  GetW() const
  {
    return m_W;
  }

  /** Returns the rotation angle in radians. */
  ValueType
  GetAngle() const;

  /** Returns the axis of the rotation.
   * It is a unit vector parallel to the axis. */
  VectorType
  GetAxis() const;

  /** Returns the Right part
   * It is a vector part of the Versor. It is
   * called Right because it is equivalent to
   * a right angle rotation. */
  VectorType
  GetRight() const;

  /** Set the versor using a vector and angle.
   *
   * The unit vector parallel to the given vector will be used. The angle is expected in radians.
   */
  void
  Set(const VectorType & axis, ValueType angle);

  /** Set the versor using an orthogonal matrix.
   *  Based on code from:
   *  https://www.euclideanspace.com/maths/geometry/rotations/
   *  conversions/matrixToQuaternion/index.htm
   */
  void
  Set(const MatrixType & mat);

  /** Set the versor using the right part.
   * the magnitude of the vector given is assumed to
   * be equal to std::sin(angle/2).
   * This method will compute internally the scalar
   * part that preserve the Versor as a unit quaternion. */
  void
  Set(const VectorType & axis);

  /** Sets a rotation around the X axis using the parameter
   * as angle in radians. This is a method provided for
   * convenience to initialize a rotation. The effect of
   * this methods is not cumulative with any value previously
   * stored in the Versor.
   * \sa Set \sa SetRotationAroundY \sa SetRotationAroundZ */
  void
  SetRotationAroundX(ValueType angle);

  /** Sets a rotation around the Y axis using the parameter
   * as angle in radians. This is a method provided for
   * convenience to initialize a rotation. The effect of
   * this methods is not cumulative with any value previously
   * stored in the Versor.
   * \sa Set \sa SetRotationAroundX \sa SetRotationAroundZ */
  void
  SetRotationAroundY(ValueType angle);

  /** Sets a rotation around the Y axis using the parameter
   * as angle in radians. This is a method provided for
   * convenience to initialize a rotation. The effect of
   * this methods is not cumulative with any value previously
   * stored in the Versor.
   * \sa Set \sa SetRotationAroundX \sa SetRotationAroundY */
  void
  SetRotationAroundZ(ValueType angle);

  /** Reset the values so the versor is equivalent to an identity
   *  transformation. This is equivalent to set a zero angle */
  void
  SetIdentity();

  /** Transform a vector. */
  VectorType
  Transform(const VectorType & v) const;

  /** Transform a covariant vector.
   *
   * Given that this is an orthogonal transformation CovariantVectors are transformed as vectors.
   */
  CovariantVectorType
  Transform(const CovariantVectorType & v) const;

  /** Transform a point. */
  PointType
  Transform(const PointType & v) const;

  /** Transform a vnl_vector. */
  VnlVectorType
  Transform(const VnlVectorType & v) const;

  /** Get the matrix representation. */
  MatrixType
  GetMatrix() const;

  /** Get the Square root of the unit quaternion. */
  Self
  SquareRoot() const;

  /** Compute the Exponential of the unit quaternion.
   * Exponentiation by a factor is equivalent to
   * multiplication of the rotation angle of the quaternion. */
  Self
  Exponential(ValueType exponent) const;

private:
  /** use different epsilon for float and double */
  static inline ValueType
  Epsilon(double *)
  {
    return 1e-10;
  }
  static inline ValueType
  Epsilon(float *)
  {
    return 1e-7;
  }
  static inline ValueType
  Epsilon()
  {
    return Epsilon((ValueType *)nullptr);
  }

  /** Component parallel to x axis.  */
  ValueType m_X{};

  /** Component parallel to y axis.  */
  ValueType m_Y{};

  /** Component parallel to z axis.  */
  ValueType m_Z{};

  /** Escalar component of the Versor.  */
  ValueType m_W{ NumericTraits<T>::OneValue() };
};

template <typename T>
std::ostream &
operator<<(std::ostream & os, const Versor<T> & v)
{
  os << "[ ";
  os << v.GetX() << ", " << v.GetY() << ", ";
  os << v.GetZ() << ", " << v.GetW() << " ]";
  return os;
}

template <typename T>
std::istream &
operator>>(std::istream & is, Versor<T> & v);
} // end namespace itk

#ifndef ITK_MANUAL_INSTANTIATION
#  include "itkVersor.hxx"
#endif

#endif