1use std::fmt;
4
5use glib::translate::*;
6
7use crate::{ffi, Box, Point3D, Ray, RayIntersectionKind, Sphere, Triangle, Vec3};
8
9impl Ray {
10    #[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    #[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    #[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    #[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    #[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}