Commit fee5e8ab authored by Frédéric Larue's avatar Frédéric Larue

Box/ray intersection functions added.

parent e46b6bf0
...@@ -489,6 +489,17 @@ public: ...@@ -489,6 +489,17 @@ public:
* \return Box volume. * \return Box volume.
*/ */
inline float Volume() const; inline float Volume() const;
/** Performs an intersection between a ray and the box.
*
* \param[in]
*/
inline bool Intersects( const QVector3D& rayOrigin,
const QVector3D& rayDirection,
QVector3D& intersectionCoords,
float& intersectionDist );
inline bool Intersects( const QVector3D& rayOrigin,
const QVector3D& rayDirection );
}; };
...@@ -620,6 +631,58 @@ inline float Box3f::Volume() const ...@@ -620,6 +631,58 @@ inline float Box3f::Volume() const
} }
inline bool Box3f::Intersects( const QVector3D& rayOrigin,
const QVector3D& rayDirection,
QVector3D& intersectionCoords,
float& intersectionDist )
{
// For each of the three dimensions, only the plane that faces the ray is checked. This leads to three possible intersection
// points, but only the farthest positive one may correspond to the real ray/cube intersection, if it exists.
int intersectionPlaneId = -1;
intersectionDist = 0.0f;
for( int i=0; i<3; ++i )
{
float frontFacingPlane = (rayDirection[i] > 0.0f)? m_Min[i] : m_Max[i];
float t = (frontFacingPlane - rayOrigin[i]) / rayDirection[i];
if( t > intersectionDist )
{
intersectionDist = t;
intersectionPlaneId = i;
}
}
if( intersectionPlaneId < 0 )
return false;
// Computes the coordinates of this ray/plane intersection, as well as its distance to the observer.
intersectionCoords = rayOrigin + intersectionDist * rayDirection;
// If the intersection point on the plane is inside the bounds of the cube face, the ray effectively intersects the cube.
int planeX = (intersectionPlaneId + 1) % 3;
int planeY = (intersectionPlaneId + 2) % 3;
bool isIntersecting = intersectionCoords[planeX] >= m_Min[planeX] &&
intersectionCoords[planeX] <= m_Max[planeX] &&
intersectionCoords[planeY] >= m_Min[planeY] &&
intersectionCoords[planeY] <= m_Max[planeY];
return isIntersecting;
}
inline bool Box3f::Intersects( const QVector3D& rayOrigin,
const QVector3D& rayDirection )
{
QVector3D intersectionCoords;
float intersectionDist;
return Intersects( rayOrigin, rayDirection, intersectionCoords, intersectionDist );
}
#endif /*__BOX_H__*/ #endif /*__BOX_H__*/
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment