tentacli_traits/types/
position.rs

1use std::io::BufRead;
2
3use byteorder::{LittleEndian, ReadBytesExt};
4use serde::Serialize;
5
6use crate::BinaryConverter;
7
8#[derive(Serialize, Copy, Clone, Default, Debug, PartialEq)]
9pub struct Point3D {
10    pub x: f32,
11    pub y: f32,
12    pub z: f32,
13}
14
15impl Point3D {
16    pub fn new(x: f32, y: f32, z: f32) -> Self {
17        Self { x, y, z }
18    }
19}
20
21impl BinaryConverter for Point3D {
22    fn write_into(&mut self, buffer: &mut Vec<u8>) -> anyhow::Result<()> {
23        self.x.write_into(buffer)?;
24        self.y.write_into(buffer)?;
25        self.z.write_into(buffer)?;
26
27        Ok(())
28    }
29
30    fn read_from<R: BufRead>(reader: &mut R, _: &mut Vec<u8>) -> anyhow::Result<Self> {
31        let x = reader.read_f32::<LittleEndian>()?;
32        let y = reader.read_f32::<LittleEndian>()?;
33        let z = reader.read_f32::<LittleEndian>()?;
34
35        Ok(Self { x, y, z })
36    }
37}
38
39#[derive(Serialize, Copy, Clone, Default, Debug, PartialEq)]
40pub struct Vector3D {
41    pub point: Point3D,
42    pub direction: f32,
43}
44
45impl Vector3D {
46    pub fn new(x: f32, y: f32, z: f32, direction: f32) -> Self {
47        Self { point: Point3D::new(x, y, z), direction }
48    }
49}
50
51impl BinaryConverter for Vector3D {
52    fn write_into(&mut self, buffer: &mut Vec<u8>) -> anyhow::Result<()> {
53        self.point.write_into(buffer)?;
54        self.direction.write_into(buffer)?;
55
56        Ok(())
57    }
58
59    fn read_from<R: BufRead>(reader: &mut R, _: &mut Vec<u8>) -> anyhow::Result<Self> {
60        let point = Point3D::read_from(reader, &mut vec![])?;
61        let direction = reader.read_f32::<LittleEndian>()?;
62        Ok(Self { point, direction })
63    }
64}