springrep 1.0.0

An exact boundary-representation geometry kernel, in safe Rust
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
//! Hidden-line drawings: a solid's edges and the outlines of its curved faces seen along a
//! direction, each cut into the stretches the viewer sees and those the solid hides.
//!
//! Visibility along a curve changes only where, seen from the viewer, it passes behind another
//! edge or outline, or meets one. So every stroke (an edge between faces that meet at an angle,
//! or an outline where the line of sight grazes a curved face, trimmed to the face) is cut where
//! its projection crosses another's, found by Newton's method from the crossing chords, and where
//! another stroke ends on it. Each piece between cuts is then seen or hidden throughout, and the
//! exact ray caster decides which at its middle, far from where the line of sight grazes anything.

use std::f64::consts::TAU;

use crate::{
    error::{Error, Result},
    geom::{Circle, Curve, Line, Surface},
    math::{Frame, Point3, Vec2, Vec3},
    measure::{bbox, in_face, uv_polygons},
    rays::Rays,
    topo::{Brep, FaceId},
};

/// A drawing's polylines, in space: the stretches seen and the stretches hidden.
#[derive(Clone, Debug, Default)]
pub struct Drawing {
    pub visible: Vec<Vec<Point3>>,
    pub hidden: Vec<Vec<Point3>>,
}

/// One curve of the drawing over `[t0, t1]`, and the faces it lies on: an edge's two, or the one
/// an outline grazes.
struct Stroke {
    curve: Curve,
    t0: f64,
    t1: f64,
    faces: Vec<FaceId>,
}

/// The drawing of `b` looking along `direction`, every chord within `tolerance` of its curve.
pub fn hidden_lines(b: &Brep, direction: Vec3, tolerance: f64) -> Result<Drawing> {
    let d = direction
        .unit()
        .ok_or_else(|| Error::InvalidInput("a view needs a direction".into()))?;
    let mut out = Drawing::default();
    if b.solid_ids().next().is_none() {
        return Ok(out);
    }
    let scale = bbox(b).diagonal().max(1.0);
    let view = Frame::new(Point3::ZERO, d, d.any_perpendicular())?;
    let flat = |p: Point3| {
        let l = view.to_local(p);
        Vec2::new(l.x, l.y)
    };
    let strokes = strokes(b, d, tolerance);

    // Each stroke's samples, close enough for the tolerance, and their projections.
    let samples: Vec<Vec<f64>> = strokes
        .iter()
        .map(|s| {
            let mut ts = Vec::new();
            sample(&|t| s.curve.eval(t), s.t0, s.t1, tolerance, 0, &mut ts);
            ts.push(s.t1);
            ts
        })
        .collect();
    let flats: Vec<Vec<Vec2>> = strokes
        .iter()
        .zip(&samples)
        .map(|(s, ts)| ts.iter().map(|&t| flat(s.curve.eval(t))).collect())
        .collect();

    // Where each stroke is cut: crossings with the others' projections, and their ends on it.
    let mut cuts: Vec<Vec<f64>> = vec![Vec::new(); strokes.len()];
    let near = 1e-9 * scale;
    for i in 0..strokes.len() {
        for j in 0..strokes.len() {
            if i == j {
                continue;
            }
            for (k, w) in flats[i].windows(2).enumerate() {
                for (l, x) in flats[j].windows(2).enumerate() {
                    if let Some((sa, _)) = chords_cross(w[0], w[1], x[0], x[1]) {
                        let ta = samples[i][k] + (samples[i][k + 1] - samples[i][k]) * sa;
                        let tb = samples[j][l] + (samples[j][l + 1] - samples[j][l]) * 0.5;
                        if let Some(t) = pin_crossing(&strokes[i], &strokes[j], ta, tb, &flat) {
                            cuts[i].push(t);
                        }
                    }
                }
            }
            let sj = &strokes[j];
            for end in [sj.curve.eval(sj.t0), sj.curve.eval(sj.t1)] {
                if let Some(t) = foot(&strokes[i], &samples[i], end, &flat)
                    && flat(strokes[i].curve.eval(t)).dist(flat(end)) <= near
                {
                    cuts[i].push(t);
                }
            }
        }
    }

    let rays = Rays::new(b)?;
    let eps = 1e-9 * scale;
    // Hidden where the ray toward the viewer leaves the solid. It starts a hair off the faces the
    // point lies on, out of the solid, so it runs beside them rather than along them.
    let hidden = |s: &Stroke, p: Point3| {
        let off = s
            .faces
            .iter()
            .map(|&f| outward(b, f, p))
            .fold(Vec3::ZERO, |a, n| a + n)
            .unit()
            .unwrap_or(Vec3::ZERO);
        rays.cast(p + (off - d) * (100.0 * eps), -d, (0.0, 1e3 * scale))
            .is_ok_and(|hits| hits.iter().any(|h| h.normal.dot(-d) > 1e-9))
    };

    for (i, s) in strokes.iter().enumerate() {
        let span = s.t1 - s.t0;
        let mut ts: Vec<f64> = cuts[i]
            .iter()
            .copied()
            .filter(|&t| (t - s.t0) * span.signum() > 1e-9 * span.abs())
            .filter(|&t| (s.t1 - t) * span.signum() > 1e-9 * span.abs())
            .collect();
        ts.push(s.t0);
        ts.push(s.t1);
        ts.sort_by(|x, y| ((x - s.t0) / span).total_cmp(&((y - s.t0) / span)));
        ts.dedup_by(|x, y| (*x - *y).abs() <= 1e-12 * span.abs().max(1.0));
        // Consecutive pieces of one visibility join.
        let mut runs: Vec<(bool, f64, f64)> = Vec::new();
        for w in ts.windows(2) {
            let mid = s.curve.eval(0.5 * (w[0] + w[1]));
            let h = hidden(s, mid);
            match runs.last_mut() {
                Some(last) if last.0 == h => last.2 = w[1],
                _ => runs.push((h, w[0], w[1])),
            }
        }
        for (h, a, z) in runs {
            let mut pts: Vec<f64> = samples[i]
                .iter()
                .copied()
                .filter(|&t| ((t - a) / span) > 1e-12 && ((z - t) / span) > 1e-12)
                .collect();
            pts.insert(0, a);
            pts.push(z);
            let line: Vec<Point3> = pts.iter().map(|&t| s.curve.eval(t)).collect();
            if h {
                out.hidden.push(line);
            } else {
                out.visible.push(line);
            }
        }
    }
    Ok(out)
}

