MinGfx Toolkit
1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
|
Stores the mathematical object of a ray that begins at an origin (a 3D point) and points in a direction (a unit 3D vector).
Rays can intersect a variety of other computer graphics objects, such as planes, triangles, spheres, 3D meshes, etc. These intersections can be tested with the Intersect...() methods. The Ray can also be transformed by a Matrix4. Example:
#include <ray.h>
Public Member Functions | |
Ray () | |
Defaults to a ray at the origin and pointing in the -Z direction. More... | |
Ray (const Point3 &origin, const Vector3 &direction) | |
Creates a ray from a 3D origin and direction. More... | |
virtual | ~Ray () |
Ray destructor. More... | |
bool | operator== (const Ray &other) const |
Check for "equality", taking floating point imprecision into account. More... | |
bool | operator!= (const Ray &other) const |
Check for "inequality", taking floating point imprecision into account. More... | |
float | Length () const |
Returns the length of the direction vector. More... | |
bool | IntersectPlane (const Point3 &planePt, const Vector3 &planeNormal, float *iTime, Point3 *iPoint) const |
Checks to see if the ray intersects a plane defined by a point and a normal. More... | |
bool | IntersectTriangle (const Point3 &v1, const Point3 &v2, const Point3 &v3, float *iTime, Point3 *iPoint) const |
Checks to see if the ray intersects a triangle defined by the vertices v1, v2, and v3. More... | |
bool | IntersectQuad (const Point3 &v1, const Point3 &v2, const Point3 &v3, const Point3 &v4, float *iTime, Point3 *iPoint) const |
Checks to see if the ray intersects a quad defined by the vertices v1, v2, v3, and v4. More... | |
bool | IntersectSphere (const Point3 ¢er, float radius, float *iTime, Point3 *iPoint) const |
Checks to see if the ray intersects a sphere defined by a center point and a radius. More... | |
bool | IntersectMesh (const Mesh &mesh, float *iTime, Point3 *iPoint, int *iTriangleID) const |
Checks to see if the ray intersects a triangle mesh. More... | |
bool | FastIntersectMesh (Mesh *mesh, float *iTime, Point3 *iPoint, int *iTriangleID) const |
Checks to see if the ray intersects a triangle mesh. More... | |
bool | IntersectAABB (const AABB &box, float *iTime) const |
Checks to see if the ray intersects an AABB (Axis-Aligned Bounding Box). More... | |
Point3 | origin () const |
Returns the origin. More... | |
Vector3 | direction () const |
Returns the direction. More... | |
void | set (Point3 newOrigin, Vector3 newDir) |
Sets a new origin and direction. More... | |
mingfx::Ray::Ray | ( | ) |
Defaults to a ray at the origin and pointing in the -Z direction.
Creates a ray from a 3D origin and direction.
|
virtual |
Ray destructor.
Vector3 mingfx::Ray::direction | ( | ) | const |
Returns the direction.
bool mingfx::Ray::FastIntersectMesh | ( | Mesh * | mesh, |
float * | iTime, | ||
Point3 * | iPoint, | ||
int * | iTriangleID | ||
) | const |
Checks to see if the ray intersects a triangle mesh.
This uses a BVH (Bounding Volume Hierarchy) to accelerate the ray-triangle intersection tests. Each mesh can optionally store a BVH. If a BVH has already been calculated for the mesh (done with Mesh::CalculateBVH()), then this function will be much faster than the brute-force IntersectMesh() function. If a BVH has not already been calculated for the mesh, the first call to FastIntersectMesh() will trigger the mesh to create a BVH (not a fast operation) but then subsequent calls to FastIntersectMesh() will be fast.
bool mingfx::Ray::IntersectAABB | ( | const AABB & | box, |
float * | iTime | ||
) | const |
Checks to see if the ray intersects an AABB (Axis-Aligned Bounding Box).
Typically, this is the first step of a more detailed intersection test and we don't care about the actual point of intersection, just whether it intersects or not. So, we don't bother calculating the iPoint. We get the iTime for free though, so we do return that. You can calc the iPoint if you want using:
bool mingfx::Ray::IntersectMesh | ( | const Mesh & | mesh, |
float * | iTime, | ||
Point3 * | iPoint, | ||
int * | iTriangleID | ||
) | const |
Checks to see if the ray intersects a triangle mesh.
This is a brute-force check over each triangle in the mesh. If there was an intersection, true is returned, iTime is set to the intersection time, iPoint is set to the intersection point, and iTriangleID is set to the ID of the closest intersected triangle along the ray.
bool mingfx::Ray::IntersectPlane | ( | const Point3 & | planePt, |
const Vector3 & | planeNormal, | ||
float * | iTime, | ||
Point3 * | iPoint | ||
) | const |
Checks to see if the ray intersects a plane defined by a point and a normal.
If there was an intersection, true is returned, iTime is set to the intersection time, and iPoint is set to the intersection point. The plane is considered to be 1-sided. That is the intersection will only occur if the ray hits the plane from its front side as determined by the plane's normal.
bool mingfx::Ray::IntersectQuad | ( | const Point3 & | v1, |
const Point3 & | v2, | ||
const Point3 & | v3, | ||
const Point3 & | v4, | ||
float * | iTime, | ||
Point3 * | iPoint | ||
) | const |
Checks to see if the ray intersects a quad defined by the vertices v1, v2, v3, and v4.
The vertices must be provided in counter-clockwise order so that the normal of the triangle can be determined via the right-hand rule. The intersection will only happen if the ray hits the front side of the triangle. If there was an intersection, true is returned, iTime is set to the intersection time, and iPoint is set to the intersection point.
bool mingfx::Ray::IntersectSphere | ( | const Point3 & | center, |
float | radius, | ||
float * | iTime, | ||
Point3 * | iPoint | ||
) | const |
Checks to see if the ray intersects a sphere defined by a center point and a radius.
If there was an intersection, true is returned, iTime is set to the intersection time, and iPoint is set to the intersection point.
bool mingfx::Ray::IntersectTriangle | ( | const Point3 & | v1, |
const Point3 & | v2, | ||
const Point3 & | v3, | ||
float * | iTime, | ||
Point3 * | iPoint | ||
) | const |
Checks to see if the ray intersects a triangle defined by the vertices v1, v2, and v3.
The vertices must be provided in counter-clockwise order so that the normal of the triangle can be determined via the right-hand rule. The intersection will only happen if the ray hits the front side of the triangle. If there was an intersection, true is returned, iTime is set to the intersection time, and iPoint is set to the intersection point.
float mingfx::Ray::Length | ( | ) | const |
Returns the length of the direction vector.
bool mingfx::Ray::operator!= | ( | const Ray & | other | ) | const |
Check for "inequality", taking floating point imprecision into account.
bool mingfx::Ray::operator== | ( | const Ray & | other | ) | const |
Check for "equality", taking floating point imprecision into account.
Point3 mingfx::Ray::origin | ( | ) | const |
Returns the origin.