dymokomi/luce-usd
Source of release 0.9.6
0.9.6 / src / convert / common.lucb
## Stage to GeometrySet conversion, after the mapping in docs/MAPPING.md
## (design ยง2): gprims flatten into one component per family, baked to
## world space, with each element's prim path in the text attribute `path`.
## This fragment holds the options, warnings and the parallel chunking the
## others share; meshes.lucb builds the polygon topology, primvars.lucb the
## attributes, mesh_extras.lucb subsets, subdivision and bindings, and
## load.lucb the whole import.
from luce_std import math, parallel
import memory
import interop
import sync
import errors
from sdf.names import Names, absolute_root, empty_path
from sdf.layer_data import LayerData, Spec, SpecType
from sdf.values import Value, ListOp, ListItem, ListPart, specifier_def, variability_uniform, variability_varying
import sdf.value_types as usd_types
from sdf.value_types import ValueKind, scalar_of, components, element_bytes, find_type
from stage import Stage, earliest, scalar_number
from geom_tokens import GeomTokens
from usd_geom import Matrix4d, Gprim, GprimKind, Gathered, gather, component_at, is_shape
from luce_geocore.geocore import Vector3, Mesh
from luce_geocore.kernel import Float3, Attribute, AttributeStore, Domain, DataType, Role, TableBuilder, GeometrySet, assembled, put_attribute, mesh_dispose, set_put_detail, empty_set, dispose_set, mesh_component, BitsBuilder, edge_bits_from_pairs, PointCloud, cloud_assembled, points_component, Curves, curves_assembled, curves_type, Instances, new_instances, instances_component, Placement
from luce_geocore.kernel import set_detail, cloud_store, cloud_positions, cloud_origin, curves_origin, CurveSettings, KnotMode, Bits
from luce_geocore.core.growing import Growing
from luce_cad.cad_geometry import cad_component, set_models, put_models
from luce_cad.model import CadModel, BrepModel, CadTolerances, MeshQuality
from luce_cad.exact import FaceSupport
from luce_geocore.core.shared import Column
from luce_geocore.geocore import Matrix4
from luce_geocore.splats import ColorSpace, Floats, Quat, orient_name, scale_name, opacity_name, color_name, sh_name, restorient_name, color_space_name, color_space_text, detail_color_space, is_splat_store, sh_items, sh_count, sh_degree, float32_points, float64_points, float32_values, float64_values, uniform_sh, color_of_dc, placed_point_store, unbaked_store, point_floats, up_axis_name
## What an import does.
pub struct ImportOptions:
## The time code values resolve at; NaN for the layer's startTimeCode,
## else each attribute's first sample.
pub var time: f64
## Bake instances into the other components ("Flatten everything").
pub var realize: bool
## Purposes kept besides default (usd_geom's purpose bits).
pub var purposes: u8
## Rotate a Z-up stage to Y-up and scale to meters.
pub var convert_units: bool
## Subdivide meshes that author no subdivisionScheme (USD's default is
## Catmull-Clark).
pub var subdivide_unauthored: bool
## Messages about what an import left out or changed, one line each,
## each message once.
pub struct Warnings:
pub var text: Growing[u8]
pub func init():
self.text = Growing[u8]()
pub func close():
self.text.close()
## Add `message` unless it is there already.
pub func add(message: str) -> !:
let held = (str)self.text.view()
var start: usize = 0
while start < held.length:
var end = start
while end < held.length and held.bytes[end] != 10: end += 1
if (str)held.bytes[start..<end] == message: return
start = end + 1
for byte in message.bytes: try self.text.push(byte)
try self.text.push(10)
## Add `message` followed by a path's text.
pub func add_path(message: str, names: const Names*, path: u32) -> !:
var line = Growing[u8]()
defer line.close()
for byte in message.bytes: try line.push(byte)
try names.write_path(path, &line)
try self.add((str)line.view())
## Elements per parallel chunk.
let grain: usize = 65536
## A piece of one prim's elements for a parallel pass.
struct Chunk:
var source: usize
var first: usize
var end: usize
## Split `counts[i]` elements of each source into chunks of `grain`.
func chunks_of(counts: const usize[], out: Growing[Chunk]*) -> !:
for source in 0..<counts.length:
var first: usize = 0
while first < counts[source]:
let end = first + grain if counts[source] - first > grain else counts[source]
try out.push(Chunk(source = source, first = first, end = end))
first = end
## Typed views of a value's packed elements.
func ints_of(value: const Value*) -> const i32[]:
let bytes = value.bytes()
return i32[]((i32*)bytes.data, bytes.length // 4)
## The text of `path` as element `path` attributes hold it: absolute, or
## from `anchor` down ("/Tree/Leaf" for a prototype /Forest/Protos/Tree).
func write_element_path(names: Names*, path: u32, anchor: u32, out: Growing[u8]*) -> !:
out.clear()
if anchor == absolute_root:
try names.write_path(path, out)
return
var parts = Growing[u32]()
defer parts.close()
var at = path
while at != anchor and at != absolute_root and at != empty_path:
try parts.push(names.name(at))
at = names.parent(at)
var index = parts.count
while index > 0:
index -= 1
try out.push('/')
for byte in names.text_of(parts.items[index]).bytes: try out.push(byte)