Skip to main content

cadkit_core/
geom.rs

1//! Basic geometry types.
2
3use serde::{Deserialize, Serialize};
4
5/// A 3D point. 2D data uses `z = 0` (or the elevation).
6#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
7pub struct Point3 {
8    /// X.
9    pub x: f64,
10    /// Y.
11    pub y: f64,
12    /// Z.
13    pub z: f64,
14}
15
16impl Point3 {
17    /// Creates a point.
18    pub const fn new(x: f64, y: f64, z: f64) -> Self {
19        Self { x, y, z }
20    }
21
22    /// Creates a point on the XY plane.
23    pub const fn xy(x: f64, y: f64) -> Self {
24        Self { x, y, z: 0.0 }
25    }
26}
27
28/// A 3D vector (directions, normals, axes).
29#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
30pub struct Vec3 {
31    /// X.
32    pub x: f64,
33    /// Y.
34    pub y: f64,
35    /// Z.
36    pub z: f64,
37}
38
39impl Vec3 {
40    /// The world Z axis, the default normal.
41    pub const Z: Vec3 = Vec3 {
42        x: 0.0,
43        y: 0.0,
44        z: 1.0,
45    };
46
47    /// Creates a vector.
48    pub const fn new(x: f64, y: f64, z: f64) -> Self {
49        Self { x, y, z }
50    }
51}
52
53/// An axis-aligned bounding box.
54#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
55pub struct BBox {
56    /// Minimum corner.
57    pub min: Point3,
58    /// Maximum corner.
59    pub max: Point3,
60}