Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.4

0.11.4 / src / sketch_constrain.luc

## The Sketch editor's constraint and dimension tools on a sketch: which
## constraint a tool makes of its picks (the first the reference), and
## which dimension Sketch Dimension measures. The editor adds what these
## return and settles it (sketch_editor.luc).
from luce_cad.sketch import CadSketch
from luce_std import math
from sketch_geometry import side_of
from sketch_kinds import coincident, on_curve, horizontal, vertical, parallel, perpendicular, tangent, equal, fix, midpoint, concentric, collinear, symmetric, distance, line_distance, angle, radius, diameter, curvature, point_kind, line_kind, circle_kind, arc_kind, spline_kind, conic_kind

## What a constraint tool's picks came to.
pub let more_picks = -1
## The picks do not fit the tool.
pub let no_fit = -2
## Fix/UnFix took a fix away.
pub let unfixed = -3

## Constraint tool `tool` on `picks`: the constraint it added, or
## more_picks, no_fit or unfixed.
pub func constrain_picks(sketch: CadSketch, tool: str, picks: list[int]) -> int!:
    let p = picks
    let kinds: list[int] = []
    let lines: list[int] = []
    let rounds: list[int] = []
    let points: list[int] = []
    for at in p:
        let kind = sketch.kind(at)
        kinds.append(kind)
        if kind == line_kind:
            lines.append(at)
        elif kind == point_kind:
            points.append(at)
        else:
            rounds.append(at)
    if tool == "Horizontal/Vertical":
        if kinds.length >= 1 and kinds[0] == line_kind:
            let a = sketch.part(p[0], 0)
            let b = sketch.part(p[0], 1)
            let level = math.abs(sketch.x(b) - sketch.x(a)) >= math.abs(sketch.y(b) - sketch.y(a))
            return sketch.constrain(horizontal if level else vertical, p[0])
        if points.length == 2:
            let level = math.abs(sketch.x(p[1]) - sketch.x(p[0])) >= math.abs(sketch.y(p[1]) - sketch.y(p[0]))
            return sketch.constrain(horizontal if level else vertical, p[0], p[1])
        return more_picks
    if tool == "Fix/UnFix":
        for index in 0..<sketch.constraint_count():
            if sketch.constraint_kind(index) == fix and sketch.constraint_entity(index, 0) == p[0]:
                sketch.unconstrain(index)
                return unfixed
        return sketch.constrain(fix, p[0])
    if p.length < 2:
        return more_picks
    if tool == "Coincident":
        if kinds[0] == point_kind and kinds[1] == point_kind:
            return sketch.constrain(coincident, p[1], p[0])
        if kinds[0] != point_kind and kinds[1] == point_kind:
            return sketch.constrain(on_curve, p[1], p[0])
        if kinds[0] == point_kind:
            return sketch.constrain(on_curve, p[0], p[1])
    elif tool in ["Parallel", "Perpendicular", "Collinear"] and lines.length == 2:
        return sketch.constrain(parallel if tool == "Parallel" else (perpendicular if tool == "Perpendicular" else collinear), p[0], p[1])
    elif tool == "Equal" and (lines.length == 2 or rounds.length == 2):
        return sketch.constrain(equal, p[0], p[1])
    elif tool == "Concentric" and rounds.length == 2:
        return sketch.constrain(concentric, p[0], p[1])
    elif tool in ["Tangent", "Curvature"] and (kinds[0] in [spline_kind, conic_kind] or kinds[1] in [spline_kind, conic_kind]):
        # A spline's or conic's end against a line, circle or arc.
        let curve = p[0] if kinds[0] in [spline_kind, conic_kind] else p[1]
        let other = p[1] if curve == p[0] else p[0]
        # (A fit point spline's ends follow its points: not yet held.)
        let fitted = sketch.kind(curve) == spline_kind and sketch.fit(curve)
        if sketch.kind(other) in [line_kind, circle_kind, arc_kind] and not fitted and (tool == "Tangent" or sketch.kind(curve) == spline_kind):
            let side = sketch.end_nearest(curve, other)
            let turn = sketch.curvature_turn(curve, side, other) if tool == "Curvature" else 0.0
            return sketch.constrain(tangent if tool == "Tangent" else curvature, curve, other, value = turn, side = side)
    elif tool == "Tangent" and rounds.length >= 1 and kinds[0] != point_kind and kinds[1] != point_kind:
        return sketch.constrain(tangent, p[0], p[1], side = tangent_side(sketch, p[0], p[1]))
    elif tool == "MidPoint" and points.length == 1 and lines.length == 1:
        return sketch.constrain(midpoint, points[0], lines[0])
    elif tool == "Symmetry":
        if p.length < 3:
            return more_picks
        if kinds[0] == point_kind and kinds[1] == point_kind and kinds[2] == line_kind:
            return sketch.constrain(symmetric, p[0], p[1], p[2])
    return no_fit

