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 //
3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 
37 #ifndef INCLUDED_IMATHFRAME_H
38 #define INCLUDED_IMATHFRAME_H
39 
40 #include "ImathNamespace.h"
41 
43 
44 template<class T> class Vec3;
45 template<class T> class Matrix44;
46 
47 //
48 // These methods compute a set of reference frames, defined by their
49 // transformation matrix, along a curve. It is designed so that the
50 // array of points and the array of matrices used to fetch these routines
51 // don't need to be ordered as the curve.
52 //
53 // A typical usage would be :
54 //
55 // m[0] = IMATH_INTERNAL_NAMESPACE::firstFrame( p[0], p[1], p[2] );
56 // for( int i = 1; i < n - 1; i++ )
57 // {
58 // m[i] = IMATH_INTERNAL_NAMESPACE::nextFrame( m[i-1], p[i-1], p[i], t[i-1], t[i] );
59 // }
60 // m[n-1] = IMATH_INTERNAL_NAMESPACE::lastFrame( m[n-2], p[n-2], p[n-1] );
61 //
62 // See Graphics Gems I for the underlying algorithm.
63 //
64 
65 template<class T> Matrix44<T> firstFrame( const Vec3<T>&, // First point
66  const Vec3<T>&, // Second point
67  const Vec3<T>& ); // Third point
68 
69 template<class T> Matrix44<T> nextFrame( const Matrix44<T>&, // Previous matrix
70  const Vec3<T>&, // Previous point
71  const Vec3<T>&, // Current point
72  Vec3<T>&, // Previous tangent
73  Vec3<T>& ); // Current tangent
74 
75 template<class T> Matrix44<T> lastFrame( const Matrix44<T>&, // Previous matrix
76  const Vec3<T>&, // Previous point
77  const Vec3<T>& ); // Last point
78 
79 //
80 // firstFrame - Compute the first reference frame along a curve.
81 //
82 // This function returns the transformation matrix to the reference frame
83 // defined by the three points 'pi', 'pj' and 'pk'. Note that if the two
84 // vectors <pi,pj> and <pi,pk> are colinears, an arbitrary twist value will
85 // be choosen.
86 //
87 // Throw 'NullVecExc' if 'pi' and 'pj' are equals.
88 //
89 
90 template<class T> Matrix44<T> firstFrame
91 (
92  const Vec3<T>& pi, // First point
93  const Vec3<T>& pj, // Second point
94  const Vec3<T>& pk ) // Third point
95 {
96  Vec3<T> t = pj - pi; t.normalizeExc();
97 
98  Vec3<T> n = t.cross( pk - pi ); n.normalize();
99  if( n.length() == 0.0f )
100  {
101  int i = fabs( t[0] ) < fabs( t[1] ) ? 0 : 1;
102  if( fabs( t[2] ) < fabs( t[i] )) i = 2;
103 
104  Vec3<T> v( 0.0, 0.0, 0.0 ); v[i] = 1.0;
105  n = t.cross( v ); n.normalize();
106  }
107 
108  Vec3<T> b = t.cross( n );
109 
110  Matrix44<T> M;
111 
112  M[0][0] = t[0]; M[0][1] = t[1]; M[0][2] = t[2]; M[0][3] = 0.0,
113  M[1][0] = n[0]; M[1][1] = n[1]; M[1][2] = n[2]; M[1][3] = 0.0,
114  M[2][0] = b[0]; M[2][1] = b[1]; M[2][2] = b[2]; M[2][3] = 0.0,
115  M[3][0] = pi[0]; M[3][1] = pi[1]; M[3][2] = pi[2]; M[3][3] = 1.0;
116 
117  return M;
118 }
119 
120 //
121 // nextFrame - Compute the next reference frame along a curve.
122 //
123 // This function returns the transformation matrix to the next reference
124 // frame defined by the previously computed transformation matrix and the
125 // new point and tangent vector along the curve.
126 //
127 
128 template<class T> Matrix44<T> nextFrame
129 (
130  const Matrix44<T>& Mi, // Previous matrix
131  const Vec3<T>& pi, // Previous point
132  const Vec3<T>& pj, // Current point
133  Vec3<T>& ti, // Previous tangent vector
134  Vec3<T>& tj ) // Current tangent vector
135 {
136  Vec3<T> a(0.0, 0.0, 0.0); // Rotation axis.
137  T r = 0.0; // Rotation angle.
138 
139  if( ti.length() != 0.0 && tj.length() != 0.0 )
140  {
141  ti.normalize(); tj.normalize();
142  T dot = ti.dot( tj );
143 
144  //
145  // This is *really* necessary :
146  //
147 
148  if( dot > 1.0 ) dot = 1.0;
149  else if( dot < -1.0 ) dot = -1.0;
150 
151  r = acosf( dot );
152  a = ti.cross( tj );
153  }
154 
155  if( a.length() != 0.0 && r != 0.0 )
156  {
157  Matrix44<T> R; R.setAxisAngle( a, r );
158  Matrix44<T> Tj; Tj.translate( pj );
159  Matrix44<T> Ti; Ti.translate( -pi );
160 
161  return Mi * Ti * R * Tj;
162  }
163  else
164  {
165  Matrix44<T> Tr; Tr.translate( pj - pi );
166 
167  return Mi * Tr;
168  }
169 }
170 
171 //
172 // lastFrame - Compute the last reference frame along a curve.
173 //
174 // This function returns the transformation matrix to the last reference
175 // frame defined by the previously computed transformation matrix and the
176 // last point along the curve.
177 //
178 
179 template<class T> Matrix44<T> lastFrame
180 (
181  const Matrix44<T>& Mi, // Previous matrix
182  const Vec3<T>& pi, // Previous point
183  const Vec3<T>& pj ) // Last point
184 {
185  Matrix44<T> Tr; Tr.translate( pj - pi );
186 
187  return Mi * Tr;
188 }
189 
191 
192 #endif // INCLUDED_IMATHFRAME_H
#define IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
SYS_API float acosf(float x)
const Matrix44 & setAxisAngle(const Vec3< S > &ax, S ang)
Matrix44< T > nextFrame(const Matrix44< T > &, const Vec3< T > &, const Vec3< T > &, Vec3< T > &, Vec3< T > &)
Definition: ImathFrame.h:129
#define IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
Vec3 cross(const Vec3 &v) const
Definition: ImathVec.h:1481
const GLdouble * v
Definition: glcorearb.h:836
Matrix44< T > lastFrame(const Matrix44< T > &, const Vec3< T > &, const Vec3< T > &)
Definition: ImathFrame.h:180
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
png_uint_32 i
Definition: png.h:2877
GLdouble n
Definition: glcorearb.h:2007
const Matrix44 & translate(const Vec3< S > &t)
T dot(const Vec3 &v) const
Definition: ImathVec.h:1467
fpreal64 dot(const CE_VectorT< T > &a, const CE_VectorT< T > &b)
Definition: CE_Vector.h:218
const Vec3 & normalizeExc()
Definition: ImathVec.h:1704
T length() const
Definition: ImathVec.h:1663
const Vec3 & normalize()
Definition: ImathVec.h:1682
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
Matrix44< T > firstFrame(const Vec3< T > &, const Vec3< T > &, const Vec3< T > &)
Definition: ImathFrame.h:91
GLboolean r
Definition: glcorearb.h:1221