/// The drawing's strokes: every edge but a seam or one between faces that meet smoothly, and each
/// outline trimmed to the stretches of it that lie on its face.
fn strokes(b: &Brep, d: Vec3, tolerance: f64) -> Vec<Stroke> {
    let mut out = Vec::new();
    for e in b.edge_ids() {
        let edge = b.edge(e);
        let Some(c) = &edge.curve else { continue };
        let faces: Vec<FaceId> = b
            .coedge_ids()
            .filter(|&co| b.coedge(co).edge == e)
            .map(|co| b.coedge_face(co))
            .collect();
        if faces.len() == 2 && faces[0] == faces[1] {
            continue;
        }
        let (t0, t1) = edge.range;
        let mid = c.eval(0.5 * (t0 + t1));
        if faces.len() == 2 && smooth(b, faces[0], faces[1], mid) {
            continue;
        }
        out.push(Stroke {
            curve: c.clone(),
            t0,
            t1,
            faces,
        });
    }
    for f in b.face_ids() {
        let s = &b.face(f).surface;
        let polys = uv_polygons(b, f);
        let on = |p: Point3| in_face(s, &polys, s.project(p)).is_some();
        for (curve, t0, t1) in outlines(b, f, d) {
            let mut ts = Vec::new();
            sample(&|t| curve.eval(t), t0, t1, tolerance, 0, &mut ts);
            ts.push(t1);
            let inside: Vec<bool> = ts.iter().map(|&t| on(curve.eval(t))).collect();
            // Each change of membership pinned by bisection.
            let pin = |mut a: f64, mut z: f64, la: bool| {
                for _ in 0..80 {
                    let m = 0.5 * (a + z);
                    if on(curve.eval(m)) == la {
                        a = m;
                    } else {
                        z = m;
                    }
                }
                0.5 * (a + z)
            };
            let mut start = inside[0].then_some(ts[0]);
            for k in 0..ts.len() - 1 {
                if inside[k] != inside[k + 1] {
                    let t = pin(ts[k], ts[k + 1], inside[k]);
                    match start.take() {
                        Some(a) => out.push(Stroke {
                            curve: curve.clone(),
                            t0: a,
                            t1: t,
                            faces: vec![f],
                        }),
                        None => start = Some(t),
                    }
                }
            }
            if let Some(a) = start {
                out.push(Stroke {
                    curve: curve.clone(),
                    t0: a,
                    t1: ts[ts.len() - 1],
                    faces: vec![f],
                });
            }
        }
    }
    out.retain(|s| (s.t1 - s.t0).abs() > 0.0);
    out
}

