Made getVoxel() bounds checking optional, based on a function parameter.

This commit is contained in:
Daviw Williams 2013-05-17 14:32:20 +02:00
parent db1c2bf0f8
commit 617834bed4
8 changed files with 46 additions and 25 deletions

View File

@ -147,9 +147,9 @@ namespace PolyVox
/// Gets the length of the diagonal in voxels /// Gets the length of the diagonal in voxels
float getDiagonalLength(void) const; float getDiagonalLength(void) const;
/// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates /// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates
VoxelType getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos) const; VoxelType getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos, bool bPerformBoundsChecks = true) const;
/// Gets a voxel at the position given by a 3D vector /// Gets a voxel at the position given by a 3D vector
VoxelType getVoxel(const Vector3DInt32& v3dPos) const; VoxelType getVoxel(const Vector3DInt32& v3dPos, bool bPerformBoundsChecks = true) const;
/// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates /// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates
VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const; VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const;
/// Gets a voxel at the position given by a 3D vector /// Gets a voxel at the position given by a 3D vector

View File

@ -160,7 +160,7 @@ namespace PolyVox
/// \return The voxel value /// \return The voxel value
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename VoxelType> template <typename VoxelType>
VoxelType BaseVolume<VoxelType>::getVoxel(int32_t /*uXPos*/, int32_t /*uYPos*/, int32_t /*uZPos*/) const VoxelType BaseVolume<VoxelType>::getVoxel(int32_t /*uXPos*/, int32_t /*uYPos*/, int32_t /*uZPos*/, bool /*bPerformBoundsChecks*/) const
{ {
POLYVOX_THROW(not_implemented, "You should never call the base class version of this function."); POLYVOX_THROW(not_implemented, "You should never call the base class version of this function.");
return VoxelType(); return VoxelType();
@ -171,7 +171,7 @@ namespace PolyVox
/// \return The voxel value /// \return The voxel value
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename VoxelType> template <typename VoxelType>
VoxelType BaseVolume<VoxelType>::getVoxel(const Vector3DInt32& /*v3dPos*/) const VoxelType BaseVolume<VoxelType>::getVoxel(const Vector3DInt32& /*v3dPos*/, bool /*bPerformBoundsChecks*/) const
{ {
POLYVOX_THROW(not_implemented, "You should never call the base class version of this function."); POLYVOX_THROW(not_implemented, "You should never call the base class version of this function.");
return VoxelType(); return VoxelType();

View File

@ -266,9 +266,9 @@ namespace PolyVox
~LargeVolume(); ~LargeVolume();
/// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates /// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates
VoxelType getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos) const; VoxelType getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos, bool bPerformBoundsChecks = true) const;
/// Gets a voxel at the position given by a 3D vector /// Gets a voxel at the position given by a 3D vector
VoxelType getVoxel(const Vector3DInt32& v3dPos) const; VoxelType getVoxel(const Vector3DInt32& v3dPos, bool bPerformBoundsChecks = true) const;
/// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates /// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates
VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const; VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const;
/// Gets a voxel at the position given by a 3D vector /// Gets a voxel at the position given by a 3D vector

View File

@ -122,10 +122,17 @@ namespace PolyVox
/// \return The voxel value /// \return The voxel value
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename VoxelType> template <typename VoxelType>
VoxelType LargeVolume<VoxelType>::getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos) const VoxelType LargeVolume<VoxelType>::getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos, bool bPerformBoundsChecks) const
{ {
// PolyVox does not throw an exception when a voxel is out of range. Please see 'Error Handling' in the User Manual. // If bounds checking is enabled then we validate the
POLYVOX_ASSERT(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)), "Position is outside valid region"); // bounds, and throw an exception if they are violated.
if(bPerformBoundsChecks)
{
if(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)) == false)
{
POLYVOX_THROW(std::out_of_range, "Position is outside valid region");
}
}
const int32_t blockX = uXPos >> m_uBlockSideLengthPower; const int32_t blockX = uXPos >> m_uBlockSideLengthPower;
const int32_t blockY = uYPos >> m_uBlockSideLengthPower; const int32_t blockY = uYPos >> m_uBlockSideLengthPower;
@ -145,9 +152,9 @@ namespace PolyVox
/// \return The voxel value /// \return The voxel value
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename VoxelType> template <typename VoxelType>
VoxelType LargeVolume<VoxelType>::getVoxel(const Vector3DInt32& v3dPos) const VoxelType LargeVolume<VoxelType>::getVoxel(const Vector3DInt32& v3dPos, bool bPerformBoundsChecks) const
{ {
return getVoxel(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ()); return getVoxel(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), bPerformBoundsChecks);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -116,9 +116,9 @@ namespace PolyVox
~RawVolume(); ~RawVolume();
/// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates /// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates
VoxelType getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos) const; VoxelType getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos, bool bPerformBoundsChecks = true) const;
/// Gets a voxel at the position given by a 3D vector /// Gets a voxel at the position given by a 3D vector
VoxelType getVoxel(const Vector3DInt32& v3dPos) const; VoxelType getVoxel(const Vector3DInt32& v3dPos, bool bPerformBoundsChecks = true) const;
/// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates /// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates
VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const; VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const;
/// Gets a voxel at the position given by a 3D vector /// Gets a voxel at the position given by a 3D vector

