diff --git a/include/Nazara/Math/Basic.hpp b/include/Nazara/Math/Basic.hpp index c1d98ca10..41f695235 100644 --- a/include/Nazara/Math/Basic.hpp +++ b/include/Nazara/Math/Basic.hpp @@ -31,7 +31,7 @@ inline unsigned int NzGetNumberLength(unsigned long long number); inline unsigned int NzGetNumberLength(float number, nzUInt8 precision = NAZARA_CORE_REAL_PRECISION); inline unsigned int NzGetNumberLength(double number, nzUInt8 precision = NAZARA_CORE_REAL_PRECISION); inline unsigned int NzGetNumberLength(long double number, nzUInt8 precision = NAZARA_CORE_REAL_PRECISION); -template T NzLerp(T from, T to, F interpolation); +template T NzLerp(T from, T to, T2 interpolation); template T NzNormalizeAngle(T angle); template bool NzNumberEquals(T a, T b); inline NzString NzNumberToString(long long number, nzUInt8 radix = 10); diff --git a/include/Nazara/Math/Basic.inl b/include/Nazara/Math/Basic.inl index 57afcd2c0..f6323deee 100644 --- a/include/Nazara/Math/Basic.inl +++ b/include/Nazara/Math/Basic.inl @@ -11,6 +11,7 @@ #include #define F(a) static_cast(a) +#define F2(a) static_cast(a) template T NzApproach(T value, T objective, T increment) @@ -132,18 +133,18 @@ unsigned int NzGetNumberLength(long double number, nzUInt8 precision) return NzGetNumberLength(static_cast(number)) + precision + 1; // Plus un pour le point } -template -T NzLerp(T from, T to, F interpolation) +template +T NzLerp(T from, T to, T2 interpolation) { #ifdef NAZARA_DEBUG - if (interpolation < F(0.0) || interpolation > F(1.0)) + if (interpolation < F2(0.0) || interpolation > F2(1.0)) { NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); - return Zero(); + return F(0.0); } #endif - return from + interpolation*(to-from); + return from + interpolation*(to - from); } template @@ -284,6 +285,7 @@ long long NzStringToNumber(NzString str, nzUInt8 radix, bool* ok) return (negative) ? -static_cast(total) : total; } +#undef F2 #undef F #include diff --git a/include/Nazara/Math/Cube.hpp b/include/Nazara/Math/Cube.hpp index 8930831aa..0d3de354d 100644 --- a/include/Nazara/Math/Cube.hpp +++ b/include/Nazara/Math/Cube.hpp @@ -37,6 +37,8 @@ class NzCube bool IsValid() const; + void MakeZero(); + void Set(T X, T Y, T Z, T Width, T Height, T Depth); void Set(const T cube[6]); void Set(const NzRect& rect); @@ -50,7 +52,15 @@ class NzCube T& operator[](unsigned int i); T operator[](unsigned int i) const; + NzCube operator*(T scalar) const; + + NzCube& operator*=(T scalar); + + bool operator==(const NzCube& cube) const; + bool operator!=(const NzCube& cube) const; + static NzCube Lerp(const NzCube& from, const NzCube& to, T interpolation); + static NzCube Zero(); T x, y, z, width, height, depth; }; diff --git a/include/Nazara/Math/Cube.inl b/include/Nazara/Math/Cube.inl index 4ea4b07c1..3d02c2327 100644 --- a/include/Nazara/Math/Cube.inl +++ b/include/Nazara/Math/Cube.inl @@ -127,6 +127,17 @@ bool NzCube::IsValid() const return width > F(0.0) && height > F(0.0) && depth > F(0.0); } +template +void NzCube::MakeZero() +{ + x = F(0.0); + y = F(0.0); + z = F(0.0); + width = F(0.0); + height = F(0.0); + depth = F(0.0); +} + template void NzCube::Set(T X, T Y, T Z, T Width, T Height, T Depth) { @@ -229,6 +240,33 @@ T NzCube::operator[](unsigned int i) const return *(&x+i); } +template +NzCube NzCube::operator*(T scalar) const +{ + return NzCube(x, y, z, width*scalar, height*scalar, depth*scalar); +} + +template +NzCube& NzCube::operator*=(T scalar) +{ + width *= scalar; + height *= scalar; + depth *= scalar; +} + +template +bool NzCube::operator==(const NzCube& cube) const +{ + return NzNumberEquals(x, cube.x) && NzNumberEquals(y, cube.y) && NzNumberEquals(z, cube.z) && + NzNumberEquals(width, cube.width) && NzNumberEquals(height, cube.height) && NzNumberEquals(depth, cube.depth); +} + +template +bool NzCube::operator!=(const NzCube& cube) const +{ + return !operator==(cube); +} + template NzCube NzCube::Lerp(const NzCube& from, const NzCube& to, T interpolation) { @@ -251,6 +289,15 @@ NzCube NzCube::Lerp(const NzCube& from, const NzCube& to, T interpolation) return cube; } +template +NzCube NzCube::Zero() +{ + NzCube cube; + cube.MakeZero(); + + return cube; +} + template std::ostream& operator<<(std::ostream& out, const NzCube& cube) { diff --git a/include/Nazara/Math/Rect.hpp b/include/Nazara/Math/Rect.hpp index d7a6162dd..0d373507d 100644 --- a/include/Nazara/Math/Rect.hpp +++ b/include/Nazara/Math/Rect.hpp @@ -35,6 +35,8 @@ class NzRect bool IsValid() const; + void MakeZero(); + void Set(T X, T Y, T Width, T Height); void Set(const T rect[4]); void Set(const NzVector2& vec1, const NzVector2& vec2); @@ -47,7 +49,15 @@ class NzRect T& operator[](unsigned int i); T operator[](unsigned int i) const; + NzRect operator*(T scalar) const; + + NzRect& operator*=(T scalar); + + bool operator==(const NzRect& rect) const; + bool operator!=(const NzRect& rect) const; + static NzRect Lerp(const NzRect& from, const NzRect& to, T interpolation); + static NzRect Zero(); T x, y, width, height; }; diff --git a/include/Nazara/Math/Rect.inl b/include/Nazara/Math/Rect.inl index 4d4e73e7b..62f7684f4 100644 --- a/include/Nazara/Math/Rect.inl +++ b/include/Nazara/Math/Rect.inl @@ -112,6 +112,15 @@ bool NzRect::IsValid() const return width > F(0.0) && height > F(0.0); } +template +void NzRect::MakeZero() +{ + x = F(0.0); + y = F(0.0); + width = F(0.0); + height = F(0.0); +} + template void NzRect::Set(T X, T Y, T Width, T Height) { @@ -195,6 +204,32 @@ T NzRect::operator[](unsigned int i) const return *(&x+i); } +template +NzRect NzRect::operator*(T scalar) const +{ + return NzRect(x, y, width*scalar, height*scalar); +} + +template +NzRect& NzRect::operator*=(T scalar) +{ + width *= scalar; + height *= scalar; +} + +template +bool NzRect::operator==(const NzRect& rect) const +{ + return NzNumberEquals(x, rect.x) && NzNumberEquals(y, rect.y) && + NzNumberEquals(width, rect.width) && NzNumberEquals(height, rect.height); +} + +template +bool NzRect::operator!=(const NzRect& rect) const +{ + return !operator==(rect); +} + template NzRect NzRect::Lerp(const NzRect& from, const NzRect& to, T interpolation) { @@ -215,6 +250,15 @@ NzRect NzRect::Lerp(const NzRect& from, const NzRect& to, T interpolation) return rect; } +template +NzRect NzRect::Zero() +{ + NzRect rect; + rect.MakeZero(); + + return rect; +} + template std::ostream& operator<<(std::ostream& out, const NzRect& rect) { diff --git a/include/Nazara/Utility/AxisAlignedBox.hpp b/include/Nazara/Utility/AxisAlignedBox.hpp index 47934cda1..1f0a123be 100644 --- a/include/Nazara/Utility/AxisAlignedBox.hpp +++ b/include/Nazara/Utility/AxisAlignedBox.hpp @@ -15,6 +15,7 @@ class NAZARA_API NzAxisAlignedBox { public: NzAxisAlignedBox(); + NzAxisAlignedBox(const NzCubef& cube); NzAxisAlignedBox(const NzVector3f& vec1, const NzVector3f& vec2); NzAxisAlignedBox(nzExtend extend); @@ -37,6 +38,8 @@ class NAZARA_API NzAxisAlignedBox NzString ToString() const; + static NzAxisAlignedBox Lerp(const NzAxisAlignedBox& from, const NzAxisAlignedBox& to, float interpolation); + static const NzAxisAlignedBox Infinite; static const NzAxisAlignedBox Null; diff --git a/src/Nazara/Utility/AxisAlignedBox.cpp b/src/Nazara/Utility/AxisAlignedBox.cpp index 67a9fe15e..75b23468a 100644 --- a/src/Nazara/Utility/AxisAlignedBox.cpp +++ b/src/Nazara/Utility/AxisAlignedBox.cpp @@ -11,6 +11,12 @@ m_extend(nzExtend_Null) { } +NzAxisAlignedBox::NzAxisAlignedBox(const NzCubef& cube) : +m_extend(nzExtend_Finite), +m_cube(cube) +{ +} + NzAxisAlignedBox::NzAxisAlignedBox(const NzVector3f& vec1, const NzVector3f& vec2) : m_extend(nzExtend_Finite), m_cube(vec1, vec2) @@ -37,7 +43,6 @@ void NzAxisAlignedBox::ExtendTo(const NzAxisAlignedBox& box) switch (m_extend) { case nzExtend_Finite: - { switch (box.m_extend) { case nzExtend_Finite: @@ -50,18 +55,16 @@ void NzAxisAlignedBox::ExtendTo(const NzAxisAlignedBox& box) case nzExtend_Null: break; - - break; } + break; - case nzExtend_Infinite: - // Rien à faire - break; + case nzExtend_Infinite: + // Rien à faire + break; - case nzExtend_Null: - operator=(box); - break; - } + case nzExtend_Null: + operator=(box); + break; } } @@ -152,6 +155,61 @@ NzString NzAxisAlignedBox::ToString() const return "NzAxisAlignedBox(ERROR)"; } +NzAxisAlignedBox NzAxisAlignedBox::Lerp(const NzAxisAlignedBox& from, const NzAxisAlignedBox& to, float interpolation) +{ + #ifdef NAZARA_DEBUG + if (interpolation < 0.f || interpolation > 1.f) + { + NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')'); + return Null; + } + #endif + + if (NzNumberEquals(interpolation, 0.f)) + return from; + + if (NzNumberEquals(interpolation, 1.f)) + return to; + + switch (to.m_extend) + { + case nzExtend_Finite: + { + switch (from.m_extend) + { + case nzExtend_Finite: + return NzCubef::Lerp(from.m_cube, to.m_cube, interpolation); + + case nzExtend_Infinite: + return Infinite; + + case nzExtend_Null: + return from.m_cube * interpolation; + } + } + + case nzExtend_Infinite: + return Infinite; // Un petit peu d'infini est infini quand même ;) + + case nzExtend_Null: + { + switch (from.m_extend) + { + case nzExtend_Finite: + return from.m_cube * (1.f - interpolation); + + case nzExtend_Infinite: + return Infinite; + + case nzExtend_Null: + return Null; + } + } + } + + return Null; +} + const NzAxisAlignedBox NzAxisAlignedBox::Infinite(nzExtend_Infinite); const NzAxisAlignedBox NzAxisAlignedBox::Null(nzExtend_Null);