/// Face `f`'s outward unit normal at its point nearest `p`.
fn outward(b: &Brep, f: FaceId, p: Point3) -> Vec3 {
    let s = &b.face(f).surface;
    let uv = s.project(p);
    let n = s.normal(uv.x, uv.y);
    if b.face(f).reversed { -n } else { n }
}

/// Whether faces `f` and `g` meet smoothly at `p`: their outward normals agree there.
fn smooth(b: &Brep, f: FaceId, g: FaceId, p: Point3) -> bool {
    let (n, m) = (outward(b, f, p), outward(b, g, p));
    n.cross(m).norm() <= 1e-7 && n.dot(m) > 0.0
}

/// The curves on face `f`'s surface where the line of sight along `d` grazes it, each over a
/// parameter range that covers the face: generators of a cylinder or cone, the great circle of a
/// ball. Other surfaces have none drawn.
fn outlines(b: &Brep, f: FaceId, d: Vec3) -> Vec<(Curve, f64, f64)> {
    let s = &b.face(f).surface;
    let polys = uv_polygons(b, f);
    let (vlo, vhi) = polys
        .iter()
        .flatten()
        .fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), q| {
            (lo.min(q.y), hi.max(q.y))
        });
    let mut out = Vec::new();
    match s {
        Surface::Cylinder(_) | Surface::Cone(_) => {
            // The normal is the same all along a generator: the grazing ones are the roots in u
            // of n(u)·d, which 720 samples bracket and bisection pins.
            let v = 0.5 * (vlo + vhi);
            let g = |u: f64| s.normal(u, v).dot(d);
            let n = 720;
            for i in 0..n {
                let (mut a, mut z) = (TAU * i as f64 / n as f64, TAU * (i + 1) as f64 / n as f64);
                if g(a) == 0.0 || g(a).signum() == g(z).signum() {
                    continue;
                }
                for _ in 0..80 {
                    let m = 0.5 * (a + z);
                    if g(m).signum() == g(a).signum() {
                        a = m;
                    } else {
                        z = m;
                    }
                }
                let u = 0.5 * (a + z);
                let p = s.eval(u, vlo);
                if let Some((line, len)) = Line::through(p, s.eval(u, vhi)) {
                    out.push((Curve::Line(line), 0.0, len));
                }
            }
        }
        Surface::Sphere(sp) => {
            if let Ok(frame) = Frame::new(sp.frame.origin, d, d.any_perpendicular()) {
                let circle = Circle {
                    frame,
                    radius: sp.radius,
                };
                out.push((Curve::Circle(circle), 0.0, TAU));
            }
        }
        _ => {}
    }
    out
}

/// Where chords `ab` and `cd` cross, as fractions along each, if they do.
fn chords_cross(a: Vec2, b: Vec2, c: Vec2, d: Vec2) -> Option<(f64, f64)> {
    let (r, s) = (b - a, d - c);
    let den = r.cross(s);
    if den.abs() <= 1e-300 {
        return None;
    }
    let q = c - a;
    let (t, u) = (q.cross(s) / den, q.cross(r) / den);
    ((0.0..=1.0).contains(&t) && (0.0..=1.0).contains(&u)).then_some((t, u))
}

