Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / sketch / snapping.lucb

## Snapping in a sketch: where the pointer lands among what the sketch
## already has, for the editor's snapping layer (luced-3d snapping.luc). In
## order: a point (an end, a center, any sketch point), the crossing of two
## curves near it, a line's or arc's middle, a place on a curve. Each kind
## can be left out, and an entity skipped (the point a drag moves, a
## line's start while its end is placed), and with a point the curves
## drawn on it (a dragged corner does not snap to its own sides), and the
## points held on it by coincident constraints and theirs. Crossings take
## lines, circles and arcs with any curve; two splines, ellipses or conics
## have none.

## The kinds, as bits of a mask and as a result.
pub let snap_point: i64 = 1

pub let snap_middle: i64 = 2

pub let snap_crossing: i64 = 4

pub let snap_curve: i64 = 8

## Where a snap landed: its kind (0 none), place and entity (a crossing's
## two curves: `entity` and `other`; a middle's line or arc).
pub struct SnapHit:
    pub var kind: i64
    pub var x: f64
    pub var y: f64
    pub var entity: i64
    pub var other: i64

## The snap for (`x`, `y`) within `reach` among the kinds in `mask`, not
## entity `skip`.
pub func snap_in(sketch: const Sketch*, x: f64, y: f64, reach: f64, mask: i64, skip: i64) -> SnapHit!:
    var best = SnapHit(0, x, y, -1, -1)
    # Points first: the closest within reach.
    if mask & snap_point != 0:
        var closest = reach
        for at in 0..<sketch.entities.count:
            let e = sketch.entities.items[at]
            if e.kind != EntityKind.point or e.origin != 0 or (i64)at == skip or joined(sketch, (i64)at, skip): continue
            let d = math.hypot(sketch.x((i64)at) - x, sketch.y((i64)at) - y)
            if d <= closest:
                closest = d
                best = SnapHit(snap_point, sketch.x((i64)at), sketch.y((i64)at), (i64)at, -1)
        if best.kind != 0: return best
    # Curves near the pointer (within twice the reach): their crossings,
    # middles and closest places.
    var near = Growing[i64]()
    defer near.close()
    var drawn_on = Growing[i64]()
    defer drawn_on.close()
    for at in 0..<sketch.entities.count:
        let e = sketch.entities.items[at]
        if e.kind == EntityKind.point or e.kind == EntityKind.text or e.origin != 0 or (i64)at == skip: continue
        if skip >= 0 and sketch.entity(skip).kind == EntityKind.point:
            try sketch.points_of(e, &drawn_on)
            var on_skip = false
            for p in drawn_on.view(): on_skip = on_skip or p == skip or joined(sketch, p, skip)
            if on_skip: continue
        if distance_to(sketch, e, (i64)at, x, y) <= 2.0 * reach: try near.push((i64)at)
    if mask & snap_crossing != 0:
        var closest = reach
        var found = Growing[Crossing]()
        defer found.close()
        for i in 0..<near.count:
            for j in i + 1..<near.count:
                found.clear()
                # Along a line, circle or arc (meet's first curve).
                let (a, b) = (near.items[j], near.items[i]) if is_sampled(sketch.entity(near.items[i]).kind) else (near.items[i], near.items[j])
                try meet(sketch, a, b, false, &found)
                for c in found.view():
                    let d = math.hypot(c.x - x, c.y - y)
                    if d <= closest:
                        closest = d
                        best = SnapHit(snap_crossing, c.x, c.y, near.items[i], near.items[j])
        if best.kind != 0: return best
    if mask & snap_middle != 0:
        var closest = reach
        for c in near.view():
            let e = sketch.entity(c)
            if e.kind != EntityKind.line and e.kind != EntityKind.arc: continue
            let (mx, my) = at_place(sketch, c, 0.5)
            let d = math.hypot(mx - x, my - y)
            if d <= closest:
                closest = d
                best = SnapHit(snap_middle, mx, my, c, -1)
        if best.kind != 0: return best
    if mask & snap_curve != 0:
        var closest = reach
        for c in near.view():
            let e = sketch.entity(c)
            let d = distance_to(sketch, e, c, x, y)
            if d <= closest:
                closest = d
                let (on_x, on_y) = nearest_on(sketch, c, x, y)
                best = SnapHit(snap_curve, on_x, on_y, c, -1)
    return best

## Whether points `a` and `b` are held together by a coincident constraint.
func joined(sketch: const Sketch*, a: i64, b: i64) -> bool:
    if a < 0 or b < 0: return false
    for k in sketch.constraints.view():
        if k.kind == ConstraintKind.coincident and ((k.first == a and k.second == b) or (k.first == b and k.second == a)): return true
    return false