Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.8

0.11.8 / src / cad_preview.luc

## Cached CAD display data: the models' tessellations (kept in the models)
## and their overlays (analytic boundaries, control hulls and normal
## guides), held as luce-cad's Base objects and placed by `matrix`; the
## overlay points reach the viewport through OverlayLines without passing
## through Luce. A model that cannot be tessellated shows independent
## per-face previews instead. Never replaces the DAG's analytic payload.
from luce_cad.model import CadModel, CadOverlay, CadPreviewSet
from luce_cad.cad_geometry import CadGeometry
from luce_geocore.geocore import Mesh
from luce_3d.graphics import Matrix4
from overlay_lines import OverlayLines

pub class CadPreview:
    pub let meshes: list[Mesh]
    ## The models' overlays, and the per-face previews of models that did
    ## not tessellate, both before `matrix`.
    let overlays: list[CadOverlay]
    let fallbacks: list[CadPreviewSet]
    pub let matrix: Matrix4
    ## Made for a drag's frame: models with moved control vertices may show
    ## quick previews (luce-cad brep/quick.lucb), not full tessellations.
    pub var quick: bool = false
    ## The surface divisions its meshes were made at.
    pub var divisions: int = 16
    pub var failed_faces: int = 0
    pub var first_error: str = ""
    ## Why some faces do not follow their moved control vertices ("": all do).
    pub var cv_warning: str = ""

    ## The preview of `models`; or `source`'s placed by `matrix` (its meshes
    ## placed, its overlays shared).
    pub func init(self, models: list[CadModel], source: CadPreview? = none, matrix: Matrix4? = none, progress: func(float) -> unit!? = none, quick: bool = false, divisions: int = 16) -> unit!:
        self.meshes = []
        self.overlays = []
        self.fallbacks = []
        self.matrix = Matrix4()
        self.quick = quick
        self.divisions = divisions
        if let original = source:
            self.quick = original.quick
            self.divisions = original.divisions
            let placement = matrix else Matrix4()
            self.failed_faces = original.failed_faces
            self.first_error = original.first_error
            self.cv_warning = original.cv_warning
            for mesh in original.meshes:
                self.meshes.append(mesh.placed(placement))
            self.overlays = original.overlays
            self.fallbacks = original.fallbacks
            self.matrix = placement.multiply(original.matrix)
            return
        # A CAD model shows its tessellation at the viewport's quality, kept
        # in the model: a Tessellate node with the same divisions places the
        # same mesh (no copy, no new GPU buffers). A model that cannot be
        # tessellated falls back to independent per-face previews. A failure
        # of `progress` (the worker's cancellation) is the caller's: it ends
        # the preview rather than counting as the model's.
        var stopped: str? = none
        for index in 0..<models.length:
            let model = models[index]
            let report = func (fraction: float) -> unit!:
                if let callback = progress:
                    callback((float(index) + fraction) / float(models.length)) catch failure:
                        stopped = failure.message
                        error(failure.code, failure.message)
            let mesh = model.tessellate(divisions, 0.0, report, model.supports_curvature(), false, quick) catch failure:
                if let message = stopped:
                    error(failure.code, message)
                self.failed_faces += model.face_count()
                if self.first_error == "":
                    self.first_error = failure.message
                self.append_previews(model, report)
                if let message = stopped:
                    error(failure.code, message)
                continue
            self.meshes.append(mesh)
            if self.cv_warning == "":
                self.cv_warning = tessellation_warning(mesh)
            self.overlays.append(model.overlay(mesh))

    ## Independent per-face previews of `model`, in display batches.
    func append_previews(self, model: CadModel, progress: func(float) -> unit!) -> unit!:
        let part = model.preview(progress) catch failure:
            return
        for at in 0..<part.mesh_count():
            self.meshes.append(part.mesh(at))
        self.fallbacks.append(part)

    ## Part `part` of the overlays (0 boundaries, 1 control hulls, 2 normal
    ## guides, 3 the control nets of faces a modeling feature named), placed by `matrix` after this preview's own placement, added
    ## to `lines`.
    pub func lines(self, part: int, matrix: Matrix4, lines: OverlayLines) -> unit!:
        let place = matrix.multiply(self.matrix)
        for overlay in self.overlays:
            lines.add_cad(overlay, part, place)
        for previews in self.fallbacks:
            lines.add_preview(previews, part, place)

    ## Overlay endpoints held (the result store's footprint).
    pub func point_count(self) -> int:
        var total = 0
        for overlay in self.overlays:
            total += overlay.boundary_count() + overlay.hull_count() + overlay.guide_count()
        for previews in self.fallbacks:
            total += previews.boundary_count() + previews.guide_count()
        return total

## What a CAD tessellation could not follow ("" when it followed every
## face): faces no mesher could follow (the first named, and how many more),
## and faces left out because their moved control vertices fold their trims.
pub func tessellation_warning(mesh: Mesh) -> str!:
    let parts: list[str] = []
    let skipped: list[str] = []
    for line in CadGeometry.skipped_faces(mesh).split("\n"):
        if line != "":
            skipped.append(line)
    if skipped.length == 1:
        parts.append(skipped[0])
    elif skipped.length > 1:
        parts.append(f"{skipped[0]} (and {skipped.length - 1} more)")
    let dropped = CadGeometry.dropped_faces(mesh)
    if dropped > 0:
        parts.append(f"{dropped} CAD faces are left out: their moved control vertices fold their trims")
    return parts.join("; ")