dymokomi/luce-cad
Source of release 0.9.1
0.9.1 / src / circular_lattice.lucb
## Agree on a circular row family before sampling any shared boundary.
## Unioning independently phased full circles and split arcs makes almost
## coincident rows. Instead retain every CAD endpoint as a mandatory angle and
## evenly divide the intervals between them at the strongest requested density.
import memory
from luce_std import math
from luce_geocore.geocore import Vector3, invalid
from brep_types import Curve, Surface, BrepFace
from patch_sides import PatchSides, edge_index
from layout_types import Stations, GridLayout
from layout_phase import compatible, parameter
## Share angular rows without duplicating matched, physically coincident endpoints.
pub func regularize(edges: const Curve[], surfaces: const Surface[], faces: const BrepFace[], sides: const PatchSides[], plans: GridLayout[], baseline: Stations[], divisions: i64, tolerance: f64) -> !:
let parents = try new usize[edges.length] --- in memory.heap
defer free(parents) in memory.heap
let heads = try new usize[edges.length] --- in memory.heap
defer free(heads) in memory.heap
let next = try new usize[edges.length] --- in memory.heap
defer free(next) in memory.heap
let densities = try new f64[edges.length] --- in memory.heap
defer free(densities) in memory.heap
for at in 0..<edges.length:
parents[at] = at
heads[at] = edges.length
densities[at] = (f64)(baseline[at].count - 1) / math.abs(edges[at].sweep) if edges[at].kind == 1 else 0.0
for at in 0..<faces.length:
if not sides[at].valid or plans[at].cut_first or surfaces[(usize)faces[at].surface].kind == 0: continue
let face = faces[at]
let side = sides[at]
var low = try edges[edge_index(face.uses[0])].at(0.0)
var high = low
for use in face.uses:
for fraction in [0.0, 0.25, 0.5, 0.75, 1.0]:
let p = try edges[edge_index(use)].at(fraction)
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))
# Reserve half the sampled display error for each row direction. Merely
# retaining old counts can expose under-resolution once duplicate phases
# disappear; the circular sagitta supplies an independent density floor.
let deviation = math.max(tolerance, high.subtract(low).length() / ((f64)divisions * (f64)divisions * 16.0)) * 0.5
for pair in 0..<(usize)2:
if not (try compatible(face, edges, side, pair)): continue
for axis in [pair, pair + 2]:
for segment in 0..<side.lengths[axis]:
let index = edge_index(face.uses[(side.starts[axis] + segment) % face.uses.length])
let angle = 2.0 * math.acos(math.max(-1.0, 1.0 - deviation / edges[index].radius))
if angle > 1e-12: densities[index] = math.max(densities[index], 1.0 / angle)
let a = root(parents, edge_index(face.uses[side.starts[pair]]))
let b = root(parents, edge_index(face.uses[(side.starts[axis] + segment) % face.uses.length]))
parents[b if a < b else a] = a if a < b else b
for at in 0..<edges.length:
let parent = root(parents, at)
parents[at] = parent
next[at] = heads[parent]
heads[parent] = at
let changed = try new bool[edges.length] --- in memory.heap
defer free(changed) in memory.heap
for at in 0..<changed.length: changed[at] = false
for at in 0..<heads.length:
if heads[at] == edges.length or next[heads[at]] == edges.length: continue
build_family(edges, heads[at], next, baseline, densities, tolerance) catch failure:
if failure.code != invalid: error(failure.code, failure.message)
continue
var member = heads[at]
while member < edges.length:
changed[member] = true
member = next[member]
for face in 0..<faces.length:
for use in 0..<faces[face].uses.length:
let index = edge_index(faces[face].uses[use])
if not changed[index]: continue
let row = &plans[face].rows[use]
row.count = 0
for value in baseline[index].values[..<baseline[index].count]: _ = try row.insert(value, 1e-12)
func root(parents: const usize[], at: usize) -> usize:
var result = at
while parents[result] != result: result = parents[result]
return result
func build_family(edges: const Curve[], head: usize, next: const usize[], baseline: Stations[], densities: const f64[], tolerance: f64) -> !:
let reference = edges[head]
var anchors = Stations()
try anchors.open(1025)
defer anchors.close()
_ = try anchors.insert(0.0, 1e-12)
_ = try anchors.insert(2.0 * math.pi, 1e-12)
var density: f64 = 0.0
var member = head
while member < edges.length:
let edge = edges[member]
density = math.max(density, densities[member])
for endpoint in [0.0, 1.0]:
let p = (try edge.at(endpoint)).subtract(edge.origin)
var angle = math.atan2(p.dot(reference.y), p.dot(reference.x)) - reference.angle
angle -= math.floor(angle / (2.0 * math.pi)) * 2.0 * math.pi
_ = try anchors.insert(angle, 1e-10)
member = next[member]
# Keep useful quadrant stations/extrema, but not a microscopic interval
# beside a mandatory CAD endpoint. Only these optional anchors may be
# omitted; no CAD endpoint is moved or merged with another topology ID.
anchors.separation = 0.25 / density
for quadrant in 1..<4: _ = try anchors.insert((f64)quadrant * math.pi * 0.5, 1e-12)
anchors.separation = 0.0
var stations = Stations()
try stations.open(1025)
defer stations.close()
for interval in 0..<anchors.count - 1:
let low = anchors.values[interval]
let span = anchors.values[interval + 1] - low
let divisions = (usize)math.max(1.0, math.ceil(span * density - 1e-9))
if divisions > 1024: error(invalid, "circular lattice exceeds station budget")
for at in 0..<divisions:
_ = try stations.insert(low + span * (f64)at / (f64)divisions, 1e-12)
# Prepare every member before touching the original family. A bounded
# planning failure must leave all old row/baseline contracts intact.
var pending = Stations()
try pending.open(1025)
defer pending.close()
member = head
while member < edges.length:
try fill(&pending, edges[member], reference, stations, tolerance)
member = next[member]
member = head
while member < edges.length:
try fill(&baseline[member], edges[member], reference, stations, tolerance)
member = next[member]
func fill(output: Stations*, edge: Curve, reference: Curve, stations: Stations, tolerance: f64) -> !:
output.count = 0
_ = try output.insert(0.0, 1e-12)
_ = try output.insert(1.0, 1e-12)
# Coverage matching in parameter() admits endpoint differences up to 1e-9
# of the arc span. An agreed endpoint must not become a second station when
# it lies just inside another fitted arc. Both authored endpoints remain
# exact, and the allowance is capped at 1% of the File/model tolerance so
# enormous circles or very tight models cannot erase meaningful cuts.
let endpoint_epsilon = math.min(1e-9, tolerance * 0.01 / (edge.radius * math.abs(edge.sweep)))
for angle in stations.values[..<stations.count]:
let theta = angle + reference.angle
let p = reference.x.multiply_scalar(math.cos(theta)).add(reference.y.multiply_scalar(math.sin(theta)))
let t = parameter(edge, math.atan2(p.dot(edge.y), p.dot(edge.x)))
if t <= endpoint_epsilon or t >= 1.0 - endpoint_epsilon: continue
if t >= 0.0: _ = try output.insert(t, 1e-12)