Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.7

0.11.7 / src / sketch_freeform.luc

## The Sketch editor's free-form curves (luce-cad's splines, ellipses and
## conics), click by click as Fusion's:
##
##   Fit Point Spline       the points it passes through, Enter (or the last
##                          point again) to finish: degree 5, less with
##                          fewer points
##   Control Point Spline   its control points, the same way
##   Ellipse                its center, its major axis' end, then how wide
##   Conic Curve            its start and end, then where its end tangents
##                          meet (rho 0.5, a parabola)
##   Text                   where its baseline starts, then how tall its
##                          capitals are; then what it says
##   Blend Curve (MODIFY)   a curve's end, then another's: a quintic
##                          control point spline between them, G1, G2 or G3
##                          to each line or arc (Tangent, Curvature, Smooth)
from luce_cad.sketch import CadSketch
from luce_cad.curves import CadCurveTools
from luce_cad.nurbs import CadSpline
from luce_3d.graphics import Vector3
from overlay_lines import OverlayLines
from sketch_editor import SketchEditor
from sketch_kinds import point_kind, line_kind, circle_kind, arc_kind, spline_kind, ellipse_kind, conic_kind, curvature, smooth, tangent
import sketch_tools
from luce_std import math
from sketch_geometry import off_line

pub func freeform_tools() -> list[str]:
    return ["Fit Point Spline", "Control Point Spline", "Ellipse", "Conic Curve", "Text"]

## How tall a text is when its height click is on its baseline.
let text_height = 5.0

## Free-form tool `e.tool`'s clicks are in: make it.
pub func finish_freeform(e: SketchEditor) -> unit!:
    let sketch = e.sketch else return
    let c = e.clicks
    if sketch_tools.open_ended(e.tool):
        let points: list[int] = []
        for at in 0..<c.length:
            let point = e.made_point(at)
            if points.length == 0 or points[-1] != point:
                points.append(point)
        if points.length >= 2:
            _ = sketch.add_spline(points, e.tool == "Fit Point Spline")
    elif e.tool == "Ellipse":
        let minor = off_line(c[0], c[1], c[2])
        let major = math.hypot(c[1][0] - c[0][0], c[1][1] - c[0][1])
        if minor > 0.0 and major > 0.0:
            _ = sketch.add_ellipse(e.made_point(0), e.made_point(1), minor)
    elif e.tool == "Conic Curve":
        _ = sketch.add_conic(e.made_point(0), e.made_point(1), e.made_point(2), 0.5)
    elif e.tool == "Text":
        # Where it goes and how tall: then what it says (on_text asks).
        let tall = math.abs(c[1][1] - c[0][1])
        e.text_at = [c[0][0], c[0][1], tall if tall > 1e-6 else text_height, float(e.click_snaps[0])]
        if let ask = e.on_text:
            ask()

## What it would make with the cursor where it is.
pub func preview_freeform(e: SketchEditor, lines: OverlayLines) -> unit!:
    let c = e.clicks
    let f = e.frame
    let all = c + [e.cursor]
    if sketch_tools.open_ended(e.tool):
        # The points so far, joined (for a control point spline its net).
        for at in 1..<all.length:
            lines.add_segment(f.at(all[at - 1][0], all[at - 1][1]), f.at(all[at][0], all[at][1]))
    elif e.tool == "Ellipse":
        if c.length == 1:
            lines.add_segment(f.at(c[0][0], c[0][1]), f.at(e.cursor[0], e.cursor[1]))
            return
        let a = math.hypot(c[1][0] - c[0][0], c[1][1] - c[0][1])
        let b = off_line(c[0], c[1], e.cursor)
        if a <= 0.0:
            return
        let ux = (c[1][0] - c[0][0]) / a
        let uy = (c[1][1] - c[0][1]) / a
        var previous = f.at(c[0][0] + a * ux, c[0][1] + a * uy)
        for step in 1..<97:
            let t = 2.0 * math.pi * float(step) / 96.0
            let x = a * math.cos(t)
            let y = b * math.sin(t)
            let next = f.at(c[0][0] + x * ux - y * uy, c[0][1] + x * uy + y * ux)
            lines.add_segment(previous, next)
            previous = next
    elif e.tool == "Text":
        # Its baseline and how tall its capitals stand.
        let top = e.cursor[1]
        lines.add_segment(f.at(c[0][0], c[0][1]), f.at(c[0][0] + 20.0, c[0][1]))
        lines.add_segment(f.at(c[0][0], c[0][1]), f.at(c[0][0], top))
    elif e.tool == "Conic Curve":
        if c.length == 1:
            lines.add_segment(f.at(c[0][0], c[0][1]), f.at(e.cursor[0], e.cursor[1]))
            return
        # A parabola from start to end with the cursor as its apex.
        let apex = e.cursor
        var previous = f.at(c[0][0], c[0][1])
        for step in 1..<49:
            let t = float(step) / 48.0
            let s = 1.0 - t
            let next = f.at(s * s * c[0][0] + 2.0 * s * t * apex[0] + t * t * c[1][0], s * s * c[0][1] + 2.0 * s * t * apex[1] + t * t * c[1][1])
            lines.add_segment(previous, next)
            previous = next

