diff --git a/include/Nazara/Math/Cube.hpp b/include/Nazara/Math/Cube.hpp index db43a231a..ff56fdb8a 100644 --- a/include/Nazara/Math/Cube.hpp +++ b/include/Nazara/Math/Cube.hpp @@ -9,7 +9,9 @@ #include #include +#include #include +#include #include template @@ -33,12 +35,15 @@ class NzCube NzCube& ExtendTo(const NzVector3& point); NzCube& ExtendTo(const NzCube& cube); + NzSphere GetBoundingSphere() const; NzVector3 GetCorner(nzCorner corner) const; NzVector3 GetCenter() const; NzVector3 GetNegativeVertex(const NzVector3& normal) const; NzVector3 GetPosition() const; NzVector3 GetPositiveVertex(const NzVector3& normal) const; + T GetRadius() const; NzVector3 GetSize() const; + T GetSquaredRadius() const; bool Intersect(const NzCube& cube, NzCube* intersection = nullptr) const; @@ -55,6 +60,8 @@ class NzCube NzString ToString() const; + NzCube& Transform(const NzMatrix4& matrix, bool applyTranslation = true); + T& operator[](unsigned int i); T operator[](unsigned int i) const; diff --git a/include/Nazara/Math/Cube.inl b/include/Nazara/Math/Cube.inl index 48cb38b2a..12e381ed9 100644 --- a/include/Nazara/Math/Cube.inl +++ b/include/Nazara/Math/Cube.inl @@ -138,10 +138,16 @@ NzVector3 NzCube::GetCorner(nzCorner corner) const return NzVector3f(); } +template +NzSphere NzCube::GetBoundingSphere() const +{ + return NzSphere(GetCenter(), GetRadius()); +} + template NzVector3 NzCube::GetCenter() const { - return NzVector3(x + width/F(2.0), y + height/F(2.0), z + depth/F(2.0)); + return NzVector3(x + width*F(0.5), y + height*F(0.5), z + depth*F(0.5)); } template @@ -184,12 +190,27 @@ NzVector3 NzCube::GetPositiveVertex(const NzVector3& normal) const return pos; } +template +T NzCube::GetRadius() const +{ + return std::sqrt(GetSquaredRadius()); +} + template NzVector3 NzCube::GetSize() const { return NzVector3(width, height, depth); } +template +T NzCube::GetSquaredRadius() const +{ + NzVector3 size(GetSize()); + size *= F(0.5); // La taille étant relative à la position (minimum) du cube et non pas à son centre + + return size.GetSquaredLength(); +} + template bool NzCube::Intersect(const NzCube& cube, NzCube* intersection) const { @@ -319,6 +340,19 @@ NzString NzCube::ToString() const return ss << "Cube(" << x << ", " << y << ", " << z << ", " << width << ", " << height << ", " << depth << ')'; } +template +NzCube& NzCube::Transform(const NzMatrix4& matrix, bool applyTranslation) +{ + NzVector3 center = matrix.Transform(GetCenter(), (applyTranslation) ? F(1.0) : F(0.0)); // Valeur multipliant la translation + NzVector3 halfSize = GetSize() * F(0.5); + + halfSize.Set(std::fabs(matrix(0,0))*halfSize.x + std::fabs(matrix(1,0))*halfSize.y + std::fabs(matrix(2,0))*halfSize.z, + std::fabs(matrix(0,1))*halfSize.x + std::fabs(matrix(1,1))*halfSize.y + std::fabs(matrix(2,1))*halfSize.z, + std::fabs(matrix(0,2))*halfSize.x + std::fabs(matrix(1,2))*halfSize.y + std::fabs(matrix(2,2))*halfSize.z); + + return Set(center - halfSize, center + halfSize); +} + template T& NzCube::operator[](unsigned int i) {