devela/num/geom/shape/point/
methods.rs
1#[cfg(feature = "alg")]
7use crate::Vector;
8use crate::{Point, Point2d, Point3d};
9
10#[rustfmt::skip]
11impl<T, const D: usize> Point<T, D> {
12 #[must_use]
14 pub const fn new(coords: [T; D]) -> Self {
15 Self { coords }
16 }
17
18 #[must_use]
20 #[cfg(feature = "alg")]
21 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alg")))]
22 pub fn into_vector(self) -> Vector<T, D> {
23 Vector::new(self.coords)
24 }
25 #[must_use]
27 #[cfg(feature = "alg")]
28 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alg")))]
29 pub const fn to_vector(self) -> Vector<T, D> where T: Copy {
30 Vector::new(self.coords)
31 }
32
33 #[must_use]
35 #[cfg(feature = "alg")]
36 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alg")))]
37 pub fn from_vector(v: Vector<T, D>) -> Self {
38 Self::new(v.coords)
39 }
40 #[must_use]
42 #[cfg(feature = "alg")]
43 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alg")))]
44 pub const fn from_vector_const(v: Vector<T, D>) -> Self where T: Copy {
45 Self::new(v.coords)
46 }
47}
48
49#[rustfmt::skip]
52impl<T> Point2d<T> {
53 #[must_use]
55 pub const fn x(self) -> T where T: Copy { self.coords[0] }
56 #[must_use]
58 pub const fn y(self) -> T where T: Copy { self.coords[1] }
59
60 #[must_use]
62 pub const fn x_ref(&self) -> &T { &self.coords[0] }
63 #[must_use]
65 pub const fn y_ref(&self) -> &T { &self.coords[1] }
66
67 #[must_use]
69 pub fn x_mut(&mut self) -> &mut T { &mut self.coords[0] }
70 #[must_use]
72 pub fn y_mut(&mut self) -> &mut T { &mut self.coords[1] }
73}
74
75#[rustfmt::skip]
76impl<T> Point3d<T> {
77 #[must_use]
79 pub const fn x(self) -> T where T: Copy { self.coords[0] }
80 #[must_use]
82 pub const fn y(self) -> T where T: Copy { self.coords[1] }
83 #[must_use]
85 pub const fn z(self) -> T where T: Copy { self.coords[2] }
86
87 #[must_use]
89 pub const fn x_ref(&self) -> &T { &self.coords[0] }
90 #[must_use]
92 pub const fn y_ref(&self) -> &T { &self.coords[1] }
93 #[must_use]
95 pub const fn z_ref(&self) -> &T { &self.coords[2] }
96
97 #[must_use]
99 pub fn x_mut(&mut self) -> &mut T { &mut self.coords[0] }
100 #[must_use]
102 pub fn y_mut(&mut self) -> &mut T { &mut self.coords[1] }
103 #[must_use]
105 pub fn z_mut(&mut self) -> &mut T { &mut self.coords[2] }
106}
107
108macro_rules! impl_point {
113 () => {
114 impl_point![sint i8:"_int_i8", i16:"_int_i16", i32:"_int_i32",
115 i64:"_int_i64", i128:"_int_i128", isize:"_int_isize"];
116 impl_point![uint u8:"_int_u8", u16:"_int_u16", u32:"_int_u32",
117 u64:"_int_u64", u128:"_int_u128", usize:"_int_usize"];
118 impl_point![float f32:"_float_f32", f64:"_float_f64"];
119 };
120
121 (int $($t:ty : $cap:literal),+) => { $( impl_point![@int $t:$cap]; )+ };
123 (@int $t:ty : $cap:literal) => {
124 #[cfg(feature = $cap )]
125 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $cap)))]
126 impl<const D: usize> Point<$t, D> {
127 #[must_use]
129 #[cfg(feature = "alg")]
130 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alg")))]
131 pub const fn c_add_vector(self, v: Vector<$t, D>) -> Self {
132 Self { coords: Vector::new(self.coords).c_add(v).coords }
133 }
134 }
142 };
143 (sint $($t:ty : $cap:literal),+) => { $( impl_point![@sint $t:$cap]; )+ };
144 (@sint $t:ty : $cap:literal ) => {
145 impl_point![int $t:$cap];
146
147 };
150 (uint $($t:ty : $cap:literal),+) => { $( impl_point![@uint $t:$cap]; )+ };
151 (@uint $t:ty : $cap:literal) => {
152 impl_point![int $t:$cap];
153
154 };
157
158 (float $( $f:ty : $cap:literal ),+) => { $( impl_point![@float $f:$cap]; )+ };
160 (@float $f:ty : $cap:literal) => {
161 #[cfg(feature = $cap )]
162 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = $cap)))]
163 impl<const D: usize> Point<$f, D> {
164 }
181 };
182}
183impl_point!();