ImathFrame.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #ifndef INCLUDED_IMATHFRAME_H
  35. #define INCLUDED_IMATHFRAME_H
  36. #include "ImathNamespace.h"
  37. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  38. template<class T> class Vec3;
  39. template<class T> class Matrix44;
  40. //
  41. // These methods compute a set of reference frames, defined by their
  42. // transformation matrix, along a curve. It is designed so that the
  43. // array of points and the array of matrices used to fetch these routines
  44. // don't need to be ordered as the curve.
  45. //
  46. // A typical usage would be :
  47. //
  48. // m[0] = IMATH_INTERNAL_NAMESPACE::firstFrame( p[0], p[1], p[2] );
  49. // for( int i = 1; i < n - 1; i++ )
  50. // {
  51. // m[i] = IMATH_INTERNAL_NAMESPACE::nextFrame( m[i-1], p[i-1], p[i], t[i-1], t[i] );
  52. // }
  53. // m[n-1] = IMATH_INTERNAL_NAMESPACE::lastFrame( m[n-2], p[n-2], p[n-1] );
  54. //
  55. // See Graphics Gems I for the underlying algorithm.
  56. //
  57. template<class T> Matrix44<T> firstFrame( const Vec3<T>&, // First point
  58. const Vec3<T>&, // Second point
  59. const Vec3<T>& ); // Third point
  60. template<class T> Matrix44<T> nextFrame( const Matrix44<T>&, // Previous matrix
  61. const Vec3<T>&, // Previous point
  62. const Vec3<T>&, // Current point
  63. Vec3<T>&, // Previous tangent
  64. Vec3<T>& ); // Current tangent
  65. template<class T> Matrix44<T> lastFrame( const Matrix44<T>&, // Previous matrix
  66. const Vec3<T>&, // Previous point
  67. const Vec3<T>& ); // Last point
  68. //
  69. // firstFrame - Compute the first reference frame along a curve.
  70. //
  71. // This function returns the transformation matrix to the reference frame
  72. // defined by the three points 'pi', 'pj' and 'pk'. Note that if the two
  73. // vectors <pi,pj> and <pi,pk> are colinears, an arbitrary twist value will
  74. // be choosen.
  75. //
  76. // Throw 'NullVecExc' if 'pi' and 'pj' are equals.
  77. //
  78. template<class T> Matrix44<T> firstFrame
  79. (
  80. const Vec3<T>& pi, // First point
  81. const Vec3<T>& pj, // Second point
  82. const Vec3<T>& pk ) // Third point
  83. {
  84. Vec3<T> t = pj - pi; t.normalizeExc();
  85. Vec3<T> n = t.cross( pk - pi ); n.normalize();
  86. if( n.length() == 0.0f )
  87. {
  88. int i = fabs( t[0] ) < fabs( t[1] ) ? 0 : 1;
  89. if( fabs( t[2] ) < fabs( t[i] )) i = 2;
  90. Vec3<T> v( 0.0, 0.0, 0.0 ); v[i] = 1.0;
  91. n = t.cross( v ); n.normalize();
  92. }
  93. Vec3<T> b = t.cross( n );
  94. Matrix44<T> M;
  95. M[0][0] = t[0]; M[0][1] = t[1]; M[0][2] = t[2]; M[0][3] = 0.0,
  96. M[1][0] = n[0]; M[1][1] = n[1]; M[1][2] = n[2]; M[1][3] = 0.0,
  97. M[2][0] = b[0]; M[2][1] = b[1]; M[2][2] = b[2]; M[2][3] = 0.0,
  98. M[3][0] = pi[0]; M[3][1] = pi[1]; M[3][2] = pi[2]; M[3][3] = 1.0;
  99. return M;
  100. }
  101. //
  102. // nextFrame - Compute the next reference frame along a curve.
  103. //
  104. // This function returns the transformation matrix to the next reference
  105. // frame defined by the previously computed transformation matrix and the
  106. // new point and tangent vector along the curve.
  107. //
  108. template<class T> Matrix44<T> nextFrame
  109. (
  110. const Matrix44<T>& Mi, // Previous matrix
  111. const Vec3<T>& pi, // Previous point
  112. const Vec3<T>& pj, // Current point
  113. Vec3<T>& ti, // Previous tangent vector
  114. Vec3<T>& tj ) // Current tangent vector
  115. {
  116. Vec3<T> a(0.0, 0.0, 0.0); // Rotation axis.
  117. T r = 0.0; // Rotation angle.
  118. if( ti.length() != 0.0 && tj.length() != 0.0 )
  119. {
  120. ti.normalize(); tj.normalize();
  121. T dot = ti.dot( tj );
  122. //
  123. // This is *really* necessary :
  124. //
  125. if( dot > 1.0 ) dot = 1.0;
  126. else if( dot < -1.0 ) dot = -1.0;
  127. r = acosf( dot );
  128. a = ti.cross( tj );
  129. }
  130. if( a.length() != 0.0 && r != 0.0 )
  131. {
  132. Matrix44<T> R; R.setAxisAngle( a, r );
  133. Matrix44<T> Tj; Tj.translate( pj );
  134. Matrix44<T> Ti; Ti.translate( -pi );
  135. return Mi * Ti * R * Tj;
  136. }
  137. else
  138. {
  139. Matrix44<T> Tr; Tr.translate( pj - pi );
  140. return Mi * Tr;
  141. }
  142. }
  143. //
  144. // lastFrame - Compute the last reference frame along a curve.
  145. //
  146. // This function returns the transformation matrix to the last reference
  147. // frame defined by the previously computed transformation matrix and the
  148. // last point along the curve.
  149. //
  150. template<class T> Matrix44<T> lastFrame
  151. (
  152. const Matrix44<T>& Mi, // Previous matrix
  153. const Vec3<T>& pi, // Previous point
  154. const Vec3<T>& pj ) // Last point
  155. {
  156. Matrix44<T> Tr; Tr.translate( pj - pi );
  157. return Mi * Tr;
  158. }
  159. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  160. #endif // INCLUDED_IMATHFRAME_H