graphene/sphere.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, Point3D, Sphere, Vec3};
8
9impl Sphere {
10 /// Initializes the given [`Sphere`][crate::Sphere] with the given `center` and `radius`.
11 /// ## `center`
12 /// the coordinates of the center of the sphere, or [`None`]
13 /// for a center in (0, 0, 0)
14 /// ## `radius`
15 /// the radius of the sphere
16 ///
17 /// # Returns
18 ///
19 /// the initialized [`Sphere`][crate::Sphere]
20 #[doc(alias = "graphene_sphere_init")]
21 pub fn new(center: Option<&Point3D>, radius: f32) -> Self {
22 assert_initialized_main_thread!();
23 unsafe {
24 let mut sph = Self::uninitialized();
25 ffi::graphene_sphere_init(sph.to_glib_none_mut().0, center.to_glib_none().0, radius);
26 sph
27 }
28 }
29
30 /// Initializes the given [`Sphere`][crate::Sphere] using the given array
31 /// of 3D coordinates so that the sphere includes them.
32 ///
33 /// The center of the sphere can either be specified, or will be center
34 /// of the 3D volume that encompasses all `points`.
35 /// ## `points`
36 /// an array of [`Point3D`][crate::Point3D]
37 /// ## `center`
38 /// the center of the sphere
39 ///
40 /// # Returns
41 ///
42 /// the initialized [`Sphere`][crate::Sphere]
43 #[doc(alias = "graphene_sphere_init_from_points")]
44 #[doc(alias = "init_from_points")]
45 pub fn from_points(points: &[Point3D], center: Option<&Point3D>) -> Self {
46 assert_initialized_main_thread!();
47
48 let n = points.len() as u32;
49
50 unsafe {
51 let mut sph = Self::uninitialized();
52 ffi::graphene_sphere_init_from_points(
53 sph.to_glib_none_mut().0,
54 n,
55 points.to_glib_none().0,
56 center.to_glib_none().0,
57 );
58 sph
59 }
60 }
61
62 /// Initializes the given [`Sphere`][crate::Sphere] using the given array
63 /// of 3D coordinates so that the sphere includes them.
64 ///
65 /// The center of the sphere can either be specified, or will be center
66 /// of the 3D volume that encompasses all `vectors`.
67 /// ## `vectors`
68 /// an array of [`Vec3`][crate::Vec3]
69 /// ## `center`
70 /// the center of the sphere
71 ///
72 /// # Returns
73 ///
74 /// the initialized [`Sphere`][crate::Sphere]
75 #[doc(alias = "graphene_sphere_init_from_vectors")]
76 #[doc(alias = "init_from_vectors")]
77 pub fn from_vectors(vectors: &[Vec3], center: Option<&Point3D>) -> Self {
78 assert_initialized_main_thread!();
79
80 let n = vectors.len() as u32;
81
82 unsafe {
83 let mut sph = Self::uninitialized();
84 ffi::graphene_sphere_init_from_vectors(
85 sph.to_glib_none_mut().0,
86 n,
87 vectors.to_glib_none().0,
88 center.to_glib_none().0,
89 );
90 sph
91 }
92 }
93}
94
95impl fmt::Debug for Sphere {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 f.debug_struct("Sphere")
98 .field("radius", &self.radius())
99 .field("center", &self.center())
100 .finish()
101 }
102}