View File

@ -80,10 +80,17 @@ namespace PolyVox
/// \return The voxel value /// \return The voxel value
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename VoxelType> template <typename VoxelType>
VoxelType RawVolume<VoxelType>::getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos) const VoxelType RawVolume<VoxelType>::getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos, bool bPerformBoundsChecks) const
{ {
// PolyVox does not throw an exception when a voxel is out of range. Please see 'Error Handling' in the User Manual. // If bounds checking is enabled then we validate the
POLYVOX_ASSERT(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)), "Position is outside valid region"); // bounds, and throw an exception if they are violated.
if(bPerformBoundsChecks)
{
if(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)) == false)
{
POLYVOX_THROW(std::out_of_range, "Position is outside valid region");
}
}
const Vector3DInt32& v3dLowerCorner = this->m_regValidRegion.getLowerCorner(); const Vector3DInt32& v3dLowerCorner = this->m_regValidRegion.getLowerCorner();
int32_t iLocalXPos = uXPos - v3dLowerCorner.getX(); int32_t iLocalXPos = uXPos - v3dLowerCorner.getX();
@ -103,9 +110,9 @@ namespace PolyVox
/// \return The voxel value /// \return The voxel value
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename VoxelType> template <typename VoxelType>
VoxelType RawVolume<VoxelType>::getVoxel(const Vector3DInt32& v3dPos) const VoxelType RawVolume<VoxelType>::getVoxel(const Vector3DInt32& v3dPos, bool bPerformBoundsChecks) const
{ {
return getVoxel(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ()); return getVoxel(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), bPerformBoundsChecks);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View File

@ -155,9 +155,9 @@ namespace PolyVox
~SimpleVolume(); ~SimpleVolume();
/// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates /// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates
VoxelType getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos) const; VoxelType getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos, bool bPerformBoundsChecks = true) const;
/// Gets a voxel at the position given by a 3D vector /// Gets a voxel at the position given by a 3D vector
VoxelType getVoxel(const Vector3DInt32& v3dPos) const; VoxelType getVoxel(const Vector3DInt32& v3dPos, bool bPerformBoundsChecks = true) const;
/// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates /// Gets a voxel at the position given by <tt>x,y,z</tt> coordinates
VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const; VoxelType getVoxelAt(int32_t uXPos, int32_t uYPos, int32_t uZPos) const;
/// Gets a voxel at the position given by a 3D vector /// Gets a voxel at the position given by a 3D vector

View File

@ -80,10 +80,17 @@ namespace PolyVox
/// \return The voxel value /// \return The voxel value
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename VoxelType> template <typename VoxelType>
VoxelType SimpleVolume<VoxelType>::getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos) const VoxelType SimpleVolume<VoxelType>::getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos, bool bPerformBoundsChecks) const
{ {
// PolyVox does not throw an exception when a voxel is out of range. Please see 'Error Handling' in the User Manual. // If bounds checking is enabled then we validate the
POLYVOX_ASSERT(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)), "Position is outside valid region"); // bounds, and throw an exception if they are violated.
if(bPerformBoundsChecks)
{
if(this->m_regValidRegion.containsPoint(Vector3DInt32(uXPos, uYPos, uZPos)) == false)
{
POLYVOX_THROW(std::out_of_range, "Position is outside valid region");
}
}
const int32_t blockX = uXPos >> m_uBlockSideLengthPower; const int32_t blockX = uXPos >> m_uBlockSideLengthPower;
const int32_t blockY = uYPos >> m_uBlockSideLengthPower; const int32_t blockY = uYPos >> m_uBlockSideLengthPower;
@ -103,9 +110,9 @@ namespace PolyVox
/// \return The voxel value /// \return The voxel value
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template <typename VoxelType> template <typename VoxelType>
VoxelType SimpleVolume<VoxelType>::getVoxel(const Vector3DInt32& v3dPos) const VoxelType SimpleVolume<VoxelType>::getVoxel(const Vector3DInt32& v3dPos, bool bPerformBoundsChecks) const
{ {
return getVoxel(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ()); return getVoxel(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ(), bPerformBoundsChecks);
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////