Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / layout / types.lucb

## Face layout (internal): where a face's mesh rows and columns go before
## any face is meshed. Each edge's stations (curve_spacing, edge_sampling),
## a face's four logical sides (patch_sides), its coedges chained in one
## chart (trim_chains, trim_memo), row counts agreed across shared edges
## (counts, balance, phase, density, rows, seam_flow, circular_lattice),
## and the checks a plan must pass (feasibility, policy, envelope, pitch,
## cell_error, flow_admission, trim_crossings, trim_alignment).
##
## This file: the layout's shared types.
import memory
import interop
import ownership
from luce_std import math, parallel
from luce_geocore.geocore import invalid, Vector3, squared_triangle_distance
from luce_geocore.core.growing import Growing
from geometry import surface_jet, Surface, Curve, BrepFace, edge_index, project, chart_uv, surface_spline, curve_line, evaluate, curve_circle, curve_spline, chart_parameter, surface_point, chart_gap, chart_near, surface_plane, surface_cylinder, surface_cone, surface_torus, curve_ellipse, surface_sphere, union_root, Samples, wrap, nonzero, support_normal

## The most stations along one axis of a face's layout. A long pipe at 64
## divisions takes about 1,050 round its axis (its sag target is relative
## to its section) and only a few along it.
pub let axis_stations: usize = 2049

## Bound the two-dimensional layout rather than assuming square patches.
## The total matches TrimGrid's 131,072 grid-point budget; a narrow strip may
## use more than 256 intervals along its long direction without a larger grid.
pub func check_grid_size(u: usize, v: usize) -> !:
    if u > axis_stations or v > axis_stations or u * v > 131072:
        error(invalid, "trim support grid exceeds total station budget")

## The most stations on one edge (its samples, and the grid lines of the
## faces on either side crossing it). Two faces' lattices meet on a shared
## edge each with its own phase, so an edge may hold the stations of both:
## a long pipe's end circle at 64 divisions takes the pipe's columns (about
## 750) and its cap's lattice crossings (about 500).
pub let edge_stations: usize = 4097

## Cached parameter chain for one oriented face use; points remain in the
## canonical edge direction. Its length is the edge's monotonic revision.
pub struct UVChain:
    pub var points: Vector3[]
    pub var times: f64[]

## Corner normals evaluated directly from the clipped grid's known UVs.
pub struct NormalCache:
    pub var values: f64[]

## Frozen global point IDs selected by one face for its own interior rows.
pub struct RowIds:
    pub var values: i64[]

## How finely a mesh follows its surfaces (MeshQuality, as the mesher
## takes it): `steps` per quarter turn of a curve or surface (each step
## spans at most a quarter turn over them), and the farthest a mesh may
## stray from its surface, `chord` (mm; 0: no such limit).
pub struct Density:
    pub var steps: i64
    pub var chord: f64 = 0.0

    ## The sag a mesh of a feature `extent` across may have at `share`
    ## steps squared of it (the mesher's scale-invariant targets), within
    ## the chord limit, never under the mesher's identity `floor`.
    pub func sag(extent: f64, share: f64, floor: f64) -> f64:
        let steps = (f64)self.steps
        let target = extent / (steps * steps * share)
        return math.max(floor, math.min(target, self.chord) if self.chord > 0.0 else target)

    ## `target` within the chord limit.
    pub func within(target: f64) -> f64:
        return math.min(target, self.chord) if self.chord > 0.0 else target

## How many times longer than wide a mesh cell may stretch along a
## surface's straight direction (a cylinder's axis, an extrusion's): past
## it a row is added, so no display triangle becomes a needle. Measured
## against the mean step across, so a cell stays within about 32. Fusion's
## display keeps cells up to about 48 (a cylinder as tall as wide, one row
## at Very High); its export, 21.5.
pub let row_stretch: f64 = 24.0

## No sag limit (a flat face, or no chord limit).
pub let flat_sag: f64 = 1.0e300

## Stations closer than this (in their parameter: an edge's arc station, a
## chart angle) are one: exact stations computed two ways land within it.
pub let same_station: f64 = 1e-12

## A shared edge cut (a station another face's rows put there) closer
## than this to one already there, in the edge's parameter, is that one.
pub let same_cut: f64 = 1e-9

