vbsp_common/
vector.rs

1use crate::EntityParseError;
2use binrw::BinRead;
3use cgmath::Vector3;
4use serde::de::{Error, Unexpected};
5use serde::{Deserialize, Deserializer};
6use std::cmp::Ordering;
7use std::fmt::Debug;
8use std::ops::{Add, Mul, Sub};
9use std::str::FromStr;
10
11#[derive(Debug, Clone, Copy, BinRead, Default)]
12pub struct Vector {
13    pub x: f32,
14    pub y: f32,
15    pub z: f32,
16}
17
18impl Vector {
19    pub fn iter(&self) -> impl Iterator<Item = f32> {
20        [self.x, self.y, self.z].into_iter()
21    }
22
23    pub fn length_squared(&self) -> f32 {
24        self.x.powf(2.0) + self.y.powf(2.0) + self.z.powf(2.0)
25    }
26}
27
28impl Add<Vector> for Vector {
29    type Output = Vector;
30
31    fn add(self, rhs: Vector) -> Self::Output {
32        Vector {
33            x: self.x + rhs.x,
34            y: self.y + rhs.y,
35            z: self.z + rhs.z,
36        }
37    }
38}
39
40impl Sub<Vector> for Vector {
41    type Output = Vector;
42
43    fn sub(self, rhs: Vector) -> Self::Output {
44        Vector {
45            x: self.x - rhs.x,
46            y: self.y - rhs.y,
47            z: self.z - rhs.z,
48        }
49    }
50}
51
52impl Mul<f32> for Vector {
53    type Output = Vector;
54
55    fn mul(self, rhs: f32) -> Self::Output {
56        Vector {
57            x: self.x * rhs,
58            y: self.y * rhs,
59            z: self.z * rhs,
60        }
61    }
62}
63
64impl PartialEq for Vector {
65    fn eq(&self, other: &Self) -> bool {
66        self.x == other.x && self.y == other.y && self.z == other.z
67    }
68}
69
70impl PartialOrd for Vector {
71    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
72        self.length_squared().partial_cmp(&other.length_squared())
73    }
74}
75
76impl From<Vector> for [f32; 3] {
77    fn from(vector: Vector) -> Self {
78        [vector.x, vector.y, vector.z]
79    }
80}
81
82impl From<[f32; 3]> for Vector {
83    fn from(vector: [f32; 3]) -> Self {
84        Vector {
85            x: vector[0],
86            y: vector[1],
87            z: vector[2],
88        }
89    }
90}
91
92impl From<&Vector> for [f32; 3] {
93    fn from(vector: &Vector) -> Self {
94        [vector.x, vector.y, vector.z]
95    }
96}
97
98impl FromStr for Vector {
99    type Err = EntityParseError;
100
101    fn from_str(s: &str) -> Result<Self, Self::Err> {
102        let mut floats = s.split_whitespace().map(f32::from_str);
103        let x = floats.next().ok_or(EntityParseError::ElementCount)??;
104        let y = floats.next().ok_or(EntityParseError::ElementCount)??;
105        let z = floats.next().ok_or(EntityParseError::ElementCount)??;
106        if floats.next().is_some() {
107            return Err(EntityParseError::ElementCount);
108        }
109        Ok(Vector { x, y, z })
110    }
111}
112
113impl From<Vector> for Vector3<f32> {
114    fn from(v: Vector) -> Self {
115        Vector3::new(v.x, v.y, v.z)
116    }
117}
118
119impl<'de> Deserialize<'de> for Vector {
120    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
121    where
122        D: Deserializer<'de>,
123    {
124        let str = <&str>::deserialize(deserializer)?;
125        str.parse()
126            .map_err(|_| D::Error::invalid_value(Unexpected::Other(str), &"a vector"))
127    }
128}