Skip to main content

arc_graph_core/
vec3.rs

1//! 最小三维向量(arc-flow-core 只提供 2D `Vec2`,3D 在本 crate 内补齐)。
2//! 语义与 `arc_flow_core::geometry::Vec2` 对齐:plus / minus / scale / length。
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
7pub struct Vec3 {
8    pub x: f32,
9    pub y: f32,
10    pub z: f32,
11}
12
13impl Vec3 {
14    pub const ZERO: Self = Self {
15        x: 0.0,
16        y: 0.0,
17        z: 0.0,
18    };
19
20    pub const fn new(x: f32, y: f32, z: f32) -> Self {
21        Self { x, y, z }
22    }
23
24    pub fn plus(self, o: Self) -> Self {
25        Self::new(self.x + o.x, self.y + o.y, self.z + o.z)
26    }
27
28    pub fn minus(self, o: Self) -> Self {
29        Self::new(self.x - o.x, self.y - o.y, self.z - o.z)
30    }
31
32    pub fn scale(self, k: f32) -> Self {
33        Self::new(self.x * k, self.y * k, self.z * k)
34    }
35
36    pub fn length(self) -> f32 {
37        (self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
38    }
39
40    pub fn distance(self, o: Self) -> f32 {
41        self.minus(o).length()
42    }
43
44    pub fn dot(self, o: Self) -> f32 {
45        self.x * o.x + self.y * o.y + self.z * o.z
46    }
47}