From 2857e0e4bef165f618c6d51234989c1ca9d3a6f3 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Mon, 25 Jul 2016 00:10:46 +0300 Subject: [PATCH] Improve the Vector3 registration code to avoid using overloads where not necessary. Also restructured the registration code a bit. --- source/Base/AABB.cpp | 20 ++++---- source/Base/Vector3.cpp | 107 ++++++++++++++++++++++++++++------------ source/Base/Vector3.hpp | 30 +++++++---- source/CoreEntity.cpp | 2 +- source/CoreEvents.cpp | 2 +- 5 files changed, 108 insertions(+), 53 deletions(-) diff --git a/source/Base/AABB.cpp b/source/Base/AABB.cpp index 7de22ecd..11d4897e 100644 --- a/source/Base/AABB.cpp +++ b/source/Base/AABB.cpp @@ -60,24 +60,24 @@ AABB::AABB(const Vector3 & vmin, const Vector3 & vmax) // ------------------------------------------------------------------------------------------------ AABB & AABB::operator = (Value s) { - min.Set(-s); - max.Set(std::fabs(s)); + min.SetScalar(-s); + max.SetScalar(std::fabs(s)); return *this; } // ------------------------------------------------------------------------------------------------ AABB & AABB::operator = (const Vector3 & v) { - min.Set(-v); - max.Set(v.Abs()); + min.SetVector3(-v); + max.SetVector3(v.Abs()); return *this; } // ------------------------------------------------------------------------------------------------ AABB & AABB::operator = (const Vector4 & v) { - min.Set(-v); - max.Set(v.Abs()); + min.SetVector4(-v); + max.SetVector4(v.Abs()); return *this; } @@ -336,15 +336,15 @@ void AABB::Set(Value ns) // ------------------------------------------------------------------------------------------------ void AABB::Set(Value nx, Value ny, Value nz) { - min.Set(-nx, -ny, -nz); - max.Set(std::fabs(nx), std::fabs(ny), std::fabs(nz)); + min.SetVector3Ex(-nx, -ny, -nz); + max.SetVector3Ex(std::fabs(nx), std::fabs(ny), std::fabs(nz)); } // ------------------------------------------------------------------------------------------------ void AABB::Set(Value xmin, Value ymin, Value zmin, Value xmax, Value ymax, Value zmax) { - min.Set(xmin, ymin, zmin); - max.Set(xmax, ymax, zmax); + min.SetVector3Ex(xmin, ymin, zmin); + max.SetVector3Ex(xmax, ymax, zmax); } // ------------------------------------------------------------------------------------------------ diff --git a/source/Base/Vector3.cpp b/source/Base/Vector3.cpp index 46644fa8..4de381d7 100644 --- a/source/Base/Vector3.cpp +++ b/source/Base/Vector3.cpp @@ -57,6 +57,7 @@ Vector3 & Vector3::operator = (Value s) return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator = (const Vector4 & v) { x = v.x; @@ -65,6 +66,7 @@ Vector3 & Vector3::operator = (const Vector4 & v) return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator = (const Quaternion & q) { x = q.x; @@ -82,6 +84,7 @@ Vector3 & Vector3::operator += (const Vector3 & v) return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator -= (const Vector3 & v) { x -= v.x; @@ -90,6 +93,7 @@ Vector3 & Vector3::operator -= (const Vector3 & v) return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator *= (const Vector3 & v) { x *= v.x; @@ -98,6 +102,7 @@ Vector3 & Vector3::operator *= (const Vector3 & v) return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator /= (const Vector3 & v) { x /= v.x; @@ -106,6 +111,7 @@ Vector3 & Vector3::operator /= (const Vector3 & v) return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator %= (const Vector3 & v) { x = std::fmod(x, v.x); @@ -123,6 +129,7 @@ Vector3 & Vector3::operator += (Value s) return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator -= (Value s) { x -= s; @@ -131,6 +138,7 @@ Vector3 & Vector3::operator -= (Value s) return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator *= (Value s) { x *= s; @@ -139,6 +147,7 @@ Vector3 & Vector3::operator *= (Value s) return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator /= (Value s) { x /= s; @@ -147,6 +156,7 @@ Vector3 & Vector3::operator /= (Value s) return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator %= (Value s) { x = std::fmod(x, s); @@ -164,6 +174,7 @@ Vector3 & Vector3::operator ++ () return *this; } +// ------------------------------------------------------------------------------------------------ Vector3 & Vector3::operator -- () { --x; @@ -182,6 +193,7 @@ Vector3 Vector3::operator ++ (int) return state; } +// ------------------------------------------------------------------------------------------------ Vector3 Vector3::operator -- (int) { Vector3 state(*this); @@ -197,21 +209,25 @@ 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)); @@ -223,21 +239,25 @@ 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)); @@ -249,6 +269,7 @@ Vector3 Vector3::operator + () const return Vector3(std::fabs(x), std::fabs(y), std::fabs(z)); } +// ------------------------------------------------------------------------------------------------ Vector3 Vector3::operator - () const { return Vector3(-x, -y, -z); @@ -260,26 +281,31 @@ 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); @@ -309,14 +335,23 @@ CSStr Vector3::ToString() const } // ------------------------------------------------------------------------------------------------ -void Vector3::Set(Value ns) +void Vector3::SetScalar(Value ns) { x = ns; y = ns; z = ns; } -void Vector3::Set(Value nx, Value ny, Value nz) +// ------------------------------------------------------------------------------------------------ +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; @@ -324,21 +359,23 @@ void Vector3::Set(Value nx, Value ny, Value nz) } // ------------------------------------------------------------------------------------------------ -void Vector3::Set(const Vector3 & v) +void Vector3::SetVector4(const Vector4 & v) { x = v.x; y = v.y; z = v.z; } -void Vector3::Set(const Vector4 & v) +// ------------------------------------------------------------------------------------------------ +void Vector3::SetVector4Ex(Value nx, Value ny, Value nz, Value /*nw*/) { - x = v.x; - y = v.y; - z = v.z; + x = nx; + y = ny; + z = nz; } -void Vector3::Set(const Quaternion & q) +// ------------------------------------------------------------------------------------------------ +void Vector3::SetQuaternion(const Quaternion & q) { x = q.x; y = q.y; @@ -346,9 +383,17 @@ void Vector3::Set(const Quaternion & q) } // ------------------------------------------------------------------------------------------------ -void Vector3::Set(CSStr values, SQChar delim) +void Vector3::SetQuaternionEx(Value nx, Value ny, Value nz, Value /*nw*/) { - Set(Vector3::Get(values, delim)); + x = nx; + y = ny; + z = nz; +} + +// ------------------------------------------------------------------------------------------------ +void Vector3::SetStr(CSStr values, SQChar delim) +{ + SetVector3(Vector3::Get(values, delim)); } // ------------------------------------------------------------------------------------------------ @@ -359,6 +404,7 @@ void Vector3::Generate() z = GetRandomFloat32(); } +// ------------------------------------------------------------------------------------------------ void Vector3::Generate(Value min, Value max) { if (EpsLt(max, min)) @@ -371,6 +417,7 @@ void Vector3::Generate(Value min, Value 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)) @@ -425,24 +472,27 @@ const Vector3 & GetVector3() return vec; } +// ------------------------------------------------------------------------------------------------ const Vector3 & GetVector3(Float32 sv) { static Vector3 vec; - vec.Set(sv); + vec.SetScalar(sv); return vec; } +// ------------------------------------------------------------------------------------------------ const Vector3 & GetVector3(Float32 xv, Float32 yv, Float32 zv) { static Vector3 vec; - vec.Set(xv, yv, zv); + vec.SetVector3Ex(xv, yv, zv); return vec; } +// ------------------------------------------------------------------------------------------------ const Vector3 & GetVector3(const Vector3 & o) { static Vector3 vec; - vec.Set(o); + vec.SetVector3(o); return vec; } @@ -463,8 +513,6 @@ void Register_Vector3(HSQUIRRELVM vm) .Var(_SC("X"), &Vector3::x) .Var(_SC("Y"), &Vector3::y) .Var(_SC("Z"), &Vector3::z) - // Properties - .Prop(_SC("Abs"), &Vector3::Abs) // Core Meta-methods .Func(_SC("_tostring"), &Vector3::ToString) .SquirrelFunc(_SC("_typename"), &Vector3::Typename) @@ -476,19 +524,22 @@ void Register_Vector3(HSQUIRRELVM vm) .Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("_div"), &Vector3::operator /) .Func< Vector3 (Vector3::*)(const Vector3 &) const >(_SC("_modulo"), &Vector3::operator %) .Func< Vector3 (Vector3::*)(void) const >(_SC("_unm"), &Vector3::operator -) - // Setters - .Overload< void (Vector3::*)(Val) >(_SC("Set"), &Vector3::Set) - .Overload< void (Vector3::*)(Val, Val, Val) >(_SC("Set"), &Vector3::Set) - .Overload< void (Vector3::*)(const Vector3 &) >(_SC("SetVec3"), &Vector3::Set) - .Overload< void (Vector3::*)(const Vector4 &) >(_SC("SetVec4"), &Vector3::Set) - .Overload< void (Vector3::*)(const Quaternion &) >(_SC("SetQuat"), &Vector3::Set) - .Overload< void (Vector3::*)(CSStr, SQChar) >(_SC("SetStr"), &Vector3::Set) - // Random Generators + // Properties + .Prop(_SC("Abs"), &Vector3::Abs) + // 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) + .Func(_SC("SetStr"), &Vector3::SetStr) + .Func(_SC("Clear"), &Vector3::Clear) + // 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) - // Utility Methods - .Func(_SC("Clear"), &Vector3::Clear) // Static Overloads .StaticOverload< const Vector3 & (*)(CSStr) >(_SC("FromStr"), &Vector3::Get) .StaticOverload< const Vector3 & (*)(CSStr, SQChar) >(_SC("FromStr"), &Vector3::Get) @@ -501,33 +552,27 @@ void Register_Vector3(HSQUIRRELVM vm) .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 <) diff --git a/source/Base/Vector3.hpp b/source/Base/Vector3.hpp index 386ad378..3cab2b0b 100644 --- a/source/Base/Vector3.hpp +++ b/source/Base/Vector3.hpp @@ -267,32 +267,42 @@ struct Vector3 /* -------------------------------------------------------------------------------------------- * Set all components to the specified scalar value. */ - void Set(Value ns); - - /* -------------------------------------------------------------------------------------------- - * Set all components to the specified values. - */ - void Set(Value nx, Value ny, Value nz); + void SetScalar(Value ns); /* -------------------------------------------------------------------------------------------- * Copy the values from another instance of this type. */ - void Set(const Vector3 & v); + 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 Set(const Vector4 & v); + 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 Set(const Quaternion & q); + 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 Set(CSStr values, SQChar delim); + void SetStr(CSStr values, SQChar delim); /* -------------------------------------------------------------------------------------------- * Generate random values for all components of this instance. diff --git a/source/CoreEntity.cpp b/source/CoreEntity.cpp index 21ced8be..f9c9bdc8 100644 --- a/source/CoreEntity.cpp +++ b/source/CoreEntity.cpp @@ -31,7 +31,7 @@ void Core::ImportBlips() m_Blips[i].mWorld = world; m_Blips[i].mScale = scale; m_Blips[i].mSprID = sprid; - m_Blips[i].mPosition.Set(x, y, z); + m_Blips[i].mPosition.SetVector3Ex(x, y, z); m_Blips[i].mColor.SetRGBA(color); // Attempt to allocate the instance AllocBlip(i, false, SQMOD_CREATE_IMPORT, NullObject()); diff --git a/source/CoreEvents.cpp b/source/CoreEvents.cpp index b165d49d..8d484f30 100644 --- a/source/CoreEvents.cpp +++ b/source/CoreEvents.cpp @@ -880,7 +880,7 @@ void Core::EmitEntityPool(vcmpEntityPool entity_type, Int32 entity_id, bool is_d inst.mScale = scale; inst.mSprID = sprid; inst.mColor.SetRGBA(color); - inst.mPosition.Set(x, y, z); + inst.mPosition.SetVector3Ex(x, y, z); // Now we can try to allocate the instance after we have all the information AllocBlip(entity_id, false, SQMOD_CREATE_POOL, NullObject()); }