Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-3d

Source of release 0.9.5

0.9.5 / src / renderers / curve_lines.lucb

## Curves as lines: the evaluated polyline of every curve in a geometry set
## (instances placed), and its control hull: the control polygon of smooth
## curves, Bezier handle arms, and a small cross at every control point. The
## segments are world-space endpoint pairs, as WireBatch draws them, extracted
## in parallel from luce-geocore's evaluated-curve cache.
import interop
from luce_std import math, parallel
import memory
import errors
from luce_geocore.geocore import Vector3, Matrix4, GeometrySet
from luce_geocore.kernel import Curves, CurveType, Float3, set_curves, evaluated_offsets, evaluated_positions, instances_component, Instances, curves_origin, curve_float3_span
from luce_geocore.core.growing import Growing
from renderers.wire_batch import WireBatch, wire_batch_type

## Instances deeper than this are not drawn (as the renderer's limit).
let most_depth: usize = 64

## Append the segments of every curve under `set` placed by `matrix`: the
## curves themselves to `lines`, their hulls to `hulls` (either may be none).
pub func append_curve_segments(set: const GeometrySet*, matrix: Matrix4, lines: Growing[Vector3]*?, hulls: Growing[Vector3]*?) -> !:
    try append_nested(set, matrix, lines, hulls, 0)

func append_nested(set: const GeometrySet*, matrix: Matrix4, lines: Growing[Vector3]*?, hulls: Growing[Vector3]*?, depth: usize) -> !:
    if depth > most_depth: return
    if let curves = set_curves(set):
        if let out = lines: try append_lines(curves, matrix, out)
        if let out = hulls: try append_hull(curves, matrix, out)
    let data = set.find(instances_component()) else return
    let instances = (const Instances*)data
    for index in 0..<instances.count():
        let item = instances.item(index)
        if not item.visible: continue
        try append_nested(item.set, matrix.multiply(item.matrix()), lines, hulls, depth + 1)

## Every evaluated segment, closing segments of cyclic curves included.
func append_lines(curves: const Curves*, matrix: Matrix4, out: Growing[Vector3]*) -> !:
    let offsets = try evaluated_offsets(curves)
    let points = try evaluated_positions(curves)
    let count = curves.curve_count()
    let firsts = try new usize[count + 1] --- in memory.heap
    defer free(firsts) in memory.heap
    var total = out.count
    for curve in 0..<count:
        firsts[curve] = total
        let size = (usize)offsets[curve + 1] - (usize)offsets[curve]
        let segments = (size if curves.settings(curve).cyclic else size - 1) if size > 1 else 0
        total += segments * 2
    firsts[count] = total
    try out.reserve(total)
    var work = LineWork(curves, matrix, offsets, points, firsts, out.items)
    try parallel.for_each_range(count, 64, (void*)&work, fill_lines)
    out.count = total

struct LineWork:
    let curves: const Curves*
    let matrix: Matrix4
    let offsets: const i32[]
    let points: const Float3[]
    let firsts: const usize[]
    let out: Vector3[]

func fill_lines(context: void*, first: usize, end: usize, worker: usize):
    let work = (LineWork*)context
    for curve in first..<end:
        let low = (usize)work.offsets[curve]
        let size = (usize)work.offsets[curve + 1] - low
        var at = work.firsts[curve]
        let stop = work.firsts[curve + 1]
        var index: usize = 0
        let origin = curves_origin(work.curves)
        while at < stop:
            let a = work.points[low + index].vector().add(origin)
            let b = work.points[low + (index + 1) % size].vector().add(origin)
            work.out[at] = work.matrix.transform_point(a)
            work.out[at + 1] = work.matrix.transform_point(b)
            at += 2
            index += 1

## Control polygons of non-poly curves, handle arms of Bezier curves, and a
## cross at each control point sized to the curves' extent.
func append_hull(curves: const Curves*, matrix: Matrix4, out: Growing[Vector3]*) -> !:
    let handles_l = curve_float3_span(curves, "handle_l")
    let handles_r = curve_float3_span(curves, "handle_r")
    let origin = curves_origin(curves)
    var low = curves.point(0)
    var high = low
    for at in 0..<curves.point_count():
        let p = curves.point(at)
        low = Vector3(math.min(low.x, p.x), math.min(low.y, p.y), math.min(low.z, p.z))
        high = Vector3(math.max(high.x, p.x), math.max(high.y, p.y), math.max(high.z, p.z))
    let mark = math.max(high.subtract(low).length() * 0.004, 0.000001)
    for curve in 0..<curves.curve_count():
        let settings = curves.settings(curve)
        if settings.kind == CurveType.poly: continue
        let (first, size) = curves.range(curve)
        let segments = (size if settings.cyclic else size - 1) if size > 1 else 0
        for at in 0..<segments:
            try out.push(matrix.transform_point(curves.point(first + at)))
            try out.push(matrix.transform_point(curves.point(first + (at + 1) % size)))
        for at in first..<first + size:
            let p = curves.point(at)
            if settings.kind == CurveType.bezier:
                if let left = handles_l:
                    if let right = handles_r:
                        try out.push(matrix.transform_point(left[at].vector().add(origin)))
                        try out.push(matrix.transform_point(right[at].vector().add(origin)))
            for axis in 0..<(usize)3:
                let offset = Vector3(mark if axis == 0 else 0.0, mark if axis == 1 else 0.0, mark if axis == 2 else 0.0)
                try out.push(matrix.transform_point(p.subtract(offset)))
                try out.push(matrix.transform_point(p.add(offset)))

## Luce's view: the curves of a set as wire batches.
pub struct CurveLines:
    let reserved: u8 = 0

    ## Segments of every curve in `geometry` (0: nothing to draw).
    pub static func count(geometry: interop.Reference[GeometrySet]) -> i64!:
        var lines = Growing[Vector3]()
        defer lines.close()
        try append_curve_segments(try geometry.get(), Matrix4(), &lines, none)
        return (i64)(lines.count // 2)

    ## The evaluated curves of `geometry` as a batch, or their hulls, placed
    ## by `matrix`.
    pub static func batch(geometry: interop.Reference[GeometrySet], hull: bool = false, matrix: Matrix4 = Matrix4()) -> interop.Reference[WireBatch]!:
        var points = Growing[Vector3]()
        defer points.close()
        try append_curve_segments(try geometry.get(), matrix, &points if not hull else none, &points if hull else none)
        return try interop.Reference[WireBatch].adopt(try new WireBatch(points.view()) in memory.heap, wire_batch_type, memory.heap)