Made it more obvious that the length of a raycast direction vector is important.

This commit is contained in:
unknown 2012-01-27 10:01:44 +01:00
parent d2cd3e7c62
commit 01af6b938a
4 changed files with 27 additions and 18 deletions

View File

@ -59,6 +59,14 @@ namespace PolyVox
/// of the RaycastResult structure and the intersectionFound flag is set to true, otherwise /// of the RaycastResult structure and the intersectionFound flag is set to true, otherwise
/// the intersectionFound flag is set to false. /// the intersectionFound flag is set to false.
/// ///
/// <b>Important Note:</b> These has been confusion in the past with people not realising
/// that the length of the direction vector is important. Most graphics API can provide
/// a camera position and view direction for picking purposes, but the view direction is
/// usually normalised (i.e. of length one). If you use this view direction directly you
/// will only iterate over a single voxel and won't find what you are looking for. Instead
/// you must scale the direction vector so that it's length represents the maximum distance
/// over which you want the ray to be cast.
///
/// The following code snippet shows how the class is used: /// The following code snippet shows how the class is used:
/// \code /// \code
/// Vector3DFloat start(rayOrigin.x(), rayOrigin.y(), rayOrigin.z()); /// Vector3DFloat start(rayOrigin.x(), rayOrigin.y(), rayOrigin.z());
@ -88,12 +96,12 @@ namespace PolyVox
{ {
public: public:
///Constructor ///Constructor
Raycast(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirection, RaycastResult& result); Raycast(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, RaycastResult& result);
///Sets the start position for the ray. ///Sets the start position for the ray.
void setStart(const Vector3DFloat& v3dStart); void setStart(const Vector3DFloat& v3dStart);
///Set the direction for the ray. ///Set the direction for the ray.
void setDirection(const Vector3DFloat& v3dDirection); void setDirection(const Vector3DFloat& v3dDirectionAndLength);
///Performs the raycast. ///Performs the raycast.
void execute(); void execute();
@ -107,7 +115,7 @@ namespace PolyVox
typename VolumeType<VoxelType>::Sampler m_sampVolume; typename VolumeType<VoxelType>::Sampler m_sampVolume;
Vector3DFloat m_v3dStart; Vector3DFloat m_v3dStart;
Vector3DFloat m_v3dDirection; Vector3DFloat m_v3dDirectionAndLength;
float m_fMaxDistance; float m_fMaxDistance;
}; };
} }

View File

@ -27,17 +27,17 @@ namespace PolyVox
/// Builds a Raycast object. /// Builds a Raycast object.
/// \param volData A pointer to the volume through which the ray will be cast. /// \param volData A pointer to the volume through which the ray will be cast.
/// \param v3dStart The starting position of the ray. /// \param v3dStart The starting position of the ray.
/// \param v3dDirection The direction of the ray. The length of this vector also /// \param v3dDirectionAndLength The direction of the ray. The length of this vector also
/// represents the length of the ray. /// represents the length of the ray.
/// \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& v3dDirection, RaycastResult& result) Raycast<VolumeType, VoxelType>::Raycast(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, RaycastResult& result)
: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_v3dDirection(v3dDirection) ,m_v3dDirectionAndLength(v3dDirectionAndLength)
{ {
} }
@ -51,13 +51,14 @@ namespace PolyVox
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
/// \param v3dDirection The direction of the ray. The length of this vector also /// \param v3dDirectionAndLength The direction of the ray. The length of this vector also
/// represents the length of the ray. /// represents the length of the ray.
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template< template<typename> class VolumeType, typename VoxelType> template< template<typename> class VolumeType, typename VoxelType>
void Raycast<VolumeType, VoxelType>::setDirection(const Vector3DFloat& v3dDirection) void Raycast<VolumeType, VoxelType>::setDirection(const Vector3DFloat& v3dDirectionAndLength)
{ {
m_v3dDirection = v3dDirection; //FIXME: We should add a warning when the ray direction is of length one, as this seems to be a common mistake.
m_v3dDirectionAndLength = v3dDirectionAndLength;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -72,7 +73,7 @@ namespace PolyVox
Vector3DFloat v3dStart = m_v3dStart + Vector3DFloat(0.5f, 0.5f, 0.5f); Vector3DFloat v3dStart = m_v3dStart + Vector3DFloat(0.5f, 0.5f, 0.5f);
//Compute the end point //Compute the end point
Vector3DFloat v3dEnd = v3dStart + m_v3dDirection; Vector3DFloat v3dEnd = v3dStart + m_v3dDirectionAndLength;
//Do the raycast //Do the raycast
doRaycast(v3dStart.getX(), v3dStart.getY(), v3dStart.getZ(), v3dEnd.getX(), v3dEnd.getY(), v3dEnd.getZ()); doRaycast(v3dStart.getX(), v3dStart.getY(), v3dStart.getZ(), v3dEnd.getX(), v3dEnd.getY(), v3dEnd.getZ());

View File

@ -33,12 +33,12 @@ namespace PolyVox
{ {
public: public:
///Constructor ///Constructor
RaycastWithCallback(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirection, polyvox_function<bool(const Vector3DInt32& position)> funcCallback); RaycastWithCallback(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, polyvox_function<bool(const Vector3DInt32& position)> funcCallback);
///Sets the start position for the ray. ///Sets the start position for the ray.
void setStart(const Vector3DFloat& v3dStart); void setStart(const Vector3DFloat& v3dStart);
///Set the direction for the ray. ///Set the direction for the ray.
void setDirection(const Vector3DFloat& v3dDirection); void setDirection(const Vector3DFloat& v3dDirectionAndLength);
///Performs the raycast. ///Performs the raycast.
void execute(); void execute();
@ -52,7 +52,7 @@ namespace PolyVox
typename VolumeType<VoxelType>::Sampler m_sampVolume; typename VolumeType<VoxelType>::Sampler m_sampVolume;
Vector3DFloat m_v3dStart; Vector3DFloat m_v3dStart;
Vector3DFloat m_v3dDirection; Vector3DFloat m_v3dDirectionAndLength;
float m_fMaxDistance; float m_fMaxDistance;
}; };
} }

