1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #pragma once19 20 #include "../MathBuildConfig.h"21 #include "[SSEMath.h]"22 23 #ifdef MATH_ENABLE_STL_SUPPORT24 #include <string>25 #endif26 #include "../MathGeoLibFwd.h"27 28 #ifdef MATH_QT_INTEROP29 #include <QQuaternion>30 #endif31 #ifdef MATH_OGRE_INTEROP32 #include <OgreQuaternion.h>33 #endif34 #ifdef MATH_BULLET_INTEROP35 #include <LinearMath/btQuaternion.h>36 #endif37 #ifdef MATH_URHO3D_INTEROP38 #include <Urho3D/Math/Quaternion.h>39 #endif40 41 [MATH_BEGIN_NAMESPACE]42 43 44 class [ALIGN16] [Quat]45 {46 public:47 48 #ifdef MATH_SIMD49 [NAMELESS_UNION_BEGIN] 50 51 union52 {53 struct54 {55 #endif56 57 float [x]; 58 float [y]; 59 float [z]; 60 float [w]; 61 #ifdef MATH_SIMD62 };63 simd4f q;64 };65 66 [NAMELESS_UNION_END]67 68 [Quat](simd4f quat):q(quat) {}69 inline [Quat] &operator =(simd4f quat) { q = quat; return *this; }70 inline operator simd4f() const { return q; }71 #endif72 73 74 [Quat]() {}75 76 #ifdef MATH_EXPLICIT_COPYCTORS77 78 79 [Quat](const [Quat] &rhs) { x = rhs.[x]; y = rhs.[y]; z = rhs.[z]; w = rhs.[w]; }80 #endif81 82 83 84 85 explicit [Quat](const float *data);86 87 explicit [Quat](const [float3x3] &rotationMatrix) { Set(rotationMatrix); }88 explicit [Quat](const [float3x4] &rotationMatrix) { Set(rotationMatrix); }89 explicit [Quat](const [float4x4] &rotationMatrix) { Set(rotationMatrix); }90 91 92 93 94 95 96 [Quat](float x, float y, float z, float w);97 98 99 100 101 102 103 [Quat](const [float3] &rotationAxis, float rotationAngleRadians) { SetFromAxisAngle(rotationAxis, rotationAngleRadians); }104 [Quat](const [float4] &rotationAxis, float rotationAngleRadians) { SetFromAxisAngle(rotationAxis, rotationAngleRadians); }105 106 107 vec WorldX() const;108 109 vec WorldY() const;110 111 vec WorldZ() const;112 113 114 vec Axis() const;115 116 117 float Angle() const;118 119 120 121 float [Dot](const [Quat] &rhs) const;122 123 float LengthSq() const;124 125 float [Length]() const;126 127 128 129 float Normalize();130 131 132 [Quat] Normalized() const;133 134 135 bool IsNormalized(float [epsilon] = 1[e]-5f) const;136 137 bool IsInvertible(float [epsilon] = 1[e]-3f) const;138 139 140 bool [IsFinite]() const;141 142 143 bool Equals(const [Quat] &rhs, float [epsilon] = 1[e]-3f) const;144 145 146 147 bool BitEquals(const [Quat] &other) const;148 149 150 151 [FORCE_INLINE] float *[ptr]() { return &x; }152 [FORCE_INLINE] const float *[ptr]() const { return &x; }153 154 155 156 157 158 void Inverse();159 160 161 [MUST_USE_RESULT] [Quat] Inverted() const;162 163 164 165 166 167 168 float InverseAndNormalize();169 170 171 void Conjugate();172 173 174 [MUST_USE_RESULT] [Quat] Conjugated() const;175 176 177 [MUST_USE_RESULT] [float3] Transform(float x, float y, float z) const;178 [MUST_USE_RESULT] [float3] Transform(const [float3] &vec) const;179 180 181 [MUST_USE_RESULT] [float4] Transform(const [float4] &vec) const;182 183 [MUST_USE_RESULT] [Quat] [Lerp](const [Quat] &target, float t) const;184 static [FORCE_INLINE] [MUST_USE_RESULT] [Quat] [Lerp](const [Quat] &source, const [Quat] &target, float t) { return source.[Lerp](target, t); }185 [MUST_USE_RESULT] [Quat] [Slerp](const [Quat] &target, float t) const;186 static [FORCE_INLINE] [MUST_USE_RESULT] [Quat] [Slerp](const [Quat] &source, const [Quat] &target, float t) { return source.[Slerp](target, t); }187 188 189 190 191 192 193 194 195 static [MUST_USE_RESULT] [float3] SlerpVector(const [float3] &from, const [float3] &to, float t);196 197 198 199 200 201 202 203 204 205 static [MUST_USE_RESULT] [float3] SlerpVectorAbs(const [float3] &from, const [float3] &to, float angleRadians);206 207 208 [MUST_USE_RESULT] float AngleBetween(const [Quat] &target) const;209 210 [MUST_USE_RESULT] vec AxisFromTo(const [Quat] &target) const;211 212 213 214 215 void ToAxisAngle([float3] &rotationAxis, float &rotationAngleRadians) const;216 void ToAxisAngle([float4] &rotationAxis, float &rotationAngleRadians) const;217 218 219 220 221 void SetFromAxisAngle(const [float3] &rotationAxis, float rotationAngleRadians);222 void SetFromAxisAngle(const [float4] &rotationAxis, float rotationAngleRadians);223 224 225 void Set(const [float3x3] &matrix);226 void Set(const [float3x4] &matrix);227 void Set(const [float4x4] &matrix);228 229 230 231 void Set(float x, float y, float z, float w);232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 static [MUST_USE_RESULT] [Quat] LookAt(const [float3] &localForward, const [float3] &targetDirection, const [float3] &localUp, const [float3] &worldUp);260 261 262 static [MUST_USE_RESULT] [Quat] RotateX(float angleRadians);263 264 static [MUST_USE_RESULT] [Quat] RotateY(float angleRadians);265 266 static [MUST_USE_RESULT] [Quat] RotateZ(float angleRadians);267 268 269 static [MUST_USE_RESULT] [Quat] RotateAxisAngle(const [float3] &axisDirection, float angleRadians);270 271 272 273 274 275 276 277 static [MUST_USE_RESULT] [Quat] RotateFromTo(const [float3] &sourceDirection, const [float3] &targetDirection);278 static [MUST_USE_RESULT] [Quat] RotateFromTo(const [float4] &sourceDirection, const [float4] &targetDirection);279 280 281 282 283 284 static [MUST_USE_RESULT] [Quat] RotateFromTo(const [float3] &sourceDirection, const [float3] &targetDirection,285 const [float3] &sourceDirection2, const [float3] &targetDirection2);286 287 288 289 290 static [MUST_USE_RESULT] [Quat] FromEulerXYX(float x2, float y, float x);291 static [MUST_USE_RESULT] [Quat] FromEulerXZX(float x2, float z, float x); 292 static [MUST_USE_RESULT] [Quat] FromEulerYXY(float y2, float x, float y); 293 static [MUST_USE_RESULT] [Quat] FromEulerYZY(float y2, float z, float y); 294 static [MUST_USE_RESULT] [Quat] FromEulerZXZ(float z2, float x, float z); 295 static [MUST_USE_RESULT] [Quat] FromEulerZYZ(float z2, float y, float z); 296 static [MUST_USE_RESULT] [Quat] FromEulerXYZ(float x, float y, float z); 297 static [MUST_USE_RESULT] [Quat] FromEulerXZY(float x, float z, float y); 298 static [MUST_USE_RESULT] [Quat] FromEulerYXZ(float y, float x, float z); 299 static [MUST_USE_RESULT] [Quat] FromEulerYZX(float y, float z, float x); 300 static [MUST_USE_RESULT] [Quat] FromEulerZXY(float z, float x, float y); 301 static [MUST_USE_RESULT] [Quat] FromEulerZYX(float z, float y, float x); 302 303 304 static [MUST_USE_RESULT] [Quat] RandomRotation([LCG] &lcg);305 306 307 308 309 310 311 312 313 314 315 316 317 318 [float3] [MUST_USE_RESULT] ToEulerXYX() const;319 [float3] [MUST_USE_RESULT] ToEulerXZX() const; 320 [float3] [MUST_USE_RESULT] ToEulerYXY() const; 321 [float3] [MUST_USE_RESULT] ToEulerYZY() const; 322 [float3] [MUST_USE_RESULT] ToEulerZXZ() const; 323 [float3] [MUST_USE_RESULT] ToEulerZYZ() const; 324 [float3] [MUST_USE_RESULT] ToEulerXYZ() const; 325 [float3] [MUST_USE_RESULT] ToEulerXZY() const; 326 [float3] [MUST_USE_RESULT] ToEulerYXZ() const; 327 [float3] [MUST_USE_RESULT] ToEulerYZX() const; 328 [float3] [MUST_USE_RESULT] ToEulerZXY() const; 329 [float3] [MUST_USE_RESULT] ToEulerZYX() const; 330 331 [float3x3] [MUST_USE_RESULT] ToFloat3x3() const;332 [float3x4] [MUST_USE_RESULT] ToFloat3x4() const;333 [float4x4] [MUST_USE_RESULT] ToFloat4x4() const;334 [float4x4] [MUST_USE_RESULT] ToFloat4x4(const [float3] &translation) const;335 [float4x4] [MUST_USE_RESULT] ToFloat4x4(const [float4] &translation) const;336 337 #ifdef MATH_ENABLE_STL_SUPPORT338 339 std::string [MUST_USE_RESULT] ToString() const;340 341 342 std::string [MUST_USE_RESULT] ToString2() const;343 344 345 std::string [MUST_USE_RESULT] SerializeToString() const;346 347 348 std::string SerializeToCodeString() const;349 #endif350 351 static [MUST_USE_RESULT] [Quat] FromString(const char *str, const char **outEndStr = 0);352 #ifdef MATH_ENABLE_STL_SUPPORT353 static [MUST_USE_RESULT] [Quat] [FromString](const std::string &str) { return [FromString](str.c_str()); }354 #endif355 356 357 358 359 [Quat] [operator *](const [Quat] &rhs) const;360 361 362 363 364 365 [float3] [operator *](const [float3] &rhs) const;366 [float4] [operator *](const [float4] &rhs) const;367 368 369 370 static const [Quat] [identity];371 372 373 374 375 static const [Quat] [nan];376 377 378 [Quat] operator /(const [Quat] &rhs) const;379 380 381 [Quat] [operator +]() const { return *this; }382 383 #ifdef MATH_OGRE_INTEROP384 [Quat](const Ogre::Quaternion &other):x(other.x), y(other.y), z(other.z), w(other.w) {}385 operator Ogre::Quaternion() const { return Ogre::Quaternion(w, x, y, z); }386 #endif387 #ifdef MATH_QT_INTEROP388 [Quat](const QQuaternion &other):x(other.x()), y(other.y()), z(other.z()), w(other.w()) {}389 operator QQuaternion() const { return QQuaternion(w, x, y, z); }390 operator QString() const { return toString(); }391 QString toString() const { return ToString2().c_str(); }392 QQuaternion ToQQuaternion() const { return (QQuaternion)*this; }393 static [MUST_USE_RESULT] [Quat] FromQQuaternion(const QQuaternion &q) { return ([Quat])q; }394 static [MUST_USE_RESULT] [Quat] FromString(const QString &str) { return FromString(str.toStdString()); }395 #endif396 #ifdef MATH_BULLET_INTEROP397 [Quat](const btQuaternion &other):x(other.x()), y(other.y()), z(other.z()), w(other.w()) {}398 operator btQuaternion() const { return btQuaternion(x, y, z, w); }399 #endif400 #ifdef MATH_URHO3D_INTEROP401 [Quat](const Urho3D::Quaternion &other) : x(other.x_), y(other.y_), z(other.z_), w(other.w_) {}402 operator Urho3D::Quaternion() const { return Urho3D::Quaternion(w, x, y, z); }403 #endif404 405 406 407 [Quat] [MUST_USE_RESULT] [Mul](const [Quat] &rhs) const;408 409 [Quat] [MUST_USE_RESULT] [Mul](const [float3x3] &rhs) const;410 411 412 413 414 [float3] [MUST_USE_RESULT] [Mul](const [float3] &vector) const;415 [float4] [MUST_USE_RESULT] [Mul](const [float4] &vector) const;416 417 418 419 [Quat] [Neg]() const { return -*this; }420 421 private: 422 423 424 425 426 427 428 [Quat] [operator *](float scalar) const;429 430 [Quat] operator /(float scalar) const;431 432 433 434 [Quat] [operator +](const [Quat] &rhs) const;435 436 [Quat] [operator -](const [Quat] &rhs) const;437 438 439 440 [Quat] [operator -]() const;441 };442 443 #ifdef MATH_ENABLE_STL_SUPPORT444 445 std::ostream &[operator <<](std::ostream &out, const [Quat] &rhs);446 #endif447 448 [FORCE_INLINE] [Quat] [Lerp](const [Quat] &a, const [Quat] &b, float t) { return a.[Lerp](b, t); }449 [FORCE_INLINE] [Quat] [Slerp](const [Quat] &a, const [Quat] &b, float t) { return a.[Slerp](b, t); }450 451 #ifdef MATH_QT_INTEROP452 Q_DECLARE_METATYPE([Quat])453 Q_DECLARE_METATYPE([Quat]*)454 #endif455 456 [MATH_END_NAMESPACE] Go back to previous page