truck_geometry/specifieds/
mod.rs

1use crate::{prelude::*, *};
2
3/// line
4/// # Example
5/// ```
6/// use truck_geometry::prelude::*;
7/// let line = Line(Point2::new(0.0, 0.0), Point2::new(1.0, 1.0));
8/// assert_near!(line.subs(0.5), Point2::new(0.5, 0.5));
9/// ```
10#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
11pub struct Line<P>(pub P, pub P);
12
13/// unit circle
14#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
15pub struct UnitCircle<P>(std::marker::PhantomData<P>);
16
17/// unit hyperbola
18#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
19pub struct UnitHyperbola<P>(std::marker::PhantomData<P>);
20
21/// parabola whose apex is the origin.
22#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize)]
23pub struct UnitParabola<P>(std::marker::PhantomData<P>);
24
25/// plane
26/// # Example
27/// ```
28/// use truck_geometry::prelude::*;
29///
30/// // arbitrary three points
31/// let pt0 = Point3::new(0.0, 1.0, 2.0);
32/// let pt1 = Point3::new(1.0, 1.0, 3.0);
33/// let pt2 = Point3::new(0.0, 2.0, 3.0);
34///
35/// // Creates a plane
36/// let plane: Plane = Plane::new(pt0, pt1, pt2);
37/// // The origin of the plane is pt0.
38/// assert_near!(plane.origin(), pt0);
39/// // The u-axis of the plane is the vector from pt0 to pt1.
40/// assert_near!(plane.u_axis(), pt1 - pt0);
41/// // The v-axis of the plane is the vector from pt0 to pt2.
42/// assert_near!(plane.v_axis(), pt2 - pt0);
43/// // The normal is the normalized u-axis × v-axis
44/// assert_near!(plane.normal(), (pt1 - pt0).cross(pt2 - pt0).normalize());
45/// ```
46#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
47pub struct Plane {
48    o: Point3,
49    p: Point3,
50    q: Point3,
51}
52
53/// sphere
54/// # Examples
55/// ```
56/// use truck_geometry::prelude::*;
57/// use std::f64::consts::PI;
58///
59/// let center = Point3::new(1.0, 2.0, 3.0);
60/// let radius = 4.56;
61///
62/// let sphere = Sphere::new(center, radius);
63/// const N: usize = 100;
64/// for i in 0..=N {
65///     for j in 0..=N {
66///         // the parameter u is latitude
67///         let u = PI * i as f64 / N as f64;
68///         // the parameter v is longitude
69///         let v = 2.0 * PI * j as f64 / N as f64;
70///
71///         // simple relation between a point and its normal.
72///         let pt = sphere.subs(u, v);
73///         let n = sphere.normal(u, v);
74///         assert_near!(pt - center, n * radius);
75///
76///         // the proof of u is latitude and v is longitude
77///         assert!((PI / 2.0 - u) * (pt.z - center.z) >= 0.0);
78///         assert!((PI - v) * (pt.y - center.y) >= 0.0);
79///     }
80/// }
81/// ```
82#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
83pub struct Sphere {
84    center: Point3,
85    radius: f64,
86}
87
88/// torus
89#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
90pub struct Torus {
91    center: Point3,
92    large_radius: f64,
93    small_radius: f64,
94}
95
96mod circle;
97mod hyperbola;
98mod line;
99mod parabola;
100mod plane;
101mod sphere;
102mod torus;
103
104macro_rules! always_true {
105    ($ty: tt) => {
106        impl<P> PartialEq for $ty<P> {
107            fn eq(&self, _: &Self) -> bool { true }
108        }
109        impl<P> Eq for $ty<P> {}
110    };
111}
112
113always_true!(UnitCircle);
114always_true!(UnitParabola);
115always_true!(UnitHyperbola);