dymokomi/luce-cad
Source of release 0.9.2
0.9.2 / src / geometry / records.lucb
## B-rep records the tessellator and exporters read: owned copies of a
## model's curves, surfaces and faces, independent of file syntax.
## Shared, immutable boundary discretization borrowed by face workers.
pub struct Samples:
pub let ids: i64[]
pub let baseline: i64[]
## An edge's curve record: kind (curve_line, curve_circle, curve_spline,
## curve_ellipse), end vertices, frame and radius, the B-spline's net (a
## line keeps its two ends), and the interval it runs (angle, sweep).
pub struct Curve:
pub let kind: i64
pub let start: i64
pub let end: i64
pub let origin: Vector3
pub let x: Vector3
pub let y: Vector3
pub let radius: f64
pub let points: Vector3[]
pub let weights: f64[]
pub let knots: f64[]
pub let degree: i64
pub let sense: bool
pub let angle: f64
pub let sweep: f64
## kind 0 line, 1 circle arc, 2 trimmed B-spline interval, 3 ellipse arc.
## Ellipse minor radius is stored as the single weight; splines own a full net.
pub func init(kind: i64, start: i64, end: i64, origin: Vector3, x: Vector3, y: Vector3, radius: f64,
points: const Vector3[], weights: const f64[], knots: const f64[], degree: i64, sense: bool,
angle: f64 = 0.0, sweep: f64 = 0.0) -> !:
let p = try copied[Vector3](points)
errdefer:
if p.length > 0: free(p) in memory.heap
let w = try copied[f64](weights)
errdefer:
if w.length > 0: free(w) in memory.heap
let k = try copied[f64](knots)
self.kind = kind
self.start = start
self.end = end
self.origin = origin
self.x = x
self.y = y
self.radius = radius
self.points = p
self.weights = w
self.knots = k
self.degree = degree
self.sense = sense
self.angle = angle
self.sweep = sweep
pub func at(fraction: f64) -> Vector3!:
if fraction < 0.0 or fraction > 1.0 or not math.is_finite(fraction):
error(invalid, "invalid curve fraction")
if self.kind == curve_line:
return self.points[0].add(self.points[1].subtract(self.points[0]).multiply_scalar(fraction))
if self.kind == curve_circle or self.kind == curve_ellipse:
let a = self.angle + self.sweep * fraction
let minor = self.weights[0] if self.kind == curve_ellipse else self.radius
return self.origin.add(self.x.multiply_scalar(self.radius * math.cos(a))).add(self.y.multiply_scalar(minor * math.sin(a)))
let low = self.knots[(usize)self.degree]
let high = self.knots[self.points.length]
var parameter = self.angle + self.sweep * fraction
if self.angle == high and self.sweep == low - high:
parameter = low + (high - low) * (1.0 - fraction)
# Closed curves may cross their parameter seam. The interval was checked
# against both topological endpoints before the immutable curve was built.
# Reconstructing an endpoint as angle+sweep can miss a knot by a few
# ulps. Do not turn that roundoff into an entire periodic traversal:
# an open edge then jumps back to its start and poisons arc-length and
# curvature planning, even though the topological endpoint is correct.
let roundoff = math.max(1.0, math.max(math.abs(low), math.abs(high))) * 8e-15
if parameter < low - roundoff: parameter += high - low
if parameter > high + roundoff: parameter -= high - low
return try NurbsCurve.at(self.points, self.weights, self.degree, self.knots, math.clamp(parameter, low, high))
pub func close():
if self.points.length > 0: free(self.points) in memory.heap
if self.weights.length > 0: free(self.weights) in memory.heap
if self.knots.length > 0: free(self.knots) in memory.heap
## A face's support record: kind (surface_plane ... surface_torus), frame
## (origin, x, y = z x x, z), radius and angle, and a B-spline's net, knots
## and degrees.
pub struct Surface:
pub let kind: i64
pub let origin: Vector3
pub let x: Vector3
pub let y: Vector3
pub let z: Vector3
pub let radius: f64
pub let angle: f64
pub let points: Vector3[]
pub let weights: f64[]
pub let ku: f64[]
pub let kv: f64[]
pub let nu: i64
pub let nv: i64
pub let du: i64
pub let dv: i64
## Its cvs were moved after import: a boundary point that no longer lies
## on it takes its nearest parameters instead of failing the face.
pub var edited: bool
## kind 0 plane, 1 cylinder, 2 cone, 3 rational B-spline, 4 sphere, 5 torus.
## angle is the cone's semi-angle or, for a torus, its minor radius.
pub func init(kind: i64, origin: Vector3, x: Vector3, z: Vector3, radius: f64, angle: f64,
points: const Vector3[], weights: const f64[], ku: const f64[], kv: const f64[],
nu: i64, nv: i64, du: i64, dv: i64) -> !:
if kind == surface_spline: try NurbsSurface.check(points, weights, nu, nv, du, dv, ku, kv)
let p = try copied[Vector3](points)
errdefer:
if p.length > 0: free(p) in memory.heap
let w = try copied[f64](weights)
errdefer:
if w.length > 0: free(w) in memory.heap
let u = try copied[f64](ku)
errdefer:
if u.length > 0: free(u) in memory.heap
let v = try copied[f64](kv)
self.kind = kind
self.origin = origin
self.x = x
self.z = z
self.y = z.cross(x)
self.radius = radius
self.angle = angle
self.points = p
self.weights = w
self.ku = u
self.kv = v
self.nu = nu
self.nv = nv
self.du = du
self.dv = dv
self.edited = false
pub func at(u: f64, v: f64) -> Vector3!:
return try NurbsSurface.sample_checked(self.points, self.weights, self.nu, self.nv, self.du, self.dv, self.ku, self.kv, u, v)
pub func close():
if self.points.length > 0: free(self.points) in memory.heap
if self.weights.length > 0: free(self.weights) in memory.heap
if self.ku.length > 0: free(self.ku) in memory.heap
if self.kv.length > 0: free(self.kv) in memory.heap
## A face record: its surface, its loops (signed edge uses, outer loop
## first; sizes), whether its normal is the surface's, and its placement,
## color and path.
pub struct BrepFace:
pub var path: interop.Packet[str]?
pub var placement: Matrix4
pub var color: Vector3
pub var colored: bool
pub let surface: i64
pub let uses: i64[]
pub let sizes: i64[]
pub let sense: bool
pub func init(surface: i64, uses: const i64[], sizes: const i64[], sense: bool) -> !:
self.path = none
self.placement = Matrix4()
self.color = Vector3(1.0, 1.0, 1.0)
self.colored = false
let owned_uses = try copied[i64](uses)
errdefer:
if owned_uses.length > 0: free(owned_uses) in memory.heap
let owned_sizes = try copied[i64](sizes)
self.surface = surface
self.uses = owned_uses
self.sizes = owned_sizes
self.sense = sense
pub func close():
if self.uses.length > 0: free(self.uses) in memory.heap
if self.sizes.length > 0: free(self.sizes) in memory.heap
if let path = self.path: path.release()