rgeometry/data/point/
add.rs

1use super::Point;
2use super::Vector;
3use array_init::array_init;
4use num_traits::NumOps;
5use std::ops::Add;
6use std::ops::AddAssign;
7use std::ops::Index;
8
9// &point + &vector = point
10impl<'a, 'b, T, const N: usize> Add<&'a Vector<T, N>> for &'b Point<T, N>
11where
12  T: Add<Output = T> + Clone,
13{
14  type Output = Point<T, N>;
15
16  fn add(self: &'b Point<T, N>, other: &'a Vector<T, N>) -> Self::Output {
17    Point {
18      array: array_init(|i| self.array.index(i).clone() + other.0.index(i).clone()),
19    }
20  }
21}
22
23// point + vector = point
24impl<T, const N: usize> Add<Vector<T, N>> for Point<T, N>
25where
26  T: Add<Output = T> + Clone,
27{
28  type Output = Point<T, N>;
29
30  fn add(self: Point<T, N>, other: Vector<T, N>) -> Self::Output {
31    self.add(&other)
32  }
33}
34
35// point + &vector = point
36impl<T, const N: usize> Add<&Vector<T, N>> for Point<T, N>
37where
38  T: Add<Output = T> + Clone,
39{
40  type Output = Point<T, N>;
41
42  fn add(self: Point<T, N>, other: &Vector<T, N>) -> Self::Output {
43    (&self).add(other)
44  }
45}
46
47// point += &vector
48impl<T, const N: usize> AddAssign<&Vector<T, N>> for Point<T, N>
49where
50  T: NumOps + Clone + AddAssign,
51{
52  fn add_assign(&mut self, other: &Vector<T, N>) {
53    for i in 0..N {
54      self.array[i] += other.0.index(i).clone()
55    }
56  }
57}
58
59// point += vector
60impl<T, const N: usize> AddAssign<Vector<T, N>> for Point<T, N>
61where
62  T: NumOps + Clone + AddAssign,
63{
64  fn add_assign(&mut self, other: Vector<T, N>) {
65    self.add_assign(&other)
66  }
67}
68
69// // point + point = point
70// impl<T, const N: usize> Add<Point<T, N>> for Point<T, N>
71// where
72//   T: Add<T, Output = T> + Clone,
73// {
74//   type Output = Point<T, N>;
75
76//   fn add(self: Point<T, N>, other: Point<T, N>) -> Self::Output {
77//     let arr: [T; N] = raw_arr_zipwith(&self.0, &other.0, |a, b| a.clone() + b.clone());
78//     Point(arr)
79//   }
80// }
81
82// point + vector = point
83// impl<'a, 'b, T, const N: usize> Add<Vector<&'a T, N>> for &'b Point<T, N>
84// where
85//   T: Add<T, Output = T> + Clone,
86// {
87//   type Output = Point<T, N>;
88
89//   fn add(self: &'b Point<T, N>, other: Vector<&'a T, N>) -> Self::Output {
90//     let arr: [T; N] = unsafe {
91//       let mut arr = MaybeUninit::uninit();
92//       for i in 0..N {
93//         (arr.as_mut_ptr() as *mut T)
94//           .add(i)
95//           .write(self.0.index(i).clone() + (*other.0.index(i)).clone());
96//       }
97//       arr.assume_init()
98//     };
99//     Point(arr)
100//   }
101// }