1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-15 22:57:12 +02:00

Rename source to module.

This commit is contained in:
Sandu Liviu Catalin
2020-03-21 23:02:27 +02:00
parent a5c87bae5e
commit c0fd374404
237 changed files with 0 additions and 272718 deletions

923
module/Base/AABB.cpp Normal file
View File

@ -0,0 +1,923 @@
// ------------------------------------------------------------------------------------------------
#include "Base/AABB.hpp"
#include "Base/Sphere.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("AABB"))
// ------------------------------------------------------------------------------------------------
const AABB AABB::NIL = AABB(0, 0);
const AABB AABB::MIN = AABB(-1, -1, -1, 1, 1, 1);
const AABB AABB::MAX = AABB(HUGE_VALF, -HUGE_VALF);
// ------------------------------------------------------------------------------------------------
SQChar AABB::Delim = ',';
// ------------------------------------------------------------------------------------------------
AABB::AABB()
: min(HUGE_VALF), max(-HUGE_VALF)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB::AABB(Value mins, Value maxs)
: min(mins), max(maxs)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB::AABB(Value xv, Value yv, Value zv)
: min(xv, yv, zv)
, max(xv, yv, zv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB::AABB(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax)
: min(xmin, ymin, zmin), max(xmax, ymax, zmax)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB::AABB(const Vector3 & vmin, const Vector3 & vmax)
: min(vmin), max(vmax)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator = (const Vector3 & v)
{
DefineVector3(v);
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator += (const AABB & b)
{
min += b.min;
max += b.max;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator -= (const AABB & b)
{
min -= b.min;
max -= b.max;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator *= (const AABB & b)
{
min *= b.min;
max *= b.max;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator /= (const AABB & b)
{
min /= b.min;
max /= b.max;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator %= (const AABB & b)
{
min %= b.min;
max %= b.max;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator += (Value s)
{
min += s;
max += s;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator -= (Value s)
{
min -= s;
max -= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator *= (Value s)
{
min *= s;
max *= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator /= (Value s)
{
min /= s;
max /= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator %= (Value s)
{
min %= s;
max %= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator ++ ()
{
++min;
++max;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB & AABB::operator -- ()
{
--min;
--max;
return *this;
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator ++ (int)
{
AABB state(*this);
++min;
++max;
return state;
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator -- (int)
{
AABB state(*this);
--min;
--max;
return state;
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator + (const AABB & b) const
{
return AABB(min + b.min, max + b.max);
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator - (const AABB & b) const
{
return AABB(min - b.min, max - b.max);
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator * (const AABB & b) const
{
return AABB(min * b.min, max * b.max);
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator / (const AABB & b) const
{
return AABB(min / b.min, max / b.max);
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator % (const AABB & b) const
{
return AABB(min % b.min, max % b.max);
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator + (Value s) const
{
return AABB(min + s, max + s);
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator - (Value s) const
{
return AABB(min - s, max - s);
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator * (Value s) const
{
return AABB(min * s, max * s);
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator / (Value s) const
{
return AABB(min / s, max / s);
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator % (Value s) const
{
return AABB(min % s, max % s);
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator + () const
{
return AABB(min.Abs(), max.Abs());
}
// ------------------------------------------------------------------------------------------------
AABB AABB::operator - () const
{
return AABB(-min, -max);
}
// ------------------------------------------------------------------------------------------------
bool AABB::operator == (const AABB & b) const
{
return (min == b.min) && (max == b.max);
}
// ------------------------------------------------------------------------------------------------
bool AABB::operator != (const AABB & b) const
{
return (min != b.min) || (max != b.max);
}
// ------------------------------------------------------------------------------------------------
bool AABB::operator < (const AABB & b) const
{
return (min < b.min) && (max < b.max);
}
// ------------------------------------------------------------------------------------------------
bool AABB::operator > (const AABB & b) const
{
return (min > b.min) && (max > b.max);
}
// ------------------------------------------------------------------------------------------------
bool AABB::operator <= (const AABB & b) const
{
return (min <= b.min) && (max <= b.max);
}
// ------------------------------------------------------------------------------------------------
bool AABB::operator >= (const AABB & b) const
{
return (min >= b.min) && (max >= b.max);
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::Cmp(const AABB & o) const
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
CSStr AABB::ToString() const
{
return ToStrF("%f,%f,%f,%f,%f,%f", min.x, min.y, min.z, max.x, max.y, max.z);
}
// ------------------------------------------------------------------------------------------------
void AABB::SetStr(SQChar delim, StackStrF & values)
{
DefineAABB(AABB::GetEx(delim, values));
}
// ------------------------------------------------------------------------------------------------
void AABB::Clear()
{
min.SetVector3Ex(HUGE_VALF, HUGE_VALF, HUGE_VALF);
max.SetVector3Ex(-HUGE_VALF, -HUGE_VALF, -HUGE_VALF);
}
// ------------------------------------------------------------------------------------------------
void AABB::DefineScalar(Value mins, Value maxs)
{
min.SetVector3Ex(mins, mins, mins);
max.SetVector3Ex(maxs, maxs, maxs);
}
// ------------------------------------------------------------------------------------------------
void AABB::DefineVector3(const Vector3 & point)
{
min = max = point;
}
// ------------------------------------------------------------------------------------------------
void AABB::DefineVector3Ex(Value x, Value y, Value z)
{
min.SetVector3Ex(x, y, z);
max.SetVector3Ex(x, y, z);
}
// ------------------------------------------------------------------------------------------------
void AABB::DefineAllVector3(const Vector3 & nmin, const Vector3 & nmax)
{
min = nmin;
max = nmax;
}
// ------------------------------------------------------------------------------------------------
void AABB::DefineAllVector3Ex(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax)
{
min.SetVector3Ex(xmin, ymin, zmin);
max.SetVector3Ex(xmax, ymax, zmax);
}
// ------------------------------------------------------------------------------------------------
void AABB::DefineAABB(const AABB & box)
{
min = box.min;
max = box.max;
}
// ------------------------------------------------------------------------------------------------
void AABB::DefineSphere(const Sphere & sphere)
{
min = sphere.pos + Vector3(-sphere.rad);
max = sphere.pos + Vector3(sphere.rad);
}
// ------------------------------------------------------------------------------------------------
void AABB::DefineSphereEx(Value x, Value y, Value z, Value r)
{
DefineSphere(Sphere(x, y, z, r));
}
// ------------------------------------------------------------------------------------------------
void AABB::MergeVector3(const Vector3 & point)
{
MergeVector3Ex(point.x, point.y, point.z);
}
// ------------------------------------------------------------------------------------------------
void AABB::MergeVector3Ex(Value x, Value y, Value z)
{
if (x < min.x)
{
min.x = x;
}
if (y < min.y)
{
min.y = y;
}
if (z < min.z)
{
min.z = z;
}
if (x > max.x)
{
max.x = x;
}
if (y > max.y)
{
max.y = y;
}
if (z > max.z)
{
max.z = z;
}
}
// ------------------------------------------------------------------------------------------------
void AABB::MergeAABB(const AABB & box)
{
if (box.min.x < min.x)
{
min.x = box.min.x;
}
if (box.min.y < min.y)
{
min.y = box.min.y;
}
if (box.min.z < min.z)
{
min.z = box.min.z;
}
if (box.max.x > max.x)
{
max.x = box.max.x;
}
if (box.max.y > max.y)
{
max.y = box.max.y;
}
if (box.max.z > max.z)
{
max.z = box.max.z;
}
}
// ------------------------------------------------------------------------------------------------
void AABB::MergeAABBEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax)
{
if (xmin < min.x)
{
min.x = xmin;
}
if (ymin < min.y)
{
min.y = ymin;
}
if (zmin < min.z)
{
min.z = zmin;
}
if (xmax > max.x)
{
max.x = xmax;
}
if (ymax > max.y)
{
max.y = ymax;
}
if (zmax > max.z)
{
max.z = zmax;
}
}
// ------------------------------------------------------------------------------------------------
void AABB::MergeSphere(const Sphere & sphere)
{
MergeVector3(sphere.pos + Vector3(sphere.rad));
MergeVector3(sphere.pos + Vector3(-sphere.rad));
}
// ------------------------------------------------------------------------------------------------
void AABB::MergeSphereEx(Value x, Value y, Value z, Value r)
{
MergeSphere(Sphere(x, y, z, r));
}
// ------------------------------------------------------------------------------------------------
bool AABB::Empty() const
{
return (min == max);
}
// ------------------------------------------------------------------------------------------------
bool AABB::Defined() const
{
return min.x != HUGE_VALF;
}
// ------------------------------------------------------------------------------------------------
Vector3 AABB::Center() const
{
return (max + min) * 0.5f;
}
// ------------------------------------------------------------------------------------------------
Vector3 AABB::Size() const
{
return max - min;
}
// ------------------------------------------------------------------------------------------------
Vector3 AABB::HalfSize() const
{
return (max - min) * 0.5f;
}
// ------------------------------------------------------------------------------------------------
AABB::Value AABB::Radius() const
{
return Size().GetLength() / Value(2);
}
// ------------------------------------------------------------------------------------------------
AABB::Value AABB::Volume() const
{
const Vector3 v = Size();
return static_cast< Value >(v.x * v.y * v.z);
}
// ------------------------------------------------------------------------------------------------
AABB::Value AABB::Area() const
{
const Vector3 v = Size();
return static_cast< Value >(Value(2) * (v.x * v.y + v.x * v.z + v.y * v.z));
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsVector3Inside(const Vector3 & point) const
{
return (point.x < min.x || point.x > max.x ||
point.y < min.y || point.y > max.y ||
point.z < min.z || point.z > max.z) ? SQMODI_OUTSIDE : SQMODI_INSIDE;
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsVector3InsideEx(Value x, Value y, Value z) const
{
return (x < min.x || x > max.x ||
y < min.y || y > max.y ||
z < min.z || z > max.z) ? SQMODI_OUTSIDE : SQMODI_INSIDE;
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsAABBInside(const AABB & box) const
{
if (box.max.x < min.x || box.min.x > max.x ||
box.max.y < min.y || box.min.y > max.y ||
box.max.z < min.z || box.min.z > max.z)
{
return SQMODI_OUTSIDE;
}
else if (box.min.x < min.x || box.max.x > max.x ||
box.min.y < min.y || box.max.y > max.y ||
box.min.z < min.z || box.max.z > max.z)
{
return SQMODI_INTERSECTS;
}
else
{
return SQMODI_INSIDE;
}
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsAABBInsideEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const
{
if (xmax < min.x || xmin > max.x ||
ymax < min.y || ymin > max.y ||
zmax < min.z || zmin > max.z)
{
return SQMODI_OUTSIDE;
}
else if (xmin < min.x || xmax > max.x ||
ymin < min.y || ymax > max.y ||
zmin < min.z || zmax > max.z)
{
return SQMODI_INTERSECTS;
}
else
{
return SQMODI_INSIDE;
}
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsAABBInsideFast(const AABB & box) const
{
if (box.max.x < min.x || box.min.x > max.x ||
box.max.y < min.y || box.min.y > max.y ||
box.max.z < min.z || box.min.z > max.z)
{
return SQMODI_OUTSIDE;
}
else
{
return SQMODI_INSIDE;
}
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsAABBInsideFastEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const
{
if (xmax < min.x || xmin > max.x ||
ymax < min.y || ymin > max.y ||
zmax < min.z || zmin > max.z)
{
return SQMODI_OUTSIDE;
}
else
{
return SQMODI_INSIDE;
}
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsSphereInside(const Sphere & sphere) const
{
Value dist_squared = 0, temp;
const Vector3 & center = sphere.pos;
if (center.x < min.x)
{
temp = center.x - min.x;
dist_squared += temp * temp;
}
else if (center.x > max.x)
{
temp = center.x - max.x;
dist_squared += temp * temp;
}
if (center.y < min.y)
{
temp = center.y - min.y;
dist_squared += temp * temp;
}
else if (center.y > max.y)
{
temp = center.y - max.y;
dist_squared += temp * temp;
}
if (center.z < min.z)
{
temp = center.z - min.z;
dist_squared += temp * temp;
}
else if (center.z > max.z)
{
temp = center.z - max.z;
dist_squared += temp * temp;
}
const Value radius = sphere.rad;
if (dist_squared >= radius * radius)
{
return SQMODI_OUTSIDE;
}
else if (center.x - radius < min.x || center.x + radius > max.x || center.y - radius < min.y ||
center.y + radius > max.y || center.z - radius < min.z || center.z + radius > max.z)
{
return SQMODI_INTERSECTS;
}
else
{
return SQMODI_INSIDE;
}
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsSphereInsideEx(Value x, Value y, Value z, Value r) const
{
return IsSphereInside(Sphere(x, y, z, r));
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsSphereInsideFast(const Sphere & sphere) const
{
Value dist_squared = 0, temp;
const Vector3& center = sphere.pos;
if (center.x < min.x)
{
temp = center.x - min.x;
dist_squared += temp * temp;
}
else if (center.x > max.x)
{
temp = center.x - max.x;
dist_squared += temp * temp;
}
if (center.y < min.y)
{
temp = center.y - min.y;
dist_squared += temp * temp;
}
else if (center.y > max.y)
{
temp = center.y - max.y;
dist_squared += temp * temp;
}
if (center.z < min.z)
{
temp = center.z - min.z;
dist_squared += temp * temp;
}
else if (center.z > max.z)
{
temp = center.z - max.z;
dist_squared += temp * temp;
}
const Value radius = sphere.rad;
if (dist_squared >= radius * radius)
{
return SQMODI_OUTSIDE;
}
else
{
return SQMODI_INSIDE;
}
}
// ------------------------------------------------------------------------------------------------
Int32 AABB::IsSphereInsideFastEx(Value x, Value y, Value z, Value r) const
{
return IsSphereInsideFast(Sphere(x, y, z, r));
}
// ------------------------------------------------------------------------------------------------
const AABB & AABB::Get(StackStrF & str)
{
return AABB::GetEx(AABB::Delim, str);
}
// ------------------------------------------------------------------------------------------------
const AABB & AABB::GetEx(SQChar delim, StackStrF & str)
{
// The format specifications that will be used to scan the string
static SQChar fs[] = _SC(" %f , %f , %f , %f , %f , %f ");
static AABB box;
// Clear previous values, if any
box.Clear();
// Is the specified string empty?
if (str.mLen <= 0)
{
return box; // Return the value as is!
}
// Assign the specified delimiter
fs[4] = delim;
fs[9] = delim;
fs[14] = delim;
fs[19] = delim;
fs[24] = delim;
// Attempt to extract the component values from the specified string
std::sscanf(str.mPtr, fs, &box.min.x, &box.min.y, &box.min.z, &box.max.x, &box.max.y, &box.max.z);
// Return the resulted value
return box;
}
// ------------------------------------------------------------------------------------------------
const AABB & GetAABB()
{
static AABB box;
box.Clear();
return box;
}
// ------------------------------------------------------------------------------------------------
const AABB & GetAABB(Float32 mins, Float32 maxs)
{
static AABB box;
box.DefineScalar(mins, maxs);
return box;
}
// ------------------------------------------------------------------------------------------------
const AABB & GetAABB(Float32 xv, Float32 yv, Float32 zv)
{
static AABB box;
box.DefineVector3Ex(xv, yv, zv);
return box;
}
// ------------------------------------------------------------------------------------------------
const AABB & GetAABB(Float32 xmin, Float32 ymin, Float32 zmin, Float32 xmax, Float32 ymax, Float32 zmax)
{
static AABB box;
box.DefineAllVector3Ex(xmin, ymin, zmin, xmax, ymax, zmax);
return box;
}
// ------------------------------------------------------------------------------------------------
const AABB & GetAABB(const Vector3 & vmin, const Vector3 & vmax)
{
static AABB box;
box.DefineAllVector3(vmin, vmax);
return box;
}
// ------------------------------------------------------------------------------------------------
const AABB & GetAABB(const AABB & o)
{
static AABB box;
box.DefineAABB(o);
return box;
}
// ================================================================================================
void Register_AABB(HSQUIRRELVM vm)
{
typedef AABB::Value Val;
RootTable(vm).Bind(Typename::Str,
Class< AABB >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< const AABB & >()
.Ctor< Val, Val, Val >()
.Ctor< Val, Val, Val, Val, Val, Val >()
.Ctor< const Vector3 &, const Vector3 & >()
// Member Variables
.Var(_SC("min"), &AABB::min)
.Var(_SC("max"), &AABB::max)
.Var(_SC("Min"), &AABB::min)
.Var(_SC("Max"), &AABB::max)
// Core Meta-methods
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &AABB::ToString)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< AABB >, SQFloat, SQInteger, bool, std::nullptr_t, AABB >)
.Func< AABB (AABB::*)(void) const >(_SC("_unm"), &AABB::operator -)
// Properties
.Prop(_SC("Empty"), &AABB::Empty)
.Prop(_SC("Defined"), &AABB::Defined)
.Prop(_SC("Center"), &AABB::Center)
.Prop(_SC("Size"), &AABB::Size)
.Prop(_SC("Extent"), &AABB::Size)
.Prop(_SC("HalfSize"), &AABB::HalfSize)
.Prop(_SC("HalfExtent"), &AABB::HalfSize)
.Prop(_SC("Radius"), &AABB::Radius)
.Prop(_SC("Volume"), &AABB::Volume)
.Prop(_SC("Area"), &AABB::Area)
// Member Methods
.FmtFunc(_SC("SetStr"), &AABB::SetStr)
.Func(_SC("Clear"), &AABB::Clear)
.Func(_SC("DefineScalar"), &AABB::DefineScalar)
.Func(_SC("DefineVector3"), &AABB::DefineVector3)
.Func(_SC("DefineVector3Ex"), &AABB::DefineVector3Ex)
.Func(_SC("DefineAllVector3"), &AABB::DefineAllVector3)
.Func(_SC("DefineAllVector3Ex"), &AABB::DefineAllVector3Ex)
.Func(_SC("DefineAABB"), &AABB::DefineAABB)
.Func(_SC("DefineSphere"), &AABB::DefineSphere)
.Func(_SC("DefineSphereEx"), &AABB::DefineSphereEx)
.Func(_SC("MergeVector3"), &AABB::MergeVector3)
.Func(_SC("MergeVector3Ex"), &AABB::MergeVector3Ex)
.Func(_SC("MergeAABB"), &AABB::MergeAABB)
.Func(_SC("MergeAABBEx"), &AABB::MergeAABBEx)
.Func(_SC("MergeSphere"), &AABB::MergeSphere)
.Func(_SC("MergeSphereEx"), &AABB::MergeSphereEx)
.Func(_SC("IsVector3Inside"), &AABB::IsVector3Inside)
.Func(_SC("IsVector3InsideEx"), &AABB::IsVector3InsideEx)
.Func(_SC("IsAABBInside"), &AABB::IsAABBInside)
.Func(_SC("IsAABBInsideEx"), &AABB::IsAABBInsideEx)
.Func(_SC("IsAABBInsideFast"), &AABB::IsAABBInsideFast)
.Func(_SC("IsAABBInsideFastEx"), &AABB::IsAABBInsideFastEx)
.Func(_SC("IsSphereInside"), &AABB::IsSphereInside)
.Func(_SC("IsSphereInsideEx"), &AABB::IsSphereInsideEx)
.Func(_SC("IsSphereInsideFast"), &AABB::IsSphereInsideFast)
.Func(_SC("IsSphereInsideFastEx"), &AABB::IsSphereInsideFastEx)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< AABB >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< AABB >)
.StaticFmtFunc(_SC("FromStr"), &AABB::Get)
.StaticFmtFunc(_SC("FromStrEx"), &AABB::GetEx)
// Operator Exposure
.Func< AABB & (AABB::*)(const AABB &) >(_SC("opAddAssign"), &AABB::operator +=)
.Func< AABB & (AABB::*)(const AABB &) >(_SC("opSubAssign"), &AABB::operator -=)
.Func< AABB & (AABB::*)(const AABB &) >(_SC("opMulAssign"), &AABB::operator *=)
.Func< AABB & (AABB::*)(const AABB &) >(_SC("opDivAssign"), &AABB::operator /=)
.Func< AABB & (AABB::*)(const AABB &) >(_SC("opModAssign"), &AABB::operator %=)
.Func< AABB & (AABB::*)(AABB::Value) >(_SC("opAddAssignS"), &AABB::operator +=)
.Func< AABB & (AABB::*)(AABB::Value) >(_SC("opSubAssignS"), &AABB::operator -=)
.Func< AABB & (AABB::*)(AABB::Value) >(_SC("opMulAssignS"), &AABB::operator *=)
.Func< AABB & (AABB::*)(AABB::Value) >(_SC("opDivAssignS"), &AABB::operator /=)
.Func< AABB & (AABB::*)(AABB::Value) >(_SC("opModAssignS"), &AABB::operator %=)
.Func< AABB & (AABB::*)(void) >(_SC("opPreInc"), &AABB::operator ++)
.Func< AABB & (AABB::*)(void) >(_SC("opPreDec"), &AABB::operator --)
.Func< AABB (AABB::*)(int) >(_SC("opPostInc"), &AABB::operator ++)
.Func< AABB (AABB::*)(int) >(_SC("opPostDec"), &AABB::operator --)
.Func< AABB (AABB::*)(const AABB &) const >(_SC("opAdd"), &AABB::operator +)
.Func< AABB (AABB::*)(const AABB &) const >(_SC("opSub"), &AABB::operator -)
.Func< AABB (AABB::*)(const AABB &) const >(_SC("opMul"), &AABB::operator *)
.Func< AABB (AABB::*)(const AABB &) const >(_SC("opDiv"), &AABB::operator /)
.Func< AABB (AABB::*)(const AABB &) const >(_SC("opMod"), &AABB::operator %)
.Func< AABB (AABB::*)(AABB::Value) const >(_SC("opAddS"), &AABB::operator +)
.Func< AABB (AABB::*)(AABB::Value) const >(_SC("opSubS"), &AABB::operator -)
.Func< AABB (AABB::*)(AABB::Value) const >(_SC("opMulS"), &AABB::operator *)
.Func< AABB (AABB::*)(AABB::Value) const >(_SC("opDivS"), &AABB::operator /)
.Func< AABB (AABB::*)(AABB::Value) const >(_SC("opModS"), &AABB::operator %)
.Func< AABB (AABB::*)(void) const >(_SC("opUnPlus"), &AABB::operator +)
.Func< AABB (AABB::*)(void) const >(_SC("opUnMinus"), &AABB::operator -)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opEqual"), &AABB::operator ==)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opNotEqual"), &AABB::operator !=)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opLessThan"), &AABB::operator <)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opGreaterThan"), &AABB::operator >)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opLessEqual"), &AABB::operator <=)
.Func< bool (AABB::*)(const AABB &) const >(_SC("opGreaterEqual"), &AABB::operator >=)
);
}
} // Namespace:: SqMod

483
module/Base/AABB.hpp Normal file
View File

@ -0,0 +1,483 @@
#ifndef _BASE_AABB_HPP_
#define _BASE_AABB_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Vector3.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Class used to represent an axis aligned bounding box in three-dimensional space.
*/
struct AABB
{
/* --------------------------------------------------------------------------------------------
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* Helper instances for common values mostly used as return types or comparison.
*/
static const AABB NIL;
static const AABB MIN;
static const AABB MAX;
/* --------------------------------------------------------------------------------------------
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* The minimum and maximum components of this type.
*/
Vector3 min, max;
/* --------------------------------------------------------------------------------------------
* Construct with zero size.
*/
AABB();
/* --------------------------------------------------------------------------------------------
* Construct a an equally sized and perfectly shaped box from scalar values.
*/
explicit AABB(Value mins, Value maxs);
/* --------------------------------------------------------------------------------------------
* Construct a an equally sized but imperfectly shaped box from individual components of a
* three-dimensional point.
*/
AABB(Value xv, Value yv, Value zv);
/* --------------------------------------------------------------------------------------------
* Construct a an unequally sized and imperfectly shaped box from individual components of two
* three-dimensional points.
*/
AABB(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax);
/* --------------------------------------------------------------------------------------------
* Construct a an unequally sized and imperfectly shaped box from two three-dimensional
* vectors representing two three-dimensional points.
*/
AABB(const Vector3 & vmin, const Vector3 & vmax);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
AABB(const AABB & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
AABB(AABB && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~AABB() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
AABB & operator = (const AABB & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
AABB & operator = (AABB && o) = default;
/* --------------------------------------------------------------------------------------------
* Three-dimensional vector assignment operator.
*/
AABB & operator = (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* Addition assignment operator.
*/
AABB & operator += (const AABB & b);
/* --------------------------------------------------------------------------------------------
* Subtraction assignment operator.
*/
AABB & operator -= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* Multiplication assignment operator.
*/
AABB & operator *= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* Division assignment operator.
*/
AABB & operator /= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* Modulo assignment operator.
*/
AABB & operator %= (const AABB & b);
/* --------------------------------------------------------------------------------------------
* Scalar value addition assignment operator.
*/
AABB & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction assignment operator.
*/
AABB & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication assignment operator.
*/
AABB & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value division assignment operator.
*/
AABB & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value modulo assignment operator.
*/
AABB & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* Pre-increment operator.
*/
AABB & operator ++ ();
/* --------------------------------------------------------------------------------------------
* Pre-decrement operator.
*/
AABB & operator -- ();
/* --------------------------------------------------------------------------------------------
* Post-increment operator.
*/
AABB operator ++ (int);
/* --------------------------------------------------------------------------------------------
* Post-decrement operator.
*/
AABB operator -- (int);
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
AABB operator + (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
AABB operator - (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
AABB operator * (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
AABB operator / (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Modulo operator.
*/
AABB operator % (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Scalar value addition operator.
*/
AABB operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction operator.
*/
AABB operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication operator.
*/
AABB operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value division operator.
*/
AABB operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value modulo operator.
*/
AABB operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* Unary plus operator.
*/
AABB operator + () const;
/* --------------------------------------------------------------------------------------------
* Unary minus operator.
*/
AABB operator - () const;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const AABB & b) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
{
return Cmp(AABB(s, s, s, s, s, s));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
{
const Value v = static_cast< Value >(s);
return Cmp(AABB(v, v, v, v, v, v));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
{
const Value v = static_cast< Value >(s);
return Cmp(AABB(v, v, v, v, v, v));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
{
const Value v = static_cast< Value >(0);
return Cmp(AABB(v, v, v, v, v, v));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/
void SetStr(SQChar delim, StackStrF & values);
/* --------------------------------------------------------------------------------------------
* Clear the component values to default.
*/
void Clear();
/* --------------------------------------------------------------------------------------------
* Define from minimum and maximum floats (all dimensions same).
*/
void DefineScalar(Value mins, Value maxs);
/* --------------------------------------------------------------------------------------------
* Define from a point.
*/
void DefineVector3(const Vector3 & point);
/* --------------------------------------------------------------------------------------------
* Define from a point.
*/
void DefineVector3Ex(Value x, Value y, Value z);
/* --------------------------------------------------------------------------------------------
* Define from minimum and maximum vectors.
*/
void DefineAllVector3(const Vector3 & nmin, const Vector3 & nmax);
/* --------------------------------------------------------------------------------------------
* Define from minimum and maximum vectors.
*/
void DefineAllVector3Ex(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax);
/* --------------------------------------------------------------------------------------------
* Define from another bounding box.
*/
void DefineAABB(const AABB & box);
/* --------------------------------------------------------------------------------------------
* Define from a sphere.
*/
void DefineSphere(const Sphere & sphere);
/* --------------------------------------------------------------------------------------------
* Define from a sphere.
*/
void DefineSphereEx(Value x, Value y, Value z, Value r);
/* --------------------------------------------------------------------------------------------
* Merge a point into this bounding box.
*/
void MergeVector3(const Vector3 & point);
/* --------------------------------------------------------------------------------------------
* Merge a point into this bounding box.
*/
void MergeVector3Ex(Value x, Value y, Value z);
/* --------------------------------------------------------------------------------------------
* Merge another bounding box into this bounding box.
*/
void MergeAABB(const AABB & box);
/* --------------------------------------------------------------------------------------------
* Merge another bounding box into this bounding box.
*/
void MergeAABBEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax);
/* --------------------------------------------------------------------------------------------
* Merge a sphere into this bounding box.
*/
void MergeSphere(const Sphere & sphere);
/* --------------------------------------------------------------------------------------------
* Merge a sphere into this bounding box.
*/
void MergeSphereEx(Value x, Value y, Value z, Value r);
/* --------------------------------------------------------------------------------------------
* Check if the box is empty. This means that there is no space between the min and max edge.
*/
bool Empty() const;
/* --------------------------------------------------------------------------------------------
* Return true if this bounding box is defined via a previous call to Define() or Merge().
*/
bool Defined() const;
/* --------------------------------------------------------------------------------------------
* Return center.
*/
Vector3 Center() const;
/* --------------------------------------------------------------------------------------------
* Get size/extent of the box (maximal distance of two points in the box).
*/
Vector3 Size() const;
/* --------------------------------------------------------------------------------------------
* Return half-size.
*/
Vector3 HalfSize() const;
/* --------------------------------------------------------------------------------------------
* Get radius of the bounding sphere.
*/
Value Radius() const;
/* --------------------------------------------------------------------------------------------
* Get the volume enclosed by the box in cubed units.
*/
Value Volume() const;
/* --------------------------------------------------------------------------------------------
* Get the surface area of the box in squared units.
*/
Value Area() const;
/* --------------------------------------------------------------------------------------------
* Test if a point is inside.
*/
Int32 IsVector3Inside(const Vector3 & point) const;
/* --------------------------------------------------------------------------------------------
* Test if a point is inside.
*/
Int32 IsVector3InsideEx(Value x, Value y, Value z) const;
/* --------------------------------------------------------------------------------------------
* Test if another bounding box is inside, outside or intersects.
*/
Int32 IsAABBInside(const AABB & box) const;
/* --------------------------------------------------------------------------------------------
* Test if another bounding box is inside, outside or intersects.
*/
Int32 IsAABBInsideEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const;
/* --------------------------------------------------------------------------------------------
* Test if another bounding box is (partially) inside or outside.
*/
Int32 IsAABBInsideFast(const AABB & box) const;
/* --------------------------------------------------------------------------------------------
* Test if another bounding box is (partially) inside or outside.
*/
Int32 IsAABBInsideFastEx(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) const;
/* --------------------------------------------------------------------------------------------
* Test if a sphere is inside, outside or intersects.
*/
Int32 IsSphereInside(const Sphere & sphere) const;
/* --------------------------------------------------------------------------------------------
* Test if a sphere is inside, outside or intersects.
*/
Int32 IsSphereInsideEx(Value x, Value y, Value z, Value r) const;
/* --------------------------------------------------------------------------------------------
* Test if a sphere is (partially) inside or outside.
*/
Int32 IsSphereInsideFast(const Sphere & sphere) const;
/* --------------------------------------------------------------------------------------------
* Test if a sphere is (partially) inside or outside.
*/
Int32 IsSphereInsideFastEx(Value x, Value y, Value z, Value r) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the AABB type from a string.
*/
static const AABB & Get(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the AABB type from a string.
*/
static const AABB & GetEx(SQChar delim, StackStrF & str);
};
} // Namespace:: SqMod
#endif // _BASE_AABB_HPP_

612
module/Base/Buffer.cpp Normal file
View File

@ -0,0 +1,612 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Buffer.hpp"
#include "sqrat/sqratUtil.h"
// ------------------------------------------------------------------------------------------------
#include <cstdlib>
#include <cstring>
#include <exception>
#include <stdexcept>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Compute the next power of two for the specified number.
*/
inline unsigned int NextPow2(unsigned int num)
{
--num;
num |= num >> 1;
num |= num >> 2;
num |= num >> 4;
num |= num >> 8;
num |= num >> 16;
return ++num;
}
/* ------------------------------------------------------------------------------------------------
* Throw an memory exception.
*/
void ThrowMemExcept(const char * msg, ...)
{
// Exception messages should be concise
SQChar buffer[256];
// Variable arguments structure
va_list args;
// Get the specified arguments
va_start(args, msg);
// Run the specified format
int ret = std::vsnprintf(buffer, sizeof(buffer), msg, args);
// Check for formatting errors
if (ret < 0)
{
throw Sqrat::Exception(_SC("Unknown memory error"));
}
// Throw the actual exception
throw Sqrat::Exception(buffer);
}
/* ------------------------------------------------------------------------------------------------
* Allocate a memory buffer and return it.
*/
static Buffer::Pointer AllocMem(Buffer::SzType size)
{
// Attempt to allocate memory directly
Buffer::Pointer ptr = reinterpret_cast< Buffer::Pointer >(std::malloc(size));
// Validate the allocated memory
if (!ptr)
{
ThrowMemExcept("Unable to allocate (%u) bytes of memory", size);
}
// Return the allocated memory
return ptr;
}
/* ------------------------------------------------------------------------------------------------
* ...
*/
class MemCat
{
// --------------------------------------------------------------------------------------------
friend class Memory;
friend class Buffer;
public:
// --------------------------------------------------------------------------------------------
typedef Buffer::Value Value; // The type of value used to represent a byte.
// --------------------------------------------------------------------------------------------
typedef Buffer::Reference Reference; // A reference to the stored value type.
typedef Buffer::ConstRef ConstRef; // A const reference to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Buffer::Pointer Pointer; // A pointer to the stored value type.
typedef Buffer::ConstPtr ConstPtr; // A const pointer to the stored value type.
// --------------------------------------------------------------------------------------------
typedef Buffer::SzType SzType; // The type used to represent size in general.
private:
/* --------------------------------------------------------------------------------------------
* Structure used to store a memory chunk in the linked list.
*/
struct Node
{
// ----------------------------------------------------------------------------------------
SzType mCap; // The size of the memory chunk.
Pointer mPtr; // Pointer to the memory chunk.
Node* mNext; // The next node in the list.
/* ----------------------------------------------------------------------------------------
* Base constructor.
*/
Node(Node * next)
: mCap(0)
, mPtr(nullptr)
, mNext(next)
{
/* ... */
}
};
// --------------------------------------------------------------------------------------------
static Node * s_Nodes; /* List of unused node instances. */
// --------------------------------------------------------------------------------------------
Node* m_Head; /* The head memory node. */
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
MemCat()
: m_Head(nullptr)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~MemCat()
{
for (Node * node = m_Head, * next = nullptr; node; node = next)
{
// Free the memory (if any)
if (node->mPtr)
{
std::free(node->mPtr);
}
// Save the next node
next = node->mNext;
// Release the node instance
delete node;
}
// Explicitly set the head node to null
m_Head = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Clear all memory buffers from the pool.
*/
void Clear()
{
for (Node * node = m_Head, * next = nullptr; node; node = next)
{
// Free the memory (if any)
if (node->mPtr)
{
free(node->mPtr);
}
// Save the next node
next = node->mNext;
// Release the node instance
Push(node);
}
// Explicitly set the head node to null
m_Head = nullptr;
}
/* --------------------------------------------------------------------------------------------
* Grab a memory buffer from the pool.
*/
void Grab(Pointer & ptr, SzType & size)
{
// NOTE: Function assumes (size > 0)
// Find a buffer large enough to satisfy the requested size
for (Node * node = m_Head, * prev = nullptr; node; prev = node, node = node->mNext)
{
// Is this buffer large enough?
if (node->mCap >= size)
{
// Was there a previous node?
if (prev)
{
prev->mNext = node->mNext;
}
// Probably this was the head
else
{
m_Head = node->mNext;
}
// Assign the memory
ptr = node->mPtr;
// Assign the size
size = node->mCap;
// Release the node instance
Push(node);
// Exit the function
return;
}
}
// Round up the size to a power of two number
size = (size & (size - 1)) ? NextPow2(size) : size;
// Allocate the memory directly
ptr = AllocMem(size);
// See if the memory could be allocated
// (shouldn't reach this point if allocation failed)
if (!ptr)
{
// Revert the size
size = 0;
// Throw the exception
ThrowMemExcept("Unable to allocate (%u) bytes of memory", size);
}
}
/* --------------------------------------------------------------------------------------------
* Return a memory buffer to the pool.
*/
void Drop(Pointer & ptr, SzType & size)
{
if (!ptr)
{
ThrowMemExcept("Cannot store invalid memory buffer");
}
// Request a node instance
Node * node = Pull();
// Assign the specified memory
node->mPtr = ptr;
// Assign the specified size
node->mCap = size;
// Demote the current head node
node->mNext = m_Head;
// Promote as the head node
m_Head = node;
}
/* --------------------------------------------------------------------------------------------
* Allocate a group of nodes and pool them for later use.
*/
static void Make()
{
for (SzType n = 16; n; --n)
{
// Create a new node instance
s_Nodes = new Node(s_Nodes);
// Validate the head node
if (!s_Nodes)
{
ThrowMemExcept("Unable to allocate memory nodes");
}
}
}
/* --------------------------------------------------------------------------------------------
* Retrieve an unused node from the free list.
*/
static Node * Pull()
{
// Are there any nodes available?
if (!s_Nodes)
{
Make(); // Make some!
}
// Grab the head node
Node * node = s_Nodes;
// Promote the next node as the head
s_Nodes = node->mNext;
// Return the node
return node;
}
/* --------------------------------------------------------------------------------------------
* Return a node to the free list.
*/
static void Push(Node * node)
{
// See if the node is even valid
if (!node)
{
ThrowMemExcept("Attempting to push invalid node");
}
// Demote the current head node
node->mNext = s_Nodes;
// Promote as the head node
s_Nodes = node;
}
};
// ------------------------------------------------------------------------------------------------
MemCat::Node * MemCat::s_Nodes = nullptr;
/* ------------------------------------------------------------------------------------------------
* Lightweight memory allocator to reduce the overhead of small allocations.
*/
class Memory
{
// --------------------------------------------------------------------------------------------
friend class Buffer; // Allow the buffer type to access the memory categories.
friend class MemRef; // Allow the memory manager reference to create new instances.
private:
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Memory()
: m_Small()
, m_Medium()
, m_Large()
{
// Allocate several nodes for when memory starts pooling
MemCat::Make();
}
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Memory()
{
for (MemCat::Node * node = MemCat::s_Nodes, * next = nullptr; node; node = next)
{
// Save the next node
next = node->mNext;
// Release the node instance
delete node;
}
// Explicitly set the head node to null
MemCat::s_Nodes = nullptr;
}
private:
// --------------------------------------------------------------------------------------------
MemCat m_Small; // Small memory allocations of <= 1024 bytes.
MemCat m_Medium; // Medium memory allocations of <= 4096 bytes.
MemCat m_Large; // Large memory allocations of <= 4096 bytes.
};
// ------------------------------------------------------------------------------------------------
MemRef MemRef::s_Mem;
// ------------------------------------------------------------------------------------------------
void MemRef::Grab()
{
if (m_Ptr)
{
++(*m_Ref);
}
}
// ------------------------------------------------------------------------------------------------
void MemRef::Drop()
{
if (m_Ptr && --(*m_Ref) == 0)
{
delete m_Ptr;
delete m_Ref;
m_Ptr = nullptr;
m_Ref = nullptr;
}
}
// ------------------------------------------------------------------------------------------------
const MemRef & MemRef::Get()
{
if (!s_Mem.m_Ptr)
{
s_Mem.m_Ptr = new Memory();
s_Mem.m_Ref = new Counter(1);
}
return s_Mem;
}
// ------------------------------------------------------------------------------------------------
Buffer::Buffer(const Buffer & o)
: m_Ptr(nullptr)
, m_Cap(o.m_Cap)
, m_Cur(o.m_Cur)
, m_Mem(o.m_Mem)
{
if (m_Cap)
{
Request(o.m_Cap);
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
}
// ------------------------------------------------------------------------------------------------
Buffer::~Buffer()
{
// Do we have a buffer?
if (m_Ptr)
{
Release(); // Release it!
}
}
// ------------------------------------------------------------------------------------------------
Buffer & Buffer::operator = (const Buffer & o)
{
if (m_Ptr != o.m_Ptr)
{
// Can we work in the current buffer?
if (m_Cap && o.m_Cap <= m_Cap)
{
// It's safe to copy the data
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
// Do we even have data to copy?
else if (!o.m_Cap)
{
// Do we have a buffer?
if (m_Ptr)
{
Release(); // Release it!
}
}
else
{
// Do we have a buffer?
if (m_Ptr)
{
Release(); // Release it!
}
// Request a larger buffer
Request(o.m_Cap);
// Now it's safe to copy the data
std::memcpy(m_Ptr, o.m_Ptr, o.m_Cap);
}
// Also copy the edit cursor
m_Cur = o.m_Cur;
}
return *this;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Grow(SzType n)
{
// Backup the current memory
Buffer bkp(m_Ptr, m_Cap, m_Cur, m_Mem);
// Acquire a bigger buffer
Request(bkp.m_Cap + n);
// Copy the data from the old buffer
std::memcpy(m_Ptr, bkp.m_Ptr, bkp.m_Cap);
// Copy the previous edit cursor
m_Cur = bkp.m_Cur;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Request(SzType n)
{
// NOTE: Function assumes (n > 0)
// Is there a memory manager available?
if (!m_Mem)
{
// Round up the size to a power of two number
n = (n & (n - 1)) ? NextPow2(n) : n;
// Allocate the memory directly
m_Ptr = AllocMem(n);
}
// Find out in which category does this buffer reside
else if (n <= 1024)
{
m_Mem->m_Small.Grab(m_Ptr, n);
}
else if (n <= 4096)
{
m_Mem->m_Medium.Grab(m_Ptr, n);
}
else
{
m_Mem->m_Large.Grab(m_Ptr, n);
}
// If no errors occurred then we can set the size
m_Cap = n;
}
// ------------------------------------------------------------------------------------------------
void Buffer::Release()
{
// TODO: Implement a limit on how much memory can actually be pooled.
// Is there a memory manager available?
if (!m_Mem)
{
std::free(m_Ptr); // Deallocate the memory directly
}
// Find out to which category does this buffer belong
else if (m_Cap <= 1024)
{
m_Mem->m_Small.Drop(m_Ptr, m_Cap);
}
else if (m_Cap <= 4096)
{
m_Mem->m_Medium.Drop(m_Ptr, m_Cap);
}
else
{
m_Mem->m_Large.Drop(m_Ptr, m_Cap);
}
// Explicitly reset the buffer
m_Ptr = nullptr;
m_Cap = 0;
m_Cur = 0;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::Write(SzType pos, ConstPtr data, SzType size)
{
// Do we have what to write?
if (!data || !size)
{
return 0;
}
// See if the buffer size must be adjusted
else if ((pos + size) >= m_Cap)
{
// Acquire a larger buffer
Grow((pos + size) - m_Cap + 32);
}
// Copy the data into the internal buffer
std::memcpy(m_Ptr + pos, data, size);
// Return the amount of data written to the buffer
return size;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, ...)
{
// Initialize the variable argument list
va_list args;
va_start(args, fmt);
// Call the function that takes the variable argument list
const SzType ret = WriteF(pos, fmt, args);
// Finalize the variable argument list
va_end(args);
// Return the result
return ret;
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteF(SzType pos, const char * fmt, va_list args)
{
// Is the specified position within range?
if (pos >= m_Cap)
{
// Acquire a larger buffer
Grow(pos - m_Cap + 32);
}
// Backup the variable argument list
va_list args_cpy;
va_copy(args_cpy, args);
// Attempt to write to the current buffer
// (if empty, it should tell us the necessary size)
int ret = std::vsnprintf(m_Ptr + pos, m_Cap, fmt, args);
// Do we need a bigger buffer?
if ((pos + ret) >= m_Cap)
{
// Acquire a larger buffer
Grow((pos + ret) - m_Cap + 32);
// Retry writing the requested information
ret = std::vsnprintf(m_Ptr + pos, m_Cap, fmt, args_cpy);
}
// Return the value 0 if data could not be written
if (ret < 0)
{
return 0;
}
// Return the number of written characters
return static_cast< SzType >(ret);
}
// ------------------------------------------------------------------------------------------------
Buffer::SzType Buffer::WriteS(SzType pos, ConstPtr str)
{
// Is there any string to write?
if (str && *str != '\0')
{
// Forward this to the regular write function
return Write(pos, str, std::strlen(str));
}
// Nothing to write
return 0;
}
// ------------------------------------------------------------------------------------------------
void Buffer::AppendF(const char * fmt, ...)
{
// Initialize the variable argument list
va_list args;
va_start(args, fmt);
// Forward this to the regular write function
m_Cur += WriteF(m_Cur, fmt, args);
// Finalize the variable argument list
va_end(args);
}
// ------------------------------------------------------------------------------------------------
void Buffer::AppendS(const char * str)
{
// Is there any string to write?
if (str)
{
m_Cur += Write(m_Cur, str, std::strlen(str));
}
}
} // Namespace:: SqMod

1026
module/Base/Buffer.hpp Normal file

File diff suppressed because it is too large Load Diff

635
module/Base/Circle.cpp Normal file
View File

@ -0,0 +1,635 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Circle.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Circle"))
// ------------------------------------------------------------------------------------------------
const Circle Circle::NIL = Circle();
const Circle Circle::MIN = Circle(0.0);
const Circle Circle::MAX = Circle(std::numeric_limits< Circle::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Circle::Delim = ',';
// ------------------------------------------------------------------------------------------------
Circle::Circle()
: pos(0.0, 0.0), rad(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Circle::Circle(Value rv)
: pos(0.0, 0.0), rad(rv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Circle::Circle(const Vector2 & pv, Value rv)
: pos(pv), rad(rv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Circle::Circle(Value xv, Value yv, Value rv)
: pos(xv, yv), rad(rv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator = (Value r)
{
rad = r;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator = (const Vector2 & p)
{
pos = p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator += (const Circle & c)
{
pos += c.pos;
rad += c.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator -= (const Circle & c)
{
pos -= c.pos;
rad -= c.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator *= (const Circle & c)
{
pos *= c.pos;
rad *= c.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator /= (const Circle & c)
{
pos /= c.pos;
rad /= c.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator %= (const Circle & c)
{
pos %= c.pos;
rad = std::fmod(rad, c.rad);
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator += (Value r)
{
rad += r;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator -= (Value r)
{
rad -= r;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator *= (Value r)
{
rad *= r;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator /= (Value r)
{
rad /= r;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator %= (Value r)
{
rad = std::fmod(rad, r);
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator += (const Vector2 & p)
{
pos += p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator -= (const Vector2 & p)
{
pos -= p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator *= (const Vector2 & p)
{
pos *= p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator /= (const Vector2 & p)
{
pos /= p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator %= (const Vector2 & p)
{
pos %= p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator ++ ()
{
++pos;
++rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle & Circle::operator -- ()
{
--pos;
--rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator ++ (int)
{
Circle state(*this);
++pos;
++rad;
return state;
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator -- (int)
{
Circle state(*this);
--pos;
--rad;
return state;
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator + (const Circle & c) const
{
return Circle(pos + c.pos, rad + c.rad);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator - (const Circle & c) const
{
return Circle(pos - c.pos, rad - c.rad);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator * (const Circle & c) const
{
return Circle(pos * c.pos, rad * c.rad);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator / (const Circle & c) const
{
return Circle(pos / c.pos, rad / c.rad);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator % (const Circle & c) const
{
return Circle(pos % c.pos, std::fmod(rad, c.rad));
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator + (Value r) const
{
return Circle(rad + r);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator - (Value r) const
{
return Circle(rad - r);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator * (Value r) const
{
return Circle(rad * r);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator / (Value r) const
{
return Circle(rad / r);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator % (Value r) const
{
return Circle(std::fmod(rad, r));
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator + (const Vector2 & p) const
{
return Circle(pos + p, rad);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator - (const Vector2 & p) const
{
return Circle(pos - p, rad);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator * (const Vector2 & p) const
{
return Circle(pos * p, rad);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator / (const Vector2 & p) const
{
return Circle(pos / p, rad);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator % (const Vector2 & p) const
{
return Circle(pos % p, rad);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator + () const
{
return Circle(pos.Abs(), std::fabs(rad));
}
// ------------------------------------------------------------------------------------------------
Circle Circle::operator - () const
{
return Circle(-pos, -rad);
}
// ------------------------------------------------------------------------------------------------
bool Circle::operator == (const Circle & c) const
{
return EpsEq(rad, c.rad) && (pos == c.pos);
}
// ------------------------------------------------------------------------------------------------
bool Circle::operator != (const Circle & c) const
{
return !EpsEq(rad, c.rad) || (pos != c.pos);
}
// ------------------------------------------------------------------------------------------------
bool Circle::operator < (const Circle & c) const
{
return EpsLt(rad, c.rad) && (pos < c.pos);
}
// ------------------------------------------------------------------------------------------------
bool Circle::operator > (const Circle & c) const
{
return EpsGt(rad, c.rad) && (pos > c.pos);
}
// ------------------------------------------------------------------------------------------------
bool Circle::operator <= (const Circle & c) const
{
return EpsLtEq(rad, c.rad) && (pos <= c.pos);
}
// ------------------------------------------------------------------------------------------------
bool Circle::operator >= (const Circle & c) const
{
return EpsGtEq(rad, c.rad) && (pos >= c.pos);
}
// ------------------------------------------------------------------------------------------------
Int32 Circle::Cmp(const Circle & o) const
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
CSStr Circle::ToString() const
{
return ToStrF("%f,%f,%f", pos.x, pos.y, rad);
}
// ------------------------------------------------------------------------------------------------
void Circle::SetRadius(Value nr)
{
rad = nr;
}
// ------------------------------------------------------------------------------------------------
void Circle::SetCircle(const Circle & nc)
{
pos = nc.pos;
rad = nc.rad;
}
// ------------------------------------------------------------------------------------------------
void Circle::SetCircleEx(Value nx, Value ny, Value nr)
{
pos.SetVector2Ex(nx, ny);
rad = nr;
}
// ------------------------------------------------------------------------------------------------
void Circle::SetValues(const Vector2 & np, Value nr)
{
pos = np;
rad = nr;
}
// ------------------------------------------------------------------------------------------------
void Circle::SetPosition(const Vector2 & np)
{
pos = np;
}
// ------------------------------------------------------------------------------------------------
void Circle::SetPositionEx(Value nx, Value ny)
{
pos.SetVector2Ex(nx, ny);
}
// ------------------------------------------------------------------------------------------------
void Circle::SetStr(SQChar delim, StackStrF & values)
{
SetCircle(Circle::GetEx(delim, values));
}
// ------------------------------------------------------------------------------------------------
void Circle::Generate()
{
pos.Generate();
rad = GetRandomFloat32();
}
// ------------------------------------------------------------------------------------------------
void Circle::Generate(Value min, Value max, bool r)
{
if (EpsLt(max, min))
{
STHROWF("max value is lower than min value");
}
else if (r)
{
rad = GetRandomFloat32(min, max);
}
else
{
pos.Generate(min, max);
}
}
// ------------------------------------------------------------------------------------------------
void Circle::Generate(Value xmin, Value xmax, Value ymin, Value ymax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin))
{
STHROWF("max value is lower than min value");
}
pos.Generate(xmin, xmax, ymin, ymax);
}
// ------------------------------------------------------------------------------------------------
void Circle::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value rmin, Value rmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(rmax, rmin))
{
STHROWF("max value is lower than min value");
}
pos.Generate(xmin, xmax, ymin, ymax);
rad = GetRandomFloat32(rmin, rmax);
}
// ------------------------------------------------------------------------------------------------
Circle Circle::Abs() const
{
return Circle(pos.Abs(), std::fabs(rad));
}
// ------------------------------------------------------------------------------------------------
const Circle & Circle::Get(StackStrF & str)
{
return Circle::GetEx(Circle::Delim, str);
}
// ------------------------------------------------------------------------------------------------
const Circle & Circle::GetEx(SQChar delim, StackStrF & str)
{
// The format specifications that will be used to scan the string
static SQChar fs[] = _SC(" %f , %f , %f ");
static Circle circle;
// Clear previous values, if any
circle.Clear();
// Is the specified string empty?
if (str.mLen <= 0)
{
return circle; // Return the value as is!
}
// Assign the specified delimiter
fs[4] = delim;
fs[9] = delim;
// Attempt to extract the component values from the specified string
std::sscanf(str.mPtr, fs, &circle.pos.x, &circle.pos.y, &circle.rad);
// Return the resulted value
return circle;
}
// ------------------------------------------------------------------------------------------------
const Circle & GetCircle()
{
static Circle circle;
circle.Clear();
return circle;
}
// ------------------------------------------------------------------------------------------------
const Circle & GetCircle(Float32 rv)
{
static Circle circle;
circle.SetRadius(rv);
return circle;
}
// ------------------------------------------------------------------------------------------------
const Circle & GetCircle(const Vector2 & pv, Float32 rv)
{
static Circle circle;
circle.SetValues(pv, rv);
return circle;
}
// ------------------------------------------------------------------------------------------------
const Circle & GetCircle(Float32 xv, Float32 yv, Float32 rv)
{
static Circle circle;
circle.SetCircleEx(xv, yv, rv);
return circle;
}
// ------------------------------------------------------------------------------------------------
const Circle & GetCircle(const Circle & o)
{
static Circle circle;
circle.SetCircle(o);
return circle;
}
// ================================================================================================
void Register_Circle(HSQUIRRELVM vm)
{
typedef Circle::Value Val;
RootTable(vm).Bind(Typename::Str,
Class< Circle >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< Val >()
.Ctor< const Vector2 &, Val >()
.Ctor< Val, Val, Val >()
// Member Variables
.Var(_SC("pos"), &Circle::pos)
.Var(_SC("rad"), &Circle::rad)
.Var(_SC("Pos"), &Circle::pos)
.Var(_SC("Rad"), &Circle::rad)
// Core Meta-methods
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Circle::ToString)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< Circle >, SQFloat, SQInteger, bool, std::nullptr_t, Circle >)
.Func< Circle (Circle::*)(void) const >(_SC("_unm"), &Circle::operator -)
// Properties
.Prop(_SC("Abs"), &Circle::Abs)
// Member Methods
.Func(_SC("SetRadius"), &Circle::SetRadius)
.Func(_SC("SetCircle"), &Circle::SetCircle)
.Func(_SC("SetCircleEx"), &Circle::SetCircleEx)
.Func(_SC("SetValues"), &Circle::SetValues)
.Func(_SC("SetPos"), &Circle::SetPosition)
.Func(_SC("SetPosition"), &Circle::SetPosition)
.Func(_SC("SetPosEx"), &Circle::SetPositionEx)
.Func(_SC("SetPositionEx"), &Circle::SetPositionEx)
.FmtFunc(_SC("SetStr"), &Circle::SetStr)
.Func(_SC("Clear"), &Circle::Clear)
// Member Overloads
.Overload< void (Circle::*)(void) >(_SC("Generate"), &Circle::Generate)
.Overload< void (Circle::*)(Val, Val, bool) >(_SC("Generate"), &Circle::Generate)
.Overload< void (Circle::*)(Val, Val, Val, Val) >(_SC("Generate"), &Circle::Generate)
.Overload< void (Circle::*)(Val, Val, Val, Val, Val, Val) >(_SC("Generate"), &Circle::Generate)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Circle >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Circle >)
.StaticFmtFunc(_SC("FromStr"), &Circle::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Circle::GetEx)
// Operator Exposure
.Func< Circle & (Circle::*)(const Circle &) >(_SC("opAddAssign"), &Circle::operator +=)
.Func< Circle & (Circle::*)(const Circle &) >(_SC("opSubAssign"), &Circle::operator -=)
.Func< Circle & (Circle::*)(const Circle &) >(_SC("opMulAssign"), &Circle::operator *=)
.Func< Circle & (Circle::*)(const Circle &) >(_SC("opDivAssign"), &Circle::operator /=)
.Func< Circle & (Circle::*)(const Circle &) >(_SC("opModAssign"), &Circle::operator %=)
.Func< Circle & (Circle::*)(Circle::Value) >(_SC("opAddAssignR"), &Circle::operator +=)
.Func< Circle & (Circle::*)(Circle::Value) >(_SC("opSubAssignR"), &Circle::operator -=)
.Func< Circle & (Circle::*)(Circle::Value) >(_SC("opMulAssignR"), &Circle::operator *=)
.Func< Circle & (Circle::*)(Circle::Value) >(_SC("opDivAssignR"), &Circle::operator /=)
.Func< Circle & (Circle::*)(Circle::Value) >(_SC("opModAssignR"), &Circle::operator %=)
.Func< Circle & (Circle::*)(const Vector2 &) >(_SC("opAddAssignP"), &Circle::operator +=)
.Func< Circle & (Circle::*)(const Vector2 &) >(_SC("opSubAssignP"), &Circle::operator -=)
.Func< Circle & (Circle::*)(const Vector2 &) >(_SC("opMulAssignP"), &Circle::operator *=)
.Func< Circle & (Circle::*)(const Vector2 &) >(_SC("opDivAssignP"), &Circle::operator /=)
.Func< Circle & (Circle::*)(const Vector2 &) >(_SC("opModAssignP"), &Circle::operator %=)
.Func< Circle & (Circle::*)(void) >(_SC("opPreInc"), &Circle::operator ++)
.Func< Circle & (Circle::*)(void) >(_SC("opPreDec"), &Circle::operator --)
.Func< Circle (Circle::*)(int) >(_SC("opPostInc"), &Circle::operator ++)
.Func< Circle (Circle::*)(int) >(_SC("opPostDec"), &Circle::operator --)
.Func< Circle (Circle::*)(const Circle &) const >(_SC("opAdd"), &Circle::operator +)
.Func< Circle (Circle::*)(const Circle &) const >(_SC("opSub"), &Circle::operator -)
.Func< Circle (Circle::*)(const Circle &) const >(_SC("opMul"), &Circle::operator *)
.Func< Circle (Circle::*)(const Circle &) const >(_SC("opDiv"), &Circle::operator /)
.Func< Circle (Circle::*)(const Circle &) const >(_SC("opMod"), &Circle::operator %)
.Func< Circle (Circle::*)(Circle::Value) const >(_SC("opAddR"), &Circle::operator +)
.Func< Circle (Circle::*)(Circle::Value) const >(_SC("opSubR"), &Circle::operator -)
.Func< Circle (Circle::*)(Circle::Value) const >(_SC("opMulR"), &Circle::operator *)
.Func< Circle (Circle::*)(Circle::Value) const >(_SC("opDivR"), &Circle::operator /)
.Func< Circle (Circle::*)(Circle::Value) const >(_SC("opModR"), &Circle::operator %)
.Func< Circle (Circle::*)(const Vector2 &) const >(_SC("opAddP"), &Circle::operator +)
.Func< Circle (Circle::*)(const Vector2 &) const >(_SC("opSubP"), &Circle::operator -)
.Func< Circle (Circle::*)(const Vector2 &) const >(_SC("opMulP"), &Circle::operator *)
.Func< Circle (Circle::*)(const Vector2 &) const >(_SC("opDivP"), &Circle::operator /)
.Func< Circle (Circle::*)(const Vector2 &) const >(_SC("opModP"), &Circle::operator %)
.Func< Circle (Circle::*)(void) const >(_SC("opUnPlus"), &Circle::operator +)
.Func< Circle (Circle::*)(void) const >(_SC("opUnMinus"), &Circle::operator -)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opEqual"), &Circle::operator ==)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opNotEqual"), &Circle::operator !=)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opLessThan"), &Circle::operator <)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opGreaterThan"), &Circle::operator >)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opLessEqual"), &Circle::operator <=)
.Func< bool (Circle::*)(const Circle &) const >(_SC("opGreaterEqual"), &Circle::operator >=)
);
}
} // Namespace:: SqMod

428
module/Base/Circle.hpp Normal file
View File

@ -0,0 +1,428 @@
#ifndef _BASE_CIRCLE_HPP_
#define _BASE_CIRCLE_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Vector2.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Class used to represent a two-dimensional circle.
*/
struct Circle
{
/* --------------------------------------------------------------------------------------------
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* Helper instances for common values mostly used as return types or comparison.
*/
static const Circle NIL;
static const Circle MIN;
static const Circle MAX;
/* --------------------------------------------------------------------------------------------
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* The position and radius components of this type.
*/
Vector2 pos;
Value rad;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Circle();
/* --------------------------------------------------------------------------------------------
* Construct a circle at position 0,0 using the specified radius.
*/
explicit Circle(Value rv);
/* --------------------------------------------------------------------------------------------
* Construct a circle at the specified position using the specified radius.
*/
Circle(const Vector2 & pv, Value rv);
/* --------------------------------------------------------------------------------------------
* Construct a circle at the specified position using the specified radius.
*/
Circle(Value xv, Value yv, Value rv);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Circle(const Circle & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Circle(Circle && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Circle() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Circle & operator = (const Circle & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Circle & operator = (Circle && o) = default;
/* --------------------------------------------------------------------------------------------
* Radius assignment operator.
*/
Circle & operator = (Value r);
/* --------------------------------------------------------------------------------------------
* Position assignment operator.
*/
Circle & operator = (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* Addition assignment operator.
*/
Circle & operator += (const Circle & c);
/* --------------------------------------------------------------------------------------------
* Subtraction assignment operator.
*/
Circle & operator -= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* Multiplication assignment operator.
*/
Circle & operator *= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* Division assignment operator.
*/
Circle & operator /= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* Modulo assignment operator.
*/
Circle & operator %= (const Circle & c);
/* --------------------------------------------------------------------------------------------
* Radius addition assignment operator.
*/
Circle & operator += (Value r);
/* --------------------------------------------------------------------------------------------
* Radius subtraction assignment operator.
*/
Circle & operator -= (Value r);
/* --------------------------------------------------------------------------------------------
* Radius multiplication assignment operator.
*/
Circle & operator *= (Value r);
/* --------------------------------------------------------------------------------------------
* Radius division assignment operator.
*/
Circle & operator /= (Value r);
/* --------------------------------------------------------------------------------------------
* Radius modulo assignment operator.
*/
Circle & operator %= (Value r);
/* --------------------------------------------------------------------------------------------
* Position addition assignment operator.
*/
Circle & operator += (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* Position subtraction assignment operator.
*/
Circle & operator -= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* Position multiplication assignment operator.
*/
Circle & operator *= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* Position division assignment operator.
*/
Circle & operator /= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* Position modulo assignment operator.
*/
Circle & operator %= (const Vector2 & p);
/* --------------------------------------------------------------------------------------------
* Pre-increment operator.
*/
Circle & operator ++ ();
/* --------------------------------------------------------------------------------------------
* Pre-decrement operator.
*/
Circle & operator -- ();
/* --------------------------------------------------------------------------------------------
* Post-increment operator.
*/
Circle operator ++ (int);
/* --------------------------------------------------------------------------------------------
* Post-decrement operator.
*/
Circle operator -- (int);
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
Circle operator + (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
Circle operator - (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
Circle operator * (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
Circle operator / (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Modulo operator.
*/
Circle operator % (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Radius addition operator.
*/
Circle operator + (Value r) const;
/* --------------------------------------------------------------------------------------------
* Radius subtraction operator.
*/
Circle operator - (Value r) const;
/* --------------------------------------------------------------------------------------------
* Radius multiplication operator.
*/
Circle operator * (Value r) const;
/* --------------------------------------------------------------------------------------------
* Radius division operator.
*/
Circle operator / (Value r) const;
/* --------------------------------------------------------------------------------------------
* Radius modulo operator.
*/
Circle operator % (Value r) const;
/* --------------------------------------------------------------------------------------------
* Position addition operator.
*/
Circle operator + (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* Position subtraction operator.
*/
Circle operator - (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* Position multiplication operator.
*/
Circle operator * (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* Position division operator.
*/
Circle operator / (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* Position modulo operator.
*/
Circle operator % (const Vector2 & p) const;
/* --------------------------------------------------------------------------------------------
* Unary plus operator.
*/
Circle operator + () const;
/* --------------------------------------------------------------------------------------------
* Unary minus operator.
*/
Circle operator - () const;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Circle & c) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
{
return Cmp(Circle(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
{
return Cmp(Circle(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
{
return Cmp(Circle(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
{
return Cmp(Circle(static_cast< Value >(0)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Set the specified radius.
*/
void SetRadius(Value nr);
/* --------------------------------------------------------------------------------------------
* Copy the circle from another instance of this type.
*/
void SetCircle(const Circle & nc);
/* --------------------------------------------------------------------------------------------
* Set the specified position and radius.
*/
void SetCircleEx(Value nx, Value ny, Value nr);
/* --------------------------------------------------------------------------------------------
* Set the specified position and radius.
*/
void SetValues(const Vector2 & np, Value nr);
/* --------------------------------------------------------------------------------------------
* Set the specified position.
*/
void SetPosition(const Vector2 & np);
/* --------------------------------------------------------------------------------------------
* Set the specified position.
*/
void SetPositionEx(Value nx, Value ny);
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/
void SetStr(SQChar delim, StackStrF & values);
/* --------------------------------------------------------------------------------------------
* Generate a randomly sized and positioned circle.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* Generate a randomly sized or positioned circle within the specified bounds.
*/
void Generate(Value min, Value max, bool r);
/* --------------------------------------------------------------------------------------------
* Generate a randomly positioned circle within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax);
/* --------------------------------------------------------------------------------------------
* Generate a randomly sized and positioned circle within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value rmin, Value rmax);
/* --------------------------------------------------------------------------------------------
* Clear the component values to default.
*/
void Clear()
{
pos.Clear();
rad = 0.0;
}
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Circle Abs() const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Circle type from a string.
*/
static const Circle & Get(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Circle type from a string.
*/
static const Circle & GetEx(SQChar delim, StackStrF & str);
};
} // Namespace:: SqMod
#endif // _BASE_CIRCLE_HPP_

814
module/Base/Color3.cpp Normal file
View File

@ -0,0 +1,814 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Color3.hpp"
#include "Base/Color4.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Color3"))
// ------------------------------------------------------------------------------------------------
const Color3 Color3::NIL = Color3();
const Color3 Color3::MIN = Color3(std::numeric_limits< Color3::Value >::min());
const Color3 Color3::MAX = Color3(std::numeric_limits< Color3::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Color3::Delim = ',';
// ------------------------------------------------------------------------------------------------
Color3::Color3()
: r(0), g(0), b(0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3::Color3(Value sv)
: r(sv), g(sv), b(sv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3::Color3(Value rv, Value gv, Value bv)
: r(rv), g(gv), b(bv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3::Color3(Value rv, Value gv, Value bv, Value /*av*/)
: r(rv), g(gv), b(bv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator = (Value s)
{
r = s;
g = s;
b = s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator = (const Color4 & c)
{
r = c.r;
g = c.g;
b = c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator += (const Color3 & c)
{
r += c.r;
g += c.g;
b += c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator -= (const Color3 & c)
{
r -= c.r;
g -= c.g;
b -= c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator *= (const Color3 & c)
{
r *= c.r;
g *= c.g;
b *= c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator /= (const Color3 & c)
{
r /= c.r;
g /= c.g;
b /= c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator %= (const Color3 & c)
{
r %= c.r;
g %= c.g;
b %= c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator &= (const Color3 & c)
{
r &= c.r;
g &= c.g;
b &= c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator |= (const Color3 & c)
{
r |= c.r;
g |= c.g;
b |= c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator ^= (const Color3 & c)
{
r ^= c.r;
g ^= c.g;
b ^= c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator <<= (const Color3 & c)
{
r <<= c.r;
g <<= c.g;
b <<= c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator >>= (const Color3 & c)
{
r >>= c.r;
g >>= c.g;
b >>= c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator += (Value s)
{
r += s;
g += s;
b += s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator -= (Value s)
{
r -= s;
g -= s;
b -= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator *= (Value s)
{
r *= s;
g *= s;
b *= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator /= (Value s)
{
r /= s;
g /= s;
b /= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator %= (Value s)
{
r %= s;
g %= s;
b %= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator &= (Value s)
{
r &= s;
g &= s;
b &= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator |= (Value s)
{
r |= s;
g |= s;
b |= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator ^= (Value s)
{
r ^= s;
g ^= s;
b ^= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator <<= (Value s)
{
r <<= s;
g <<= s;
b <<= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator >>= (Value s)
{
r >>= s;
g >>= s;
b >>= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator ++ ()
{
++r;
++g;
++b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 & Color3::operator -- ()
{
--r;
--g;
--b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator ++ (int)
{
Color3 state(*this);
++r;
++g;
++b;
return state;
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator -- (int)
{
Color3 state(*this);
--r;
--g;
--b;
return state;
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator + (const Color3 & c) const
{
return Color3(r + c.r, g + c.g, b + c.b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator - (const Color3 & c) const
{
return Color3(r - c.r, g - c.g, b - c.b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator * (const Color3 & c) const
{
return Color3(r * c.r, g * c.g, b * c.b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator / (const Color3 & c) const
{
return Color3(r / c.r, g / c.g, b / c.b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator % (const Color3 & c) const
{
return Color3(r % c.r, g % c.g, b % c.b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator & (const Color3 & c) const
{
return Color3(r & c.r, g & c.g, b & c.b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator | (const Color3 & c) const
{
return Color3(r | c.r, g | c.g, b | c.b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator ^ (const Color3 & c) const
{
return Color3(r ^ c.r, g ^ c.g, b ^ c.b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator << (const Color3 & c) const
{
return Color3(r << c.r, g << c.g, b << c.b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator >> (const Color3 & c) const
{
return Color3(r >> c.r, g >> c.g, b >> c.b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator + (Value s) const
{
return Color3(r + s, g + s, b + s);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator - (Value s) const
{
return Color3(r - s, g - s, b - s);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator * (Value s) const
{
return Color3(r * s, g * s, b * s);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator / (Value s) const
{
return Color3(r / s, g / s, b / s);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator % (Value s) const
{
return Color3(r % s, g % s, b % s);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator & (Value s) const
{
return Color3(r & s, g & s, b & s);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator | (Value s) const
{
return Color3(r | s, g | s, b | s);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator ^ (Value s) const
{
return Color3(r ^ s, g ^ s, b ^ s);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator << (Value s) const
{
return Color3(r << s, g << s, b << s);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator >> (Value s) const
{
return Color3(r >> s, g >> s, b >> s);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator + () const
{
return Color3(r, g, b);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator - () const
{
return Color3(0, 0, 0);
}
// ------------------------------------------------------------------------------------------------
Color3 Color3::operator ~ () const
{
return Color3(~r, ~g, ~b);
}
// ------------------------------------------------------------------------------------------------
bool Color3::operator == (const Color3 & c) const
{
return (r == c.r) && (g == c.g) && (b == c.b);
}
// ------------------------------------------------------------------------------------------------
bool Color3::operator != (const Color3 & c) const
{
return (r != c.r) || (g != c.g) || (b != c.b);
}
// ------------------------------------------------------------------------------------------------
bool Color3::operator < (const Color3 & c) const
{
return (r < c.r) && (g < c.g) && (b < c.b);
}
// ------------------------------------------------------------------------------------------------
bool Color3::operator > (const Color3 & c) const
{
return (r > c.r) && (g > c.g) && (b > c.b);
}
// ------------------------------------------------------------------------------------------------
bool Color3::operator <= (const Color3 & c) const
{
return (r <= c.r) && (g <= c.g) && (b <= c.b);
}
// ------------------------------------------------------------------------------------------------
bool Color3::operator >= (const Color3 & c) const
{
return (r >= c.r) && (g >= c.g) && (b >= c.b);
}
// ------------------------------------------------------------------------------------------------
Color3::operator Color4 () const
{
return Color4(r, g, b);
}
// ------------------------------------------------------------------------------------------------
Int32 Color3::Cmp(const Color3 & o) const
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
CSStr Color3::ToString() const
{
return ToStrF("%u,%u,%u", r, g, b);
}
// ------------------------------------------------------------------------------------------------
void Color3::SetScalar(Value ns)
{
r = ns;
g = ns;
b = ns;
}
// ------------------------------------------------------------------------------------------------
void Color3::SetColor3(const Color3 & c)
{
r = c.r;
g = c.g;
b = c.b;
}
// ------------------------------------------------------------------------------------------------
void Color3::SetColor3Ex(Value nr, Value ng, Value nb)
{
r = nr;
g = ng;
b = nb;
}
// ------------------------------------------------------------------------------------------------
void Color3::SetColor4(const Color4 & c)
{
r = c.r;
g = c.g;
b = c.b;
}
// ------------------------------------------------------------------------------------------------
void Color3::SetColor4Ex(Value nr, Value ng, Value nb, Value /*na*/)
{
r = nr;
g = ng;
b = nb;
}
// ------------------------------------------------------------------------------------------------
void Color3::SetStr(SQChar delim, StackStrF & values)
{
SetColor3(Color3::GetEx(delim, values));
}
// ------------------------------------------------------------------------------------------------
void Color3::SetName(StackStrF & name)
{
SetColor3(GetColor(name));
}
// ------------------------------------------------------------------------------------------------
Uint32 Color3::GetRGB() const
{
return Uint32(r << 16 | g << 8 | b);
}
// ------------------------------------------------------------------------------------------------
void Color3::SetRGB(Uint32 p)
{
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
Uint32 Color3::GetRGBA() const
{
return Uint32(r << 24 | g << 16 | b << 8 | 0x00);
}
// ------------------------------------------------------------------------------------------------
void Color3::SetRGBA(Uint32 p)
{
r = static_cast< Value >((p >> 24) & 0xFF);
g = static_cast< Value >((p >> 16) & 0xFF);
b = static_cast< Value >((p >> 8) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
Uint32 Color3::GetARGB() const
{
return Uint32(0x00 << 24 | r << 16 | g << 8 | b);
}
// ------------------------------------------------------------------------------------------------
void Color3::SetARGB(Uint32 p)
{
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
void Color3::Generate()
{
r = GetRandomUint8();
g = GetRandomUint8();
b = GetRandomUint8();
}
// ------------------------------------------------------------------------------------------------
void Color3::Generate(Value min, Value max)
{
if (max < min)
{
STHROWF("max value is lower than min value");
}
r = GetRandomUint8(min, max);
g = GetRandomUint8(min, max);
b = GetRandomUint8(min, max);
}
// ------------------------------------------------------------------------------------------------
void Color3::Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax)
{
if (rmax < rmin || gmax < gmin || bmax < bmin)
{
STHROWF("max value is lower than min value");
}
r = GetRandomUint8(rmin, rmax);
g = GetRandomUint8(gmin, gmax);
b = GetRandomUint8(bmin, bmax);
}
// ------------------------------------------------------------------------------------------------
void Color3::Random()
{
SetColor3(GetRandomColor());
}
// ------------------------------------------------------------------------------------------------
void Color3::Inverse()
{
r = static_cast< Value >(~r);
g = static_cast< Value >(~g);
b = static_cast< Value >(~b);
}
// ------------------------------------------------------------------------------------------------
const Color3 & Color3::Get(StackStrF & str)
{
return Color3::GetEx(Color3::Delim, str);
}
// ------------------------------------------------------------------------------------------------
const Color3 & Color3::GetEx(SQChar delim, StackStrF & str)
{
// The format specifications that will be used to scan the string
static SQChar fs[] = _SC(" %u , %u , %u ");
static Color3 col;
// The minimum and maximum values supported by the Color3 type
static const Uint32 min = std::numeric_limits< Color3::Value >::min();
static const Uint32 max = std::numeric_limits< Color3::Value >::max();
// Clear previous values, if any
col.Clear();
// Is the specified string empty?
if (str.mLen <= 0)
{
return col; // Return the value as is!
}
// Assign the specified delimiter
fs[4] = delim;
fs[9] = delim;
// The sscanf function requires at least 32 bit integers
Uint32 r = 0, g = 0, b = 0;
// Attempt to extract the component values from the specified string
std::sscanf(str.mPtr, fs, &r, &g, &b);
// Cast the extracted integers to the value used by the Color3 type
col.r = static_cast< Color3::Value >(Clamp(r, min, max));
col.g = static_cast< Color3::Value >(Clamp(g, min, max));
col.b = static_cast< Color3::Value >(Clamp(b, min, max));
// Return the resulted value
return col;
}
// ------------------------------------------------------------------------------------------------
const Color3 & GetColor3()
{
static Color3 col;
col.Clear();
return col;
}
// ------------------------------------------------------------------------------------------------
const Color3 & GetColor3(Uint8 sv)
{
static Color3 col;
col.SetScalar(sv);
return col;
}
// ------------------------------------------------------------------------------------------------
const Color3 & GetColor3(Uint8 rv, Uint8 gv, Uint8 bv)
{
static Color3 col;
col.SetColor3Ex(rv, gv, bv);
return col;
}
// ------------------------------------------------------------------------------------------------
const Color3 & GetColor3(const Color3 & o)
{
static Color3 col;
col.SetColor3(o);
return col;
}
// ================================================================================================
void Register_Color3(HSQUIRRELVM vm)
{
typedef Color3::Value Val;
RootTable(vm).Bind(Typename::Str,
Class< Color3 >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< Val >()
.Ctor< Val, Val, Val >()
.Ctor< Val, Val, Val, Val >()
// Member Variables
.Var(_SC("r"), &Color3::r)
.Var(_SC("g"), &Color3::g)
.Var(_SC("b"), &Color3::b)
.Var(_SC("R"), &Color3::r)
.Var(_SC("G"), &Color3::g)
.Var(_SC("B"), &Color3::b)
// Core Meta-methods
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Color3::ToString)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< Color3 >, SQFloat, SQInteger, bool, std::nullptr_t, Color3 >)
.Func< Color3 (Color3::*)(void) const >(_SC("_unm"), &Color3::operator -)
// Properties
.Prop(_SC("RGB"), &Color3::GetRGB, &Color3::SetRGB)
.Prop(_SC("RGBA"), &Color3::GetRGBA, &Color3::SetRGBA)
.Prop(_SC("ARGB"), &Color3::GetARGB, &Color3::SetARGB)
// Member Methods
.Func(_SC("SetScalar"), &Color3::SetScalar)
.Func(_SC("SetColor3"), &Color3::SetColor3)
.Func(_SC("SetColor3Ex"), &Color3::SetColor3Ex)
.Func(_SC("SetColor4"), &Color3::SetColor4)
.Func(_SC("SetColor4Ex"), &Color3::SetColor4Ex)
.FmtFunc(_SC("SetStr"), &Color3::SetStr)
.FmtFunc(_SC("SetName"), &Color3::SetName)
.Func(_SC("Clear"), &Color3::Clear)
.Func(_SC("Random"), &Color3::Random)
.Func(_SC("Inverse"), &Color3::Inverse)
// Member Overloads
.Overload< void (Color3::*)(void) >(_SC("Generate"), &Color3::Generate)
.Overload< void (Color3::*)(Val, Val) >(_SC("Generate"), &Color3::Generate)
.Overload< void (Color3::*)(Val, Val, Val, Val, Val, Val) >(_SC("Generate"), &Color3::Generate)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Color3 >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Color3 >)
.StaticFmtFunc(_SC("FromStr"), &Color3::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Color3::GetEx)
// Operator Exposure
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opAddAssign"), &Color3::operator +=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opSubAssign"), &Color3::operator -=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opMulAssign"), &Color3::operator *=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opDivAssign"), &Color3::operator /=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opModAssign"), &Color3::operator %=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opAndAssign"), &Color3::operator &=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opOrAssign"), &Color3::operator |=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opXorAssign"), &Color3::operator ^=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opShlAssign"), &Color3::operator <<=)
.Func< Color3 & (Color3::*)(const Color3 &) >(_SC("opShrAssign"), &Color3::operator >>=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opAddAssignS"), &Color3::operator +=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opSubAssignS"), &Color3::operator -=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opMulAssignS"), &Color3::operator *=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opDivAssignS"), &Color3::operator /=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opModAssignS"), &Color3::operator %=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opAndAssignS"), &Color3::operator &=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opOrAssignS"), &Color3::operator |=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opXorAssignS"), &Color3::operator ^=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opShlAssignS"), &Color3::operator <<=)
.Func< Color3 & (Color3::*)(Color3::Value) >(_SC("opShrAssignS"), &Color3::operator >>=)
.Func< Color3 & (Color3::*)(void) >(_SC("opPreInc"), &Color3::operator ++)
.Func< Color3 & (Color3::*)(void) >(_SC("opPreDec"), &Color3::operator --)
.Func< Color3 (Color3::*)(int) >(_SC("opPostInc"), &Color3::operator ++)
.Func< Color3 (Color3::*)(int) >(_SC("opPostDec"), &Color3::operator --)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opAdd"), &Color3::operator +)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opSub"), &Color3::operator -)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opMul"), &Color3::operator *)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opDiv"), &Color3::operator /)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opMod"), &Color3::operator %)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opAnd"), &Color3::operator &)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opOr"), &Color3::operator |)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opShl"), &Color3::operator ^)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opShl"), &Color3::operator <<)
.Func< Color3 (Color3::*)(const Color3 &) const >(_SC("opShr"), &Color3::operator >>)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opAddS"), &Color3::operator +)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opSubS"), &Color3::operator -)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opMulS"), &Color3::operator *)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opDivS"), &Color3::operator /)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opModS"), &Color3::operator %)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opAndS"), &Color3::operator &)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opOrS"), &Color3::operator |)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opShlS"), &Color3::operator ^)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opShlS"), &Color3::operator <<)
.Func< Color3 (Color3::*)(Color3::Value) const >(_SC("opShrS"), &Color3::operator >>)
.Func< Color3 (Color3::*)(void) const >(_SC("opUnPlus"), &Color3::operator +)
.Func< Color3 (Color3::*)(void) const >(_SC("opUnMinus"), &Color3::operator -)
.Func< Color3 (Color3::*)(void) const >(_SC("opCom"), &Color3::operator ~)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opEqual"), &Color3::operator ==)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opNotEqual"), &Color3::operator !=)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opLessThan"), &Color3::operator <)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opGreaterThan"), &Color3::operator >)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opLessEqual"), &Color3::operator <=)
.Func< bool (Color3::*)(const Color3 &) const >(_SC("opGreaterEqual"), &Color3::operator >=)
);
}
} // Namespace:: SqMod

515
module/Base/Color3.hpp Normal file
View File

@ -0,0 +1,515 @@
#ifndef _BASE_COLOR3_HPP_
#define _BASE_COLOR3_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Class used to represent an opaque RGB color.
*/
struct Color3
{
/* --------------------------------------------------------------------------------------------
* The type of value used by components of type.
*/
typedef unsigned char Value;
/* --------------------------------------------------------------------------------------------
* Helper instances for common values mostly used as return types or comparison.
*/
static const Color3 NIL;
static const Color3 MIN;
static const Color3 MAX;
/* --------------------------------------------------------------------------------------------
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* The red, green and blue components of this type.
*/
Value r, g, b;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Color3();
/* --------------------------------------------------------------------------------------------
* Construct a color with all components with the same specified color.
*/
explicit Color3(Value sv);
/* --------------------------------------------------------------------------------------------
* Construct with individually specified red, green and blue colors.
*/
Color3(Value rv, Value gv, Value bv);
/* --------------------------------------------------------------------------------------------
* Construct with individually specified red, green, blue and alpha colors.
*/
Color3(Value rv, Value gv, Value bv, Value av);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Color3(const Color3 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Color3(Color3 && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Color3() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Color3 & operator = (const Color3 & o) = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Color3 & operator = (Color3 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Color3 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* Transparent color assignment operator.
*/
Color3 & operator = (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Addition assignment operator.
*/
Color3 & operator += (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Subtraction assignment operator.
*/
Color3 & operator -= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Multiplication assignment operator.
*/
Color3 & operator *= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Division assignment operator.
*/
Color3 & operator /= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Modulo assignment operator.
*/
Color3 & operator %= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Bitwise AND assignment operator.
*/
Color3 & operator &= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Bitwise OR assignment operator.
*/
Color3 & operator |= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Bitwise XOR assignment operator.
*/
Color3 & operator ^= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Bitwise left shift assignment operator.
*/
Color3 & operator <<= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Bitwise right shift assignment operator.
*/
Color3 & operator >>= (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Scalar value addition assignment operator.
*/
Color3 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction assignment operator.
*/
Color3 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication assignment operator.
*/
Color3 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value division assignment operator.
*/
Color3 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value modulo assignment operator.
*/
Color3 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise AND assignment operator.
*/
Color3 & operator &= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise OR assignment operator.
*/
Color3 & operator |= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise XOR assignment operator.
*/
Color3 & operator ^= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise left shift assignment operator.
*/
Color3 & operator <<= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise right shift assignment operator.
*/
Color3 & operator >>= (Value s);
/* --------------------------------------------------------------------------------------------
* Pre-increment operator.
*/
Color3 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* Pre-decrement operator.
*/
Color3 & operator -- ();
/* --------------------------------------------------------------------------------------------
* Post-increment operator.
*/
Color3 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* Post-decrement operator.
*/
Color3 operator -- (int);
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
Color3 operator + (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
Color3 operator - (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
Color3 operator * (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
Color3 operator / (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Modulo operator.
*/
Color3 operator % (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Bitwise AND operator.
*/
Color3 operator & (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Bitwise OR operator.
*/
Color3 operator | (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Bitwise XOR operator.
*/
Color3 operator ^ (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Bitwise shift left operator.
*/
Color3 operator << (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Bitwise shift right operator.
*/
Color3 operator >> (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Scalar value addition operator.
*/
Color3 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction operator.
*/
Color3 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication operator.
*/
Color3 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value division operator.
*/
Color3 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value modulo operator.
*/
Color3 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise AND operator.
*/
Color3 operator & (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise OR operator.
*/
Color3 operator | (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise XOR operator.
*/
Color3 operator ^ (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise shift left operator.
*/
Color3 operator << (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise shift right operator.
*/
Color3 operator >> (Value s) const;
/* --------------------------------------------------------------------------------------------
* Unary plus operator.
*/
Color3 operator + () const;
/* --------------------------------------------------------------------------------------------
* Unary minus operator.
*/
Color3 operator - () const;
/* --------------------------------------------------------------------------------------------
* Bitwise NOT operator.
*/
Color3 operator ~ () const;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Implicit conversion to transparent color.
*/
operator Color4 () const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Color3 & c) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
{
return Cmp(Color3(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
{
return Cmp(Color3(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
{
return Cmp(Color3(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
{
return Cmp(Color3(static_cast< Value >(0)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void SetScalar(Value ns);
/* --------------------------------------------------------------------------------------------
* Copy the values from another instance of this type.
*/
void SetColor3(const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetColor3Ex(Value nr, Value ng, Value nb);
/* --------------------------------------------------------------------------------------------
* Copy the values from an opaque color.
*/
void SetColor4(const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetColor4Ex(Value nr, Value ng, Value nb, Value na);
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/
void SetStr(SQChar delim, StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Set the values from the identified color.
*/
void SetName(StackStrF & name);
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetRGB() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetRGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetRGBA() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetRGBA(Uint32 p);
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetARGB() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetARGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax);
/* --------------------------------------------------------------------------------------------
* Clear the component values to default.
*/
void Clear()
{
r = 0, g = 0, b = 0;
}
/* --------------------------------------------------------------------------------------------
* Set the component values to a randomly chosen color.
*/
void Random();
/* --------------------------------------------------------------------------------------------
* Inverse the color.
*/
void Inverse();
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Color3 type from a string.
*/
static const Color3 & Get(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Color3 type from a string.
*/
static const Color3 & GetEx( SQChar delim, StackStrF & str);
};
} // Namespace:: SqMod
#endif // _BASE_COLOR3_HPP_

861
module/Base/Color4.cpp Normal file
View File

@ -0,0 +1,861 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Color4.hpp"
#include "Base/Color3.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Color4"))
// ------------------------------------------------------------------------------------------------
const Color4 Color4::NIL = Color4();
const Color4 Color4::MIN = Color4(std::numeric_limits< Color4::Value >::min());
const Color4 Color4::MAX = Color4(std::numeric_limits< Color4::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Color4::Delim = ',';
// ------------------------------------------------------------------------------------------------
Color4::Color4()
: r(0), g(0), b(0), a(0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4::Color4(Value sv)
: r(sv), g(sv), b(sv), a(0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4::Color4(Value rv, Value gv, Value bv)
: r(rv), g(gv), b(bv), a(0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4::Color4(Value rv, Value gv, Value bv, Value av)
: r(rv), g(gv), b(bv), a(av)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator = (Value s)
{
r = s;
g = s;
b = s;
a = s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator = (const Color3 & c)
{
r = c.r;
g = c.g;
b = c.b;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator += (const Color4 & c)
{
r += c.r;
g += c.g;
b += c.b;
a += c.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator -= (const Color4 & c)
{
r -= c.r;
g -= c.g;
b -= c.b;
a -= c.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator *= (const Color4 & c)
{
r *= c.r;
g *= c.g;
b *= c.b;
a *= c.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator /= (const Color4 & c)
{
r /= c.r;
g /= c.g;
b /= c.b;
a /= c.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator %= (const Color4 & c)
{
r %= c.r;
g %= c.g;
b %= c.b;
a %= c.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator &= (const Color4 & c)
{
r &= c.r;
g &= c.g;
b &= c.b;
a &= c.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator |= (const Color4 & c)
{
r |= c.r;
g |= c.g;
b |= c.b;
a |= c.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator ^= (const Color4 & c)
{
r ^= c.r;
g ^= c.g;
b ^= c.b;
a ^= c.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator <<= (const Color4 & c)
{
r <<= c.r;
g <<= c.g;
b <<= c.b;
a <<= c.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator >>= (const Color4 & c)
{
r >>= c.r;
g >>= c.g;
b >>= c.b;
a >>= c.a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator += (Value s)
{
r += s;
g += s;
b += s;
a += s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator -= (Value s)
{
r -= s;
g -= s;
b -= s;
a -= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator *= (Value s)
{
r *= s;
g *= s;
b *= s;
a *= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator /= (Value s)
{
r /= s;
g /= s;
b /= s;
a /= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator %= (Value s)
{
r %= s;
g %= s;
b %= s;
a %= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator &= (Value s)
{
r &= s;
g &= s;
b &= s;
a &= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator |= (Value s)
{
r |= s;
g |= s;
b |= s;
a |= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator ^= (Value s)
{
r ^= s;
g ^= s;
b ^= s;
a ^= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator <<= (Value s)
{
r <<= s;
g <<= s;
b <<= s;
a <<= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator >>= (Value s)
{
r >>= s;
g >>= s;
b >>= s;
a >>= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator ++ ()
{
++r;
++g;
++b;
++a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 & Color4::operator -- ()
{
--r;
--g;
--b;
--a;
return *this;
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator ++ (int)
{
Color4 state(*this);
++r;
++g;
++b;
++a;
return state;
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator -- (int)
{
Color4 state(*this);
--r;
--g;
--b;
--a;
return state;
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator + (const Color4 & c) const
{
return Color4(r + c.r, g + c.g, b + c.b, a + c.a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator - (const Color4 & c) const
{
return Color4(r - c.r, g - c.g, b - c.b, a - c.a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator * (const Color4 & c) const
{
return Color4(r * c.r, g * c.g, b * c.b, a * c.a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator / (const Color4 & c) const
{
return Color4(r / c.r, g / c.g, b / c.b, a / c.a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator % (const Color4 & c) const
{
return Color4(r % c.r, g % c.g, b % c.b, a % c.a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator & (const Color4 & c) const
{
return Color4(r & c.r, g & c.g, b & c.b, a & c.a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator | (const Color4 & c) const
{
return Color4(r | c.r, g | c.g, b | c.b, a | c.a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator ^ (const Color4 & c) const
{
return Color4(r ^ c.r, g ^ c.g, b ^ c.b, a ^ c.a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator << (const Color4 & c) const
{
return Color4(r << c.r, g << c.g, b << c.b, a << c.a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator >> (const Color4 & c) const
{
return Color4(r >> c.r, g >> c.g, b >> c.b, a >> c.a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator + (Value s) const
{
return Color4(r + s, g + s, b + s, a + s);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator - (Value s) const
{
return Color4(r - s, g - s, b - s, a - s);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator * (Value s) const
{
return Color4(r * s, g * s, b * s, a * s);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator / (Value s) const
{
return Color4(r / s, g / s, b / s, a / s);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator % (Value s) const
{
return Color4(r % s, g % s, b % s, a % s);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator & (Value s) const
{
return Color4(r & s, g & s, b & s, a & s);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator | (Value s) const
{
return Color4(r | s, g | s, b | s, a | s);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator ^ (Value s) const
{
return Color4(r ^ s, g ^ s, b ^ s, a ^ s);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator << (Value s) const
{
return Color4(r << s, g << s, b << s, a << s);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator >> (Value s) const
{
return Color4(r >> s, g >> s, b >> s, a >> s);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator + () const
{
return Color4(r, g, b, a);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator - () const
{
return Color4(0, 0, 0, 0);
}
// ------------------------------------------------------------------------------------------------
Color4 Color4::operator ~ () const
{
return Color4(~r, ~g, ~b, ~a);
}
// ------------------------------------------------------------------------------------------------
bool Color4::operator == (const Color4 & c) const
{
return (r == c.r) && (g == c.g) && (b == c.b) && (a == c.a);
}
// ------------------------------------------------------------------------------------------------
bool Color4::operator != (const Color4 & c) const
{
return (r != c.r) || (g != c.g) || (b != c.b) || (a != c.a);
}
// ------------------------------------------------------------------------------------------------
bool Color4::operator < (const Color4 & c) const
{
return (r < c.r) && (g < c.g) && (b < c.b) && (a < c.a);
}
// ------------------------------------------------------------------------------------------------
bool Color4::operator > (const Color4 & c) const
{
return (r > c.r) && (g > c.g) && (b > c.b) && (a > c.a);
}
// ------------------------------------------------------------------------------------------------
bool Color4::operator <= (const Color4 & c) const
{
return (r <= c.r) && (g <= c.g) && (b <= c.b) && (a <= c.a);
}
// ------------------------------------------------------------------------------------------------
bool Color4::operator >= (const Color4 & c) const
{
return (r >= c.r) && (g >= c.g) && (b >= c.b) && (a >= c.a);
}
// ------------------------------------------------------------------------------------------------
Color4::operator Color3 () const
{
return Color3(r, g, b);
}
// ------------------------------------------------------------------------------------------------
Int32 Color4::Cmp(const Color4 & o) const
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
CSStr Color4::ToString() const
{
return ToStrF("%u,%u,%u,%u", r, g, b, a);
}
// ------------------------------------------------------------------------------------------------
void Color4::SetScalar(Value ns)
{
r = ns;
g = ns;
b = ns;
a = ns;
}
// ------------------------------------------------------------------------------------------------
void Color4::SetColor3(const Color3 & c)
{
r = c.r;
g = c.g;
b = c.b;
a = 0;
}
// ------------------------------------------------------------------------------------------------
void Color4::SetColor3Ex(Value nr, Value ng, Value nb)
{
r = nr;
g = ng;
b = nb;
}
// ------------------------------------------------------------------------------------------------
void Color4::SetColor4(const Color4 & c)
{
r = c.r;
g = c.g;
b = c.b;
a = c.a;
}
// ------------------------------------------------------------------------------------------------
void Color4::SetColor4Ex(Value nr, Value ng, Value nb, Value na)
{
r = nr;
g = ng;
b = nb;
a = na;
}
// ------------------------------------------------------------------------------------------------
void Color4::SetStr(SQChar delim, StackStrF & values)
{
SetColor4(Color4::GetEx(delim, values));
}
// ------------------------------------------------------------------------------------------------
void Color4::SetName(StackStrF & name)
{
SetColor3(GetColor(name));
}
// ------------------------------------------------------------------------------------------------
Uint32 Color4::GetRGB() const
{
return Uint32(r << 16 | g << 8 | b);
}
// ------------------------------------------------------------------------------------------------
void Color4::SetRGB(Uint32 p)
{
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
Uint32 Color4::GetRGBA() const
{
return Uint32(r << 24 | g << 16 | b << 8 | a);
}
// ------------------------------------------------------------------------------------------------
void Color4::SetRGBA(Uint32 p)
{
r = static_cast< Value >((p >> 24) & 0xFF);
g = static_cast< Value >((p >> 16) & 0xFF);
b = static_cast< Value >((p >> 8) & 0xFF);
a = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
Uint32 Color4::GetARGB() const
{
return Uint32(a << 24 | r << 16 | g << 8 | b);
}
// ------------------------------------------------------------------------------------------------
void Color4::SetARGB(Uint32 p)
{
a = static_cast< Value >((p >> 24) & 0xFF);
r = static_cast< Value >((p >> 16) & 0xFF);
g = static_cast< Value >((p >> 8) & 0xFF);
b = static_cast< Value >((p) & 0xFF);
}
// ------------------------------------------------------------------------------------------------
void Color4::Generate()
{
r = GetRandomUint8();
g = GetRandomUint8();
b = GetRandomUint8();
a = GetRandomUint8();
}
// ------------------------------------------------------------------------------------------------
void Color4::Generate(Value min, Value max)
{
if (max < min)
{
STHROWF("max value is lower than min value");
}
r = GetRandomUint8(min, max);
g = GetRandomUint8(min, max);
b = GetRandomUint8(min, max);
a = GetRandomUint8(min, max);
}
// ------------------------------------------------------------------------------------------------
void Color4::Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax, Value amin, Value amax)
{
if (rmax < rmin || gmax < gmin || bmax < bmin || amax < amin)
{
STHROWF("max value is lower than min value");
}
r = GetRandomUint8(rmin, rmax);
g = GetRandomUint8(gmin, gmax);
b = GetRandomUint8(bmin, bmax);
a = GetRandomUint8(bmin, bmax);
}
// ------------------------------------------------------------------------------------------------
void Color4::Random()
{
SetColor3(GetRandomColor());
}
// ------------------------------------------------------------------------------------------------
void Color4::Inverse()
{
r = static_cast< Value >(~r);
g = static_cast< Value >(~g);
b = static_cast< Value >(~b);
a = static_cast< Value >(~a);
}
// ------------------------------------------------------------------------------------------------
const Color4 & Color4::Get(StackStrF & str)
{
return Color4::GetEx(Color4::Delim, str);
}
// ------------------------------------------------------------------------------------------------
const Color4 & Color4::GetEx(SQChar delim, StackStrF & str)
{
// The format specifications that will be used to scan the string
static SQChar fs[] = _SC(" %u , %u , %u , %u ");
static Color4 col;
// The minimum and maximum values supported by the Color4 type
static const Uint32 min = std::numeric_limits< Color4::Value >::min();
static const Uint32 max = std::numeric_limits< Color4::Value >::max();
// Clear previous values, if any
col.Clear();
// Is the specified string empty?
if (str.mLen <= 0)
{
return col; // Return the value as is!
}
// Assign the specified delimiter
fs[4] = delim;
fs[9] = delim;
fs[14] = delim;
// The sscanf function requires at least 32 bit integers
Uint32 r = 0, g = 0, b = 0, a = 0;
// Attempt to extract the component values from the specified string
std::sscanf(str.mPtr, fs, &r, &g, &b, &a);
// Cast the extracted integers to the value used by the Color4 type
col.r = static_cast< Color4::Value >(Clamp(r, min, max));
col.g = static_cast< Color4::Value >(Clamp(g, min, max));
col.b = static_cast< Color4::Value >(Clamp(b, min, max));
col.a = static_cast< Color4::Value >(Clamp(a, min, max));
// Return the resulted value
return col;
}
// ------------------------------------------------------------------------------------------------
const Color4 & GetColor4()
{
static Color4 col;
col.Clear();
return col;
}
// ------------------------------------------------------------------------------------------------
const Color4 & GetColor4(Uint8 sv)
{
static Color4 col;
col.SetScalar(sv);
return col;
}
// ------------------------------------------------------------------------------------------------
const Color4 & GetColor4(Uint8 rv, Uint8 gv, Uint8 bv)
{
static Color4 col;
col.SetColor3Ex(rv, gv, bv);
return col;
}
// ------------------------------------------------------------------------------------------------
const Color4 & GetColor4(Uint8 rv, Uint8 gv, Uint8 bv, Uint8 av)
{
static Color4 col;
col.SetColor4Ex(rv, gv, bv, av);
return col;
}
// ------------------------------------------------------------------------------------------------
const Color4 & GetColor4(const Color4 & o)
{
static Color4 col;
col.SetColor4(o);
return col;
}
// ================================================================================================
void Register_Color4(HSQUIRRELVM vm)
{
typedef Color4::Value Val;
RootTable(vm).Bind(Typename::Str,
Class< Color4 >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< Val >()
.Ctor< Val, Val, Val >()
.Ctor< Val, Val, Val, Val >()
// Member Variables
.Var(_SC("r"), &Color4::r)
.Var(_SC("g"), &Color4::g)
.Var(_SC("b"), &Color4::b)
.Var(_SC("a"), &Color4::a)
.Var(_SC("R"), &Color4::r)
.Var(_SC("G"), &Color4::g)
.Var(_SC("B"), &Color4::b)
.Var(_SC("A"), &Color4::a)
// Core Meta-methods
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Color4::ToString)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< Color4 >, SQFloat, SQInteger, bool, std::nullptr_t, Color4 >)
.Func< Color4 (Color4::*)(void) const >(_SC("_unm"), &Color4::operator -)
// Properties
.Prop(_SC("RGB"), &Color4::GetRGB, &Color4::SetRGB)
.Prop(_SC("RGBA"), &Color4::GetRGBA, &Color4::SetRGBA)
.Prop(_SC("ARGB"), &Color4::GetARGB, &Color4::SetARGB)
// Member Methods
.Func(_SC("SetScalar"), &Color4::SetScalar)
.Func(_SC("SetColor3"), &Color4::SetColor3)
.Func(_SC("SetColor3Ex"), &Color4::SetColor3Ex)
.Func(_SC("SetColor4"), &Color4::SetColor4)
.Func(_SC("SetColor4Ex"), &Color4::SetColor4Ex)
.FmtFunc(_SC("SetStr"), &Color4::SetStr)
.FmtFunc(_SC("SetName"), &Color4::SetName)
.Func(_SC("Clear"), &Color4::Clear)
.Func(_SC("Random"), &Color4::Random)
.Func(_SC("Inverse"), &Color4::Inverse)
// Member Overloads
.Overload< void (Color4::*)(void) >(_SC("Generate"), &Color4::Generate)
.Overload< void (Color4::*)(Val, Val) >(_SC("Generate"), &Color4::Generate)
.Overload< void (Color4::*)(Val, Val, Val, Val, Val, Val, Val, Val) >(_SC("Generate"), &Color4::Generate)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Color4 >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Color4 >)
.StaticFmtFunc(_SC("FromStr"), &Color4::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Color4::GetEx)
// Operator Exposure
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opAddAssign"), &Color4::operator +=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opSubAssign"), &Color4::operator -=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opMulAssign"), &Color4::operator *=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opDivAssign"), &Color4::operator /=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opModAssign"), &Color4::operator %=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opAndAssign"), &Color4::operator &=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opOrAssign"), &Color4::operator |=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opXorAssign"), &Color4::operator ^=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opShlAssign"), &Color4::operator <<=)
.Func< Color4 & (Color4::*)(const Color4 &) >(_SC("opShrAssign"), &Color4::operator >>=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opAddAssignS"), &Color4::operator +=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opSubAssignS"), &Color4::operator -=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opMulAssignS"), &Color4::operator *=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opDivAssignS"), &Color4::operator /=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opModAssignS"), &Color4::operator %=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opAndAssignS"), &Color4::operator &=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opOrAssignS"), &Color4::operator |=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opXorAssignS"), &Color4::operator ^=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opShlAssignS"), &Color4::operator <<=)
.Func< Color4 & (Color4::*)(Color4::Value) >(_SC("opShrAssignS"), &Color4::operator >>=)
.Func< Color4 & (Color4::*)(void) >(_SC("opPreInc"), &Color4::operator ++)
.Func< Color4 & (Color4::*)(void) >(_SC("opPreDec"), &Color4::operator --)
.Func< Color4 (Color4::*)(int) >(_SC("opPostInc"), &Color4::operator ++)
.Func< Color4 (Color4::*)(int) >(_SC("opPostDec"), &Color4::operator --)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opAdd"), &Color4::operator +)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opSub"), &Color4::operator -)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opMul"), &Color4::operator *)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opDiv"), &Color4::operator /)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opMod"), &Color4::operator %)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opAnd"), &Color4::operator &)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opOr"), &Color4::operator |)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opShl"), &Color4::operator ^)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opShl"), &Color4::operator <<)
.Func< Color4 (Color4::*)(const Color4 &) const >(_SC("opShr"), &Color4::operator >>)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opAddS"), &Color4::operator +)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opSubS"), &Color4::operator -)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opMulS"), &Color4::operator *)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opDivS"), &Color4::operator /)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opModS"), &Color4::operator %)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opAndS"), &Color4::operator &)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opOrS"), &Color4::operator |)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opShlS"), &Color4::operator ^)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opShlS"), &Color4::operator <<)
.Func< Color4 (Color4::*)(Color4::Value) const >(_SC("opShrS"), &Color4::operator >>)
.Func< Color4 (Color4::*)(void) const >(_SC("opUnPlus"), &Color4::operator +)
.Func< Color4 (Color4::*)(void) const >(_SC("opUnMinus"), &Color4::operator -)
.Func< Color4 (Color4::*)(void) const >(_SC("opCom"), &Color4::operator ~)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opEqual"), &Color4::operator ==)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opNotEqual"), &Color4::operator !=)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opLessThan"), &Color4::operator <)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opGreaterThan"), &Color4::operator >)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opLessEqual"), &Color4::operator <=)
.Func< bool (Color4::*)(const Color4 &) const >(_SC("opGreaterEqual"), &Color4::operator >=)
);
}
} // Namespace:: SqMod

515
module/Base/Color4.hpp Normal file
View File

@ -0,0 +1,515 @@
#ifndef _BASE_COLOR4_HPP_
#define _BASE_COLOR4_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Class used to represent a transparent RGBA color.
*/
struct Color4
{
/* --------------------------------------------------------------------------------------------
* The type of value used by components of type.
*/
typedef unsigned char Value;
/* --------------------------------------------------------------------------------------------
* Helper instances for common values mostly used as return types or comparison.
*/
static const Color4 NIL;
static const Color4 MIN;
static const Color4 MAX;
/* --------------------------------------------------------------------------------------------
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* The red, green and blue components of this type.
*/
Value r, g, b, a;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Color4();
/* --------------------------------------------------------------------------------------------
* Construct a color with all components with the same specified color.
*/
explicit Color4(Value sv);
/* --------------------------------------------------------------------------------------------
* Construct with individually specified red, green and blue colors.
*/
Color4(Value rv, Value gv, Value bv);
/* --------------------------------------------------------------------------------------------
* Construct with individually specified red, green, blue and alpha colors.
*/
Color4(Value rv, Value gv, Value bv, Value av);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Color4(const Color4 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Color4(Color4 && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Color4() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Color4 & operator = (const Color4 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Color4 & operator = (Color4 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Color4 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* Opaque color assignment operator.
*/
Color4 & operator = (const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Addition assignment operator.
*/
Color4 & operator += (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Subtraction assignment operator.
*/
Color4 & operator -= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Multiplication assignment operator.
*/
Color4 & operator *= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Division assignment operator.
*/
Color4 & operator /= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Modulo assignment operator.
*/
Color4 & operator %= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Bitwise AND assignment operator.
*/
Color4 & operator &= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Bitwise OR assignment operator.
*/
Color4 & operator |= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Bitwise XOR assignment operator.
*/
Color4 & operator ^= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Bitwise left shift assignment operator.
*/
Color4 & operator <<= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Bitwise right shift assignment operator.
*/
Color4 & operator >>= (const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Scalar value addition assignment operator.
*/
Color4 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction assignment operator.
*/
Color4 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication assignment operator.
*/
Color4 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value division assignment operator.
*/
Color4 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value modulo assignment operator.
*/
Color4 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise AND assignment operator.
*/
Color4 & operator &= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise OR assignment operator.
*/
Color4 & operator |= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise XOR assignment operator.
*/
Color4 & operator ^= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise left shift assignment operator.
*/
Color4 & operator <<= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise right shift assignment operator.
*/
Color4 & operator >>= (Value s);
/* --------------------------------------------------------------------------------------------
* Pre-increment operator.
*/
Color4 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* Pre-decrement operator.
*/
Color4 & operator -- ();
/* --------------------------------------------------------------------------------------------
* Post-increment operator.
*/
Color4 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* Post-decrement operator.
*/
Color4 operator -- (int);
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
Color4 operator + (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
Color4 operator - (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
Color4 operator * (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
Color4 operator / (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Modulo operator.
*/
Color4 operator % (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Bitwise AND operator.
*/
Color4 operator & (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Bitwise OR operator.
*/
Color4 operator | (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Bitwise XOR operator.
*/
Color4 operator ^ (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Bitwise shift left operator.
*/
Color4 operator << (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Bitwise shift right operator.
*/
Color4 operator >> (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Scalar value addition operator.
*/
Color4 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction operator.
*/
Color4 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication operator.
*/
Color4 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value division operator.
*/
Color4 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value modulo operator.
*/
Color4 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise AND operator.
*/
Color4 operator & (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise OR operator.
*/
Color4 operator | (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise XOR operator.
*/
Color4 operator ^ (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise shift left operator.
*/
Color4 operator << (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise shift right operator.
*/
Color4 operator >> (Value s) const;
/* --------------------------------------------------------------------------------------------
* Unary plus operator.
*/
Color4 operator + () const;
/* --------------------------------------------------------------------------------------------
* Unary minus operator.
*/
Color4 operator - () const;
/* --------------------------------------------------------------------------------------------
* Bitwise NOT operator.
*/
Color4 operator ~ () const;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Implicit conversion to opaque color.
*/
operator Color3 () const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Color4 & c) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
{
return Cmp(Color4(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
{
return Cmp(Color4(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
{
return Cmp(Color4(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
{
return Cmp(Color4(static_cast< Value >(0)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void SetScalar(Value ns);
/* --------------------------------------------------------------------------------------------
* Copy the values from an opaque color.
*/
void SetColor3(const Color3 & c);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetColor3Ex(Value nr, Value ng, Value nb);
/* --------------------------------------------------------------------------------------------
* Copy the values from another instance of this type.
*/
void SetColor4(const Color4 & c);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetColor4Ex(Value nr, Value ng, Value nb, Value na);
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/
void SetStr(SQChar delim, StackStrF & name);
/* --------------------------------------------------------------------------------------------
* Set the values from the identified color.
*/
void SetName(StackStrF & name);
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetRGB() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetRGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetRGBA() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetRGBA(Uint32 p);
/* --------------------------------------------------------------------------------------------
* Get the component values packed inside an integer value.
*/
Uint32 GetARGB() const;
/* --------------------------------------------------------------------------------------------
* Set the component values wxtracted from an integer value.
*/
void SetARGB(Uint32 p);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value rmin, Value rmax, Value gmin, Value gmax, Value bmin, Value bmax, Value amin, Value amax);
/* --------------------------------------------------------------------------------------------
* Clear the component values to default.
*/
void Clear()
{
r = 0, g = 0, b = 0, a = 0;
}
/* --------------------------------------------------------------------------------------------
* Set the component values to a randomly chosen color.
*/
void Random();
/* --------------------------------------------------------------------------------------------
* Inverse the color.
*/
void Inverse();
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Color4 type from a string.
*/
static const Color4 & Get(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Color4 type from a string.
*/
static const Color4 & GetEx(SQChar delim, StackStrF & str);
};
} // Namespace:: SqMod
#endif // _BASE_COLOR4_HPP_

809
module/Base/DynArg.hpp Normal file
View File

@ -0,0 +1,809 @@
#ifndef _BASE_DYNARG_HPP_
#define _BASE_DYNARG_HPP_
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Helper class used to check if a certain operation is possible on the specified argument.
*/
template < typename U > struct SqDynArg
{
static inline bool CanPop(HSQUIRRELVM vm)
{
return (sq_gettype(vm, 2) == OT_INSTANCE);
}
template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
{
Var< U * > var(vm, 2);
// Validate the obtained instance
if (!var.value)
{
STHROWF("No such instance");
}
// Attempt to perform the requested operation
return fn(*var.value);
}
template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
{
Var< U * > var(vm, 2);
// Validate the obtained instance
if (!var.value)
{
STHROWF("No such instance");
}
// Attempt to perform the requested operation
return fn(*var.value);
}
};
/* ------------------------------------------------------------------------------------------------
* Instance pointer specializations of the argument checking structure.
*/
template < typename U > struct SqDynArg< U * >
{
static inline bool CanPop(HSQUIRRELVM vm)
{
return (sq_gettype(vm, 2) == OT_INSTANCE);
}
template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
{
Var< U * > var(vm, 2);
// Validate the obtained instance
if (!var.value)
{
STHROWF("No such instance");
}
// Attempt to perform the requested operation
return fn(var.value);
}
template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
{
Var< U * > var(vm, 2);
// Validate the obtained instance
if (!var.value)
{
STHROWF("No such instance");
}
// Attempt to perform the requested operation
return fn(var.value);
}
};
/* ------------------------------------------------------------------------------------------------
* Instance pointer specializations of the argument checking structure.
*/
template < typename U > struct SqDynArg< const U * >
{
static inline bool CanPop(HSQUIRRELVM vm)
{
return (sq_gettype(vm, 2) == OT_INSTANCE);
}
template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
{
Var< U * > var(vm, 2);
// Validate the obtained instance
if (!var.value)
{
STHROWF("No such instance");
}
// Attempt to perform the requested operation
return fn(*var.value);
}
template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
{
Var< U * > var(vm, 2);
// Validate the obtained instance
if (!var.value)
{
STHROWF("No such instance");
}
// Attempt to perform the requested operation
return fn(*var.value);
}
};
/* ------------------------------------------------------------------------------------------------
* Inherited by the base specializations of the argument checking structure.
*/
template < typename U, SQObjectType SqT > struct SqDynArgBase
{
static inline bool CanPop(HSQUIRRELVM vm)
{
return (sq_gettype(vm, 2) == SqT);
}
template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
{
Var< U > var(vm, 2);
// Attempt to perform the requested operation
return fn(var.value);
}
template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
{
Var< U > var(vm, 2);
// Attempt to perform the requested operation
return fn(var.value);
}
};
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< char >
: public SqDynArgBase< char, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< signed char >
: public SqDynArgBase< signed char, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< unsigned char >
: public SqDynArgBase< unsigned char, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< signed short >
: public SqDynArgBase< signed short, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< unsigned short >
: public SqDynArgBase< unsigned short, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< signed int >
: public SqDynArgBase< signed int, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< unsigned int >
: public SqDynArgBase< unsigned int, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< signed long >
: public SqDynArgBase< signed long, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< unsigned long >
: public SqDynArgBase< unsigned long, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< signed long long >
: public SqDynArgBase< signed long long, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< unsigned long long >
: public SqDynArgBase< unsigned long long, OT_INTEGER >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< float >
: public SqDynArgBase< float, OT_FLOAT >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< double >
: public SqDynArgBase< double, OT_FLOAT >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< bool >
: public SqDynArgBase< bool, OT_BOOL >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< SQChar * >
: public SqDynArgBase< SQChar *, OT_STRING >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< const SQChar * >
: public SqDynArgBase< const SQChar *, OT_STRING >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< String >
: public SqDynArgBase< String, OT_STRING >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< Table >
: public SqDynArgBase< Table, OT_TABLE >
{ /* ... */ };
// ------------------------------------------------------------------------------------------------
template < > struct SqDynArg< Array >
: public SqDynArgBase< Array, OT_ARRAY >
{ /* ... */ };
/* ------------------------------------------------------------------------------------------------
* Specialization of the argument checking structure for functions.
*/
template < > struct SqDynArg< Function >
{
static inline bool CanPop(HSQUIRRELVM vm)
{
const SQObjectType type = sq_gettype(vm, 2);
return (type == OT_CLOSURE || type == OT_NATIVECLOSURE);
}
template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
{
Var< Function > var(vm, 2);
// Attempt to perform the requested operation
return fn(var.value);
}
template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
{
Var< Function > var(vm, 2);
// Attempt to perform the requested operation
return fn(var.value);
}
};
/* ------------------------------------------------------------------------------------------------
* Specialization of the argument checking structure for objects.
*/
template < > struct SqDynArg< Object >
{
static inline bool CanPop(HSQUIRRELVM /*vm*/)
{
return true; // Objects can use any type.
}
template < typename F > static inline auto Do(F & fn, HSQUIRRELVM vm)
{
Var< Object > var(vm, 2);
// Attempt to perform the requested operation
return fn(var.value);
}
template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM vm)
{
Var< Object > var(vm, 2);
// Attempt to perform the requested operation
return fn(var.value);
}
};
/* ------------------------------------------------------------------------------------------------
* Specialization of the argument checking structure for null.
*/
template < > struct SqDynArg< std::nullptr_t >
{
static inline bool CanPop(HSQUIRRELVM vm)
{
return (sq_gettype(vm, 2) == OT_NULL);
}
template < typename F > static inline auto Do(F & fn, HSQUIRRELVM /*vm*/)
{
// Attempt to perform the requested operation
return fn(nullptr);
}
template < typename F > static inline auto Do(const F & fn, HSQUIRRELVM /*vm*/)
{
// Attempt to perform the requested operation
return fn(nullptr);
}
};
/* ------------------------------------------------------------------------------------------------
* Used to perform an operation on multiple types specified at compile time.
*/
template < typename... Ts > struct SqDynArgImpl;
/* ------------------------------------------------------------------------------------------------
* Zeroth case of the operation. No known operation of such pairs of types at this point.
*/
template < > struct SqDynArgImpl< >
{
template < typename T > static Int32 Try(const T & /*val*/, HSQUIRRELVM vm)
{
const String tn1(SqTypeName(vm, 1));
const String tn2(SqTypeName(vm, 2));
return sq_throwerror(vm, ToStrF("Such operation is not possible between (%s) and (%s)",
tn1.c_str(), tn2.c_str()));
}
};
/* ------------------------------------------------------------------------------------------------
* Argument pack type pealing. Attempt to perform the operation with a specified type.
*/
template < typename U, typename... Ts > struct SqDynArgImpl< U, Ts... >
{
template < typename F > static Int32 Try(F & fn, HSQUIRRELVM vm)
{
typedef typename std::decay< U >::type ArgType;
// Can the stack value be used with the current type?
if (SqDynArg< ArgType >::CanPop(vm))
{
// If not an instance then don't use a try catch block
if (sq_gettype(vm, 2) != OT_INSTANCE)
{
// Attempt to perform the operation and push the result on the stack
PushVar(vm, SqDynArg< ArgType >::Do(fn, vm));
// Specify that we completed the search and have a result
return 1;
}
// Instances require a try/catch block since we can't differentiate types
try
{
// Attempt to perform the operation and push the result on the stack
PushVar(vm, SqDynArg< ArgType >::Do(fn, vm));
// Specify that we completed the search and have a result
return 1;
}
catch (const Sqrat::Exception & e)
{
// Probably the wrong type
}
catch (...)
{
// Something very bad happened. At this point, we just don't want to let
// exceptions propagate to the virtual machine and we must end here.
// Either way, reaching this point is bad and we just shuved it under
// the rug. This is bad practice but circumstances forced me.
// Don't do this at home kids!
return -1;
}
}
// On to the next type
return SqDynArgImpl< Ts... >::Try(fn, vm);
}
};
/* ------------------------------------------------------------------------------------------------
* Squirrel function that forwards the call to the actual implementation.
*/
template < typename F, typename U, typename... Ts > SQInteger SqDynArgFwd(HSQUIRRELVM vm)
{
// Make sure that there are enough parameters on the stack
if (sq_gettop(vm) < 2)
{
return sq_throwerror(vm, "Insufficient parameters for such operation");
}
// Attempt to create the functor that performs the operation
F fn(vm);
// Is this functor in a valid state?
if (!fn)
{
return sq_throwerror(vm, "Functor failed to initialize");
}
// Attempt to perform the comparison
try
{
return SqDynArgImpl< U, Ts... >::Try(fn, vm);
}
catch (const Sqrat::Exception & e)
{
return sq_throwerror(vm, e.what());
}
catch (...)
{
return sq_throwerror(vm, "Unknown error occurred during comparison");
}
// We shouldn't really reach this point but something must be returned
return sq_throwerror(vm, "Operation encountered unknown behavior");
}
/* ------------------------------------------------------------------------------------------------
* Functor used to perform comparison.
*/
template < typename T > struct SqDynArgCmpFn
{
// --------------------------------------------------------------------------------------------
HSQUIRRELVM mVM; // The squirrel virtual machine.
Var< T * > mVar; // The instance of the base type.
/* --------------------------------------------------------------------------------------------
* Base constructor. (required)
*/
SqDynArgCmpFn(HSQUIRRELVM vm)
: mVM(vm), mVar(vm, 1)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean. (required)
*/
operator bool () const
{
return (mVar.value != nullptr);
}
/* --------------------------------------------------------------------------------------------
* Function call operator for references. (required)
*/
template < typename U > SQInteger operator () (U & v) const
{
return mVar.value->Cmp(v);
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const references. (required)
*/
template < typename U > SQInteger operator () (const U & v) const
{
return mVar.value->Cmp(v);
}
/* --------------------------------------------------------------------------------------------
* Function call operator for pointers. (required)
*/
template < typename U > SQInteger operator () (U * v) const
{
return mVar.value->Cmp(v);
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const pointers. (required)
*/
template < typename U > SQInteger operator () (const U * v) const
{
return mVar.value->Cmp(v);
}
/* --------------------------------------------------------------------------------------------
* Function call operator for null pointers. (required)
*/
SQInteger operator () (std::nullptr_t) const
{
return mVar.value->Cmp(nullptr);
}
};
/* ------------------------------------------------------------------------------------------------
* Functor used to perform addition.
*/
template < typename T > struct SqDynArgAddFn
{
// --------------------------------------------------------------------------------------------
HSQUIRRELVM mVM; // The squirrel virtual machine.
Var< T * > mVar; // The instance of the base type.
/* --------------------------------------------------------------------------------------------
* Base constructor. (required)
*/
SqDynArgAddFn(HSQUIRRELVM vm)
: mVM(vm), mVar(vm, 1)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean. (required)
*/
operator bool () const
{
return (mVar.value != nullptr);
}
/* --------------------------------------------------------------------------------------------
* Function call operator for references. (required)
*/
template < typename U > T operator () (U & v) const
{
return (*mVar.value) + v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const references. (required)
*/
template < typename U > T operator () (const U & v) const
{
return (*mVar.value) + v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for pointers. (required)
*/
template < typename U > T operator () (U * v) const
{
return (*mVar.value) + v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const pointers. (required)
*/
template < typename U > T operator () (const U * v) const
{
return (*mVar.value) + v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for null pointers. (required)
*/
T operator () (std::nullptr_t) const
{
return (*mVar.value);
}
};
/* ------------------------------------------------------------------------------------------------
* Functor used to perform subtraction.
*/
template < typename T > struct SqDynArgSubFn
{
// --------------------------------------------------------------------------------------------
HSQUIRRELVM mVM; // The squirrel virtual machine.
Var< T * > mVar; // The instance of the base type.
/* --------------------------------------------------------------------------------------------
* Base constructor. (required)
*/
SqDynArgSubFn(HSQUIRRELVM vm)
: mVM(vm), mVar(vm, 1)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean. (required)
*/
operator bool () const
{
return (mVar.value != nullptr);
}
/* --------------------------------------------------------------------------------------------
* Function call operator for references. (required)
*/
template < typename U > T operator () (U & v) const
{
return (*mVar.value) - v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const references. (required)
*/
template < typename U > T operator () (const U & v) const
{
return (*mVar.value) - v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for pointers. (required)
*/
template < typename U > T operator () (U * v) const
{
return (*mVar.value) - v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const pointers. (required)
*/
template < typename U > T operator () (const U * v) const
{
return (*mVar.value) - v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for null pointers. (required)
*/
T operator () (std::nullptr_t) const
{
return (*mVar.value);
}
};
/* ------------------------------------------------------------------------------------------------
* Functor used to perform multiplication.
*/
template < typename T > struct SqDynArgMulFn
{
// --------------------------------------------------------------------------------------------
HSQUIRRELVM mVM; // The squirrel virtual machine.
Var< T * > mVar; // The instance of the base type.
/* --------------------------------------------------------------------------------------------
* Base constructor. (required)
*/
SqDynArgMulFn(HSQUIRRELVM vm)
: mVM(vm), mVar(vm, 1)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean. (required)
*/
operator bool () const
{
return (mVar.value != nullptr);
}
/* --------------------------------------------------------------------------------------------
* Function call operator for references. (required)
*/
template < typename U > T operator () (U & v) const
{
return (*mVar.value) * v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const references. (required)
*/
template < typename U > T operator () (const U & v) const
{
return (*mVar.value) * v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for pointers. (required)
*/
template < typename U > T operator () (U * v) const
{
return (*mVar.value) * v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const pointers. (required)
*/
template < typename U > T operator () (const U * v) const
{
return (*mVar.value) * v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for null pointers. (required)
*/
T operator () (std::nullptr_t) const
{
return (*mVar.value) * static_cast< SQInteger >(0);
}
};
/* ------------------------------------------------------------------------------------------------
* Functor used to perform division.
*/
template < typename T > struct SqDynArgDivFn
{
// --------------------------------------------------------------------------------------------
HSQUIRRELVM mVM; // The squirrel virtual machine.
Var< T * > mVar; // The instance of the base type.
/* --------------------------------------------------------------------------------------------
* Base constructor. (required)
*/
SqDynArgDivFn(HSQUIRRELVM vm)
: mVM(vm), mVar(vm, 1)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean. (required)
*/
operator bool () const
{
return (mVar.value != nullptr);
}
/* --------------------------------------------------------------------------------------------
* Function call operator for references. (required)
*/
template < typename U > T operator () (U & v) const
{
return (*mVar.value) / v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const references. (required)
*/
template < typename U > T operator () (const U & v) const
{
return (*mVar.value) / v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for pointers. (required)
*/
template < typename U > T operator () (U * v) const
{
return (*mVar.value) / v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const pointers. (required)
*/
template < typename U > T operator () (const U * v) const
{
return (*mVar.value) / v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for null pointers. (required)
*/
T operator () (std::nullptr_t) const
{
return (*mVar.value) / static_cast< SQInteger >(0);
}
};
/* ------------------------------------------------------------------------------------------------
* Functor used to perform modulus.
*/
template < typename T > struct SqDynArgModFn
{
// --------------------------------------------------------------------------------------------
HSQUIRRELVM mVM; // The squirrel virtual machine.
Var< T * > mVar; // The instance of the base type.
/* --------------------------------------------------------------------------------------------
* Base constructor. (required)
*/
SqDynArgModFn(HSQUIRRELVM vm)
: mVM(vm), mVar(vm, 1)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to boolean. (required)
*/
operator bool () const
{
return (mVar.value != nullptr);
}
/* --------------------------------------------------------------------------------------------
* Function call operator for references. (required)
*/
template < typename U > T operator () (U & v) const
{
return (*mVar.value) % v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const references. (required)
*/
template < typename U > T operator () (const U & v) const
{
return (*mVar.value) % v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for pointers. (required)
*/
template < typename U > T operator () (U * v) const
{
return (*mVar.value) % v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for const pointers. (required)
*/
template < typename U > T operator () (const U * v) const
{
return (*mVar.value) % v;
}
/* --------------------------------------------------------------------------------------------
* Function call operator for null pointers. (required)
*/
T operator () (std::nullptr_t) const
{
return (*mVar.value) % static_cast< SQInteger >(0);
}
};
} // Namespace:: SqMod
#endif // _BASE_DYNARG_HPP_

889
module/Base/Quaternion.cpp Normal file
View File

@ -0,0 +1,889 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Quaternion.hpp"
#include "Base/Vector3.hpp"
#include "Base/Vector4.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
#define STOVAL(v) static_cast< Quaternion::Value >(v)
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Quaternion"))
// ------------------------------------------------------------------------------------------------
const Quaternion Quaternion::NIL(0);
const Quaternion Quaternion::MIN(std::numeric_limits< Quaternion::Value >::min());
const Quaternion Quaternion::MAX(std::numeric_limits< Quaternion::Value >::max());
const Quaternion Quaternion::IDENTITY(1.0, 0.0, 0.0, 0.0);
// ------------------------------------------------------------------------------------------------
SQChar Quaternion::Delim = ',';
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion()
: x(0.0), y(0.0), z(0.0), w(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion(Value sv)
: x(sv), y(sv), z(sv), w(sv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion(Value xv, Value yv, Value zv)
: x(xv), y(yv), z(zv), w(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion(Value xv, Value yv, Value zv, Value wv)
: x(xv), y(yv), z(zv), w(wv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator = (Value s)
{
x = s;
y = s;
z = s;
w = s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator = (const Vector3 & q)
{
x = q.x;
y = q.y;
z = q.z;
w = 0.0;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator = (const Vector4 & q)
{
x = q.x;
y = q.y;
z = q.z;
w = q.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator += (const Quaternion & q)
{
x += q.x;
y += q.y;
z += q.z;
w += q.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator -= (const Quaternion & q)
{
x -= q.x;
y -= q.y;
z -= q.z;
w -= q.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator *= (const Quaternion & q)
{
x *= q.x;
y *= q.y;
z *= q.z;
w *= q.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator /= (const Quaternion & q)
{
x /= q.x;
y /= q.y;
z /= q.z;
w /= q.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator %= (const Quaternion & q)
{
x = std::fmod(x, q.x);
y = std::fmod(y, q.y);
z = std::fmod(z, q.z);
w = std::fmod(w, q.w);
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator += (Value s)
{
x += s;
y += s;
z += s;
w += s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator -= (Value s)
{
x -= s;
y -= s;
z -= s;
w -= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator *= (Value s)
{
x *= s;
y *= s;
z *= s;
w *= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator /= (Value s)
{
x /= s;
y /= s;
z /= s;
w /= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator %= (Value s)
{
x = std::fmod(x, s);
y = std::fmod(y, s);
z = std::fmod(z, s);
w = std::fmod(w, s);
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator ++ ()
{
++x;
++y;
++z;
++w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion & Quaternion::operator -- ()
{
--x;
--y;
--z;
--w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator ++ (int)
{
Quaternion state(*this);
++x;
++y;
++z;
++w;
return state;
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator -- (int)
{
Quaternion state(*this);
--x;
--y;
--z;
--w;
return state;
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator + (const Quaternion & q) const
{
return Quaternion(x + q.x, y + q.y, z + q.z, w + q.w);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator + (Value s) const
{
return Quaternion(x + s, y + s, z + s, w + s);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator - (const Quaternion & q) const
{
return Quaternion(x - q.x, y - q.y, z - q.z, w - q.w);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator - (Value s) const
{
return Quaternion(x - s, y - s, z - s, w - s);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator * (const Quaternion & q) const
{
return Quaternion(x * q.x, y * q.y, z * q.z, w * q.w);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator * (Value s) const
{
return Quaternion(x * s, y * s, z * s, w * s);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator / (const Quaternion & q) const
{
return Quaternion(x / q.x, y / q.y, z / q.z, w / q.w);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator / (Value s) const
{
return Quaternion(x / s, y / s, z / s, w / s);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator % (const Quaternion & q) const
{
return Quaternion(std::fmod(x, q.x), std::fmod(y, q.y), std::fmod(z, q.z), std::fmod(w, q.w));
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator % (Value s) const
{
return Quaternion(std::fmod(x, s), std::fmod(y, s), std::fmod(z, s), std::fmod(w, s));
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator + () const
{
return Quaternion(std::fabs(x), std::fabs(y), std::fabs(z), std::fabs(w));
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::operator - () const
{
return Quaternion(-x, -y, -z, -w);
}
// ------------------------------------------------------------------------------------------------
bool Quaternion::operator == (const Quaternion & q) const
{
return EpsEq(x, q.x) && EpsEq(y, q.y) && EpsEq(z, q.z) && EpsEq(w, q.w);
}
// ------------------------------------------------------------------------------------------------
bool Quaternion::operator != (const Quaternion & q) const
{
return !EpsEq(x, q.x) || !EpsEq(y, q.y) || !EpsEq(z, q.z) || !EpsEq(w, q.w);
}
// ------------------------------------------------------------------------------------------------
bool Quaternion::operator < (const Quaternion & q) const
{
return EpsLt(x, q.x) && EpsLt(y, q.y) && EpsLt(z, q.z) && EpsLt(w, q.w);
}
// ------------------------------------------------------------------------------------------------
bool Quaternion::operator > (const Quaternion & q) const
{
return EpsGt(x, q.x) && EpsGt(y, q.y) && EpsGt(z, q.z) && EpsGt(w, q.w);
}
// ------------------------------------------------------------------------------------------------
bool Quaternion::operator <= (const Quaternion & q) const
{
return EpsLtEq(x, q.x) && EpsLtEq(y, q.y) && EpsLtEq(z, q.z) && EpsLtEq(w, q.w);
}
// ------------------------------------------------------------------------------------------------
bool Quaternion::operator >= (const Quaternion & q) const
{
return EpsGtEq(x, q.x) && EpsGtEq(y, q.y) && EpsGtEq(z, q.z) && EpsGtEq(w, q.w);
}
// ------------------------------------------------------------------------------------------------
Int32 Quaternion::Cmp(const Quaternion & o) const
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
CSStr Quaternion::ToString() const
{
return ToStrF("%f,%f,%f,%f", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
void Quaternion::SetScalar(Value ns)
{
x = ns;
y = ns;
z = ns;
w = ns;
}
// ------------------------------------------------------------------------------------------------
void Quaternion::SetQuaternion(const Quaternion & q)
{
x = q.x;
y = q.y;
z = q.z;
w = q.w;
}
// ------------------------------------------------------------------------------------------------
void Quaternion::SetQuaternionEx(Value nx, Value ny, Value nz, Value nw)
{
x = nx;
y = ny;
z = nz;
w = nw;
}
// ------------------------------------------------------------------------------------------------
void Quaternion::SetVector3(const Vector3 & v)
{
SetVector3Ex(v.x, v.y, v.z);
}
// ------------------------------------------------------------------------------------------------
void Quaternion::SetVector3Ex(Value nx, Value ny, Value nz)
{
Float64 angle;
angle = (nx * 0.5);
const Float64 sr = std::sin(angle);
const Float64 cr = std::cos(angle);
angle = (ny * 0.5);
const Float64 sp = std::sin(angle);
const Float64 cp = std::cos(angle);
angle = (nz * 0.5);
const Float64 sy = std::sin(angle);
const Float64 cy = std::cos(angle);
const Float64 cpcy = (cp * cy);
const Float64 spcy = (sp * cy);
const Float64 cpsy = (cp * sy);
const Float64 spsy = (sp * sy);
x = STOVAL(sr * cpcy - cr * spsy);
y = STOVAL(cr * spcy + sr * cpsy);
z = STOVAL(cr * cpsy - sr * spcy);
w = STOVAL(cr * cpcy + sr * spsy);
Normalize();
}
// ------------------------------------------------------------------------------------------------
void Quaternion::SetVector4(const Vector4 & v)
{
x = v.x;
y = v.y;
z = v.z;
w = v.w;
}
// ------------------------------------------------------------------------------------------------
void Quaternion::SetStr(SQChar delim, StackStrF & values)
{
SetQuaternion(Quaternion::GetEx(delim, values));
}
// ------------------------------------------------------------------------------------------------
void Quaternion::Generate()
{
x = GetRandomFloat32();
y = GetRandomFloat32();
z = GetRandomFloat32();
w = GetRandomFloat32();
}
// ------------------------------------------------------------------------------------------------
void Quaternion::Generate(Value min, Value max)
{
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);
}
// ------------------------------------------------------------------------------------------------
void Quaternion::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax)
{
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);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::Abs() const
{
return Quaternion(std::fabs(x), std::fabs(y), std::fabs(z), std::fabs(w));
}
// ------------------------------------------------------------------------------------------------
bool Quaternion::IsNaN() const
{
return std::isnan(w) || std::isnan(x) || std::isnan(y) || std::isnan(z);
}
// ------------------------------------------------------------------------------------------------
Quaternion::Quaternion::Value Quaternion::LengthSquared() const
{
return (x * x) + (y * y) + (z * z) + (w * w);
}
// ------------------------------------------------------------------------------------------------
Quaternion::Value Quaternion::DotProduct(const Quaternion & quat) const
{
return (x * quat.x) + (y * quat.y) + (z * quat.z) + (w * quat.w);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::Conjugate() const
{
return Quaternion(-x, -y, -z, w);;
}
// ------------------------------------------------------------------------------------------------
void Quaternion::Normalize()
{
const Value len_squared = LengthSquared();
if (!EpsEq(len_squared, STOVAL(1.0)) && EpsGt(len_squared, STOVAL(0.0)))
{
const Value inv_len = STOVAL(1.0) / std::sqrt(len_squared);
x *= inv_len;
y *= inv_len;
z *= inv_len;
w *= inv_len;
}
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::Normalized() const
{
const Value len_squared = LengthSquared();
if (!EpsEq(len_squared, STOVAL(1.0)) && EpsGt(len_squared, STOVAL(0.0)))
{
Value inv_len = 1.0f / sqrtf(len_squared);
return ((*this) * inv_len);
}
else
{
return (*this);
}
}
// ------------------------------------------------------------------------------------------------
void Quaternion::FromAngleAxis(Value angle, const Vector3 & axis)
{
const Vector3 norm_axis = axis.Normalized();
angle *= SQMOD_DEGTORAD_2;
const Value sin_angle = std::sin(angle);
x = norm_axis.x * sin_angle;
y = norm_axis.y * sin_angle;
z = norm_axis.z * sin_angle;
w = std::cos(angle);
}
// ------------------------------------------------------------------------------------------------
void Quaternion::FromRotationTo(const Vector3 & start, const Vector3 & end)
{
const Vector3 norm_start = start.Normalized();
const Vector3 norm_end = end.Normalized();
const Value d = norm_start.DotProduct(norm_end);
if (EpsGt(d, STOVAL(-1.0)))
{
const Vector3 c = norm_start.CrossProduct(norm_end);
const Value s = std::sqrt((STOVAL(1.0) + d) * STOVAL(2.0));
const Value inv_s = STOVAL(1.0) / s;
x = c.x * inv_s;
y = c.y * inv_s;
z = c.z * inv_s;
w = STOVAL(0.5) * s;
}
else
{
Vector3 axis = Vector3::RIGHT.CrossProduct(norm_start);
if (EpsLt(axis.GetLength(), STOVAL(0.0)))
{
FromAngleAxis(STOVAL(180), Vector3::UP.CrossProduct(norm_start));
}
else
{
FromAngleAxis(STOVAL(180), axis);
}
}
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::Inverse() const
{
const Value len_squared = LengthSquared();
if (EpsEq(len_squared, STOVAL(1.0)))
{
return Conjugate();
}
else if (EpsGtEq(len_squared, STOVAL(0.0)))
{
return Conjugate() * (STOVAL(1.0) / len_squared);
}
else
{
return IDENTITY;
}
}
// ------------------------------------------------------------------------------------------------
Vector3 Quaternion::ToEuler() const
{
const Float64 sqw = (w * w);
const Float64 sqx = (x * x);
const Float64 sqy = (y * y);
const Float64 sqz = (z * z);
const Float64 test = 2.0 * ((y * w) - (x * z));
if (EpsEq(test, 1.0))
{
return Vector3(
// bank = rotation about x-axis
STOVAL(0.0),
// attitude = rotation about y-axis
STOVAL(SQMOD_PI64 / 2.0),
// heading = rotation about z-axis
STOVAL(-2.0 * std::atan2(x, w))
);
}
else if (EpsEq(test, -1.0))
{
return Vector3(
// bank = rotation about x-axis
STOVAL(0.0),
// attitude = rotation about y-axis
STOVAL(SQMOD_PI64 / -2.0),
// heading = rotation about z-axis
STOVAL(2.0 * std::atan2(x, w))
);
}
return Vector3(
// bank = rotation about x-axis
STOVAL(std::atan2(2.0 * ((y * z) + (x * w)), (-sqx - sqy + sqz + sqw))),
// attitude = rotation about y-axis
STOVAL(std::asin(Clamp(test, -1.0, 1.0))),
// heading = rotation about z-axis
STOVAL(std::atan2(2.0 * ((x * y) + (z * w)), (sqx - sqy - sqz + sqw)))
);
}
// ------------------------------------------------------------------------------------------------
Quaternion::Value Quaternion::YawAngle() const
{
return ToEuler().y;
}
// ------------------------------------------------------------------------------------------------
Quaternion::Value Quaternion::PitchAngle() const
{
return ToEuler().x;
}
// ------------------------------------------------------------------------------------------------
Quaternion::Value Quaternion::RollAngle() const
{
return ToEuler().z;
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::Slerp(Quaternion quat, Value t) const
{
// Favor accuracy for native code builds
Value cos_angle = DotProduct(quat);
// Enable shortest path rotation
if (EpsLt(cos_angle, STOVAL(0.0)))
{
cos_angle = -cos_angle;
quat = -quat;
}
const Value angle = std::acos(cos_angle);
Value sin_angle = std::sin(angle);
Value t1, t2;
if (sin_angle > STOVAL(0.001))
{
Value inv_sin_angle = STOVAL(1.0) / sin_angle;
t1 = std::sin((STOVAL(1.0) - t) * angle) * inv_sin_angle;
t2 = std::sin(t * angle) * inv_sin_angle;
}
else
{
t1 = STOVAL(1.0) - t;
t2 = t;
}
return ((*this) * t1 + quat * t2);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::Nlerp(const Quaternion & quat, Value t) const
{
return NlerpEx(quat, t, false);
}
// ------------------------------------------------------------------------------------------------
Quaternion Quaternion::NlerpEx(const Quaternion & quat, Value t, bool shortest_path) const
{
Quaternion result;
const Value fcos = DotProduct(quat);
if (EpsLt(fcos, STOVAL(0.0)) && shortest_path)
{
result = (*this) + (((-quat) - (*this)) * t);
}
else
{
result = (*this) + ((quat - (*this)) * t);
}
result.Normalize();
return result;
}
// ------------------------------------------------------------------------------------------------
const Quaternion & Quaternion::Get(StackStrF & str)
{
return Quaternion::GetEx(Quaternion::Delim, str);
}
// ------------------------------------------------------------------------------------------------
const Quaternion & Quaternion::GetEx(SQChar delim, StackStrF & str)
{
// 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.mLen <= 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.mPtr, 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.SetScalar(sv);
return quat;
}
// ------------------------------------------------------------------------------------------------
const Quaternion & GetQuaternion(Float32 xv, Float32 yv, Float32 zv)
{
static Quaternion quat;
quat.SetVector3Ex(xv, yv, zv);
return quat;
}
// ------------------------------------------------------------------------------------------------
const Quaternion & GetQuaternion(Float32 xv, Float32 yv, Float32 zv, Float32 wv)
{
static Quaternion quat;
quat.SetQuaternionEx(xv, yv, zv, wv);
return quat;
}
// ------------------------------------------------------------------------------------------------
const Quaternion & GetQuaternion(const Quaternion & o)
{
static Quaternion quat;
quat.SetQuaternion(o);
return quat;
}
// ================================================================================================
void Register_Quaternion(HSQUIRRELVM vm)
{
typedef Quaternion::Value Val;
RootTable(vm).Bind(Typename::Str,
Class< Quaternion >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< Val >()
.Ctor< Val, Val, Val >()
.Ctor< Val, Val, Val, Val >()
// Member Variables
.Var(_SC("x"), &Quaternion::x)
.Var(_SC("y"), &Quaternion::y)
.Var(_SC("z"), &Quaternion::z)
.Var(_SC("w"), &Quaternion::w)
.Var(_SC("X"), &Quaternion::x)
.Var(_SC("Y"), &Quaternion::y)
.Var(_SC("Z"), &Quaternion::z)
.Var(_SC("W"), &Quaternion::w)
// Core Meta-methods
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Quaternion::ToString)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< Quaternion >, SQFloat, SQInteger, bool, std::nullptr_t, Quaternion >)
.Func< Quaternion (Quaternion::*)(void) const >(_SC("_unm"), &Quaternion::operator -)
// Properties
.Prop(_SC("Abs"), &Quaternion::Abs)
.Prop(_SC("NaN"), &Quaternion::IsNaN)
.Prop(_SC("LengthSq"), &Quaternion::LengthSquared)
.Prop(_SC("Conjugate"), &Quaternion::Conjugate)
.Prop(_SC("Normalized"), &Quaternion::Normalized)
.Prop(_SC("Inverse"), &Quaternion::Inverse)
.Prop(_SC("Euler"), &Quaternion::ToEuler, &Quaternion::SetVector3)
.Prop(_SC("YawAngle"), &Quaternion::YawAngle)
.Prop(_SC("PitchAngle"), &Quaternion::PitchAngle)
.Prop(_SC("RollAngle"), &Quaternion::RollAngle)
// Member Methods
.Func(_SC("SetScalar"), &Quaternion::SetScalar)
.Func(_SC("SetQuaternion"), &Quaternion::SetQuaternion)
.Func(_SC("SetQuaternionEx"), &Quaternion::SetQuaternionEx)
.Func(_SC("SetVector3"), &Quaternion::SetVector3)
.Func(_SC("SetVector3Ex"), &Quaternion::SetVector3Ex)
.Func(_SC("SetVector4"), &Quaternion::SetVector4)
.FmtFunc(_SC("SetStr"), &Quaternion::SetStr)
.Func(_SC("Clear"), &Quaternion::Clear)
.Func(_SC("DotProduct"), &Quaternion::DotProduct)
.Func(_SC("Normalize"), &Quaternion::Normalize)
.Func(_SC("FromAngleAxis"), &Quaternion::FromAngleAxis)
.Func(_SC("FromRotationTo"), &Quaternion::FromRotationTo)
.Func(_SC("Slerp"), &Quaternion::Slerp)
.Func(_SC("Nlerp"), &Quaternion::Nlerp)
.Func(_SC("NlerpEx"), &Quaternion::NlerpEx)
// Member Overloads
.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)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Quaternion >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Quaternion >)
.StaticFmtFunc(_SC("FromStr"), &Quaternion::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Quaternion::GetEx)
// 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 >=)
);
}
} // Namespace:: SqMod

456
module/Base/Quaternion.hpp Normal file
View File

@ -0,0 +1,456 @@
#ifndef _BASE_QUATERNION_HPP_
#define _BASE_QUATERNION_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Quaternion class for representing rotations.
*/
struct Quaternion
{
/* --------------------------------------------------------------------------------------------
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* Helper instances for common values mostly used as return types or comparison.
*/
static const Quaternion NIL;
static const Quaternion MIN;
static const Quaternion MAX;
static const Quaternion IDENTITY;
/* --------------------------------------------------------------------------------------------
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* The x, y, z and w components of this type.
*/
Value x, y, z, w;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Quaternion();
/* --------------------------------------------------------------------------------------------
* Construct from scalar value.
*/
explicit Quaternion(Value sv);
/* --------------------------------------------------------------------------------------------
* Construct from Euler angles (in degrees.)
*/
Quaternion(Value xv, Value yv, Value zv);
/* --------------------------------------------------------------------------------------------
* Construct from individual values.
*/
Quaternion(Value xv, Value yv, Value zv, Value wv);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Quaternion(const Quaternion & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Quaternion(Quaternion && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Quaternion() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Quaternion & operator = (const Quaternion & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Quaternion & operator = (Quaternion && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Quaternion & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* Euler assignment operator.
*/
Quaternion & operator = (const Vector3 & q);
/* --------------------------------------------------------------------------------------------
* Four-dimensional vector assignment operator threated as a three-dimensional vector.
*/
Quaternion & operator = (const Vector4 & q);
/* --------------------------------------------------------------------------------------------
* Addition assignment operator.
*/
Quaternion & operator += (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* Subtraction assignment operator.
*/
Quaternion & operator -= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* Multiplication assignment operator.
*/
Quaternion & operator *= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* Division assignment operator.
*/
Quaternion & operator /= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* Modulo assignment operator.
*/
Quaternion & operator %= (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* Scalar value addition assignment operator.
*/
Quaternion & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction assignment operator.
*/
Quaternion & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication assignment operator.
*/
Quaternion & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value division assignment operator.
*/
Quaternion & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value modulo assignment operator.
*/
Quaternion & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* Pre-increment operator.
*/
Quaternion & operator ++ ();
/* --------------------------------------------------------------------------------------------
* Pre-decrement operator.
*/
Quaternion & operator -- ();
/* --------------------------------------------------------------------------------------------
* Post-increment operator.
*/
Quaternion operator ++ (int);
/* --------------------------------------------------------------------------------------------
* Post-decrement operator.
*/
Quaternion operator -- (int);
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
Quaternion operator + (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
Quaternion operator - (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
Quaternion operator * (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
Quaternion operator / (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Modulo operator.
*/
Quaternion operator % (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Scalar value addition operator.
*/
Quaternion operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction operator.
*/
Quaternion operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication operator.
*/
Quaternion operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value division operator.
*/
Quaternion operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value modulo operator.
*/
Quaternion operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* Unary plus operator.
*/
Quaternion operator + () const;
/* --------------------------------------------------------------------------------------------
* Unary minus operator.
*/
Quaternion operator - () const;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Quaternion & q) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
{
return Cmp(Quaternion(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
{
return Cmp(Quaternion(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
{
return Cmp(Quaternion(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
{
return Cmp(Quaternion(static_cast< Value >(0)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void SetScalar(Value ns);
/* --------------------------------------------------------------------------------------------
* Copy the values from another instance of this type.
*/
void SetQuaternion(const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetQuaternionEx(Value nx, Value ny, Value nz, Value nw);
/* --------------------------------------------------------------------------------------------
* Copy the values from a three-dimensional vector as euler rotation.
*/
void SetVector3(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* Copy the values from a three-dimensional vector as euler rotation.
*/
void SetVector3Ex(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* Copy the values from a four-dimensional vector.
*/
void SetVector4(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/
void SetStr(SQChar delim, StackStrF & values);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax);
/* --------------------------------------------------------------------------------------------
* Clear the component values to default.
*/
void Clear()
{
x = 0.0, y = 0.0, z = 0.0, w = 0.0;
}
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Quaternion Abs() const;
/* --------------------------------------------------------------------------------------------
* Return whether is NaN.
*/
bool IsNaN() const;
/* --------------------------------------------------------------------------------------------
* Return squared length.
*/
Value LengthSquared() const;
/* --------------------------------------------------------------------------------------------
* Calculate dot product.
*/
Value DotProduct(const Quaternion & quat) const;
/* --------------------------------------------------------------------------------------------
* Return conjugate.
*/
Quaternion Conjugate() const;
/* --------------------------------------------------------------------------------------------
* Normalize to unit length.
*/
void Normalize();
/* --------------------------------------------------------------------------------------------
* Return normalized to unit length.
*/
Quaternion Normalized() const;
/* --------------------------------------------------------------------------------------------
* Define from an angle (in degrees) and axis.
*/
void FromAngleAxis(Value angle, const Vector3 & axis);
/* --------------------------------------------------------------------------------------------
* Define from the rotation difference between two direction vectors.
*/
void FromRotationTo(const Vector3 & start, const Vector3 & end);
/* --------------------------------------------------------------------------------------------
* Return inverse.
*/
Quaternion Inverse() const;
/* --------------------------------------------------------------------------------------------
* Return Euler angles in degrees.
*/
Vector3 ToEuler() const;
/* --------------------------------------------------------------------------------------------
* Return yaw angle in degrees.
*/
Value YawAngle() const;
/* --------------------------------------------------------------------------------------------
* Return pitch angle in degrees.
*/
Value PitchAngle() const;
/* --------------------------------------------------------------------------------------------
* Return roll angle in degrees.
*/
Value RollAngle() const;
/* --------------------------------------------------------------------------------------------
* Spherical interpolation with another quaternion.
*/
Quaternion Slerp(Quaternion quat, Value t) const;
/* --------------------------------------------------------------------------------------------
* Normalized linear interpolation with another quaternion.
*/
Quaternion Nlerp(const Quaternion & quat, Value t) const;
/* --------------------------------------------------------------------------------------------
* Normalized linear interpolation with another quaternion.
*/
Quaternion NlerpEx(const Quaternion & quat, Value t, bool shortest_path) const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Quaternion type from a string.
*/
static const Quaternion & Get(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Quaternion type from a string.
*/
static const Quaternion & GetEx(SQChar delim, StackStrF & str);
};
} // Namespace:: SqMod
#endif // _BASE_QUATERNION_HPP_

200
module/Base/ScriptSrc.cpp Normal file
View File

@ -0,0 +1,200 @@
// ------------------------------------------------------------------------------------------------
#include "Base/ScriptSrc.hpp"
// ------------------------------------------------------------------------------------------------
#include <cstdio>
#include <algorithm>
#include <stdexcept>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Helper class to ensure the file handle is closed regardless of the situation.
*/
class FileHandle
{
public:
// --------------------------------------------------------------------------------------------
std::FILE * mFile; // Handle to the opened file.
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
FileHandle(CSStr path)
: mFile(std::fopen(path, "rb"))
{
if (!mFile)
{
throw std::runtime_error(ToStrF("Unable to open script source (%s)", path));
}
}
/* --------------------------------------------------------------------------------------------
* Copy constructor. (disabled)
*/
FileHandle(const FileHandle & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move constructor. (disabled)
*/
FileHandle(FileHandle && o) = delete;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~FileHandle()
{
if (mFile)
{
std::fclose(mFile);
}
}
/* --------------------------------------------------------------------------------------------
* Copy assignment operator. (disabled)
*/
FileHandle & operator = (const FileHandle & o) = delete;
/* --------------------------------------------------------------------------------------------
* Move assignment operator. (disabled)
*/
FileHandle & operator = (FileHandle && o) = delete;
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed file handle.
*/
operator std::FILE * ()
{
return mFile;
}
/* --------------------------------------------------------------------------------------------
* Implicit conversion to the managed file handle.
*/
operator std::FILE * () const
{
return mFile;
}
};
// ------------------------------------------------------------------------------------------------
void ScriptSrc::Process()
{
// Attempt to open the specified file
FileHandle fp(mPath.c_str());
// First 2 bytes of the file will tell if this is a compiled script
std::uint16_t tag;
// Go to the end of the file
std::fseek(fp, 0, SEEK_END);
// Calculate buffer size from beginning to current position
const LongI length = std::ftell(fp);
// Go back to the beginning
std::fseek(fp, 0, SEEK_SET);
// Read the first 2 bytes of the file and determine the file type
if ((length >= 2) && (std::fread(&tag, 1, 2, fp) != 2 || tag == SQ_BYTECODE_STREAM_TAG))
{
return; // Probably an empty file or compiled script
}
// Allocate enough space to hold the file data
mData.resize(length, 0);
// Go back to the beginning
std::fseek(fp, 0, SEEK_SET);
// Read the file contents into allocated data
std::fread(&mData[0], 1, length, fp);
// Where the last line ended
size_t line_start = 0, line_end = 0;
// Process the file data and locate new lines
for (String::const_iterator itr = mData.cbegin(); itr != mData.cend();)
{
// Is this a Unix style line ending?
if (*itr == '\n')
{
// Extract the line length
line_end = std::distance(mData.cbegin(), itr);
// Store the beginning of the line
mLine.emplace_back(line_start, line_end);
// Advance to the next line
line_start = line_end+1;
// The line end character was not included
++itr;
}
// Is this a Windows style line ending?
else if (*itr == '\r')
{
if (*(++itr) == '\n')
{
// Extract the line length
line_end = std::distance(mData.cbegin(), itr)-1;
// Store the beginning of the line
mLine.emplace_back(line_start, line_end);
// Advance to the next line
line_start = line_end+2;
// The line end character was not included
++itr;
}
}
else
{
++itr;
}
}
// Should we add the last line as well?
if (mData.size() - line_start > 0)
{
mLine.emplace_back(line_start, mData.size());
}
// Specify that this script contains line information
mInfo = true;
}
// ------------------------------------------------------------------------------------------------
ScriptSrc::ScriptSrc(HSQUIRRELVM vm, String && path, bool delay, bool info)
: mExec(vm)
, mPath(std::move(path))
, mData()
, mLine()
, mInfo(info)
, mDelay(delay)
{
// Is the specified virtual machine invalid?
if (!vm)
{
throw std::runtime_error("Invalid virtual machine pointer");
}
// Is the specified path empty?
else if (mPath.empty())
{
throw std::runtime_error("Invalid or empty script path");
}
// Should we load the file contents for debugging purposes?
else if (mInfo)
{
Process();
}
}
// ------------------------------------------------------------------------------------------------
String ScriptSrc::FetchLine(size_t line, bool trim) const
{
// Do we have such line?
if (line > mLine.size())
{
return String(); // Nope!
}
// Grab it's range in the file
Line::const_reference l = mLine.at(line);
// Grab the code from that line
String code = mData.substr(l.first, l.second - l.first);
// Trim whitespace from the beginning of the code code
if (trim)
{
code.erase(0, code.find_first_not_of(" \t\n\r\f\v"));
}
// Return the resulting string
return code;
}
} // Namespace:: SqMod

83
module/Base/ScriptSrc.hpp Normal file
View File

@ -0,0 +1,83 @@
#ifndef _BASE_SCRIPTSRC_HPP_
#define _BASE_SCRIPTSRC_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Utility.hpp"
// ------------------------------------------------------------------------------------------------
#include <vector>
#include <utility>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
class Core;
/* ------------------------------------------------------------------------------------------------
* Hold a information about loaded scripts as it's contents and executable code.
*/
class ScriptSrc
{
public:
// --------------------------------------------------------------------------------------------
typedef std::vector< std::pair< Uint32, Uint32 > > Line;
// --------------------------------------------------------------------------------------------
Script mExec; // Reference to the script object.
String mPath; // Path to the script file.
String mData; // The contents of the script file.
Line mLine; // List of lines of code in the data.
bool mInfo; // Whether this script contains line information.
bool mDelay; // Don't execute immediately after compilation.
/* --------------------------------------------------------------------------------------------
* Read file contents and calculate information about the lines of code.
*/
void Process();
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
ScriptSrc(HSQUIRRELVM vm, const String & path, bool delay = false, bool info = false)
: ScriptSrc(vm, String(path), delay, info)
{
/* ... */
}
/* --------------------------------------------------------------------------------------------
* Base constructor.
*/
ScriptSrc(HSQUIRRELVM vm, String && path, bool delay = false, bool info = false);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
ScriptSrc(const ScriptSrc & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
ScriptSrc(ScriptSrc && o) = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
ScriptSrc & operator = (const ScriptSrc & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
ScriptSrc & operator = (ScriptSrc && o) = default;
/* --------------------------------------------------------------------------------------------
* Fetches a line from the code. Can also triim whitespace at the beginning.
*/
String FetchLine(size_t line, bool trim = true) const;
};
} // Namespace:: SqMod
#endif // _BASE_SCRIPTSRC_HPP_

1104
module/Base/Shared.cpp Normal file

File diff suppressed because it is too large Load Diff

259
module/Base/Shared.hpp Normal file
View File

@ -0,0 +1,259 @@
#ifndef _BASE_SHARED_HPP_
#define _BASE_SHARED_HPP_
// ------------------------------------------------------------------------------------------------
#include "Base/Utility.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Helper constants used by the bas types.
*/
constexpr Float32 SQMOD_PI = 3.14159265358979323846264338327950288f;
constexpr Float64 SQMOD_PI64 = 3.1415926535897932384626433832795028841971693993751d;
constexpr Float32 SQMOD_RECIPROCAL_PI = (1.0f / SQMOD_PI);
constexpr Float64 SQMOD_RECIPROCAL_PI64 = 1.0 / SQMOD_PI64;
constexpr Float32 SQMOD_HALF_PI = (SQMOD_PI * 0.5f);
constexpr Float32 SQMOD_HALF_PI64 = (SQMOD_PI64 * 0.5);
constexpr Float32 SQMOD_DEGTORAD = SQMOD_PI / 180.0f;
constexpr Float64 SQMOD_DEGTORAD64 = SQMOD_PI64 / 180.0;
constexpr Float32 SQMOD_DEGTORAD_2 = SQMOD_PI / 360.0f; // M_DEGTORAD / 2.f
constexpr Float64 SQMOD_DEGTORAD64_2 = SQMOD_PI64 / 360.0; // M_DEGTORAD / 2.f
constexpr Float32 SQMOD_RADTODEG = 1.0f / SQMOD_DEGTORAD;
constexpr Float64 SQMOD_RADTODEG64 = 1.0 / SQMOD_DEGTORAD64;
/* ------------------------------------------------------------------------------------------------
* Intersection test result.
*/
enum Intersection
{
SQMODI_OUTSIDE = 0,
SQMODI_INTERSECTS,
SQMODI_INSIDE,
};
/* ------------------------------------------------------------------------------------------------
* Helper used to reference and keep track of signal instances.
*/
typedef std::pair< Signal *, LightObj > SignalPair;
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Primary logging functions.
*/
extern void LogDbg(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogUsr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogScs(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogInf(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogWrn(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogErr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogFtl(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Secondary logging functions.
*/
extern void LogSDbg(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSUsr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSScs(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSInf(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSWrn(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSErr(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
extern void LogSFtl(CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 1, 2);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Primary conditional logging functions.
*/
extern bool cLogDbg(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogUsr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogScs(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogInf(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogWrn(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogErr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogFtl(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
/* ------------------------------------------------------------------------------------------------
* Forward declarations of the logging functions to avoid including the logger everywhere.
* Secondary conditional logging functions.
*/
extern bool cLogSDbg(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSUsr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSScs(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSInf(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSWrn(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSErr(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
extern bool cLogSFtl(bool exp, CCStr fmt, ...) SQMOD_FORMAT_ATTR(printf, 2, 3);
/* ------------------------------------------------------------------------------------------------
* Get a persistent AABB instance with the given values.
*/
extern const AABB & GetAABB();
extern const AABB & GetAABB(Float32 mins, Float32 maxs);
extern const AABB & GetAABB(Float32 xv, Float32 yv, Float32 zv);
extern const AABB & GetAABB(Float32 xmin, Float32 ymin, Float32 zmin, Float32 xmax, Float32 ymax, Float32 zmax);
extern const AABB & GetAABB(const Vector3 & vmin, const Vector3 & vmax);
extern const AABB & GetAABB(const AABB & o);
extern const AABB & GetAABB(AABB && o);
/* ------------------------------------------------------------------------------------------------
* Get a persistent Circle instance with the given values.
*/
extern const Circle & GetCircle();
extern const Circle & GetCircle(Float32 rv);
extern const Circle & GetCircle(const Vector2 & pv, Float32 rv);
extern const Circle & GetCircle(Float32 xv, Float32 yv, Float32 rv);
extern const Circle & GetCircle(const Circle & o);
extern const Circle & GetCircle(Circle && o);
/* ------------------------------------------------------------------------------------------------
* Get a persistent Color3 instance with the given values.
*/
extern const Color3 & GetColor3();
extern const Color3 & GetColor3(Uint8 sv);
extern const Color3 & GetColor3(Uint8 rv, Uint8 gv, Uint8 bv);
extern const Color3 & GetColor3(const Color3 & o);
extern const Color3 & GetColor3(Color3 && o);
/* ------------------------------------------------------------------------------------------------
* Get a persistent Color4 instance with the given values.
*/
extern const Color4 & GetColor4();
extern const Color4 & GetColor4(Uint8 sv);
extern const Color4 & GetColor4(Uint8 rv, Uint8 gv, Uint8 bv);
extern const Color4 & GetColor4(Uint8 rv, Uint8 gv, Uint8 bv, Uint8 av);
extern const Color4 & GetColor4(const Color4 & o);
extern const Color4 & GetColor4(Color4 && o);
/* ------------------------------------------------------------------------------------------------
* Get a persistent Quaternion instance with the given values.
*/
extern const Quaternion & GetQuaternion();
extern const Quaternion & GetQuaternion(Float32 sv);
extern const Quaternion & GetQuaternion(Float32 xv, Float32 yv, Float32 zv);
extern const Quaternion & GetQuaternion(Float32 xv, Float32 yv, Float32 zv, Float32 wv);
extern const Quaternion & GetQuaternion(const Quaternion & o);
extern const Quaternion & GetQuaternion(Quaternion && o);
/* ------------------------------------------------------------------------------------------------
* Get a persistent Sphere instance with the given values.
*/
extern const Sphere & GetSphere();
extern const Sphere & GetSphere(Float32 rv);
extern const Sphere & GetSphere(const Vector3 & pv, Float32 rv);
extern const Sphere & GetSphere(Float32 xv, Float32 yv, Float32 zv, Float32 rv);
extern const Sphere & GetSphere(const Sphere & o);
extern const Sphere & GetSphere(Sphere && o);
/* ------------------------------------------------------------------------------------------------
* Get a persistent Vector2 instance with the given values.
*/
extern const Vector2 & GetVector2();
extern const Vector2 & GetVector2(Float32 sv);
extern const Vector2 & GetVector2(Float32 xv, Float32 yv);
extern const Vector2 & GetVector2(const Vector2 & o);
extern const Vector2 & GetVector2(Vector2 && o);
/* ------------------------------------------------------------------------------------------------
* Get a persistent Vector2i instance with the given values.
*/
extern const Vector2i & GetVector2i();
extern const Vector2i & GetVector2i(Int32 sv);
extern const Vector2i & GetVector2i(Int32 xv, Int32 yv);
extern const Vector2i & GetVector2i(const Vector2i & o);
extern const Vector2i & GetVector2i(Vector2i && o);
/* ------------------------------------------------------------------------------------------------
* Get a persistent Vector3 instance with the given values.
*/
extern const Vector3 & GetVector3();
extern const Vector3 & GetVector3(Float32 sv);
extern const Vector3 & GetVector3(Float32 xv, Float32 yv, Float32 zv);
extern const Vector3 & GetVector3(const Vector3 & o);
extern const Vector3 & GetVector3(Vector3 && o);
/* ------------------------------------------------------------------------------------------------
* Get a persistent Vector4 instance with the given values.
*/
extern const Vector4 & GetVector4();
extern const Vector4 & GetVector4(Float32 sv);
extern const Vector4 & GetVector4(Float32 xv, Float32 yv, Float32 zv);
extern const Vector4 & GetVector4(Float32 xv, Float32 yv, Float32 zv, Float32 wv);
extern const Vector4 & GetVector4(const Vector4 & o);
extern const Vector4 & GetVector4(Vector4 && o);
/* ------------------------------------------------------------------------------------------------
* Get a persistent LongInt instance with the given values.
*/
const SLongInt & GetSLongInt();
const SLongInt & GetSLongInt(Int64 n);
const SLongInt & GetSLongInt(CSStr s);
const ULongInt & GetULongInt();
const ULongInt & GetULongInt(Uint64 n);
const ULongInt & GetULongInt(CSStr s);
/* ------------------------------------------------------------------------------------------------
* Initialize a signal instance into the specified pair.
*/
extern void InitSignalPair(SignalPair & sp, LightObj & et, const char * name);
/* ------------------------------------------------------------------------------------------------
* Reset/release the specified signal pair.
*/
extern void ResetSignalPair(SignalPair & sp, bool clear = true);
/* ------------------------------------------------------------------------------------------------
* A simple implementation of name filtering.
*/
bool NameFilterCheck(CSStr filter, CSStr name);
/* ------------------------------------------------------------------------------------------------
* A simple implementation of name filtering without case sensitivity.
*/
bool NameFilterCheckInsensitive(CSStr filter, CSStr name);
/* ------------------------------------------------------------------------------------------------
* Obtain a randomly chosen color from a list of known colors.
*/
const Color3 & GetRandomColor();
/* ------------------------------------------------------------------------------------------------
* Attempt to identify the color in the specified name and return it.
*/
Color3 GetColorStr(CSStr name);
/* ------------------------------------------------------------------------------------------------
* Attempt to identify the color in the specified name and return it.
*/
Color3 GetColor(StackStrF & name);
/* ------------------------------------------------------------------------------------------------
* Throw the last system error as an exception.
*/
void SqThrowLastF(CSStr msg, ...);
/* ------------------------------------------------------------------------------------------------
* Retrieve the string delimiter of a base type.
*/
template < typename T > inline SQInteger SqGetDelimiter()
{
return T::Delim;
}
/* ------------------------------------------------------------------------------------------------
* Modify the string delimiter of a base type.
*/
template < typename T > inline void SqSetDelimiter(SQInteger c)
{
T::Delim = ConvTo< SQChar >::From(c);
}
} // Namespace:: SqMod
#endif // _BASE_SHARED_HPP_

634
module/Base/Sphere.cpp Normal file
View File

@ -0,0 +1,634 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Sphere.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Sphere"))
// ------------------------------------------------------------------------------------------------
const Sphere Sphere::NIL = Sphere();
const Sphere Sphere::MIN = Sphere(0.0);
const Sphere Sphere::MAX = Sphere(std::numeric_limits< Sphere::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Sphere::Delim = ',';
// ------------------------------------------------------------------------------------------------
Sphere::Sphere()
: pos(0.0), rad(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere::Sphere(Value rv)
: pos(0.0), rad(rv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere::Sphere(const Vector3 & pv, Value rv)
: pos(pv), rad(rv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere::Sphere(Value xv, Value yv, Value zv, Value rv)
: pos(xv, yv, zv), rad(rv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator = (Value r)
{
rad = r;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator = (const Vector3 & p)
{
pos = p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator += (const Sphere & s)
{
pos += s.pos;
rad += s.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator -= (const Sphere & s)
{
pos -= s.pos;
rad -= s.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator *= (const Sphere & s)
{
pos *= s.pos;
rad *= s.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator /= (const Sphere & s)
{
pos /= s.pos;
rad /= s.rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator %= (const Sphere & s)
{
pos %= s.pos;
rad = std::fmod(rad, s.rad);
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator += (Value r)
{
rad += r;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator -= (Value r)
{
rad -= r;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator *= (Value r)
{
rad *= r;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator /= (Value r)
{
rad /= r;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator %= (Value r)
{
rad = std::fmod(rad, r);
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator += (const Vector3 & p)
{
pos += p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator -= (const Vector3 & p)
{
pos -= p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator *= (const Vector3 & p)
{
pos *= p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator /= (const Vector3 & p)
{
pos /= p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator %= (const Vector3 & p)
{
pos %= p;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator ++ ()
{
++pos;
++rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere & Sphere::operator -- ()
{
--pos;
--rad;
return *this;
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator ++ (int)
{
Sphere state(*this);
++pos;
++rad;
return state;
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator -- (int)
{
Sphere state(*this);
--pos;
--rad;
return state;
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator + (const Sphere & s) const
{
return Sphere(pos + s.pos, rad + s.rad);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator - (const Sphere & s) const
{
return Sphere(pos - s.pos, rad - s.rad);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator * (const Sphere & s) const
{
return Sphere(pos * s.pos, rad * s.rad);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator / (const Sphere & s) const
{
return Sphere(pos / s.pos, rad / s.rad);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator % (const Sphere & s) const
{
return Sphere(pos % s.pos, std::fmod(rad, s.rad));
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator + (Value r) const
{
return Sphere(rad + r);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator - (Value r) const
{
return Sphere(rad - r);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator * (Value r) const
{
return Sphere(rad * r);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator / (Value r) const
{
return Sphere(rad / r);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator % (Value r) const
{
return Sphere(std::fmod(rad, r));
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator + (const Vector3 & p) const
{
return Sphere(pos + p, rad);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator - (const Vector3 & p) const
{
return Sphere(pos - p, rad);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator * (const Vector3 & p) const
{
return Sphere(pos * p, rad);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator / (const Vector3 & p) const
{
return Sphere(pos / p, rad);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator % (const Vector3 & p) const
{
return Sphere(pos % p, rad);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator + () const
{
return Sphere(pos.Abs(), std::fabs(rad));
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::operator - () const
{
return Sphere(-pos, -rad);
}
// ------------------------------------------------------------------------------------------------
bool Sphere::operator == (const Sphere & s) const
{
return EpsEq(rad, s.rad) && (pos == s.pos);
}
// ------------------------------------------------------------------------------------------------
bool Sphere::operator != (const Sphere & s) const
{
return !EpsEq(rad, s.rad) || (pos != s.pos);
}
// ------------------------------------------------------------------------------------------------
bool Sphere::operator < (const Sphere & s) const
{
return EpsLt(rad, s.rad) && (pos < s.pos);
}
// ------------------------------------------------------------------------------------------------
bool Sphere::operator > (const Sphere & s) const
{
return EpsGt(rad, s.rad) && (pos > s.pos);
}
// ------------------------------------------------------------------------------------------------
bool Sphere::operator <= (const Sphere & s) const
{
return EpsLtEq(rad, s.rad) && (pos <= s.pos);
}
// ------------------------------------------------------------------------------------------------
bool Sphere::operator >= (const Sphere & s) const
{
return EpsGtEq(rad, s.rad) && (pos >= s.pos);
}
// ------------------------------------------------------------------------------------------------
Int32 Sphere::Cmp(const Sphere & o) const
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
CSStr Sphere::ToString() const
{
return ToStrF("%f,%f,%f,%f", pos.x, pos.y, pos.z, rad);
}
// ------------------------------------------------------------------------------------------------
void Sphere::SetRadius(Value nr)
{
rad = nr;
}
// ------------------------------------------------------------------------------------------------
void Sphere::SetSphere(const Sphere & ns)
{
pos = ns.pos;
rad = ns.rad;
}
// ------------------------------------------------------------------------------------------------
void Sphere::SetSphereEx(Value nx, Value ny, Value nz, Value nr)
{
pos.SetVector3Ex(nx, ny, nz);
rad = nr;
}
// ------------------------------------------------------------------------------------------------
void Sphere::SetValues(const Vector3 & np, Value nr)
{
pos = np;
rad = nr;
}
// ------------------------------------------------------------------------------------------------
void Sphere::SetPosition(const Vector3 & np)
{
pos = np;
}
// ------------------------------------------------------------------------------------------------
void Sphere::SetPositionEx(Value nx, Value ny, Value nz)
{
pos.SetVector3Ex(nx, ny, nz);
}
// ------------------------------------------------------------------------------------------------
void Sphere::SetStr(SQChar delim, StackStrF & values)
{
SetSphere(Sphere::GetEx(delim, values));
}
// ------------------------------------------------------------------------------------------------
void Sphere::Generate()
{
pos.Generate();
rad = GetRandomFloat32();
}
// ------------------------------------------------------------------------------------------------
void Sphere::Generate(Value min, Value max, bool r)
{
if (EpsLt(max, min))
{
STHROWF("max value is lower than min value");
}
else if (r)
{
rad = GetRandomFloat32(min, max);
}
else
{
pos.Generate(min, max);
}
}
// ------------------------------------------------------------------------------------------------
void Sphere::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin))
{
STHROWF("max value is lower than min value");
}
pos.Generate(xmin, xmax, ymin, ymax, zmin, zmax);
}
// ------------------------------------------------------------------------------------------------
void Sphere::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value rmin, Value rmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin) || EpsLt(rmax, rmin))
{
STHROWF("max value is lower than min value");
}
pos.Generate(xmin, xmax, ymin, ymax, zmin, zmax);
rad = GetRandomFloat32(rmin, rmax);
}
// ------------------------------------------------------------------------------------------------
Sphere Sphere::Abs() const
{
return Sphere(pos.Abs(), std::fabs(rad));
}
// ------------------------------------------------------------------------------------------------
const Sphere & Sphere::Get(StackStrF & str)
{
return Sphere::GetEx(Sphere::Delim, str);
}
// ------------------------------------------------------------------------------------------------
const Sphere & Sphere::GetEx(SQChar delim, StackStrF & str)
{
// The format specifications that will be used to scan the string
static SQChar fs[] = _SC(" %f , %f , %f , %f ");
static Sphere sphere;
// Clear previous values, if any
sphere.Clear();
// Is the specified string empty?
if (str.mLen <= 0)
{
return sphere; // 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.mPtr, fs, &sphere.pos.x, &sphere.pos.y, &sphere.pos.z, &sphere.rad);
// Return the resulted value
return sphere;
}
// ------------------------------------------------------------------------------------------------
const Sphere & GetSphere()
{
static Sphere sphere;
sphere.Clear();
return sphere;
}
// ------------------------------------------------------------------------------------------------
const Sphere & GetSphere(Float32 rv)
{
static Sphere sphere;
sphere.SetRadius(rv);
return sphere;
}
// ------------------------------------------------------------------------------------------------
const Sphere & GetSphere(const Vector3 & pv, Float32 rv)
{
static Sphere sphere;
sphere.SetValues(pv, rv);
return sphere;
}
// ------------------------------------------------------------------------------------------------
const Sphere & GetSphere(Float32 xv, Float32 yv, Float32 zv, Float32 rv)
{
static Sphere sphere;
sphere.SetSphereEx(xv, yv, zv, rv);
return sphere;
}
// ------------------------------------------------------------------------------------------------
const Sphere & GetSphere(const Sphere & o)
{
static Sphere sphere;
sphere.SetSphere(o);
return sphere;
}
// ================================================================================================
void Register_Sphere(HSQUIRRELVM vm)
{
typedef Sphere::Value Val;
RootTable(vm).Bind(Typename::Str,
Class< Sphere >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< Val >()
.Ctor< const Vector3 &, Val >()
.Ctor< Val, Val, Val, Val >()
// Member Variables
.Var(_SC("pos"), &Sphere::pos)
.Var(_SC("rad"), &Sphere::rad)
.Var(_SC("Pos"), &Sphere::pos)
.Var(_SC("Rad"), &Sphere::rad)
// Core Meta-methods
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Sphere::ToString)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< Sphere >, SQFloat, SQInteger, bool, std::nullptr_t, Sphere >)
.Func< Sphere (Sphere::*)(void) const >(_SC("_unm"), &Sphere::operator -)
// Properties
.Prop(_SC("Abs"), &Sphere::Abs)
// Member Methods
.Func(_SC("SetRadius"), &Sphere::SetRadius)
.Func(_SC("SetSphere"), &Sphere::SetSphere)
.Func(_SC("SetSphereEx"), &Sphere::SetSphereEx)
.Func(_SC("SetValues"), &Sphere::SetValues)
.Func(_SC("SetPosition"), &Sphere::SetPosition)
.Func(_SC("SetPositionEx"), &Sphere::SetPositionEx)
.FmtFunc(_SC("SetStr"), &Sphere::SetStr)
.Func(_SC("Clear"), &Sphere::Clear)
// Member Overloads
.Overload< void (Sphere::*)(void) >(_SC("Generate"), &Sphere::Generate)
.Overload< void (Sphere::*)(Val, Val, bool) >(_SC("Generate"), &Sphere::Generate)
.Overload< void (Sphere::*)(Val, Val, Val, Val, Val, Val) >(_SC("Generate"), &Sphere::Generate)
.Overload< void (Sphere::*)(Val, Val, Val, Val, Val, Val, Val, Val) >(_SC("Generate"), &Sphere::Generate)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Sphere >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Sphere >)
.StaticFmtFunc(_SC("FromStr"), &Sphere::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Sphere::GetEx)
// Operator Exposure
.Func< Sphere & (Sphere::*)(const Sphere &) >(_SC("opAddAssign"), &Sphere::operator +=)
.Func< Sphere & (Sphere::*)(const Sphere &) >(_SC("opSubAssign"), &Sphere::operator -=)
.Func< Sphere & (Sphere::*)(const Sphere &) >(_SC("opMulAssign"), &Sphere::operator *=)
.Func< Sphere & (Sphere::*)(const Sphere &) >(_SC("opDivAssign"), &Sphere::operator /=)
.Func< Sphere & (Sphere::*)(const Sphere &) >(_SC("opModAssign"), &Sphere::operator %=)
.Func< Sphere & (Sphere::*)(Sphere::Value) >(_SC("opAddAssignR"), &Sphere::operator +=)
.Func< Sphere & (Sphere::*)(Sphere::Value) >(_SC("opSubAssignR"), &Sphere::operator -=)
.Func< Sphere & (Sphere::*)(Sphere::Value) >(_SC("opMulAssignR"), &Sphere::operator *=)
.Func< Sphere & (Sphere::*)(Sphere::Value) >(_SC("opDivAssignR"), &Sphere::operator /=)
.Func< Sphere & (Sphere::*)(Sphere::Value) >(_SC("opModAssignR"), &Sphere::operator %=)
.Func< Sphere & (Sphere::*)(const Vector3 &) >(_SC("opAddAssignP"), &Sphere::operator +=)
.Func< Sphere & (Sphere::*)(const Vector3 &) >(_SC("opSubAssignP"), &Sphere::operator -=)
.Func< Sphere & (Sphere::*)(const Vector3 &) >(_SC("opMulAssignP"), &Sphere::operator *=)
.Func< Sphere & (Sphere::*)(const Vector3 &) >(_SC("opDivAssignP"), &Sphere::operator /=)
.Func< Sphere & (Sphere::*)(const Vector3 &) >(_SC("opModAssignP"), &Sphere::operator %=)
.Func< Sphere & (Sphere::*)(void) >(_SC("opPreInc"), &Sphere::operator ++)
.Func< Sphere & (Sphere::*)(void) >(_SC("opPreDec"), &Sphere::operator --)
.Func< Sphere (Sphere::*)(int) >(_SC("opPostInc"), &Sphere::operator ++)
.Func< Sphere (Sphere::*)(int) >(_SC("opPostDec"), &Sphere::operator --)
.Func< Sphere (Sphere::*)(const Sphere &) const >(_SC("opAdd"), &Sphere::operator +)
.Func< Sphere (Sphere::*)(const Sphere &) const >(_SC("opSub"), &Sphere::operator -)
.Func< Sphere (Sphere::*)(const Sphere &) const >(_SC("opMul"), &Sphere::operator *)
.Func< Sphere (Sphere::*)(const Sphere &) const >(_SC("opDiv"), &Sphere::operator /)
.Func< Sphere (Sphere::*)(const Sphere &) const >(_SC("opMod"), &Sphere::operator %)
.Func< Sphere (Sphere::*)(Sphere::Value) const >(_SC("opAddR"), &Sphere::operator +)
.Func< Sphere (Sphere::*)(Sphere::Value) const >(_SC("opSubR"), &Sphere::operator -)
.Func< Sphere (Sphere::*)(Sphere::Value) const >(_SC("opMulR"), &Sphere::operator *)
.Func< Sphere (Sphere::*)(Sphere::Value) const >(_SC("opDivR"), &Sphere::operator /)
.Func< Sphere (Sphere::*)(Sphere::Value) const >(_SC("opModR"), &Sphere::operator %)
.Func< Sphere (Sphere::*)(const Vector3 &) const >(_SC("opAddP"), &Sphere::operator +)
.Func< Sphere (Sphere::*)(const Vector3 &) const >(_SC("opSubP"), &Sphere::operator -)
.Func< Sphere (Sphere::*)(const Vector3 &) const >(_SC("opMulP"), &Sphere::operator *)
.Func< Sphere (Sphere::*)(const Vector3 &) const >(_SC("opDivP"), &Sphere::operator /)
.Func< Sphere (Sphere::*)(const Vector3 &) const >(_SC("opModP"), &Sphere::operator %)
.Func< Sphere (Sphere::*)(void) const >(_SC("opUnPlus"), &Sphere::operator +)
.Func< Sphere (Sphere::*)(void) const >(_SC("opUnMinus"), &Sphere::operator -)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opEqual"), &Sphere::operator ==)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opNotEqual"), &Sphere::operator !=)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opLessThan"), &Sphere::operator <)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opGreaterThan"), &Sphere::operator >)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opLessEqual"), &Sphere::operator <=)
.Func< bool (Sphere::*)(const Sphere &) const >(_SC("opGreaterEqual"), &Sphere::operator >=)
);
}
} // Namespace:: SqMod

427
module/Base/Sphere.hpp Normal file
View File

@ -0,0 +1,427 @@
#ifndef _BASE_SPHERE_HPP_
#define _BASE_SPHERE_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
#include "Base/Vector3.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Class used to represent a three-dimensional sphere.
*/
struct Sphere
{
/* --------------------------------------------------------------------------------------------
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* Helper instances for common values mostly used as return types or comparison.
*/
static const Sphere NIL;
static const Sphere MIN;
static const Sphere MAX;
/* --------------------------------------------------------------------------------------------
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* The position and radius components of this type.
*/
Vector3 pos;
Value rad;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Sphere();
/* --------------------------------------------------------------------------------------------
* Construct a sphere at position 0,0,0 using the specified radius.
*/
explicit Sphere(Value rv);
/* --------------------------------------------------------------------------------------------
* Construct a sphere at the specified position using the specified radius.
*/
Sphere(const Vector3 & pv, Value rv);
/* --------------------------------------------------------------------------------------------
* Construct a sphere at the specified position using the specified radius.
*/
Sphere(Value xv, Value yv, Value zv, Value rv);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Sphere(const Sphere & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Sphere(Sphere && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Sphere() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Sphere & operator = (const Sphere & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Sphere & operator = (Sphere && o) = default;
/* --------------------------------------------------------------------------------------------
* Radius assignment operator.
*/
Sphere & operator = (Value r);
/* --------------------------------------------------------------------------------------------
* Position assignment operator.
*/
Sphere & operator = (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* Addition assignment operator.
*/
Sphere & operator += (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* Subtraction assignment operator.
*/
Sphere & operator -= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* Multiplication assignment operator.
*/
Sphere & operator *= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* Division assignment operator.
*/
Sphere & operator /= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* Modulo assignment operator.
*/
Sphere & operator %= (const Sphere & s);
/* --------------------------------------------------------------------------------------------
* Radius addition assignment operator.
*/
Sphere & operator += (Value r);
/* --------------------------------------------------------------------------------------------
* Radius subtraction assignment operator.
*/
Sphere & operator -= (Value r);
/* --------------------------------------------------------------------------------------------
* Radius multiplication assignment operator.
*/
Sphere & operator *= (Value r);
/* --------------------------------------------------------------------------------------------
* Radius division assignment operator.
*/
Sphere & operator /= (Value r);
/* --------------------------------------------------------------------------------------------
* Radius modulo assignment operator.
*/
Sphere & operator %= (Value r);
/* --------------------------------------------------------------------------------------------
* Position addition assignment operator.
*/
Sphere & operator += (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* Position subtraction assignment operator.
*/
Sphere & operator -= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* Position multiplication assignment operator.
*/
Sphere & operator *= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* Position division assignment operator.
*/
Sphere & operator /= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* Position modulo assignment operator.
*/
Sphere & operator %= (const Vector3 & p);
/* --------------------------------------------------------------------------------------------
* Pre-increment operator.
*/
Sphere & operator ++ ();
/* --------------------------------------------------------------------------------------------
* Pre-decrement operator.
*/
Sphere & operator -- ();
/* --------------------------------------------------------------------------------------------
* Post-increment operator.
*/
Sphere operator ++ (int);
/* --------------------------------------------------------------------------------------------
* Post-decrement operator.
*/
Sphere operator -- (int);
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
Sphere operator + (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
Sphere operator - (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
Sphere operator * (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
Sphere operator / (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Modulo operator.
*/
Sphere operator % (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Radius addition operator.
*/
Sphere operator + (Value r) const;
/* --------------------------------------------------------------------------------------------
* Radius subtraction operator.
*/
Sphere operator - (Value r) const;
/* --------------------------------------------------------------------------------------------
* Radius multiplication operator.
*/
Sphere operator * (Value r) const;
/* --------------------------------------------------------------------------------------------
* Radius division operator.
*/
Sphere operator / (Value r) const;
/* --------------------------------------------------------------------------------------------
* Radius modulo operator.
*/
Sphere operator % (Value r) const;
/* --------------------------------------------------------------------------------------------
* Position addition operator.
*/
Sphere operator + (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* Position subtraction operator.
*/
Sphere operator - (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* Position multiplication operator.
*/
Sphere operator * (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* Position division operator.
*/
Sphere operator / (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* Position modulo operator.
*/
Sphere operator % (const Vector3 & p) const;
/* --------------------------------------------------------------------------------------------
* Unary plus operator.
*/
Sphere operator + () const;
/* --------------------------------------------------------------------------------------------
* Unary minus operator.
*/
Sphere operator - () const;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Sphere & s) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
{
return Cmp(Sphere(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
{
return Cmp(Sphere(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
{
return Cmp(Sphere(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
{
return Cmp(Sphere(static_cast< Value >(0)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Set the specified radius.
*/
void SetRadius(Value nr);
/* --------------------------------------------------------------------------------------------
* Copy the sphere from another instance of this type.
*/
void SetSphere(const Sphere & ns);
/* --------------------------------------------------------------------------------------------
* Set the specified position and radius.
*/
void SetSphereEx(Value nx, Value ny, Value nz, Value nr);
/* --------------------------------------------------------------------------------------------
* Set the specified position and radius.
*/
void SetValues(const Vector3 & np, Value nr);
/* --------------------------------------------------------------------------------------------
* Set the position from the specified position.
*/
void SetPosition(const Vector3 & np);
/* --------------------------------------------------------------------------------------------
* Set the specified position.
*/
void SetPositionEx(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/
void SetStr(SQChar delim, StackStrF & values);
/* --------------------------------------------------------------------------------------------
* Generate a randomly sized and positioned sphere.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* Generate a randomly sized or positioned sphere within the specified bounds.
*/
void Generate(Value min, Value max, bool r);
/* --------------------------------------------------------------------------------------------
* Generate a randomly positioned sphere within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax);
/* --------------------------------------------------------------------------------------------
* Generate a randomly sized and positioned sphere within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value rmin, Value rmax);
/* --------------------------------------------------------------------------------------------
* Clear the component values to default.
*/
void Clear()
{
pos.Clear(); rad = 0.0;
}
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Sphere Abs() const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Sphere type from a string.
*/
static const Sphere & Get(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Sphere type from a string.
*/
static const Sphere & GetEx(SQChar delim, StackStrF & str);
};
} // Namespace:: SqMod
#endif // _BASE_SPHERE_HPP_

1087
module/Base/Utility.cpp Normal file

File diff suppressed because it is too large Load Diff

1522
module/Base/Utility.hpp Normal file

File diff suppressed because it is too large Load Diff

518
module/Base/Vector2.cpp Normal file
View File

@ -0,0 +1,518 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Vector2.hpp"
#include "Base/Vector2i.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Vector2"))
// ------------------------------------------------------------------------------------------------
const Vector2 Vector2::NIL = Vector2(0);
const Vector2 Vector2::MIN = Vector2(std::numeric_limits< Vector2::Value >::min());
const Vector2 Vector2::MAX = Vector2(std::numeric_limits< Vector2::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Vector2::Delim = ',';
// ------------------------------------------------------------------------------------------------
Vector2::Vector2()
: x(0.0), y(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2::Vector2(Value sv)
: x(sv), y(sv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2::Vector2(Value xv, Value yv)
: x(xv), y(yv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator = (Value s)
{
x = s;
y = s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator = (const Vector2i & v)
{
x = ConvTo< Value >::From(v.x);
y = ConvTo< Value >::From(v.y);
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator += (const Vector2 & v)
{
x += v.x;
y += v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator -= (const Vector2 & v)
{
x -= v.x;
y -= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator *= (const Vector2 & v)
{
x *= v.x;
y *= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator /= (const Vector2 & v)
{
x /= v.x;
y /= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator %= (const Vector2 & v)
{
x = std::fmod(x, v.x);
y = std::fmod(y, v.y);
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator += (Value s)
{
x += s;
y += s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator -= (Value s)
{
x -= s;
y -= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator *= (Value s)
{
x *= s;
y *= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator /= (Value s)
{
x /= s;
y /= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator %= (Value s)
{
x = std::fmod(x, s);
y = std::fmod(y, s);
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator ++ ()
{
++x;
++y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 & Vector2::operator -- ()
{
--x;
--y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator ++ (int)
{
Vector2 state(*this);
++x;
++y;
return state;
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator -- (int)
{
Vector2 state(*this);
--x;
--y;
return state;
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator + (const Vector2 & v) const
{
return Vector2(x + v.x, y + v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator - (const Vector2 & v) const
{
return Vector2(x - v.x, y - v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator * (const Vector2 & v) const
{
return Vector2(x * v.x, y * v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator / (const Vector2 & v) const
{
return Vector2(x / v.x, y / v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator % (const Vector2 & v) const
{
return Vector2(std::fmod(x, v.x), std::fmod(y, v.y));
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator + (Value s) const
{
return Vector2(x + s, y + s);
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator - (Value s) const
{
return Vector2(x - s, y - s);
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator * (Value s) const
{
return Vector2(x * s, y * s);
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator / (Value s) const
{
return Vector2(x / s, y / s);
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator % (Value s) const
{
return Vector2(std::fmod(x, s), std::fmod(y, s));
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator + () const
{
return Vector2(std::fabs(x), std::fabs(y));
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::operator - () const
{
return Vector2(-x, -y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2::operator == (const Vector2 & v) const
{
return EpsEq(x, v.x) && EpsEq(y, v.y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2::operator != (const Vector2 & v) const
{
return !EpsEq(x, v.x) || !EpsEq(y, v.y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2::operator < (const Vector2 & v) const
{
return EpsLt(x, v.x) && EpsLt(y, v.y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2::operator > (const Vector2 & v) const
{
return EpsGt(x, v.x) && EpsGt(y, v.y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2::operator <= (const Vector2 & v) const
{
return EpsLtEq(x, v.x) && EpsLtEq(y, v.y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2::operator >= (const Vector2 & v) const
{
return EpsGtEq(x, v.x) && EpsGtEq(y, v.y);
}
// ------------------------------------------------------------------------------------------------
Int32 Vector2::Cmp(const Vector2 & o) const
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
CSStr Vector2::ToString() const
{
return ToStrF("%f,%f", x, y);
}
// ------------------------------------------------------------------------------------------------
void Vector2::SetScalar(Value ns)
{
x = ns;
y = ns;
}
// ------------------------------------------------------------------------------------------------
void Vector2::SetVector2(const Vector2 & v)
{
x = v.x;
y = v.y;
}
// ------------------------------------------------------------------------------------------------
void Vector2::SetVector2Ex(Value nx, Value ny)
{
x = nx;
y = ny;
}
// ------------------------------------------------------------------------------------------------
void Vector2::SetVector2i(const Vector2i & v)
{
x = ConvTo< Value >::From(v.x);
y = ConvTo< Value >::From(v.y);
}
// ------------------------------------------------------------------------------------------------
void Vector2::SetStr(SQChar delim, StackStrF & values)
{
SetVector2(Vector2::GetEx(delim, values));
}
// ------------------------------------------------------------------------------------------------
void Vector2::Generate()
{
x = GetRandomFloat32();
y = GetRandomFloat32();
}
// ------------------------------------------------------------------------------------------------
void Vector2::Generate(Value min, Value max)
{
if (EpsLt(max, min))
{
STHROWF("max value is lower than min value");
}
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
}
// ------------------------------------------------------------------------------------------------
void Vector2::Generate(Value xmin, Value xmax, Value ymin, Value ymax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin))
{
STHROWF("max value is lower than min value");
}
x = GetRandomFloat32(ymin, ymax);
y = GetRandomFloat32(xmin, xmax);
}
// ------------------------------------------------------------------------------------------------
Vector2 Vector2::Abs() const
{
return Vector2(std::fabs(x), std::fabs(y));
}
// ------------------------------------------------------------------------------------------------
const Vector2 & Vector2::Get(StackStrF & str)
{
return Vector2::GetEx(Vector2::Delim, str);
}
// ------------------------------------------------------------------------------------------------
const Vector2 & Vector2::GetEx(SQChar delim, StackStrF & str)
{
// The format specifications that will be used to scan the string
static SQChar fs[] = _SC(" %f , %f ");
static Vector2 vec;
// Clear previous values, if any
vec.Clear();
// Is the specified string empty?
if (str.mLen <= 0)
{
return vec; // Return the value as is!
}
// Assign the specified delimiter
fs[4] = delim;
// Attempt to extract the component values from the specified string
std::sscanf(str.mPtr, fs, &vec.x, &vec.y);
// Return the resulted value
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector2 & GetVector2()
{
static Vector2 vec;
vec.Clear();
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector2 & GetVector2(Float32 sv)
{
static Vector2 vec;
vec.SetScalar(sv);
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector2 & GetVector2(Float32 xv, Float32 yv)
{
static Vector2 vec;
vec.SetVector2Ex(xv, yv);
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector2 & GetVector2(const Vector2 & o)
{
static Vector2 vec;
vec.SetVector2(o);
return vec;
}
// ================================================================================================
void Register_Vector2(HSQUIRRELVM vm)
{
typedef Vector2::Value Val;
RootTable(vm).Bind(Typename::Str,
Class< Vector2 >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< Val >()
.Ctor< Val, Val >()
// Member Variables
.Var(_SC("x"), &Vector2::x)
.Var(_SC("y"), &Vector2::y)
.Var(_SC("X"), &Vector2::x)
.Var(_SC("Y"), &Vector2::y)
// Core Meta-methods
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector2::ToString)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< Vector2 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2 >)
.Func< Vector2 (Vector2::*)(void) const >(_SC("_unm"), &Vector2::operator -)
// Properties
.Prop(_SC("Abs"), &Vector2::Abs)
// Member Methods
.Func(_SC("SetScalar"), &Vector2::SetScalar)
.Func(_SC("SetVector2"), &Vector2::SetVector2)
.Func(_SC("SetVector2Ex"), &Vector2::SetVector2Ex)
.Func(_SC("SetVector2i"), &Vector2::SetVector2i)
.FmtFunc(_SC("SetStr"), &Vector2::SetStr)
.Func(_SC("Clear"), &Vector2::Clear)
// Member Overloads
.Overload< void (Vector2::*)(void) >(_SC("Generate"), &Vector2::Generate)
.Overload< void (Vector2::*)(Val, Val) >(_SC("Generate"), &Vector2::Generate)
.Overload< void (Vector2::*)(Val, Val, Val, Val) >(_SC("Generate"), &Vector2::Generate)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Vector2 >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Vector2 >)
.StaticFmtFunc(_SC("FromStr"), &Vector2::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Vector2::GetEx)
// Operator Exposure
.Func< Vector2 & (Vector2::*)(const Vector2 &) >(_SC("opAddAssign"), &Vector2::operator +=)
.Func< Vector2 & (Vector2::*)(const Vector2 &) >(_SC("opSubAssign"), &Vector2::operator -=)
.Func< Vector2 & (Vector2::*)(const Vector2 &) >(_SC("opMulAssign"), &Vector2::operator *=)
.Func< Vector2 & (Vector2::*)(const Vector2 &) >(_SC("opDivAssign"), &Vector2::operator /=)
.Func< Vector2 & (Vector2::*)(const Vector2 &) >(_SC("opModAssign"), &Vector2::operator %=)
.Func< Vector2 & (Vector2::*)(Vector2::Value) >(_SC("opAddAssignS"), &Vector2::operator +=)
.Func< Vector2 & (Vector2::*)(Vector2::Value) >(_SC("opSubAssignS"), &Vector2::operator -=)
.Func< Vector2 & (Vector2::*)(Vector2::Value) >(_SC("opMulAssignS"), &Vector2::operator *=)
.Func< Vector2 & (Vector2::*)(Vector2::Value) >(_SC("opDivAssignS"), &Vector2::operator /=)
.Func< Vector2 & (Vector2::*)(Vector2::Value) >(_SC("opModAssignS"), &Vector2::operator %=)
.Func< Vector2 & (Vector2::*)(void) >(_SC("opPreInc"), &Vector2::operator ++)
.Func< Vector2 & (Vector2::*)(void) >(_SC("opPreDec"), &Vector2::operator --)
.Func< Vector2 (Vector2::*)(int) >(_SC("opPostInc"), &Vector2::operator ++)
.Func< Vector2 (Vector2::*)(int) >(_SC("opPostDec"), &Vector2::operator --)
.Func< Vector2 (Vector2::*)(const Vector2 &) const >(_SC("opAdd"), &Vector2::operator +)
.Func< Vector2 (Vector2::*)(const Vector2 &) const >(_SC("opSub"), &Vector2::operator -)
.Func< Vector2 (Vector2::*)(const Vector2 &) const >(_SC("opMul"), &Vector2::operator *)
.Func< Vector2 (Vector2::*)(const Vector2 &) const >(_SC("opDiv"), &Vector2::operator /)
.Func< Vector2 (Vector2::*)(const Vector2 &) const >(_SC("opMod"), &Vector2::operator %)
.Func< Vector2 (Vector2::*)(Vector2::Value) const >(_SC("opAddS"), &Vector2::operator +)
.Func< Vector2 (Vector2::*)(Vector2::Value) const >(_SC("opSubS"), &Vector2::operator -)
.Func< Vector2 (Vector2::*)(Vector2::Value) const >(_SC("opMulS"), &Vector2::operator *)
.Func< Vector2 (Vector2::*)(Vector2::Value) const >(_SC("opDivS"), &Vector2::operator /)
.Func< Vector2 (Vector2::*)(Vector2::Value) const >(_SC("opModS"), &Vector2::operator %)
.Func< Vector2 (Vector2::*)(void) const >(_SC("opUnPlus"), &Vector2::operator +)
.Func< Vector2 (Vector2::*)(void) const >(_SC("opUnMinus"), &Vector2::operator -)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opEqual"), &Vector2::operator ==)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opNotEqual"), &Vector2::operator !=)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opLessThan"), &Vector2::operator <)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opGreaterThan"), &Vector2::operator >)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opLessEqual"), &Vector2::operator <=)
.Func< bool (Vector2::*)(const Vector2 &) const >(_SC("opGreaterEqual"), &Vector2::operator >=)
);
}
} // Namespace:: SqMod

360
module/Base/Vector2.hpp Normal file
View File

@ -0,0 +1,360 @@
#ifndef _BASE_VECTOR2_HPP_
#define _BASE_VECTOR2_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Class used to represent a two-dimensional vector.
*/
struct Vector2
{
/* --------------------------------------------------------------------------------------------
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector2 NIL;
static const Vector2 MIN;
static const Vector2 MAX;
/* --------------------------------------------------------------------------------------------
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* The x and y components of this type.
*/
Value x, y;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Vector2();
/* --------------------------------------------------------------------------------------------
* Construct a vector with the same scalar value for all components.
*/
explicit Vector2(Value sv);
/* --------------------------------------------------------------------------------------------
* Construct a vector with the specified component values.
*/
Vector2(Value xv, Value yv);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Vector2(const Vector2 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Vector2(Vector2 && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Vector2() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Vector2 & operator = (const Vector2 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector2 & operator = (Vector2 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector2 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* String assignment operator.
*/
Vector2 & operator = (CSStr values);
/* --------------------------------------------------------------------------------------------
* Integral two-dimensional vector assignment.
*/
Vector2 & operator = (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Addition assignment operator.
*/
Vector2 & operator += (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* Subtraction assignment operator.
*/
Vector2 & operator -= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* Multiplication assignment operator.
*/
Vector2 & operator *= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* Division assignment operator.
*/
Vector2 & operator /= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* Modulo assignment operator.
*/
Vector2 & operator %= (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* Scalar value addition assignment operator.
*/
Vector2 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction assignment operator.
*/
Vector2 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication assignment operator.
*/
Vector2 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value division assignment operator.
*/
Vector2 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value modulo assignment operator.
*/
Vector2 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* Pre-increment operator.
*/
Vector2 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* Pre-decrement operator.
*/
Vector2 & operator -- ();
/* --------------------------------------------------------------------------------------------
* Post-increment operator.
*/
Vector2 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* Post-decrement operator.
*/
Vector2 operator -- (int);
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
Vector2 operator + (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
Vector2 operator - (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
Vector2 operator * (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
Vector2 operator / (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Modulo operator.
*/
Vector2 operator % (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Scalar value addition operator.
*/
Vector2 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction operator.
*/
Vector2 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication operator.
*/
Vector2 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value division operator.
*/
Vector2 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value modulo operator.
*/
Vector2 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* Unary plus operator.
*/
Vector2 operator + () const;
/* --------------------------------------------------------------------------------------------
* Unary minus operator.
*/
Vector2 operator - () const;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector2 & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
{
return Cmp(Vector2(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
{
return Cmp(Vector2(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
{
return Cmp(Vector2(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
{
return Cmp(Vector2(static_cast< Value >(0)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void SetScalar(Value ns);
/* --------------------------------------------------------------------------------------------
* Copy the values from another instance of this type.
*/
void SetVector2(const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetVector2Ex(Value nx, Value ny);
/* --------------------------------------------------------------------------------------------
* Copy the values from an integral two-dimensional vector.
*/
void SetVector2i(const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/
void SetStr(SQChar delim, StackStrF & values);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax);
/* --------------------------------------------------------------------------------------------
* Clear the component values to default.
*/
void Clear()
{
x = 0.0, y = 0.0;
}
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Vector2 Abs() const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2 type from a string.
*/
static const Vector2 & Get(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2 type from a string.
*/
static const Vector2 & GetEx(SQChar delim, StackStrF & str);
};
} // Namespace:: SqMod
#endif // _BASE_VECTOR2_HPP_

685
module/Base/Vector2i.cpp Normal file
View File

@ -0,0 +1,685 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Vector2i.hpp"
#include "Base/Vector2.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Vector2i"))
// ------------------------------------------------------------------------------------------------
const Vector2i Vector2i::NIL = Vector2i(0);
const Vector2i Vector2i::MIN = Vector2i(std::numeric_limits< Vector2i::Value >::min());
const Vector2i Vector2i::MAX = Vector2i(std::numeric_limits< Vector2i::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Vector2i::Delim = ',';
// ------------------------------------------------------------------------------------------------
Vector2i::Vector2i()
: x(0), y(0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2i::Vector2i(Value sv)
: x(sv), y(sv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2i::Vector2i(Value xv, Value yv)
: x(xv), y(yv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator = (Value s)
{
x = s;
y = s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator = (const Vector2 & v)
{
x = ConvTo< Value >::From(v.x);
y = ConvTo< Value >::From(v.y);
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator += (const Vector2i & v)
{
x += v.x;
y += v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator -= (const Vector2i & v)
{
x -= v.x;
y -= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator *= (const Vector2i & v)
{
x *= v.x;
y *= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator /= (const Vector2i & v)
{
x /= v.x;
y /= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator %= (const Vector2i & v)
{
x %= v.x;
y %= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator &= (const Vector2i & v)
{
x &= v.x;
y &= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator |= (const Vector2i & v)
{
x |= v.x;
y |= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator ^= (const Vector2i & v)
{
x ^= v.x;
y ^= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator <<= (const Vector2i & v)
{
x <<= v.x;
y <<= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator >>= (const Vector2i & v)
{
x >>= v.x;
y >>= v.y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator += (Value s)
{
x += s;
y += s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator -= (Value s)
{
x -= s;
y -= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator *= (Value s)
{
x *= s;
y *= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator /= (Value s)
{
x /= s;
y /= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator %= (Value s)
{
x %= s;
y %= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator &= (Value s)
{
x &= s;
y &= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator |= (Value s)
{
x |= s;
y |= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator ^= (Value s)
{
x += s;
y += s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator <<= (Value s)
{
x <<= s;
y <<= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator >>= (Value s)
{
x >>= s;
y >>= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator ++ ()
{
++x;
++y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i & Vector2i::operator -- ()
{
--x;
--y;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator ++ (int)
{
Vector2i state(*this);
++x;
++y;
return state;
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator -- (int)
{
Vector2i state(*this);
--x;
--y;
return state;
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator + (const Vector2i & v) const
{
return Vector2i(x + v.x, y + v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator - (const Vector2i & v) const
{
return Vector2i(x - v.x, y - v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator * (const Vector2i & v) const
{
return Vector2i(x * v.x, y * v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator / (const Vector2i & v) const
{
return Vector2i(x / v.x, y / v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator % (const Vector2i & v) const
{
return Vector2i(x % v.x, y % v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator & (const Vector2i & v) const
{
return Vector2i(x & v.x, y & v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator | (const Vector2i & v) const
{
return Vector2i(x | v.x, y | v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator ^ (const Vector2i & v) const
{
return Vector2i(x ^ v.x, y ^ v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator << (const Vector2i & v) const
{
return Vector2i(x << v.x, y << v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator >> (const Vector2i & v) const
{
return Vector2i(x >> v.x, y >> v.y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator + (Value s) const
{
return Vector2i(x + s, y + s);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator - (Value s) const
{
return Vector2i(x - s, y - s);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator * (Value s) const
{
return Vector2i(x * s, y * s);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator / (Value s) const
{
return Vector2i(x / s, y / s);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator % (Value s) const
{
return Vector2i(x % s, y % s);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator & (Value s) const
{
return Vector2i(x & s, y & s);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator | (Value s) const
{
return Vector2i(x | s, y | s);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator ^ (Value s) const
{
return Vector2i(x ^ s, y ^ s);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator << (Value s) const
{
return Vector2i(x < s, y < s);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator >> (Value s) const
{
return Vector2i(x >> s, y >> s);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator + () const
{
return Vector2i(std::abs(x), std::abs(y));
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator - () const
{
return Vector2i(-x, -y);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::operator ~ () const
{
return Vector2i(~x, ~y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2i::operator == (const Vector2i & v) const
{
return (x == v.x) && (y == v.y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2i::operator != (const Vector2i & v) const
{
return (x != v.x) || (y != v.y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2i::operator < (const Vector2i & v) const
{
return (x < v.x) && (y < v.y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2i::operator > (const Vector2i & v) const
{
return (x > v.x) && (y > v.y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2i::operator <= (const Vector2i & v) const
{
return (x <= v.x) && (y <= v.y);
}
// ------------------------------------------------------------------------------------------------
bool Vector2i::operator >= (const Vector2i & v) const
{
return (x >= v.x) && (y >= v.y);
}
// ------------------------------------------------------------------------------------------------
Int32 Vector2i::Cmp(const Vector2i & o) const
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
CSStr Vector2i::ToString() const
{
return ToStrF("%d,%d", x, y);
}
// ------------------------------------------------------------------------------------------------
void Vector2i::SetScalar(Value ns)
{
x = ns;
y = ns;
}
// ------------------------------------------------------------------------------------------------
void Vector2i::SetVector2i(const Vector2i & v)
{
x = v.x;
y = v.y;
}
// ------------------------------------------------------------------------------------------------
void Vector2i::SetVector2iEx(Value nx, Value ny)
{
x = nx;
y = ny;
}
// ------------------------------------------------------------------------------------------------
void Vector2i::SetVector2(const Vector2 & v)
{
x = ConvTo< Value >::From(v.x);
y = ConvTo< Value >::From(v.y);
}
// ------------------------------------------------------------------------------------------------
void Vector2i::SetStr(SQChar delim, StackStrF & values)
{
SetVector2i(Vector2i::GetEx(delim, values));
}
// ------------------------------------------------------------------------------------------------
void Vector2i::Generate()
{
x = GetRandomInt32();
y = GetRandomInt32();
}
// ------------------------------------------------------------------------------------------------
void Vector2i::Generate(Value min, Value max)
{
if (max < min)
{
STHROWF("max value is lower than min value");
}
x = GetRandomInt32(min, max);
y = GetRandomInt32(min, max);
}
// ------------------------------------------------------------------------------------------------
void Vector2i::Generate(Value xmin, Value xmax, Value ymin, Value ymax)
{
if (xmax < xmin || ymax < ymin)
{
STHROWF("max value is lower than min value");
}
x = GetRandomInt32(ymin, ymax);
y = GetRandomInt32(xmin, xmax);
}
// ------------------------------------------------------------------------------------------------
Vector2i Vector2i::Abs() const
{
return Vector2i(std::abs(x), std::abs(y));
}
// ------------------------------------------------------------------------------------------------
const Vector2i & Vector2i::Get(StackStrF & str)
{
return Vector2i::GetEx(Vector2i::Delim, str);
}
// ------------------------------------------------------------------------------------------------
const Vector2i & Vector2i::GetEx(SQChar delim, StackStrF & str)
{
// The format specifications that will be used to scan the string
static SQChar fs[] = _SC(" %d , %d ");
static Vector2i vec;
// Clear previous values, if any
vec.Clear();
// Is the specified string empty?
if (str.mLen <= 0)
{
return vec; // Return the value as is!
}
// Assign the specified delimiter
fs[4] = delim;
// Attempt to extract the component values from the specified string
std::sscanf(str.mPtr, &fs[0], &vec.x, &vec.y);
// Return the resulted value
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector2i & GetVector2i()
{
static Vector2i vec;
vec.Clear();
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector2i & GetVector2i(Int32 sv)
{
static Vector2i vec;
vec.SetScalar(sv);
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector2i & GetVector2i(Int32 xv, Int32 yv)
{
static Vector2i vec;
vec.SetVector2iEx(xv, yv);
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector2i & GetVector2i(const Vector2i & o)
{
static Vector2i vec;
vec.SetVector2i(o);
return vec;
}
// ================================================================================================
void Register_Vector2i(HSQUIRRELVM vm)
{
typedef Vector2i::Value Val;
RootTable(vm).Bind(Typename::Str,
Class< Vector2i >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< Val >()
.Ctor< Val, Val >()
// Member Variables
.Var(_SC("x"), &Vector2i::x)
.Var(_SC("y"), &Vector2i::y)
.Var(_SC("X"), &Vector2i::x)
.Var(_SC("Y"), &Vector2i::y)
// Core Meta-methods
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector2i::ToString)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< Vector2i >, SQFloat, SQInteger, bool, std::nullptr_t, Vector2i >)
.Func< Vector2i (Vector2i::*)(void) const >(_SC("_unm"), &Vector2i::operator -)
// Properties
.Prop(_SC("Abs"), &Vector2i::Abs)
// Member Methods
.Func(_SC("SetScalar"), &Vector2i::SetScalar)
.Func(_SC("SetVector2i"), &Vector2i::SetVector2i)
.Func(_SC("SetVector2iEx"), &Vector2i::SetVector2iEx)
.Func(_SC("SetVector2"), &Vector2i::SetVector2)
.FmtFunc(_SC("SetStr"), &Vector2i::SetStr)
.Func(_SC("Clear"), &Vector2i::Clear)
// Member Overloads
.Overload< void (Vector2i::*)(void) >(_SC("Generate"), &Vector2i::Generate)
.Overload< void (Vector2i::*)(Val, Val) >(_SC("Generate"), &Vector2i::Generate)
.Overload< void (Vector2i::*)(Val, Val, Val, Val) >(_SC("Generate"), &Vector2i::Generate)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Vector2i >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Vector2i >)
.StaticFmtFunc(_SC("FromStr"), &Vector2i::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Vector2i::GetEx)
// Operator Exposure
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opAddAssign"), &Vector2i::operator +=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opSubAssign"), &Vector2i::operator -=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opMulAssign"), &Vector2i::operator *=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opDivAssign"), &Vector2i::operator /=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opModAssign"), &Vector2i::operator %=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opAndAssign"), &Vector2i::operator &=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opOrAssign"), &Vector2i::operator |=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opXorAssign"), &Vector2i::operator ^=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opShlAssign"), &Vector2i::operator <<=)
.Func< Vector2i & (Vector2i::*)(const Vector2i &) >(_SC("opShrAssign"), &Vector2i::operator >>=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opAddAssignS"), &Vector2i::operator +=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opSubAssignS"), &Vector2i::operator -=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opMulAssignS"), &Vector2i::operator *=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opDivAssignS"), &Vector2i::operator /=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opModAssignS"), &Vector2i::operator %=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opAndAssignS"), &Vector2i::operator &=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opOrAssignS"), &Vector2i::operator |=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opXorAssignS"), &Vector2i::operator ^=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opShlAssignS"), &Vector2i::operator <<=)
.Func< Vector2i & (Vector2i::*)(Vector2i::Value) >(_SC("opShrAssignS"), &Vector2i::operator >>=)
.Func< Vector2i & (Vector2i::*)(void) >(_SC("opPreInc"), &Vector2i::operator ++)
.Func< Vector2i & (Vector2i::*)(void) >(_SC("opPreDec"), &Vector2i::operator --)
.Func< Vector2i (Vector2i::*)(int) >(_SC("opPostInc"), &Vector2i::operator ++)
.Func< Vector2i (Vector2i::*)(int) >(_SC("opPostDec"), &Vector2i::operator --)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opAdd"), &Vector2i::operator +)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opSub"), &Vector2i::operator -)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opMul"), &Vector2i::operator *)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opDiv"), &Vector2i::operator /)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opMod"), &Vector2i::operator %)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opAnd"), &Vector2i::operator &)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opOr"), &Vector2i::operator |)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opShl"), &Vector2i::operator ^)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opShl"), &Vector2i::operator <<)
.Func< Vector2i (Vector2i::*)(const Vector2i &) const >(_SC("opShr"), &Vector2i::operator >>)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opAddS"), &Vector2i::operator +)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opSubS"), &Vector2i::operator -)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opMulS"), &Vector2i::operator *)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opDivS"), &Vector2i::operator /)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opModS"), &Vector2i::operator %)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opAndS"), &Vector2i::operator &)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opOrS"), &Vector2i::operator |)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opShlS"), &Vector2i::operator ^)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opShlS"), &Vector2i::operator <<)
.Func< Vector2i (Vector2i::*)(Vector2i::Value) const >(_SC("opShrS"), &Vector2i::operator >>)
.Func< Vector2i (Vector2i::*)(void) const >(_SC("opUnPlus"), &Vector2i::operator +)
.Func< Vector2i (Vector2i::*)(void) const >(_SC("opUnMinus"), &Vector2i::operator -)
.Func< Vector2i (Vector2i::*)(void) const >(_SC("opCom"), &Vector2i::operator ~)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opEqual"), &Vector2i::operator ==)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opNotEqual"), &Vector2i::operator !=)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opLessThan"), &Vector2i::operator <)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opGreaterThan"), &Vector2i::operator >)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opLessEqual"), &Vector2i::operator <=)
.Func< bool (Vector2i::*)(const Vector2i &) const >(_SC("opGreaterEqual"), &Vector2i::operator >=)
);
}
} // Namespace:: SqMod

461
module/Base/Vector2i.hpp Normal file
View File

@ -0,0 +1,461 @@
#ifndef _BASE_VECTOR2I_HPP_
#define _BASE_VECTOR2I_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Class used to represent a two-dimensional vector using integral values.
*/
struct Vector2i
{
/* --------------------------------------------------------------------------------------------
* The type of value used by components of type.
*/
typedef int Value;
/* --------------------------------------------------------------------------------------------
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector2i NIL;
static const Vector2i MIN;
static const Vector2i MAX;
/* --------------------------------------------------------------------------------------------
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* The x and y components of this type.
*/
Value x, y;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Vector2i();
/* --------------------------------------------------------------------------------------------
* Construct a vector with the same scalar value for all components.
*/
explicit Vector2i(Value sv);
/* --------------------------------------------------------------------------------------------
* Construct a vector with the specified component values.
*/
Vector2i(Value xv, Value yv);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Vector2i(const Vector2i & o) = default;
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Vector2i(Vector2i && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Vector2i() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Vector2i & operator = (const Vector2i & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector2i & operator = (Vector2i && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector2i & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* Real two-dimensional vector assignment.
*/
Vector2i & operator = (const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* Addition assignment operator.
*/
Vector2i & operator += (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Subtraction assignment operator.
*/
Vector2i & operator -= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Multiplication assignment operator.
*/
Vector2i & operator *= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Division assignment operator.
*/
Vector2i & operator /= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Modulo assignment operator.
*/
Vector2i & operator %= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Bitwise AND assignment operator.
*/
Vector2i & operator &= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Bitwise OR assignment operator.
*/
Vector2i & operator |= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Bitwise XOR assignment operator.
*/
Vector2i & operator ^= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Bitwise left shift assignment operator.
*/
Vector2i & operator <<= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Bitwise right shift assignment operator.
*/
Vector2i & operator >>= (const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Scalar value addition assignment operator.
*/
Vector2i & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction assignment operator.
*/
Vector2i & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication assignment operator.
*/
Vector2i & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value division assignment operator.
*/
Vector2i & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value modulo assignment operator.
*/
Vector2i & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise AND assignment operator.
*/
Vector2i & operator &= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise OR assignment operator.
*/
Vector2i & operator |= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise XOR assignment operator.
*/
Vector2i & operator ^= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise left shift assignment operator.
*/
Vector2i & operator <<= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise right shift assignment operator.
*/
Vector2i & operator >>= (Value s);
/* --------------------------------------------------------------------------------------------
* Pre-increment operator.
*/
Vector2i & operator ++ ();
/* --------------------------------------------------------------------------------------------
* Pre-decrement operator.
*/
Vector2i & operator -- ();
/* --------------------------------------------------------------------------------------------
* Post-increment operator.
*/
Vector2i operator ++ (int);
/* --------------------------------------------------------------------------------------------
* Post-decrement operator.
*/
Vector2i operator -- (int);
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
Vector2i operator + (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
Vector2i operator - (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
Vector2i operator * (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
Vector2i operator / (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Modulo operator.
*/
Vector2i operator % (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Bitwise AND operator.
*/
Vector2i operator & (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Bitwise OR operator.
*/
Vector2i operator | (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Bitwise XOR operator.
*/
Vector2i operator ^ (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Bitwise shift left operator.
*/
Vector2i operator << (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Bitwise shift right operator.
*/
Vector2i operator >> (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Scalar value addition operator.
*/
Vector2i operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction operator.
*/
Vector2i operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication operator.
*/
Vector2i operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value division operator.
*/
Vector2i operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value modulo operator.
*/
Vector2i operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise AND operator.
*/
Vector2i operator & (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise OR operator.
*/
Vector2i operator | (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise XOR operator.
*/
Vector2i operator ^ (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise shift left operator.
*/
Vector2i operator << (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value bitwise shift right operator.
*/
Vector2i operator >> (Value s) const;
/* --------------------------------------------------------------------------------------------
* Unary plus operator.
*/
Vector2i operator + () const;
/* --------------------------------------------------------------------------------------------
* Unary minus operator.
*/
Vector2i operator - () const;
/* --------------------------------------------------------------------------------------------
* Bitwise NOT operator.
*/
Vector2i operator ~ () const;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector2i & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
{
return Cmp(Vector2i(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
{
return Cmp(Vector2i(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
{
return Cmp(Vector2i(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
{
return Cmp(Vector2i(static_cast< Value >(0)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void SetScalar(Value ns);
/* --------------------------------------------------------------------------------------------
* Copy the values from another instance of this type.
*/
void SetVector2i(const Vector2i & v);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetVector2iEx(Value nx, Value ny);
/* --------------------------------------------------------------------------------------------
* Copy the values from a real two-dimensional vector.
*/
void SetVector2(const Vector2 & v);
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/
void SetStr(SQChar delim, StackStrF & values);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax);
/* --------------------------------------------------------------------------------------------
* Clear the component values to default.
*/
void Clear()
{
x = 0, y = 0;
}
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Vector2i Abs() const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2i type from a string.
*/
static const Vector2i & Get(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector2i type from a string.
*/
static const Vector2i & GetEx(SQChar delim, StackStrF & str);
};
} // Namespace:: SqMod
#endif // _BASE_VECTOR2I_HPP_

809
module/Base/Vector3.cpp Normal file
View File

@ -0,0 +1,809 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Vector3.hpp"
#include "Base/Vector4.hpp"
#include "Base/Quaternion.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
#define STOVAL(v) static_cast< Vector3::Value >(v)
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Vector3"))
// ------------------------------------------------------------------------------------------------
const Vector3 Vector3::NIL(STOVAL(0.0));
const Vector3 Vector3::MIN(std::numeric_limits< Vector3::Value >::min());
const Vector3 Vector3::MAX(std::numeric_limits< Vector3::Value >::max());
const Vector3 Vector3::LEFT(STOVAL(-1.0), STOVAL(0.0), STOVAL(0.0));
const Vector3 Vector3::RIGHT(STOVAL(1.0), STOVAL(0.0), STOVAL(0.0));
const Vector3 Vector3::UP(STOVAL(0.0), STOVAL(1.0), STOVAL(0.0));
const Vector3 Vector3::DOWN(STOVAL(0.0), STOVAL(-1.0), STOVAL(0.0));
const Vector3 Vector3::FORWARD(STOVAL(0.0), STOVAL(0.0), STOVAL(1.0));
const Vector3 Vector3::BACK(STOVAL(0.0), STOVAL(0.0), STOVAL(-1.0));
const Vector3 Vector3::ONE(STOVAL(1.0), STOVAL(1.0), STOVAL(1.0));
// ------------------------------------------------------------------------------------------------
SQChar Vector3::Delim = ',';
// ------------------------------------------------------------------------------------------------
Vector3::Vector3()
: x(0.0), y(0.0), z(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector3::Vector3(Value sv)
: x(sv), y(sv), z(sv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector3::Vector3(Value xv, Value yv, Value zv)
: x(xv), y(yv), z(zv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator = (Value s)
{
x = s;
y = s;
z = s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator = (const Vector4 & v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator = (const Quaternion & q)
{
x = q.x;
y = q.y;
z = q.z;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator += (const Vector3 & v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator -= (const Vector3 & v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator *= (const Vector3 & v)
{
x *= v.x;
y *= v.y;
z *= v.z;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator /= (const Vector3 & v)
{
x /= v.x;
y /= v.y;
z /= v.z;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator %= (const Vector3 & v)
{
x = std::fmod(x, v.x);
y = std::fmod(y, v.y);
z = std::fmod(z, v.z);
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator += (Value s)
{
x += s;
y += s;
z += s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator -= (Value s)
{
x -= s;
y -= s;
z -= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator *= (Value s)
{
x *= s;
y *= s;
z *= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator /= (Value s)
{
x /= s;
y /= s;
z /= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator %= (Value s)
{
x = std::fmod(x, s);
y = std::fmod(y, s);
z = std::fmod(z, s);
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator ++ ()
{
++x;
++y;
++z;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 & Vector3::operator -- ()
{
--x;
--y;
--z;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator ++ (int)
{
Vector3 state(*this);
++x;
++y;
++z;
return state;
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator -- (int)
{
Vector3 state(*this);
--x;
--y;
--z;
return state;
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator + (const Vector3 & v) const
{
return Vector3(x + v.x, y + v.y, z + v.z);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator - (const Vector3 & v) const
{
return Vector3(x - v.x, y - v.y, z - v.z);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator * (const Vector3 & v) const
{
return Vector3(x * v.x, y * v.y, z * v.z);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator / (const Vector3 & v) const
{
return Vector3(x / v.x, y / v.y, z / v.z);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator % (const Vector3 & v) const
{
return Vector3(std::fmod(x, v.x), std::fmod(y, v.y), std::fmod(z, v.z));
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator + (Value s) const
{
return Vector3(x + s, y + s, z + s);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator - (Value s) const
{
return Vector3(x - s, y - s, z - s);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator * (Value s) const
{
return Vector3(x * s, y * s, z * s);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator / (Value s) const
{
return Vector3(x / s, y / s, z / s);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator % (Value s) const
{
return Vector3(std::fmod(x, s), std::fmod(y, s), std::fmod(z, s));
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator + () const
{
return Vector3(std::fabs(x), std::fabs(y), std::fabs(z));
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::operator - () const
{
return Vector3(-x, -y, -z);
}
// ------------------------------------------------------------------------------------------------
bool Vector3::operator == (const Vector3 & v) const
{
return EpsEq(x, v.x) && EpsEq(y, v.y) && EpsEq(z, v.z);
}
// ------------------------------------------------------------------------------------------------
bool Vector3::operator != (const Vector3 & v) const
{
return !EpsEq(x, v.x) || !EpsEq(y, v.y) || !EpsEq(z, v.z);
}
// ------------------------------------------------------------------------------------------------
bool Vector3::operator < (const Vector3 & v) const
{
return EpsLt(x, v.x) && EpsLt(y, v.y) && EpsLt(z, v.z);
}
// ------------------------------------------------------------------------------------------------
bool Vector3::operator > (const Vector3 & v) const
{
return EpsGt(x, v.x) && EpsGt(y, v.y) && EpsGt(z, v.z);
}
// ------------------------------------------------------------------------------------------------
bool Vector3::operator <= (const Vector3 & v) const
{
return EpsLtEq(x, v.x) && EpsLtEq(y, v.y) && EpsLtEq(z, v.z);
}
// ------------------------------------------------------------------------------------------------
bool Vector3::operator >= (const Vector3 & v) const
{
return EpsGtEq(x, v.x) && EpsGtEq(y, v.y) && EpsGtEq(z, v.z);
}
// ------------------------------------------------------------------------------------------------
Int32 Vector3::Cmp(const Vector3 & o) const
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
CSStr Vector3::ToString() const
{
return ToStrF("%f,%f,%f", x, y, z);
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetScalar(Value ns)
{
x = ns;
y = ns;
z = ns;
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetVector3(const Vector3 & v)
{
x = v.x;
y = v.y;
z = v.z;
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetVector3Ex(Value nx, Value ny, Value nz)
{
x = nx;
y = ny;
z = nz;
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetVector4(const Vector4 & v)
{
x = v.x;
y = v.y;
z = v.z;
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetVector4Ex(Value nx, Value ny, Value nz, Value /*nw*/)
{
x = nx;
y = ny;
z = nz;
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetQuaternion(const Quaternion & q)
{
SetQuaternionEx(q.x, q.y, q.z, q.w);
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetQuaternionEx(Value qx, Value qy, Value qz, Value qw)
{
// Quick conversion to Euler angles to give tilt to user
const Value sqx = (qx * qx), sqy = (qy * qy), sqz = (qz * qz), sqw = (qw * qw);
y = std::asin(STOVAL(2.0) * ((qw * qy) - (qx * qz)));
if (EpsGt((SQMOD_PI * STOVAL(0.5)) - std::abs(y), STOVAL(1e-10)))
{
z = std::atan2(STOVAL(2.0) * ((qx * qy) + (qw * qz)), sqx - sqy - sqz + sqw);
x = std::atan2(STOVAL(2.0) * ((qw * qx) + (qy * qz)), sqw - sqx - sqy + sqz);
}
else
{
// Compute heading from local 'down' vector
z = std::atan2((STOVAL(2.0) * qy * qz) - (STOVAL(2.0) * qx * qw),
(STOVAL(2.0) * qx * qz) + (STOVAL(2.0) * qy * qw));
x = STOVAL(0.0);
// If facing down, reverse yaw
if (EpsLt(y, STOVAL(0.0)))
{
z -= SQMOD_PI;
}
}
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetStr(SQChar delim, StackStrF & values)
{
SetVector3(Vector3::GetEx(delim, values));
}
// ------------------------------------------------------------------------------------------------
void Vector3::Generate()
{
x = GetRandomFloat32();
y = GetRandomFloat32();
z = GetRandomFloat32();
}
// ------------------------------------------------------------------------------------------------
void Vector3::Generate(Value min, Value max)
{
if (EpsLt(max, min))
{
STHROWF("max value is lower than min value");
}
x = GetRandomFloat32(min, max);
y = GetRandomFloat32(min, max);
z = GetRandomFloat32(min, max);
}
// ------------------------------------------------------------------------------------------------
void Vector3::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax)
{
if (EpsLt(xmax, xmin) || EpsLt(ymax, ymin) || EpsLt(zmax, zmin))
{
STHROWF("max value is lower than min value");
}
x = GetRandomFloat32(xmin, xmax);
y = GetRandomFloat32(ymin, ymax);
z = GetRandomFloat32(zmin, zmax);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::Abs() const
{
return Vector3(std::fabs(x), std::fabs(y), std::fabs(z));
}
// ------------------------------------------------------------------------------------------------
bool Vector3::IsNaN() const
{
return std::isnan(x) || std::isnan(y) || std::isnan(z);
}
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::GetLength() const
{
return std::sqrt((x * x) + (y * y) + (z * z));
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetLength(Value length)
{
Normalize();
// Assign the specified length
*this *= length;
}
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::GetLengthSquared() const
{
return ((x * x) + (y * y) + (z * z));
}
// ------------------------------------------------------------------------------------------------
void Vector3::SetLengthSquared(Value length)
{
Normalize();
// Assign the specified length
*this *= std::sqrt(length);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::Normalized() const
{
const Value len_squared = GetLengthSquared();
if (!EpsEq(len_squared, STOVAL(1.0)) && EpsLt(len_squared, STOVAL(0.0)))
{
return (*this * (STOVAL(1.0) / std::sqrt(len_squared)));
}
else
{
return *this;
}
}
// ------------------------------------------------------------------------------------------------
void Vector3::Normalize()
{
const Value len_squared = GetLengthSquared();
if (!EpsEq(len_squared, STOVAL(1.0)) && EpsGt(len_squared, STOVAL(0.0)))
{
const Value inv_len = STOVAL(1.0) / std::sqrt(len_squared);
x *= inv_len;
y *= inv_len;
z *= inv_len;
}
}
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::DotProduct(const Vector3 & vec) const
{
return ((x * vec.x) + (y * vec.y) + (z * vec.z));
}
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::AbsDotProduct(const Vector3 & vec) const
{
return (std::abs(x * vec.x) + std::abs(y * vec.y) + std::abs(z * vec.z));
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::CrossProduct(const Vector3 & vec) const
{
return Vector3((y * vec.z) - (z * vec.y), (z * vec.x) - (x * vec.z), (x * vec.y) - (y * vec.x));
}
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::Angle(const Vector3 & vec) const
{
return std::acos(DotProduct(vec) / (GetLength() * vec.GetLength()));
}
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::GetDistanceTo(const Vector3 & vec) const
{
return std::sqrt(std::pow(x - vec.x, 2) + std::pow(y - vec.y, 2) + std::pow(z - vec.z, 2));
}
// ------------------------------------------------------------------------------------------------
Vector3::Value Vector3::GetSquaredDistanceTo(const Vector3 & vec) const
{
return (std::pow(x - vec.x, 2) + std::pow(y - vec.y, 2) + std::pow(z - vec.z, 2));
}
// ------------------------------------------------------------------------------------------------
bool Vector3::IsBetweenPoints(const Vector3 & begin, const Vector3 & end) const
{
const Value length = (end - begin).GetLengthSquared();
return EpsLtEq(GetSquaredDistanceTo(begin), length) && EpsLtEq(GetSquaredDistanceTo(end), length);
}
// ------------------------------------------------------------------------------------------------
void Vector3::Interpolate(const Vector3 & a, const Vector3 & b, Value d)
{
x = STOVAL(static_cast< Float64 >(b.x) + ((a.x - b.x) * d));
y = STOVAL(static_cast< Float64 >(b.y) + ((a.y - b.y) * d));
z = STOVAL(static_cast< Float64 >(b.z) + ((a.z - b.z) * d));
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::Interpolated(const Vector3 & vec, Value d) const
{
const Float64 inv = 1.0 - d;
return Vector3(
STOVAL((vec.x * inv) + (x * d)),
STOVAL((vec.y * inv) + (y * d)),
STOVAL((vec.z * inv) + (z * d))
);
}
// ------------------------------------------------------------------------------------------------
Vector3 Vector3::Rotated(const Vector3 & axis, Value angle) const
{
const Vector3 o(axis * axis.DotProduct(*this));
return (o + ((*this - o) * std::cos(angle)) + (axis.CrossProduct(*this) * std::sin(angle)));
}
// ------------------------------------------------------------------------------------------------
void Vector3::CenterRotateXZBy(Value degrees, const Vector3 & center)
{
degrees *= SQMOD_DEGTORAD;
const Value cs = std::cos(degrees);
const Value sn = std::sin(degrees);
x -= center.x;
z -= center.z;
x = static_cast< Value >((x * cs) - (z * sn)) + center.x;
z = static_cast< Value >((x * sn) + (z * cs)) + center.z;
}
// ------------------------------------------------------------------------------------------------
void Vector3::CenterRotateXYBy(Value degrees, const Vector3 & center)
{
degrees *= SQMOD_DEGTORAD;
const Value cs = std::cos(degrees);
const Value sn = std::sin(degrees);
x -= center.x;
y -= center.y;
x = static_cast< Value >((x * cs) - (y * sn)) + center.x;
y = static_cast< Value >((x * sn) + (y * cs)) + center.y;
}
// ------------------------------------------------------------------------------------------------
void Vector3::CenterRotateYZBy(Value degrees, const Vector3 & center)
{
degrees *= SQMOD_DEGTORAD;
const Value cs = std::cos(degrees);
const Value sn = std::sin(degrees);
z -= center.z;
y -= center.y;
y = static_cast< Value >((y * cs) - (z * sn)) + center.z;
z = static_cast< Value >((y * sn) + (z * cs)) + center.y;
}
// ------------------------------------------------------------------------------------------------
const Vector3 & Vector3::Get(StackStrF & str)
{
return Vector3::GetEx(Vector3::Delim, str);
}
// ------------------------------------------------------------------------------------------------
const Vector3 & Vector3::GetEx(SQChar delim, StackStrF & str)
{
// The format specifications that will be used to scan the string
static SQChar fs[] = _SC(" %f , %f , %f ");
static Vector3 vec;
// Clear previous values, if any
vec.Clear();
// Is the specified string empty?
if (str.mLen <= 0)
{
return vec; // Return the value as is!
}
// Assign the specified delimiter
fs[4] = delim;
fs[9] = delim;
// Attempt to extract the component values from the specified string
std::sscanf(str.mPtr, &fs[0], &vec.x, &vec.y, &vec.z);
// Return the resulted value
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector3 & GetVector3()
{
static Vector3 vec;
vec.Clear();
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector3 & GetVector3(Float32 sv)
{
static Vector3 vec;
vec.SetScalar(sv);
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector3 & GetVector3(Float32 xv, Float32 yv, Float32 zv)
{
static Vector3 vec;
vec.SetVector3Ex(xv, yv, zv);
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector3 & GetVector3(const Vector3 & o)
{
static Vector3 vec;
vec.SetVector3(o);
return vec;
}
// ================================================================================================
void Register_Vector3(HSQUIRRELVM vm)
{
typedef Vector3::Value Val;
RootTable(vm).Bind(Typename::Str,
Class< Vector3 >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< Val >()
.Ctor< Val, Val, Val >()
// Static variables
.SetStaticValue(_SC("NIL"), &Vector3::NIL)
.SetStaticValue(_SC("MIN"), &Vector3::MIN)
.SetStaticValue(_SC("MAX"), &Vector3::MAX)
.SetStaticValue(_SC("LEFT"), &Vector3::LEFT)
.SetStaticValue(_SC("RIGHT"), &Vector3::RIGHT)
.SetStaticValue(_SC("UP"), &Vector3::UP)
.SetStaticValue(_SC("DOWN"), &Vector3::DOWN)
.SetStaticValue(_SC("FORWARD"), &Vector3::FORWARD)
.SetStaticValue(_SC("BACK"), &Vector3::BACK)
.SetStaticValue(_SC("ONE"), &Vector3::ONE)
// Member Variables
.Var(_SC("x"), &Vector3::x)
.Var(_SC("y"), &Vector3::y)
.Var(_SC("z"), &Vector3::z)
.Var(_SC("X"), &Vector3::x)
.Var(_SC("Y"), &Vector3::y)
.Var(_SC("Z"), &Vector3::z)
// Core Meta-methods
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector3::ToString)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< Vector3 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector3 >)
.Func< Vector3 (Vector3::*)(void) const >(_SC("_unm"), &Vector3::operator -)
// Properties
.Prop(_SC("Abs"), &Vector3::Abs)
.Prop(_SC("NaN"), &Vector3::IsNaN)
.Prop(_SC("Length"), &Vector3::GetLength, &Vector3::SetLength)
.Prop(_SC("LengthSq"), &Vector3::GetLengthSquared, &Vector3::SetLengthSquared)
.Prop(_SC("Normalized"), &Vector3::Normalized)
// Member Methods
.Func(_SC("SetScalar"), &Vector3::SetScalar)
.Func(_SC("SetVector3"), &Vector3::SetVector3)
.Func(_SC("SetVector3Ex"), &Vector3::SetVector3Ex)
.Func(_SC("SetVector4"), &Vector3::SetVector4)
.Func(_SC("SetVector4Ex"), &Vector3::SetVector4Ex)
.Func(_SC("SetQuaternion"), &Vector3::SetQuaternion)
.Func(_SC("SetQuaternionEx"), &Vector3::SetQuaternionEx)
.FmtFunc(_SC("SetStr"), &Vector3::SetStr)
.Func(_SC("Clear"), &Vector3::Clear)
.Func(_SC("Normalize"), &Vector3::Normalize)
.Func(_SC("Dot"), &Vector3::DotProduct)
.Func(_SC("AbsDot"), &Vector3::AbsDotProduct)
.Func(_SC("Cross"), &Vector3::CrossProduct)
.Func(_SC("Angle"), &Vector3::Angle)
.Func(_SC("DistanceTo"), &Vector3::GetDistanceTo)
.Func(_SC("SqDistanceTo"), &Vector3::GetSquaredDistanceTo)
.Func(_SC("IsBetweenPoints"), &Vector3::IsBetweenPoints)
.Func(_SC("Interpolate"), &Vector3::Interpolate)
.Func(_SC("Interpolated"), &Vector3::Interpolated)
.Func(_SC("Rotated"), &Vector3::Rotated)
.Func(_SC("RotateXZBy"), &Vector3::RotateXZBy)
.Func(_SC("CenterRotateXZBy"), &Vector3::CenterRotateXZBy)
.Func(_SC("RotateXYBy"), &Vector3::RotateXYBy)
.Func(_SC("CenterRotateXYBy"), &Vector3::CenterRotateXYBy)
.Func(_SC("RotateYZBy"), &Vector3::RotateYZBy)
.Func(_SC("CenterRotateYZBy"), &Vector3::CenterRotateYZBy)
// Member Overloads
.Overload< void (Vector3::*)(void) >(_SC("Generate"), &Vector3::Generate)
.Overload< void (Vector3::*)(Val, Val) >(_SC("Generate"), &Vector3::Generate)
.Overload< void (Vector3::*)(Val, Val, Val, Val, Val, Val) >(_SC("Generate"), &Vector3::Generate)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Vector3 >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Vector3 >)
.StaticFmtFunc(_SC("FromStr"), &Vector3::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Vector3::GetEx)
// Operator Exposure
.Func< Vector3 & (Vector3::*)(const Vector3 &) >(_SC("opAddAssign"), &Vector3::operator +=)
.Func< Vector3 & (Vector3::*)(const Vector3 &) >(_SC("opSubAssign"), &Vector3::operator -=)
.Func< Vector3 & (Vector3::*)(const Vector3 &) >(_SC("opMulAssign"), &Vector3::operator *=)
.Func< Vector3 & (Vector3::*)(const Vector3 &) >(_SC("opDivAssign"), &Vector3::operator /=)
.Func< Vector3 & (Vector3::*)(const Vector3 &) >(_SC("opModAssign"), &Vector3::operator %=)
.Func< Vector3 & (Vector3::*)(Vector3::Value) >(_SC("opAddAssignS"), &Vector3::operator +=)
.Func< Vector3 & (Vector3::*)(Vector3::Value) >(_SC("opSubAssignS"), &Vector3::operator -=)
.Func< Vector3 & (Vector3::*)(Vector3::Value) >(_SC("opMulAssignS"), &Vector3::operator *=)
.Func< Vector3 & (Vector3::*)(Vector3::Value) >(_SC("opDivAssignS"), &Vector3::operator /=)
.Func< Vector3 & (Vector3::*)(Vector3::Value) >(_SC("opModAssignS"), &Vector3::operator %=)
.Func< Vector3 & (Vector3::*)(void) >(_SC("opPreInc"), &Vector3::operator ++)
.Func< Vector3 & (Vector3::*)(void) >(_SC("opPreDec"), &Vector3::operator --)
.Func< Vector3 (Vector3::*)(int) >(_SC("opPostInc"), &Vector3::operator ++)
.Func< Vector3 (Vector3::*)(int) >(_SC("opPostDec"), &Vector3::operator --)
.Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("opAdd"), &Vector3::operator +)
.Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("opSub"), &Vector3::operator -)
.Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("opMul"), &Vector3::operator *)
.Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("opDiv"), &Vector3::operator /)
.Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("opMod"), &Vector3::operator %)
.Func< Vector3 (Vector3::*)(Vector3::Value) const >(_SC("opAddS"), &Vector3::operator +)
.Func< Vector3 (Vector3::*)(Vector3::Value) const >(_SC("opSubS"), &Vector3::operator -)
.Func< Vector3 (Vector3::*)(Vector3::Value) const >(_SC("opMulS"), &Vector3::operator *)
.Func< Vector3 (Vector3::*)(Vector3::Value) const >(_SC("opDivS"), &Vector3::operator /)
.Func< Vector3 (Vector3::*)(Vector3::Value) const >(_SC("opModS"), &Vector3::operator %)
.Func< Vector3 (Vector3::*)(void) const >(_SC("opUnPlus"), &Vector3::operator +)
.Func< Vector3 (Vector3::*)(void) const >(_SC("opUnMinus"), &Vector3::operator -)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opEqual"), &Vector3::operator ==)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opNotEqual"), &Vector3::operator !=)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opLessThan"), &Vector3::operator <)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opGreaterThan"), &Vector3::operator >)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opLessEqual"), &Vector3::operator <=)
.Func< bool (Vector3::*)(const Vector3 &) const >(_SC("opGreaterEqual"), &Vector3::operator >=)
);
}
} // Namespace:: SqMod

507
module/Base/Vector3.hpp Normal file
View File

@ -0,0 +1,507 @@
#ifndef _BASE_VECTOR3_HPP_
#define _BASE_VECTOR3_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Class used to represent a three-dimensional vector.
*/
struct Vector3
{
/* --------------------------------------------------------------------------------------------
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector3 NIL; // ( 0, 0, 0)
static const Vector3 MIN; // (<0, <0, <0)
static const Vector3 MAX; // (>0, >0, >0)
static const Vector3 LEFT; // (-1, 0, 0)
static const Vector3 RIGHT; // ( 1, 0, 0)
static const Vector3 UP; // ( 0, 1, 0)
static const Vector3 DOWN; // ( 0, -1, 0)
static const Vector3 FORWARD; // ( 0, 0, 1)
static const Vector3 BACK; // ( 0, 0, -1)
static const Vector3 ONE; // ( 1, 1, 1)
/* --------------------------------------------------------------------------------------------
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* The x, y and z components of this type.
*/
Value x, y, z;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Vector3();
/* --------------------------------------------------------------------------------------------
* Construct a vector with the same scalar value for all components.
*/
explicit Vector3(Value sv);
/* --------------------------------------------------------------------------------------------
* Construct a vector with the specified component values.
*/
Vector3(Value xv, Value yv, Value zv);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Vector3(const Vector3 & o) = default;
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Vector3(Vector3 && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Vector3() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Vector3 & operator = (const Vector3 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector3 & operator = (Vector3 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector3 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* Four-dimensional vector assignment.
*/
Vector3 & operator = (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* Quaternion rotation assignment.
*/
Vector3 & operator = (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* Addition assignment operator.
*/
Vector3 & operator += (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* Subtraction assignment operator.
*/
Vector3 & operator -= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* Multiplication assignment operator.
*/
Vector3 & operator *= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* Division assignment operator.
*/
Vector3 & operator /= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* Modulo assignment operator.
*/
Vector3 & operator %= (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* Scalar value addition assignment operator.
*/
Vector3 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction assignment operator.
*/
Vector3 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication assignment operator.
*/
Vector3 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value division assignment operator.
*/
Vector3 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value modulo assignment operator.
*/
Vector3 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* Pre-increment operator.
*/
Vector3 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* Pre-decrement operator.
*/
Vector3 & operator -- ();
/* --------------------------------------------------------------------------------------------
* Post-increment operator.
*/
Vector3 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* Post-decrement operator.
*/
Vector3 operator -- (int);
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
Vector3 operator + (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
Vector3 operator - (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
Vector3 operator * (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
Vector3 operator / (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Modulo operator.
*/
Vector3 operator % (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Scalar value addition operator.
*/
Vector3 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction operator.
*/
Vector3 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication operator.
*/
Vector3 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value division operator.
*/
Vector3 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value modulo operator.
*/
Vector3 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* Unary plus operator.
*/
Vector3 operator + () const;
/* --------------------------------------------------------------------------------------------
* Unary minus operator.
*/
Vector3 operator - () const;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector3 & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
{
return Cmp(Vector3(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
{
return Cmp(Vector3(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
{
return Cmp(Vector3(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
{
return Cmp(static_cast< Value >(0));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void SetScalar(Value ns);
/* --------------------------------------------------------------------------------------------
* Copy the values from another instance of this type.
*/
void SetVector3(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetVector3Ex(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* Copy the values from a four-dimensional vector.
*/
void SetVector4(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetVector4Ex(Value nx, Value ny, Value nz, Value nw);
/* --------------------------------------------------------------------------------------------
* Copy the values from a quaternion rotation.
*/
void SetQuaternion(const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* Copy the values from a quaternion rotation.
*/
void SetQuaternionEx(Value nx, Value ny, Value nz, Value nw);
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/
void SetStr(SQChar delim, StackStrF & values);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax);
/* --------------------------------------------------------------------------------------------
* Clear the component values to default.
*/
void Clear()
{
x = 0.0, y = 0.0, z = 0.0;
}
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Vector3 Abs() const;
/* --------------------------------------------------------------------------------------------
* Return whether is NaN.
*/
bool IsNaN() const;
/* --------------------------------------------------------------------------------------------
* Retrieve the length.
*/
Value GetLength() const;
/* --------------------------------------------------------------------------------------------
* Assign the length.
*/
void SetLength(Value length);
/* --------------------------------------------------------------------------------------------
* Return the squared length.
*/
Value GetLengthSquared() const;
/* --------------------------------------------------------------------------------------------
* Assign the squared length.
*/
void SetLengthSquared(Value length);
/* --------------------------------------------------------------------------------------------
* Return normalized to unit length.
*/
Vector3 Normalized() const;
/* --------------------------------------------------------------------------------------------
* Normalize to unit length.
*/
void Normalize();
/* --------------------------------------------------------------------------------------------
* Calculate dot product.
*/
Value DotProduct(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Calculate absolute dot product.
*/
Value AbsDotProduct(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Calculate cross product.
*/
Vector3 CrossProduct(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Returns the angle between this vector and another vector in degrees.
*/
Value Angle(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Return the distance between this vector and another vector.
*/
Value GetDistanceTo(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Return the squared distance between this vector and another vector.
*/
Value GetSquaredDistanceTo(const Vector3 & vec) const;
/* --------------------------------------------------------------------------------------------
* Linear interpolation with another vector.
*/
bool IsBetweenPoints(const Vector3 & begin, const Vector3 & end) const;
/* --------------------------------------------------------------------------------------------
* Sets this vector to the linearly interpolated vector between a and b.
*/
void Interpolate(const Vector3 & a, const Vector3 & b, Value d);
/* --------------------------------------------------------------------------------------------
* Sets this vector to the linearly interpolated vector between a and b.
*/
Vector3 Interpolated(const Vector3 & vec, Value d) const;
/* --------------------------------------------------------------------------------------------
* Rotates the vector by a specified number of degrees around the Y axis and the specified center.
*/
Vector3 Rotated(const Vector3 & axis, Value angle) const;
/* --------------------------------------------------------------------------------------------
* Rotates the vector by a specified number of degrees around the Y axis and the specified center.
*/
void RotateXZBy(Value degrees)
{
CenterRotateXZBy(degrees, NIL);
}
/* --------------------------------------------------------------------------------------------
* Rotates the vector by a specified number of degrees around the Y axis and the specified center.
*/
void CenterRotateXZBy(Value degrees, const Vector3 & center);
/* --------------------------------------------------------------------------------------------
* Rotates the vector by a specified number of degrees around the Z axis and the specified center.
*/
void RotateXYBy(Value degrees)
{
CenterRotateXYBy(degrees, NIL);
}
/* --------------------------------------------------------------------------------------------
* Rotates the vector by a specified number of degrees around the Z axis and the specified center.
*/
void CenterRotateXYBy(Value degrees, const Vector3 & center);
/* --------------------------------------------------------------------------------------------
* Rotates the vector by a specified number of degrees around the X axis and the specified center.
*/
void RotateYZBy(Value degrees)
{
CenterRotateYZBy(degrees, NIL);
}
/* --------------------------------------------------------------------------------------------
* Rotates the vector by a specified number of degrees around the X axis and the specified center.
*/
void CenterRotateYZBy(Value degrees, const Vector3 & center);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector3 type from a string.
*/
static const Vector3 & Get(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector3 type from a string.
*/
static const Vector3 & GetEx(SQChar delim, StackStrF & str);
};
} // Namespace:: SqMod
#endif // _BASE_VECTOR3_HPP_

625
module/Base/Vector4.cpp Normal file
View File

@ -0,0 +1,625 @@
// ------------------------------------------------------------------------------------------------
#include "Base/Vector4.hpp"
#include "Base/Vector3.hpp"
#include "Base/Quaternion.hpp"
#include "Base/Shared.hpp"
#include "Base/DynArg.hpp"
#include "Library/Numeric/Random.hpp"
// ------------------------------------------------------------------------------------------------
#include <limits>
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQMODE_DECL_TYPENAME(Typename, _SC("Vector4"))
// ------------------------------------------------------------------------------------------------
const Vector4 Vector4::NIL = Vector4(0);
const Vector4 Vector4::MIN = Vector4(std::numeric_limits< Vector4::Value >::min());
const Vector4 Vector4::MAX = Vector4(std::numeric_limits< Vector4::Value >::max());
// ------------------------------------------------------------------------------------------------
SQChar Vector4::Delim = ',';
// ------------------------------------------------------------------------------------------------
Vector4::Vector4()
: x(0.0), y(0.0), z(0.0), w(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4::Vector4(Value sv)
: x(sv), y(sv), z(sv), w(sv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4::Vector4(Value xv, Value yv, Value zv)
: x(xv), y(yv), z(zv), w(0.0)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4::Vector4(Value xv, Value yv, Value zv, Value wv)
: x(xv), y(yv), z(zv), w(wv)
{
/* ... */
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator = (Value s)
{
x = s;
y = s;
z = s;
w = s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator = (const Vector3 & v)
{
x = v.x;
y = v.y;
z = v.z;
w = 0.0;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator = (const Quaternion & q)
{
x = q.x;
y = q.y;
z = q.z;
w = q.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator += (const Vector4 & v)
{
x += v.x;
y += v.y;
z += v.z;
w += v.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator -= (const Vector4 & v)
{
x -= v.x;
y -= v.y;
z -= v.z;
w -= v.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator *= (const Vector4 & v)
{
x *= v.x;
y *= v.y;
z *= v.z;
w *= v.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator /= (const Vector4 & v)
{
x /= v.x;
y /= v.y;
z /= v.z;
w /= v.w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator %= (const Vector4 & v)
{
x = std::fmod(x, v.x);
y = std::fmod(y, v.y);
z = std::fmod(z, v.z);
w = std::fmod(w, v.w);
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator += (Value s)
{
x += s;
y += s;
z += s;
w += s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator -= (Value s)
{
x -= s;
y -= s;
z -= s;
w -= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator *= (Value s)
{
x *= s;
y *= s;
z *= s;
w *= s;
return *this;
}
Vector4 & Vector4::operator /= (Value s)
{
x /= s;
y /= s;
z /= s;
w /= s;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator %= (Value s)
{
x = std::fmod(x, s);
y = std::fmod(y, s);
z = std::fmod(z, s);
w = std::fmod(w, s);
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator ++ ()
{
++x;
++y;
++z;
++w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 & Vector4::operator -- ()
{
--x;
--y;
--z;
--w;
return *this;
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator ++ (int)
{
Vector4 state(*this);
++x;
++y;
++z;
++w;
return state;
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator -- (int)
{
Vector4 state(*this);
--x;
--y;
--z;
--w;
return state;
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator + (const Vector4 & v) const
{
return Vector4(x + v.x, y + v.y, z + v.z, w + v.w);
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator - (const Vector4 & v) const
{
return Vector4(x - v.x, y - v.y, z - v.z, w - v.w);
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator * (const Vector4 & v) const
{
return Vector4(x * v.x, y * v.y, z * v.z, w * v.w);
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator / (const Vector4 & v) const
{
return Vector4(x / v.x, y / v.y, z / v.z, w / v.w);
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator % (const Vector4 & v) const
{
return Vector4(std::fmod(x, v.x), std::fmod(y, v.y), std::fmod(z, v.z), std::fmod(w, v.w));
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator + (Value s) const
{
return Vector4(x + s, y + s, z + s, w + s);
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator - (Value s) const
{
return Vector4(x - s, y - s, z - s, w - s);
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator * (Value s) const
{
return Vector4(x * s, y * s, z * s, w * s);
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator / (Value s) const
{
return Vector4(x / s, y / s, z / s, w / s);
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator % (Value s) const
{
return Vector4(std::fmod(x, s), std::fmod(y, s), std::fmod(z, s), std::fmod(w, s));
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator + () const
{
return Vector4(std::fabs(x), std::fabs(y), std::fabs(z), std::fabs(w));
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::operator - () const
{
return Vector4(-x, -y, -z, -w);
}
// ------------------------------------------------------------------------------------------------
bool Vector4::operator == (const Vector4 & v) const
{
return EpsEq(x, v.x) && EpsEq(y, v.y) && EpsEq(z, v.z) && EpsEq(w, v.w);
}
// ------------------------------------------------------------------------------------------------
bool Vector4::operator != (const Vector4 & v) const
{
return !EpsEq(x, v.x) || !EpsEq(y, v.y) || !EpsEq(z, v.z) || !EpsEq(w, v.w);
}
// ------------------------------------------------------------------------------------------------
bool Vector4::operator < (const Vector4 & v) const
{
return EpsLt(x, v.x) && EpsLt(y, v.y) && EpsLt(z, v.z) && EpsLt(w, v.w);
}
// ------------------------------------------------------------------------------------------------
bool Vector4::operator > (const Vector4 & v) const
{
return EpsGt(x, v.x) && EpsGt(y, v.y) && EpsGt(z, v.z) && EpsGt(w, v.w);
}
// ------------------------------------------------------------------------------------------------
bool Vector4::operator <= (const Vector4 & v) const
{
return EpsLtEq(x, v.x) && EpsLtEq(y, v.y) && EpsLtEq(z, v.z) && EpsLtEq(w, v.w);
}
// ------------------------------------------------------------------------------------------------
bool Vector4::operator >= (const Vector4 & v) const
{
return EpsGtEq(x, v.x) && EpsGtEq(y, v.y) && EpsGtEq(z, v.z) && EpsGtEq(w, v.w);
}
// ------------------------------------------------------------------------------------------------
Int32 Vector4::Cmp(const Vector4 & o) const
{
if (*this == o)
{
return 0;
}
else if (*this > o)
{
return 1;
}
else
{
return -1;
}
}
// ------------------------------------------------------------------------------------------------
CSStr Vector4::ToString() const
{
return ToStrF("%f,%f,%f,%f", x, y, z, w);
}
// ------------------------------------------------------------------------------------------------
void Vector4::SetScalar(Value ns)
{
x = ns;
y = ns;
z = ns;
w = ns;
}
// ------------------------------------------------------------------------------------------------
void Vector4::SetVector4(const Vector4 & v)
{
x = v.x;
y = v.y;
z = v.z;
w = v.w;
}
// ------------------------------------------------------------------------------------------------
void Vector4::SetVector4Ex(Value nx, Value ny, Value nz, Value nw)
{
x = nx;
y = ny;
z = nz;
w = nw;
}
// ------------------------------------------------------------------------------------------------
void Vector4::SetVector3(const Vector3 & v)
{
x = v.x;
y = v.y;
z = v.z;
w = 0.0;
}
// ------------------------------------------------------------------------------------------------
void Vector4::SetVector3Ex(Value nx, Value ny, Value nz)
{
x = nx;
y = ny;
z = nz;
}
// ------------------------------------------------------------------------------------------------
void Vector4::SetQuaternion(const Quaternion & q)
{
x = q.x;
y = q.y;
z = q.z;
w = q.w;
}
// ------------------------------------------------------------------------------------------------
void Vector4::SetQuaternionEx(Value nx, Value ny, Value nz, Value nw)
{
x = nx;
y = ny;
z = nz;
w = nw;
}
// ------------------------------------------------------------------------------------------------
void Vector4::SetStr(SQChar delim, StackStrF & values)
{
SetVector4(Vector4::GetEx(delim, values));
}
// ------------------------------------------------------------------------------------------------
void Vector4::Generate()
{
x = GetRandomFloat32();
y = GetRandomFloat32();
z = GetRandomFloat32();
w = GetRandomFloat32();
}
// ------------------------------------------------------------------------------------------------
void Vector4::Generate(Value min, Value max)
{
if (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);
}
// ------------------------------------------------------------------------------------------------
void Vector4::Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax)
{
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);
}
// ------------------------------------------------------------------------------------------------
Vector4 Vector4::Abs() const
{
return Vector4(std::fabs(x), std::fabs(y), std::fabs(z), std::fabs(w));
}
// ------------------------------------------------------------------------------------------------
const Vector4 & Vector4::Get(StackStrF & str)
{
return Vector4::GetEx(Vector4::Delim, str);
}
// ------------------------------------------------------------------------------------------------
const Vector4 & Vector4::GetEx(SQChar delim, StackStrF & str)
{
// The format specifications that will be used to scan the string
static SQChar fs[] = _SC(" %f , %f , %f , %f ");
static Vector4 vec;
// Clear previous values, if any
vec.Clear();
// Is the specified string empty?
if (str.mLen <= 0)
{
return vec; // 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.mPtr, &fs[0], &vec.x, &vec.y, &vec.z, &vec.w);
// Return the resulted value
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector4 & GetVector4()
{
static Vector4 vec;
vec.Clear();
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector4 & GetVector4(Float32 sv)
{
static Vector4 vec;
vec.SetScalar(sv);
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector4 & GetVector4(Float32 xv, Float32 yv, Float32 zv)
{
static Vector4 vec;
vec.SetVector3Ex(xv, yv, zv);
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector4 & GetVector4(Float32 xv, Float32 yv, Float32 zv, Float32 wv)
{
static Vector4 vec;
vec.SetVector4Ex(xv, yv, zv, wv);
return vec;
}
// ------------------------------------------------------------------------------------------------
const Vector4 & GetVector4(const Vector4 & o)
{
static Vector4 vec;
vec.SetVector4(o);
return vec;
}
// ================================================================================================
void Register_Vector4(HSQUIRRELVM vm)
{
typedef Vector4::Value Val;
RootTable(vm).Bind(Typename::Str,
Class< Vector4 >(vm, Typename::Str)
// Constructors
.Ctor()
.Ctor< Val >()
.Ctor< Val, Val, Val >()
.Ctor< Val, Val, Val, Val >()
// Member Variables
.Var(_SC("x"), &Vector4::x)
.Var(_SC("y"), &Vector4::y)
.Var(_SC("z"), &Vector4::z)
.Var(_SC("w"), &Vector4::w)
.Var(_SC("X"), &Vector4::x)
.Var(_SC("Y"), &Vector4::y)
.Var(_SC("Z"), &Vector4::z)
.Var(_SC("W"), &Vector4::w)
// Core Meta-methods
.SquirrelFunc(_SC("cmp"), &SqDynArgFwd< SqDynArgCmpFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
.Func(_SC("_tostring"), &Vector4::ToString)
// Meta-methods
.SquirrelFunc(_SC("_add"), &SqDynArgFwd< SqDynArgAddFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)
.SquirrelFunc(_SC("_sub"), &SqDynArgFwd< SqDynArgSubFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)
.SquirrelFunc(_SC("_mul"), &SqDynArgFwd< SqDynArgMulFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)
.SquirrelFunc(_SC("_div"), &SqDynArgFwd< SqDynArgDivFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)
.SquirrelFunc(_SC("_modulo"), &SqDynArgFwd< SqDynArgModFn< Vector4 >, SQFloat, SQInteger, bool, std::nullptr_t, Vector4 >)
.Func< Vector4 (Vector4::*)(void) const >(_SC("_unm"), &Vector4::operator -)
// Properties
.Prop(_SC("Abs"), &Vector4::Abs)
// Member Methods
.Func(_SC("SetScalar"), &Vector4::SetScalar)
.Func(_SC("SetVector4"), &Vector4::SetVector4)
.Func(_SC("SetVector4Ex"), &Vector4::SetVector4Ex)
.Func(_SC("SetVector3"), &Vector4::SetVector3)
.Func(_SC("SetVector3Ex"), &Vector4::SetVector3Ex)
.Func(_SC("SetQuaternion"), &Vector4::SetQuaternion)
.Func(_SC("SetQuaternionEx"), &Vector4::SetQuaternionEx)
.FmtFunc(_SC("SetStr"), &Vector4::SetStr)
.Func(_SC("Clear"), &Vector4::Clear)
// Member Overloads
.Overload< void (Vector4::*)(void) >(_SC("Generate"), &Vector4::Generate)
.Overload< void (Vector4::*)(Val, Val) >(_SC("Generate"), &Vector4::Generate)
.Overload< void (Vector4::*)(Val, Val, Val, Val, Val, Val, Val, Val) >(_SC("Generate"), &Vector4::Generate)
// Static Functions
.StaticFunc(_SC("GetDelimiter"), &SqGetDelimiter< Vector4 >)
.StaticFunc(_SC("SetDelimiter"), &SqSetDelimiter< Vector4 >)
.StaticFmtFunc(_SC("FromStr"), &Vector4::Get)
.StaticFmtFunc(_SC("FromStrEx"), &Vector4::GetEx)
// Operator Exposure
.Func< Vector4 & (Vector4::*)(const Vector4 &) >(_SC("opAddAssign"), &Vector4::operator +=)
.Func< Vector4 & (Vector4::*)(const Vector4 &) >(_SC("opSubAssign"), &Vector4::operator -=)
.Func< Vector4 & (Vector4::*)(const Vector4 &) >(_SC("opMulAssign"), &Vector4::operator *=)
.Func< Vector4 & (Vector4::*)(const Vector4 &) >(_SC("opDivAssign"), &Vector4::operator /=)
.Func< Vector4 & (Vector4::*)(const Vector4 &) >(_SC("opModAssign"), &Vector4::operator %=)
.Func< Vector4 & (Vector4::*)(Vector4::Value) >(_SC("opAddAssignS"), &Vector4::operator +=)
.Func< Vector4 & (Vector4::*)(Vector4::Value) >(_SC("opSubAssignS"), &Vector4::operator -=)
.Func< Vector4 & (Vector4::*)(Vector4::Value) >(_SC("opMulAssignS"), &Vector4::operator *=)
.Func< Vector4 & (Vector4::*)(Vector4::Value) >(_SC("opDivAssignS"), &Vector4::operator /=)
.Func< Vector4 & (Vector4::*)(Vector4::Value) >(_SC("opModAssignS"), &Vector4::operator %=)
.Func< Vector4 & (Vector4::*)(void) >(_SC("opPreInc"), &Vector4::operator ++)
.Func< Vector4 & (Vector4::*)(void) >(_SC("opPreDec"), &Vector4::operator --)
.Func< Vector4 (Vector4::*)(int) >(_SC("opPostInc"), &Vector4::operator ++)
.Func< Vector4 (Vector4::*)(int) >(_SC("opPostDec"), &Vector4::operator --)
.Func< Vector4 (Vector4::*)(const Vector4 &) const >(_SC("opAdd"), &Vector4::operator +)
.Func< Vector4 (Vector4::*)(const Vector4 &) const >(_SC("opSub"), &Vector4::operator -)
.Func< Vector4 (Vector4::*)(const Vector4 &) const >(_SC("opMul"), &Vector4::operator *)
.Func< Vector4 (Vector4::*)(const Vector4 &) const >(_SC("opDiv"), &Vector4::operator /)
.Func< Vector4 (Vector4::*)(const Vector4 &) const >(_SC("opMod"), &Vector4::operator %)
.Func< Vector4 (Vector4::*)(Vector4::Value) const >(_SC("opAddS"), &Vector4::operator +)
.Func< Vector4 (Vector4::*)(Vector4::Value) const >(_SC("opSubS"), &Vector4::operator -)
.Func< Vector4 (Vector4::*)(Vector4::Value) const >(_SC("opMulS"), &Vector4::operator *)
.Func< Vector4 (Vector4::*)(Vector4::Value) const >(_SC("opDivS"), &Vector4::operator /)
.Func< Vector4 (Vector4::*)(Vector4::Value) const >(_SC("opModS"), &Vector4::operator %)
.Func< Vector4 (Vector4::*)(void) const >(_SC("opUnPlus"), &Vector4::operator +)
.Func< Vector4 (Vector4::*)(void) const >(_SC("opUnMinus"), &Vector4::operator -)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opEqual"), &Vector4::operator ==)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opNotEqual"), &Vector4::operator !=)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opLessThan"), &Vector4::operator <)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opGreaterThan"), &Vector4::operator >)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opLessEqual"), &Vector4::operator <=)
.Func< bool (Vector4::*)(const Vector4 &) const >(_SC("opGreaterEqual"), &Vector4::operator >=)
);
}
} // Namespace:: SqMod

381
module/Base/Vector4.hpp Normal file
View File

@ -0,0 +1,381 @@
#ifndef _BASE_VECTOR4_HPP_
#define _BASE_VECTOR4_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Class used to represent a four-dimensional vector.
*/
struct Vector4
{
/* --------------------------------------------------------------------------------------------
* The type of value used by components of type.
*/
typedef float Value;
/* --------------------------------------------------------------------------------------------
* Helper instances for common values mostly used as return types or comparison.
*/
static const Vector4 NIL;
static const Vector4 MIN;
static const Vector4 MAX;
/* --------------------------------------------------------------------------------------------
* The delimiter character to be used when extracting values from strings.
*/
static SQChar Delim;
/* --------------------------------------------------------------------------------------------
* The x, y, z and w components of this type.
*/
Value x, y, z, w;
/* --------------------------------------------------------------------------------------------
* Default constructor.
*/
Vector4();
/* --------------------------------------------------------------------------------------------
* Construct a vector with the same scalar value for all components.
*/
explicit Vector4(Value sv);
/* --------------------------------------------------------------------------------------------
* Construct a vector with the specified component values.
*/
Vector4(Value xv, Value yv, Value zv);
/* --------------------------------------------------------------------------------------------
* Construct a vector with the specified component values.
*/
Vector4(Value xv, Value yv, Value zv, Value wv);
/* --------------------------------------------------------------------------------------------
* Copy constructor.
*/
Vector4(const Vector4 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move constructor.
*/
Vector4(Vector4 && o) = default;
/* --------------------------------------------------------------------------------------------
* Destructor.
*/
~Vector4() = default;
/* --------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Vector4 & operator = (const Vector4 & o) = default;
/* --------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Vector4 & operator = (Vector4 && o) = default;
/* --------------------------------------------------------------------------------------------
* Scalar value assignment operator.
*/
Vector4 & operator = (Value s);
/* --------------------------------------------------------------------------------------------
* Three-dimensional vector assignment operator.
*/
Vector4 & operator = (const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* Quaternion rotation assignment operator.
*/
Vector4 & operator = (const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* Addition assignment operator.
*/
Vector4 & operator += (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* Subtraction assignment operator.
*/
Vector4 & operator -= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* Multiplication assignment operator.
*/
Vector4 & operator *= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* Division assignment operator.
*/
Vector4 & operator /= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* Modulo assignment operator.
*/
Vector4 & operator %= (const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* Scalar value addition assignment operator.
*/
Vector4 & operator += (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction assignment operator.
*/
Vector4 & operator -= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication assignment operator.
*/
Vector4 & operator *= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value division assignment operator.
*/
Vector4 & operator /= (Value s);
/* --------------------------------------------------------------------------------------------
* Scalar value modulo assignment operator.
*/
Vector4 & operator %= (Value s);
/* --------------------------------------------------------------------------------------------
* Pre-increment operator.
*/
Vector4 & operator ++ ();
/* --------------------------------------------------------------------------------------------
* Pre-decrement operator.
*/
Vector4 & operator -- ();
/* --------------------------------------------------------------------------------------------
* Post-increment operator.
*/
Vector4 operator ++ (int);
/* --------------------------------------------------------------------------------------------
* Post-decrement operator.
*/
Vector4 operator -- (int);
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
Vector4 operator + (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
Vector4 operator - (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
Vector4 operator * (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
Vector4 operator / (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Modulo operator.
*/
Vector4 operator % (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Scalar value addition operator.
*/
Vector4 operator + (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value subtraction operator.
*/
Vector4 operator - (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value multiplication operator.
*/
Vector4 operator * (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value division operator.
*/
Vector4 operator / (Value s) const;
/* --------------------------------------------------------------------------------------------
* Scalar value modulo operator.
*/
Vector4 operator % (Value s) const;
/* --------------------------------------------------------------------------------------------
* Unary plus operator.
*/
Vector4 operator + () const;
/* --------------------------------------------------------------------------------------------
* Unary minus operator.
*/
Vector4 operator - () const;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
Int32 Cmp(const Vector4 & v) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQFloat s) const
{
return Cmp(Vector4(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(SQInteger s) const
{
return Cmp(Vector4(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(bool s) const
{
return Cmp(Vector4(static_cast< Value >(s)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare an instance of this type with a scalar value.
*/
Int32 Cmp(std::nullptr_t) const
{
return Cmp(Vector4(static_cast< Value >(0)));
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
CSStr ToString() const;
/* --------------------------------------------------------------------------------------------
* Set all components to the specified scalar value.
*/
void SetScalar(Value ns);
/* --------------------------------------------------------------------------------------------
* Copy the values from another instance of this type.
*/
void SetVector4(const Vector4 & v);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetVector4Ex(Value nx, Value ny, Value nz, Value nw);
/* --------------------------------------------------------------------------------------------
* Copy the values from a three-dimensional vector.
*/
void SetVector3(const Vector3 & v);
/* --------------------------------------------------------------------------------------------
* Set all components to the specified values.
*/
void SetVector3Ex(Value nx, Value ny, Value nz);
/* --------------------------------------------------------------------------------------------
* Copy the values from a quaternion rotation.
*/
void SetQuaternion(const Quaternion & q);
/* --------------------------------------------------------------------------------------------
* Copy the values from a quaternion rotation.
*/
void SetQuaternionEx(Value nx, Value ny, Value nz, Value nw);
/* --------------------------------------------------------------------------------------------
* Set the values extracted from the specified string using the specified delimiter.
*/
void SetStr(SQChar delim, StackStrF & values);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance.
*/
void Generate();
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value min, Value max);
/* --------------------------------------------------------------------------------------------
* Generate random values for all components of this instance within the specified bounds.
*/
void Generate(Value xmin, Value xmax, Value ymin, Value ymax, Value zmin, Value zmax, Value wmin, Value wmax);
/* --------------------------------------------------------------------------------------------
* Clear the component values to default.
*/
void Clear()
{
x = 0.0, y = 0.0, z = 0.0, w = 0.0;
}
/* --------------------------------------------------------------------------------------------
* Retrieve a new instance of this type with absolute component values.
*/
Vector4 Abs() const;
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector4 type from a string.
*/
static const Vector4 & Get(StackStrF & str);
/* --------------------------------------------------------------------------------------------
* Extract the values for components of the Vector4 type from a string.
*/
static const Vector4 & GetEx(SQChar delim, StackStrF & str);
};
} // Namespace:: SqMod
#endif // _BASE_VECTOR4_HPP_