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
141
142
143
144
//! Whether support faces cover a patch (`quiddity._support_patches.covered_patch`), and the
//! straight-edged planar shapes the section proofs build to ask it and to probe volumes with:
//! a polygon face (`Face(Wire.make_polygon(...))`) and a closed polyhedron (`Solid(Shell(...))`,
//! a ruled loft or an extrusion of a polygon).
//!
//! Python cuts the patch by each support in turn and skips a support whose conservative box is
//! apart from a fragment's (`_separated`), a saving that cannot change the answer. Here the
//! question is answered in the patch's parameter space by [`kernel::cover`](crate::kernel::cover),
//! checked against every question Python asks over the corpus (`crates/haecceity/tests/
//! patches.rs`), which reads only the supports on the patch's own surface.
use crate::kernel::brep::{Edge, Face, Loop, Part, Solid};
use crate::kernel::cover::{FaceRef, covered};
use crate::kernel::geom::{self, Curve, Frame, Surface, V3};
internal_entry! {
/// `covered_patch`: whether the supports leave at most 1e-9 of the patch's area uncovered.
pub fn covered_patch(patch: FaceRef<'_>, supports: &[FaceRef<'_>]) -> bool {
covered(patch, supports)
}
}
/// The unit normal of a planar loop by Newell's method, its walk counter-clockwise about it.
fn loop_normal(points: &[V3], corners: &[usize]) -> Option<V3> {
let mut n = [0.0; 3];
for (i, &a) in corners.iter().enumerate() {
let (p, q) = (points[a], points[corners[(i + 1) % corners.len()]]);
n[0] += (p[1] - q[1]) * (p[2] + q[2]);
n[1] += (p[2] - q[2]) * (p[0] + q[0]);
n[2] += (p[0] - q[0]) * (p[1] + q[1]);
}
geom::unit(n)
}
internal_entry! {
/// A part of planar faces bounded by straight edges: *faces* lists each face's corners as
/// indices into *points*, walked counter-clockwise seen from the side the face's normal points
/// to. With `closed`, the faces are one solid's shell and must point outward; otherwise they
/// are loose faces. `None` for a face with fewer than three corners or no area, or a repeated
/// corner.
pub fn polygon_part(points: &[V3], faces: &[Vec<usize>], closed: bool) -> Option<Part> {
let mut edges: Vec<Edge> = Vec::new();
let mut keys: Vec<(usize, usize)> = Vec::new();
let mut built = Vec::new();
for corners in faces {
if corners.len() < 3 {
return None;
}
let z = loop_normal(points, corners)?;
let origin = points[corners[0]];
let x = geom::unit(geom::sub(points[corners[1]], origin))?;
let x = geom::unit(geom::sub(x, geom::scale(z, geom::dot(x, z))))?;
let mut uses = Vec::new();
for (i, &a) in corners.iter().enumerate() {
let b = corners[(i + 1) % corners.len()];
if a == b {
return None;
}
let key = (a.min(b), a.max(b));
let index = keys.iter().position(|&k| k == key).unwrap_or_else(|| {
let (start, end) = (points[key.0], points[key.1]);
keys.push(key);
edges.push(Edge {
curve: Curve::Line {
origin: start,
dir: geom::sub(end, start),
},
start,
end,
vertices: key,
same_sense: true,
samples: vec![start, end],
});
edges.len() - 1
});
uses.push((index, a < b));
}
built.push(Face {
surface: Surface::Plane {
frame: Frame {
origin,
x,
y: geom::cross(z, x),
z,
},
},
reversed: false,
loops: vec![Loop {
edges: uses,
vertex: None,
}],
solid: closed.then_some(0),
pcurves: Vec::new(),
});
}
let solids = if closed {
vec![Solid {
faces: (0..built.len()).collect(),
}]
} else {
Vec::new()
};
Some(Part::new(built, edges, solids))
}
}
internal_entry! {
/// One polygon face through *corners* in order (`Face(Wire.make_polygon(corners))`), its normal
/// by the walk.
pub fn polygon_face(corners: &[V3]) -> Option<Part> {
polygon_part(corners, &[(0..corners.len()).collect()], false)
}
}
internal_entry! {
/// The closed solid between two copies of one polygon, *low* and *high* corner for corner, each
/// side a planar quad (a ruled loft of two polygons whose corners are joined by parallel lines,
/// `Solid.make_loft(..., ruled=True)`, or an extrusion). `None` unless the result is a valid
/// solid.
pub fn ruled_prism(low: &[V3], high: &[V3]) -> Option<Part> {
let n = low.len();
if n < 3 || high.len() != n {
return None;
}
let mut points = low.to_vec();
points.extend_from_slice(high);
// Low corners walked so that the normal points away from the high end.
let axis = geom::sub(high[0], low[0]);
let low_ring: Vec<usize> = (0..n).collect();
let forward = geom::dot(loop_normal(&points, &low_ring)?, axis) < 0.0;
let ring: Vec<usize> = if forward {
low_ring
} else {
(0..n).rev().collect()
};
let mut faces = vec![ring.clone(), ring.iter().rev().map(|&i| i + n).collect()];
for (k, &a) in ring.iter().enumerate() {
let b = ring[(k + 1) % n];
faces.push(vec![b, a, a + n, b + n]);
}
let part = polygon_part(&points, &faces, true)?;
part.solid_is_valid(0).then_some(part)
}
}