tf_demo_parser/demo/
vector.rs1use bitbuffer::{BitRead, BitWrite};
2use parse_display::Display;
3use serde::{Deserialize, Serialize};
4use std::ops::{Add, Sub};
5
6#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7#[derive(BitRead, BitWrite, Debug, Clone, Copy, Default, Serialize, Deserialize, Display)]
8#[display("({x}, {y}, {z})")]
9pub struct Vector {
10 pub x: f32,
11 pub y: f32,
12 pub z: f32,
13}
14
15impl From<Vector> for [f32; 3] {
16 fn from(vec: Vector) -> Self {
17 [vec.x, vec.y, vec.z]
18 }
19}
20
21impl PartialEq for Vector {
22 fn eq(&self, other: &Self) -> bool {
23 (self.x - other.x < 0.001) && (self.y - other.y < 0.001) && (self.z - other.z < 0.001)
24 }
25}
26
27impl Add for Vector {
28 type Output = Vector;
29
30 fn add(self, rhs: Self) -> Self::Output {
31 Vector {
32 x: self.x + rhs.x,
33 y: self.y + rhs.y,
34 z: self.z + rhs.z,
35 }
36 }
37}
38
39impl Sub for Vector {
40 type Output = Vector;
41
42 fn sub(self, rhs: Self) -> Self::Output {
43 Vector {
44 x: self.x - rhs.x,
45 y: self.y - rhs.y,
46 z: self.z - rhs.z,
47 }
48 }
49}
50
51#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
52#[derive(BitRead, BitWrite, Debug, Clone, Copy, Default, Serialize, Deserialize, Display)]
53#[display("({x}, {y})")]
54pub struct VectorXY {
55 pub x: f32,
56 pub y: f32,
57}
58
59impl PartialEq for VectorXY {
60 fn eq(&self, other: &Self) -> bool {
61 (self.x - other.x < 0.001) && (self.y - other.y < 0.001)
62 }
63}
64
65impl From<Vector> for VectorXY {
66 fn from(vec: Vector) -> Self {
67 VectorXY { x: vec.x, y: vec.y }
68 }
69}
70
71impl Add for VectorXY {
72 type Output = VectorXY;
73
74 fn add(self, rhs: Self) -> Self::Output {
75 VectorXY {
76 x: self.x + rhs.x,
77 y: self.y + rhs.y,
78 }
79 }
80}
81
82impl Sub for VectorXY {
83 type Output = VectorXY;
84
85 fn sub(self, rhs: Self) -> Self::Output {
86 VectorXY {
87 x: self.x - rhs.x,
88 y: self.y - rhs.y,
89 }
90 }
91}