graphene/
ray.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::fmt;
4
5use glib::translate::*;
6
7use crate::{ffi, Box, Point3D, Ray, RayIntersectionKind, Sphere, Triangle, Vec3};
8
9impl Ray {
10    /// Initializes the given [`Ray`][crate::Ray] using the given `origin`
11    /// and `direction` values.
12    /// ## `origin`
13    /// the origin of the ray
14    /// ## `direction`
15    /// the direction vector
16    ///
17    /// # Returns
18    ///
19    /// the initialized ray
20    #[doc(alias = "graphene_ray_init")]
21    pub fn new(origin: Option<&Point3D>, direction: Option<&Vec3>) -> Self {
22        assert_initialized_main_thread!();
23        unsafe {
24            let mut ray = Self::uninitialized();
25            ffi::graphene_ray_init(
26                ray.to_glib_none_mut().0,
27                origin.to_glib_none().0,
28                direction.to_glib_none().0,
29            );
30            ray
31        }
32    }
33
34    /// Initializes the given [`Ray`][crate::Ray] using the given vectors.
35    /// ## `origin`
36    /// a [`Vec3`][crate::Vec3]
37    /// ## `direction`
38    /// a [`Vec3`][crate::Vec3]
39    ///
40    /// # Returns
41    ///
42    /// the initialized ray
43    #[doc(alias = "graphene_ray_init_from_vec3")]
44    #[doc(alias = "init_from_vec3")]
45    pub fn from_vec3(origin: Option<&Vec3>, direction: Option<&Vec3>) -> Self {
46        assert_initialized_main_thread!();
47        unsafe {
48            let mut ray = Self::uninitialized();
49            ffi::graphene_ray_init_from_vec3(
50                ray.to_glib_none_mut().0,
51                origin.to_glib_none().0,
52                direction.to_glib_none().0,
53            );
54            ray
55        }
56    }
57
58    /// Intersects the given [`Ray`][crate::Ray] `self` with the given
59    /// [`Box`][crate::Box] `b`.
60    /// ## `b`
61    /// a [`Box`][crate::Box]
62    ///
63    /// # Returns
64    ///
65    /// the type of intersection
66    ///
67    /// ## `t_out`
68    /// the distance of the point on the ray that intersects the box
69    #[doc(alias = "graphene_ray_intersect_box")]
70    pub fn intersect_box(&self, b: &Box) -> (RayIntersectionKind, Option<f32>) {
71        unsafe {
72            let mut t_out = std::mem::MaybeUninit::uninit();
73            let ret = from_glib(ffi::graphene_ray_intersect_box(
74                self.to_glib_none().0,
75                b.to_glib_none().0,
76                t_out.as_mut_ptr(),
77            ));
78            match ret {
79                RayIntersectionKind::None => (ret, None),
80                _ => (ret, Some(t_out.assume_init())),
81            }
82        }
83    }
84
85    /// Intersects the given [`Ray`][crate::Ray] `self` with the given
86    /// [`Sphere`][crate::Sphere] `s`.
87    /// ## `s`
88    /// a [`Sphere`][crate::Sphere]
89    ///
90    /// # Returns
91    ///
92    /// the type of intersection
93    ///
94    /// ## `t_out`
95    /// the distance of the point on the ray that intersects the sphere
96    #[doc(alias = "graphene_ray_intersect_sphere")]
97    pub fn intersect_sphere(&self, s: &Sphere) -> (RayIntersectionKind, Option<f32>) {
98        unsafe {
99            let mut t_out = std::mem::MaybeUninit::uninit();
100            let ret = from_glib(ffi::graphene_ray_intersect_sphere(
101                self.to_glib_none().0,
102                s.to_glib_none().0,
103                t_out.as_mut_ptr(),
104            ));
105            match ret {
106                RayIntersectionKind::None => (ret, None),
107                _ => (ret, Some(t_out.assume_init())),
108            }
109        }
110    }
111
112    /// Intersects the given [`Ray`][crate::Ray] `self` with the given
113    /// [`Triangle`][crate::Triangle] `t`.
114    /// ## `t`
115    /// a [`Triangle`][crate::Triangle]
116    ///
117    /// # Returns
118    ///
119    /// the type of intersection
120    ///
121    /// ## `t_out`
122    /// the distance of the point on the ray that intersects the triangle
123    #[doc(alias = "graphene_ray_intersect_triangle")]
124    pub fn intersect_triangle(&self, t: &Triangle) -> (RayIntersectionKind, Option<f32>) {
125        unsafe {
126            let mut t_out = std::mem::MaybeUninit::uninit();
127            let ret = from_glib(ffi::graphene_ray_intersect_triangle(
128                self.to_glib_none().0,
129                t.to_glib_none().0,
130                t_out.as_mut_ptr(),
131            ));
132            match ret {
133                RayIntersectionKind::None => (ret, None),
134                _ => (ret, Some(t_out.assume_init())),
135            }
136        }
137    }
138}
139
140impl fmt::Debug for Ray {
141    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142        f.debug_struct("Ray")
143            .field("origin", &self.origin())
144            .field("direction", &self.direction())
145            .finish()
146    }
147}