axiolid-construct 0.3.6

Solid generation: profiles, lofts, sweeps, revolutions and half-space clipping
Documentation
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! General contour revolution (ADR 0059).
//!
//! Volumes are checked against Pappus (`V = 2*pi*R_centroid*A`), computed
//! independently of anything the kernel does.

use axiolid_brep_audit::geometric_audit;
use axiolid_construct::revolve_exact::revolve_profile_exact;
use axiolid_core::{Frame2, Interval, Point2, Point3, Tolerance, Vec2, Vec3};
use axiolid_curve::{Circle2, Curve2, Line2};
use axiolid_profile::{Contour, ContourProfile, Profile, ProfileSegment, SectionProfile};
use axiolid_surface::Surface;

const TAU: f64 = core::f64::consts::TAU;

fn line(from: Point2, to: Point2) -> ProfileSegment {
    ProfileSegment {
        curve: Curve2::Line(Line2 {
            origin: from,
            direction: to - from,
        }),
        domain: Interval::UNIT,
        same_sense: true,
    }
}

fn contour(segments: Vec<ProfileSegment>) -> Profile {
    Profile::Contour(ContourProfile {
        outer: Contour::new(segments),
        holes: Vec::new(),
    })
}

fn revolve(profile: &Profile, axis_x: f64) -> axiolid_brep::ExactBRep {
    let solid = revolve_profile_exact(
        profile,
        Point3::new(axis_x, 0.0, 0.0),
        Vec3::Y,
        TAU,
        Tolerance::METRE,
    )
    .expect("a contour clear of the axis revolves");
    let health = geometric_audit(&solid, Tolerance::METRE);
    assert!(
        health.is_consistent(),
        "revolved solid must audit clean, found {:?}",
        health.defects()
    );
    solid
}

#[test]
fn a_rectangular_contour_matches_the_dedicated_rectangle_path() {
    // The same shape through two independent code paths must agree, or one
    // of them is wrong.
    let as_contour = contour(vec![
        line(Point2::new(4.0, -1.5), Point2::new(6.0, -1.5)),
        line(Point2::new(6.0, -1.5), Point2::new(6.0, 1.5)),
        line(Point2::new(6.0, 1.5), Point2::new(4.0, 1.5)),
        line(Point2::new(4.0, 1.5), Point2::new(4.0, -1.5)),
    ]);
    let solid = revolve(&as_contour, 0.0);

    let mut radii: Vec<f64> = solid
        .surfaces()
        .iter()
        .filter_map(|s| match s {
            Surface::Cylinder(c) => Some(c.radius),
            _ => None,
        })
        .collect();
    radii.sort_by(f64::total_cmp);
    assert_eq!(
        radii.len(),
        2,
        "an annular tube has two walls, got {radii:?}"
    );
    assert!((radii[0] - 4.0).abs() < 1e-12, "inner: {radii:?}");
    assert!((radii[1] - 6.0).abs() < 1e-12, "outer: {radii:?}");

    // Pappus: area 2*3 = 6, centroid at 5.
    let pappus = TAU * 5.0 * 6.0;
    let value = axiolid_measure::exact_properties(&solid, Tolerance::METRE)
        .expect("a revolved solid is measurable")
        .signed_volume;
    // Signed: a revolution built inside out measures -pappus.
    assert!(
        (value - pappus).abs() < 1e-9,
        "expected {pappus}, got {value}"
    );
}

#[test]
fn an_oblique_segment_sweeps_a_cone() {
    // Measured: a segment that is neither parallel nor perpendicular to the
    // axis sweeps a cone, with r affine in z.
    let trapezoid = contour(vec![
        line(Point2::new(2.0, 0.0), Point2::new(4.0, 0.0)),
        line(Point2::new(4.0, 0.0), Point2::new(3.0, 2.0)),
        line(Point2::new(3.0, 2.0), Point2::new(2.0, 2.0)),
        line(Point2::new(2.0, 2.0), Point2::new(2.0, 0.0)),
    ]);
    let solid = revolve(&trapezoid, 0.0);
    let cones = solid
        .surfaces()
        .iter()
        .filter(|s| matches!(s, Surface::Cone(_)))
        .count();
    assert_eq!(cones, 1, "the oblique edge must sweep exactly one cone");
}

