Bringing across Region enhancements from Cubiquity branch.

This commit is contained in:
David Williams 2013-08-09 20:39:17 +02:00
parent b9546ddcfa
commit 6fb30a40f5
2 changed files with 64 additions and 6 deletions

View File

@ -74,6 +74,12 @@ namespace PolyVox
/// Inequality Operator.
bool operator!=(const Region& rhs) const;
/// Gets the 'x' position of the centre.
int32_t getCentreX(void) const;
/// Gets the 'y' position of the centre.
int32_t getCentreY(void) const;
/// Gets the 'z' position of the centrer.
int32_t getCentreZ(void) const;
/// Gets the 'x' position of the lower corner.
int32_t getLowerX(void) const;
/// Gets the 'y' position of the lower corner.
@ -87,6 +93,8 @@ namespace PolyVox
/// Gets the 'z' position of the upper corner.
int32_t getUpperZ(void) const;
/// Gets the centre of the region
Vector3DInt32 getCentre(void) const;
/// Gets the position of the lower corner.
Vector3DInt32 getLowerCorner(void) const;
/// Gets the position of the upper corner.
@ -202,6 +210,9 @@ namespace PolyVox
int32_t m_iUpperZ;
};
// Non-member functions
bool intersects(const Region& a, const Region& b);
// Non-member overloaded operators.
/// Stream insertion operator.
std::ostream& operator<<(std::ostream& os, const Region& region);
@ -210,6 +221,30 @@ namespace PolyVox
// 'inline' keyword is used for the definition rather than the declaration.
// See also http://www.parashift.com/c++-faq-lite/inline-functions.html
/**
* \return The 'x' position of the centre.
*/
inline int32_t Region::getCentreX(void) const
{
return (m_iLowerX + m_iUpperX) / 2;
}
/**
* \return The 'y' position of the centre.
*/
inline int32_t Region::getCentreY(void) const
{
return (m_iLowerY + m_iUpperY) / 2;
}
/**
* \return The 'z' position of the centre.
*/
inline int32_t Region::getCentreZ(void) const
{
return (m_iLowerZ + m_iUpperZ) / 2;
}
/**
* \return The 'x' position of the lower corner.
*/
@ -258,6 +293,14 @@ namespace PolyVox
return m_iUpperZ;
}
/**
* \return The position of the lower corner.
*/
inline Vector3DInt32 Region::getCentre(void) const
{
return Vector3DInt32(getCentreX(), getCentreY(), getCentreZ());
}
/**
* \return The position of the lower corner.
*/

View File

@ -150,12 +150,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);
@ -488,6 +488,21 @@ 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;
}
/**
* Enables the Region to be used intuitively with output streams such as cout.
* \param os The output stream to write to.