Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.3

0.7.3 / src / verbs / cleanup.lucb

## Fuse (merge the group's points closer than a distance), Merge (the
## group's points into one at their center, the first or the last, each
## connected island into one, or by distance) and Clean (drop unused
## points). Faces keep their corner order; a face whose corners collapse
## below three disappears. Fuse and Merge select the points that absorbed
## others.

let fuse_parms: ParmSpec[1] = [ParmSpec("Distance", ParmKind.number, 0.001, 0.000001, 100000.0, 1, "", "Points of the group closer than this merge into the first of them.")]

func fuse_verb() -> Verb:
    return Verb("Fuse", "Modeling", "Merge nearby points of the group", Domain.point, true, fuse_parms, true, cook_fuse)

let merge_parms: ParmSpec[2] = [
    ParmSpec("Mode", ParmKind.menu, 0.0, 0.0, 4.0, 1, "At center|At first|At last|Collapse islands|By distance", "Where the group's points merge: all at their center, the first or the last point; each connected island at its center; or those within the distance."),
    ParmSpec("Distance", ParmKind.number, 0.001, 0.000001, 100000.0, 1, "", "By distance: points of the group closer than this merge.")]

func merge_verb() -> Verb:
    return Verb("Merge Points", "Modeling", "Merge the group's points: at their center, first, last, per island or by distance", Domain.point, true, merge_parms, true, cook_merge)

func clean_verb() -> Verb:
    return Verb("Clean", "Modeling", "Remove points no face uses", Domain.point, true, placeholder_parms[..<0], true, cook_clean)

## Spatial hash of cells `distance` wide: each group point joins the lowest
## numbered earlier point within `distance` in its cell or a neighbor.
func cook_fuse(input: const VerbInput*, output: VerbOutput*) -> !:
    let distance = input.parms.at(0)
    if not (distance >= 0.000001 and distance <= 100000.0): error(errors.invalid, "Fuse distance must be in 1e-6..1e5")
    let points = (usize)input.mesh.point_count()
    let target = try new i32[points] --- in memory.heap
    defer free(target) in memory.heap
    try fuse_targets(input, distance, target)
    var nothing: Vector3[]
    try rebuild(input, output, target, nothing)
    output.selection_domain = Domain.point

## Each group point's target: the lowest numbered earlier point within
## `distance` (a PointHash of cells that wide), else itself.
func fuse_targets(input: const VerbInput*, distance: f64, target: i32[]) -> !:
    let points = target.length
    let positions = input.mesh.point_span()
    var grid = try PointHash.empty(points, distance)
    defer grid.close()
    for point in 0..<points:
        target[point] = (i32)point
        if not input.group.get(point): continue
        let p = positions[point].vector()
        let home = grid.cell_of(p)
        var found: i32 = -1
        for dx in (i64)-1..<(i64)2:
            for dy in (i64)-1..<(i64)2:
                for dz in (i64)-1..<(i64)2:
                    var candidate = grid.first(home.moved(dx, dy, dz))
                    while candidate >= 0:
                        if positions[(usize)candidate].vector().subtract(p).length() <= distance and (found < 0 or candidate < found):
                            found = candidate
                        candidate = grid.after(candidate)
        if found >= 0:
            target[point] = found
        else:
            grid.insert(point, p)

func cook_merge(input: const VerbInput*, output: VerbOutput*) -> !:
    let mode = input.parms.at(0)
    let mesh = input.mesh
    let points = (usize)mesh.point_count()
    let target = try new i32[points] --- in memory.heap
    defer free(target) in memory.heap
    for at in 0..<points: target[at] = (i32)at
    if mode == 4.0:
        let distance = input.parms.at(1)
        if not (distance >= 0.000001 and distance <= 100000.0): error(errors.invalid, "Merge distance must be in 1e-6..1e5")
        try fuse_targets(input, distance, target)
        var nothing: Vector3[]
        try rebuild(input, output, target, nothing)
        output.selection_domain = Domain.point
        return
    if input.group.population() < 2:
        output.warning = "Merge needs two or more points in the group; the input passes through"
        return
    let places = try new Vector3[points] --- in memory.heap
    defer free(places) in memory.heap
    for at in 0..<points: places[at] = builder_position(mesh, at)
    if mode == 3.0:
        try islands(input, target)
    else:
        var first: i32 = -1
        var last: i32 = -1
        for point in 0..<points:
            if not input.group.get(point): continue
            if first < 0: first = (i32)point
            last = (i32)point
        let keep = last if mode == 2.0 else first
        for point in 0..<points:
            if input.group.get(point): target[point] = keep
    # At a center: each kept point moves to the mean of those merging into it.
    if mode == 0.0 or mode == 3.0:
        let sums = try new Vector3[points] --- in memory.heap
        defer free(sums) in memory.heap
        let counts = try new u32[points] --- in memory.heap
        defer free(counts) in memory.heap
        for at in 0..<points:
            sums[at] = Vector3()
            counts[at] = 0
        for point in 0..<points:
            if not input.group.get(point): continue
            let into = (usize)target[point]
            sums[into] = sums[into].add(places[point])
            counts[into] += 1
        for point in 0..<points:
            if counts[point] > 0: places[point] = sums[point].multiply_scalar(1.0 / (f64)counts[point])
    try rebuild(input, output, target, places)
    output.selection_domain = Domain.point