/// The parameter on `a` where its projection crosses `b`'s, by Newton's method on the two
/// parameters from `(ta, tb)`; `None` where it does not settle within both strokes.
fn pin_crossing(
    a: &Stroke,
    b: &Stroke,
    mut ta: f64,
    mut tb: f64,
    flat: &dyn Fn(Point3) -> Vec2,
) -> Option<f64> {
    let flat_d = |v: Vec3| flat(v + Point3::ZERO) - flat(Point3::ZERO);
    for _ in 0..50 {
        let (pa, da) = a.curve.d1(ta);
        let (pb, db) = b.curve.d1(tb);
        let r = flat(pa) - flat(pb);
        let (ja, jb) = (flat_d(da), -flat_d(db));
        let det = ja.cross(jb);
        if det.abs() <= 1e-300 {
            return None;
        }
        // Solve ja·x + jb·y = −r.
        let (x, y) = ((-r).cross(jb) / det, ja.cross(-r) / det);
        ta += x;
        tb += y;
        if x.abs() <= 1e-15 * (1.0 + ta.abs()) && y.abs() <= 1e-15 * (1.0 + tb.abs()) {
            break;
        }
    }
    let within = |s: &Stroke, t: f64| {
        let (lo, hi) = (s.t0.min(s.t1), s.t0.max(s.t1));
        t >= lo - 1e-12 && t <= hi + 1e-12
    };
    let gap = flat(a.curve.eval(ta)).dist(flat(b.curve.eval(tb)));
    (within(a, ta) && within(b, tb) && gap <= 1e-9 * (1.0 + flat(a.curve.eval(ta)).norm()))
        .then_some(ta)
}

/// The parameter of the point of stroke `s` nearest `p` seen along the view: the nearest sample,
/// refined by Newton's method on the projected distance.
fn foot(s: &Stroke, ts: &[f64], p: Point3, flat: &dyn Fn(Point3) -> Vec2) -> Option<f64> {
    let q = flat(p);
    let mut t = *ts.iter().min_by(|&&x, &&y| {
        flat(s.curve.eval(x))
            .dist(q)
            .total_cmp(&flat(s.curve.eval(y)).dist(q))
    })?;
    let flat_d = |v: Vec3| flat(v + Point3::ZERO) - flat(Point3::ZERO);
    let (lo, hi) = (s.t0.min(s.t1), s.t0.max(s.t1));
    for _ in 0..30 {
        let (c, d1) = s.curve.d1(t);
        let h = 1e-6 * (1.0 + t.abs());
        let d2 = (flat_d(s.curve.d1(t + h).1) - flat_d(s.curve.d1(t - h).1)) * (0.5 / h);
        let (r, g) = (flat(c) - q, flat_d(d1));
        let df = g.dot(g) + r.dot(d2);
        if df <= 0.0 {
            break;
        }
        let step = r.dot(g) / df;
        t = (t - step).clamp(lo, hi);
        if step.abs() <= 1e-15 * (1.0 + t.abs()) {
            break;
        }
    }
    Some(t)
}

/// Parameters from `t0` toward `t1`, `t1` left out, close enough that each chord's middle and
/// quarter points lie within half the tolerance of it; at least sixteen.
fn sample(
    at: &dyn Fn(f64) -> Point3,
    t0: f64,
    t1: f64,
    tolerance: f64,
    depth: u32,
    out: &mut Vec<f64>,
) {
    let (a, b) = (at(t0), at(t1));
    let off = |t: f64| {
        let p = at(t);
        match (b - a).unit() {
            Some(u) => (p - a).cross(u).norm(),
            None => p.dist(a),
        }
    };
    let worst = [0.25, 0.5, 0.75]
        .iter()
        .map(|&f| off(t0 + (t1 - t0) * f))
        .fold(0.0, f64::max);
    if depth >= 4 && (worst <= 0.5 * tolerance || depth >= 24) {
        out.push(t0);
        return;
    }
    let m = 0.5 * (t0 + t1);
    sample(at, t0, m, tolerance, depth + 1, out);
    sample(at, m, t1, tolerance, depth + 1, out);
}