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    #[doc(alias = "graphene_ray_init")]
11    pub fn new(origin: Option<&Point3D>, direction: Option<&Vec3>) -> Self {
12        assert_initialized_main_thread!();
13        unsafe {
14            let mut ray = Self::uninitialized();
15            ffi::graphene_ray_init(
16                ray.to_glib_none_mut().0,
17                origin.to_glib_none().0,
18                direction.to_glib_none().0,
19            );
20            ray
21        }
22    }
23
24    #[doc(alias = "graphene_ray_init_from_vec3")]
25    #[doc(alias = "init_from_vec3")]
26    pub fn from_vec3(origin: Option<&Vec3>, direction: Option<&Vec3>) -> Self {
27        assert_initialized_main_thread!();
28        unsafe {
29            let mut ray = Self::uninitialized();
30            ffi::graphene_ray_init_from_vec3(
31                ray.to_glib_none_mut().0,
32                origin.to_glib_none().0,
33                direction.to_glib_none().0,
34            );
35            ray
36        }
37    }
38
39    #[doc(alias = "graphene_ray_intersect_box")]
40    pub fn intersect_box(&self, b: &Box) -> (RayIntersectionKind, Option<f32>) {
41        unsafe {
42            let mut t_out = std::mem::MaybeUninit::uninit();
43            let ret = from_glib(ffi::graphene_ray_intersect_box(
44                self.to_glib_none().0,
45                b.to_glib_none().0,
46                t_out.as_mut_ptr(),
47            ));
48            match ret {
49                RayIntersectionKind::None => (ret, None),
50                _ => (ret, Some(t_out.assume_init())),
51            }
52        }
53    }
54
55    #[doc(alias = "graphene_ray_intersect_sphere")]
56    pub fn intersect_sphere(&self, s: &Sphere) -> (RayIntersectionKind, Option<f32>) {
57        unsafe {
58            let mut t_out = std::mem::MaybeUninit::uninit();
59            let ret = from_glib(ffi::graphene_ray_intersect_sphere(
60                self.to_glib_none().0,
61                s.to_glib_none().0,
62                t_out.as_mut_ptr(),
63            ));
64            match ret {
65                RayIntersectionKind::None => (ret, None),
66                _ => (ret, Some(t_out.assume_init())),
67            }
68        }
69    }
70
71    #[doc(alias = "graphene_ray_intersect_triangle")]
72    pub fn intersect_triangle(&self, t: &Triangle) -> (RayIntersectionKind, Option<f32>) {
73        unsafe {
74            let mut t_out = std::mem::MaybeUninit::uninit();
75            let ret = from_glib(ffi::graphene_ray_intersect_triangle(
76                self.to_glib_none().0,
77                t.to_glib_none().0,
78                t_out.as_mut_ptr(),
79            ));
80            match ret {
81                RayIntersectionKind::None => (ret, None),
82                _ => (ret, Some(t_out.assume_init())),
83            }
84        }
85    }
86}
87
88impl fmt::Debug for Ray {
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        f.debug_struct("Ray")
91            .field("origin", &self.origin())
92            .field("direction", &self.direction())
93            .finish()
94    }
95}