Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.8

0.7.8 / src / verbs / symmetry.lucb

## Soft falloff for the Edit engine: soft weights fall off with the
## distance to the nearest group point, found through a PointHash of cells
## one radius wide. Mirror maps and symmetric groups are selecting/mirror.lucb.

## Soft weights of every point: 1 in `group`, falloff(1 - d / radius) within
## `radius` of its nearest member, 0 beyond. Falloff 0 linear, 1 quadratic,
## 2 cubic, 3 smooth (Houdini's Edit SOP falloffs).
pub func soft_weights(mesh: const Mesh*, group: Bits, radius: f64, falloff: i64, weights: f32[]) -> !:
    let points = mesh.point_span()
    var members = Growing[Vector3]()
    defer members.close()
    try members.reserve(group.population())
    for word in 0..<group.words.length:
        var pending = group.words[word]
        while pending != 0:
            let point = word * 64 + (usize)trailing_zeros(pending)
            pending = pending & (pending - 1)
            members.items[members.count] = points[point].vector()
            members.count += 1
    var grid = try PointHash.build(members.view(), radius)
    defer grid.close()
    var work = SoftWork(points, group, members.view(), &grid, radius, falloff, weights)
    try parallel.for_each_range(points.length, 4096, (void*)&work, soft_points)

## Soft weights of every point for a selection's components, as Transform
## Components weighs them: the selection's points (with their mirrors across
## `symmetry`, 1..3 for X, Y, Z; 0 none) are the group. The Edit viewport
## tints points by these while the soft radius is on.
pub func selection_weights(mesh: const Mesh*, selection: const Selection*, radius: f64, falloff: i64, symmetry: i64, weights: f32[]) -> !:
    if weights.length != (usize)mesh.point_count(): error(errors.invalid, "soft weights need one slot per point")
    if not (radius > 0.0) or not math.is_finite(radius): error(errors.invalid, "the soft radius must be positive and finite")
    if symmetry < 0 or symmetry > 3: error(errors.invalid, "symmetry is 0 (none) or 1..3 (X, Y, Z)")
    let topology = try Topology.of(mesh)
    var points = try converted(&topology, selection_bits(selection), Domain((u8)selection.domain()), Domain.point, false)
    defer points.close()
    if symmetry > 0:
        var mirrored = try symmetric(mesh, points.view(), Domain.point, symmetry)
        defer mirrored.close()
        try soft_weights(mesh, mirrored.view(), radius, falloff, weights)
        return
    try soft_weights(mesh, points.view(), radius, falloff, weights)

struct SoftWork:
    let points: const Float3[]
    let group: Bits
    let members: const Vector3[]
    let grid: const PointHash*
    let radius: f64
    let falloff: i64
    let weights: f32[]

func soft_points(context: void*, first: usize, end: usize, worker: usize):
    let work = (SoftWork*)context
    for point in first..<end:
        if work.group.get(point):
            work.weights[point] = 1.0
            continue
        let p = work.points[point].vector()
        let nearest = work.grid.nearest(work.members, p, work.radius)
        if nearest < 0:
            work.weights[point] = 0.0
            continue
        let t = 1.0 - work.members[(usize)nearest].subtract(p).length() / work.radius
        var weight = t
        if work.falloff == 1: weight = t * t
        elif work.falloff == 2: weight = t * t * t
        elif work.falloff == 3: weight = t * t * (3.0 - 2.0 * t)
        work.weights[point] = (f32)math.max(0.0, math.min(1.0, weight))