## A count's ceiling ignores roundoff this far above a whole number (a
## span of exactly n steps computed as n plus a few ulps is n).
pub let count_slack: f64 = 1e-9

## A squared length, area or table span under this is none: a guard
## against dividing by a degenerate span, far below any geometry's.
pub let degenerate: f64 = 1e-30

## A pitch or length under this is none (a collapsed side or row).
pub let vanishing: f64 = 1e-20

## Two stations closer than this in their parameter are one, by default
## (Stations.epsilon), and a plan's chart coordinates (GridLayout.epsilon).
pub let station_epsilon: f64 = 1e-7
pub let plan_epsilon: f64 = 1e-8

## Bounded, sorted parameters with separate numerical and optional row spacing.
pub struct Stations:
    pub var values: f64[]
    pub var count: usize = 0
    pub var epsilon: f64 = station_epsilon
    pub var separation: f64 = 0.0
    pub var baseline: usize = 0
    pub var limit: usize = 0

    pub func open(capacity: usize) -> !:
        self.values = try new f64[capacity if capacity < 16 else 16] --- in memory.heap
        self.count = 0
        self.limit = capacity

    ## Sorted bounded union, not an interval count: phase survives propagation.
    pub func insert(value: f64, epsilon: f64) -> bool!:
        var low: usize = 0
        var high = self.count
        while low < high:
            let mid = (low + high) // 2
            if self.values[mid] < value: low = mid + 1
            else: high = mid
        let distance = math.max(epsilon, self.separation)
        if low < self.count and math.abs(self.values[low] - value) <= distance: return false
        if low > 0 and math.abs(self.values[low - 1] - value) <= distance: return false
        if self.count == self.values.length:
            if self.count == self.limit: error(invalid, "cross-patch layout station budget exceeded")
            let capacity = self.limit if self.values.length * 2 > self.limit else self.values.length * 2
            let grown = try new f64[capacity] --- in memory.heap
            memory.copy(grown, self.values, self.count)
            free(self.values) in memory.heap
            self.values = grown
        var at = self.count
        while at > low:
            self.values[at] = self.values[at - 1]
            at -= 1
        self.values[low] = value
        self.count += 1
        return true

    pub func close():
        if self.values.length > 0: free(self.values) in memory.heap

## Per-face layout and owned coedge/normal caches, frozen before face meshing.
pub struct GridLayout:
    pub var island: i64 = -1
    pub var rows: Stations[]
    pub var row_ids: RowIds[]
    pub var flow: bool[]
    pub var u: Stations = Stations()
    pub var v: Stations = Stations()
    pub var valid: bool = false
    pub var cut_first: bool = false
    pub var epsilon: f64 = plan_epsilon
    pub var trim_low: Vector3 = Vector3()
    pub var trim_high: Vector3 = Vector3()
    pub var independent_phase: bool = false
    pub var pitch_axes: bool[2] = [false, false]
    pub var applied: bool[]
    # One face job owns this slot: 1 local rows, 2 clipped grid, 3 canonical
    # rows, 4 constrained fallback, 5 baseline fallback plus seam conformity.
    pub var method: i64[]
    pub var seen: usize = 0
    pub var chains: UVChain[]
    pub var normals: NormalCache[]

    pub func close():
        for row in self.rows: row.close()
        if self.rows.length > 0: free(self.rows) in memory.heap
        for ids in self.row_ids:
            if ids.values.length > 0: free(ids.values) in memory.heap
        if self.row_ids.length > 0: free(self.row_ids) in memory.heap
        if self.flow.length > 0: free(self.flow) in memory.heap
        self.u.close()
        self.v.close()
        if self.applied.length > 0: free(self.applied) in memory.heap
        if self.method.length > 0: free(self.method) in memory.heap
        for chain in self.chains:
            if chain.points.length > 0: free(chain.points) in memory.heap
            if chain.times.length > 0: free(chain.times) in memory.heap
        if self.chains.length > 0: free(self.chains) in memory.heap
        if self.normals.length > 0:
            if self.normals[0].values.length > 0: free(self.normals[0].values) in memory.heap
            free(self.normals) in memory.heap