Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / model / 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 of faces no modeling feature named: imported and freeform ones),
## its feature nets (those of faces a feature named, a fillet's strips: shown
## only where their cvs are edited, as Fusion shows no nets on a modeled
## body) and a normal guide per face, each as point pairs. Luce reads them by
## index; Base fills them (CadModel.overlay).

## A model's overlay lines, as point pairs (see above).
pub struct CadOverlay:
    var boundaries: Growing[Vector3]
    var hull: Growing[Vector3]
    var nets: Growing[Vector3]
    var guides: Growing[Vector3]

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

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

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

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

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

    pub func net_count() -> i64:
        return (i64)self.nets.count

    pub func net_point(index: i64) -> Vector3!:
        return try listed_point(&self.nets, index)

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

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

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

func listed_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 net_list(overlay: CadOverlay*) -> Growing[Vector3]*:
    return &overlay.nets

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() * guide_length
    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

## The B-rep's edges as `mesh` (`model`'s tessellation) samples them, onto
## `out` as point pairs: each mesh edge between polygons of two B-rep faces
## on different surfaces (seams are not drawn), or on the mesh's open
## border. Taken from the polygons' own points, an edge's line runs exactly
## along the faces beside it; the B-rep's own polyline, sampled apart from
## the mesh, crossed the faces' chords and showed dashed where it dipped
## behind them. False (nothing added) when the mesh has no faces to read.
pub func mesh_boundary_lines(mesh: const Mesh*, model: const CadModel*, out: Growing[Vector3]*) -> bool!:
    let source = mesh.find_attribute("cad_face", 2)
    if source < 0 or mesh.face_count() == 0: return false
    let faces = try mesh.attribute_entry(source)
    let ends = try mesh.edge_span()
    let edges = ends.length // 2
    let offsets = mesh.offset_span()
    let corner_edges = try mesh.corner_edge_span()
    # Each mesh edge's first two polygons' CAD faces (-1: none yet).
    let sides = try new i64[edges * 2] --- in memory.heap
    defer free(sides) in memory.heap
    for at in 0..<sides.length: sides[at] = -1
    for polygon in 0..<offsets.length - 1:
        let face = (i64)faces.number(polygon, 0)
        for corner in (usize)offsets[polygon]..<(usize)offsets[polygon + 1]:
            let edge = (usize)corner_edges[corner]
            if sides[edge * 2] < 0: sides[edge * 2] = face
            elif sides[edge * 2 + 1] < 0: sides[edge * 2 + 1] = face
    let first = (i64)model.count
    for edge in 0..<edges:
        let a = sides[edge * 2]
        let b = sides[edge * 2 + 1]
        # Independent faces draw their own outlines (CadModel.overlay).
        if a < first: continue
        if b >= 0 and (a == b or (try model.face_surface_id(a)) == (try model.face_surface_id(b))): continue
        try out.push(try mesh.point((i64)ends[edge * 2]))
        try out.push(try mesh.point((i64)ends[edge * 2 + 1]))
    return true