Ray

Struct Ray 

Source
#[repr(C)]
pub struct Ray { pub origin: Point3, pub direction: UnitVec3, }
Expand description

A ray composed of an origin point and a unit direction vector.

A ray represents a half-line that starts at the origin point and extends infinitely in the direction specified by the unit vector. Rays are fundamental for ray casting, intersection testing, and various geometric algorithms.

§Mathematical Representation

A ray can be parametrically represented as: R(t) = origin + t * direction where t >= 0

§Examples

use phys_geom::{Ray, math::Point3, math::Vec3};

// Create a ray from the origin along the X-axis
let origin = Point3::new(0.0, 0.0, 0.0);
let direction = Vec3::x_axis();
let ray = Ray::new(origin, direction);

// Create a ray using automatic normalization
let ray2 = Ray::new_with_vec3(origin, Vec3::new(2.0, 0.0, 0.0));

§Memory Layout

The struct uses #[repr(C)] for FFI compatibility and stores the origin as a Point3 and direction as a UnitVec3 (guaranteed to be normalized).

§Common Use Cases

  • Collision Detection: Testing intersections with geometric shapes
  • Picking: Selecting objects in 3D scenes
  • Visibility Testing: Determining line-of-sight
  • Rendering: Ray tracing and casting algorithms

Fields§

§origin: Point3

The starting point of the ray

§direction: UnitVec3

The direction of the ray (guaranteed to be a unit vector)

Implementations§

Source§

impl Ray

Source

pub fn new(origin: Point3, direction: UnitVec3) -> Self

Creates a new ray with the specified origin and direction.

§Arguments
  • origin - The starting point of the ray
  • direction - The direction of the ray (must be a unit vector)
§Returns

A new Ray instance.

§Examples
use phys_geom::{Ray, math::Point3, math::Vec3, math::UnitVec3};

let origin = Point3::new(1.0, 2.0, 3.0);
let direction = UnitVec3::new_normalize(Vec3::new(1.0, 0.0, 0.0));
let ray = Ray::new(origin, direction);
Source

pub fn new_with_vec3( origin: impl Into<[Real; 3]>, direction: impl Into<[Real; 3]>, ) -> Self

Creates a new ray, automatically normalizing the direction vector.

This is a convenience method that creates a ray from any non-zero direction vector by automatically normalizing it to unit length.

§Arguments
  • origin - The starting point of the ray
  • direction - The direction vector (will be normalized)
§Returns

A new Ray instance with normalized direction.

§Panics

Will panic if direction is zero or infinite length.

§Examples
use phys_geom::{Ray, math::Point3, math::Vec3, math::UnitVec3};

let origin = Point3::new(0.0, 0.0, 0.0);
let direction = Vec3::new(3.0, 4.0, 0.0); // Not normalized
let ray = Ray::new_with_vec3(origin, direction);

// Direction is automatically normalized to unit length
let dir_array = ray.direction.as_ref();
let dir_length = (dir_array[0].powi(2) + dir_array[1].powi(2) + dir_array[2].powi(2)).sqrt();
assert!((dir_length - 1.0).abs() < 1e-6);
Source

pub fn compute_one_over_direction(direction: UnitVec3) -> Vec3

Computes the reciprocal of direction components for efficient Ray-AABB intersection testing.

This method computes optimized values used in slab-based ray-AABB intersection algorithms. The result incorporates sign information and avoids division by zero by using a small epsilon.

§Mathematical Details

For each component of the direction vector, computes: result[i] = sign(direction[i]) / max(|direction[i]|, ε) where ε = 1e-15 to prevent division by zero.

§Arguments
  • direction - The unit direction vector to process
§Returns

A Vec3 containing the optimized reciprocal values. Note that this is not a unit vector.

§See Also
§Examples
use phys_geom::{Ray, math::Vec3, math::UnitVec3};

let direction = UnitVec3::new_normalize(Vec3::new(1.0, 2.0, 3.0));
let one_over = Ray::compute_one_over_direction(direction);

// The result preserves sign information
assert!(one_over.x > 0.0);
assert!(one_over.y > 0.0);
assert!(one_over.z > 0.0);
Source

pub fn one_over_direction(&self) -> Vec3

Computes the reciprocal of this ray’s direction for Ray-AABB intersection testing.

This is a convenience method that calls Self::compute_one_over_direction with this ray’s direction vector. The result is optimized for slab-based AABB intersection algorithms.

§Returns

A Vec3 containing optimized reciprocal values for this ray’s direction. Note that this is not a unit vector.

§Performance Note

Call this method once per ray and reuse the result for multiple AABB intersection tests to maximize performance.

§See Also
§Examples
use phys_geom::{Ray, math::Point3, math::Vec3};

let direction = Vec3::x_axis();
let ray = Ray::new(Point3::origin(), direction);
let one_over = ray.one_over_direction();

// Can be used for efficient AABB testing
// let intersects = aabb.raycast_by_one_over_direction(ray.origin, one_over, max_distance);
Source

pub fn inverse_transform(&self, transform: &Isometry3) -> Self

Applies the inverse of the given transformation to this ray.

This method transforms the ray by the inverse of the specified isometry, which is useful for transforming rays from world space to object space.

§Arguments
  • transform - The isometry to invert and apply to the ray
§Returns

A new Ray instance representing the transformed ray.

§Mathematical Details

The transformation applies:

  • Origin: origin' = transform⁻¹ * origin
  • Direction: direction' = transform⁻¹ * direction (rotation only)

Note that the direction is only affected by the rotational component of the transform, as directions are invariant under translation.

§Use Cases
  • Object-space ray casting: Transform world-space rays to object space
  • Hierarchical collision testing: Transform rays through scene graphs
  • Inverse transformations: Computing original rays from transformed ones
§Examples
use phys_geom::{Ray, math::Point3, math::Vec3, math::Isometry3};

let ray = Ray::new(Point3::new(1.0, 0.0, 0.0), Vec3::x_axis());
let transform = Isometry3::translation(1.0, 0.0, 0.0);

let transformed_ray = ray.inverse_transform(&transform);

// The ray origin is now at (0.0, 0.0, 0.0) in the new coordinate system
assert_eq!(transformed_ray.origin, Point3::new(0.0, 0.0, 0.0));

Trait Implementations§

Source§

impl Clone for Ray

Source§

fn clone(&self) -> Ray

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ray

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for Ray

Auto Trait Implementations§

§

impl Freeze for Ray

§

impl RefUnwindSafe for Ray

§

impl Send for Ray

§

impl Sync for Ray

§

impl Unpin for Ray

§

impl UnwindSafe for Ray

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.