tf_demo_parser/demo/
vector.rs

1use crate::demo::sendprop::SendPropValue;
2use bitbuffer::{BitRead, BitWrite};
3use parse_display::Display;
4use serde::{Deserialize, Serialize};
5use std::ops::{Add, Mul, Sub};
6
7#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
8#[derive(BitRead, BitWrite, Debug, Clone, Copy, Default, Serialize, Deserialize, Display)]
9#[display("({x}, {y}, {z})")]
10pub struct Vector {
11    pub x: f32,
12    pub y: f32,
13    pub z: f32,
14}
15
16impl From<Vector> for [f32; 3] {
17    fn from(vec: Vector) -> Self {
18        [vec.x, vec.y, vec.z]
19    }
20}
21
22impl PartialEq for Vector {
23    fn eq(&self, other: &Self) -> bool {
24        (self.x - other.x < 0.001) && (self.y - other.y < 0.001) && (self.z - other.z < 0.001)
25    }
26}
27
28impl PartialEq<[SendPropValue]> for Vector {
29    fn eq(&self, other: &[SendPropValue]) -> bool {
30        match other {
31            &[SendPropValue::Float(x), SendPropValue::Float(y), SendPropValue::Float(z)] => {
32                self.x == x && self.y == y && self.z == z
33            }
34            _ => false,
35        }
36    }
37}
38
39#[test]
40fn test_vec_array_eq() {
41    assert!(!Vector {
42        x: 1.0,
43        y: 2.0,
44        z: 3.0,
45    }
46    .eq([SendPropValue::Float(1.0), SendPropValue::Float(2.0)].as_slice()));
47
48    assert!(Vector {
49        x: 1.0,
50        y: 2.0,
51        z: 3.0,
52    }
53    .eq([
54        SendPropValue::Float(1.0),
55        SendPropValue::Float(2.0),
56        SendPropValue::Float(3.0),
57    ]
58    .as_slice()));
59}
60
61impl Add for Vector {
62    type Output = Vector;
63
64    fn add(self, rhs: Self) -> Self::Output {
65        Vector {
66            x: self.x + rhs.x,
67            y: self.y + rhs.y,
68            z: self.z + rhs.z,
69        }
70    }
71}
72
73impl Sub for Vector {
74    type Output = Vector;
75
76    fn sub(self, rhs: Self) -> Self::Output {
77        Vector {
78            x: self.x - rhs.x,
79            y: self.y - rhs.y,
80            z: self.z - rhs.z,
81        }
82    }
83}
84
85impl Mul<f32> for Vector {
86    type Output = Vector;
87
88    fn mul(self, rhs: f32) -> Self::Output {
89        Vector {
90            x: self.x * rhs,
91            y: self.y * rhs,
92            z: self.z * rhs,
93        }
94    }
95}
96
97#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
98#[derive(BitRead, BitWrite, Debug, Clone, Copy, Default, Serialize, Deserialize, Display)]
99#[display("({x}, {y})")]
100pub struct VectorXY {
101    pub x: f32,
102    pub y: f32,
103}
104
105impl PartialEq for VectorXY {
106    fn eq(&self, other: &Self) -> bool {
107        (self.x - other.x < 0.001) && (self.y - other.y < 0.001)
108    }
109}
110
111impl PartialEq<[SendPropValue]> for VectorXY {
112    fn eq(&self, other: &[SendPropValue]) -> bool {
113        match other {
114            &[SendPropValue::Float(x), SendPropValue::Float(y)] => self.x == x && self.y == y,
115            _ => false,
116        }
117    }
118}
119
120#[test]
121fn test_vec_xy_array_eq() {
122    assert!(VectorXY { x: 1.0, y: 2.0 }
123        .eq([SendPropValue::Float(1.0), SendPropValue::Float(2.0)].as_slice()));
124
125    assert!(!VectorXY { x: 1.0, y: 2.0 }.eq([
126        SendPropValue::Float(1.0),
127        SendPropValue::Float(2.0),
128        SendPropValue::Float(3.0)
129    ]
130    .as_slice()));
131}
132
133impl From<Vector> for VectorXY {
134    fn from(vec: Vector) -> Self {
135        VectorXY { x: vec.x, y: vec.y }
136    }
137}
138
139impl Add for VectorXY {
140    type Output = VectorXY;
141
142    fn add(self, rhs: Self) -> Self::Output {
143        VectorXY {
144            x: self.x + rhs.x,
145            y: self.y + rhs.y,
146        }
147    }
148}
149
150impl Sub for VectorXY {
151    type Output = VectorXY;
152
153    fn sub(self, rhs: Self) -> Self::Output {
154        VectorXY {
155            x: self.x - rhs.x,
156            y: self.y - rhs.y,
157        }
158    }
159}