dymokomi/luced-3d
Source of release 0.11.3
0.11.3 / 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 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
# A quintic: six control points (eight for G3, which holds four at each
# end), each end's on along the way it leaves its curve.
let span = math.hypot(sketch.x(finish.point) - sketch.x(start.point), sketch.y(finish.point) - sketch.y(start.point))
let g3 = e.tool.ends_with("G3")
let reaches = [0.15, 0.3, 0.45] if g3 else [0.2, 0.4]
let points = [start.point]
for reach in reaches:
points.append(start.out(sketch, span * reach))
for at in 0..<reaches.length:
points.append(finish.out(sketch, span * reaches[reaches.length - 1 - at]))
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")
## 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)