Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.4

0.7.4 / src / subdivision / settings.lucb

## Subdivision as a display attribute (after USD and Hydra's refineLevel). A
## mesh asks to be shown subdivided through detail attributes, and nothing
## else changes: downstream nodes read the cage, its points, topology and
## selection ids. The display draws the limit surface of the refined cage
## (plan.lucb, refine.lucb, limit.lucb), cached per connectivity and
## positions (display.lucb):
##
## - `subd.scheme`: 0 none, 1 Catmull-Clark, 2 Loop (triangles), 3 bilinear;
## - `subd.level`: display refinement levels (clamped so the refined mesh
##   stays within 8M faces and points); `subd.render_level` for renders;
## - `subd.boundary`: 0 sharp edges only, 1 sharp edges and corners (a
##   boundary point on a single face stays put);
## - edge attribute `crease` and point attribute `corner_sharpness`: USD's
##   sharpness (0 smooth, 10 or more infinitely sharp; a sharpness of s stays
##   sharp for s levels, the fraction blending into the smooth rule).
import interop
from luce_std import math
import memory
import sync
import errors
from luce_std import parallel
from core.growing import Growing
from core.bits import Bits, BitsBuilder
from core.shared import Column
from core.float3 import Float3
from core.attributes import Attribute, Domain, DataType, Role
from core.attribute_ops import typed, typed_mut
from math.vector import Vector3
from geometries.polygon import Mesh, mesh_share, mesh_dispose, mesh_adopt, precise_span, moved_storage, displayed, put_attribute, remove_attribute
from geometries.topology.connectivity import Topology
from verbs import TopologyBuilder
import geometries.triangulation.face_scratch
from core.limits import max_points, max_faces
from core.hash import FullKey

pub enum Scheme as u8:
    off = 0
    catmull_clark = 1
    loop = 2
    bilinear = 3

## Sharpness at or above this is infinite (USD).
let infinite: f32 = 10.0

## What the display does with a mesh.
pub struct Settings:
    pub let scheme: Scheme
    pub let level: usize
    pub let corners: bool

## A detail number of `mesh`, or `fallback` without the attribute.
func detail(mesh: const Mesh*, name: str, fallback: f64) -> f64:
    let index = mesh.attribute_on(name, Domain.detail)
    if index < 0: return fallback
    return mesh.attribute_value(index, 0) catch failure: recover fallback

## The mesh's subdivision settings, the level clamped to the budget (Loop
## needs triangles: other faces fall back to Catmull-Clark).
pub func settings_of(mesh: const Mesh*) -> Settings:
    var scheme = Scheme((u8)math.max(0.0, math.min(3.0, detail(mesh, "subd.scheme", 0.0))))
    let wanted = (usize)math.max(0.0, math.min(6.0, detail(mesh, "subd.level", 1.0)))
    let pinned = detail(mesh, "subd.boundary", 1.0) != 0.0
    if scheme == Scheme.off or wanted == 0 or mesh.face_count() == 0: return Settings(Scheme.off, 0, pinned)
    let offsets = mesh.offset_span()
    if scheme == Scheme.loop:
        for face in 0..<offsets.length - 1:
            if offsets[face + 1] - offsets[face] != 3:
                scheme = Scheme.catmull_clark
                break
    # Level 1 makes a face per corner (Loop: four per triangle), then four
    # per face each level; points grow alike.
    let corners = (f64)mesh.corner_span().length
    var faces = corners if scheme != Scheme.loop else (f64)mesh.face_count() * 4.0
    var points = (f64)mesh.point_count() + corners + (f64)mesh.face_count()
    var level: usize = 0
    while level < wanted and faces <= (f64)max_faces and points <= (f64)max_points:
        level += 1
        faces *= 4.0
        points *= 4.0
    return Settings(scheme, level, pinned)