## Blend Curve's click on `curve` at (`x`, `y`): the first picks that
## curve's nearer end, the second makes the blend to the other's.
pub func blend_click(e: SketchEditor, curve: int, x: float, y: float) -> unit!:
    let sketch = e.sketch else return
    if curve < 0 or sketch.kind(curve) in [point_kind, circle_kind, ellipse_kind]:
        return
    e.picks.append(curve)
    e.clicks.append([x, y])
    e.revision += 1
    if e.picks.length < 2:
        e.say("Blend Curve: click near the end of the curve to blend to")
        return
    let start = curve_end(sketch, e.picks[0], e.clicks[0])
    let finish = curve_end(sketch, e.picks[1], e.clicks[1])
    let picked = e.picks.copy()
    e.picks = []
    e.clicks = []
    if start.point == finish.point:
        e.say("Blend Curve joins the ends of two curves")
        return
    # The 3D kernel's blend (luce-cad's Blend Curve, in the sketch's plane):
    # a quintic of six control points (eight for G3, which holds four at
    # each end), each end's taken from its curve's jet there; the solver's
    # constraints below then keep them so as the sketch changes.
    let g3 = e.tool.ends_with("G3")
    let order = 1 if e.tool.ends_with("G1") else (3 if g3 else 2)
    let made = CadCurveTools.blend(going_back(sketch, picked[0], start), 0.0, -1, order, going_back(sketch, picked[1], finish), 0.0, -1, order, 5, [])
    let points = [start.point]
    for at in 1..<made.count() - 1:
        let q = made.point(at)
        points.append(sketch.add_point(q.x, q.y))
    points.append(finish.point)
    let spline = sketch.add_spline(points, false)
    # Along (G1), as curved as (G2), or as curved and changing as (G3) a
    # line or an arc where it leaves it.
    let continuity = tangent if e.tool.ends_with("G1") else (smooth if g3 else curvature)
    for (at, side) in [(0, 1.0), (1, -1.0)]:
        let other = picked[at]
        let kind = sketch.kind(other)
        if kind == line_kind or kind == arc_kind:
            _ = sketch.constrain(continuity, spline, other, value = sketch.curvature_turn(spline, side, other), side = side)
    if not sketch.solve():
        e.say("The blend cannot be as curved as both ends at once")
    e.commit("Blend Curve")

## The curve `curve` ends in, run back from its end `end` into itself (a
## line along it, an arc a quarter turn round its center), as the 3D
## kernel's blend end takes it (leaving its low end backwards).
func going_back(sketch: CadSketch, curve: int, end: CurveEnd) -> CadSpline!:
    let p = Vector3(sketch.x(end.point), sketch.y(end.point), 0.0)
    let back = Vector3(-end.dx, -end.dy, 0.0)
    if sketch.kind(curve) == arc_kind:
        let c = sketch.part(curve, 0)
        let inward = Vector3(sketch.x(c), sketch.y(c), 0.0).subtract(p)
        let r = inward.length()
        let corner = p.add(back.multiply_scalar(r))
        return CadSpline.make([p, corner, corner.add(inward)], [1.0, math.sqrt(0.5), 1.0], 2, [0.0, 0.0, 0.0, 1.0, 1.0, 1.0])
    return CadCurveTools.by_points([p, p.add(back)], 1)

## A curve's end: its point and the direction leaving the curve there.
pub class CurveEnd:
    pub let point: int
    pub let dx: float
    pub let dy: float

    pub func init(self, point: int, dx: float, dy: float):
        self.point = point
        self.dx = dx
        self.dy = dy

    ## A new point `reach` out from the end along that direction.
    pub func out(self, sketch: CadSketch, reach: float) -> int!:
        return sketch.add_point(sketch.x(self.point) + self.dx * reach, sketch.y(self.point) + self.dy * reach)

## The end of line, arc, spline or conic `curve` nearer `click`.
pub func curve_end(sketch: CadSketch, curve: int, click: list[float]) -> CurveEnd!:
    let kind = sketch.kind(curve)
    let count = sketch.part_count(curve)
    # Each end, and the point its direction is taken from.
    var first = sketch.part(curve, 0)
    var last = sketch.part(curve, 1)
    var before_first = sketch.part(curve, 1)
    var before_last = sketch.part(curve, 0)
    if kind == arc_kind:
        first = sketch.part(curve, 1)
        last = sketch.part(curve, 2)
    elif kind == spline_kind:
        last = sketch.part(curve, count - 1)
        before_last = sketch.part(curve, count - 2)
    elif kind == conic_kind:
        before_first = sketch.part(curve, 2)
        before_last = sketch.part(curve, 2)
    let at_start = math.hypot(click[0] - sketch.x(first), click[1] - sketch.y(first)) <= math.hypot(click[0] - sketch.x(last), click[1] - sketch.y(last))
    let point = first if at_start else last
    var dx = 0.0
    var dy = 0.0
    if kind == arc_kind:
        # Leaving an arc: on along its turn past its end, back against it
        # past its start.
        let c = sketch.part(curve, 0)
        let rx = sketch.x(point) - sketch.x(c)
        let ry = sketch.y(point) - sketch.y(c)
        dx = ry if at_start else -ry
        dy = -rx if at_start else rx
    else:
        let back = before_first if at_start else before_last
        dx = sketch.x(point) - sketch.x(back)
        dy = sketch.y(point) - sketch.y(back)
    let length = math.max(math.hypot(dx, dy), 1e-300)
    return CurveEnd(point, dx / length, dy / length)