diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h index 2053c351..77dac7a4 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/Utility.h @@ -40,12 +40,12 @@ namespace PolyVox inline int32_t roundTowardsNegInf(float r) { - return (r > 0.0) ? static_cast(r) : static_cast(r - 1.0f); + return (r >= 0.0) ? static_cast(r) : static_cast(r - 1.0f); } inline int32_t roundToNearestInteger(float r) { - return (r > 0.0) ? static_cast(r + 0.5f) : static_cast(r - 0.5f); + return (r >= 0.0) ? static_cast(r + 0.5f) : static_cast(r - 0.5f); } template diff --git a/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h b/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h index f0ef1c6e..cae21b67 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h +++ b/library/PolyVoxCore/include/PolyVoxCore/RawVolume.h @@ -117,6 +117,8 @@ namespace PolyVox /// Destructor ~RawVolume(); + VoxelType getInterpolatedValue(float fXPos, float fYPos, float fZPos); //Should be const + VoxelType getInterpolatedValue(const Vector3DFloat& v3dPos); //Should be const /// Gets a voxel at the position given by x,y,z coordinates VoxelType getVoxel(int32_t uXPos, int32_t uYPos, int32_t uZPos) const; /// Gets a voxel at the position given by a 3D vector diff --git a/library/PolyVoxCore/include/PolyVoxCore/RawVolume.inl b/library/PolyVoxCore/include/PolyVoxCore/RawVolume.inl index 1fafad5a..ae91695c 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/RawVolume.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/RawVolume.inl @@ -73,6 +73,45 @@ namespace PolyVox assert(false); // See function comment above. } + template + VoxelType RawVolume::getInterpolatedValue(float fXPos, float fYPos, float fZPos) //Should be const + { + Sampler sampler(this); + + int32_t iLowerX = roundTowardsNegInf(fXPos); + int32_t iLowerY = roundTowardsNegInf(fYPos); + int32_t iLowerZ = roundTowardsNegInf(fZPos); + + float fOffsetX = fXPos - iLowerX; + float fOffsetY = fYPos - iLowerY; + float fOffsetZ = fZPos - iLowerZ; + + /*int32_t iCeilX = iFloorX + 1; + int32_t iCeilY = iFloorY + 1; + int32_t iCeilZ = iFloorZ + 1;*/ + + sampler.setPosition(iLowerX, iLowerY, iLowerZ); + + VoxelType v000 = sampler.peekVoxel0px0py0pz(); + VoxelType v100 = sampler.peekVoxel1px0py0pz(); + VoxelType v010 = sampler.peekVoxel0px1py0pz(); + VoxelType v110 = sampler.peekVoxel1px1py0pz(); + VoxelType v001 = sampler.peekVoxel0px0py1pz(); + VoxelType v101 = sampler.peekVoxel1px0py1pz(); + VoxelType v011 = sampler.peekVoxel0px1py1pz(); + VoxelType v111 = sampler.peekVoxel1px1py1pz(); + + VoxelType result = trilerp(v000, v100, v010, v110, v001, v101, v011, v111, fOffsetX, fOffsetY, fOffsetZ); + + return result; + } + + template + VoxelType RawVolume::getInterpolatedValue(const Vector3DFloat& v3dPos) //Should be const + { + return getInterpolatedValue(v3dPos.getX(), v3dPos.getY(), v3dPos.getZ()); + } + //////////////////////////////////////////////////////////////////////////////// /// \param uXPos The \c x position of the voxel /// \param uYPos The \c y position of the voxel