## Each connected island of the group (points joined by edges with both
## ends in it) merges into its lowest numbered point.
func islands(input: const VerbInput*, target: i32[]) -> !:
    let mesh = input.mesh
    let edges = try mesh.edge_span()
    for edge in 0..<edges.length // 2:
        let a = (usize)edges[edge * 2]
        let b = (usize)edges[edge * 2 + 1]
        if not input.group.get(a) or not input.group.get(b): continue
        let root_a = root(target, (i32)a)
        let root_b = root(target, (i32)b)
        if root_a != root_b: target[(usize)(root_a if root_a > root_b else root_b)] = root_a if root_a < root_b else root_b
    for point in 0..<target.length: target[point] = root(target, (i32)point)

func root(parent: i32[], point: i32) -> i32:
    var at = point
    while parent[(usize)at] != at:
        parent[(usize)at] = parent[(usize)parent[(usize)at]]
        at = parent[(usize)at]
    return at

func cook_clean(input: const VerbInput*, output: VerbOutput*) -> !:
    let points = (usize)input.mesh.point_count()
    let target = try new i32[points] --- in memory.heap
    defer free(target) in memory.heap
    for at in 0..<points: target[at] = (i32)at
    var nothing: Vector3[]
    try rebuild(input, output, target, nothing)
    output.selection_domain = Domain.point

## Faces over points mapped through `target` (each point to the one it
## merges into, placed at `places` when given), keeping only points some
## face uses. Merged-into points are selected.
func rebuild(input: const VerbInput*, output: VerbOutput*, target: const i32[], places: const Vector3[]) -> !:
    let mesh = input.mesh
    let offsets = mesh.offset_span()
    let corners = mesh.corner_span()
    let points = target.length
    var builder = &output.builder
    try builder.begin(mesh)
    try builder.copy_points(mesh)
    output.built = true
    # Points that absorbed others are selected (and placed, when given).
    output.selection = try BitsBuilder.zeros(points)
    for point in 0..<points:
        if target[point] == (i32)point: continue
        output.selection.set((usize)target[point])
        if places.length > 0: builder.move_point(target[point], places[(usize)target[point]])
    # Faces whose points all stay copy in runs; the rest are rebuilt over
    # their targets, dropping repeated corners (and the face below three).
    var id_stack: i32[256]
    var parent_stack: i32[256]
    var run: usize = 0
    let faces = offsets.length - 1
    for face in 0..<faces:
        let start = (usize)offsets[face]
        let end = (usize)offsets[face + 1]
        var moved = false
        for corner in start..<end:
            if target[(usize)corners[corner]] != corners[corner]: moved = true
        if not moved: continue
        try builder.copy_faces(mesh, run, face)
        run = face + 1
        # Small faces on the stack, larger ones in heap scratch.
        let ids = try face_scratch.take[i32](id_stack, end - start, end - start)
        defer face_scratch.release[i32](id_stack, ids)
        let parents = try face_scratch.take[i32](parent_stack, end - start, end - start)
        defer face_scratch.release[i32](parent_stack, parents)
        var count: usize = 0
        for corner in start..<end:
            let point = target[(usize)corners[corner]]
            if count == 0 or point != ids[count - 1]:
                ids[count] = point
                parents[count] = (i32)corner
                count += 1
        if count > 1 and ids[0] == ids[count - 1]: count -= 1
        if count < 3: continue
        try builder.face((i32)face)
        for at in 0..<count: try builder.corner(ids[at], parents[at])
        try builder.end_face()
    try builder.copy_faces(mesh, run, faces)
    try builder.drop_unused_points(&output.selection)

func builder_position(mesh: const Mesh*, point: usize) -> Vector3:
    let exact = precise_span(mesh)
    if exact.length > 0: return exact[point]
    return mesh.point_span()[point].vector().add(mesh.position_origin())