From 1d1dcc875a8dcf55cb550f06f611f610710fd354 Mon Sep 17 00:00:00 2001 From: David Williams Date: Mon, 14 Jan 2013 23:13:17 +0100 Subject: [PATCH] Added 'intersects' function to Region. --- .../PolyVoxCore/include/PolyVoxCore/Region.h | 2 ++ library/PolyVoxCore/source/Region.cpp | 25 ++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/library/PolyVoxCore/include/PolyVoxCore/Region.h b/library/PolyVoxCore/include/PolyVoxCore/Region.h index d65fb022..32aa7715 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Region.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Region.h @@ -207,6 +207,8 @@ namespace PolyVox int32_t m_iUpperZ; }; + bool intersects(const Region& a, const Region& b); + // Functions to be inlined to to be in the header rather than the .cpp. // 'inline' keyword is used for the definition rather than the declaration. // See also http://www.parashift.com/c++-faq-lite/inline-functions.html diff --git a/library/PolyVoxCore/source/Region.cpp b/library/PolyVoxCore/source/Region.cpp index dee56bb3..b3512b6f 100644 --- a/library/PolyVoxCore/source/Region.cpp +++ b/library/PolyVoxCore/source/Region.cpp @@ -148,12 +148,12 @@ namespace PolyVox && (m_iUpperX == rhs.m_iUpperX) && (m_iUpperY == rhs.m_iUpperY) && (m_iUpperZ == rhs.m_iUpperZ)); } - //////////////////////////////////////////////////////////////////////////////// - /// Two regions are considered different if any of their extents differ. - /// \param rhs The Region to compare to. - /// \return true if the Regions are different. - /// \sa operator== - //////////////////////////////////////////////////////////////////////////////// + /** + * Two regions are considered different if any of their extents differ. + * \param rhs The Region to compare to. + * \return true if the Regions are different. + * \sa operator== + */ bool Region::operator!=(const Region& rhs) const { return !(*this == rhs); @@ -468,4 +468,17 @@ namespace PolyVox { shrink(v3dAmount.getX(), v3dAmount.getY(), v3dAmount.getZ()); } + /** + * This function only returns true if the regions are really intersecting and not simply touching. + */ + bool intersects(const Region& a, const Region& b) + { + // No intersection if seperated along an axis. + if(a.getUpperX() < b.getLowerX() || a.getLowerX() > b.getUpperX()) return false; + if(a.getUpperY() < b.getLowerY() || a.getLowerY() > b.getUpperY()) return false; + if(a.getUpperZ() < b.getLowerZ() || a.getLowerZ() > b.getUpperZ()) return false; + + // Overlapping on all axes means Regions are intersecting. + return true; + } }