Initial work on unclassing raycast.

This commit is contained in:
David Williams
2012-10-02 16:03:39 +02:00
parent ca66396631
commit 23184e6924
3 changed files with 130 additions and 3 deletions

View File

@ -120,6 +120,90 @@ namespace PolyVox
Vector3DFloat m_v3dDirectionAndLength;
float m_fMaxDistance;
};
template<typename VolumeType, typename Callback>
void raycast(VolumeType* volData, /*const*/ Vector3DFloat/*&*/ v3dStart, const Vector3DFloat& v3dDirectionAndLength, Callback& callback)
{
VolumeType::Sampler sampler(volData);
//The doRaycast function is assuming that it is iterating over the areas defined between
//voxels. We actually want to define the areas as being centered on voxels (as this is
//what the CubicSurfaceExtractor generates). We add (0.5,0.5,0.5) here to adjust for this.
v3dStart = v3dStart + Vector3DFloat(0.5f, 0.5f, 0.5f);
//Compute the end point
Vector3DFloat v3dEnd = v3dStart + v3dDirectionAndLength;
float x1 = v3dStart.getX();
float y1 = v3dStart.getY();
float z1 = v3dStart.getZ();
float x2 = v3dEnd.getX();
float y2 = v3dEnd.getY();
float z2 = v3dEnd.getZ();
int i = (int)floorf(x1);
int j = (int)floorf(y1);
int k = (int)floorf(z1);
int iend = (int)floorf(x2);
int jend = (int)floorf(y2);
int kend = (int)floorf(z2);
int di = ((x1 < x2) ? 1 : ((x1 > x2) ? -1 : 0));
int dj = ((y1 < y2) ? 1 : ((y1 > y2) ? -1 : 0));
int dk = ((z1 < z2) ? 1 : ((z1 > z2) ? -1 : 0));
float deltatx = 1.0f / std::abs(x2 - x1);
float deltaty = 1.0f / std::abs(y2 - y1);
float deltatz = 1.0f / std::abs(z2 - z1);
float minx = floorf(x1), maxx = minx + 1.0f;
float tx = ((x1 > x2) ? (x1 - minx) : (maxx - x1)) * deltatx;
float miny = floorf(y1), maxy = miny + 1.0f;
float ty = ((y1 > y2) ? (y1 - miny) : (maxy - y1)) * deltaty;
float minz = floorf(z1), maxz = minz + 1.0f;
float tz = ((z1 > z2) ? (z1 - minz) : (maxz - z1)) * deltatz;
sampler.setPosition(i,j,k);
//m_result.previousVoxel = Vector3DInt32(i,j,k);
for(;;)
{
if(!callback(sampler))
{
//m_result.foundIntersection = true;
//m_result.intersectionVoxel = Vector3DInt32(i,j,k);
return;
}
//m_result.previousVoxel = Vector3DInt32(i,j,k);
if(tx <= ty && tx <= tz)
{
if(i == iend) break;
tx += deltatx;
i += di;
if(di == 1) sampler.movePositiveX();
if(di == -1) sampler.moveNegativeX();
} else if (ty <= tz)
{
if(j == jend) break;
ty += deltaty;
j += dj;
if(dj == 1) sampler.movePositiveY();
if(dj == -1) sampler.moveNegativeY();
} else
{
if(k == kend) break;
tz += deltatz;
k += dk;
if(dk == 1) sampler.movePositiveZ();
if(dk == -1) sampler.moveNegativeZ();
}
}
}
}
#include "PolyVoxCore/Raycast.inl"