#[test]
fn an_arc_segment_sweeps_a_torus_and_matches_pappus() {
    // A quarter-round fillet on the outer face. Pappus gives the volume from
    // the section area and its centroid, independently of the kernel.
    let r = 0.5;
    let quarter = core::f64::consts::FRAC_PI_2;
    // Section: a 2x2 square at x in [3,5] with the top-outer corner rounded.
    let profile = contour(vec![
        line(Point2::new(3.0, 0.0), Point2::new(5.0, 0.0)),
        line(Point2::new(5.0, 0.0), Point2::new(5.0, 2.0 - r)),
        // Arc from (5, 2-r) to (5-r, 2), centre (5-r, 2-r): a convex quarter.
        ProfileSegment {
            curve: Curve2::Circle(Circle2 {
                frame: Frame2 {
                    origin: Point2::new(5.0 - r, 2.0 - r),
                    x: Vec2::X,
                    y: Vec2::Y,
                },
                radius: r,
            }),
            domain: Interval::new(0.0, quarter),
            same_sense: true,
        },
        line(Point2::new(5.0 - r, 2.0), Point2::new(3.0, 2.0)),
        line(Point2::new(3.0, 2.0), Point2::new(3.0, 0.0)),
    ]);
    let solid = revolve(&profile, 0.0);

    let tori: Vec<(f64, f64)> = solid
        .surfaces()
        .iter()
        .filter_map(|s| match s {
            Surface::Torus(t) => Some((t.major_radius, t.minor_radius)),
            _ => None,
        })
        .collect();
    assert_eq!(tori.len(), 1, "the arc must sweep one torus, got {tori:?}");
    assert!(
        (tori[0].0 - (5.0 - r)).abs() < 1e-12 && (tori[0].1 - r).abs() < 1e-12,
        "torus radii should be ({}, {r}), got {:?}",
        5.0 - r,
        tori[0]
    );
}

#[test]
fn a_section_profile_revolves_instead_of_refusing() {
    // ADR 0057 shipped section lowering; the revolution refusal was stale.
    let profile = Profile::Section(SectionProfile::Trapezium {
        bottom_x: 1.0,
        top_x: 0.6,
        y: 1.5,
        top_offset: 0.2,
    });
    // Trapezium corners start at the origin, so revolve about an axis well
    // clear of it.
    let solid = revolve(&profile, -3.0);
    assert!(
        solid.topology().faces().len() >= 3,
        "a revolved trapezium needs at least three faces"
    );
}

#[test]
fn a_centre_line_profile_revolves_instead_of_refusing() {
    // ADR 0056 shipped centre-line offsetting; the refusal was stale.
    let path = Contour::new(vec![line(Point2::new(2.0, 0.0), Point2::new(2.0, 3.0))]);
    let profile = Profile::CenterLine(axiolid_profile::CenterLineProfile::from_width(path, 0.4));
    let solid = revolve(&profile, 0.0);
    let cylinders = solid
        .surfaces()
        .iter()
        .filter(|s| matches!(s, Surface::Cylinder(_)))
        .count();
    assert_eq!(cylinders, 2, "an offset strip revolves to two walls");
}

#[test]
fn a_section_crossing_the_axis_is_still_refused() {
    // Not every refusal was stale: a section straddling the axis collapses
    // its inner wall, which is a different topology.
    let straddling = contour(vec![
        line(Point2::new(-1.0, 0.0), Point2::new(1.0, 0.0)),
        line(Point2::new(1.0, 0.0), Point2::new(1.0, 1.0)),
        line(Point2::new(1.0, 1.0), Point2::new(-1.0, 1.0)),
        line(Point2::new(-1.0, 1.0), Point2::new(-1.0, 0.0)),
    ]);
    let error = revolve_profile_exact(&straddling, Point3::ZERO, Vec3::Y, TAU, Tolerance::METRE)
        .expect_err("a section crossing the axis must refuse");
    assert!(
        format!("{error:?}").contains("crossing the axis"),
        "got {error:?}"
    );
}

