graphene/plane.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, Plane, Point3D, Vec3, Vec4};
8
9impl Plane {
10 /// Initializes the given [`Plane`][crate::Plane] using the given `normal` vector
11 /// and `constant` values.
12 /// ## `normal`
13 /// a unit length normal vector defining the plane
14 /// pointing towards the origin; if unset, we use the X axis by default
15 /// ## `constant`
16 /// the distance from the origin to the plane along the
17 /// normal vector; the sign determines the half-space occupied by the
18 /// plane
19 ///
20 /// # Returns
21 ///
22 /// the initialized plane
23 #[doc(alias = "graphene_plane_init")]
24 pub fn new(normal: Option<&Vec3>, constant: f32) -> Self {
25 assert_initialized_main_thread!();
26 unsafe {
27 let mut plane = Self::uninitialized();
28 ffi::graphene_plane_init(
29 plane.to_glib_none_mut().0,
30 normal.to_glib_none().0,
31 constant,
32 );
33 plane
34 }
35 }
36
37 /// Initializes the given [`Plane`][crate::Plane] using the given normal vector
38 /// and an arbitrary co-planar point.
39 /// ## `normal`
40 /// a normal vector defining the plane pointing towards the origin
41 /// ## `point`
42 /// a [`Point3D`][crate::Point3D]
43 ///
44 /// # Returns
45 ///
46 /// the initialized plane
47 #[doc(alias = "graphene_plane_init_from_point")]
48 #[doc(alias = "init_from_point")]
49 pub fn from_point(normal: &Vec3, point: &Point3D) -> Self {
50 assert_initialized_main_thread!();
51 unsafe {
52 let mut plane = Self::uninitialized();
53 ffi::graphene_plane_init_from_point(
54 plane.to_glib_none_mut().0,
55 normal.to_glib_none().0,
56 point.to_glib_none().0,
57 );
58 plane
59 }
60 }
61
62 /// Initializes the given [`Plane`][crate::Plane] using the 3 provided co-planar
63 /// points.
64 ///
65 /// The winding order is counter-clockwise, and determines which direction
66 /// the normal vector will point.
67 /// ## `a`
68 /// a [`Point3D`][crate::Point3D]
69 /// ## `b`
70 /// a [`Point3D`][crate::Point3D]
71 /// ## `c`
72 /// a [`Point3D`][crate::Point3D]
73 ///
74 /// # Returns
75 ///
76 /// the initialized plane
77 #[doc(alias = "graphene_plane_init_from_points")]
78 #[doc(alias = "init_from_points")]
79 pub fn from_points(a: &Point3D, b: &Point3D, c: &Point3D) -> Self {
80 assert_initialized_main_thread!();
81 unsafe {
82 let mut plane = Self::uninitialized();
83 ffi::graphene_plane_init_from_points(
84 plane.to_glib_none_mut().0,
85 a.to_glib_none().0,
86 b.to_glib_none().0,
87 c.to_glib_none().0,
88 );
89 plane
90 }
91 }
92
93 /// Initializes the given [`Plane`][crate::Plane] using the components of
94 /// the given [`Vec4`][crate::Vec4] vector.
95 /// ## `src`
96 /// a [`Vec4`][crate::Vec4] containing the normal vector in its first
97 /// three components, and the distance in its fourth component
98 ///
99 /// # Returns
100 ///
101 /// the initialized plane
102 #[doc(alias = "graphene_plane_init_from_vec4")]
103 #[doc(alias = "init_from_vec4")]
104 pub fn from_vec4(src: &Vec4) -> Self {
105 assert_initialized_main_thread!();
106 unsafe {
107 let mut plane = Self::uninitialized();
108 ffi::graphene_plane_init_from_vec4(plane.to_glib_none_mut().0, src.to_glib_none().0);
109 plane
110 }
111 }
112}
113
114impl fmt::Debug for Plane {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 f.debug_struct("Plane")
117 .field("constant", &self.constant())
118 .field("normal", &self.normal())
119 .finish()
120 }
121}