PVRTQuaternion.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /******************************************************************************
  2. @File PVRTQuaternion.h
  3. @Title PVRTQuaternion
  4. @Version
  5. @Copyright Copyright (C) Imagination Technologies Limited.
  6. @Platform ANSI compatible
  7. @Description Quaternion functions for floating and fixed point math.
  8. ******************************************************************************/
  9. #ifndef _PVRTQUATERNION_H_
  10. #define _PVRTQUATERNION_H_
  11. #include "PVRTGlobal.h"
  12. #include "PVRTMatrix.h"
  13. /****************************************************************************
  14. ** Typedefs
  15. ****************************************************************************/
  16. /*!***************************************************************************
  17. Floating point Quaternion
  18. *****************************************************************************/
  19. typedef struct
  20. {
  21. float x; /*!< x coordinate */
  22. float y; /*!< y coordinate */
  23. float z; /*!< z coordinate */
  24. float w; /*!< w coordinate */
  25. } PVRTQUATERNIONf;
  26. /*!***************************************************************************
  27. Fixed point Quaternion
  28. *****************************************************************************/
  29. typedef struct
  30. {
  31. int x; /*!< x coordinate */
  32. int y; /*!< y coordinate */
  33. int z; /*!< z coordinate */
  34. int w; /*!< w coordinate */
  35. } PVRTQUATERNIONx;
  36. /****************************************************************************
  37. ** Float or fixed
  38. ****************************************************************************/
  39. #ifdef PVRT_FIXED_POINT_ENABLE
  40. typedef PVRTQUATERNIONx PVRTQUATERNION;
  41. #define PVRTMatrixQuaternionIdentity PVRTMatrixQuaternionIdentityX
  42. #define PVRTMatrixQuaternionRotationAxis PVRTMatrixQuaternionRotationAxisX
  43. #define PVRTMatrixQuaternionToAxisAngle PVRTMatrixQuaternionToAxisAngleX
  44. #define PVRTMatrixQuaternionSlerp PVRTMatrixQuaternionSlerpX
  45. #define PVRTMatrixQuaternionNormalize PVRTMatrixQuaternionNormalizeX
  46. #define PVRTMatrixRotationQuaternion PVRTMatrixRotationQuaternionX
  47. #define PVRTMatrixQuaternionMultiply PVRTMatrixQuaternionMultiplyX
  48. #else
  49. typedef PVRTQUATERNIONf PVRTQUATERNION;
  50. #define PVRTMatrixQuaternionIdentity PVRTMatrixQuaternionIdentityF
  51. #define PVRTMatrixQuaternionRotationAxis PVRTMatrixQuaternionRotationAxisF
  52. #define PVRTMatrixQuaternionToAxisAngle PVRTMatrixQuaternionToAxisAngleF
  53. #define PVRTMatrixQuaternionSlerp PVRTMatrixQuaternionSlerpF
  54. #define PVRTMatrixQuaternionNormalize PVRTMatrixQuaternionNormalizeF
  55. #define PVRTMatrixRotationQuaternion PVRTMatrixRotationQuaternionF
  56. #define PVRTMatrixQuaternionMultiply PVRTMatrixQuaternionMultiplyF
  57. #endif
  58. /****************************************************************************
  59. ** Functions
  60. ****************************************************************************/
  61. /*!***************************************************************************
  62. @Function PVRTMatrixQuaternionIdentityF
  63. @Output qOut Identity quaternion
  64. @Description Sets the quaternion to (0, 0, 0, 1), the identity quaternion.
  65. *****************************************************************************/
  66. void PVRTMatrixQuaternionIdentityF(
  67. PVRTQUATERNIONf &qOut);
  68. /*!***************************************************************************
  69. @Function PVRTMatrixQuaternionIdentityX
  70. @Output qOut Identity quaternion
  71. @Description Sets the quaternion to (0, 0, 0, 1), the identity quaternion.
  72. *****************************************************************************/
  73. void PVRTMatrixQuaternionIdentityX(
  74. PVRTQUATERNIONx &qOut);
  75. /*!***************************************************************************
  76. @Function PVRTMatrixQuaternionRotationAxisF
  77. @Output qOut Rotation quaternion
  78. @Input vAxis Axis to rotate around
  79. @Input fAngle Angle to rotate
  80. @Description Create quaternion corresponding to a rotation of fAngle
  81. radians around submitted vector.
  82. *****************************************************************************/
  83. void PVRTMatrixQuaternionRotationAxisF(
  84. PVRTQUATERNIONf &qOut,
  85. const PVRTVECTOR3f &vAxis,
  86. const float fAngle);
  87. /*!***************************************************************************
  88. @Function PVRTMatrixQuaternionRotationAxisX
  89. @Output qOut Rotation quaternion
  90. @Input vAxis Axis to rotate around
  91. @Input fAngle Angle to rotate
  92. @Description Create quaternion corresponding to a rotation of fAngle
  93. radians around submitted vector.
  94. *****************************************************************************/
  95. void PVRTMatrixQuaternionRotationAxisX(
  96. PVRTQUATERNIONx &qOut,
  97. const PVRTVECTOR3x &vAxis,
  98. const int fAngle);
  99. /*!***************************************************************************
  100. @Function PVRTMatrixQuaternionToAxisAngleF
  101. @Input qIn Quaternion to transform
  102. @Output vAxis Axis of rotation
  103. @Output fAngle Angle of rotation
  104. @Description Convert a quaternion to an axis and angle. Expects a unit
  105. quaternion.
  106. *****************************************************************************/
  107. void PVRTMatrixQuaternionToAxisAngleF(
  108. const PVRTQUATERNIONf &qIn,
  109. PVRTVECTOR3f &vAxis,
  110. float &fAngle);
  111. /*!***************************************************************************
  112. @Function PVRTMatrixQuaternionToAxisAngleX
  113. @Input qIn Quaternion to transform
  114. @Output vAxis Axis of rotation
  115. @Output fAngle Angle of rotation
  116. @Description Convert a quaternion to an axis and angle. Expects a unit
  117. quaternion.
  118. *****************************************************************************/
  119. void PVRTMatrixQuaternionToAxisAngleX(
  120. const PVRTQUATERNIONx &qIn,
  121. PVRTVECTOR3x &vAxis,
  122. int &fAngle);
  123. /*!***************************************************************************
  124. @Function PVRTMatrixQuaternionSlerpF
  125. @Output qOut Result of the interpolation
  126. @Input qA First quaternion to interpolate from
  127. @Input qB Second quaternion to interpolate from
  128. @Input t Coefficient of interpolation
  129. @Description Perform a Spherical Linear intERPolation between quaternion A
  130. and quaternion B at time t. t must be between 0.0f and 1.0f
  131. *****************************************************************************/
  132. void PVRTMatrixQuaternionSlerpF(
  133. PVRTQUATERNIONf &qOut,
  134. const PVRTQUATERNIONf &qA,
  135. const PVRTQUATERNIONf &qB,
  136. const float t);
  137. /*!***************************************************************************
  138. @Function PVRTMatrixQuaternionSlerpX
  139. @Output qOut Result of the interpolation
  140. @Input qA First quaternion to interpolate from
  141. @Input qB Second quaternion to interpolate from
  142. @Input t Coefficient of interpolation
  143. @Description Perform a Spherical Linear intERPolation between quaternion A
  144. and quaternion B at time t. t must be between 0.0f and 1.0f
  145. Requires input quaternions to be normalized
  146. *****************************************************************************/
  147. void PVRTMatrixQuaternionSlerpX(
  148. PVRTQUATERNIONx &qOut,
  149. const PVRTQUATERNIONx &qA,
  150. const PVRTQUATERNIONx &qB,
  151. const int t);
  152. /*!***************************************************************************
  153. @Function PVRTMatrixQuaternionNormalizeF
  154. @Modified quat Vector to normalize
  155. @Description Normalize quaternion.
  156. *****************************************************************************/
  157. void PVRTMatrixQuaternionNormalizeF(PVRTQUATERNIONf &quat);
  158. /*!***************************************************************************
  159. @Function PVRTMatrixQuaternionNormalizeX
  160. @Modified quat Vector to normalize
  161. @Description Normalize quaternion.
  162. Original quaternion is scaled down prior to be normalized in
  163. order to avoid overflow issues.
  164. *****************************************************************************/
  165. void PVRTMatrixQuaternionNormalizeX(PVRTQUATERNIONx &quat);
  166. /*!***************************************************************************
  167. @Function PVRTMatrixRotationQuaternionF
  168. @Output mOut Resulting rotation matrix
  169. @Input quat Quaternion to transform
  170. @Description Create rotation matrix from submitted quaternion.
  171. Assuming the quaternion is of the form [X Y Z W]:
  172. | 2 2 |
  173. | 1 - 2Y - 2Z 2XY - 2ZW 2XZ + 2YW 0 |
  174. | |
  175. | 2 2 |
  176. M = | 2XY + 2ZW 1 - 2X - 2Z 2YZ - 2XW 0 |
  177. | |
  178. | 2 2 |
  179. | 2XZ - 2YW 2YZ + 2XW 1 - 2X - 2Y 0 |
  180. | |
  181. | 0 0 0 1 |
  182. *****************************************************************************/
  183. void PVRTMatrixRotationQuaternionF(
  184. PVRTMATRIXf &mOut,
  185. const PVRTQUATERNIONf &quat);
  186. /*!***************************************************************************
  187. @Function PVRTMatrixRotationQuaternionX
  188. @Output mOut Resulting rotation matrix
  189. @Input quat Quaternion to transform
  190. @Description Create rotation matrix from submitted quaternion.
  191. Assuming the quaternion is of the form [X Y Z W]:
  192. | 2 2 |
  193. | 1 - 2Y - 2Z 2XY - 2ZW 2XZ + 2YW 0 |
  194. | |
  195. | 2 2 |
  196. M = | 2XY + 2ZW 1 - 2X - 2Z 2YZ - 2XW 0 |
  197. | |
  198. | 2 2 |
  199. | 2XZ - 2YW 2YZ + 2XW 1 - 2X - 2Y 0 |
  200. | |
  201. | 0 0 0 1 |
  202. *****************************************************************************/
  203. void PVRTMatrixRotationQuaternionX(
  204. PVRTMATRIXx &mOut,
  205. const PVRTQUATERNIONx &quat);
  206. /*!***************************************************************************
  207. @Function PVRTMatrixQuaternionMultiplyF
  208. @Output qOut Resulting quaternion
  209. @Input qA First quaternion to multiply
  210. @Input qB Second quaternion to multiply
  211. @Description Multiply quaternion A with quaternion B and return the
  212. result in qOut.
  213. *****************************************************************************/
  214. void PVRTMatrixQuaternionMultiplyF(
  215. PVRTQUATERNIONf &qOut,
  216. const PVRTQUATERNIONf &qA,
  217. const PVRTQUATERNIONf &qB);
  218. /*!***************************************************************************
  219. @Function PVRTMatrixQuaternionMultiplyX
  220. @Output qOut Resulting quaternion
  221. @Input qA First quaternion to multiply
  222. @Input qB Second quaternion to multiply
  223. @Description Multiply quaternion A with quaternion B and return the
  224. result in qOut.
  225. Input quaternions must be normalized.
  226. *****************************************************************************/
  227. void PVRTMatrixQuaternionMultiplyX(
  228. PVRTQUATERNIONx &qOut,
  229. const PVRTQUATERNIONx &qA,
  230. const PVRTQUATERNIONx &qB);
  231. #endif
  232. /*****************************************************************************
  233. End of file (PVRTQuaternion.h)
  234. *****************************************************************************/