Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.1

0.9.1 / src / tests / normal_scale_contract.lucb

## Surface-normal directions must not depend on model, knot or weight units.
from luce_std import math
from luce_geocore.geocore import Vector3, invalid
from tessellation.surface import surface_normal_checked, NurbsSurface

pub func normal_scale_contract() -> !:
    for scale in [1e-9,1e-5,1.0,1e5]:
        for domain in [1e-4,1.0,1e4]:
            for weight in [1e-10,1.0,1e10]:
                for translated in [false,true]:
                    let origin = Vector3(100.0,-200.0,400.0).multiply_scalar(scale) if translated else Vector3()
                    let basis = [Vector3(1.0,0.0,0.0),Vector3(1.0,0.0,2.0),Vector3(1.0,1.0,0.0),Vector3(1.0,1.0,2.0),Vector3(0.0,1.0,0.0),Vector3(0.0,1.0,2.0)]
                    var points = basis
                    for at in 0..<(usize)6: points[at] = origin.add(basis[at].multiply_scalar(scale))
                    let weights = [weight,weight,weight*math.sqrt(0.5),weight*math.sqrt(0.5),weight,weight]
                    let ku = [7.0*domain,7.0*domain,7.0*domain,11.0*domain,11.0*domain,11.0*domain]
                    let kv = [-2.0*domain,-2.0*domain,3.0*domain,3.0*domain]
                    try NurbsSurface.check(points,weights,3,2,2,1,ku,kv)
                    for fraction in [0.0,0.25,0.5,0.75,1.0]:
                        for vertical in [0.0,0.5,1.0]:
                            let u = ku[2]+(ku[3]-ku[2])*fraction
                            let v = kv[1]+(kv[2]-kv[1])*vertical
                            let n = try surface_normal_checked(points,weights,3,2,2,1,ku,kv,u,v)
                            let p = (try NurbsSurface.at(points,weights,3,2,2,1,ku,kv,u,v)).subtract(origin).multiply_scalar(1.0/scale)
                            if n.subtract(Vector3(p.x,p.y,0.0)).length() > 1e-10:
                                error(invalid,"rational normal depends on spatial, knot or homogeneous weight scale")
    for scale in [1e-9,1.0,1e9]:
        let origin = Vector3(25.0,-20.0,10.0).multiply_scalar(scale)
        let points = [origin,origin,origin.add(Vector3(scale,0.0,0.0)),origin.add(Vector3(scale,scale,0.0))]
        let knots = [0.0,0.0,1.0,1.0]
        for v in [0.0,0.5,1.0]:
            for u in [0.0,1e-8,0.5,1.0]:
                let n = try surface_normal_checked(points,[1.0,1.0,1.0,1.0],2,2,1,1,knots,knots,u,v)
                let expected = Vector3() if u == 0.0 else Vector3(0.0,0.0,1.0)
                if n.subtract(expected).length() > 1e-12:
                    error(invalid,"a collapsed edge is singular, but its nearby regular surface has a unit normal")