Quaternion.inl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. Original file from GamePlay3D: http://gameplay3d.org
  13. This file was modified to fit the cocos2d-x project
  14. */
  15. #include "math/Quaternion.h"
  16. #include <float.h>
  17. NS_CC_MATH_BEGIN
  18. inline Quaternion Quaternion::operator*(const Quaternion& q) const
  19. {
  20. Quaternion result(*this);
  21. result.multiply(q);
  22. return result;
  23. }
  24. inline Quaternion& Quaternion::operator*=(const Quaternion& q)
  25. {
  26. multiply(q);
  27. return *this;
  28. }
  29. inline Vec3 Quaternion::operator*(const Vec3& v) const
  30. {
  31. Vec3 uv, uuv;
  32. Vec3 qvec(x, y, z);
  33. Vec3::cross(qvec, v, &uv);
  34. Vec3::cross(qvec, uv, &uuv);
  35. uv *= (2.0f * w);
  36. uuv *= 2.0f;
  37. return v + uv + uuv;
  38. }
  39. /* --- --- --- --- Add by XuJJ start --- --- --- --- */
  40. float Dot( const Quaternion& q1, const Quaternion& q2 );
  41. inline float Magnitude(const Quaternion& q)
  42. {
  43. // return SqrtImpl (SqrMagnitude (q));
  44. return sqrt (SqrMagnitude (q));
  45. }
  46. inline float SqrMagnitude(const Quaternion& q)
  47. {
  48. return Dot (q, q);
  49. }
  50. inline float Dot( const Quaternion& q1, const Quaternion& q2 )
  51. {
  52. return (q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w);
  53. }
  54. inline Quaternion NormalizeSafe (const Quaternion& q)
  55. {
  56. float mag = Magnitude (q);
  57. if (mag < FLT_EPSILON)
  58. return Quaternion::identity ();
  59. else
  60. return q / mag;
  61. }
  62. inline float& Quaternion::operator[] (int i)
  63. {
  64. // DebugAssertIf (i < 0 || i > 3);
  65. CC_ASSERT(!(i < 0 || i > 3));
  66. return (&x)[i];
  67. }
  68. inline const float& Quaternion::operator[] (int i)const
  69. {
  70. // DebugAssertIf (i < 0 || i > 3);
  71. CC_ASSERT(!(i < 0 || i > 3));
  72. return (&x)[i];
  73. }
  74. inline Quaternion& Quaternion::operator+= (const Quaternion& aQuat)
  75. {
  76. x += aQuat.x;
  77. y += aQuat.y;
  78. z += aQuat.z;
  79. w += aQuat.w;
  80. return *this;
  81. }
  82. inline Quaternion& Quaternion::operator-= (const Quaternion& aQuat)
  83. {
  84. x -= aQuat.x;
  85. y -= aQuat.y;
  86. z -= aQuat.z;
  87. w -= aQuat.w;
  88. return *this;
  89. }
  90. inline Quaternion& Quaternion::operator *= (float aScalar)
  91. {
  92. x *= aScalar;
  93. y *= aScalar;
  94. z *= aScalar;
  95. w *= aScalar;
  96. return *this;
  97. }
  98. inline Quaternion& Quaternion::operator /= (const float aScalar)
  99. {
  100. // AssertIf (CompareApproximately (aScalar, 0.0F));
  101. x /= aScalar;
  102. y /= aScalar;
  103. z /= aScalar;
  104. w /= aScalar;
  105. return *this;
  106. }
  107. //inline Quaternion& Quaternion::operator *= (const Quaternion& rhs)
  108. //{
  109. // float tempx = w*rhs.x + x*rhs.w + y*rhs.z - z*rhs.y;
  110. // float tempy = w*rhs.y + y*rhs.w + z*rhs.x - x*rhs.z;
  111. // float tempz = w*rhs.z + z*rhs.w + x*rhs.y - y*rhs.x;
  112. // float tempw = w*rhs.w - x*rhs.x - y*rhs.y - z*rhs.z;
  113. // x = tempx; y = tempy; z = tempz; w = tempw;
  114. // return *this;
  115. //}
  116. inline bool IsFinite (const Quaternion& f)
  117. {
  118. unsigned int intval = *reinterpret_cast<const unsigned int*>(&(f.x));
  119. bool flag = ((intval & 0x7f800000) != 0x7f800000);
  120. intval = *reinterpret_cast<const unsigned int*>(&(f.y));
  121. flag = flag & ((intval & 0x7f800000) != 0x7f800000);
  122. intval = *reinterpret_cast<const unsigned int*>(&(f.z));
  123. flag = flag & ((intval & 0x7f800000) != 0x7f800000);
  124. intval = *reinterpret_cast<const unsigned int*>(&(f.w));
  125. flag = flag & ((intval & 0x7f800000) != 0x7f800000);
  126. return flag;
  127. // return RRP::IsFinite(f.x) & RRP::IsFinite(f.y) & RRP::IsFinite(f.z) & RRP::IsFinite(f.w);
  128. }
  129. /* --- --- --- --- Add by XuJJ end --- --- --- --- */
  130. NS_CC_MATH_END