dymokomi/luce-fbx
Source of release 0.12.2
0.12.2 / src / convert / curves.lucb
## Line and NurbsCurve geometries as the curves component:
##
## - a Line's `Points` (f64 triples) joined by `PointsIndex` (a run ends at
## a negative ~index, as polygons do) are poly curves, one per run;
## - a NurbsCurve's `Points` (x, y, z, w) with its `Order` and `KnotVector`
## is a NURBS curve with custom knots and point weights `w`. A closed
## curve wraps its first point after its last, a periodic one its first
## order - 1 points (ufbx's num_wrap_control_points); with those repeated
## and the whole knot vector (points + wraps + order knots) it evaluates
## as the same open curve over the knots' inner range.
##
## Each curve's model path is its `path`; points are placed in world space.
## One model placing one curve geometry.
struct CurveUse:
var node: u32
var nurbs: bool
var matrix: Matrix4
var path: i32
## Decoded: Points, and PointsIndex (Line) or KnotVector (NURBS).
var points: f64[]
var index: i32[]
var knots: f64[]
var order: i64
## Points the curve repeats from its start (Form Closed: 1, Periodic:
## order - 1).
var wrap: usize
func release():
if self.points.length > 0: free(self.points) in memory.heap
if self.index.length > 0: free(self.index) in memory.heap
if self.knots.length > 0: free(self.knots) in memory.heap
## geocore's NURBS orders.
let most_order: i64 = 8
## The curve geometries models place: their arrays planned for decoding.
func gather_curves(scene: const Scene*, hierarchy: const Hierarchy*, options: const ImportOptions*, paths: TableBuilder*, uses: Growing[CurveUse]*, jobs: Growing[ArrayJob]*, warnings: Warnings*) -> !:
let document = scene.document
let convert = conversion(&scene.settings) if options.convert_units else Matrix4()
for model in 1..<(u32)hierarchy.models.count:
let entry = &hierarchy.models.items[(usize)model]
# Curve geometries connect to their model; FBX 6 may also keep the
# arrays in the Model itself (candidate 0 then).
let own: usize = 1 if scene.legacy else 0
for index in 0..<own + scene.child_count(entry.object):
var child = entry.object
if index >= own:
let connection = scene.child_connection(entry.object, index - own)
if connection.property != absent: continue
child = connection.child
if scene.kind(child) != Kind.geometry: continue
let subclass = scene.subclass(child)
if subclass != "Line" and subclass != "NurbsCurve": continue
let node = scene.objects.items[(usize)child].node
let nurbs = subclass == "NurbsCurve"
let points = array_node(document, node, "Points") else continue
let second = array_node(document, node, "KnotVector" if nurbs else "PointsIndex") else continue
let order = document.child_integer(node, "Order", 4) catch failure:
_ = failure
recover 4
if nurbs and (order < 2 or order > most_order):
try warnings.add("Skipped NURBS curves of an order geocore does not draw (2 to 8)")
continue
var wrap: usize = 0
if nurbs:
if let form = document.child(node, "Form"):
let text = document.text(form, 0) else ""
if text == "Closed": wrap = 1
elif text == "Periodic": wrap = (usize)order - 1
var use = CurveUse(node = node, nurbs = nurbs, matrix = convert.multiply(entry.geometry_world), path = try paths.intern(hierarchy.path(model)), points = empty_reals(), index = empty_ints(), knots = empty_reals(), order = order, wrap = wrap)
errdefer use.release()
use.points = try new f64[document.array_count(points)] --- in memory.heap
try jobs.push(ArrayJob(points, Element.real64, bytes_of[f64](use.points)))
if nurbs:
use.knots = try new f64[document.array_count(second)] --- in memory.heap
try jobs.push(ArrayJob(second, Element.real64, bytes_of[f64](use.knots)))
else:
use.index = try new i32[document.array_count(second)] --- in memory.heap
try jobs.push(ArrayJob(second, Element.int32, bytes_of[i32](use.index)))
try uses.push(use)
## One output curve.
struct CurveRow:
var nurbs: bool
var order: i64
var path: i32
## The decoded curve uses as one Curves, or none.
func curves_of(uses: const CurveUse[], paths: const TableBuilder*, warnings: Warnings*) -> Curves*?!:
var points = Growing[Vector3]()
defer points.close()
var weights = Growing[f32]()
defer weights.close()
var counts = Growing[i32]()
defer counts.close()
var rows = Growing[CurveRow]()
defer rows.close()
var knots = Growing[f32]()
defer knots.close()
var knot_counts = Growing[i32]()
defer knot_counts.close()
var any_nurbs = false
for use in uses:
if use.nurbs:
let unique = use.points.length // 4
let count = unique + use.wrap
if unique < 2 or use.wrap > unique or use.knots.length != count + (usize)use.order or (i64)count < use.order:
try warnings.add("Skipped NURBS curves whose knots do not match their points and order")
continue
for point in 0..<count:
let at = (point % unique) * 4
try points.push(use.matrix.transform_point(Vector3(use.points[at], use.points[at + 1], use.points[at + 2])))
try weights.push((f32)use.points[at + 3])
for knot in use.knots: try knots.push((f32)knot)
try counts.push((i32)count)
try knot_counts.push((i32)use.knots.length)
try rows.push(CurveRow(nurbs = true, order = use.order, path = use.path))
any_nurbs = true
continue
# A Line: one poly curve per run of PointsIndex.
let available = use.points.length // 3
var run: usize = 0
for value in use.index:
let point = (usize)(-(i64)value - 1 if value < 0 else (i64)value)
if point >= available:
try warnings.add("Skipped line points past a line's Points")
else:
try points.push(use.matrix.transform_point(Vector3(use.points[point * 3], use.points[point * 3 + 1], use.points[point * 3 + 2])))
try weights.push(1.0)
run += 1
if value < 0:
try close_run(&points, &weights, &counts, &knot_counts, &rows, run, use.path)
run = 0
try close_run(&points, &weights, &counts, &knot_counts, &rows, run, use.path)
if rows.count == 0: return none
return try assemble_curves(points.view(), weights.view(), counts.view(), knots.view(), knot_counts.view(), rows.view(), any_nurbs, paths)
## End a Line run: a poly curve when it has two points, else its points go.
func close_run(points: Growing[Vector3]*, weights: Growing[f32]*, counts: Growing[i32]*, knot_counts: Growing[i32]*, rows: Growing[CurveRow]*, run: usize, path: i32) -> !:
if run >= 2:
try counts.push((i32)run)
try knot_counts.push(0)
try rows.push(CurveRow(nurbs = false, order = 2, path = path))
return
points.count -= run
weights.count -= run
## The Curves of gathered rows: f32 points over an origin, the settings
## per curve, weights and paths.
func assemble_curves(points: const Vector3[], weights: const f32[], counts: const i32[], knots: const f32[], knot_counts: const i32[], rows: const CurveRow[], any_nurbs: bool, paths: const TableBuilder*) -> Curves*!:
var low = Vector3(math.infinity, math.infinity, math.infinity)
var high = Vector3(-math.infinity, -math.infinity, -math.infinity)
for p in points:
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 origin = origin_for(low, high)
var positions = try Column[Float3].allocate(points.length)
errdefer positions.release()
let out = positions.span()
for (at, p) in points.indexed(): out[at] = Float3((f32)(p.x - origin.x), (f32)(p.y - origin.y), (f32)(p.z - origin.z))
var offsets = try Column[i32].allocate(counts.length + 1)
errdefer offsets.release()
let starts = offsets.span()
starts[0] = 0
for (at, count) in counts.indexed(): starts[at + 1] = starts[at] + count
var knot_column = try Column[f32].copy_of(knots)
errdefer knot_column.release()
var knot_offsets = try Column[i32].allocate(counts.length + 1 if any_nurbs else 0)
errdefer knot_offsets.release()
if any_nurbs:
let knot_starts = knot_offsets.span()
knot_starts[0] = 0
for (at, count) in knot_counts.indexed(): knot_starts[at + 1] = knot_starts[at] + count
let store = try curve_store(rows, weights, any_nurbs, paths)
# curves_assembled takes every column and the store, even on failure.
return try curves_assembled(positions.take(), origin, offsets.take(), knot_column.take(), knot_offsets.take(), store)
## The curves' attributes: settings per curve, weights, paths.
func curve_store(rows: const CurveRow[], weights: const f32[], any_nurbs: bool, paths: const TableBuilder*) -> AttributeStore!:
var store = AttributeStore()
errdefer store.close()
try put_curve_rows(&store, rows)
if any_nurbs:
var attribute = try Attribute.create("w", Domain.point, DataType.float32, 1, weights.length)
let values = f32[]((f32*)attribute.data.span().data, weights.length)
memory.copy(values, weights, weights.length)
try store.put(attribute)
var path_values = Growing[i32]()
defer path_values.close()
for row in rows: try path_values.push(row.path)
var table = try paths.finish()
defer table.release()
try store.put(try Attribute.text("path", Domain.face, rows.length, table, path_values.view()))
return store
## Per curve: its type (0 poly, 2 NURBS), order, knot mode (2 custom for
## NURBS) and cyclic (never: periodic FBX curves keep their wrapped points).
func put_curve_rows(store: AttributeStore*, rows: const CurveRow[]) -> !:
var kinds = try Attribute.create("curve_type", Domain.face, DataType.int8, 1, rows.length)
for (at, row) in rows.indexed(): kinds.data.span()[at] = 2 if row.nurbs else 0
try store.put(kinds)
var orders = try Attribute.create("order", Domain.face, DataType.int8, 1, rows.length)
for (at, row) in rows.indexed(): orders.data.span()[at] = (u8)row.order
try store.put(orders)
var modes = try Attribute.create("knot_mode", Domain.face, DataType.int8, 1, rows.length)
for (at, row) in rows.indexed(): modes.data.span()[at] = 2 if row.nurbs else 1
try store.put(modes)