Skip to main content

geop_core_topology/model/
display.rs

1use core::fmt::{self, Display};
2use std::collections::HashMap;
3use std::hash::Hash;
4
5use geop_core_math::scalars::Scalar;
6
7use super::Model;
8
9/// `map`'s entries sorted by id (`.0`, the stable numeric part every
10/// `*Id` newtype wraps) — a `HashMap`'s own iteration order is arbitrary,
11/// which would otherwise make `Model`'s `Display` output nondeterministic
12/// from run to run.
13fn sorted_by_id<Id: Copy + Eq + Hash, V>(map: &HashMap<Id, V>) -> Vec<(Id, &V)>
14where
15    Id: Into<u64>,
16{
17    let mut entries: Vec<(Id, &V)> = map.iter().map(|(&id, v)| (id, v)).collect();
18    entries.sort_by_key(|(id, _)| (*id).into());
19    entries
20}
21
22impl<S: Scalar> Display for Model<S> {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        writeln!(f, "Model {{")?;
25
26        writeln!(f, "  vertices:")?;
27        for (id, vertex) in sorted_by_id(&self.vertices) {
28            writeln!(f, "    {id}: point={}", vertex.point)?;
29        }
30
31        writeln!(f, "  edges:")?;
32        for (id, edge) in sorted_by_id(&self.edges) {
33            writeln!(
34                f,
35                "    {id}: {} -> {}, curve={}",
36                edge.start_vertex, edge.end_vertex, edge.curve
37            )?;
38        }
39
40        writeln!(f, "  coedges:")?;
41        for (id, coedge) in sorted_by_id(&self.coedges) {
42            writeln!(
43                f,
44                "    {id}: geometry={:?}, sense={:?}, next={}, prev={}, face={}, pcurve={}",
45                coedge.geometry, coedge.sense, coedge.next, coedge.prev, coedge.face, coedge.pcurve
46            )?;
47        }
48
49        writeln!(f, "  faces:")?;
50        for (id, face) in sorted_by_id(&self.faces) {
51            writeln!(
52                f,
53                "    {id}: shell={}, outer={:?}, holes={:?}, surface={}",
54                face.shell, face.outer, face.holes, face.surface
55            )?;
56        }
57
58        writeln!(f, "  shells:")?;
59        for (id, shell) in sorted_by_id(&self.shells) {
60            writeln!(
61                f,
62                "    {id}: solid={}, faces={:?}",
63                shell.solid, shell.faces
64            )?;
65        }
66
67        writeln!(f, "  solids:")?;
68        for (id, solid) in sorted_by_id(&self.solids) {
69            writeln!(f, "    {id}: shells={:?}", solid.shells)?;
70        }
71
72        write!(f, "}}")
73    }
74}