1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::collections::BTreeSet;
use fj_interop::mesh::Color;
use crate::{
algorithms::validate::ValidationConfig,
objects::{Face, Faces, Handedness},
};
use super::{
curve::CurveCache, cycle::CycleApprox, Approx, ApproxPoint, Tolerance,
};
impl Approx for &Faces {
type Approximation = BTreeSet<FaceApprox>;
type Cache = CurveCache;
fn approx_with_cache(
self,
tolerance: impl Into<Tolerance>,
cache: &mut Self::Cache,
) -> Self::Approximation {
let tolerance = tolerance.into();
let approx = self
.into_iter()
.map(|face| face.approx_with_cache(tolerance, cache))
.collect();
let min_distance = ValidationConfig::default().distinct_min_distance;
let mut all_points: BTreeSet<ApproxPoint<2>> = BTreeSet::new();
for approx in &approx {
let approx: &FaceApprox = approx;
for point in &approx.points() {
for p in &all_points {
let distance =
(p.global_form - point.global_form).magnitude();
if p.global_form != point.global_form
&& distance < min_distance
{
let a = p;
let b = point;
panic!(
"Invalid approximation: \
Distinct points are too close \
(a: {:?}, b: {:?}, distance: {distance})\n\
source of `a`: {:#?}\n\
source of `b`: {:#?}\n",
a.global_form, b.global_form, a.source, b.source
);
}
}
all_points.insert(point.clone());
}
}
approx
}
}
impl Approx for &Face {
type Approximation = FaceApprox;
type Cache = CurveCache;
fn approx_with_cache(
self,
tolerance: impl Into<Tolerance>,
cache: &mut Self::Cache,
) -> Self::Approximation {
let tolerance = tolerance.into();
let exterior = self.exterior().approx_with_cache(tolerance, cache);
let mut interiors = BTreeSet::new();
for cycle in self.interiors() {
let cycle = cycle.approx_with_cache(tolerance, cache);
interiors.insert(cycle);
}
FaceApprox {
exterior,
interiors,
color: self.color(),
coord_handedness: self.coord_handedness(),
}
}
}
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct FaceApprox {
pub exterior: CycleApprox,
pub interiors: BTreeSet<CycleApprox>,
pub color: Color,
pub coord_handedness: Handedness,
}
impl FaceApprox {
pub fn points(&self) -> BTreeSet<ApproxPoint<2>> {
let mut points = BTreeSet::new();
points.extend(self.exterior.points());
for cycle_approx in &self.interiors {
points.extend(cycle_approx.points());
}
points
}
}