manifold3d_types/math/
vec3.rs

1use manifold3d_sys::ManifoldVec3;
2use std::ops::{Add, Sub};
3
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub struct Vec3 {
6    pub x: f64,
7    pub y: f64,
8    pub z: f64,
9}
10
11impl Vec3 {
12    pub fn new(x: f64, y: f64, z: f64) -> Self {
13        Self { x, y, z }
14    }
15}
16
17impl From<ManifoldVec3> for Vec3 {
18    fn from(value: ManifoldVec3) -> Self {
19        Vec3 {
20            x: value.x,
21            y: value.y,
22            z: value.z,
23        }
24    }
25}
26
27impl Add for Vec3 {
28    type Output = Self;
29
30    fn add(self, rhs: Self) -> Self::Output {
31        Vec3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
32    }
33}
34
35impl<T> Add<T> for Vec3
36where
37    f64: From<T>,
38    T: num_traits::ToPrimitive,
39{
40    type Output = Self;
41
42    fn add(self, rhs: T) -> Self::Output {
43        let value = f64::from(rhs);
44        Vec3::new(self.x + value, self.y + value, self.z + value)
45    }
46}
47
48impl Sub for Vec3 {
49    type Output = Self;
50
51    fn sub(self, rhs: Self) -> Self::Output {
52        Vec3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
53    }
54}
55
56impl<T> Sub<T> for Vec3
57where
58    f64: From<T>,
59    T: num_traits::ToPrimitive,
60{
61    type Output = Self;
62
63    fn sub(self, rhs: T) -> Self::Output {
64        let value = f64::from(rhs);
65        Vec3::new(self.x - value, self.y - value, self.z - value)
66    }
67}
68
69#[cfg(feature = "nalgebra_interop")]
70impl From<nalgebra::Vector3<f64>> for Vec3 {
71    fn from(value: nalgebra::Vector3<f64>) -> Self {
72        Vec3 {
73            x: value.x,
74            y: value.y,
75            z: value.z,
76        }
77    }
78}