Skip to main content

brep_offset/offset/
offset_shell.rs

1use crate::boolean::{assemble_open_fragments, edge_interior_lies_on, finalize_assembled_solid};
2pub use crate::classification::{parameter_point_in_face, PolygonClass};
3use crate::face_merge::merge_same_surface_faces_open;
4use crate::fragment::{fragment_solid, FaceFragmentRecord, FragmentEdgeSource};
5use crate::imprint::{
6    EdgeSplitRecord, FaceImprints, FaceKey, FacePcurve, ImprintOptions, ImprintPieceRecord,
7    ImprintResultRecord, ImprintVertex,
8};
9pub use crate::mass_properties::parameter_space_area;
10pub use crate::project_point_to_surface;
11pub use crate::topology::{
12    BrepSolid, CoedgeRecord, EdgeRecord, FaceRecord, LoopRecord, ShellRecord, VertexRecord,
13};
14pub use crate::{
15    apply_edge_splits, build_imprints, classify_point, DiagnosticSeverity,
16    KernelDiagnostics, KernelOutcome, KernelStage, KernelTolerances, PointClass, Vec2, Vec3,
17};
18pub use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
19use serde::Serialize;
20
21pub const SOURCE_OPERAND: u8 = 250;
22
23/// The smallest |distance| an offset shell answers: at or under it the shell
24/// refuses ("distance must be finite and non-zero"). One constant, because the
25/// revolved-wedge lane runs in FRONT of that refusal and must stand down exactly
26/// where it fires — it once took (−1e-7, 0) as an outward distance and built a
27/// sliver where the shell had always refused.
28const MINIMUM_DISTANCE: f64 = 1e-7;
29
30fn debug_enabled() -> bool {
31    env_read!("BREP_OS_DEBUG").is_some_and(|value| !value.is_empty() && value != "0")
32}
33
34macro_rules! os_debug {
35    ($($arg:tt)*) => {
36        if debug_enabled() {
37            eprintln!($($arg)*);
38        }
39    };
40}
41
42#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
43#[serde(rename_all = "lowercase")]
44pub enum OffsetFaceRole {
45    Source,
46    Offset,
47    Wall,
48}
49
50#[derive(Clone, Debug, Serialize)]
51pub struct OffsetShellResultRecord {
52    pub solid: BrepSolid,
53    pub face_images: Vec<OffsetShellFaceImageRecord>,
54}
55
56#[derive(Clone, Debug, Serialize)]
57pub struct OffsetShellFaceImageRecord {
58    pub role: OffsetFaceRole,
59    pub source_face_id: u64,
60}
61
62#[derive(Clone)]
63struct Carrier {
64    solid: BrepSolid,
65    source_face_id: u64,
66    kind: OffsetFaceRole,
67}
68
69#[path = "offset_shell/carriers.rs"]
70pub mod carriers;
71#[path = "offset_shell/smooth_sync.rs"]
72mod smooth_sync;
73#[path = "offset_shell/carrier_rebuild.rs"]
74pub mod carrier_rebuild;
75#[path = "offset_shell/connectors.rs"]
76mod connectors;
77#[path = "offset_shell/orientation.rs"]
78mod orientation;
79#[path = "offset_shell/rim_welds.rs"]
80mod rim_welds;
81#[path = "offset_shell/rim_round.rs"]
82pub mod rim_round;
83
84#[path = "offset_shell/revolved_wedge.rs"]
85pub mod revolved_wedge;
86#[path = "offset_shell/planar_shell.rs"]
87pub mod planar_shell;
88#[path = "offset_shell/sphere_body.rs"]
89pub mod sphere_body;
90#[path = "offset_shell/sphere_cut_shell.rs"]
91pub mod sphere_cut_shell;
92#[path = "offset_shell/outline_offset.rs"]
93pub mod outline_offset;
94#[path = "offset_shell/vacated_rim.rs"]
95mod vacated_rim;
96#[path = "offset_shell/pipeline.rs"]
97pub mod pipeline;
98#[path = "offset_shell/acceptance.rs"]
99pub mod acceptance;
100#[path = "offset_shell/profile.rs"]
101mod profile;
102#[path = "offset_shell/solid.rs"]
103pub mod solid;
104#[path = "offset_shell/joint_offset.rs"]
105mod joint_offset;
106#[path = "offset_shell/refusals.rs"]
107pub mod refusals;
108#[path = "offset_shell/torus_body.rs"]
109pub mod torus_body;
110#[path = "offset_shell/bore_dimples.rs"]
111mod bore_dimples;
112#[path = "offset_shell/revolved_parallel.rs"]
113pub mod revolved_parallel;
114#[path = "offset_shell/pipe_parallel.rs"]
115pub mod pipe_parallel;
116#[path = "offset_shell/prism_parallel.rs"]
117pub mod prism_parallel;
118#[path = "offset_shell/prism_erosion.rs"]
119pub mod prism_erosion;
120#[path = "offset_shell/revolved_erosion.rs"]
121pub mod revolved_erosion;
122#[path = "offset_shell/prism_growth.rs"]
123pub mod prism_growth;
124#[path = "offset_shell/full_turn.rs"]
125mod full_turn;
126#[path = "offset_shell/rim_witness.rs"]
127mod rim_witness;
128#[path = "offset_shell/tangent_rim_cuts.rs"]
129mod tangent_rim_cuts;
130#[path = "offset_shell/opening_surface_cut.rs"]
131mod opening_surface_cut;
132#[path = "offset_shell/outward_side_widen.rs"]
133mod outward_side_widen;
134#[path = "offset_shell/box_cavity.rs"]
135pub mod box_cavity;
136#[path = "offset_shell/revolved_shell.rs"]
137pub mod revolved_shell;
138
139pub use carrier_rebuild::*;
140pub use carriers::*;
141use connectors::*;
142use orientation::*;
143pub use outline_offset::*;
144pub use vacated_rim::*;
145pub use rim_welds::*;
146pub use smooth_sync::*;
147
148pub use pipeline::{offset_shell, offset_shell_with_diagnostics};
149pub use solid::{offset_shell_by_boolean, offset_shell_closed, offset_solid, OffsetCorners};
150pub use refusals::{is_offset_shell_no_retained_face, is_offset_shell_open_curved_rim};
151pub use orientation::{flip_all_faces, flip_shell_faces, orient_open_solid_faces};
152
153/// Merge connected shells, preserving the first shell record and face encounter order.
154pub fn merge_connected_shells(solid: &mut BrepSolid, shell_unions: Vec<(usize, usize)>) {
155    if shell_unions.is_empty() {
156        return;
157    }
158    let mut parent = (0..solid.shells.len()).collect::<Vec<_>>();
159    fn root(parent: &mut [usize], index: usize) -> usize {
160        crate::union_find::find_root(parent, index)
161    }
162    for (first, second) in shell_unions {
163        let first_root = root(&mut parent, first);
164        let second_root = root(&mut parent, second);
165        if first_root != second_root {
166            parent[second_root] = first_root;
167        }
168    }
169    let original = std::mem::take(&mut solid.shells);
170    let mut merged = Vec::<ShellRecord>::new();
171    let mut group_of = HashMap::<usize, usize>::default();
172    for (index, shell) in original.into_iter().enumerate() {
173        let group = root(&mut parent, index);
174        if let Some(target) = group_of.get(&group).copied() {
175            merged[target].faces.extend(shell.faces);
176        } else {
177            group_of.insert(group, merged.len());
178            merged.push(shell);
179        }
180    }
181    solid.shells = merged;
182}