2
0

mAngAxis.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "math/mAngAxis.h"
  23. #include "math/mQuat.h"
  24. #include "math/mMatrix.h"
  25. AngAxisF & AngAxisF::set( const QuatF & q )
  26. {
  27. angle = 2.0f * mAcos( q.w );
  28. F32 sinHalfAngle = mSqrt(q.x * q.x + q.y * q.y + q.z * q.z);
  29. if (sinHalfAngle != 0.0f)
  30. axis.set( q.x / sinHalfAngle, q.y / sinHalfAngle, q.z / sinHalfAngle );
  31. else
  32. axis.set(1.0f,0.0f,0.0f);
  33. return *this;
  34. }
  35. AngAxisF& AngAxisF::set(const EulerF& eul)
  36. {
  37. F32 c1 = mCos(eul.y / 2);
  38. F32 s1 = mSin(eul.y / 2);
  39. F32 c2 = mCos(eul.z / 2);
  40. F32 s2 = mSin(eul.z / 2);
  41. F32 c3 = mCos(eul.x / 2);
  42. F32 s3 = mSin(eul.x / 2);
  43. F32 c1c2 = c1 * c2;
  44. F32 s1s2 = s1 * s2;
  45. F32 w = c1c2 * c3 - s1s2 * s3;
  46. F32 x = c1c2 * s3 + s1s2 * c3;
  47. F32 y = s1 * c2 * c3 + c1 * s2 * s3;
  48. F32 z = c1 * s2 * c3 - s1 * c2 * s3;
  49. angle = 2.0f * mAcos(w);
  50. F32 norm = x * x + y * y + z * z;
  51. if (norm < POINT_EPSILON)
  52. {
  53. axis.set(1.0f, 0.0f, 0.0f);
  54. }
  55. else
  56. {
  57. norm = mSqrt(norm);
  58. x /= norm;
  59. y /= norm;
  60. z /= norm;
  61. }
  62. axis.set(x, y, z);
  63. return *this;
  64. }
  65. AngAxisF & AngAxisF::set( const MatrixF & mat )
  66. {
  67. QuatF q( mat );
  68. set( q );
  69. return *this;
  70. }
  71. MatrixF * AngAxisF::setMatrix( MatrixF * mat ) const
  72. {
  73. QuatF q( *this );
  74. return q.setMatrix( mat );
  75. }
  76. void AngAxisF::RotateX(F32 angle, MatrixF * mat)
  77. {
  78. // for now...do it the easy way
  79. AngAxisF rotX(Point3F(1.0f,0.0f,0.0f),angle);
  80. rotX.setMatrix(mat);
  81. }
  82. void AngAxisF::RotateY(F32 angle, MatrixF * mat)
  83. {
  84. // for now...do it the easy way
  85. AngAxisF rotY(Point3F(0.0f,1.0f,0.0f),angle);
  86. rotY.setMatrix(mat);
  87. }
  88. void AngAxisF::RotateZ(F32 angle, MatrixF * mat)
  89. {
  90. // for now...do it the easy way
  91. AngAxisF rotZ(Point3F(0.0f,0.0f,1.0f),angle);
  92. rotZ.setMatrix(mat);
  93. }
  94. void AngAxisF::RotateX(F32 angle, const Point3F & from, Point3F * to)
  95. {
  96. // for now...do it the easy way
  97. MatrixF mat;
  98. AngAxisF::RotateX(angle,&mat);
  99. mat.mulV(from,to);
  100. }
  101. void AngAxisF::RotateY(F32 angle, const Point3F & from, Point3F * to)
  102. {
  103. // for now...do it the easy way
  104. MatrixF mat;
  105. AngAxisF::RotateY(angle,&mat);
  106. mat.mulV(from,to);
  107. }
  108. void AngAxisF::RotateZ(F32 angle, const Point3F & from, Point3F * to)
  109. {
  110. // for now...do it the easy way
  111. MatrixF mat;
  112. AngAxisF::RotateZ(angle,&mat);
  113. mat.mulV(from,to);
  114. }
  115. EulerF AngAxisF::toEuler() const
  116. {
  117. EulerF r;
  118. F32 s = mSin(angle);
  119. F32 c = mCos(angle);
  120. F32 invc = 1 - c;
  121. if ((axis.x * axis.y * invc + axis.z * s) > (1 - POINT_EPSILON))
  122. {
  123. r.y = 2.0f * mAtan2(axis.x * mSin(angle / 2), mCos(angle / 2));
  124. r.z = -M_HALFPI_F;
  125. r.x = 0.f;
  126. return r;
  127. }
  128. if ((axis.x * axis.y * invc + axis.z * s) < -(1 - POINT_EPSILON))
  129. {
  130. r.y = -2.0f * mAtan2(axis.x * mSin(angle / 2), mCos(angle / 2));
  131. r.z = -M_HALFPI_F;
  132. r.x = 0.f;
  133. return r;
  134. }
  135. r.x = mAtan2(axis.x * s - axis.y * axis.z * invc, 1.0f - (axis.x * axis.x + axis.z * axis.z) * invc);
  136. r.y = mAtan2(axis.y * s - axis.x * axis.z * invc, 1.0f - (axis.y * axis.y + axis.z * axis.z) * invc);
  137. r.z = mAsin(axis.x * axis.y * invc + axis.z * s);
  138. return r;
  139. }