Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / seam_flow_tests.luc

## Row transport stops at creases and incompatible smooth parameter frames.
from luce_cad.model import BrepModel
from luce_geocore.geocore import Vector3, Mesh
from checks import near, manifold
from parallel_cad_tests import planned_matches
from luce_std import math

func fixture(hard: bool, angle: float) -> BrepModel!:
    let b = BrepModel()
    let corners = [Vector3(),Vector3(1.0,0.0,0.0),Vector3(1.0,1.0,0.0),Vector3(0.0,1.0,0.0),
                   Vector3(1.0,0.0,1.0) if hard else Vector3(2.0,0.0,0.0),
                   Vector3(1.0,1.0,1.0) if hard else Vector3(2.0,1.0,0.0)]
    for p in corners: _ = b.add_vertex(p)
    for at in 0..<3: _ = b.add_line(at,at+1)
    let knots = [0.0,0.0,0.0,1.0,1.0,1.0]
    _ = b.add_spline_edge(3,0,[corners[3],Vector3(0.0,0.5,0.5),corners[0]],[1.0,1.0,1.0],2,knots,true)
    for pair in [[1,4],[4,5],[5,2]]: _ = b.add_line(pair[0],pair[1])
    # The interior needs rows, but the common boundary is exactly straight.
    # The squared falloff gives a truly G1 join to the horizontal plane.
    let controls: list[Vector3] = []
    for u in 0..<3:
        for v in 0..<3:
            controls.append(Vector3(float(u)*0.5,float(v)*0.5,0.5 if u == 0 and v == 1 else 0.0))
    _ = b.add_spline_surface(controls,[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_surface(0,corners[1],Vector3(-1.0,0.0,0.0) if hard else Vector3(0.0,0.0,1.0),
                          Vector3(0.0,0.0,1.0) if hard else Vector3(math.cos(angle),math.sin(angle),0.0))
    _ = b.add_face(0,[1,2,3,4],[4])
    _ = b.add_face(1,[5,6,7,-2],[4])
    return b

func check(mesh: Mesh, flows: bool) -> unit!:
    let parent = mesh.find_attribute("cad_face",2)
    let island = mesh.find_attribute("cad_flow_island",2)
    assert(parent >= 0 and island >= 0)
    var right_faces = 0
    var boundary_corners = 0
    for face in 0..<mesh.face_count():
        let side = mesh.attribute_value(parent,face,0)
        near(mesh.attribute_value(island,face,0),0.0 if flows else side)
        if side != 1.0: continue
        right_faces += 1
        if not flows:
            boundary_corners = mesh.face_size(face)
            assert(boundary_corners > 8)
        else: assert(mesh.face_size(face) == 4)
    assert(right_faces > 8 if flows else right_faces == 1)
    let uses: list[int] = []
    for edge in 0..<mesh.edge_count(): uses.append(0)
    for face in 0..<mesh.face_count():
        for at in 0..<mesh.face_size(face): uses[mesh.face_edge(face,at)] += 1
    var segments = 0
    for edge in 0..<mesh.edge_count():
        let a = mesh.point(mesh.edge_point(edge,0))
        let b = mesh.point(mesh.edge_point(edge,1))
        if math.abs(a.x-1.0)+math.abs(b.x-1.0)+math.abs(a.z)+math.abs(b.z) > 0.000001: continue
        assert(uses[edge] == 2)
        segments += 1
    assert(segments >= 8)
    if not flows: assert(boundary_corners == segments+3)
    for triangle in 0..<mesh.index_count()//3:
        let a = mesh.vertex(mesh.index(triangle*3))
        let b = mesh.vertex(mesh.index(triangle*3+1))
        let c = mesh.vertex(mesh.index(triangle*3+2))
        assert(b.position.subtract(a.position).cross(c.position.subtract(a.position)).dot(a.normal) > 0.0)

pub func seam_flow_tests() -> unit!:
    checked_case(true,0.0,false)
    checked_case(false,math.pi/4.0,false)
    checked_case(false,math.pi/2.0,true)
    checked_case(false,0.0,true)
    curved_strip()
    print("PASS hard-seam boundary n-gons, rotated G1 flow stops, 90-degree row transport and canonical stitching")

func checked_case(hard: bool, angle: float, flows: bool) -> unit!:
    let b = fixture(hard,angle)
    let mesh = b.mesh(8)
    check(mesh,flows)
    for selected in [0,1]: planned_matches(b,mesh,selected,8,0.0)

func curved_strip() -> unit!:
    let b = BrepModel()
    let points = [Vector3(),Vector3(1.0,0.0,0.0),Vector3(0.0,0.0,1.0),Vector3(1.0,0.0,1.0)]
    for p in points: _ = b.add_vertex(p)
    let quadratic = [0.0,0.0,0.0,1.0,1.0,1.0]
    _ = b.add_spline_edge(0,1,[points[0],Vector3(0.5,0.5,0.0),points[1]],[1.0,1.0,1.0],2,quadratic,true)
    _ = b.add_line(1,3)
    _ = b.add_spline_edge(2,3,[points[2],Vector3(0.5,0.5,1.0),points[3]],[1.0,1.0,1.0],2,quadratic,true)
    _ = b.add_line(2,0)
    _ = b.add_line(0,1)
    _ = b.add_line(2,3)
    let controls = [points[0],points[2],Vector3(0.5,0.5,0.0),Vector3(0.5,0.5,1.0),points[1],points[3]]
    _ = b.add_spline_surface(controls,[1.0,1.0,1.0,1.0,1.0,1.0],3,2,2,1,quadratic,[0.0,0.0,1.0,1.0])
    _ = b.add_surface(0,points[0],Vector3(0.0,0.0,1.0),Vector3(1.0,0.0,0.0))
    _ = b.add_surface(0,points[2],Vector3(0.0,0.0,1.0),Vector3(1.0,0.0,0.0))
    _ = b.add_surface(0,points[0],Vector3(0.0,-1.0,0.0),Vector3(1.0,0.0,0.0))
    _ = b.add_face(0,[1,2,-3,4],[4],false)
    _ = b.add_face(1,[1,-5],[2],false)
    _ = b.add_face(2,[3,-6],[2])
    _ = b.add_face(3,[5,2,-6,4],[4])
    for spacing in [0.0,0.125]:
        let mesh = b.mesh(8,edge_size=spacing)
        planned_matches(b,mesh,0,8,spacing)
        manifold(mesh)
        let parents = mesh.find_attribute("cad_face",2)
        var cells = 0
        var interior_quads = 0
        var trimmed = 0
        for face in 0..<mesh.face_count():
            if mesh.attribute_value(parents,face,0) != 0.0: continue
            let count = mesh.face_size(face)
            assert(count >= 4)
            cells += 1
            var boundary_corners = 0
            for at in 0..<count:
                let p = mesh.point(mesh.face_point(face,at))
                near(p.y,p.x*(1.0-p.x))
                if p.z < 0.000001 or p.z > 0.999999: boundary_corners += 1
            # Extra samples may terminate only on the two hard curved trims;
            # no extra rows or diagonal edges are allowed through the interior.
            if count > 4:
                trimmed += 1
                assert(boundary_corners >= count-2)
            elif boundary_corners == 0: interior_quads += 1
        assert(cells >= 8)
        if spacing > 0.0: assert(interior_quads >= 8)
        else: assert(trimmed > 0)
        for triangle in 0..<mesh.index_count()//3:
            let a = mesh.vertex(mesh.index(triangle*3))
            let middle = mesh.vertex(mesh.index(triangle*3+1))
            let c = mesh.vertex(mesh.index(triangle*3+2))
            let cross = middle.position.subtract(a.position).cross(c.position.subtract(a.position))
            for n in [a.normal,middle.normal,c.normal]: assert(cross.dot(n) > cross.length()*0.0000000001)