Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.4

0.9.4 / src / sketch / removal.lucb

## Removing from a sketch: an entity with what goes with it (a point's
## curves, a curve's points nothing else uses, the constraints and links
## on anything removed), a constraint, and the renumbering after.

## Remove entity `at`: a point with what is drawn on it, a curve with
## its points that nothing else uses; constraints on anything removed go
## too. Later entities and constraints are renumbered.
pub func remove_entity(sketch: Sketch*, at: i64) -> !:
    if at < 0 or at >= (i64)sketch.entities.count: error(invalid, "the sketch has no such entity")
    var gone = Growing[bool]()
    defer gone.close()
    try gone.fill(sketch.entities.count, false)
    gone.items[(usize)at] = true
    var points = Growing[i64]()
    defer points.close()
    # A removed point takes the curves on it.
    for index in 0..<sketch.entities.count:
        let e = sketch.entities.items[index]
        if e.kind == EntityKind.point: continue
        try sketch.points_of(e, &points)
        for p in points.view():
            if gone.items[(usize)p]: gone.items[index] = true
    # A removed curve's points go unless a kept curve uses them.
    for index in 0..<sketch.entities.count:
        let e = sketch.entities.items[index]
        if not gone.items[index] or e.kind == EntityKind.point: continue
        try sketch.points_of(e, &points)
        for p in points.view():
            if not (try curve_on_point(sketch, p, gone.view())): gone.items[(usize)p] = true
    try compact(sketch, gone.view())

## Remove constraint `at` (an Offset relation with all its pairs).
pub func remove_constraint(sketch: Sketch*, at: i64) -> !:
    if at < 0 or at >= (i64)sketch.constraints.count: error(invalid, "the sketch has no such constraint")
    let k = sketch.constraints.items[(usize)at]
    for index in (usize)at..<sketch.constraints.count - 1:
        sketch.constraints.items[index] = sketch.constraints.items[index + 1]
    sketch.constraints.count -= 1
    if k.kind == ConstraintKind.offset: drop_pairs(sketch)

## Drop the Offset pairs whose relation is gone (no offset constraint has
## their group as its copy).
func drop_pairs(sketch: Sketch*):
    var kept: usize = 0
    for at in 0..<sketch.offsets.count:
        let pair = sketch.offsets.items[at]
        var kept_by = false
        for k in sketch.constraints.view():
            if k.kind == ConstraintKind.offset and k.second == pair.group: kept_by = true
        if not kept_by: continue
        sketch.offsets.items[kept] = pair
        kept += 1
    sketch.offsets.count = kept

## Whether a curve other than `except` is drawn on point `point`.
pub func point_used(sketch: const Sketch*, point: i64, except: i64 = -1) -> bool!:
    var none_gone: bool[1] = [false]
    return try curve_on_point(sketch, point, none_gone[..<0], except)

## Whether a curve not in `gone` (nor `except`) is drawn on point `point`.
func curve_on_point(sketch: const Sketch*, point: i64, gone: const bool[], except: i64 = -1) -> bool!:
    var points = Growing[i64]()
    defer points.close()
    for index in 0..<sketch.entities.count:
        let e = sketch.entities.items[index]
        if (i64)index == except or (index < gone.length and gone[index]) or e.kind == EntityKind.point: continue
        try sketch.points_of(e, &points)
        for p in points.view():
            if p == point: return true
    return false

## Keep the entities not in `gone` (with their parameters and point
## lists) and the constraints on them, renumbered.
func compact(sketch: Sketch*, gone: const bool[]) -> !:
    var renumbered = Growing[i64]()
    defer renumbered.close()
    var params = Growing[f64]()
    errdefer params.close()
    var lists = Growing[i64]()
    errdefer lists.close()
    var kept = Growing[SketchEntity]()
    errdefer kept.close()
    for index in 0..<sketch.entities.count:
        if gone[index]:
            try renumbered.push(-1)
            continue
        try renumbered.push((i64)kept.count)
        var e = sketch.entities.items[index]
        if e.kind == EntityKind.point:
            let x = (i64)params.count
            try params.push(sketch.params.items[(usize)e.first])
            try params.push(sketch.params.items[(usize)e.first + 1])
            e.first = x
        elif e.kind == EntityKind.spline:
            let start = (i64)lists.count
            for at in 0..<e.second: try lists.push(renumbered.items[(usize)sketch.spline_point(e, at)])
            e.first = start
        else:
            e.first = renumbered.items[(usize)e.first]
            if e.kind == EntityKind.line:
                e.second = renumbered.items[(usize)e.second]
            else:
                let r = (i64)params.count
                try params.push(sketch.params.items[(usize)e.second])
                e.second = r
                # A text's third is its label, which stays.
                if e.third >= 0 and e.kind != EntityKind.text: e.third = renumbered.items[(usize)e.third]
                if e.fourth >= 0: e.fourth = renumbered.items[(usize)e.fourth]
        try kept.push(e)
    var constraints = Growing[SketchConstraint]()
    errdefer constraints.close()
    for k in sketch.constraints.view():
        var c = k
        var lost = false
        for named in [k.first, k.second, k.third]:
            if named >= 0 and renumbered.items[(usize)named] < 0: lost = true
        # A polygon goes with any of its sides.
        if k.kind == ConstraintKind.polygon:
            for side in k.second..<k.third + 1:
                if renumbered.items[(usize)side] < 0: lost = true
        if lost: continue
        c.first = renumbered.items[(usize)k.first] if k.first >= 0 else -1
        c.second = renumbered.items[(usize)k.second] if k.second >= 0 else -1
        c.third = renumbered.items[(usize)k.third] if k.third >= 0 else -1
        try constraints.push(c)
    # Links of curves that stay.
    var links = Growing[Link]()
    errdefer links.close()
    for l in sketch.links.view():
        let moved = renumbered.items[(usize)l.entity]
        if moved < 0: continue
        try links.push(Link(moved, l.reference, l.reference_length))
    # Offset pairs of curves that stay (and relations that stay, below).
    var offsets = Growing[OffsetPair]()
    errdefer offsets.close()
    for pair in sketch.offsets.view():
        let group = renumbered.items[(usize)pair.group]
        let source = renumbered.items[(usize)pair.source]
        let copy = renumbered.items[(usize)pair.copy]
        if group < 0 or source < 0 or copy < 0: continue
        try offsets.push(OffsetPair(group, source, copy, pair.side))
    let labels = sketch.labels
    let chars = sketch.chars
    let faces = sketch.faces
    sketch.labels = Growing[Label]()
    sketch.chars = Growing[u8]()
    sketch.faces = Growing[ProjectedFace]()
    sketch.close()
    sketch.params = params
    sketch.entities = kept
    sketch.constraints = constraints
    sketch.lists = lists
    sketch.labels = labels
    sketch.chars = chars
    sketch.links = links
    sketch.offsets = offsets
    sketch.faces = faces
    drop_pairs(sketch)