Fixed debug-mode compilation

-Also added multiplication operator and equality comparison to Cube and
Rect


Former-commit-id: b4194a50fbe3025d3be1fc25d48a85d5a05fc5ac
This commit is contained in:
Lynix 2012-10-08 23:08:46 +02:00
parent 94268ae6b2
commit 5bbc8d0fa4
8 changed files with 190 additions and 16 deletions

View File

@ -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<typename T, typename F> T NzLerp(T from, T to, F interpolation);
template<typename T, typename T2> T NzLerp(T from, T to, T2 interpolation);
template<typename T> T NzNormalizeAngle(T angle);
template<typename T> bool NzNumberEquals(T a, T b);
inline NzString NzNumberToString(long long number, nzUInt8 radix = 10);

View File

@ -11,6 +11,7 @@
#include <Nazara/Core/Debug.hpp>
#define F(a) static_cast<T>(a)
#define F2(a) static_cast<T2>(a)
template<typename T>
T NzApproach(T value, T objective, T increment)
@ -132,18 +133,18 @@ unsigned int NzGetNumberLength(long double number, nzUInt8 precision)
return NzGetNumberLength(static_cast<long long>(number)) + precision + 1; // Plus un pour le point
}
template<typename T, typename F>
T NzLerp(T from, T to, F interpolation)
template<typename T, typename T2>
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<typename T>
@ -284,6 +285,7 @@ long long NzStringToNumber(NzString str, nzUInt8 radix, bool* ok)
return (negative) ? -static_cast<long long>(total) : total;
}
#undef F2
#undef F
#include <Nazara/Core/DebugOff.hpp>

View File

@ -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<T>& 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;
};

View File

@ -127,6 +127,17 @@ bool NzCube<T>::IsValid() const
return width > F(0.0) && height > F(0.0) && depth > F(0.0);
}
template<typename T>
void NzCube<T>::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<typename T>
void NzCube<T>::Set(T X, T Y, T Z, T Width, T Height, T Depth)
{
@ -229,6 +240,33 @@ T NzCube<T>::operator[](unsigned int i) const
return *(&x+i);
}
template<typename T>
NzCube<T> NzCube<T>::operator*(T scalar) const
{
return NzCube(x, y, z, width*scalar, height*scalar, depth*scalar);
}
template<typename T>
NzCube<T>& NzCube<T>::operator*=(T scalar)
{
width *= scalar;
height *= scalar;
depth *= scalar;
}
template<typename T>
bool NzCube<T>::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<typename T>
bool NzCube<T>::operator!=(const NzCube& cube) const
{
return !operator==(cube);
}
template<typename T>
NzCube<T> NzCube<T>::Lerp(const NzCube& from, const NzCube& to, T interpolation)
{
@ -251,6 +289,15 @@ NzCube<T> NzCube<T>::Lerp(const NzCube& from, const NzCube& to, T interpolation)
return cube;
}
template<typename T>
NzCube<T> NzCube<T>::Zero()
{
NzCube cube;
cube.MakeZero();
return cube;
}
template<typename T>
std::ostream& operator<<(std::ostream& out, const NzCube<T>& cube)
{

View File

@ -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<T>& vec1, const NzVector2<T>& 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;
};

View File

@ -112,6 +112,15 @@ bool NzRect<T>::IsValid() const
return width > F(0.0) && height > F(0.0);
}
template<typename T>
void NzRect<T>::MakeZero()
{
x = F(0.0);
y = F(0.0);
width = F(0.0);
height = F(0.0);
}
template<typename T>
void NzRect<T>::Set(T X, T Y, T Width, T Height)
{
@ -195,6 +204,32 @@ T NzRect<T>::operator[](unsigned int i) const
return *(&x+i);
}
template<typename T>
NzRect<T> NzRect<T>::operator*(T scalar) const
{
return NzRect(x, y, width*scalar, height*scalar);
}
template<typename T>
NzRect<T>& NzRect<T>::operator*=(T scalar)
{
width *= scalar;
height *= scalar;
}
template<typename T>
bool NzRect<T>::operator==(const NzRect& rect) const
{
return NzNumberEquals(x, rect.x) && NzNumberEquals(y, rect.y) &&
NzNumberEquals(width, rect.width) && NzNumberEquals(height, rect.height);
}
template<typename T>
bool NzRect<T>::operator!=(const NzRect& rect) const
{
return !operator==(rect);
}
template<typename T>
NzRect<T> NzRect<T>::Lerp(const NzRect& from, const NzRect& to, T interpolation)
{
@ -215,6 +250,15 @@ NzRect<T> NzRect<T>::Lerp(const NzRect& from, const NzRect& to, T interpolation)
return rect;
}
template<typename T>
NzRect<T> NzRect<T>::Zero()
{
NzRect rect;
rect.MakeZero();
return rect;
}
template<typename T>
std::ostream& operator<<(std::ostream& out, const NzRect<T>& rect)
{

View File

@ -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;

View File

@ -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);