Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / src / model / cv_cloud.lucb

## CAD control vertices as a point cloud, so geocore's point verbs edit them.
## The cloud holds every cv of a set's models, placed (model placement, then
## the surface's face placement), in model then cv order, with no faces;
## a hidden detail attribute marks it. A point verb (Transform Components,
## Mountain, Flatten, Snap) runs on the cloud, and the cvs whose points it
## moved go back into copies of the models: only those, so every other cv
## keeps its exact f64 position and the per-face tessellation cache reuses
## every face they do not reach. A moved cv takes its exact old position
## plus the cloud point's change.
##
## Each point carries its surface's frame at its Greville point
## (`cad_cv_normal`, `cad_cv_du`, `cad_cv_dv`), and where it is in its net: `cad_cv_model` (the model's
## index in the set), `cad_cv_surface` (its surface), `cad_cv_u` and
## `cad_cv_v` (its row and column) and `cad_cv_nu` and `cad_cv_nv` (the
## net's size), so a pick can take a whole row (Alias's hull picking) and
## a falloff can count rows.

## The detail attribute that marks a cv cloud.
let cloud_marker = ".cad_cvs"

## The cloud of `models`' cvs, then `extra_points` (a set's curves' cvs,
## cad_curves.lucb) with their places (six numbers a point, as
## net_places) and frames (three vectors a point).
pub func cloud_mesh(models: const interop.Reference[CadModel][], extra_points: const Vector3[], extra_places: const f64[], extra_frames: const Vector3[]) -> interop.Reference[Mesh]!:
    var points = Growing[Vector3]()
    defer points.close()
    for model in models: try placed_cvs(model.value(), &points)
    for point in extra_points: try points.push(point)
    var builder = try MeshBuilder()
    defer builder.close()
    for point in points.view(): _ = try builder.point(point)
    let bare = try builder.finish()
    defer bare.release()
    var places = Growing[f64]()
    defer places.close()
    for at in 0..<models.length: try net_places(models[at].value(), at, &places)
    for value in extra_places: try places.push(value)
    var frames = Growing[Vector3]()
    defer frames.close()
    for model in models: try placed_cv_frames(model.value(), &frames)
    for frame in extra_frames: try frames.push(frame)
    if places.count != points.count * 6 or frames.count != points.count * 3: error(invalid, "a control vertex cloud's places do not match its points")
    var named = bare.clone()
    let names: str[6] = ["cad_cv_model", "cad_cv_surface", "cad_cv_u", "cad_cv_v", "cad_cv_nu", "cad_cv_nv"]
    var column = Growing[f64]()
    defer column.close()
    for k in 0..<(usize)6:
        column.clear()
        for at in 0..<points.count: try column.push(places.items[at * 6 + k])
        let next = named.value().with_attribute(names[k], 0, 1, column.view()) catch failure:
            named.release()
            error(failure.code, failure.message)
        named.release()
        named = next
    # The surface frames NUV moves along (Alias's): normal, u and v.
    let frame_names: str[3] = ["cad_cv_normal", "cad_cv_du", "cad_cv_dv"]
    for k in 0..<(usize)3:
        column.clear()
        for at in 0..<points.count:
            let f = frames.items[at * 3 + k]
            try column.push(f.x)
            try column.push(f.y)
            try column.push(f.z)
        let next = named.value().with_attribute(frame_names[k], 0, 3, column.view()) catch failure:
            named.release()
            error(failure.code, failure.message)
        named.release()
        named = next
    defer named.release()
    var marker = try Attribute.create(cloud_marker, Domain.detail, DataType.int32, 1, 1)
    return named.value().with_attribute_entry(marker) catch failure:
        marker.release()
        error(failure.code, failure.message)

## Model `index`'s cvs' places (model, surface, u, v, nu, nv), six numbers
## a cv, in cv order, onto `out`.
func net_places(model: const CadModel*, index: usize, out: Growing[f64]*) -> !:
    let b = model.brep() else return
    for surface in 0..<b.surface_count():
        var record = try b.surface_record(surface)
        defer record.close()
        if record.kind != surface_spline: continue
        for u in 0..<record.nu:
            for v in 0..<record.nv:
                try out.push((f64)index)
                try out.push((f64)surface)
                try out.push((f64)u)
                try out.push((f64)v)
                try out.push((f64)record.nu)
                try out.push((f64)record.nv)

## Whether `mesh` is a cv cloud.
pub func is_cloud(mesh: const Mesh*) -> bool:
    return mesh.face_count() == 0 and mesh.find_attribute(cloud_marker, (i64)(u8)Domain.detail) >= 0

## `models` with the cvs `after` moved relative to `before` (both clouds of
## `models`, perhaps with a set's curves' cvs after theirs), into `out`; a
## model none of whose cvs moved is shared. Returns where the models'
## cvs end in the cloud.
pub func moved_models(models: const interop.Reference[CadModel][], before: const Mesh*, after: const Mesh*, out: Growing[interop.Reference[CadModel]]*) -> usize!:
    let old_points = before.point_span()
    let new_points = after.point_span()
    if old_points.length != new_points.length: error(invalid, "the control vertex cloud changed its point count")
    let old_origin = before.position_origin()
    let new_origin = after.position_origin()
    let same_origin = old_origin.subtract(new_origin).length() == 0.0
    # Without a shared origin, points moved by less than float rounding stay.
    let bounds = before.bounds()
    let slack = 0.0 if same_origin else bounds.maximum.subtract(bounds.minimum).length() * 0.000001
    var ids = Growing[i64]()
    defer ids.close()
    var placed = Growing[Vector3]()
    defer placed.close()
    var exact = Growing[Vector3]()
    defer exact.close()
    var first: usize = 0
    for model in models:
        let count = (usize)model.value().cv_count()
        if first + count > old_points.length: error(invalid, "the control vertex cloud does not match its models")
        ids.clear()
        placed.clear()
        exact.clear()
        try placed_cvs(model.value(), &exact)
        for at in first..<first + count:
            let was = old_points[at]
            let now = new_points[at]
            if same_origin and was.x == now.x and was.y == now.y and was.z == now.z: continue
            let change = now.vector().add(new_origin).subtract(was.vector().add(old_origin))
            if change.length() <= slack: continue
            let cv = (i64)(at - first)
            try ids.push(cv)
            try placed.push(exact.items[at - first].add(change))
        first += count
        let result = (try model.value().with_cvs_placed(ids.view(), placed.view())) if ids.count > 0 else model.clone()
        out.push(result) catch failure:
            result.release()
            error(failure.code, failure.message)
    if first > old_points.length: error(invalid, "the control vertex cloud does not match its models")
    return first