// ------------------------------------------------------------------------------------------------ #include "Base/Sphere.hpp" #include "Base/Shared.hpp" #include "Library/Random.hpp" // ------------------------------------------------------------------------------------------------ namespace SqMod { // ------------------------------------------------------------------------------------------------ const Sphere Sphere::NIL = Sphere(); const Sphere Sphere::MIN = Sphere(0.0); const Sphere Sphere::MAX = Sphere(NumLimit< Sphere::Value >::Max); // ------------------------------------------------------------------------------------------------ SQChar Sphere::Delim = ','; // ------------------------------------------------------------------------------------------------ SQInteger Sphere::Typename(HSQUIRRELVM vm) { static SQChar name[] = _SC("Sphere"); sq_pushstring(vm, name, sizeof(name)); return 1; } // ------------------------------------------------------------------------------------------------ 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 = 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 = 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, 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(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(), 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::Set(Value nr) { rad = nr; } void Sphere::Set(const Sphere & ns) { pos = ns.pos; rad = ns.rad; } void Sphere::Set(const Vector3 & np) { pos = np; } void Sphere::Set(const Vector3 & np, Value nr) { pos = np; rad = nr; } // ------------------------------------------------------------------------------------------------ void Sphere::Set(Value nx, Value ny, Value nz) { pos.Set(nx, ny, nz); } void Sphere::Set(Value nx, Value ny, Value nz, Value nr) { pos.Set(nx, ny, nz); rad = nr; } // ------------------------------------------------------------------------------------------------ void Sphere::Set(CSStr values, SQChar delim) { Set(GetSphere(values, delim)); } // ------------------------------------------------------------------------------------------------ void Sphere::Generate() { pos.Generate(); rad = GetRandomFloat32(); } void Sphere::Generate(Value min, Value max, bool r) { if (EpsLt(max, min)) SqThrowF("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)) SqThrowF("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)) SqThrowF("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(), fabs(rad)); } // ================================================================================================ void Register_Sphere(HSQUIRRELVM vm) { typedef Sphere::Value Val; RootTable(vm).Bind(_SC("Sphere"), Class< Sphere >(vm, _SC("Sphere")) /* Constructors */ .Ctor() .Ctor< Val >() .Ctor< const Vector3 &, Val >() .Ctor< Val, Val, Val, Val >() /* Static Members */ .SetStaticValue(_SC("Delim"), &Sphere::Delim) /* Member Variables */ .Var(_SC("pos"), &Sphere::pos) .Var(_SC("rad"), &Sphere::rad) /* Properties */ .Prop(_SC("abs"), &Sphere::Abs) /* Core Metamethods */ .Func(_SC("_tostring"), &Sphere::ToString) .SquirrelFunc(_SC("_typename"), &Sphere::Typename) .Func(_SC("_cmp"), &Sphere::Cmp) /* Metamethods */ .Func(_SC("_add"), &Sphere::operator +) .Func(_SC("_sub"), &Sphere::operator -) .Func(_SC("_mul"), &Sphere::operator *) .Func(_SC("_div"), &Sphere::operator /) .Func(_SC("_modulo"), &Sphere::operator %) .Func(_SC("_unm"), &Sphere::operator -) /* Setters */ .Overload(_SC("Set"), &Sphere::Set) .Overload(_SC("Set"), &Sphere::Set) .Overload(_SC("Set"), &Sphere::Set) .Overload(_SC("SetRad"), &Sphere::Set) .Overload(_SC("SetVec3"), &Sphere::Set) .Overload(_SC("SetVec3"), &Sphere::Set) .Overload(_SC("SetStr"), &Sphere::Set) /* Utility Methods */ .Func(_SC("Clear"), &Sphere::Clear) /* Operator Exposure */ .Func(_SC("opAddAssign"), &Sphere::operator +=) .Func(_SC("opSubAssign"), &Sphere::operator -=) .Func(_SC("opMulAssign"), &Sphere::operator *=) .Func(_SC("opDivAssign"), &Sphere::operator /=) .Func(_SC("opModAssign"), &Sphere::operator %=) .Func(_SC("opAddAssignR"), &Sphere::operator +=) .Func(_SC("opSubAssignR"), &Sphere::operator -=) .Func(_SC("opMulAssignR"), &Sphere::operator *=) .Func(_SC("opDivAssignR"), &Sphere::operator /=) .Func(_SC("opModAssignR"), &Sphere::operator %=) .Func(_SC("opAddAssignP"), &Sphere::operator +=) .Func(_SC("opSubAssignP"), &Sphere::operator -=) .Func(_SC("opMulAssignP"), &Sphere::operator *=) .Func(_SC("opDivAssignP"), &Sphere::operator /=) .Func(_SC("opModAssignP"), &Sphere::operator %=) .Func(_SC("opPreInc"), &Sphere::operator ++) .Func(_SC("opPreDec"), &Sphere::operator --) .Func(_SC("opPostInc"), &Sphere::operator ++) .Func(_SC("opPostDec"), &Sphere::operator --) .Func(_SC("opAdd"), &Sphere::operator +) .Func(_SC("opSub"), &Sphere::operator -) .Func(_SC("opMul"), &Sphere::operator *) .Func(_SC("opDiv"), &Sphere::operator /) .Func(_SC("opMod"), &Sphere::operator %) .Func(_SC("opAddR"), &Sphere::operator +) .Func(_SC("opSubR"), &Sphere::operator -) .Func(_SC("opMulR"), &Sphere::operator *) .Func(_SC("opDivR"), &Sphere::operator /) .Func(_SC("opModR"), &Sphere::operator %) .Func(_SC("opAddP"), &Sphere::operator +) .Func(_SC("opSubP"), &Sphere::operator -) .Func(_SC("opMulP"), &Sphere::operator *) .Func(_SC("opDivP"), &Sphere::operator /) .Func(_SC("opModP"), &Sphere::operator %) .Func(_SC("opUnPlus"), &Sphere::operator +) .Func(_SC("opUnMinus"), &Sphere::operator -) .Func(_SC("opEqual"), &Sphere::operator ==) .Func(_SC("opNotEqual"), &Sphere::operator !=) .Func(_SC("opLessThan"), &Sphere::operator <) .Func(_SC("opGreaterThan"), &Sphere::operator >) .Func(_SC("opLessEqual"), &Sphere::operator <=) .Func(_SC("opGreaterEqual"), &Sphere::operator >=) ); } } // Namespace:: SqMod