dymokomi/luce-usd
Source of release 0.9.2
0.9.2 / src / convert / export_curves.lucb
## Curves written back as BasisCurves and NurbsCurves prims, one prim per
## path and curve class (see export_elements.lucb). Every written vertex
## names the curve point its values come from, so point attributes stay
## vertex primvars: a Bezier handle takes its control point's values, a
## cyclic NURBS curve's repeated points their originals'.
## Curve classes, each written as one prim.
enum CurveClass as u8:
linear = 0
bezier = 1
bspline = 2
nurbs = 3
catmull_rom = 4
let class_suffixes: str[5] = ["_linear", "_bezier", "_bspline", "_nurbs", "_catmullRom"]
## A prim holds one class of curves, open or closed (BasisCurves' wrap is
## per prim): slot class * 2 + closed.
func slot_of(settings: const CurveSettings*) -> usize:
return (usize)(u8)class_of(settings) * 2 + (1 if settings.cyclic else 0)
## One class's vertices being written.
struct CurveWrite:
var counts: Growing[i32]
var sources: Growing[i32]
var positions: Growing[f64]
var cyclic: Growing[bool]
var orders: Growing[i32]
var knots: Growing[f64]
var weights: Growing[f64]
var curves: Growing[i32]
func init():
self.counts = Growing[i32]()
self.sources = Growing[i32]()
self.positions = Growing[f64]()
self.cyclic = Growing[bool]()
self.orders = Growing[i32]()
self.knots = Growing[f64]()
self.weights = Growing[f64]()
self.curves = Growing[i32]()
func close():
self.counts.close()
self.sources.close()
self.positions.close()
self.cyclic.close()
self.orders.close()
self.knots.close()
self.weights.close()
self.curves.close()
## A vertex at `p` (absolute) taking point `source`'s values.
func vertex(p: Vector3, source: usize) -> !:
try self.positions.push(p.x)
try self.positions.push(p.y)
try self.positions.push(p.z)
try self.sources.push((i32)source)
func class_of(settings: const CurveSettings*) -> CurveClass:
match settings.kind:
.poly: return CurveClass.linear
.bezier: return CurveClass.bezier
.catmull_rom: return CurveClass.catmull_rom
_:
if settings.order == 4 and settings.knot_mode == KnotMode.uniform: return CurveClass.bspline
return CurveClass.nurbs
## The set's curves as curve prims.
func export_curves(curves: const Curves*, author: Author*, base: u32, fallback: u32, options: const ExportOptions*, warnings: Warnings*) -> !:
let store = curves.store()
var parts = Growing[ElementPart]()
defer close_element_parts(&parts)
try split_elements(store, Domain.face, curves.curve_count(), author.names, base, fallback, options.split, &parts)
let tokens = try GeomTokens.of(author.names)
for part in parts.view():
var used: usize = 0
var slots: usize = 0
for curve in part.elements.view():
let settings = curves.settings((usize)curve)
let bit = (usize)1 << slot_of(&settings)
if used & bit == 0: slots += 1
used = used | bit
for slot in 0..<(usize)10:
if used & ((usize)1 << slot) == 0: continue
let kind = CurveClass((u8)(slot // 2))
var write = CurveWrite()
defer write.close()
for curve in part.elements.view():
let settings = curves.settings((usize)curve)
if slot_of(&settings) != slot: continue
try add_curve(curves, (usize)curve, &settings, kind, &write)
var path = part.path
if slots > 1:
var name = Growing[u8]()
defer name.close()
try name.assign(author.names.name_text(part.path).bytes)
for byte in class_suffixes[slot // 2].bytes: try name.push(byte)
if slot % 2 == 1:
for byte in "_closed".bytes: try name.push(byte)
path = try author.names.child(author.names.parent(part.path), try author.names.token((str)name.view()))
try write_curve_prim(author, &tokens, path, kind, &write, store, options, warnings)
## One curve's vertices (and knots, weights) in its class's layout.
func add_curve(curves: const Curves*, curve: usize, settings: const CurveSettings*, kind: CurveClass, write: CurveWrite*) -> !:
let (first, count) = curves.range(curve)
let before = write.sources.count
try write.curves.push((i32)curve)
try write.cyclic.push(settings.cyclic)
match kind:
.bezier:
for at in 0..<count:
let point = first + at
try write.vertex(curves.point(point), point)
if at + 1 < count or settings.cyclic:
let next = first + (at + 1) % count
try write.vertex(handle_position(curves, first, count, at, true, settings.cyclic), point)
try write.vertex(handle_position(curves, first, count, (at + 1) % count, false, settings.cyclic), next)
.nurbs:
let order = settings.order
for at in 0..<count: try write.vertex(curves.point(first + at), first + at)
if settings.cyclic:
for at in 0..<order - 1: try write.vertex(curves.point(first + at % count), first + at % count)
try write.orders.push((i32)order)
try append_knots(curves, curve, settings, count, &write.knots)
let written = count + (order - 1 if settings.cyclic else 0)
for at in 0..<written: try write.weights.push(weight_of(curves, first + at % count))
_:
for at in 0..<count: try write.vertex(curves.point(first + at), first + at)
try write.counts.push((i32)(write.sources.count - before))
func weight_of(curves: const Curves*, point: usize) -> f64:
let weights = curves.find("w", Domain.point) else return 1.0
return weights.number(point, 0)
## A Bezier point's handle (right: after it), absolute: the stored
## handle_l/handle_r, or geocore's automatic one.
func handle_position(curves: const Curves*, first: usize, count: usize, at: usize, right: bool, cyclic: bool) -> Vector3:
if let stored = curves.find("handle_r" if right else "handle_l", Domain.point):
let point = first + at
return Vector3(stored.number(point, 0), stored.number(point, 1), stored.number(point, 2)).add(curves_origin(curves))
let p = curves.point(first + at)
var previous = Vector3()
var next = Vector3()
if cyclic:
previous = curves.point(first + (at + count - 1) % count)
next = curves.point(first + (at + 1) % count)
else:
previous = curves.point(first + at - 1) if at > 0 else p.multiply_scalar(2.0).subtract(curves.point(first + 1))
next = curves.point(first + at + 1) if at + 1 < count else p.multiply_scalar(2.0).subtract(curves.point(first + at - 1))
let tangent = next.subtract(previous).multiply_scalar(1.0 / 6.0)
return p.add(tangent) if right else p.subtract(tangent)
## A NURBS curve's knots as geocore evaluates them (custom ones, else
## Blender's normal or endpoint knots), a cyclic curve's repeated tail
## included.
func append_knots(curves: const Curves*, curve: usize, settings: const CurveSettings*, points: usize, out: Growing[f64]*) -> !:
let order = settings.order
let total = points + order + (order - 1 if settings.cyclic else 0)
let custom = curves.custom_knots(curve)
if settings.knot_mode == KnotMode.custom and custom.length == points + order:
for knot in custom: try out.push((f64)knot)
if settings.cyclic:
let shift = (f64)custom[custom.length - 1] - (f64)custom[order - 1]
for at in 0..<order - 1: try out.push((f64)custom[order + at] + shift)
return
let start = out.count
try out.fill(start + total, 0.0)
let values = out.items[start..<start + total]
let endpoint = settings.knot_mode != KnotMode.uniform
let head = (order - (1 if settings.cyclic else 0)) if endpoint else 1
let tail = 2 * order - 1 if settings.cyclic else (order if endpoint else 0)
var repeat = head
var current: f64 = 0.0
var from_at: usize = 0
if endpoint and settings.cyclic:
values[0] = current
current += 1.0
from_at = 1
for at in from_at..<total - tail:
values[at] = current
repeat -= 1
if repeat == 0:
current += 1.0
repeat = 1
let tail_first = total - tail
for at in 0..<tail: values[tail_first + at] = current + (values[at] - values[0])
## One class's prim.
func write_curve_prim(author: Author*, tokens: const GeomTokens*, path: u32, kind: CurveClass, write: const CurveWrite*, store: const AttributeStore*, options: const ExportOptions*, warnings: Warnings*) -> !:
let nurbs = kind == CurveClass.nurbs
let spec = try author.define(path, tokens.nurbs_curves if nurbs else tokens.basis_curves, tokens.xform)
let vertices = write.sources.count
var column = try Column[u8].allocate(vertices * (24 if options.precise else 12))
errdefer column.release()
let out = column.span()
var low: f64[3] = [math.infinity, math.infinity, math.infinity]
var high: f64[3] = [-math.infinity, -math.infinity, -math.infinity]
for at in 0..<vertices * 3:
let value = write.positions.items[at]
low[at % 3] = math.min(low[at % 3], value)
high[at % 3] = math.max(high[at % 3], value)
if options.precise: memory.write[f64]((void*)&out[at * 8], value)
else: memory.write[f32]((void*)&out[at * 4], (f32)value)
let taken = column.take()
_ = try author.attribute(path, spec, "points", "point3d[]" if options.precise else "point3f[]", false, Value.of_array(ValueKind.vec3d if options.precise else ValueKind.vec3f, vertices, taken))
let extent: f32[6] = [(f32)low[0], (f32)low[1], (f32)low[2], (f32)high[0], (f32)high[1], (f32)high[2]]
_ = try author.attribute(path, spec, "extent", "float3[]", false, try array_of(ValueKind.vec3f, 2, u8[]((u8*)&extent[0], 24)))
_ = try author.attribute(path, spec, "curveVertexCounts", "int[]", false, try array_of(ValueKind.int, write.counts.count, u8[]((u8*)write.counts.items.data, write.counts.count * 4)))
if nurbs:
_ = try author.attribute(path, spec, "order", "int[]", false, try array_of(ValueKind.int, write.orders.count, u8[]((u8*)write.orders.items.data, write.orders.count * 4)))
_ = try author.attribute(path, spec, "knots", "double[]", false, try array_of(ValueKind.double, write.knots.count, u8[]((u8*)write.knots.items.data, write.knots.count * 8)))
_ = try author.attribute(path, spec, "pointWeights", "double[]", false, try array_of(ValueKind.double, write.weights.count, u8[]((u8*)write.weights.items.data, write.weights.count * 8)))
else:
let cubic = kind != CurveClass.linear
_ = try author.attribute(path, spec, "type", "token", true, Value.scalar(ValueKind.token, (u64)(try author.names.token("cubic" if cubic else "linear"))))
if cubic:
let basis = "bezier" if kind == CurveClass.bezier else ("bspline" if kind == CurveClass.bspline else "catmullRom")
_ = try author.attribute(path, spec, "basis", "token", true, Value.scalar(ValueKind.token, (u64)(try author.names.token(basis))))
let cyclic = write.cyclic.count > 0 and write.cyclic.items[0]
let wrap = "periodic" if cyclic else ("pinned" if kind == CurveClass.catmull_rom else "nonperiodic")
_ = try author.attribute(path, spec, "wrap", "token", true, Value.scalar(ValueKind.token, (u64)(try author.names.token(wrap))))
try write_element_attributes(author, path, spec, store, Domain.point, write.sources.view(), "vertex", warnings)
try write_element_attributes(author, path, spec, store, Domain.face, write.curves.view(), "uniform", warnings)