dymokomi/luce-cad
Source of release 0.9.3
0.9.3 / src / brep / model.lucb
## The B-rep as a structure of arrays: vertices, curves, surfaces, faces and
## loops each a set of columns (store.lucb), shared by count between copies
## of a model. Edge IDs are shared across incident faces. Built through
## building.lucb; the surfaces' control vertices form the `cv` domain
## (cvs.lucb). The tessellator reads owned records (geometry/records.lucb)
## made from the columns once per model and kept until the model changes
## (records.lucb, meshes.lucb).
import interop
import memory
from luce_std import math, parallel
import sync
from luce_geocore.geocore import Vector3, Matrix4, Mesh, invalid
from luce_geocore.core.growing import Growing
from luce_geocore.kernel import BitsBuilder, GeometryWriter, GeometryReader, PropertyHeader, dtype_float64, dtype_int32, dtype_uint8
from luce_geocore.core.shared import Column
from luce_geocore.kernel import TableBuilder, Domain, text_attribute
from geometry import NurbsSurface, NurbsCurve, Curve, Surface, BrepFace, surface_jet, curve_parameter, project, evaluate, least_tolerance, tolerance_for, edge_index, surface_plane, surface_cylinder, surface_cone, surface_spline, surface_sphere, surface_torus, curve_line
from luce_geocore.kernel import moved_storage, mesh_adopt, precise_span, Float3, Attribute
from tessellation import tessellate, FaceCache, dispose_cache, inherited, face_mesh
## Values per curve frame: origin, x and y axes, radius, start angle, sweep.
let curve_stride: usize = 12
## Values per surface frame: origin, x and z axes, radius, angle.
let surface_stride: usize = 11
## The tessellator's records, made once from the columns: the first
## `made_*` of each array are valid.
pub struct Records:
pub var edges: Curve[]
pub var surfaces: Surface[]
pub var faces: BrepFace[]
var made_edges: usize = 0
var made_surfaces: usize = 0
var made_faces: usize = 0
pub func close():
for edge in self.edges[..<self.made_edges]: edge.close()
for surface in self.surfaces[..<self.made_surfaces]: surface.close()
for face in self.faces[..<self.made_faces]: face.close()
free(self.edges) in memory.heap
free(self.surfaces) in memory.heap
free(self.faces) in memory.heap
## A whole-model tessellation kept for its parameters.
struct KeptMesh:
let divisions: i64
let edge_size: f64
let curvature: bool
let directions: bool
let mesh: interop.Reference[Mesh]
## What a model derives from its columns on demand (from any thread): the
## tessellator's records, the per-face tessellation cache and the last few
## whole tessellations. Behind a pointer, so reading a model can fill it.
struct Derived:
var lock: sync.Mutex
var records: Records*?
var faces: FaceCache*?
var meshes: Growing[KeptMesh]
## A copy with moved cvs: where its quick preview comes from, and the
## preview once made (quick.lucb).
var quick: QuickSource*? = none
var quick_result: interop.Reference[Mesh]? = none
## The edges' polylines (overlay.lucb), made once; copies with moved
## cvs share every edge, so they start with them.
var boundaries: Growing[Vector3] = Growing[Vector3]()
## A boundary representation: vertices, edges on curves, faces on
## surfaces bounded by loops of signed edge uses, stored as shared columns,
## with the records, meshes and previews made from them kept until it
## changes.
pub struct BrepModel:
let tolerance: f64
var vertices: Store[Vector3]
# Curves: kind (0 line, 1 circle arc, 2 B-spline interval, 3 ellipse
# arc), end vertices, frame, degree, sense, control points (lines keep
# their two ends), weights (an ellipse its minor radius) and knots.
var curve_kind: Store[u8]
var curve_ends: Store[i32]
var curve_frame: Store[f64]
var curve_degree: Store[i32]
var curve_sense: Store[u8]
var curve_point_offsets: Store[i32]
var curve_points: Store[Vector3]
var curve_weight_offsets: Store[i32]
var curve_weights: Store[f64]
var curve_knot_offsets: Store[i32]
var curve_knots: Store[f64]
# Precision (Parasolid's tolerant edges and vertices): how far an edge's
# faces or a vertex's edges may lie from it, 0 for the model's
# tolerance; Stitch gives the edges it joins across a gap theirs.
var edge_precision: Store[f64]
var vertex_precision: Store[f64]
# Surfaces: kind (0 plane, 1 cylinder, 2 cone, 3 rational B-spline,
# 4 sphere, 5 torus), frame, dimensions (nu, nv, du, dv), control
# vertices (the cv domain: positions and weights), knots (u then v), and
# whether its cvs were moved since import (its trims may leave it).
var surface_kind: Store[u8]
var surface_edited: Store[u8]
var surface_frame: Store[f64]
var surface_dims: Store[i32]
var surface_cv_offsets: Store[i32]
var cv_positions: Store[Vector3]
var surface_weight_offsets: Store[i32]
var cv_weights: Store[f64]
var surface_knot_offsets: Store[i32]
var surface_knots: Store[f64]
# Faces: support surface, sense, edge uses (signed: +(edge + 1) forward),
# loop sizes (outer first), placement, color and path.
var face_surface: Store[i32]
var face_sense: Store[u8]
var face_use_offsets: Store[i32]
var face_uses: Store[i32]
var face_loop_offsets: Store[i32]
var loop_sizes: Store[i32]
var face_placement: Store[Matrix4]
var face_color: Store[Vector3]
var face_colored: Store[u8]
var face_paths: FaceTexts
var face_names: FaceTexts
var derived: Derived*
pub func init(tolerance: f64 = 0.000001) -> !:
if not math.is_finite(tolerance) or tolerance < least_tolerance or tolerance > 1.0:
error(invalid, "CAD tolerance must be finite and between 1e-9 and 1 source unit")
self.tolerance = tolerance
self.vertices = Store[Vector3]()
self.curve_kind = Store[u8]()
self.curve_ends = Store[i32]()
self.curve_frame = Store[f64]()
self.curve_degree = Store[i32]()
self.curve_sense = Store[u8]()
self.curve_point_offsets = Store[i32]()
self.curve_points = Store[Vector3]()
self.curve_weight_offsets = Store[i32]()
self.curve_weights = Store[f64]()
self.curve_knot_offsets = Store[i32]()
self.curve_knots = Store[f64]()
self.edge_precision = Store[f64]()
self.vertex_precision = Store[f64]()
self.surface_kind = Store[u8]()
self.surface_edited = Store[u8]()
self.surface_frame = Store[f64]()
self.surface_dims = Store[i32]()
self.surface_cv_offsets = Store[i32]()
self.cv_positions = Store[Vector3]()
self.surface_weight_offsets = Store[i32]()
self.cv_weights = Store[f64]()
self.surface_knot_offsets = Store[i32]()
self.surface_knots = Store[f64]()
self.face_surface = Store[i32]()
self.face_sense = Store[u8]()
self.face_use_offsets = Store[i32]()
self.face_uses = Store[i32]()
self.face_loop_offsets = Store[i32]()
self.loop_sizes = Store[i32]()
self.face_placement = Store[Matrix4]()
self.face_color = Store[Vector3]()
self.face_colored = Store[u8]()
self.face_paths = face_texts()
self.face_names = face_texts()
self.derived = try new Derived(sync.Mutex(), none, none, Growing[KeptMesh]()) in memory.heap
# A failure below lets go of what is made so far.
errdefer self.close()
try self.curve_point_offsets.push(0)
try self.curve_weight_offsets.push(0)
try self.curve_knot_offsets.push(0)
try self.surface_cv_offsets.push(0)
try self.surface_weight_offsets.push(0)
try self.surface_knot_offsets.push(0)
try self.face_use_offsets.push(0)
try self.face_loop_offsets.push(0)
## An empty B-rep of tolerance `tolerance`, held by reference.
pub static func held(tolerance: f64) -> interop.Reference[BrepModel]!:
return try interop.Reference[BrepModel].adopt(new BrepModel(tolerance) in memory.heap, brep_model_type, memory.heap)
## An empty B-rep for a model of extent `size` (its tolerance_for).
pub static func sized(size: f64) -> interop.Reference[BrepModel]!:
return try BrepModel.held(tolerance_for(size))
pub func face_count() -> i64:
return (i64)self.face_surface.count
pub func edge_count() -> i64:
return (i64)self.curve_kind.count
pub func vertex_count() -> i64:
return (i64)self.vertices.count
pub func surface_count() -> i64:
return (i64)self.surface_kind.count
func check_face(face: i64) -> !:
if face < 0 or face >= self.face_count(): error(invalid, "CAD face index out of range")
## Identity of the underlying support surface; trimmed faces may share it.
pub func face_surface_id(face: i64) -> i64!:
try self.check_face(face)
return (i64)self.face_surface.get((usize)face)
## Support type: plane=0, cylinder=1, cone=2, NURBS=3, sphere=4, torus=5.
pub func face_surface_kind(face: i64) -> i64!:
try self.check_face(face)
return (i64)self.surface_kind.get((usize)self.face_surface.get((usize)face))
## A format-independent object path attached to an analytic face.
pub func face_path(face: i64) -> str!:
try self.check_face(face)
return self.face_paths.get((usize)face)
pub func set_face_path(face: i64, path: str) -> !:
try self.check_face(face)
try self.face_paths.set((usize)face, path)
## The name modeling gave the face ("" when none): which feature made
## it and from what, kept through later features (docs/MODELING.md).
pub func face_name(face: i64) -> str!:
try self.check_face(face)
return self.face_names.get((usize)face)
pub func set_face_name(face: i64, name: str) -> !:
try self.check_face(face)
try self.face_names.set((usize)face, name)
## The edge uses of face `face` (signed) and its loop sizes.
func uses_of(face: usize) -> const i32[]:
return self.face_uses.range((usize)self.face_use_offsets.get(face), (usize)self.face_use_offsets.get(face + 1))
func loops_of(face: usize) -> const i32[]:
return self.loop_sizes.range((usize)self.face_loop_offsets.get(face), (usize)self.face_loop_offsets.get(face + 1))
## Extract faces with compact shared topology; unrelated points and curves
## are not copied into the result. Face paths/colors/placements survive.
pub func subset(selection: const i64[]) -> interop.Reference[BrepModel]!:
let output = try BrepModel.held(self.tolerance)
errdefer output.release()
let target = output.value()
let vertices = try new i64[self.vertices.count] --- in memory.heap
defer free(vertices) in memory.heap
let edges = try new i64[self.curve_kind.count] --- in memory.heap
defer free(edges) in memory.heap
let surfaces = try new i64[self.surface_kind.count] --- in memory.heap
defer free(surfaces) in memory.heap
for index in 0..<vertices.length: vertices[index] = -1
for index in 0..<edges.length: edges[index] = -1
for index in 0..<surfaces.length: surfaces[index] = -1
var uses = Growing[i64]()
defer uses.close()
var sizes = Growing[i64]()
defer sizes.close()
for id in selection:
try self.check_face(id)
uses.clear()
sizes.clear()
for use in self.uses_of((usize)id):
let index = edge_index(use)
if edges[index] < 0:
for side in 0..<(usize)2:
let point = (usize)self.curve_ends.get(index * 2 + side)
if vertices[point] < 0:
vertices[point] = try target.add_vertex(self.vertices.get(point))
try target.vertex_precision.set((usize)vertices[point], self.vertex_precision.get(point))
edges[index] = try target.copy_curve(&self, index, vertices)
try target.edge_precision.set((usize)edges[index], self.edge_precision.get(index))
try uses.push((edges[index] + 1) * (1 if use > 0 else -1))
for size in self.loops_of((usize)id): try sizes.push((i64)size)
let sid = (usize)self.face_surface.get((usize)id)
if surfaces[sid] < 0:
surfaces[sid] = try target.copy_surface(&self, sid)
try target.surface_edited.set((usize)surfaces[sid], self.surface_edited.get(sid))
let copied = try target.add_face(surfaces[sid], uses.view(), sizes.view(), self.face_sense.get((usize)id) == 1)
try target.face_placement.set((usize)copied, self.face_placement.get((usize)id))
try target.face_color.set((usize)copied, self.face_color.get((usize)id))
try target.face_colored.set((usize)copied, self.face_colored.get((usize)id))
try target.set_face_path(copied, try self.face_path(id))
try target.set_face_name(copied, try self.face_name(id))
return output
## Curve `index` of `source`, its end vertices mapped through `vertices`.
func copy_curve(source: const BrepModel*, index: usize, vertices: const i64[]) -> i64!:
let start = vertices[(usize)source.curve_ends.get(index * 2)]
let end = vertices[(usize)source.curve_ends.get(index * 2 + 1)]
try self.push_curve(source.curve_kind.get(index), start, end, source.curve_frame.range(index * curve_stride, (index + 1) * curve_stride),
source.curve_points_of(index), source.curve_weights_of(index), source.curve_knots_of(index), (i64)source.curve_degree.get(index), source.curve_sense.get(index) == 1)
return self.edge_count() - 1
func copy_surface(source: const BrepModel*, index: usize) -> i64!:
let dims = source.surface_dims.range(index * 4, index * 4 + 4)
try self.push_surface(source.surface_kind.get(index), source.surface_frame.range(index * surface_stride, (index + 1) * surface_stride),
source.cvs_of(index), source.cv_weights_of(index), source.knots_of(index, 0), source.knots_of(index, 1), dims)
return self.surface_count() - 1
func curve_points_of(index: usize) -> const Vector3[]:
return self.curve_points.range((usize)self.curve_point_offsets.get(index), (usize)self.curve_point_offsets.get(index + 1))
func curve_weights_of(index: usize) -> const f64[]:
return self.curve_weights.range((usize)self.curve_weight_offsets.get(index), (usize)self.curve_weight_offsets.get(index + 1))
func curve_knots_of(index: usize) -> const f64[]:
return self.curve_knots.range((usize)self.curve_knot_offsets.get(index), (usize)self.curve_knot_offsets.get(index + 1))
## Surface `index`'s control vertices (the cv domain range) and weights.
func cvs_of(index: usize) -> const Vector3[]:
return self.cv_positions.range((usize)self.surface_cv_offsets.get(index), (usize)self.surface_cv_offsets.get(index + 1))
func cv_weights_of(index: usize) -> const f64[]:
return self.cv_weights.range((usize)self.surface_weight_offsets.get(index), (usize)self.surface_weight_offsets.get(index + 1))
## Surface `index`'s knots in u (`axis` 0) or v (1).
func knots_of(index: usize, axis: usize) -> const f64[]:
let at = index * 2 + axis
return self.surface_knots.range((usize)self.surface_knot_offsets.get(at), (usize)self.surface_knot_offsets.get(at + 1))
pub func face_edge_count(face: i64) -> i64!:
try self.check_face(face)
return (i64)self.uses_of((usize)face).length
func use_at(face: i64, use_index: i64) -> i64!:
let count = try self.face_edge_count(face)
if use_index < 0 or use_index >= count:
error(invalid, "CAD edge use index out of range")
return (i64)self.uses_of((usize)face)[(usize)use_index]
pub func face_edge_point(face: i64, use_index: i64, fraction: f64) -> Vector3!:
let use = try self.use_at(face, use_index)
let edge = try self.curve(edge_index(use))
defer edge.close()
return self.face_placement.get((usize)face).transform_point(try edge.at(fraction if use > 0 else 1.0 - fraction))
pub func face_edge_linear(face: i64, use_index: i64) -> bool!:
let use = try self.use_at(face, use_index)
return self.curve_kind.get(edge_index(use)) == 0
pub func face_edge_id(face: i64, use_index: i64) -> i64!:
let use = try self.use_at(face, use_index)
return (i64)edge_index(use)
pub func set_face_color(face: i64, color: Vector3) -> !:
if face < 0 or face >= self.face_count() or not color.is_finite() or color.x < 0.0 or color.x > 1.0 or color.y < 0.0 or color.y > 1.0 or color.z < 0.0 or color.z > 1.0:
error(invalid, "invalid CAD face color")
try self.face_color.set((usize)face, color)
try self.face_colored.set((usize)face, 1)
self.changed()
## Preserve rigid assembly placement without changing support geometry.
pub func set_face_placement(face: i64, matrix: Matrix4) -> !:
if face < 0 or face >= self.face_count() or not matrix.is_finite() or math.abs(matrix.determinant() - 1.0) > 0.000001:
error(invalid, "invalid rigid CAD face placement")
try self.face_placement.set((usize)face, matrix)
self.changed()
pub func close():
self.changed()
self.derived.meshes.close()
self.derived.boundaries.close()
free(self.derived) in memory.heap
self.vertices.close()
self.curve_kind.close()
self.curve_ends.close()
self.curve_frame.close()
self.curve_degree.close()
self.curve_sense.close()
self.curve_point_offsets.close()
self.curve_points.close()
self.curve_weight_offsets.close()
self.curve_weights.close()
self.curve_knot_offsets.close()
self.curve_knots.close()
self.edge_precision.close()
self.vertex_precision.close()
self.surface_kind.close()
self.surface_edited.close()
self.surface_frame.close()
self.surface_dims.close()
self.surface_cv_offsets.close()
self.cv_positions.close()
self.surface_weight_offsets.close()
self.cv_weights.close()
self.surface_knot_offsets.close()
self.surface_knots.close()
self.face_surface.close()
self.face_sense.close()
self.face_use_offsets.close()
self.face_uses.close()
self.face_loop_offsets.close()
self.loop_sizes.close()
self.face_placement.close()
self.face_color.close()
self.face_colored.close()
self.face_paths.close()
self.face_names.close()
pub let brep_model_type: interop.Type[BrepModel] = interop.Type[BrepModel]("BrepModel", BrepModel.close, closeable = true)