graphene/
box_.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, Vec3};
8
9impl Box {
10    /// Computes the vertices of the given [`Box`][crate::Box].
11    ///
12    /// # Returns
13    ///
14    ///
15    /// ## `vertices`
16    /// return location for an array
17    ///  of 8 [`Vec3`][crate::Vec3]
18    #[doc(alias = "graphene_box_get_vertices")]
19    #[doc(alias = "get_vertices")]
20    pub fn vertices(&self) -> &[Vec3; 8] {
21        unsafe {
22            let mut out: [ffi::graphene_vec3_t; 8] = std::mem::zeroed();
23            ffi::graphene_box_get_vertices(self.to_glib_none().0, &mut out as *mut _);
24            &*(&out as *const [ffi::graphene_vec3_t; 8] as *const [Vec3; 8])
25        }
26    }
27
28    /// Initializes the given [`Box`][crate::Box] with two vertices.
29    /// ## `min`
30    /// the coordinates of the minimum vertex
31    /// ## `max`
32    /// the coordinates of the maximum vertex
33    ///
34    /// # Returns
35    ///
36    /// the initialized [`Box`][crate::Box]
37    #[doc(alias = "graphene_box_init")]
38    pub fn new(min: Option<&Point3D>, max: Option<&Point3D>) -> Self {
39        assert_initialized_main_thread!();
40        unsafe {
41            let mut b = Self::uninitialized();
42            ffi::graphene_box_init(
43                b.to_glib_none_mut().0,
44                min.to_glib_none().0,
45                max.to_glib_none().0,
46            );
47            b
48        }
49    }
50
51    /// Initializes the given [`Box`][crate::Box] with the given array
52    /// of vertices.
53    ///
54    /// If `n_points` is 0, the returned box is initialized with
55    /// [`empty()`][Self::empty()].
56    /// ## `points`
57    /// an array of [`Point3D`][crate::Point3D]
58    ///
59    /// # Returns
60    ///
61    /// the initialized [`Box`][crate::Box]
62    #[doc(alias = "graphene_box_init_from_points")]
63    #[doc(alias = "init_from_points")]
64    pub fn from_points(points: &[Point3D]) -> Self {
65        assert_initialized_main_thread!();
66
67        let n = points.len() as u32;
68
69        unsafe {
70            let mut b = Self::uninitialized();
71            ffi::graphene_box_init_from_points(b.to_glib_none_mut().0, n, points.to_glib_none().0);
72            b
73        }
74    }
75
76    /// Initializes the given [`Box`][crate::Box] with two vertices
77    /// stored inside [`Vec3`][crate::Vec3].
78    /// ## `min`
79    /// the coordinates of the minimum vertex
80    /// ## `max`
81    /// the coordinates of the maximum vertex
82    ///
83    /// # Returns
84    ///
85    /// the initialized [`Box`][crate::Box]
86    #[doc(alias = "graphene_box_init_from_vec3")]
87    #[doc(alias = "init_from_vec3")]
88    pub fn from_vec3(min: Option<&Vec3>, max: Option<&Vec3>) -> Self {
89        assert_initialized_main_thread!();
90        unsafe {
91            let mut b = Self::uninitialized();
92            ffi::graphene_box_init_from_vec3(
93                b.to_glib_none_mut().0,
94                min.to_glib_none().0,
95                max.to_glib_none().0,
96            );
97            b
98        }
99    }
100
101    /// Initializes the given [`Box`][crate::Box] with the given array
102    /// of vertices.
103    ///
104    /// If `n_vectors` is 0, the returned box is initialized with
105    /// [`empty()`][Self::empty()].
106    /// ## `vectors`
107    /// an array of [`Vec3`][crate::Vec3]
108    ///
109    /// # Returns
110    ///
111    /// the initialized [`Box`][crate::Box]
112    #[doc(alias = "graphene_box_init_from_vectors")]
113    #[doc(alias = "init_from_vectors")]
114    pub fn from_vectors(vectors: &[Vec3]) -> Self {
115        assert_initialized_main_thread!();
116
117        let n = vectors.len() as u32;
118
119        unsafe {
120            let mut b = Self::uninitialized();
121            ffi::graphene_box_init_from_vectors(
122                b.to_glib_none_mut().0,
123                n,
124                vectors.to_glib_none().0,
125            );
126            b
127        }
128    }
129}
130
131impl Default for Box {
132    fn default() -> Self {
133        Self::zero()
134    }
135}
136
137impl fmt::Debug for Box {
138    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139        f.debug_struct("Box")
140            .field("min", &self.min())
141            .field("max", &self.max())
142            .finish()
143    }
144}