Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / model / mesh_quality.lucb

## How finely a model's display (or export) mesh follows its curved
## surfaces (docs/research/KERNEL-ROBUSTNESS.md, "Meshing"): a setting of
## its own, apart from the modeling tolerances (CadTolerances), as Alias's
## tessellation quality is apart from its Construction Options. Nothing in
## it depends on a model's size: a part and its copy scaled by any factor
## mesh alike.
##   angle   the largest angle a mesh edge may span on a curved surface
##           or curve (the turn between the normals at its ends), radians
##   chord   the farthest a mesh may stray from its surfaces, mm (0: none;
##           Alias's and Fusion's chord height or surface deviation)
##   edge    the longest mesh edge, mm (0: none)
## A trim always lies within the positional tolerance P of its edges,
## whatever the quality (the validity contract): that is the only modeling
## tolerance meshing honours.

## Fusion 360's display qualities as its meshes measure (probe 19: a
## sphere's largest edge angle): Low, Normal, High, Very High, in degrees.
pub let low_angle: f64 = 25.9
pub let medium_angle: f64 = 9.43
pub let high_angle: f64 = 4.71
pub let very_high_angle: f64 = 2.36

## The finest angle the mesher takes: a quarter turn in 128 steps.
pub let finest_angle: f64 = 0.703125

## The roundoff of a quarter turn over an angle made from a step count
## (steps): a whole count is not rounded up to the next.
let step_roundoff: f64 = 1e-12

pub struct MeshQuality:
    pub var angle: f64 = 0.16458208140316117
    pub var chord: f64 = 0.0
    pub var edge: f64 = 0.0

    ## Quality presets.
    pub static func low() -> MeshQuality:
        return MeshQuality(low_angle * math.pi / 180.0, 0.0, 0.0)
    pub static func medium() -> MeshQuality:
        return MeshQuality(medium_angle * math.pi / 180.0, 0.0, 0.0)
    pub static func high() -> MeshQuality:
        return MeshQuality(high_angle * math.pi / 180.0, 0.0, 0.0)
    pub static func very_high() -> MeshQuality:
        return MeshQuality(very_high_angle * math.pi / 180.0, 0.0, 0.0)

    ## A quarter turn in `count` steps (the mesher's divisions; a test's
    ## or a tool's fixed step count).
    pub static func steps(count: i64) -> MeshQuality:
        return MeshQuality(math.pi * 0.5 / (f64)count, 0.0, 0.0)

    ## An error unless the angle is from `finest_angle` to 45 degrees and
    ## the chord and the longest edge finite and not negative.
    pub func checked() -> !:
        if not math.is_finite(self.angle) or self.angle < finest_angle * math.pi / 180.0 * 0.999999 or self.angle > math.pi * 0.25:
            error(invalid, "a mesh's angle must be from 0.703125 to 45 degrees")
        if not math.is_finite(self.chord) or self.chord < 0.0: error(invalid, "a mesh's chord height must be finite and not negative")
        if not math.is_finite(self.edge) or self.edge < 0.0: error(invalid, "a mesh's longest edge must be finite and not negative")

    ## The mesher's divisions: steps per quarter turn, so that no step spans
    ## more than the angle.
    pub func divisions() -> i64:
        return (i64)math.max(1.0, math.min(128.0, math.ceil(math.pi * 0.5 / self.angle * (1.0 - step_roundoff))))