View File

@ -24,11 +24,11 @@ freely, subject to the following restrictions:
namespace PolyVox namespace PolyVox
{ {
template< template<typename> class VolumeType, typename VoxelType> template< template<typename> class VolumeType, typename VoxelType>
RaycastWithCallback<VolumeType, VoxelType>::RaycastWithCallback(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirection, polyvox_function<bool(const Vector3DInt32& position)> funcCallback) RaycastWithCallback<VolumeType, VoxelType>::RaycastWithCallback(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirectionAndLength, polyvox_function<bool(const Vector3DInt32& position)> funcCallback)
:m_volData(volData) :m_volData(volData)
,m_sampVolume(volData) ,m_sampVolume(volData)
,m_v3dStart(v3dStart) ,m_v3dStart(v3dStart)
,m_v3dDirection(v3dDirection) ,m_v3dDirectionAndLength(v3dDirectionAndLength)
,m_funcCallback(funcCallback) ,m_funcCallback(funcCallback)
{ {
//Check the user provided a callback, because it //Check the user provided a callback, because it
@ -43,9 +43,9 @@ namespace PolyVox
} }
template< template<typename> class VolumeType, typename VoxelType> template< template<typename> class VolumeType, typename VoxelType>
void RaycastWithCallback<VolumeType, VoxelType>::setDirection(const Vector3DFloat& v3dDirection) void RaycastWithCallback<VolumeType, VoxelType>::setDirection(const Vector3DFloat& v3dDirectionAndLength)
{ {
m_v3dDirection = v3dDirection; m_v3dDirectionAndLength = v3dDirectionAndLength;
} }
template< template<typename> class VolumeType, typename VoxelType> template< template<typename> class VolumeType, typename VoxelType>
@ -57,7 +57,7 @@ namespace PolyVox
Vector3DFloat v3dStart = m_v3dStart + Vector3DFloat(0.5f, 0.5f, 0.5f); Vector3DFloat v3dStart = m_v3dStart + Vector3DFloat(0.5f, 0.5f, 0.5f);
//Compute the end point //Compute the end point
Vector3DFloat v3dEnd = v3dStart + m_v3dDirection; Vector3DFloat v3dEnd = v3dStart + m_v3dDirectionAndLength;
//Do the raycast //Do the raycast
doRaycast(v3dStart.getX(), v3dStart.getY(), v3dStart.getZ(), v3dEnd.getX(), v3dEnd.getY(), v3dEnd.getZ()); doRaycast(v3dStart.getX(), v3dStart.getY(), v3dStart.getZ(), v3dEnd.getX(), v3dEnd.getY(), v3dEnd.getZ());