quat.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*************************************************************************/
  2. /* quat.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef QUAT_H
  30. #define QUAT_H
  31. #include "math_defs.h"
  32. #include "math_funcs.h"
  33. #include "ustring.h"
  34. #include "vector3.h"
  35. /**
  36. @author Juan Linietsky <[email protected]>
  37. */
  38. class Quat{
  39. public:
  40. real_t x,y,z,w;
  41. _FORCE_INLINE_ real_t length_squared() const;
  42. real_t length() const;
  43. void normalize();
  44. Quat normalized() const;
  45. Quat inverse() const;
  46. _FORCE_INLINE_ real_t dot(const Quat& q) const;
  47. void set_euler(const Vector3& p_euler);
  48. Quat slerp(const Quat& q, const real_t& t) const;
  49. Quat slerpni(const Quat& q, const real_t& t) const;
  50. Quat cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const;
  51. _FORCE_INLINE_ void get_axis_and_angle(Vector3& r_axis, real_t &r_angle) const {
  52. r_angle = 2 * Math::acos(w);
  53. r_axis.x = -x / Math::sqrt(1-w*w);
  54. r_axis.y = -y / Math::sqrt(1-w*w);
  55. r_axis.z = -z / Math::sqrt(1-w*w);
  56. }
  57. void operator*=(const Quat& q);
  58. Quat operator*(const Quat& q) const;
  59. _FORCE_INLINE_ void operator+=(const Quat& q);
  60. _FORCE_INLINE_ void operator-=(const Quat& q);
  61. _FORCE_INLINE_ void operator*=(const real_t& s);
  62. _FORCE_INLINE_ void operator/=(const real_t& s);
  63. _FORCE_INLINE_ Quat operator+(const Quat& q2) const;
  64. _FORCE_INLINE_ Quat operator-(const Quat& q2) const;
  65. _FORCE_INLINE_ Quat operator-() const;
  66. _FORCE_INLINE_ Quat operator*(const real_t& s) const;
  67. _FORCE_INLINE_ Quat operator/(const real_t& s) const;
  68. _FORCE_INLINE_ bool operator==(const Quat& p_quat) const;
  69. _FORCE_INLINE_ bool operator!=(const Quat& p_quat) const;
  70. operator String() const;
  71. inline void set( real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  72. x=p_x; y=p_y; z=p_z; w=p_w;
  73. }
  74. inline Quat(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
  75. x=p_x; y=p_y; z=p_z; w=p_w;
  76. }
  77. Quat(const Vector3& axis, const real_t& angle);
  78. inline Quat() {x=y=z=0; w=1; }
  79. };
  80. real_t Quat::dot(const Quat& q) const {
  81. return x * q.x+y * q.y+z * q.z+w * q.w;
  82. }
  83. real_t Quat::length_squared() const {
  84. return dot(*this);
  85. }
  86. void Quat::operator+=(const Quat& q) {
  87. x += q.x; y += q.y; z += q.z; w += q.w;
  88. }
  89. void Quat::operator-=(const Quat& q) {
  90. x -= q.x; y -= q.y; z -= q.z; w -= q.w;
  91. }
  92. void Quat::operator*=(const real_t& s) {
  93. x *= s; y *= s; z *= s; w *= s;
  94. }
  95. void Quat::operator/=(const real_t& s) {
  96. *this *= 1.0 / s;
  97. }
  98. Quat Quat::operator+(const Quat& q2) const {
  99. const Quat& q1 = *this;
  100. return Quat( q1.x+q2.x, q1.y+q2.y, q1.z+q2.z, q1.w+q2.w );
  101. }
  102. Quat Quat::operator-(const Quat& q2) const {
  103. const Quat& q1 = *this;
  104. return Quat( q1.x-q2.x, q1.y-q2.y, q1.z-q2.z, q1.w-q2.w);
  105. }
  106. Quat Quat::operator-() const {
  107. const Quat& q2 = *this;
  108. return Quat( -q2.x, -q2.y, -q2.z, -q2.w);
  109. }
  110. Quat Quat::operator*(const real_t& s) const {
  111. return Quat(x * s, y * s, z * s, w * s);
  112. }
  113. Quat Quat::operator/(const real_t& s) const {
  114. return *this * (1.0 / s);
  115. }
  116. bool Quat::operator==(const Quat& p_quat) const {
  117. return x==p_quat.x && y==p_quat.y && z==p_quat.z && w==p_quat.w;
  118. }
  119. bool Quat::operator!=(const Quat& p_quat) const {
  120. return x!=p_quat.x || y!=p_quat.y || z!=p_quat.z || w!=p_quat.w;
  121. }
  122. #endif