Use planes to define vertices

This commit is contained in:
Hanno Braun 2024-12-17 20:11:53 +01:00
parent a6b1bbfdcb
commit f675fd3db6
4 changed files with 49 additions and 9 deletions

View File

@ -0,0 +1,6 @@
use super::Vector;
pub struct Bivector<const D: usize> {
pub a: Vector<D>,
pub b: Vector<D>,
}

View File

@ -1,5 +1,10 @@
mod bivector;
mod plane;
mod point;
mod scalar;
mod vector;
pub use self::{point::Point, scalar::Scalar, vector::Vector};
pub use self::{
bivector::Bivector, plane::Plane, point::Point, scalar::Scalar,
vector::Vector,
};

View File

@ -0,0 +1,13 @@
use super::{Bivector, Point};
pub struct Plane {
pub origin: Point<3>,
pub coords: Bivector<3>,
}
impl Plane {
pub fn point_from_local(&self, point: impl Into<Point<2>>) -> Point<3> {
let [u, v] = point.into().coords.components;
self.origin + self.coords.a * u + self.coords.b * v
}
}

View File

@ -1,5 +1,6 @@
use crate::{
geometry::{Shape, Triangle, Vertex},
math::{Bivector, Plane, Point, Vector},
storage::Store,
};
@ -7,16 +8,31 @@ pub fn model(shape: &mut Shape) {
let mut vertices = Store::<Vertex>::new();
let mut triangles = Store::<Triangle>::new();
let bottom = Plane {
origin: Point::from([0., 0., -0.5]),
coords: Bivector {
a: Vector::from([1., 0., 0.]),
b: Vector::from([0., 1., 0.]),
},
};
let top = Plane {
origin: Point::from([0., 0., 0.5]),
coords: Bivector {
a: Vector::from([1., 0., 0.]),
b: Vector::from([0., 1., 0.]),
},
};
let (a, b, c, d, e, f, g, h) = shape
.extend_with(&mut vertices)
.add([-0.5, -0.5, -0.5])
.add([0.5, -0.5, -0.5])
.add([-0.5, 0.5, -0.5])
.add([0.5, 0.5, -0.5])
.add([-0.5, -0.5, 0.5])
.add([0.5, -0.5, 0.5])
.add([-0.5, 0.5, 0.5])
.add([0.5, 0.5, 0.5])
.add(bottom.point_from_local([-0.5, -0.5]))
.add(bottom.point_from_local([0.5, -0.5]))
.add(bottom.point_from_local([-0.5, 0.5]))
.add(bottom.point_from_local([0.5, 0.5]))
.add(top.point_from_local([-0.5, -0.5]))
.add(top.point_from_local([0.5, -0.5]))
.add(top.point_from_local([-0.5, 0.5]))
.add(top.point_from_local([0.5, 0.5]))
.get_added();
shape