Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / tests / regressions / seamed_sphere_tests.luc

## Rotating a pole chart must retain a retraced authored seam as a fixed
## interior constraint, not reject it as overlap or erase/split its samples.
from luce_cad.model import BrepModel
from luce_geocore.geocore import Vector3, Matrix4
from luce_std import math

pub func seamed_sphere_tests() -> unit!:
    for radius in [0.005,1.0]:
        for reverse in [false,true]:
            for start in 0..<5:
                check(radius,reverse,start)
    print("PASS spherical retraced seams, rotated charts, canonical segments, winding and analytic normals")

func check(radius: float, reverse: bool, start: int) -> unit!:
    let model = BrepModel(radius*0.00001)
    let frame = Matrix4.compose(Vector3(2.0,3.0,-5.0),Vector3(0.21,-0.17,0.38),Vector3(1.0,1.0,1.0))
    let origin = frame.transform_point(Vector3())
    let axes = [frame.transform_point(Vector3(1.0,0.0,0.0)).subtract(origin),frame.transform_point(Vector3(0.0,1.0,0.0)).subtract(origin),frame.transform_point(Vector3(0.0,0.0,1.0)).subtract(origin)]
    let pole = axes[0].add(axes[1]).add(axes[2]).normalized()
    for axis in axes: _ = model.add_vertex(origin.add(axis.multiply_scalar(radius)))
    _ = model.add_vertex(origin.add(pole.multiply_scalar(radius)))
    for at in 0..<3: _ = model.add_circle(at,(at+1)%3,origin,axes[(at+2)%3],axes[at],radius)
    _ = model.add_circle(0,3,origin,axes[0].cross(pole).normalized(),axes[0],radius)
    _ = model.add_surface(4,origin,pole,axes[0],radius)
    let original = [4,-4,1,2,3]
    let uses: list[int] = []
    for at in 0..<5: uses.append(-original[(start+5-at)%5] if reverse else original[(start+at)%5])
    _ = model.add_face(0,uses,[5],not reverse)
    let mesh = model.mesh(8)
    let counts: list[int] = []
    for edge in 0..<mesh.edge_count(): counts.append(0)
    for face in 0..<mesh.face_count():
        for at in 0..<mesh.face_size(face): counts[mesh.face_edge(face,at)] += 1
    for count in counts: assert(count == 1 or count == 2)
    for at in 0..<mesh.point_count(): assert(math.abs(mesh.point(at).subtract(origin).length()/radius-1.0) < 0.00001)
    # Five initial circle intervals at divisions=8. The local chart must not
    # independently replace them with differently sampled surface chords.
    let angle = math.acos(1.0/math.sqrt(3.0))
    let tangent = axes[1].add(axes[2]).normalized()
    let vertices: list[int] = []
    for at in 0..<6:
        let t = float(at)*angle/5.0
        let expected = origin.add(axes[0].multiply_scalar(math.cos(t)*radius)).add(tangent.multiply_scalar(math.sin(t)*radius))
        var found = -1
        for point in 0..<mesh.point_count():
            # Output meshes store f32 points.
            if mesh.point(point).subtract(expected).length() < radius*0.000001: found = point
        assert(found >= 0)
        vertices.append(found)
    for at in 0..<5:
        var found = false
        for edge in 0..<mesh.edge_count():
            let a = mesh.edge_point(edge,0)
            let b = mesh.edge_point(edge,1)
            if (a == vertices[at] and b == vertices[at+1]) or (b == vertices[at] and a == vertices[at+1]):
                assert(counts[edge] == 2)
                found = true
        assert(found)
    var area = 0.0
    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))
        let normal = b.position.subtract(a.position).cross(c.position.subtract(a.position))
        for n in [a.normal,b.normal,c.normal]: assert(normal.dot(n) > 0.0)
        area += normal.length()*0.5
    assert(math.abs(area/(radius*radius)-math.pi/2.0) < 0.01)