#[test]
fn a_clockwise_arc_sweeps_the_material_side_of_the_tube() {
    // The end angle must come from the arc's own sweep, not from `atan2` of
    // the endpoint. Contour lowering already refuses any segment of half a
    // turn or more (ADR 0053), so the two rules can only disagree by SIGN:
    // a clockwise quarter-arc and a counter-clockwise one share endpoints
    // but bulge opposite ways, and the tube must follow the stated turn.
    let r = 0.5;
    let quarter = core::f64::consts::FRAC_PI_2;

    // A concave quarter-round notch cut into the outer face: the arc runs
    // clockwise (negative sweep) about a centre OUTSIDE the material.
    let profile = contour(vec![
        line(Point2::new(3.0, 0.0), Point2::new(5.0, 0.0)),
        line(Point2::new(5.0, 0.0), Point2::new(5.0, 2.0 - r)),
        // Centre at (5, 2): sweeping from angle -90 (i.e. (5, 2-r)) by -90
        // reaches (5-r, 2) the short way through the material side.
        ProfileSegment {
            curve: Curve2::Circle(Circle2 {
                frame: Frame2 {
                    origin: Point2::new(5.0, 2.0),
                    x: Vec2::X,
                    y: Vec2::Y,
                },
                radius: r,
            }),
            domain: Interval::new(-quarter, -core::f64::consts::PI),
            same_sense: true,
        },
        line(Point2::new(5.0 - r, 2.0), Point2::new(3.0, 2.0)),
        line(Point2::new(3.0, 2.0), Point2::new(3.0, 0.0)),
    ]);
    let solid = revolve(&profile, 0.0);

    // The notch is a torus whose tube centre sits at x = 5, not at 5 - r:
    // a builder that ignored the arc direction would place the centre on
    // the convex side and report a major radius of 4.5.
    let tori: Vec<(f64, f64)> = solid
        .surfaces()
        .iter()
        .filter_map(|s| match s {
            Surface::Torus(t) => Some((t.major_radius, t.minor_radius)),
            _ => None,
        })
        .collect();
    assert_eq!(tori.len(), 1, "one arc sweeps one torus: {tori:?}");
    assert!(
        (tori[0].0 - 5.0).abs() < 1e-12,
        "concave notch centres its tube at x = 5, got {tori:?}"
    );
}

#[test]
fn an_arc_crossing_the_tube_branch_cut_keeps_its_stated_sweep() {
    // `atan2` returns in (-pi, pi], so an arc that passes through the tube
    // angle pi -- the side of the tube FACING the axis -- has an endpoint
    // whose reported angle is 2*pi away from its true end. Inferring the end
    // from the endpoint then sweeps the long way backwards round the tube
    // and builds the complementary region. Arcs that stay clear of the cut
    // cannot see the difference, which is why the fillet fixtures above pass
    // either way.
    let r = 0.5;
    let cx = 4.0;
    let cy = 1.0;
    let third = 3.0 * core::f64::consts::FRAC_PI_4; // 135 degrees
    let fifth = 5.0 * core::f64::consts::FRAC_PI_4; // 225 degrees
    let ax = cx + r * third.cos();
    let ay = cy + r * third.sin();
    let bx = cx + r * fifth.cos();
    let by = cy + r * fifth.sin();

    // Section: a block whose axis-facing face bulges out over the arc. The
    // arc runs from 135 to 225 degrees, passing through 180 -- the point
    // nearest the axis, and exactly the branch cut.
    let profile = contour(vec![
        ProfileSegment {
            curve: Curve2::Circle(Circle2 {
                frame: Frame2 {
                    origin: Point2::new(cx, cy),
                    x: Vec2::X,
                    y: Vec2::Y,
                },
                radius: r,
            }),
            domain: Interval::new(third, fifth),
            same_sense: true,
        },
        line(Point2::new(bx, by), Point2::new(5.0, by)),
        line(Point2::new(5.0, by), Point2::new(5.0, ay)),
        line(Point2::new(5.0, ay), Point2::new(ax, ay)),
    ]);
    let solid = revolve(&profile, 0.0);

    // The tube must be centred on the arc centre and carry the stated minor
    // radius. The audit inside `revolve` is the real check: a long-way-round
    // tube leaves the face boundary disagreeing with its surface.
    let tori: Vec<(f64, f64)> = solid
        .surfaces()
        .iter()
        .filter_map(|s| match s {
            Surface::Torus(t) => Some((t.major_radius, t.minor_radius)),
            _ => None,
        })
        .collect();
    assert_eq!(tori.len(), 1, "one arc sweeps one torus: {tori:?}");
    assert!(
        (tori[0].0 - cx).abs() < 1e-12 && (tori[0].1 - r).abs() < 1e-12,
        "tube must sit on the arc centre: {tori:?}"
    );

    // The seam crosses the tube, so its interval carries the tube sweep. The
    // stated turn is +90 degrees; inferring the end angle from the endpoint
    // instead reports the same POINT but spans -270 degrees, traversing the
    // long way backwards. Only the span distinguishes them.
    let tube_spans: Vec<f64> = solid
        .topology()
        .loops()
        .iter()
        .flat_map(|loop_value| loop_value.edges.iter())
        .filter_map(|edge_use| solid.edge_interval(edge_use.edge))
        .map(|interval| interval.end - interval.start)
        .filter(|span| (span.abs() - TAU).abs() > 1e-9 && span.abs() > 1e-9)
        .collect();
    let quarter = core::f64::consts::FRAC_PI_2;
    assert!(
        tube_spans.iter().any(|span| (span - quarter).abs() < 1e-9),
        "the tube seam must span +90 degrees, got {tube_spans:?}"
    );
    assert!(
        !tube_spans
            .iter()
            .any(|span| (span + 3.0 * quarter).abs() < 1e-9),
        "a -270 degree seam means the end angle came from the endpoint: {tube_spans:?}"
    );
}

