Skip to main content

brepkit_operations/
offset_v2.rs

1//! V2 offset operations delegating to brepkit-offset.
2
3use brepkit_offset::{JointType, OffsetError, OffsetOptions};
4use brepkit_topology::Topology;
5use brepkit_topology::face::FaceId;
6use brepkit_topology::solid::SolidId;
7
8use crate::OperationsError;
9
10/// Map an `OffsetError` to the most appropriate `OperationsError` variant,
11/// preserving structured error information where possible.
12fn map_offset_error(e: OffsetError) -> OperationsError {
13    match e {
14        OffsetError::Topology(t) => OperationsError::Topology(t),
15        OffsetError::Math(m) => OperationsError::Math(m),
16
17        other => OperationsError::InvalidInput {
18            reason: format!("{other}"),
19        },
20    }
21}
22
23/// Offset all faces of a solid (V2 pipeline).
24///
25/// # Errors
26///
27/// Returns an error if the offset fails.
28pub fn offset_solid_v2(
29    topo: &mut Topology,
30    solid: SolidId,
31    distance: f64,
32) -> Result<SolidId, OperationsError> {
33    brepkit_offset::offset_solid(topo, solid, distance, OffsetOptions::default())
34        .map_err(map_offset_error)
35}
36
37/// Shell (hollow solid) operation (V2 pipeline).
38///
39/// # Errors
40///
41/// Returns an error if the offset fails.
42pub fn shell_v2(
43    topo: &mut Topology,
44    solid: SolidId,
45    thickness: f64,
46    exclude: &[FaceId],
47) -> Result<SolidId, OperationsError> {
48    brepkit_offset::thick_solid(topo, solid, thickness, exclude, OffsetOptions::default())
49        .map_err(map_offset_error)
50}
51
52/// Offset with arc joints (V2 pipeline).
53///
54/// # Errors
55///
56/// Returns an error if the offset fails.
57pub fn offset_solid_arc_v2(
58    topo: &mut Topology,
59    solid: SolidId,
60    distance: f64,
61) -> Result<SolidId, OperationsError> {
62    let options = OffsetOptions {
63        joint: JointType::Arc,
64        ..Default::default()
65    };
66    brepkit_offset::offset_solid(topo, solid, distance, options).map_err(map_offset_error)
67}
68
69#[cfg(test)]
70mod tests {
71    #![allow(clippy::unwrap_used)]
72    use super::*;
73    use brepkit_topology::Topology;
74
75    #[test]
76    fn offset_v2_box() {
77        let mut topo = Topology::new();
78        let solid = crate::primitives::make_box(&mut topo, 2.0, 2.0, 2.0).unwrap();
79        let result = offset_solid_v2(&mut topo, solid, 0.5).unwrap();
80        let shell = topo
81            .shell(topo.solid(result).unwrap().outer_shell())
82            .unwrap();
83        assert_eq!(shell.faces().len(), 6);
84    }
85}