Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / tests / editor / cad_display_tests.luc

## CAD in the viewport: a File's CAD shows the model's kept tessellation
## (a Tessellate node with the same settings places the same mesh), its
## edges and normal guides, and its control nets as hulls.
from luce_cad.model import BrepModel, CadModel
from luce_geocore.geocore import Vector3
from geometry_data import cad_data
from scene_projection import append_cad_hulls, append_patch_boundaries
from luce_3d.graphics import Matrix4
from cad_preview import CadPreview
from overlay_lines import OverlayLines
from luce_ui import ui

## A raised B-spline dome over a planar cap: a closed solid.
pub func dome_model() -> CadModel!:
    let b = BrepModel()
    let knots = [0.0, 0.0, 0.0, 1.0, 1.0, 1.0]
    let points: list[Vector3] = []
    for u in 0..<3:
        for v in 0..<3:
            points.append(Vector3(float(u), float(v), 4.0 if u == 1 and v == 1 else 0.0))
    for p in [points[0], points[6], points[8], points[2]]: _ = b.add_vertex(p)
    for edge in 0..<4: _ = b.add_line(edge, (edge + 1) % 4)
    let dome = b.add_spline_surface(points, [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], 3, 3, 2, 2, knots, knots)
    _ = b.add_face(dome, [1, 2, 3, 4], [4])
    let plane = b.add_surface(0, Vector3(), Vector3(0.0, 0.0, 1.0), Vector3(1.0, 0.0, 0.0))
    _ = b.add_face(plane, [-4, -3, -2, -1], [4], false)
    let model = CadModel()
    model.attach_brep(b)
    return model

## Segments of overlay part `part` (0 edges, 1 hull, 2 normal guides).
func count(preview: CadPreview, part: int) -> int!:
    let lines = OverlayLines.make()
    preview.lines(part, Matrix4(), lines)
    return lines.count()

pub func cad_display_tests() -> unit!:
    let data = cad_data(dome_model())
    let preview = data.preview()
    assert(preview.meshes.length == 1 and preview.failed_faces == 0)
    # The four straight edges as the mesh samples them (at least a segment
    # each), two guides, the 3x3 net's 12 lines.
    assert(count(preview, 0) >= 4 and count(preview, 2) == 2 and count(preview, 1) == 12)
    # A Tessellate node at the display's settings places the shown mesh.
    let tessellated = data.tessellated(16)
    assert((tessellated.mesh else trap("mesh")).points_id() == preview.meshes[0].points_id())
    # The worker's overlays: edges and the hull, placed.
    let hulls = OverlayLines.make()
    append_cad_hulls(data, Matrix4(), hulls)
    let edges = OverlayLines.make()
    append_patch_boundaries(data, Matrix4(), edges)
    assert(hulls.count() == 12 and edges.count() >= 4)
    # The worker's cancellation (its progress callback failing) ends the
    # preview with that failure: no failed faces, nothing kept.
    let cancelled = cad_data(dome_model())
    var message = ""
    let stop = func (fraction: float) -> unit!:
        error(ui.invalid, "Computation superseded")
    _ = cancelled.preview(stop) catch failure:
        message = failure.message
        recover preview
    assert(message == "Computation superseded")
    let shown = cancelled.preview()
    assert(shown.meshes.length == 1 and shown.failed_faces == 0 and shown.first_error == "")
    print("PASS CAD display: kept tessellation shown and placed by Tessellate, edges, normal guides and control hulls; a cancelled preview fails whole")