Add vertex merging

This commit is contained in:
Jarrod Doyle 2023-09-22 10:17:49 +01:00
parent 1ffa8a9860
commit 901fb79ae6
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
2 changed files with 51 additions and 23 deletions

View File

@ -1,10 +1,14 @@
use glam::Vec3; use glam::Vec3;
use crate::plane::{Plane, PlaneIntersection}; use crate::{
math,
plane::{Plane, PlaneIntersection},
};
#[derive(Default)] #[derive(Default, Debug, Clone)]
pub struct MaterialId(pub String); pub struct MaterialId(pub String);
#[derive(Debug, Clone, Copy)]
pub struct TextureProjection { pub struct TextureProjection {
pub u: Plane, pub u: Plane,
pub v: Plane, pub v: Plane,
@ -25,7 +29,7 @@ impl Default for TextureProjection {
} }
} }
#[derive(Default)] #[derive(Default, Debug, Clone)]
pub struct BrushPlane { pub struct BrushPlane {
pub plane: Plane, pub plane: Plane,
pub material: MaterialId, pub material: MaterialId,
@ -34,18 +38,27 @@ pub struct BrushPlane {
pub struct Brush { pub struct Brush {
pub planes: Vec<BrushPlane>, pub planes: Vec<BrushPlane>,
raw_vertices: Vec<PlaneIntersection>,
vertices: Vec<Vec3>,
} }
impl Brush { impl Brush {
pub fn get_vertices(&self) -> Vec<Vec3> { pub fn new(planes: &[BrushPlane]) -> Self {
if self.planes.len() < 3 { Self {
println!("fug"); planes: planes.to_vec(),
return vec![]; raw_vertices: vec![],
vertices: vec![],
}
}
pub fn rebuild(&mut self) {
if self.planes.len() < 4 {
panic!("Invalid number of planes. Minimum 4 planes required.");
} }
// Test all permutations of brush planes for intersections // Test all permutations of brush planes for intersections
let mut intersections = vec![]; let mut intersections = vec![];
let mut vs = vec![]; let mut vs: Vec<Vec3> = vec![];
let len = self.planes.len(); let len = self.planes.len();
for i in 0..(len - 2) { for i in 0..(len - 2) {
for j in (i + 1)..(len - 1) { for j in (i + 1)..(len - 1) {
@ -63,7 +76,7 @@ impl Brush {
// Validate intersections against other brush planes // Validate intersections against other brush planes
// TODO: No need to check against planes that are part of the intersection // TODO: No need to check against planes that are part of the intersection
for intersection in intersections { for intersection in &intersections {
let mut valid = true; let mut valid = true;
for bplane in &self.planes { for bplane in &self.planes {
if !intersection.in_halfspace_mat(&bplane.plane) { if !intersection.in_halfspace_mat(&bplane.plane) {
@ -72,11 +85,31 @@ impl Brush {
} }
} }
// Merge vertices
if valid { if valid {
vs.push(intersection.get_intersection_point()); let point = intersection.get_intersection_point();
let mut duplicate = false;
for v in &vs {
if v.abs_diff_eq(point, math::EPSILON) {
duplicate = true;
break;
}
}
if !duplicate {
vs.push(point);
}
} }
} }
vs self.vertices = vs;
self.raw_vertices = intersections;
}
pub fn get_vertices(&self) -> Vec<Vec3> {
self.vertices.clone()
}
pub fn get_intersections(&self) -> Vec<PlaneIntersection> {
self.raw_vertices.clone()
} }
} }

View File

@ -22,13 +22,9 @@ fn generate_box_brush(position: Vec3, extent: Vec3) -> Brush {
plane: Plane::from_point_normal(point, *normal), plane: Plane::from_point_normal(point, *normal),
..Default::default() ..Default::default()
}); });
println!("Plane: {:?}", brush_planes.last().unwrap().plane);
} }
Brush { Brush::new(&brush_planes)
planes: brush_planes,
}
} }
/// Position is center of base plane /// Position is center of base plane
@ -43,14 +39,12 @@ fn generate_pyramid_brush(position: Vec3, height: f32, side_count: u32) -> Brush
for i in 0..side_count { for i in 0..side_count {
let angle = (i as f32 / side_count as f32) * 360.0_f32.to_radians(); let angle = (i as f32 / side_count as f32) * 360.0_f32.to_radians();
dbg!(angle);
let rot_mat = Mat3::from_cols( let rot_mat = Mat3::from_cols(
Vec3::new(angle.cos(), 0.0, -angle.sin()), Vec3::new(angle.cos(), 0.0, -angle.sin()),
Vec3::Y, Vec3::Y,
Vec3::new(angle.sin(), 0.0, angle.cos()), Vec3::new(angle.sin(), 0.0, angle.cos()),
); );
let normal = rot_mat * Vec3::new(1.0, 1.0, 0.0); let normal = rot_mat * Vec3::new(1.0, 1.0, 0.0);
dbg!(normal);
brush_planes.push(BrushPlane { brush_planes.push(BrushPlane {
plane: Plane::from_point_normal(tip, normal), plane: Plane::from_point_normal(tip, normal),
@ -58,18 +52,19 @@ fn generate_pyramid_brush(position: Vec3, height: f32, side_count: u32) -> Brush
}); });
} }
Brush { Brush::new(&brush_planes)
planes: brush_planes,
}
} }
fn main() { fn main() {
let brush = generate_box_brush(Vec3::new(1.0, 0.0, 0.0), Vec3::new(1.0, 2.0, 1.0)); let mut brush = generate_box_brush(Vec3::new(1.0, 0.0, 0.0), Vec3::new(1.0, 2.0, 1.0));
brush.rebuild();
let vs = brush.get_vertices(); let vs = brush.get_vertices();
println!("Vertices: {vs:?}"); println!("Vertices: {vs:?}");
let vs = generate_pyramid_brush(Vec3::ZERO, 1.0, 4).get_vertices(); let mut brush = generate_pyramid_brush(Vec3::ZERO, 1.0, 4);
brush.rebuild();
let vs = brush.get_vertices();
println!("Vertices: {vs:?}"); println!("Vertices: {vs:?}");
} }