Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.3

0.7.3 / src / geometries / shading / normal_refresh.lucb

## Authored normals (N) after a position edit: the points, corners or faces
## around the moved points take the smooth normals of the new shape (the rule
## of smooth_normals.lucb: angle-weighted, split at a 60-degree cusp), and
## everything else keeps its authored value. CAD shading survives a local
## Move instead of falling back to a recomputed surface. The columns are
## written copy-on-write, so snapshots sharing them keep theirs, and the new
## column names the old one as its parent (GPU copies update in place).
import memory
from luce_std import parallel
from core.attributes import AttributeStore, DataType, Domain
from core.attribute_ops import typed_mut
from math.vector import Vector3
from geometries.shading.smooth_normals import Surface, affected_points, point_normal, corner_normal

pub func refresh_normals(attributes: AttributeStore*, shape: const Surface*, moved: const u32[]) -> !:
    if moved.length == 0: return
    var affected: u32[]
    defer:
        if affected.length > 0: free(affected) in memory.heap
    for index in 0..<attributes.count():
        let entry = attributes.entry(index)
        if entry.name() != "N" or entry.width != 3 or (entry.kind != DataType.float64 and entry.kind != DataType.float32): continue
        if entry.domain == Domain.detail or entry.domain == Domain.edge or (u8)entry.storage == 1: continue
        if affected.length == 0: affected = try affected_points(shape, moved)
        let domain = entry.domain
        let wide = entry.kind == DataType.float64
        let bytes = try attributes.writable(index)
        if domain == Domain.face:
            for point in moved:
                for at in (usize)shape.incident_offsets[(usize)point]..<(usize)shape.incident_offsets[(usize)point + 1]:
                    let face = (usize)shape.incident_faces[at]
                    write(bytes, wide, face, shape.normals[face].vector())
            continue
        # Each affected point writes only its own point or corners.
        var work = Work(shape, affected, bytes, wide, domain == Domain.corner)
        try parallel.for_each_range(affected.length, 64, (void*)&work, refresh_range)
        let range = written_range(shape, affected, domain == Domain.corner)
        attributes.set_changed(index, range.0, range.1)

struct Work:
    let shape: const Surface*
    let points: const u32[]
    let bytes: u8[]
    let wide: bool
    let corners: bool

func refresh_range(context: void*, first: usize, end: usize, worker: usize):
    let work = (Work*)context
    let shape = work.shape
    for index in first..<end:
        let point = work.points[index]
        if not work.corners:
            write(work.bytes, work.wide, (usize)point, point_normal(shape, (usize)point).0)
            continue
        for at in (usize)shape.incident_offsets[(usize)point]..<(usize)shape.incident_offsets[(usize)point + 1]:
            let face = (usize)shape.incident_faces[at]
            for corner in (usize)shape.offsets[face]..<(usize)shape.offsets[face + 1]:
                if (u32)shape.corners[corner] == point:
                    write(work.bytes, work.wide, corner, corner_normal(shape, face, corner))

func write(bytes: u8[], wide: bool, element: usize, normal: Vector3):
    if wide:
        let values = typed_mut[f64](bytes)
        values[element * 3] = normal.x
        values[element * 3 + 1] = normal.y
        values[element * 3 + 2] = normal.z
    else:
        let values = typed_mut[f32](bytes)
        values[element * 3] = (f32)normal.x
        values[element * 3 + 1] = (f32)normal.y
        values[element * 3 + 2] = (f32)normal.z

## The elements the refresh wrote: the affected points (sorted), or the
## corners of their faces.
func written_range(shape: const Surface*, affected: const u32[], corners: bool) -> (usize, usize):
    if affected.length == 0: return (0, 0)
    if not corners: return ((usize)affected[0], (usize)affected[affected.length - 1] + 1)
    var first = shape.corners.length
    var end: usize = 0
    for point in affected:
        for at in (usize)shape.incident_offsets[(usize)point]..<(usize)shape.incident_offsets[(usize)point + 1]:
            let face = (usize)shape.incident_faces[at]
            let start = (usize)shape.offsets[face]
            let stop = (usize)shape.offsets[face + 1]
            first = start if start < first else first
            end = stop if stop > end else end
    return (first, end)