1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 16:57:16 +01:00
SqMod/source/Base/Quaternion.cpp

590 lines
18 KiB
C++
Raw Normal View History

// ------------------------------------------------------------------------------------------------
2015-09-30 02:56:11 +02:00
#include "Base/Quaternion.hpp"
#include "Base/Vector3.hpp"
#include "Base/Vector4.hpp"
#include "Base/Shared.hpp"
#include "Library/Numeric/Random.hpp"
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
#include <limits>
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
const Quaternion Quaternion::NIL = Quaternion(0);
const Quaternion Quaternion::MIN = Quaternion(std::numeric_limits< Quaternion::Value >::min());
const Quaternion Quaternion::MAX = Quaternion(std::numeric_limits< Quaternion::Value >::max());
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
SQChar Quaternion::Delim = ',';
// ------------------------------------------------------------------------------------------------
SQInteger Quaternion::Typename(HSQUIRRELVM vm)
{
static const SQChar name[] = _SC("Quaternion");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion()
2015-09-30 02:56:11 +02:00
: x(0.0), y(0.0), z(0.0), w(0.0)
{
/* ... */
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion(Value sv)
: x(sv), y(sv), z(sv), w(sv)
2015-09-30 02:56:11 +02:00
{
/* ... */
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion(Value xv, Value yv, Value zv)
2015-09-30 02:56:11 +02:00
: x(xv), y(yv), z(zv), w(0.0)
{
/* ... */
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion(Value xv, Value yv, Value zv, Value wv)
2015-09-30 02:56:11 +02:00
: x(xv), y(yv), z(zv), w(wv)
{
/* ... */
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator = (Value s)
2015-09-30 02:56:11 +02:00
{
x = s;
y = s;
z = s;
w = s;
return *this;
}
Quaternion & Quaternion::operator = (const Vector3 & q)
2015-09-30 02:56:11 +02:00
{
x = q.x;
y = q.y;
z = q.z;
w = 0.0;
return *this;
}
Quaternion & Quaternion::operator = (const Vector4 & q)
2015-09-30 02:56:11 +02:00
{
x = q.x;
y = q.y;
z = q.z;
w = q.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator += (const Quaternion & q)
2015-09-30 02:56:11 +02:00
{
x += q.x;
y += q.y;
z += q.z;
w += q.w;
return *this;
}
Quaternion & Quaternion::operator -= (const Quaternion & q)
2015-09-30 02:56:11 +02:00
{
x -= q.x;
y -= q.y;
z -= q.z;
w -= q.w;
return *this;
}
Quaternion & Quaternion::operator *= (const Quaternion & q)
2015-09-30 02:56:11 +02:00
{
x *= q.x;
y *= q.y;
z *= q.z;
w *= q.w;
return *this;
}
Quaternion & Quaternion::operator /= (const Quaternion & q)
2015-09-30 02:56:11 +02:00
{
x /= q.x;
y /= q.y;
z /= q.z;
w /= q.w;
return *this;
}
Quaternion & Quaternion::operator %= (const Quaternion & q)
2015-09-30 02:56:11 +02:00
{
x = std::fmod(x, q.x);
y = std::fmod(y, q.y);
z = std::fmod(z, q.z);
w = std::fmod(w, q.w);
2015-09-30 02:56:11 +02:00
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator += (Value s)
2015-09-30 02:56:11 +02:00
{
x += s;
y += s;
z += s;
w += s;
return *this;
}
Quaternion & Quaternion::operator -= (Value s)
2015-09-30 02:56:11 +02:00
{
x -= s;
y -= s;
z -= s;
w -= s;
return *this;
}
Quaternion & Quaternion::operator *= (Value s)
2015-09-30 02:56:11 +02:00
{
x *= s;
y *= s;
z *= s;
w *= s;
return *this;
}
Quaternion & Quaternion::operator /= (Value s)
2015-09-30 02:56:11 +02:00
{
x /= s;
y /= s;
z /= s;
w /= s;
return *this;
}
Quaternion & Quaternion::operator %= (Value s)
2015-09-30 02:56:11 +02:00
{
x = std::fmod(x, s);
y = std::fmod(y, s);
z = std::fmod(z, s);
w = std::fmod(w, s);
2015-09-30 02:56:11 +02:00
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator ++ ()
2015-09-30 02:56:11 +02:00
{
++x;
++y;
++z;
++w;
return *this;
}
Quaternion & Quaternion::operator -- ()
2015-09-30 02:56:11 +02:00
{
--x;
--y;
--z;
--w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator ++ (int)
2015-09-30 02:56:11 +02:00
{
Quaternion state(*this);
++x;
++y;
++z;
++w;
return state;
}
Quaternion Quaternion::operator -- (int)
2015-09-30 02:56:11 +02:00
{
Quaternion state(*this);
--x;
--y;
--z;
--w;
return state;
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator + (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return Quaternion(x + q.x, y + q.y, z + q.z, w + q.w);
}
Quaternion Quaternion::operator + (Value s) const
2015-09-30 02:56:11 +02:00
{
return Quaternion(x + s, y + s, z + s, w + s);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator - (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return Quaternion(x - q.x, y - q.y, z - q.z, w - q.w);
}
Quaternion Quaternion::operator - (Value s) const
2015-09-30 02:56:11 +02:00
{
return Quaternion(x - s, y - s, z - s, w - s);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator * (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return Quaternion(x * q.x, y * q.y, z * q.z, w * q.w);
}
Quaternion Quaternion::operator * (Value s) const
2015-09-30 02:56:11 +02:00
{
return Quaternion(x * s, y * s, z * s, w * s);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator / (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return Quaternion(x / q.x, y / q.y, z / q.z, w / q.w);
}
Quaternion Quaternion::operator / (Value s) const
2015-09-30 02:56:11 +02:00
{
return Quaternion(x / s, y / s, z / s, w / s);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator % (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return Quaternion(std::fmod(x, q.x), std::fmod(y, q.y), std::fmod(z, q.z), std::fmod(w, q.w));
2015-09-30 02:56:11 +02:00
}
Quaternion Quaternion::operator % (Value s) const
2015-09-30 02:56:11 +02:00
{
return Quaternion(std::fmod(x, s), std::fmod(y, s), std::fmod(z, s), std::fmod(w, s));
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator + () const
2015-09-30 02:56:11 +02:00
{
return Quaternion(std::fabs(x), std::fabs(y), std::fabs(z), std::fabs(w));
2015-09-30 02:56:11 +02:00
}
Quaternion Quaternion::operator - () const
2015-09-30 02:56:11 +02:00
{
return Quaternion(-x, -y, -z, -w);
}
// ------------------------------------------------------------------------------------------------
bool Quaternion::operator == (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return EpsEq(x, q.x) && EpsEq(y, q.y) && EpsEq(z, q.z) && EpsEq(w, q.w);
}
bool Quaternion::operator != (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return !EpsEq(x, q.x) && !EpsEq(y, q.y) && !EpsEq(z, q.z) && !EpsEq(w, q.w);
}
bool Quaternion::operator < (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return EpsLt(x, q.x) && EpsLt(y, q.y) && EpsLt(z, q.z) && EpsLt(w, q.w);
2015-09-30 02:56:11 +02:00
}
bool Quaternion::operator > (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return EpsGt(x, q.x) && EpsGt(y, q.y) && EpsGt(z, q.z) && EpsGt(w, q.w);
2015-09-30 02:56:11 +02:00
}
bool Quaternion::operator <= (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return EpsLtEq(x, q.x) && EpsLtEq(y, q.y) && EpsLtEq(z, q.z) && EpsLtEq(w, q.w);
2015-09-30 02:56:11 +02:00
}
bool Quaternion::operator >= (const Quaternion & q) const
2015-09-30 02:56:11 +02:00
{
return EpsGtEq(x, q.x) && EpsGtEq(y, q.y) && EpsGtEq(z, q.z) && EpsGtEq(w, q.w);
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
Int32 Quaternion::Cmp(const Quaternion & o) const
2015-09-30 02:56:11 +02:00
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
CSStr Quaternion::ToString() const
2015-09-30 02:56:11 +02:00
{
return ToStrF("%f,%f,%f,%f", x, y, z, w);
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
void Quaternion::Set(Value ns)
2015-09-30 02:56:11 +02:00
{
x = ns;
y = ns;
z = ns;
w = ns;
}
void Quaternion::Set(Value nx, Value ny, Value nz)
2015-09-30 02:56:11 +02:00
{
x = nx;
y = ny;
z = nz;
}
void Quaternion::Set(Value nx, Value ny, Value nz, Value nw)
2015-09-30 02:56:11 +02:00
{
x = nx;
y = ny;
z = nz;
w = nw;
}
// ------------------------------------------------------------------------------------------------
void Quaternion::Set(const Quaternion & q)
2015-09-30 02:56:11 +02:00
{
x = q.x;
y = q.y;
z = q.z;
w = q.w;
}
void Quaternion::Set(const Vector3 & v)
2015-09-30 02:56:11 +02:00
{
x = v.x;
y = v.y;
z = v.z;
w = 0.0;
}
void Quaternion::Set(const Vector4 & v)
2015-09-30 02:56:11 +02:00
{
x = v.x;
y = v.y;
z = v.z;
w = v.w;
}
// ------------------------------------------------------------------------------------------------
void Quaternion::Set(CSStr values, SQChar delim)
2015-09-30 02:56:11 +02:00
{
Set(Quaternion::Get(values, delim));
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
void Quaternion::Generate()
2015-09-30 02:56:11 +02:00
{
x = GetRandomFloat32();
y = GetRandomFloat32();
z = GetRandomFloat32();
w = GetRandomFloat32();
2015-09-30 02:56:11 +02:00
}
void Quaternion::Generate(Value min, Value max)
2015-09-30 02:56:11 +02:00
{
if (EpsLt(max, min))
{
STHROWF("max value is lower than min value");
}
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
2015-09-30 02:56:11 +02:00
}
void Quaternion::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax)
2015-09-30 02:56:11 +02:00
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin) || EpsLt(wmax, wmin))
{
STHROWF("max value is lower than min value");
}
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
y = GetRandomFloat32(ymin, ymax);
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::Abs() const
2015-09-30 02:56:11 +02:00
{
return Quaternion(std::fabs(x), std::fabs(y), std::fabs(z), std::fabs(w));
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
const Quaternion & Quaternion::Get(CSStr str)
{
return Quaternion::Get(str, Quaternion::Delim);
}
// ------------------------------------------------------------------------------------------------
const Quaternion & Quaternion::Get(CSStr str, SQChar delim)
{
// The format specifications that will be used to scan the string
static SQChar fs[] = _SC(" %f , %f , %f , %f ");
static Quaternion quat;
// Clear previous values, if any
quat.Clear();
// Is the specified string empty?
if (!str || *str == '\0')
{
return quat; // Return the value as is!
}
// Assign the specified delimiter
fs[4] = delim;
fs[9] = delim;
fs[14] = delim;
// Attempt to extract the component values from the specified string
std::sscanf(str, fs, &quat.x, &quat.y, &quat.z, &quat.w);
// Return the resulted value
return quat;
}
// ------------------------------------------------------------------------------------------------
const Quaternion & GetQuaternion()
{
static Quaternion quat;
quat.Clear();
return quat;
}
const Quaternion & GetQuaternion(Float32 sv)
{
static Quaternion quat;
quat.Set(sv);
return quat;
}
const Quaternion & GetQuaternion(Float32 xv, Float32 yv, Float32 zv)
{
static Quaternion quat;
quat.Set(xv, yv, zv);
return quat;
}
const Quaternion & GetQuaternion(Float32 xv, Float32 yv, Float32 zv, Float32 wv)
{
static Quaternion quat;
quat.Set(xv, yv, zv, wv);
return quat;
}
const Quaternion & GetQuaternion(const Quaternion & o)
{
static Quaternion quat;
quat.Set(o);
return quat;
}
2015-09-30 02:56:11 +02:00
// ================================================================================================
void Register_Quaternion(HSQUIRRELVM vm)
2015-09-30 02:56:11 +02:00
{
typedef Quaternion::Value Val;
RootTable(vm).Bind(_SC("Quaternion"), Class< Quaternion >(vm, _SC("Quaternion"))
// Constructors
2015-09-30 02:56:11 +02:00
.Ctor()
.Ctor< Val >()
.Ctor< Val, Val, Val >()
.Ctor< Val, Val, Val, Val >()
// Static Members
.SetStaticValue(_SC("Delim"), &Quaternion::Delim)
// Member Variables
.Var(_SC("X"), &Quaternion::x)
.Var(_SC("Y"), &Quaternion::y)
.Var(_SC("Z"), &Quaternion::z)
.Var(_SC("W"), &Quaternion::w)
// Properties
.Prop(_SC("Abs"), &Quaternion::Abs)
// Core Meta-methods
2015-09-30 02:56:11 +02:00
.Func(_SC("_tostring"), &Quaternion::ToString)
.SquirrelFunc(_SC("_typename"), &Quaternion::Typename)
2015-09-30 02:56:11 +02:00
.Func(_SC("_cmp"), &Quaternion::Cmp)
// Meta-methods
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("_add"), &Quaternion::operator +)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("_sub"), &Quaternion::operator -)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("_mul"), &Quaternion::operator *)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("_div"), &Quaternion::operator /)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("_modulo"), &Quaternion::operator %)
.Func< Quaternion (Quaternion::*)(void) const >(_SC("_unm"), &Quaternion::operator -)
// Setters
.Overload< void (Quaternion::*)(Val) >(_SC("Set"), &Quaternion::Set)
.Overload< void (Quaternion::*)(Val, Val, Val) >(_SC("Set"), &Quaternion::Set)
.Overload< void (Quaternion::*)(Val, Val, Val, Val) >(_SC("Set"), &Quaternion::Set)
.Overload< void (Quaternion::*)(const Quaternion &) >(_SC("SetQuat"), &Quaternion::Set)
.Overload< void (Quaternion::*)(const Vector3 &) >(_SC("SetVec3"), &Quaternion::Set)
.Overload< void (Quaternion::*)(const Vector4 &) >(_SC("SetVec4"), &Quaternion::Set)
.Overload< void (Quaternion::*)(CSStr, SQChar) >(_SC("SetStr"), &Quaternion::Set)
// Random Generators
.Overload< void (Quaternion::*)(void) >(_SC("Generate"), &Quaternion::Generate)
.Overload< void (Quaternion::*)(Val, Val) >(_SC("Generate"), &Quaternion::Generate)
.Overload< void (Quaternion::*)(Val, Val, Val, Val, Val, Val, Val, Val) >(_SC("Generate"), &Quaternion::Generate)
// Utility Methods
.Func(_SC("Clear"), &Quaternion::Clear)
// Static Overloads
.StaticOverload< const Quaternion & (*)(CSStr) >(_SC("FromStr"), &Quaternion::Get)
.StaticOverload< const Quaternion & (*)(CSStr, SQChar) >(_SC("FromStr"), &Quaternion::Get)
// Operator Exposure
.Func< Quaternion & (Quaternion::*)(const Quaternion &) >(_SC("opAddAssign"), &Quaternion::operator +=)
.Func< Quaternion & (Quaternion::*)(const Quaternion &) >(_SC("opSubAssign"), &Quaternion::operator -=)
.Func< Quaternion & (Quaternion::*)(const Quaternion &) >(_SC("opMulAssign"), &Quaternion::operator *=)
.Func< Quaternion & (Quaternion::*)(const Quaternion &) >(_SC("opDivAssign"), &Quaternion::operator /=)
.Func< Quaternion & (Quaternion::*)(const Quaternion &) >(_SC("opModAssign"), &Quaternion::operator %=)
.Func< Quaternion & (Quaternion::*)(Quaternion::Value) >(_SC("opAddAssignS"), &Quaternion::operator +=)
.Func< Quaternion & (Quaternion::*)(Quaternion::Value) >(_SC("opSubAssignS"), &Quaternion::operator -=)
.Func< Quaternion & (Quaternion::*)(Quaternion::Value) >(_SC("opMulAssignS"), &Quaternion::operator *=)
.Func< Quaternion & (Quaternion::*)(Quaternion::Value) >(_SC("opDivAssignS"), &Quaternion::operator /=)
.Func< Quaternion & (Quaternion::*)(Quaternion::Value) >(_SC("opModAssignS"), &Quaternion::operator %=)
.Func< Quaternion & (Quaternion::*)(void) >(_SC("opPreInc"), &Quaternion::operator ++)
.Func< Quaternion & (Quaternion::*)(void) >(_SC("opPreDec"), &Quaternion::operator --)
.Func< Quaternion (Quaternion::*)(int) >(_SC("opPostInc"), &Quaternion::operator ++)
.Func< Quaternion (Quaternion::*)(int) >(_SC("opPostDec"), &Quaternion::operator --)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("opAdd"), &Quaternion::operator +)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("opSub"), &Quaternion::operator -)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("opMul"), &Quaternion::operator *)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("opDiv"), &Quaternion::operator /)
.Func< Quaternion (Quaternion::*)(const Quaternion &) const >(_SC("opMod"), &Quaternion::operator %)
.Func< Quaternion (Quaternion::*)(Quaternion::Value) const >(_SC("opAddS"), &Quaternion::operator +)
.Func< Quaternion (Quaternion::*)(Quaternion::Value) const >(_SC("opSubS"), &Quaternion::operator -)
.Func< Quaternion (Quaternion::*)(Quaternion::Value) const >(_SC("opMulS"), &Quaternion::operator *)
.Func< Quaternion (Quaternion::*)(Quaternion::Value) const >(_SC("opDivS"), &Quaternion::operator /)
.Func< Quaternion (Quaternion::*)(Quaternion::Value) const >(_SC("opModS"), &Quaternion::operator %)
.Func< Quaternion (Quaternion::*)(void) const >(_SC("opUnPlus"), &Quaternion::operator +)
.Func< Quaternion (Quaternion::*)(void) const >(_SC("opUnMinus"), &Quaternion::operator -)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opEqual"), &Quaternion::operator ==)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opNotEqual"), &Quaternion::operator !=)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opLessThan"), &Quaternion::operator <)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opGreaterThan"), &Quaternion::operator >)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opLessEqual"), &Quaternion::operator <=)
.Func< bool (Quaternion::*)(const Quaternion &) const >(_SC("opGreaterEqual"), &Quaternion::operator >=)
2015-09-30 02:56:11 +02:00
);
}
} // Namespace:: SqMod