## Which side `first` keeps of `second` (or the other way), for Tangent:
## a line's side its circle's center is on, or two circles outside (+1)
## or inside (-1) each other.
func tangent_side(sketch: CadSketch, first: int, second: int) -> float:
    var line = first
    var round = second
    if sketch.kind(second) == line_kind:
        line = second
        round = first
    if sketch.kind(line) == line_kind:
        let c = sketch.part(round, 0)
        return side_of(sketch, line, sketch.x(c), sketch.y(c))
    let c1 = sketch.part(first, 0)
    let c2 = sketch.part(second, 0)
    let apart = math.hypot(sketch.x(c2) - sketch.x(c1), sketch.y(c2) - sketch.y(c1))
    return 1.0 if apart >= math.max(sketch.radius(first), sketch.radius(second)) else -1.0

## Sketch Dimension on `picks`, measuring the sketch as it is: a line's
## length, a circle's diameter, an arc's radius; two points' distance, a
## point's from a line, two lines' angle (or distance, when parallel). The
## dimension added, or -1 when the picks are none of those.
pub func measure(sketch: CadSketch, picks: list[int]) -> int!:
    let a = picks[0]
    if picks.length == 1:
        let k = sketch.kind(a)
        if k == line_kind:
            let s = sketch.part(a, 0)
            let e = sketch.part(a, 1)
            return sketch.constrain(distance, s, e, value = math.hypot(sketch.x(e) - sketch.x(s), sketch.y(e) - sketch.y(s)))
        if k == circle_kind:
            return sketch.constrain(diameter, a, value = 2.0 * sketch.radius(a))
        if k == arc_kind:
            return sketch.constrain(radius, a, value = sketch.radius(a))
        return -1
    let b = picks[1]
    let ka = sketch.kind(a)
    let kb = sketch.kind(b)
    if ka == point_kind and kb == point_kind:
        return sketch.constrain(distance, a, b, value = math.hypot(sketch.x(b) - sketch.x(a), sketch.y(b) - sketch.y(a)))
    if ka == point_kind and kb == line_kind:
        return point_line(sketch, a, b)
    if ka == line_kind and kb == point_kind:
        return point_line(sketch, b, a)
    if ka == line_kind and kb == line_kind:
        return two_lines(sketch, a, b)
    return -1

## Point `point`'s distance from line `line`, on its side.
func point_line(sketch: CadSketch, point: int, line: int) -> int!:
    let a = sketch.part(line, 0)
    let b = sketch.part(line, 1)
    let dx = sketch.x(b) - sketch.x(a)
    let dy = sketch.y(b) - sketch.y(a)
    let length = math.hypot(dx, dy)
    if length <= 0.0:
        return -1
    let signed = (dx * (sketch.y(point) - sketch.y(a)) - dy * (sketch.x(point) - sketch.x(a))) / length
    return sketch.constrain(line_distance, point, line, value = math.abs(signed), side = 1.0 if signed >= 0.0 else -1.0)

## Two lines' angle, or their distance when parallel.
func two_lines(sketch: CadSketch, first: int, second: int) -> int!:
    let ax = sketch.x(sketch.part(first, 1)) - sketch.x(sketch.part(first, 0))
    let ay = sketch.y(sketch.part(first, 1)) - sketch.y(sketch.part(first, 0))
    let bx = sketch.x(sketch.part(second, 1)) - sketch.x(sketch.part(second, 0))
    let by = sketch.y(sketch.part(second, 1)) - sketch.y(sketch.part(second, 0))
    let cross = ax * by - ay * bx
    let dot = ax * bx + ay * by
    if math.abs(cross) <= 1e-9 * math.hypot(ax, ay) * math.hypot(bx, by):
        return point_line(sketch, sketch.part(second, 0), first)
    return sketch.constrain(angle, first, second, value = math.atan2(cross, dot))