JointTransform.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __JOINTTRANSFORM_H__
  21. #define __JOINTTRANSFORM_H__
  22. /*
  23. ===============================================================================
  24. Joint Quaternion
  25. ===============================================================================
  26. */
  27. class idJointQuat {
  28. public:
  29. idQuat q;
  30. idVec3 t;
  31. };
  32. /*
  33. ===============================================================================
  34. Joint Matrix
  35. idMat3 m;
  36. idVec3 t;
  37. m[0][0], m[1][0], m[2][0], t[0]
  38. m[0][1], m[1][1], m[2][1], t[1]
  39. m[0][2], m[1][2], m[2][2], t[2]
  40. ===============================================================================
  41. */
  42. class idJointMat {
  43. public:
  44. void SetRotation( const idMat3 &m );
  45. void SetTranslation( const idVec3 &t );
  46. idVec3 operator*( const idVec3 &v ) const; // only rotate
  47. idVec3 operator*( const idVec4 &v ) const; // rotate and translate
  48. idJointMat & operator*=( const idJointMat &a ); // transform
  49. idJointMat & operator/=( const idJointMat &a ); // untransform
  50. bool Compare( const idJointMat &a ) const; // exact compare, no epsilon
  51. bool Compare( const idJointMat &a, const float epsilon ) const; // compare with epsilon
  52. bool operator==( const idJointMat &a ) const; // exact compare, no epsilon
  53. bool operator!=( const idJointMat &a ) const; // exact compare, no epsilon
  54. idMat3 ToMat3( void ) const;
  55. idVec3 ToVec3( void ) const;
  56. idJointQuat ToJointQuat( void ) const;
  57. const float * ToFloatPtr( void ) const;
  58. float * ToFloatPtr( void );
  59. private:
  60. float mat[3*4];
  61. };
  62. ID_INLINE void idJointMat::SetRotation( const idMat3 &m ) {
  63. // NOTE: idMat3 is transposed because it is column-major
  64. mat[0 * 4 + 0] = m[0][0];
  65. mat[0 * 4 + 1] = m[1][0];
  66. mat[0 * 4 + 2] = m[2][0];
  67. mat[1 * 4 + 0] = m[0][1];
  68. mat[1 * 4 + 1] = m[1][1];
  69. mat[1 * 4 + 2] = m[2][1];
  70. mat[2 * 4 + 0] = m[0][2];
  71. mat[2 * 4 + 1] = m[1][2];
  72. mat[2 * 4 + 2] = m[2][2];
  73. }
  74. ID_INLINE void idJointMat::SetTranslation( const idVec3 &t ) {
  75. mat[0 * 4 + 3] = t[0];
  76. mat[1 * 4 + 3] = t[1];
  77. mat[2 * 4 + 3] = t[2];
  78. }
  79. ID_INLINE idVec3 idJointMat::operator*( const idVec3 &v ) const {
  80. return idVec3( mat[0 * 4 + 0] * v[0] + mat[0 * 4 + 1] * v[1] + mat[0 * 4 + 2] * v[2],
  81. mat[1 * 4 + 0] * v[0] + mat[1 * 4 + 1] * v[1] + mat[1 * 4 + 2] * v[2],
  82. mat[2 * 4 + 0] * v[0] + mat[2 * 4 + 1] * v[1] + mat[2 * 4 + 2] * v[2] );
  83. }
  84. ID_INLINE idVec3 idJointMat::operator*( const idVec4 &v ) const {
  85. return idVec3( mat[0 * 4 + 0] * v[0] + mat[0 * 4 + 1] * v[1] + mat[0 * 4 + 2] * v[2] + mat[0 * 4 + 3] * v[3],
  86. mat[1 * 4 + 0] * v[0] + mat[1 * 4 + 1] * v[1] + mat[1 * 4 + 2] * v[2] + mat[1 * 4 + 3] * v[3],
  87. mat[2 * 4 + 0] * v[0] + mat[2 * 4 + 1] * v[1] + mat[2 * 4 + 2] * v[2] + mat[2 * 4 + 3] * v[3] );
  88. }
  89. ID_INLINE idJointMat &idJointMat::operator*=( const idJointMat &a ) {
  90. float dst[3];
  91. dst[0] = mat[0 * 4 + 0] * a.mat[0 * 4 + 0] + mat[1 * 4 + 0] * a.mat[0 * 4 + 1] + mat[2 * 4 + 0] * a.mat[0 * 4 + 2];
  92. dst[1] = mat[0 * 4 + 0] * a.mat[1 * 4 + 0] + mat[1 * 4 + 0] * a.mat[1 * 4 + 1] + mat[2 * 4 + 0] * a.mat[1 * 4 + 2];
  93. dst[2] = mat[0 * 4 + 0] * a.mat[2 * 4 + 0] + mat[1 * 4 + 0] * a.mat[2 * 4 + 1] + mat[2 * 4 + 0] * a.mat[2 * 4 + 2];
  94. mat[0 * 4 + 0] = dst[0];
  95. mat[1 * 4 + 0] = dst[1];
  96. mat[2 * 4 + 0] = dst[2];
  97. dst[0] = mat[0 * 4 + 1] * a.mat[0 * 4 + 0] + mat[1 * 4 + 1] * a.mat[0 * 4 + 1] + mat[2 * 4 + 1] * a.mat[0 * 4 + 2];
  98. dst[1] = mat[0 * 4 + 1] * a.mat[1 * 4 + 0] + mat[1 * 4 + 1] * a.mat[1 * 4 + 1] + mat[2 * 4 + 1] * a.mat[1 * 4 + 2];
  99. dst[2] = mat[0 * 4 + 1] * a.mat[2 * 4 + 0] + mat[1 * 4 + 1] * a.mat[2 * 4 + 1] + mat[2 * 4 + 1] * a.mat[2 * 4 + 2];
  100. mat[0 * 4 + 1] = dst[0];
  101. mat[1 * 4 + 1] = dst[1];
  102. mat[2 * 4 + 1] = dst[2];
  103. dst[0] = mat[0 * 4 + 2] * a.mat[0 * 4 + 0] + mat[1 * 4 + 2] * a.mat[0 * 4 + 1] + mat[2 * 4 + 2] * a.mat[0 * 4 + 2];
  104. dst[1] = mat[0 * 4 + 2] * a.mat[1 * 4 + 0] + mat[1 * 4 + 2] * a.mat[1 * 4 + 1] + mat[2 * 4 + 2] * a.mat[1 * 4 + 2];
  105. dst[2] = mat[0 * 4 + 2] * a.mat[2 * 4 + 0] + mat[1 * 4 + 2] * a.mat[2 * 4 + 1] + mat[2 * 4 + 2] * a.mat[2 * 4 + 2];
  106. mat[0 * 4 + 2] = dst[0];
  107. mat[1 * 4 + 2] = dst[1];
  108. mat[2 * 4 + 2] = dst[2];
  109. dst[0] = mat[0 * 4 + 3] * a.mat[0 * 4 + 0] + mat[1 * 4 + 3] * a.mat[0 * 4 + 1] + mat[2 * 4 + 3] * a.mat[0 * 4 + 2];
  110. dst[1] = mat[0 * 4 + 3] * a.mat[1 * 4 + 0] + mat[1 * 4 + 3] * a.mat[1 * 4 + 1] + mat[2 * 4 + 3] * a.mat[1 * 4 + 2];
  111. dst[2] = mat[0 * 4 + 3] * a.mat[2 * 4 + 0] + mat[1 * 4 + 3] * a.mat[2 * 4 + 1] + mat[2 * 4 + 3] * a.mat[2 * 4 + 2];
  112. mat[0 * 4 + 3] = dst[0];
  113. mat[1 * 4 + 3] = dst[1];
  114. mat[2 * 4 + 3] = dst[2];
  115. mat[0 * 4 + 3] += a.mat[0 * 4 + 3];
  116. mat[1 * 4 + 3] += a.mat[1 * 4 + 3];
  117. mat[2 * 4 + 3] += a.mat[2 * 4 + 3];
  118. return *this;
  119. }
  120. ID_INLINE idJointMat &idJointMat::operator/=( const idJointMat &a ) {
  121. float dst[3];
  122. mat[0 * 4 + 3] -= a.mat[0 * 4 + 3];
  123. mat[1 * 4 + 3] -= a.mat[1 * 4 + 3];
  124. mat[2 * 4 + 3] -= a.mat[2 * 4 + 3];
  125. dst[0] = mat[0 * 4 + 0] * a.mat[0 * 4 + 0] + mat[1 * 4 + 0] * a.mat[1 * 4 + 0] + mat[2 * 4 + 0] * a.mat[2 * 4 + 0];
  126. dst[1] = mat[0 * 4 + 0] * a.mat[0 * 4 + 1] + mat[1 * 4 + 0] * a.mat[1 * 4 + 1] + mat[2 * 4 + 0] * a.mat[2 * 4 + 1];
  127. dst[2] = mat[0 * 4 + 0] * a.mat[0 * 4 + 2] + mat[1 * 4 + 0] * a.mat[1 * 4 + 2] + mat[2 * 4 + 0] * a.mat[2 * 4 + 2];
  128. mat[0 * 4 + 0] = dst[0];
  129. mat[1 * 4 + 0] = dst[1];
  130. mat[2 * 4 + 0] = dst[2];
  131. dst[0] = mat[0 * 4 + 1] * a.mat[0 * 4 + 0] + mat[1 * 4 + 1] * a.mat[1 * 4 + 0] + mat[2 * 4 + 1] * a.mat[2 * 4 + 0];
  132. dst[1] = mat[0 * 4 + 1] * a.mat[0 * 4 + 1] + mat[1 * 4 + 1] * a.mat[1 * 4 + 1] + mat[2 * 4 + 1] * a.mat[2 * 4 + 1];
  133. dst[2] = mat[0 * 4 + 1] * a.mat[0 * 4 + 2] + mat[1 * 4 + 1] * a.mat[1 * 4 + 2] + mat[2 * 4 + 1] * a.mat[2 * 4 + 2];
  134. mat[0 * 4 + 1] = dst[0];
  135. mat[1 * 4 + 1] = dst[1];
  136. mat[2 * 4 + 1] = dst[2];
  137. dst[0] = mat[0 * 4 + 2] * a.mat[0 * 4 + 0] + mat[1 * 4 + 2] * a.mat[1 * 4 + 0] + mat[2 * 4 + 2] * a.mat[2 * 4 + 0];
  138. dst[1] = mat[0 * 4 + 2] * a.mat[0 * 4 + 1] + mat[1 * 4 + 2] * a.mat[1 * 4 + 1] + mat[2 * 4 + 2] * a.mat[2 * 4 + 1];
  139. dst[2] = mat[0 * 4 + 2] * a.mat[0 * 4 + 2] + mat[1 * 4 + 2] * a.mat[1 * 4 + 2] + mat[2 * 4 + 2] * a.mat[2 * 4 + 2];
  140. mat[0 * 4 + 2] = dst[0];
  141. mat[1 * 4 + 2] = dst[1];
  142. mat[2 * 4 + 2] = dst[2];
  143. dst[0] = mat[0 * 4 + 3] * a.mat[0 * 4 + 0] + mat[1 * 4 + 3] * a.mat[1 * 4 + 0] + mat[2 * 4 + 3] * a.mat[2 * 4 + 0];
  144. dst[1] = mat[0 * 4 + 3] * a.mat[0 * 4 + 1] + mat[1 * 4 + 3] * a.mat[1 * 4 + 1] + mat[2 * 4 + 3] * a.mat[2 * 4 + 1];
  145. dst[2] = mat[0 * 4 + 3] * a.mat[0 * 4 + 2] + mat[1 * 4 + 3] * a.mat[1 * 4 + 2] + mat[2 * 4 + 3] * a.mat[2 * 4 + 2];
  146. mat[0 * 4 + 3] = dst[0];
  147. mat[1 * 4 + 3] = dst[1];
  148. mat[2 * 4 + 3] = dst[2];
  149. return *this;
  150. }
  151. ID_INLINE bool idJointMat::Compare( const idJointMat &a ) const {
  152. int i;
  153. for ( i = 0; i < 12; i++ ) {
  154. if ( mat[i] != a.mat[i] ) {
  155. return false;
  156. }
  157. }
  158. return true;
  159. }
  160. ID_INLINE bool idJointMat::Compare( const idJointMat &a, const float epsilon ) const {
  161. int i;
  162. for ( i = 0; i < 12; i++ ) {
  163. if ( idMath::Fabs( mat[i] - a.mat[i] ) > epsilon ) {
  164. return false;
  165. }
  166. }
  167. return true;
  168. }
  169. ID_INLINE bool idJointMat::operator==( const idJointMat &a ) const {
  170. return Compare( a );
  171. }
  172. ID_INLINE bool idJointMat::operator!=( const idJointMat &a ) const {
  173. return !Compare( a );
  174. }
  175. ID_INLINE idMat3 idJointMat::ToMat3( void ) const {
  176. return idMat3( mat[0 * 4 + 0], mat[1 * 4 + 0], mat[2 * 4 + 0],
  177. mat[0 * 4 + 1], mat[1 * 4 + 1], mat[2 * 4 + 1],
  178. mat[0 * 4 + 2], mat[1 * 4 + 2], mat[2 * 4 + 2] );
  179. }
  180. ID_INLINE idVec3 idJointMat::ToVec3( void ) const {
  181. return idVec3( mat[0 * 4 + 3], mat[1 * 4 + 3], mat[2 * 4 + 3] );
  182. }
  183. ID_INLINE const float *idJointMat::ToFloatPtr( void ) const {
  184. return mat;
  185. }
  186. ID_INLINE float *idJointMat::ToFloatPtr( void ) {
  187. return mat;
  188. }
  189. #endif /* !__JOINTTRANSFORM_H__ */