From 28ea9fc9a074b5c7c817328a605795bc45420834 Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 20 Feb 2013 16:41:29 +0100 Subject: [PATCH] Added Frustum::Contains(AxisAlignedBox) Fixed Frustum::Contains, Intersect and Plane::Distance not being const Former-commit-id: e219a10fac78f50743f19ebe523345bcac0b0fb7 --- include/Nazara/Math/Frustum.hpp | 13 +++++---- include/Nazara/Math/Frustum.inl | 48 +++++++++++++++++++++++++++++---- include/Nazara/Math/Plane.hpp | 4 +-- include/Nazara/Math/Plane.inl | 4 +-- 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/include/Nazara/Math/Frustum.hpp b/include/Nazara/Math/Frustum.hpp index edf72da76..5c147f789 100644 --- a/include/Nazara/Math/Frustum.hpp +++ b/include/Nazara/Math/Frustum.hpp @@ -8,6 +8,7 @@ #define NAZARA_FRUSTUM_HPP #include +#include #include #include #include @@ -25,10 +26,11 @@ class NzFrustum NzFrustum& Build(T angle, T ratio, T zNear, T zFar, const NzVector3& eye, const NzVector3& target, const NzVector3& up = NzVector3::Up()); - bool Contains(const NzCube& cube); - bool Contains(const NzSphere& sphere); - bool Contains(const NzVector3& point); - bool Contains(const NzVector3* points, unsigned int pointCount); + bool Contains(const NzAxisAlignedBox& box) const; + bool Contains(const NzCube& cube) const; + bool Contains(const NzSphere& sphere) const; + bool Contains(const NzVector3& point) const; + bool Contains(const NzVector3* points, unsigned int pointCount) const; NzFrustum& Extract(const NzMatrix4& clipMatrix); NzFrustum& Extract(const NzMatrix4& view, const NzMatrix4& projection); @@ -36,9 +38,10 @@ class NzFrustum const NzVector3& GetCorner(nzCorner corner) const; const NzPlane& GetPlane(nzFrustumPlane plane) const; + nzIntersectionSide Intersect(const NzAxisAlignedBox& box) const; nzIntersectionSide Intersect(const NzCube& cube) const; nzIntersectionSide Intersect(const NzSphere& sphere) const; - nzIntersectionSide Intersect(const NzVector3* points, unsigned int pointCount); + nzIntersectionSide Intersect(const NzVector3* points, unsigned int pointCount) const; NzFrustum& Set(const NzFrustum& frustum); template NzFrustum& Set(const NzFrustum& frustum); diff --git a/include/Nazara/Math/Frustum.inl b/include/Nazara/Math/Frustum.inl index 673780218..505c476eb 100644 --- a/include/Nazara/Math/Frustum.inl +++ b/include/Nazara/Math/Frustum.inl @@ -66,7 +66,26 @@ NzFrustum& NzFrustum::Build(T angle, T ratio, T zNear, T zFar, const NzVec } template -bool NzFrustum::Contains(const NzCube& cube) +bool NzFrustum::Contains(const NzAxisAlignedBox& box) const +{ + switch (box.extend) + { + case nzExtend_Finite: + return Contains(box.cube); + + case nzExtend_Infinite: + return true; + + case nzExtend_Null: + return false; + } + + NazaraError("Invalid extend type (0x" + NzString::Number(box.extend, 16) + ')'); + return false; +} + +template +bool NzFrustum::Contains(const NzCube& cube) const { // http://www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-testing-boxes-ii/ for(unsigned int i = 0; i <= nzFrustumPlane_Max; i++) @@ -79,7 +98,7 @@ bool NzFrustum::Contains(const NzCube& cube) } template -bool NzFrustum::Contains(const NzSphere& sphere) +bool NzFrustum::Contains(const NzSphere& sphere) const { for(unsigned int i = 0; i <= nzFrustumPlane_Max; i++) { @@ -91,7 +110,7 @@ bool NzFrustum::Contains(const NzSphere& sphere) } template -bool NzFrustum::Contains(const NzVector3& point) +bool NzFrustum::Contains(const NzVector3& point) const { for(unsigned int i = 0; i <= nzFrustumPlane_Max; ++i) { @@ -103,7 +122,7 @@ bool NzFrustum::Contains(const NzVector3& point) } template -bool NzFrustum::Contains(const NzVector3* points, unsigned int pointCount) +bool NzFrustum::Contains(const NzVector3* points, unsigned int pointCount) const { for (unsigned int i = 0; i <= nzFrustumPlane_Max; ++i) { @@ -327,6 +346,25 @@ const NzPlane& NzFrustum::GetPlane(nzFrustumPlane plane) const return m_planes[plane]; } +template +nzIntersectionSide NzFrustum::Intersect(const NzAxisAlignedBox& box) const +{ + switch (box.extend) + { + case nzExtend_Finite: + return Intersect(box.cube); + + case nzExtend_Infinite: + return nzIntersectionSide_Intersecting; + + case nzExtend_Null: + return nzIntersectionSide_Outside; + } + + NazaraError("Invalid extend type (0x" + NzString::Number(box.extend, 16) + ')'); + return nzIntersectionSide_Outside; +} + template nzIntersectionSide NzFrustum::Intersect(const NzCube& cube) const { @@ -363,7 +401,7 @@ nzIntersectionSide NzFrustum::Intersect(const NzSphere& sphere) const } template -nzIntersectionSide NzFrustum::Intersect(const NzVector3* points, unsigned int pointCount) +nzIntersectionSide NzFrustum::Intersect(const NzVector3* points, unsigned int pointCount) const { unsigned int c = 0; diff --git a/include/Nazara/Math/Plane.hpp b/include/Nazara/Math/Plane.hpp index 598f77c24..a9bf0a9a5 100644 --- a/include/Nazara/Math/Plane.hpp +++ b/include/Nazara/Math/Plane.hpp @@ -24,8 +24,8 @@ class NzPlane NzPlane(const NzPlane& plane) = default; ~NzPlane() = default; - T Distance(const NzVector3& point); - T Distance(T x, T y, T z); + T Distance(const NzVector3& point) const; + T Distance(T x, T y, T z) const; NzPlane& Set(T normalX, T normalY, T normalZ, T Distance); NzPlane& Set(const T plane[4]); diff --git a/include/Nazara/Math/Plane.inl b/include/Nazara/Math/Plane.inl index 5cfd7e5c6..5a3f4145d 100644 --- a/include/Nazara/Math/Plane.inl +++ b/include/Nazara/Math/Plane.inl @@ -47,13 +47,13 @@ NzPlane::NzPlane(const NzPlane& plane) } template -T NzPlane::Distance(const NzVector3& point) +T NzPlane::Distance(const NzVector3& point) const { return normal.DotProduct(point) + distance; } template -T NzPlane::Distance(T x, T y, T z) +T NzPlane::Distance(T x, T y, T z) const { return Distance(NzVector3(x, y, z)); }