#[test]
fn every_revolved_wall_faces_out_of_the_solid() {
    // A loop anticlockwise in (u, v) runs anticlockwise about S_u x S_v, and
    // that winding -- after the face and bound flips -- is the sense the
    // tessellator and edge-use pairing read. The revolution frames were
    // left-handed, so every Forward wall pointed INTO the solid; both audits
    // compare faces with each other and passed it. Read here from the
    // geometry alone: the annulus x in [4, 6] has its outer wall facing away
    // from the axis and its inner wall facing towards it.
    use axiolid_evaluate::partials;
    use axiolid_topology::Orientation;

    let section = contour(vec![
        line(Point2::new(4.0, -1.5), Point2::new(6.0, -1.5)),
        line(Point2::new(6.0, -1.5), Point2::new(6.0, 1.5)),
        line(Point2::new(6.0, 1.5), Point2::new(4.0, 1.5)),
        line(Point2::new(4.0, 1.5), Point2::new(4.0, -1.5)),
    ]);
    let solid = revolve(&section, 0.0);
    let topology = solid.topology();
    let mut walls = 0;
    for face in topology.faces() {
        let surface = &solid.surfaces()[face.surface.expect("surface").index()];
        let Surface::Cylinder(cylinder) = surface else {
            continue;
        };
        walls += 1;
        let bound = &face.bounds[0];
        let wire = &topology.loops()[bound.loop_id.index()];
        // Shoelace over the pcurve end points: the seam loop is straight
        // in (u, v), so its corners state it exactly.
        let mut corners = Vec::new();
        for (index, use_) in wire.edges.iter().enumerate() {
            let Curve2::Line(pcurve) = &solid.curves2()[use_.pcurve.expect("pcurve").index()]
            else {
                panic!("a seam loop is straight in its parameters");
            };
            let span = solid
                .pcurve_interval(bound.loop_id, index)
                .expect("interval");
            corners.push(pcurve.origin + pcurve.direction * span.start);
        }
        let mut twice_area = 0.0;
        for i in 0..corners.len() {
            twice_area += corners[i].perp_dot(corners[(i + 1) % corners.len()]);
        }
        let mut sense = twice_area.signum();
        if face.orientation == Orientation::Reversed {
            sense = -sense;
        }
        if bound.orientation == Orientation::Reversed {
            sense = -sense;
        }
        let (su, sv) = partials(surface, 1.0, 1.0).expect("partials");
        let normal = su.cross(sv) * sense;
        // Away from the axis (world y through x = 0) at the probed point.
        let point = axiolid_evaluate::surface::evaluate(surface, 1.0, 1.0).expect("point");
        let radial = Vec3::new(point.x, 0.0, point.z).normalize();
        let outward = if (cylinder.radius - 6.0).abs() < 1e-12 {
            1.0
        } else {
            -1.0
        };
        assert!(
            normal.dot(radial) * outward > 0.0,
            "wall of radius {} faces into the solid",
            cylinder.radius
        );
    }
    assert_eq!(walls, 2, "an annular tube has two walls");
}