Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.1

0.9.1 / src / cad_overlay.lucb

## What the viewport draws over a CAD model's shaded tessellation: its
## analytic boundaries (each edge once), its hull (the B-spline control
## nets) and a normal guide per face, each as point pairs. Luce reads them
## by index; Base fills them (CadModel.overlay).
import interop
import memory
from luce_geocore.geocore import Vector3, Mesh, invalid
from luce_geocore.core.growing import Growing

pub struct CadOverlay:
    var boundaries: Growing[Vector3]
    var hull: Growing[Vector3]
    var guides: Growing[Vector3]

    pub func init():
        self.boundaries = Growing[Vector3]()
        self.hull = Growing[Vector3]()
        self.guides = Growing[Vector3]()

    pub func boundary_count() -> i64:
        return (i64)self.boundaries.count

    pub func boundary(index: i64) -> Vector3!:
        return try point(&self.boundaries, index)

    pub func hull_count() -> i64:
        return (i64)self.hull.count

    pub func hull_point(index: i64) -> Vector3!:
        return try point(&self.hull, index)

    pub func guide_count() -> i64:
        return (i64)self.guides.count

    pub func guide(index: i64) -> Vector3!:
        return try point(&self.guides, index)

    pub func close():
        self.boundaries.close()
        self.hull.close()
        self.guides.close()

func point(list: const Growing[Vector3]*, index: i64) -> Vector3!:
    if index < 0 or (usize)index >= list.count: error(invalid, "CAD overlay index out of range")
    return list.items[(usize)index]

## The lists Base fills (Luce reads them through the methods above).
pub func boundary_list(overlay: CadOverlay*) -> Growing[Vector3]*:
    return &overlay.boundaries

pub func hull_list(overlay: CadOverlay*) -> Growing[Vector3]*:
    return &overlay.hull

pub func guide_list(overlay: CadOverlay*) -> Growing[Vector3]*:
    return &overlay.guides

pub let cad_overlay_type: interop.Type[CadOverlay] = interop.Type[CadOverlay]("CadOverlay", CadOverlay.close, closeable = true)

## A normal guide per CAD face of `mesh` (by `cad_face`): from its middle
## polygon's center along its normal, 3% of the mesh's size long.
pub func face_guides(mesh: const Mesh*, out: Growing[Vector3]*) -> !:
    let source = mesh.find_attribute("cad_face", 2)
    let faces = mesh.face_count()
    if source < 0 or faces == 0: return
    let owners = try mesh.attribute_entry(source)
    let box = mesh.bounds()
    let length = box.maximum.subtract(box.minimum).length() * 0.03
    var first: i64 = 0
    while first < faces:
        let owner = owners.number((usize)first, 0)
        var end = first + 1
        while end < faces and owners.number((usize)end, 0) == owner: end += 1
        let middle = first + (end - first) // 2
        let center = try mesh.face_center(middle)
        try out.push(center)
        try out.push(center.add((try mesh.face_normal(middle)).multiply_scalar(length)))
        first = end