HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathFrame.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5 
6 //
7 // Functions for computing reference frames.
8 //
9 
10 #ifndef INCLUDED_IMATHFRAME_H
11 #define INCLUDED_IMATHFRAME_H
12 
13 #include "ImathNamespace.h"
14 
15 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
16 
17 /// @cond Doxygen_Suppress
18 template <class T> class Vec3;
19 template <class T> class Matrix44;
20 /// @endcond
21 
22 ///
23 /// @{
24 /// @name Functions for computing reference frames
25 ///
26 /// These methods compute a set of reference frames, defined by their
27 /// transformation matrix, along a curve. It is designed so that the
28 /// array of points and the array of matrices used to fetch these
29 /// routines don't need to be ordered as the curve.
30 ///
31 /// A typical usage would be :
32 ///
33 /// m[0] = IMATH_INTERNAL_NAMESPACE::firstFrame( p[0], p[1], p[2] );
34 /// for( int i = 1; i < n - 1; i++ )
35 /// {
36 /// m[i] = IMATH_INTERNAL_NAMESPACE::nextFrame( m[i-1], p[i-1], p[i], t[i-1], t[i] );
37 /// }
38 /// m[n-1] = IMATH_INTERNAL_NAMESPACE::lastFrame( m[n-2], p[n-2], p[n-1] );
39 ///
40 /// See Graphics Gems I for the underlying algorithm.
41 
42 template <class T>
43 Matrix44<T> constexpr firstFrame (
44  const Vec3<T>&, // First point
45  const Vec3<T>&, // Second point
46  const Vec3<T>&) IMATH_NOEXCEPT; // Third point
47 
48 template <class T>
49 Matrix44<T> constexpr nextFrame (
50  const Matrix44<T>&, // Previous matrix
51  const Vec3<T>&, // Previous point
52  const Vec3<T>&, // Current point
53  Vec3<T>&, // Previous tangent
54  Vec3<T>&) IMATH_NOEXCEPT; // Current tangent
55 
56 template <class T>
57 Matrix44<T> constexpr lastFrame (
58  const Matrix44<T>&, // Previous matrix
59  const Vec3<T>&, // Previous point
60  const Vec3<T>&) IMATH_NOEXCEPT; // Last point
61 
62 ///
63 /// Compute the first reference frame along a curve.
64 ///
65 /// This function returns the transformation matrix to the reference
66 /// frame defined by the three points `pi`, `pj` and `pk`. Note that
67 /// if the two vectors <`pi`,`pj`> and <`pi`,`pk`> are colinears, an
68 /// arbitrary twist value will be choosen.
69 ///
70 /// Throw `std::domain_error` if `pi` and `pj` are equal.
71 ///
72 /// @param pi
73 /// First point
74 /// @param pj
75 /// Second point
76 /// @param pk
77 /// Third point
78 ///
79 template <class T>
80 Matrix44<T> constexpr firstFrame (
81  const Vec3<T>& pi, // first point
82  const Vec3<T>& pj, // secont point
83  const Vec3<T>& pk) IMATH_NOEXCEPT // third point
84 {
85  Vec3<T> t = pj - pi;
86  t.normalizeExc ();
87 
88  Vec3<T> n = t.cross (pk - pi);
89  n.normalize ();
90  if (n.length () == 0.0f)
91  {
92  Vec3<T> v (0.0, 0.0, 0.0);
93 
94  int i = fabs (t.x) < fabs (t.y) ? 0 : 1;
95  if (fabs (t.z) < fabs (t[i])) i = 2;
96 
97  v[i] = 1.0;
98  n = t.cross (v);
99  n.normalize ();
100  }
101 
102  Vec3<T> b = t.cross (n);
103 
104  Matrix44<T> M;
105 
106  M[0][0] = t.x;
107  M[0][1] = t.y;
108  M[0][2] = t.z;
109  M[0][3] = 0.0;
110  M[1][0] = n.x;
111  M[1][1] = n.y;
112  M[1][2] = n.z;
113  M[1][3] = 0.0;
114  M[2][0] = b.x;
115  M[2][1] = b.y;
116  M[2][2] = b.z;
117  M[2][3] = 0.0;
118  M[3][0] = pi.x;
119  M[3][1] = pi.y;
120  M[3][2] = pi.z;
121  M[3][3] = 1.0;
122 
123  return M;
124 }
125 
126 ///
127 /// Compute the next reference frame along a curve.
128 ///
129 /// This function returns the transformation matrix to the next reference
130 /// frame defined by the previously computed transformation matrix and the
131 /// new point and tangent vector along the curve.
132 ///
133 /// @param Mi
134 /// The previous matrix
135 /// @param pi
136 /// The previous point
137 /// @param pj
138 /// The current point
139 /// @param ti
140 /// The previous tangent vector
141 /// @param tj
142 /// The current tangent vector
143 
144 template <class T>
146  const Matrix44<T>& Mi, // Previous matrix
147  const Vec3<T>& pi, // Previous point
148  const Vec3<T>& pj, // Current point
149  Vec3<T>& ti, // Previous tangent vector
150  Vec3<T>& tj) IMATH_NOEXCEPT // Current tangent vector
151 {
152  Vec3<T> a (0.0, 0.0, 0.0); /// Rotation axis.
153  T r = 0.0; // Rotation angle.
154 
155  if (ti.length () != 0.0 && tj.length () != 0.0)
156  {
157  ti.normalize ();
158  tj.normalize ();
159  T dot = ti.dot (tj);
160 
161  //
162  // This is *really* necessary :
163  //
164 
165  if (dot > 1.0)
166  dot = 1.0;
167  else if (dot < -1.0)
168  dot = -1.0;
169 
170  r = acosf (dot);
171  a = ti.cross (tj);
172  }
173 
174  if (a.length () != 0.0 && r != 0.0)
175  {
176  Matrix44<T> R;
177  R.setAxisAngle (a, r);
178  Matrix44<T> Tj;
179  Tj.translate (pj);
180  Matrix44<T> Ti;
181  Ti.translate (-pi);
182 
183  return Mi * Ti * R * Tj;
184  }
185  else
186  {
187  Matrix44<T> Tr;
188  Tr.translate (pj - pi);
189 
190  return Mi * Tr;
191  }
192 }
193 
194 ///
195 /// Compute the last reference frame along a curve.
196 ///
197 /// This function returns the transformation matrix to the last reference
198 /// frame defined by the previously computed transformation matrix and the
199 /// last point along the curve.
200 ///
201 /// @param Mi
202 /// The previous matrix
203 /// @param pi
204 /// The previous point
205 /// @param pj
206 /// The last point
207 
208 template <class T>
210  const Matrix44<T>& Mi, // Previous matrix
211  const Vec3<T>& pi, // Previous point
212  const Vec3<T>& pj) IMATH_NOEXCEPT // Last point
213 {
214  Matrix44<T> Tr;
215  Tr.translate (pj - pi);
216 
217  return Mi * Tr;
218 }
219 
220 /// @}
221 
222 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
223 
224 #endif // INCLUDED_IMATHFRAME_H
T z
Definition: ImathVec.h:368
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:78
Definition: ImathVec.h:40
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
IMATH_HOSTDEVICE T length() const IMATH_NOEXCEPT
Return the Euclidean norm.
Definition: ImathVec.h:2064
const GLdouble * v
Definition: glcorearb.h:837
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
T x[4][4]
Matrix elements.
Definition: ImathMatrix.h:809
GLdouble n
Definition: glcorearb.h:2008
Matrix44< T > constexpr nextFrame(const Matrix44< T > &, const Vec3< T > &, const Vec3< T > &, Vec3< T > &, Vec3< T > &) IMATH_NOEXCEPT
Definition: ImathFrame.h:145
T x
Definition: ImathVec.h:368
Matrix44< T > constexpr lastFrame(const Matrix44< T > &, const Vec3< T > &, const Vec3< T > &) IMATH_NOEXCEPT
Definition: ImathFrame.h:209
fpreal64 dot(const CE_VectorT< T > &a, const CE_VectorT< T > &b)
Definition: CE_Vector.h:138
const Vec3 & normalizeExc()
Normalize in place. If length()==0, throw an exception.
Definition: ImathVec.h:2105
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44 & translate(const Vec3< S > &t) IMATH_NOEXCEPT
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLdouble t
Definition: glad.h:2397
Matrix44< T > constexpr firstFrame(const Vec3< T > &, const Vec3< T > &, const Vec3< T > &) IMATH_NOEXCEPT
Definition: ImathFrame.h:80
__hostdev__ constexpr T pi()
Pi constant taken from Boost to match old behaviour.
Definition: NanoVDB.h:976
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 const Matrix44 & setAxisAngle(const Vec3< S > &ax, S ang) IMATH_NOEXCEPT
T y
Definition: ImathVec.h:368
GLboolean r
Definition: glcorearb.h:1222
IMATH_HOSTDEVICE constexpr Vec3 cross(const Vec3 &v) const IMATH_NOEXCEPT
Right-handed cross product.
Definition: ImathVec.h:1889
IMATH_HOSTDEVICE const Vec3 & normalize() IMATH_NOEXCEPT
Normalize in place. If length()==0, return a null vector.
Definition: ImathVec.h:2083