Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.3

0.9.3 / src / layout / cell_error.lucb

## Plan complete support-grid rows from the error of display triangles.
## Directional curve tests alone miss mixed curvature and diagonal deviation.

## The sag of a circular arc per unit chord when it turns a quarter
## turn's 1/divisions (pi / (2 divisions)): about the angle over 8.
pub func turn_sag(divisions: i64) -> f64:
    return math.pi / (16.0 * (f64)divisions)

## Refine before edge/grid intersections are frozen, so adjacent patches can
## share the final cuts. Existing stations and all CAD trims remain untouched.
pub func refine_cells(u: Stations*, v: Stations*, s: Surface, divisions: i64, tolerance: f64, epsilon: f64) -> !:
    if s.kind != surface_spline: return
    for pass in 0..<8:
        let nx = u.count
        let ny = v.count
        try check_grid_size(nx, ny)
        let points = try new Vector3[nx * ny] --- in memory.heap
        defer free(points) in memory.heap
        let split_u = try new bool[nx - 1] --- in memory.heap
        defer free(split_u) in memory.heap
        let split_v = try new bool[ny - 1] --- in memory.heap
        defer free(split_v) in memory.heap
        for at in 0..<split_u.length: split_u[at] = false
        for at in 0..<split_v.length: split_v[at] = false
        var low = Vector3(1e100, 1e100, 1e100)
        var high = Vector3(-1e100, -1e100, -1e100)
        for row in 0..<ny:
            for col in 0..<nx:
                let p = try evaluate(s, Vector3(u.values[col], v.values[row], 0.0))
                points[row * nx + col] = p
                low = Vector3(math.min(low.x, p.x), math.min(low.y, p.y), math.min(low.z, p.z))
                high = Vector3(math.max(high.x, p.x), math.max(high.y, p.y), math.max(high.z, p.z))
        let target = math.max(tolerance, high.subtract(low).length() / ((f64)divisions * (f64)divisions * 32.0))
        var changed = false
        for row in 0..<ny - 1:
            for col in 0..<nx - 1:
                let a = points[row * nx + col]
                let b = points[row * nx + col + 1]
                let c = points[(row + 1) * nx + col + 1]
                let d = points[(row + 1) * nx + col]
                var error2: f64 = 0.0
                for f in [Vector3(0.5, 0.5, 0.0), Vector3(1.0 / 3.0, 1.0 / 3.0, 0.0), Vector3(2.0 / 3.0, 2.0 / 3.0, 0.0), Vector3(1.0 / 3.0, 2.0 / 3.0, 0.0), Vector3(2.0 / 3.0, 1.0 / 3.0, 0.0)]:
                    let p = try evaluate(s, Vector3(u.values[col] * (1.0 - f.x) + u.values[col + 1] * f.x, v.values[row] * (1.0 - f.y) + v.values[row + 1] * f.y, 0.0))
                    let first = math.min(squared_triangle_distance(p, a, b, c), squared_triangle_distance(p, a, c, d))
                    let second = math.min(squared_triangle_distance(p, a, b, d), squared_triangle_distance(p, b, c, d))
                    error2 = math.max(error2, math.max(first, second))
                # Or within the sag of an arc turning a quarter turn's
                # 1/divisions across the cell's diagonal, as circles and
                # cylinders are sampled: a small patch is no finer than a
                # cylinder of its curvature.
                let diagonal = math.max(a.subtract(c).length(), b.subtract(d).length())
                let allowed = math.max(target, diagonal * turn_sag(divisions))
                if error2 <= allowed * allowed: continue
                let width = a.subtract(b).length() + c.subtract(d).length()
                let height = a.subtract(d).length() + b.subtract(c).length()
                if width >= height * 0.5: split_u[col] = true
                if height >= width * 0.5: split_v[row] = true
                changed = true
        if not changed: return
        let su = u.separation
        let sv = v.separation
        u.separation = 0.0
        v.separation = 0.0
        defer:
            u.separation = su
            v.separation = sv
        # Descend so inserts do not invalidate the original interval indices.
        for offset in 0..<nx - 1:
            let at = nx - 2 - offset
            if split_u[at]: _ = try u.insert((u.values[at] + u.values[at + 1]) * 0.5, epsilon)
        for offset in 0..<ny - 1:
            let at = ny - 2 - offset
            if split_v[at]: _ = try v.insert((v.values[at] + v.values[at + 1]) * 0.5, epsilon)
    error(invalid, "support grid diagonal error did not converge")