From af3f9cf1fed5bc3aa60e4ccdfaa73dbe6e18fcc0 Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Wed, 5 Feb 2025 10:48:37 -0800
Subject: [PATCH] deduplicate ahead of time

---
 lib/bsp_loader/src/brush.rs | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/lib/bsp_loader/src/brush.rs b/lib/bsp_loader/src/brush.rs
index 2eed980..45737b0 100644
--- a/lib/bsp_loader/src/brush.rs
+++ b/lib/bsp_loader/src/brush.rs
@@ -43,16 +43,10 @@ impl std::fmt::Display for BrushToMeshError{
 }
 impl core::error::Error for BrushToMeshError{}
 
-fn planes_to_faces(face_list:Vec<Face>)->Option<Faces>{
+fn planes_to_faces(face_list:std::collections::HashSet<Face>)->Option<Faces>{
 	let mut faces=Vec::new();
 	// for each face, determine one edge at a time until you complete the face
-	let mut dedup=std::collections::HashSet::new();
 	'face: for face0 in &face_list{
-		// don't generate duplicate faces
-		if !dedup.insert(face0){
-			continue 'face;
-		}
-
 		// 1. find first edge
 		// 2. follow edges around face
 
@@ -156,7 +150,7 @@ fn planes_to_faces(face_list:Vec<Face>)->Option<Faces>{
 
 			// find the next face moving clockwise around face0
 			let (new_face,new_intersection,_)=face_list.iter().filter_map(|new_face|{
-				// ignore faces we've seen before
+				// ignore faces that are part of the current edge
 				if core::ptr::eq(face0,new_face)
 				  |core::ptr::eq(face2,new_face){
 					return None;
@@ -168,6 +162,7 @@ fn planes_to_faces(face_list:Vec<Face>)->Option<Faces>{
 				if d_new_intersection.le_ratio(d_intersection){
 					return None;
 				}
+
 				Some((new_face,new_intersection,d_new_intersection))
 			}).min_by_key(|&(_,_,d)|d)?;
 
@@ -202,7 +197,7 @@ pub fn brush_to_mesh(bsp:&vbsp::Bsp,brush:&vbsp::Brush)->Result<model::Mesh,Brus
 			normal:valve_transform(plane.normal.into()),
 			dot:valve_transform_dist(plane.dist.into()),
 		})
-	}).collect::<Option<Vec<_>>>().ok_or(BrushToMeshError::MissingPlane)?;
+	}).collect::<Option<std::collections::HashSet<_>>>().ok_or(BrushToMeshError::MissingPlane)?;
 
 	if face_list.len()<4{
 		return Err(BrushToMeshError::InvalidFaceCount{count:face_list.len()});