quat.h
1 /*************************************************************************/
2 /* quat.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 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 
32 #include "math_defs.h"
33 #include "math_funcs.h"
34 #include "ustring.h"
35 #include "vector3.h"
36 
40 class Quat{
41 public:
42 
43  real_t x,y,z,w;
44 
45  _FORCE_INLINE_ real_t length_squared() const;
46  real_t length() const;
47  void normalize();
48  Quat normalized() const;
49  Quat inverse() const;
50  _FORCE_INLINE_ real_t dot(const Quat& q) const;
51  void set_euler(const Vector3& p_euler);
52  Quat slerp(const Quat& q, const real_t& t) const;
53  Quat slerpni(const Quat& q, const real_t& t) const;
54  Quat cubic_slerp(const Quat& q, const Quat& prep, const Quat& postq,const real_t& t) const;
55 
56  _FORCE_INLINE_ void get_axis_and_angle(Vector3& r_axis, real_t &r_angle) const {
57  r_angle = 2 * Math::acos(w);
58  r_axis.x = -x / Math::sqrt(1-w*w);
59  r_axis.y = -y / Math::sqrt(1-w*w);
60  r_axis.z = -z / Math::sqrt(1-w*w);
61  }
62 
63  void operator*=(const Quat& q);
64  Quat operator*(const Quat& q) const;
65 
66 
67 
68  Quat operator*(const Vector3& v) const
69  {
70  return Quat( w * v.x + y * v.z - z * v.y,
71  w * v.y + z * v.x - x * v.z,
72  w * v.z + x * v.y - y * v.x,
73  -x * v.x - y * v.y - z * v.z);
74  }
75 
76  _FORCE_INLINE_ Vector3 xform(const Vector3& v) const {
77 
78  Quat q = *this * v;
79  q *= this->inverse();
80  return Vector3(q.x,q.y,q.z);
81  }
82 
83  _FORCE_INLINE_ void operator+=(const Quat& q);
84  _FORCE_INLINE_ void operator-=(const Quat& q);
85  _FORCE_INLINE_ void operator*=(const real_t& s);
86  _FORCE_INLINE_ void operator/=(const real_t& s);
87  _FORCE_INLINE_ Quat operator+(const Quat& q2) const;
88  _FORCE_INLINE_ Quat operator-(const Quat& q2) const;
89  _FORCE_INLINE_ Quat operator-() const;
90  _FORCE_INLINE_ Quat operator*(const real_t& s) const;
91  _FORCE_INLINE_ Quat operator/(const real_t& s) const;
92 
93 
94  _FORCE_INLINE_ bool operator==(const Quat& p_quat) const;
95  _FORCE_INLINE_ bool operator!=(const Quat& p_quat) const;
96 
97  operator String() const;
98 
99  inline void set( real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
100  x=p_x; y=p_y; z=p_z; w=p_w;
101  }
102  inline Quat(real_t p_x, real_t p_y, real_t p_z, real_t p_w) {
103  x=p_x; y=p_y; z=p_z; w=p_w;
104  }
105  Quat(const Vector3& axis, const real_t& angle);
106 
107  Quat(const Vector3& v0, const Vector3& v1) // shortest arc
108  {
109  Vector3 c = v0.cross(v1);
110  real_t d = v0.dot(v1);
111 
112  if (d < -1.0 + CMP_EPSILON) {
113  x=0;
114  y=1;
115  z=0;
116  w=0;
117  } else {
118 
119  real_t s = Math::sqrt((1.0f + d) * 2.0f);
120  real_t rs = 1.0f / s;
121 
122  x=c.x*rs;
123  y=c.y*rs;
124  z=c.z*rs;
125  w=s * 0.5;
126  }
127  }
128 
129  inline Quat() {x=y=z=0; w=1; }
130 
131 
132 };
133 
134 
135 real_t Quat::dot(const Quat& q) const {
136  return x * q.x+y * q.y+z * q.z+w * q.w;
137 }
138 
139 real_t Quat::length_squared() const {
140  return dot(*this);
141 }
142 
143 void Quat::operator+=(const Quat& q) {
144  x += q.x; y += q.y; z += q.z; w += q.w;
145 }
146 
147 void Quat::operator-=(const Quat& q) {
148  x -= q.x; y -= q.y; z -= q.z; w -= q.w;
149 }
150 
151 void Quat::operator*=(const real_t& s) {
152  x *= s; y *= s; z *= s; w *= s;
153 }
154 
155 
156 void Quat::operator/=(const real_t& s) {
157 
158  *this *= 1.0 / s;
159 }
160 
161 Quat Quat::operator+(const Quat& q2) const {
162  const Quat& q1 = *this;
163  return Quat( q1.x+q2.x, q1.y+q2.y, q1.z+q2.z, q1.w+q2.w );
164 }
165 
166 Quat Quat::operator-(const Quat& q2) const {
167  const Quat& q1 = *this;
168  return Quat( q1.x-q2.x, q1.y-q2.y, q1.z-q2.z, q1.w-q2.w);
169 }
170 
171 Quat Quat::operator-() const {
172  const Quat& q2 = *this;
173  return Quat( -q2.x, -q2.y, -q2.z, -q2.w);
174 }
175 
176 Quat Quat::operator*(const real_t& s) const {
177  return Quat(x * s, y * s, z * s, w * s);
178 }
179 
180 Quat Quat::operator/(const real_t& s) const {
181  return *this * (1.0 / s);
182 }
183 
184 
185 bool Quat::operator==(const Quat& p_quat) const {
186 
187  return x==p_quat.x && y==p_quat.y && z==p_quat.z && w==p_quat.w;
188 }
189 
190 bool Quat::operator!=(const Quat& p_quat) const {
191 
192  return x!=p_quat.x || y!=p_quat.y || z!=p_quat.z || w!=p_quat.w;
193 }
194 
195 
196 #endif
Definition: quat.h:40
Definition: vector3.h:38
Definition: ustring.h:64