dymokomi/luce-cad
Source of release 0.9.3
## Trim stages (surfacing Stage 3: Alias keeps a surface's trims as a stack
## of stages, which Untrim Last and Untrim All pop). A set's stage
## component holds, for each trim made in it, the face's name and the face
## as it was before (a one-face body, placed with the set): Untrim puts the
## latest (or the first) back in place of the face's pieces. A face trimmed
## with no stage kept (an imported trimmed face) untrims to its natural
## surface instead (luce_cad.modeling's natural_face).
##
## The component places, joins and filters with the set (a stage follows
## its face's name as an object path), traces its bodies like the CAD
## component, and is saved in geometry files (element `cad_trims`, a
## `cad_model` child per stage, the face's name its face's).
import interop
import memory
import ownership
from luce_std import math
from luce_geocore.geocore import Vector3, Matrix4, Bounds, invalid, GeometrySet
from luce_geocore.core.growing import Growing
from luce_geocore.kernel import ComponentType, Placement, IdSet, TableBuilder, PathFilter
from luce_geocore.kernel import adopt_set, dispose_set, append_text, append_number
from luce_geocore.kernel import register_codec, GeometryWriter, GeometryReader
from geometry import push_decimal
from model import CadModel, encode_model, decode_model
## One stage: the face's name and the face before the trim.
struct Stage:
var face: Growing[u8]
var model: interop.Reference[CadModel]
func close():
self.face.close()
self.model.release()
## A set's stages, oldest first: immutable once published; each owner has
## its own list of references (as CadParts: the cycle collector traces each
## owner's references once).
pub struct StageList:
var stages: Growing[Stage]
func add(face: str, model: interop.Reference[CadModel]) -> !:
var name = Growing[u8]()
errdefer name.close()
try name.assign(face.bytes)
let owner = model.clone()
self.stages.push(Stage(name, owner)) catch failure:
owner.release()
error(failure.code, failure.message)
func new_list() -> StageList*!:
let storage = try new StageList[1] --- in memory.heap
storage[0].stages = Growing[Stage]()
return storage.data
pub let trim_component: ComponentType = ComponentType("cad_trims", share_list, close_list, list_size, list_bytes, list_bounds, place_list, join_lists, list_paths, filter_list, describe_list,
"", trace_list)
func share_list(data: const void*) -> void*!:
let copy = try new_list()
errdefer close_list((void*)copy)
for s in ((const StageList*)data).stages.view(): try copy.add((str)s.face.view(), s.model)
return (void*)copy
func close_list(data: void*):
let list = (StageList*)data
for at in 0..<list.stages.count: list.stages.items[at].close()
list.stages.close()
free(StageList[](list, 1)) in memory.heap
func list_size(data: const void*) -> usize:
return ((const StageList*)data).stages.count
func list_bytes(data: const void*, ids: IdSet*) -> usize!:
var total: usize = 64
for s in ((const StageList*)data).stages.view():
total += try ids.charge((u64)(usize)s.model.value(), (usize)s.model.value().face_count() * 16384)
return total
## Stages are not drawn: no bounds.
func list_bounds(data: const void*, matrix: Matrix4, out: Bounds*) -> bool!:
_ = data
_ = matrix
_ = out
return false
func place_list(data: const void*, placement: const Placement*) -> void*!:
let placed = try new_list()
errdefer close_list((void*)placed)
for s in ((const StageList*)data).stages.view():
let moved = try s.model.value().transformed(placement.translation, placement.rotation, placement.scale)
defer moved.release()
try placed.add((str)s.face.view(), moved)
return (void*)placed
func join_lists(first: const void*, second: const void*) -> void*!:
let joined = try new_list()
errdefer close_list((void*)joined)
for s in ((const StageList*)first).stages.view(): try joined.add((str)s.face.view(), s.model)
for s in ((const StageList*)second).stages.view(): try joined.add((str)s.face.view(), s.model)
return (void*)joined
## Stages name no objects of their own.
func list_paths(data: const void*, out: TableBuilder*) -> !:
_ = data
_ = out
## A stage stays with its face: kept when the filter keeps the face's name.
func filter_list(data: const void*, filter: const PathFilter*, keep: bool) -> void*!:
let filtered = try new_list()
errdefer close_list((void*)filtered)
for s in ((const StageList*)data).stages.view():
let selected = filter.is_empty() or filter.matches((str)s.face.view())
if selected == keep: try filtered.add((str)s.face.view(), s.model)
return (void*)filtered
func describe_list(data: const void*, out: Growing[u8]*) -> !:
let count = ((const StageList*)data).stages.count
try append_number(out, count)
try append_text(out, " trim stage" if count == 1 else " trim stages")
func trace_list(data: const void*, visit: func(ownership.Object*, void*) -> unit, context: void*):
for s in ((const StageList*)data).stages.view(): s.model.trace(visit, context)
# -- Geometry files: element `cad_trims`, a `cad_model` child per stage
# (its one face carries the face's name).
func encode_list(data: const void*, writer: GeometryWriter*, path: str) -> !:
let list = (const StageList*)data
try writer.crate.element(path, "cad_trims")
try writer.crate.integer("count", (i64)list.stages.count)
var base = Growing[u8]()
defer base.close()
try base.assign(path.bytes)
var name = Growing[u8]()
defer name.close()
for index in 0..<list.stages.count:
try name.assign("s".bytes)
try push_decimal(&name, (i64)index)
try encode_model(list.stages.items[index].model.value(), writer, try writer.child((str)base.view(), (str)name.view()))
func decode_list(reader: GeometryReader*, path: str) -> void*!:
let list = try new_list()
errdefer close_list((void*)list)
var base = Growing[u8]()
defer base.close()
try base.assign(path.bytes)
while let header = try reader.next_property(): _ = header
while let child = try reader.next_child((str)base.view()):
let model = try decode_model(reader, child.0)
defer model.release()
let face = try model.value().face_name(0) if model.value().face_count() > 0 else ""
try list.add(face, model)
return (void*)list
pub func register_trims() -> !:
try register_codec(&trim_component, encode_list, decode_list)
## `set`'s stage list, or none.
pub func stages_of(set: const GeometrySet*) -> const StageList*?:
let data = set.find(&trim_component) else return none
return (const StageList*)data
## How many stages `set` keeps for face `face`.
pub func stage_count(set: const GeometrySet*, face: str) -> usize:
let list = stages_of(set) else return 0
var count: usize = 0
for s in list.stages.view():
if (str)s.face.view() == face: count += 1
return count
## Face `face`'s stage: its last (`last`) or its first; none when it has none.
pub func stage_model(set: const GeometrySet*, face: str, last: bool) -> interop.Reference[CadModel]?:
let list = stages_of(set) else return none
var found: interop.Reference[CadModel]? = none
for s in list.stages.view():
if (str)s.face.view() != face: continue
if found == none or last: found = s.model
if let model = found: return model.clone()
return none
## `set` with a stage for `face` pushed (`model`: the face before a trim),
## or with `face`'s stages popped (`pop` > 0: the last one; all of them with
## `pop` < 0).
pub func with_stage(set: const GeometrySet*, face: str, model: interop.Reference[CadModel]?, pop: i64) -> interop.Reference[GeometrySet]!:
let list = try new_list()
var popped: i64 = 0
if let existing = stages_of(set):
# Popping the last: find it first.
var last: i64 = -1
for at in 0..<existing.stages.count:
if (str)existing.stages.items[at].face.view() == face: last = (i64)at
for at in 0..<existing.stages.count:
let s = &existing.stages.items[at]
let mine = (str)s.face.view() == face
if mine and (pop < 0 or (pop > 0 and (i64)at == last)):
popped += 1
continue
list.add((str)s.face.view(), s.model) catch failure:
close_list((void*)list)
error(failure.code, failure.message)
_ = popped
if let pushed = model:
list.add(face, pushed) catch failure:
close_list((void*)list)
error(failure.code, failure.message)
let result = set.storage() catch failure:
close_list((void*)list)
error(failure.code, failure.message)
result.remove(&trim_component)
if list.stages.count == 0:
close_list((void*)list)
else:
result.put(&trim_component, (void*)list) catch failure:
close_list((void*)list)
dispose_set(result)
error(failure.code, failure.message)
return try adopt_set(result)