diff --git a/include/Nazara/Math/OrientedCube.hpp b/include/Nazara/Math/OrientedCube.hpp new file mode 100644 index 000000000..762e8c16e --- /dev/null +++ b/include/Nazara/Math/OrientedCube.hpp @@ -0,0 +1,73 @@ +// Copyright (C) 2012 Jérôme Leclercq +// This file is part of the "Nazara Engine - Mathematics module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_ORIENTEDCUBE_HPP +#define NAZARA_ORIENTEDCUBE_HPP + +#include +#include +#include +#include + +template +class NzOrientedCube +{ + public: + NzOrientedCube() = default; + NzOrientedCube(T X, T Y, T Z, T Width, T Height, T Depth); + NzOrientedCube(const NzCube& cube); + NzOrientedCube(const NzVector3& vec1, const NzVector3& vec2); + template explicit NzOrientedCube(const NzOrientedCube& orientedCube); + NzOrientedCube(const NzOrientedCube& orientedCube) = default; + ~NzOrientedCube() = default; + + const NzVector3& GetCorner(nzCorner corner) const; + + bool IsValid() const; + + NzOrientedCube& MakeZero(); + + NzOrientedCube& Set(T X, T Y, T Z, T Width, T Height, T Depth); + NzOrientedCube& Set(const NzCube& cube); + NzOrientedCube& Set(const NzOrientedCube& orientedCube); + NzOrientedCube& Set(const NzVector3& vec1, const NzVector3& vec2); + template NzOrientedCube& Set(const NzOrientedCube& orientedCube); + + NzString ToString() const; + + void Update(const NzMatrix4& transformMatrix); + + operator NzVector3*(); + operator const NzVector3*() const; + + NzVector3& operator()(unsigned int i); + NzVector3 operator()(unsigned int i) const; + + NzOrientedCube operator*(T scalar) const; + + NzOrientedCube& operator*=(T scalar); + + bool operator==(const NzOrientedCube& cube) const; + bool operator!=(const NzOrientedCube& cube) const; + + static NzOrientedCube Lerp(const NzOrientedCube& from, const NzOrientedCube& to, T interpolation); + static NzOrientedCube Zero(); + + NzCube localCube; + + private: + NzVector3 m_corners[nzCorner_Max+1]; // Ne peuvent pas être modifiés directement +}; + +template +std::ostream& operator<<(std::ostream& out, const NzOrientedCube& orientedCube); + +typedef NzOrientedCube NzOrientedCubed; +typedef NzOrientedCube NzOrientedCubef; + +#include + +#endif // NAZARA_ORIENTEDCUBE_HPP diff --git a/include/Nazara/Math/OrientedCube.inl b/include/Nazara/Math/OrientedCube.inl new file mode 100644 index 000000000..c8c71b831 --- /dev/null +++ b/include/Nazara/Math/OrientedCube.inl @@ -0,0 +1,234 @@ +// Copyright (C) 2012 Jérôme Leclercq +// This file is part of the "Nazara Engine - Mathematics module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include + +#define F(a) static_cast(a) + +template +NzOrientedCube::NzOrientedCube(T X, T Y, T Z, T Width, T Height, T Depth) +{ + Set(X, Y, Z, Width, Height, Depth); +} + +template +NzOrientedCube::NzOrientedCube(const NzCube& cube) +{ + Set(cube); +} + +template +NzOrientedCube::NzOrientedCube(const NzVector3& vec1, const NzVector3& vec2) +{ + Set(vec1, vec2); +} + +template +template +NzOrientedCube::NzOrientedCube(const NzOrientedCube& orientedCube) +{ + Set(orientedCube); +} + +template +const NzVector3& NzOrientedCube::GetCorner(nzCorner corner) const +{ + #ifdef NAZARA_DEBUG + if (corner > nzCorner_Max) + { + NazaraError("Corner not handled (0x" + NzString::Number(corner, 16) + ')'); + + static NzVector3 dummy; + return dummy; + } + #endif + + return m_corners[corner]; +} + +template +bool NzOrientedCube::IsValid() const +{ + return localCube.IsValid(); +} + +template +NzOrientedCube& NzOrientedCube::MakeZero() +{ + localCube.MakeZero(); + + return *this; +} + +template +NzOrientedCube& NzOrientedCube::Set(T X, T Y, T Z, T Width, T Height, T Depth) +{ + localCube.Set(X, Y, Z, Width, Height, Depth); + + return *this; +} + +template +NzOrientedCube& NzOrientedCube::Set(const NzCube& cube) +{ + localCube.Set(cube); + + return *this; +} + +template +NzOrientedCube& NzOrientedCube::Set(const NzOrientedCube& orientedCube) +{ + std::memcpy(this, &orientedCube, sizeof(NzOrientedCube)); + + return *this; +} + +template +NzOrientedCube& NzOrientedCube::Set(const NzVector3& vec1, const NzVector3& vec2) +{ + localCube.Set(vec1, vec2); + + return *this; +} + +template +template +NzOrientedCube& NzOrientedCube::Set(const NzOrientedCube& orientedCube) +{ + for (unsigned int i = 0; i <= nzCorner_Max; ++i) + m_corners[i].Set(orientedCube.m_corners[i]); + + localCube = orientedCube.localCube; + + return *this; +} + +template +NzString NzOrientedCube::ToString() const +{ + NzStringStream ss; + + return ss << "OrientedCube(FLB: " << m_corners[nzCorner_FarLeftBottom].ToString() << "\n" + << " FLT: " << m_corners[nzCorner_FarLeftTop].ToString() << "\n" + << " FRB: " << m_corners[nzCorner_FarRightBottom].ToString() << "\n" + << " FRT: " << m_corners[nzCorner_FarRightTop].ToString() << "\n" + << " NLB: " << m_corners[nzCorner_NearLeftBottom].ToString() << "\n" + << " NLT: " << m_corners[nzCorner_NearLeftTop].ToString() << "\n" + << " NRB: " << m_corners[nzCorner_NearRightBottom].ToString() << "\n" + << " NRT: " << m_corners[nzCorner_NearRightTop].ToString() << ")\n"; +} + +template +void NzOrientedCube::Update(const NzMatrix4& transformMatrix) +{ + for (unsigned int i = 0; i <= nzCorner_Max; ++i) + m_corners[i] = transformMatrix.Transform(localCube.GetCorner(static_cast(i))); +} + +template +NzOrientedCube::operator NzVector3*() +{ + return &m_corners[0]; +} + +template +NzOrientedCube::operator const NzVector3*() const +{ + return &m_corners[0]; +} + +template +NzVector3& NzOrientedCube::operator()(unsigned int i) +{ + #if NAZARA_MATH_SAFE + if (i > nzCorner_Max) + { + NzStringStream ss; + ss << "Index out of range: (" << i << " >= 3)"; + + NazaraError(ss); + throw std::out_of_range(ss.ToString()); + } + #endif + + return &m_corners[i]; +} + +template +NzVector3 NzOrientedCube::operator()(unsigned int i) const +{ + #if NAZARA_MATH_SAFE + if (i > nzCorner_Max) + { + NzStringStream ss; + ss << "Index out of range: (" << i << " >= 3)"; + + NazaraError(ss); + throw std::out_of_range(ss.ToString()); + } + #endif + + return &m_corners[i]; +} + +template +NzOrientedCube NzOrientedCube::operator*(T scalar) const +{ + NzOrientedCube box(*this); + box *= scalar; + + return box; +} + +template +NzOrientedCube& NzOrientedCube::operator*=(T scalar) +{ + localCube *= scalar; + + return *this; +} + +template +bool NzOrientedCube::operator==(const NzOrientedCube& box) const +{ + return localCube == box.localCube; +} + +template +bool NzOrientedCube::operator!=(const NzOrientedCube& box) const +{ + return !operator==(box); +} + +template +NzOrientedCube NzOrientedCube::Lerp(const NzOrientedCube& from, const NzOrientedCube& to, T interpolation) +{ + NzOrientedCube orientedCube; + orientedCube.Set(NzCube::Lerp(from.localCube, to.localCube, interpolation)); + + return orientedCube; +} + +template +NzOrientedCube NzOrientedCube::Zero() +{ + NzOrientedCube orientedCube; + orientedCube.MakeZero(); + + return orientedCube; +} + +template +std::ostream& operator<<(std::ostream& out, const NzOrientedCube& orientedCube) +{ + return out << orientedCube.ToString(); +} + +#undef F + +#include