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

530 lines
16 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/Random.hpp"
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
const Quaternion Quaternion::NIL = Quaternion(0);
const Quaternion Quaternion::MIN = Quaternion(NumLimit< Quaternion::Value >::Min);
const Quaternion Quaternion::MAX = Quaternion(NumLimit< Quaternion::Value >::Max);
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
SQChar Quaternion::Delim = ',';
// ------------------------------------------------------------------------------------------------
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(const Quaternion & o)
: x(o.x), y(o.y), z(o.z), w(o.w)
2015-09-30 02:56:11 +02:00
{
/* ... */
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
Quaternion::~Quaternion()
{
/* ... */
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator = (const Quaternion & o)
2015-09-30 02:56:11 +02:00
{
x = o.x;
y = o.y;
z = o.z;
w = o.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 = (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 = fmod(x, q.x);
y = fmod(y, q.y);
z = fmod(z, q.z);
w = 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 = fmod(x, s);
y = fmod(y, s);
z = fmod(z, s);
w = 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(fmod(x, q.x), fmod(y, q.y), fmod(z, q.z), 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(fmod(x, s), fmod(y, s), fmod(z, s), 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(fabs(x), fabs(y), fabs(z), 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 ToStringF("%f,%f,%f,%f", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
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(GetQuaternion(values, delim));
}
// ------------------------------------------------------------------------------------------------
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))
2015-09-30 02:56:11 +02:00
{
SqThrow("max value is lower than min value");
2015-09-30 02:56:11 +02:00
}
else
{
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))
2015-09-30 02:56:11 +02:00
{
SqThrow("max value is lower than min value");
2015-09-30 02:56:11 +02:00
}
else
{
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(fabs(x), fabs(y), fabs(z), fabs(w));
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 */
2015-09-30 02:56:11 +02:00
.Var(_SC("x"), &Quaternion::x)
.Var(_SC("y"), &Quaternion::y)
.Var(_SC("z"), &Quaternion::z)
.Var(_SC("w"), &Quaternion::w)
/* Properties */
2015-09-30 02:56:11 +02:00
.Prop(_SC("abs"), &Quaternion::Abs)
/* Core Metamethods */
2015-09-30 02:56:11 +02:00
.Func(_SC("_tostring"), &Quaternion::ToString)
.Func(_SC("_cmp"), &Quaternion::Cmp)
/* Metamethods */
2015-09-30 02:56:11 +02:00
.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)
/* Operator Exposure */
2015-09-30 02:56:11 +02:00
.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 >=)
);
}
} // Namespace:: SqMod