Added callback function to Raycast, which is used to determine when a ray should stop.

This commit is contained in:
unknown 2012-03-15 16:52:54 +01:00
parent f73b2a479b
commit d9192270b6
3 changed files with 12 additions and 4 deletions

View File

@ -96,7 +96,7 @@ namespace PolyVox
{ {
public: public:
///Constructor ///Constructor
Raycast(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, RaycastResult& result); Raycast(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, RaycastResult& result, polyvox_function<bool(const typename VolumeType<VoxelType>::Sampler& sampler)> funcIsPassable);
///Sets the start position for the ray. ///Sets the start position for the ray.
void setStart(const Vector3DFloat& v3dStart); void setStart(const Vector3DFloat& v3dStart);
@ -109,6 +109,8 @@ namespace PolyVox
private: private:
RaycastResult& m_result; RaycastResult& m_result;
polyvox_function<bool(const typename VolumeType<VoxelType>::Sampler& position)> m_funcIsPassable;
void doRaycast(float x1, float y1, float z1, float x2, float y2, float z2); void doRaycast(float x1, float y1, float z1, float x2, float y2, float z2);
VolumeType<VoxelType>* m_volData; VolumeType<VoxelType>* m_volData;

View File

@ -32,12 +32,13 @@ namespace PolyVox
/// \param result An instance of RaycastResult in which the result will be stored. /// \param result An instance of RaycastResult in which the result will be stored.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template< template<typename> class VolumeType, typename VoxelType> template< template<typename> class VolumeType, typename VoxelType>
Raycast<VolumeType, VoxelType>::Raycast(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, RaycastResult& result) Raycast<VolumeType, VoxelType>::Raycast(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, RaycastResult& result, polyvox_function<bool(const typename VolumeType<VoxelType>::Sampler& sampler)> funcIsPassable)
:m_result(result) :m_result(result)
,m_volData(volData) ,m_volData(volData)
,m_sampVolume(volData) ,m_sampVolume(volData)
,m_v3dStart(v3dStart) ,m_v3dStart(v3dStart)
,m_v3dDirectionAndLength(v3dDirectionAndLength) ,m_v3dDirectionAndLength(v3dDirectionAndLength)
,m_funcIsPassable(funcIsPassable)
{ {
} }
@ -139,7 +140,7 @@ namespace PolyVox
for(;;) for(;;)
{ {
if(m_sampVolume.getVoxel().getDensity() > VoxelType::getThreshold()) if(!m_funcIsPassable(m_sampVolume))
{ {
m_result.foundIntersection = true; m_result.foundIntersection = true;
m_result.intersectionVoxel = Vector3DInt32(i,j,k); m_result.intersectionVoxel = Vector3DInt32(i,j,k);

View File

@ -33,6 +33,11 @@ freely, subject to the following restrictions:
using namespace PolyVox; using namespace PolyVox;
bool isPassableByRay(const SimpleVolume<Density8>::Sampler& sampler)
{
return sampler.getVoxel().getDensity() < Density8::getThreshold();
}
void TestRaycast::testExecute() void TestRaycast::testExecute()
{ {
const int32_t uVolumeSideLength = 32; const int32_t uVolumeSideLength = 32;
@ -67,7 +72,7 @@ void TestRaycast::testExecute()
for(int ct = 0; ct < 1000000; ct++) for(int ct = 0; ct < 1000000; ct++)
{ {
RaycastResult result; RaycastResult result;
Raycast<SimpleVolume, Density8> raycast(&volData, start, randomUnitVectors[ct % 1024] * 1000.0f, result); Raycast<SimpleVolume, Density8> raycast(&volData, start, randomUnitVectors[ct % 1024] * 1000.0f, result, isPassableByRay);
raycast.execute(); raycast.execute();
if(result.foundIntersection) if(result.foundIntersection)
{ {