use crate::geom3::half_edge3::{
BestEffortOpts, DecimateOpts, DecimateReport, HalfEdgeMesh3, RepairOpts, RepairReport,
};
use crate::{Mesh3, Result};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct EditReport {
pub repair: RepairReport,
pub smoothing_passes: usize,
pub decimate_collapses: usize,
}
impl std::fmt::Display for EditReport {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ingest: {}", self.repair)?;
if self.smoothing_passes > 0 {
write!(f, "; {} smoothing passes", self.smoothing_passes)?;
}
if self.decimate_collapses > 0 {
write!(f, "; {} collapses", self.decimate_collapses)?;
}
Ok(())
}
}
pub struct MeshEditor<'a> {
half_edge: HalfEdgeMesh3,
original: &'a Mesh3,
report: EditReport,
}
impl<'a> MeshEditor<'a> {
pub fn new(mesh: &'a Mesh3) -> Result<Self> {
Ok(Self {
half_edge: HalfEdgeMesh3::try_from(mesh)?,
original: mesh,
report: EditReport::default(),
})
}
pub fn with_repair(mesh: &'a Mesh3, opts: &RepairOpts) -> Result<Self> {
let (half_edge, repair) = HalfEdgeMesh3::from_mesh_repaired(mesh, opts)?;
Ok(Self {
half_edge,
original: mesh,
report: EditReport {
repair,
..Default::default()
},
})
}
pub fn original(&self) -> &'a Mesh3 {
self.original
}
pub fn report(&self) -> &EditReport {
&self.report
}
pub fn half_edge(&self) -> &HalfEdgeMesh3 {
&self.half_edge
}
pub fn half_edge_mut(&mut self) -> &mut HalfEdgeMesh3 {
&mut self.half_edge
}
pub fn vertex_count(&self) -> usize {
self.half_edge.vertex_count()
}
pub fn face_count(&self) -> usize {
self.half_edge.face_count()
}
pub fn smooth(&mut self, iterations: usize) -> Result<&mut Self> {
for _ in 0..iterations {
self.half_edge.neighborhood_smooth()?;
}
self.report.smoothing_passes += iterations;
Ok(self)
}
pub fn decimate_guaranteed(&mut self, opts: &DecimateOpts) -> Result<DecimateReport> {
let report = self.half_edge.decimate_guaranteed(opts)?;
self.report.decimate_collapses += report.collapses;
Ok(report)
}
pub fn decimate_best_effort(&mut self, opts: &BestEffortOpts) -> Result<DecimateReport> {
let report = self.half_edge.decimate_best_effort(opts)?;
self.report.decimate_collapses += report.collapses;
Ok(report)
}
pub fn to_mesh(&mut self, is_solid: bool, allow_attribute_loss: bool) -> Result<Mesh3> {
self.original
.check_attribute_loss("a mesh editing session", allow_attribute_loss)?;
self.half_edge.to_mesh(is_solid)
}
pub fn into_mesh(mut self, is_solid: bool, allow_attribute_loss: bool) -> Result<Mesh3> {
self.to_mesh(is_solid, allow_attribute_loss)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::points::dist;
#[test]
fn a_clean_mesh_round_trips_unchanged() {
let mesh = crate::tests::engine_blade();
let editor = MeshEditor::new(&mesh).unwrap();
assert_eq!(editor.face_count(), mesh.faces().len());
assert!(editor.report().repair.is_clean());
let out = editor.into_mesh(false, false).unwrap();
assert_eq!(out.faces().len(), mesh.faces().len());
assert_eq!(out.points().len(), mesh.points().len());
}
#[test]
fn the_repairing_constructor_takes_a_dirty_mesh() {
let mesh = Mesh3::stanford_bunny_res4();
let editor = MeshEditor::with_repair(&mesh, &RepairOpts::default()).unwrap();
let repair = editor.report().repair;
assert!(!repair.is_clean());
assert_eq!(repair.faces_rejected_by_ingest, 0);
assert_eq!(editor.face_count(), 868);
}
#[test]
fn smoothing_is_counted_and_moves_points() {
let mesh = Mesh3::stanford_bunny_res4();
let mut editor = MeshEditor::with_repair(&mesh, &RepairOpts::default()).unwrap();
let before = editor.half_edge().clone_vertices().unwrap();
editor.smooth(2).unwrap();
let after = editor.half_edge().clone_vertices().unwrap();
assert_eq!(editor.report().smoothing_passes, 2);
assert_eq!(before.len(), after.len());
let moved = before
.iter()
.zip(after.iter())
.filter(|(a, b)| dist(*a, *b) > 1e-12)
.count();
assert!(moved > 0, "smoothing should have moved some points");
}
#[test]
fn zero_smoothing_passes_change_nothing() {
let mesh = crate::tests::engine_blade();
let mut editor = MeshEditor::new(&mesh).unwrap();
let before = editor.half_edge().clone_vertices().unwrap();
editor.smooth(0).unwrap();
let after = editor.half_edge().clone_vertices().unwrap();
assert_eq!(editor.report().smoothing_passes, 0);
assert_eq!(before, after);
}
#[test]
fn operations_chain() {
let mesh = Mesh3::stanford_bunny_res4();
let mut editor = MeshEditor::with_repair(&mesh, &RepairOpts::default()).unwrap();
editor.smooth(1).unwrap().smooth(1).unwrap();
assert_eq!(editor.report().smoothing_passes, 2);
}
#[test]
fn both_decimation_paths_are_reachable() {
const TOL: f64 = 0.01;
let mesh = Mesh3::stanford_bunny_res4();
let mut guaranteed = MeshEditor::with_repair(&mesh, &RepairOpts::default()).unwrap();
let before = guaranteed.face_count();
let a = guaranteed
.decimate_guaranteed(&DecimateOpts::to_tolerance(TOL))
.unwrap();
let mut best = MeshEditor::with_repair(&mesh, &RepairOpts::default()).unwrap();
let b = best
.decimate_best_effort(&BestEffortOpts::to_tolerance(TOL))
.unwrap();
for (report, editor) in [(a, &guaranteed), (b, &best)] {
assert!(
report.collapses > 0,
"nothing collapsed at a tolerance of {TOL}"
);
assert!(editor.face_count() < before);
assert_eq!(editor.report().decimate_collapses, report.collapses);
}
assert!(
best.face_count() < guaranteed.face_count(),
"best effort should be the more aggressive path: {} against {}",
best.face_count(),
guaranteed.face_count()
);
}
#[test]
fn a_guarantee_cannot_follow_a_best_effort_pass() {
let mesh = Mesh3::stanford_bunny_res4();
let mut editor = MeshEditor::with_repair(&mesh, &RepairOpts::default()).unwrap();
editor
.decimate_best_effort(&BestEffortOpts::to_tolerance(0.01))
.unwrap();
let err = editor
.decimate_guaranteed(&DecimateOpts::to_tolerance(0.01))
.expect_err("a guaranteed pass should refuse to follow a best-effort one");
assert!(
err.to_string().contains("best-effort"),
"the refusal should say why: {err}"
);
let mut editor = MeshEditor::with_repair(&mesh, &RepairOpts::default()).unwrap();
editor
.decimate_guaranteed(&DecimateOpts::to_tolerance(0.01))
.unwrap();
editor
.decimate_guaranteed(&DecimateOpts::to_tolerance(0.01))
.unwrap();
editor
.decimate_best_effort(&BestEffortOpts::to_tolerance(0.01))
.unwrap();
}
#[test]
fn the_original_is_the_mesh_the_session_started_from() {
let mesh = Mesh3::stanford_bunny_res4();
let mut editor = MeshEditor::with_repair(&mesh, &RepairOpts::default()).unwrap();
editor.smooth(3).unwrap();
assert_eq!(editor.original().faces().len(), mesh.faces().len());
assert_eq!(editor.original().points().len(), mesh.points().len());
}
#[test]
fn attributes_must_be_given_up_explicitly() {
let mut mesh = Mesh3::create_box(1.0, 1.0, 1.0, false);
let labels: Vec<u32> = (0..mesh.faces().len() as u32).collect();
mesh.set_face_labels(Some(labels)).unwrap();
let mut editor = MeshEditor::new(&mesh).unwrap();
assert!(
editor.to_mesh(false, false).is_err(),
"dropping attributes should require an opt-in"
);
let out = editor.to_mesh(false, true).unwrap();
assert!(out.face_labels().is_none());
}
#[test]
fn a_mesh_without_attributes_needs_no_opt_in() {
let mesh = Mesh3::create_box(1.0, 1.0, 1.0, false);
let editor = MeshEditor::new(&mesh).unwrap();
assert!(editor.into_mesh(false, false).is_ok());
}
#[test]
fn to_mesh_leaves_the_session_usable() {
let mesh = Mesh3::stanford_bunny_res4();
let mut editor = MeshEditor::with_repair(&mesh, &RepairOpts::default()).unwrap();
let first = editor.to_mesh(false, true).unwrap();
editor.smooth(1).unwrap();
let second = editor.to_mesh(false, true).unwrap();
assert_eq!(first.faces().len(), second.faces().len());
assert_eq!(editor.report().smoothing_passes, 1);
}
#[test]
fn is_solid_is_carried_onto_the_result() {
let mesh = Mesh3::create_box(1.0, 1.0, 1.0, false);
let solid = MeshEditor::new(&mesh)
.unwrap()
.into_mesh(true, false)
.unwrap();
assert!(solid.is_solid());
let hollow = MeshEditor::new(&mesh)
.unwrap()
.into_mesh(false, false)
.unwrap();
assert!(!hollow.is_solid());
}
}