threecrate_simplification/
quadric_error.rs

1//! Quadric error decimation
2//! 
3//! Implementation of the Garland-Heckbert algorithm for mesh simplification
4//! based on quadric error metrics.
5
6use threecrate_core::{TriangleMesh, Result, Error, Point3f};
7use crate::MeshSimplifier;
8use nalgebra::{Matrix4, Vector4};
9// use rayon::prelude::*;
10use priority_queue::PriorityQueue;
11use std::collections::{HashMap, HashSet};
12use std::cmp::Ordering;
13
14/// Edge collapse operation
15#[derive(Debug, Clone)]
16struct EdgeCollapse {
17    /// Source vertex index
18    vertex1: usize,
19    /// Target vertex index  
20    vertex2: usize,
21    /// New position after collapse
22    new_position: Point3f,
23    /// Quadric error cost
24    cost: f64,
25}
26
27impl PartialEq for EdgeCollapse {
28    fn eq(&self, other: &Self) -> bool {
29        self.cost.total_cmp(&other.cost) == Ordering::Equal
30    }
31}
32
33impl Eq for EdgeCollapse {}
34
35impl PartialOrd for EdgeCollapse {
36    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
37        Some(self.cmp(other))
38    }
39}
40
41impl Ord for EdgeCollapse {
42    fn cmp(&self, other: &Self) -> Ordering {
43        // Reverse ordering for min-heap (smallest cost first)
44        other.cost.total_cmp(&self.cost)
45    }
46}
47
48/// Face adjacency information
49#[allow(dead_code)]
50#[derive(Debug, Clone)]
51struct FaceInfo {
52    vertices: [usize; 3],
53    plane: Vector4<f64>, // ax + by + cz + d = 0
54}
55
56/// Vertex information including quadric matrix
57#[derive(Debug, Clone)]
58struct VertexInfo {
59    position: Point3f,
60    quadric: Matrix4<f64>,
61    faces: HashSet<usize>,
62    neighbors: HashSet<usize>,
63}
64
65/// Quadric error decimation simplifier
66pub struct QuadricErrorSimplifier {
67    /// Configuration parameters
68    pub max_edge_length: Option<f32>,
69    pub preserve_boundary: bool,
70    pub feature_angle_threshold: f32,
71}
72
73impl Default for QuadricErrorSimplifier {
74    fn default() -> Self {
75        Self {
76            max_edge_length: None,
77            preserve_boundary: true,
78            feature_angle_threshold: 45.0_f32.to_radians(),
79        }
80    }
81}
82
83impl QuadricErrorSimplifier {
84    /// Create new quadric error simplifier with default settings
85    pub fn new() -> Self {
86        Self::default()
87    }
88    
89    /// Create with custom parameters
90    pub fn with_params(
91        max_edge_length: Option<f32>,
92        preserve_boundary: bool,
93        feature_angle_threshold: f32,
94    ) -> Self {
95        Self {
96            max_edge_length,
97            preserve_boundary,
98            feature_angle_threshold,
99        }
100    }
101    
102    /// Compute plane equation from triangle vertices
103    fn compute_plane(&self, v0: &Point3f, v1: &Point3f, v2: &Point3f) -> Vector4<f64> {
104        let edge1 = v1 - v0;
105        let edge2 = v2 - v0;
106        let normal = edge1.cross(&edge2).normalize();
107        
108        // Handle degenerate triangles
109        if !normal.iter().all(|x| x.is_finite()) {
110            return Vector4::new(0.0, 0.0, 1.0, 0.0);
111        }
112        
113        let d = -normal.dot(&v0.coords);
114        Vector4::new(normal.x as f64, normal.y as f64, normal.z as f64, d as f64)
115    }
116    
117    /// Compute quadric matrix from plane equation
118    fn plane_to_quadric(&self, plane: &Vector4<f64>) -> Matrix4<f64> {
119        let a = plane[0];
120        let b = plane[1]; 
121        let c = plane[2];
122        let d = plane[3];
123        
124        Matrix4::new(
125            a*a, a*b, a*c, a*d,
126            a*b, b*b, b*c, b*d,
127            a*c, b*c, c*c, c*d,
128            a*d, b*d, c*d, d*d,
129        )
130    }
131    
132    /// Initialize vertex information including quadrics
133    fn initialize_vertices(&self, mesh: &TriangleMesh) -> Vec<VertexInfo> {
134        let mut vertices: Vec<VertexInfo> = mesh.vertices.iter().enumerate().map(|(_i, &pos)| {
135            VertexInfo {
136                position: pos,
137                quadric: Matrix4::zeros(),
138                faces: HashSet::new(),
139                neighbors: HashSet::new(),
140            }
141        }).collect();
142        
143        // Compute face planes and accumulate quadrics
144        for (face_idx, face) in mesh.faces.iter().enumerate() {
145            let v0 = mesh.vertices[face[0]];
146            let v1 = mesh.vertices[face[1]];
147            let v2 = mesh.vertices[face[2]];
148            
149            let plane = self.compute_plane(&v0, &v1, &v2);
150            let quadric = self.plane_to_quadric(&plane);
151            
152            // Add quadric to each vertex of the face
153            for &vertex_idx in face.iter() {
154                vertices[vertex_idx].quadric += quadric;
155                vertices[vertex_idx].faces.insert(face_idx);
156            }
157            
158            // Build adjacency
159            vertices[face[0]].neighbors.insert(face[1]);
160            vertices[face[0]].neighbors.insert(face[2]);
161            vertices[face[1]].neighbors.insert(face[0]);
162            vertices[face[1]].neighbors.insert(face[2]);
163            vertices[face[2]].neighbors.insert(face[0]);
164            vertices[face[2]].neighbors.insert(face[1]);
165        }
166        
167        vertices
168    }
169    
170    /// Find boundary edges in the mesh
171    fn find_boundary_edges(&self, mesh: &TriangleMesh) -> HashSet<(usize, usize)> {
172        let mut edge_count: HashMap<(usize, usize), usize> = HashMap::new();
173        
174        for face in &mesh.faces {
175            let edges = [
176                (face[0].min(face[1]), face[0].max(face[1])),
177                (face[1].min(face[2]), face[1].max(face[2])),
178                (face[2].min(face[0]), face[2].max(face[0])),
179            ];
180            
181            for edge in edges.iter() {
182                *edge_count.entry(*edge).or_insert(0) += 1;
183            }
184        }
185        
186        edge_count.into_iter()
187            .filter(|(_, count)| *count == 1)
188            .map(|(edge, _)| edge)
189            .collect()
190    }
191    
192    /// Check if vertex is on boundary
193    #[allow(dead_code)]
194    fn is_boundary_vertex(&self, vertex_idx: usize, boundary_edges: &HashSet<(usize, usize)>) -> bool {
195        boundary_edges.iter().any(|&(v1, v2)| v1 == vertex_idx || v2 == vertex_idx)
196    }
197    
198    /// Compute optimal position and cost for edge collapse
199    fn compute_collapse_cost(&self, v1_idx: usize, v2_idx: usize, vertices: &[VertexInfo]) -> Option<EdgeCollapse> {
200        let v1 = &vertices[v1_idx];
201        let v2 = &vertices[v2_idx];
202        
203        // Check edge length constraint
204        if let Some(max_len) = self.max_edge_length {
205            let edge_len = (v1.position - v2.position).magnitude();
206            if edge_len > max_len {
207                return None;
208            }
209        }
210        
211        // Combined quadric
212        let q_combined = v1.quadric + v2.quadric;
213        
214        // Try to solve for optimal position: ∇(v^T Q v) = 0
215        // This gives us: 2Qv = 0, or the 3x3 upper-left block of Q
216        let q_3x3 = q_combined.fixed_view::<3, 3>(0, 0);
217        let q_3x1 = q_combined.fixed_view::<3, 1>(0, 3);
218        
219        let optimal_pos = if let Some(inv_q) = q_3x3.try_inverse() {
220            let optimal_homogeneous = -inv_q * q_3x1;
221            Point3f::new(
222                optimal_homogeneous[0] as f32,
223                optimal_homogeneous[1] as f32,
224                optimal_homogeneous[2] as f32,
225            )
226        } else {
227                         // If not invertible, use midpoint
228             Point3f::from((v1.position.coords + v2.position.coords) * 0.5)
229        };
230        
231        // Compute quadric error at optimal position
232        let pos_homogeneous = Vector4::new(
233            optimal_pos.x as f64,
234            optimal_pos.y as f64,
235            optimal_pos.z as f64,
236            1.0,
237        );
238        
239                 let cost = (pos_homogeneous.transpose() * q_combined * pos_homogeneous)[0];
240        
241        Some(EdgeCollapse {
242            vertex1: v1_idx,
243            vertex2: v2_idx,
244            new_position: optimal_pos,
245            cost,
246        })
247    }
248    
249    /// Generate all valid edge collapses
250    fn generate_edge_collapses(&self, vertices: &[VertexInfo], boundary_edges: &HashSet<(usize, usize)>) -> Vec<EdgeCollapse> {
251        let mut collapses = Vec::new();
252        
253        for (v1_idx, vertex) in vertices.iter().enumerate() {
254            for &v2_idx in &vertex.neighbors {
255                if v1_idx < v2_idx { // Avoid duplicate edges
256                    // Skip boundary edges if preservation is enabled
257                    if self.preserve_boundary {
258                        let edge = (v1_idx.min(v2_idx), v1_idx.max(v2_idx));
259                        if boundary_edges.contains(&edge) {
260                            continue;
261                        }
262                    }
263                    
264                    if let Some(collapse) = self.compute_collapse_cost(v1_idx, v2_idx, vertices) {
265                        collapses.push(collapse);
266                    }
267                }
268            }
269        }
270        
271        collapses
272    }
273    
274    /// Apply edge collapse to mesh data structures
275    fn apply_collapse(
276        &self,
277        collapse: &EdgeCollapse,
278        vertices: &mut Vec<VertexInfo>,
279        faces: &mut Vec<[usize; 3]>,
280        vertex_mapping: &mut HashMap<usize, usize>,
281    ) -> Result<()> {
282        let v1_idx = collapse.vertex1;
283        let v2_idx = collapse.vertex2;
284        
285                 // Update vertex position and combine quadrics
286         let v2_quadric = vertices[v2_idx].quadric.clone();
287         vertices[v1_idx].position = collapse.new_position;
288         vertices[v1_idx].quadric += v2_quadric;
289        
290        // Update faces that reference v2 to reference v1
291        for face in faces.iter_mut() {
292            for vertex_ref in face.iter_mut() {
293                if *vertex_ref == v2_idx {
294                    *vertex_ref = v1_idx;
295                }
296            }
297        }
298        
299        // Remove degenerate faces (triangles with repeated vertices)
300        faces.retain(|face| {
301            face[0] != face[1] && face[1] != face[2] && face[2] != face[0]
302        });
303        
304        // Update vertex mapping
305        vertex_mapping.insert(v2_idx, v1_idx);
306        
307        // Update adjacency information
308        let v2_neighbors = vertices[v2_idx].neighbors.clone();
309        for &neighbor in &v2_neighbors {
310            if neighbor != v1_idx {
311                vertices[v1_idx].neighbors.insert(neighbor);
312                vertices[neighbor].neighbors.remove(&v2_idx);
313                vertices[neighbor].neighbors.insert(v1_idx);
314            }
315        }
316        vertices[v1_idx].neighbors.remove(&v2_idx);
317        
318        // Mark v2 as invalid
319        vertices[v2_idx].neighbors.clear();
320        vertices[v2_idx].faces.clear();
321        
322        Ok(())
323    }
324    
325    /// Rebuild mesh from simplified vertex and face data
326    fn rebuild_mesh(&self, vertices: &[VertexInfo], faces: &[[usize; 3]]) -> TriangleMesh {
327        // Create mapping from old indices to new indices (compacting)
328        let mut old_to_new: HashMap<usize, usize> = HashMap::new();
329        let mut new_vertices = Vec::new();
330        
331        // Collect valid vertices
332        for (old_idx, vertex) in vertices.iter().enumerate() {
333            if !vertex.neighbors.is_empty() || !vertex.faces.is_empty() {
334                old_to_new.insert(old_idx, new_vertices.len());
335                new_vertices.push(vertex.position);
336            }
337        }
338        
339        // Update face indices
340        let new_faces: Vec<[usize; 3]> = faces.iter()
341            .filter_map(|face| {
342                if let (Some(&new_v0), Some(&new_v1), Some(&new_v2)) = (
343                    old_to_new.get(&face[0]),
344                    old_to_new.get(&face[1]),
345                    old_to_new.get(&face[2]),
346                ) {
347                    Some([new_v0, new_v1, new_v2])
348                } else {
349                    None
350                }
351            })
352            .collect();
353        
354        TriangleMesh::from_vertices_and_faces(new_vertices, new_faces)
355    }
356}
357
358impl MeshSimplifier for QuadricErrorSimplifier {
359    /// Simplify mesh with target reduction ratio
360    fn simplify(&self, mesh: &TriangleMesh, reduction_ratio: f32) -> Result<TriangleMesh> {
361        if mesh.is_empty() {
362            return Err(Error::InvalidData("Mesh is empty".to_string()));
363        }
364        
365        if !(0.0..=1.0).contains(&reduction_ratio) {
366            return Err(Error::InvalidData("Reduction ratio must be between 0.0 and 1.0".to_string()));
367        }
368        
369        if reduction_ratio == 0.0 {
370            return Ok(mesh.clone());
371        }
372        
373        let target_face_count = ((1.0 - reduction_ratio) * mesh.faces.len() as f32) as usize;
374        
375        // Initialize vertex information
376        let mut vertices = self.initialize_vertices(mesh);
377        let mut faces = mesh.faces.clone();
378        let mut vertex_mapping = HashMap::new();
379        
380        // Find boundary edges
381        let boundary_edges = self.find_boundary_edges(mesh);
382        
383        // Generate initial edge collapses
384        let mut collapse_queue = PriorityQueue::new();
385        let initial_collapses = self.generate_edge_collapses(&vertices, &boundary_edges);
386        
387        for (idx, collapse) in initial_collapses.into_iter().enumerate() {
388            collapse_queue.push(idx, collapse);
389        }
390        
391        // Perform edge collapses until target is reached
392        let mut collapse_counter = 0;
393        while faces.len() > target_face_count && !collapse_queue.is_empty() {
394            if let Some((_, collapse)) = collapse_queue.pop() {
395                // Verify collapse is still valid
396                if vertices[collapse.vertex1].neighbors.contains(&collapse.vertex2) {
397                    self.apply_collapse(&collapse, &mut vertices, &mut faces, &mut vertex_mapping)?;
398                    collapse_counter += 1;
399                    
400                    // Periodically regenerate collapses to maintain quality
401                    if collapse_counter % 100 == 0 {
402                        collapse_queue.clear();
403                        let new_collapses = self.generate_edge_collapses(&vertices, &boundary_edges);
404                        for (idx, collapse) in new_collapses.into_iter().enumerate() {
405                            collapse_queue.push(collapse_counter * 1000 + idx, collapse);
406                        }
407                    }
408                }
409            }
410        }
411        
412        Ok(self.rebuild_mesh(&vertices, &faces))
413    }
414}
415
416#[cfg(test)]
417mod tests {
418    use super::*;
419    use nalgebra::Point3;
420
421    #[test]
422    fn test_quadric_error_simplifier_creation() {
423        let simplifier = QuadricErrorSimplifier::new();
424        assert!(simplifier.preserve_boundary);
425        assert!(simplifier.max_edge_length.is_none());
426    }
427
428    #[test]
429    fn test_plane_computation() {
430        let simplifier = QuadricErrorSimplifier::new();
431        let v0 = Point3::new(0.0, 0.0, 0.0);
432        let v1 = Point3::new(1.0, 0.0, 0.0);
433        let v2 = Point3::new(0.0, 1.0, 0.0);
434        
435        let plane = simplifier.compute_plane(&v0, &v1, &v2);
436        
437        // Should be z = 0 plane: 0x + 0y + 1z + 0 = 0
438        assert!((plane[0]).abs() < 1e-6);
439        assert!((plane[1]).abs() < 1e-6);
440        assert!((plane[2] - 1.0).abs() < 1e-6);
441        assert!((plane[3]).abs() < 1e-6);
442    }
443
444    #[test]
445    fn test_empty_mesh() {
446        let simplifier = QuadricErrorSimplifier::new();
447        let mesh = TriangleMesh::new();
448        
449        let result = simplifier.simplify(&mesh, 0.5);
450        assert!(result.is_err());
451    }
452
453    #[test]
454    fn test_zero_reduction() {
455        let simplifier = QuadricErrorSimplifier::new();
456        let vertices = vec![
457            Point3::new(0.0, 0.0, 0.0),
458            Point3::new(1.0, 0.0, 0.0),
459            Point3::new(0.5, 1.0, 0.0),
460        ];
461        let faces = vec![[0, 1, 2]];
462        let mesh = TriangleMesh::from_vertices_and_faces(vertices, faces);
463        
464        let result = simplifier.simplify(&mesh, 0.0).unwrap();
465        assert_eq!(result.vertex_count(), 3);
466        assert_eq!(result.face_count(), 1);
467    }
468
469    #[test]
470    fn test_invalid_reduction_ratio() {
471        let simplifier = QuadricErrorSimplifier::new();
472        let vertices = vec![
473            Point3::new(0.0, 0.0, 0.0),
474            Point3::new(1.0, 0.0, 0.0),
475            Point3::new(0.5, 1.0, 0.0),
476        ];
477        let faces = vec![[0, 1, 2]];
478        let mesh = TriangleMesh::from_vertices_and_faces(vertices, faces);
479        
480        assert!(simplifier.simplify(&mesh, -0.1).is_err());
481        assert!(simplifier.simplify(&mesh, 1.1).is_err());
482    }
483
484    #[test]
485    fn test_quadric_matrix_computation() {
486        let simplifier = QuadricErrorSimplifier::new();
487        let plane = Vector4::new(1.0, 0.0, 0.0, -1.0); // x = 1 plane
488        
489        let quadric = simplifier.plane_to_quadric(&plane);
490        
491        // Check diagonal elements
492        assert!((quadric[(0, 0)] - 1.0).abs() < 1e-10);
493        assert!((quadric[(1, 1)] - 0.0).abs() < 1e-10);
494        assert!((quadric[(2, 2)] - 0.0).abs() < 1e-10);
495        assert!((quadric[(3, 3)] - 1.0).abs() < 1e-10);
496    }
497
498    #[test]
499    fn test_tetrahedron_simplification() {
500        let simplifier = QuadricErrorSimplifier::new();
501        
502        // Create a tetrahedron
503        let vertices = vec![
504            Point3::new(0.0, 0.0, 0.0),
505            Point3::new(1.0, 0.0, 0.0),
506            Point3::new(0.5, 1.0, 0.0),
507            Point3::new(0.5, 0.5, 1.0),
508        ];
509        let faces = vec![
510            [0, 1, 2],
511            [0, 1, 3],
512            [0, 2, 3],
513            [1, 2, 3],
514        ];
515        let mesh = TriangleMesh::from_vertices_and_faces(vertices, faces);
516        
517        let simplified = simplifier.simplify(&mesh, 0.5).unwrap();
518        
519        // Should have fewer faces
520        assert!(simplified.face_count() <= mesh.face_count());
521        assert!(simplified.vertex_count() <= mesh.vertex_count());
522    }
523}