1#![allow(rustdoc::private_intra_doc_links)]
41use serde::{Deserialize, Serialize};
42#[allow(unused_imports)]
43use wasm_bindgen::prelude::*;
44
45
46#[macro_export]
51macro_rules! env_read {
52 ($name:literal) => {{
53 static CELL: std::sync::OnceLock<Option<String>> = std::sync::OnceLock::new();
54 $crate::env_read::read($name, &CELL)
55 }};
56}
57#[macro_export]
65macro_rules! switch {
66 (live $name:expr, $default:expr) => {
67 $crate::env_read::switch_live($name, $default, $crate::env_read::Grammar::Switch)
68 };
69 ($name:expr, $default:expr) => {{
70 static CELL: std::sync::OnceLock<Option<bool>> = std::sync::OnceLock::new();
71 $crate::env_read::switch($name, &CELL, $default, $crate::env_read::Grammar::Switch)
72 }};
73}
74
75#[macro_export]
79macro_rules! debug_flag {
80 (live $name:expr) => {
81 $crate::env_read::switch_live($name, false, $crate::env_read::Grammar::Flag)
82 };
83 ($name:expr) => {{
84 static CELL: std::sync::OnceLock<Option<bool>> = std::sync::OnceLock::new();
85 $crate::env_read::switch($name, &CELL, false, $crate::env_read::Grammar::Flag)
86 }};
87}
88
89#[macro_export]
93macro_rules! mode_switch {
94 (live $name:expr, $default_on:expr) => {
95 $crate::env_read::mode_live($name, $default_on)
96 };
97 ($name:expr, $default_on:expr) => {{
98 static CELL: std::sync::OnceLock<Option<$crate::env_read::Mode>> = std::sync::OnceLock::new();
99 $crate::env_read::mode($name, &CELL, $default_on)
100 }};
101}
102
103#[macro_export]
106macro_rules! profile_line {
107 ($($arg:tt)*) => { $crate::profile::profile_emit(format_args!($($arg)*)) };
108}
109
110#[doc(hidden)]
111
112pub mod profile;
113#[doc(hidden)]
114pub mod claim_slots;
115#[cfg(feature = "profile-wall")]
116#[doc(hidden)]
117pub mod wall_profile;
118#[cfg(not(feature = "profile-wall"))]
119#[path = "wall_profile_off.rs"]
120#[doc(hidden)]
121pub mod wall_profile;
122#[cfg(feature = "profile-wall")]
123#[doc(hidden)]
124pub mod reader_profile;
125#[cfg(not(feature = "profile-wall"))]
126#[path = "reader_profile_off.rs"]
127#[doc(hidden)]
128pub mod reader_profile;
129
130
131#[doc(hidden)]
134#[doc(hidden)]
135pub mod env_read;
136
137#[doc(hidden)]
142pub mod panic_hook;
143
144#[doc(hidden)]
147pub mod text;
148
149#[path = "geometry/tolerance.rs"]
150#[doc(hidden)]
151pub mod tolerance;
152pub use tolerance::{
153 curve_model_scale, model_scale, report_scale_migration, SolidScale,
154 offset_construction_band, vertex_tolerance_from_edges, KernelTolerances,
155 MeasuredTolerance, OFFSET_CONSTRUCTION_FLOOR, OFFSET_CONSTRUCTION_REL,
156 VERTEX_MATCH_FLOOR, VOLUME_DRIFT_ABS, VOLUME_DRIFT_REL,
157};
158#[path = "props/diagnostics.rs"]
159#[doc(hidden)]
160pub mod diagnostics;
161pub use diagnostics::{
162 Approximation, ApproximationBudget, Backing, BudgetMechanism, BudgetReason, MeasuredComponent, DiagnosticEvent, DiagnosticSeverity, EdgeOrigin, KernelDiagnostics,
163 KernelOutcome, KernelRefusal, OffCarrierEdge,
164 KernelStage, OrRefuse, RefusalClass, SoundnessDefect, UndecidedCause,
165};
166#[path = "geometry/polygon.rs"]
167#[doc(hidden)]
168pub mod polygon;
169#[path = "geometry/curve.rs"]
170#[doc(hidden)]
171pub mod curve;
172pub use curve::{
173 make_arc, make_circle, make_hyperbola, make_line, make_parabola, uniform_clamped_knots,
174 KnotVector, NurbsCurve, Vec4, KNOT_DEDUP_EPS, KNOT_IDENTITY_TOL,
175};
176#[path = "geometry/fit.rs"]
177#[doc(hidden)]
178pub mod fit;
179pub use fit::{
180 fit_polyline, fit_polyline_at, fit_polyline_of_degree, interpolate_curve, interpolate_curve_closed,
181 interpolate_curve_local, interpolate_curve_thinned, interpolate_curve_with_end_tangents,
182 simplify_polyline, simplify_polyline_indices, solve_banded, solve_dense, PolylineFit,
183 PolylineFitExit, PolylineFitLedger, PolylineFitReport, PolylineFitScope, MAX_FIT_STATIONS,
184};
185#[path = "geometry/image_curve.rs"]
186#[doc(hidden)]
187pub mod image_curve;
188pub use image_curve::{affine_image_curve, image_curve, image_curve_pair, ImageCurve, ImageCurveTier};
189#[path = "geometry/surface.rs"]
190#[doc(hidden)]
191pub mod surface;
192pub use surface::{
193 carrier_preview_patch, make_cone_surface, make_cylinder_surface, make_extrusion, make_plane,
194 make_revolution, make_sphere_surface, make_sphere_surface_framed, make_torus_surface,
195 ExtendRefusal, NurbsSurface, SurfaceSide, WeightSite, MAXIMUM_FOLD_ANGLE,
196 MAXIMUM_GROWTH_RATIO,
197};
198#[path = "geometry/coons.rs"]
199#[doc(hidden)]
200pub mod coons;
201pub use coons::{
202 coons_bilinear, coons_bilinear_reported, coons_with_ribbons, CompatWork, CoonsRefusal,
203 CoonsReport, RibbonReport,
204};
205#[path = "geometry/gordon.rs"]
206#[doc(hidden)]
207pub mod gordon;
208pub use gordon::{gordon_surface, gordon_surface_reported, GordonRefusal, GordonReport};
209#[path = "geometry/analytic_surface.rs"]
210#[doc(hidden)]
211pub mod analytic_surface;
212pub use analytic_surface::{
213 circle_angle_to_parameter, intersect_analytic_pair, plane_ruled_section_arc,
214 revolution_structure, AnalyticSurface, RevolutionFrame, RevolutionStructure,
215};
216pub use analytic_surface::{
217 intersect_analytic_pair_with, ruled_gate,
218 ruled_gate_census, RuledGate,
219};
220#[path = "geometry/sphere_chart.rs"]
221#[doc(hidden)]
222pub mod sphere_chart;
223pub use sphere_chart::{
224 canonicalize_points, chart_grid_divisions, chart_grid_parameters, corner_index,
225 cube_edge_corner, cube_edge_divisions, cube_edge_index, cube_edge_parameters,
226 cube_edge_parts, Chart, ChartSegment, ChartSide, SphereAtlas,
227 SphericalRegion, CHART_COUNT, CUBE_CORNER_COUNT, CUBE_EDGE_COUNT,
228};
229#[path = "geometry/projection.rs"]
230#[doc(hidden)]
231pub mod projection;
232#[path = "geometry/projection_profile.rs"]
233#[doc(hidden)]
234pub mod projection_profile;
235#[doc(hidden)]
236pub use projection_profile::report as projection_profile_report;
237#[path = "geometry/affine_sphere.rs"]
238#[doc(hidden)]
239pub mod affine_sphere;
240pub use projection::{
241 project_point_to_curve, project_point_to_surface, project_point_to_surface_seeded,
242 CurveProjection, SurfaceProjection,
243};
244#[doc(hidden)]
245pub use projection::{
246 project_point_to_surface_basin_census, project_point_to_surface_general_lanes, BasinCensus,
247 GlobalStats,
248};
249#[path = "intersect/curve_surface_intersection.rs"]
250#[doc(hidden)]
251pub mod curve_surface_intersection;
252pub use curve_surface_intersection::{intersect_curve_surface, CurveSurfaceIntersection};
253#[path = "intersect/exact_line_surface.rs"]
254#[doc(hidden)]
255pub mod exact_line_surface;
256#[path = "intersect/curve_curve_intersection.rs"]
257#[doc(hidden)]
258pub mod curve_curve_intersection;
259pub use curve_curve_intersection::{intersect_curves, CurveCurveIntersection};
260#[path = "intersect/surface_surface_intersection.rs"]
261#[doc(hidden)]
262pub mod surface_surface_intersection;
263pub use surface_surface_intersection::{
264 intersect_surfaces, intersect_surfaces_supplemental, SurfaceIntersectionCurve,
265 SurfaceIntersectionOptions,
266};
267pub use surface_surface_intersection::TRANSVERSE_SEED_CROSS;
268#[path = "intersect/tangent_curve.rs"]
269#[doc(hidden)]
270pub mod tangent_curve;
271#[path = "intersect/crossing_node.rs"]
272#[doc(hidden)]
273pub mod crossing_node;
274pub use crossing_node::{crossing_node_split_enabled, split_at_crossing_nodes};
275pub use tangent_curve::{
276 classify_tangent_contact_at, find_tangent_contact, trace_tangent_curve, TangentContact,
277 EXTENDED_RANK_FLOOR,
278};
279#[path = "geometry/spatial.rs"]
280#[doc(hidden)]
281pub mod spatial;
282pub use spatial::{Aabb, Bvh};
283#[path = "intersect/arrangement.rs"]
284#[doc(hidden)]
285pub mod arrangement;
286pub use arrangement::{
287 arrange_segments, point_in_polygon, segment_intersection, ArrangementPiece, ArrangementRegion,
288 CycleUse, Segment2, Vec2,
289};
290#[path = "geometry/pcurve.rs"]
291#[doc(hidden)]
292pub mod pcurve;
293pub use pcurve::{
294 build_pcurve_on_surface, build_pcurve_on_surface_marched, build_pcurve_on_surface_range,
295 build_pcurve_on_surface_range_dense, build_pcurve_on_surface_stations, fit_pcurve_on_surface,
296 fit_pcurve_on_surface_marched, fit_pcurve_on_surface_range, fit_pcurve_on_surface_range_dense,
297 fit_pcurve_on_surface_stations, PcurveFit, PcurveFitExit, PcurveFitLedger, PcurveFitReport,
298 PcurveFitScope, PCURVE_OFF_BAR, PCURVE_REFINEMENT_TOLERANCE,
299};
300#[path = "offset/point.rs"]
305#[doc(hidden)]
306pub mod offset_point;
307pub use offset_point::{OffsetEvaluator, OffsetNormal, OffsetSample};
308#[path = "offset/analytic_pair.rs"]
315#[doc(hidden)]
316pub mod offset_analytic_pair;
317#[path = "solvers/linear_algebra.rs"]
318#[doc(hidden)]
319pub mod solver_linear_algebra;
320
321#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
322pub struct Vec3 {
323 pub x: f64,
324 pub y: f64,
325 pub z: f64,
326}
327
328impl Vec3 {
329 pub const ZERO: Self = Self { x: 0.0, y: 0.0, z: 0.0 };
330 pub const X: Self = Self { x: 1.0, y: 0.0, z: 0.0 };
331 pub const Y: Self = Self { x: 0.0, y: 1.0, z: 0.0 };
332 pub const Z: Self = Self { x: 0.0, y: 0.0, z: 1.0 };
333
334 pub fn normalized_or(self, fallback: Self) -> Self {
336 self.normalized().unwrap_or(fallback)
337 }
338
339 #[inline]
341 pub fn rotate_about(self, axis: Self, angle: f64) -> Self {
342 let (sin, cos) = angle.sin_cos();
343 self.scale(cos)
344 .add(axis.cross(self).scale(sin))
345 .add(axis.scale(axis.dot(self) * (1.0 - cos)))
346 }
347
348 #[inline]
350 pub fn distance_to_segment(self, start: Self, end: Self, guard: f64) -> f64 {
351 let segment = end.sub(start);
352 let length_squared = segment.dot(segment);
353 if length_squared <= guard {
354 return self.sub(start).length();
355 }
356 let parameter = (self.sub(start).dot(segment) / length_squared).clamp(0.0, 1.0);
357 self.sub(start.add(segment.scale(parameter))).length()
358 }
359
360 #[inline]
362 pub fn distance_to_segment_squared(self, start: Self, end: Self) -> f64 {
363 let segment = end.sub(start);
364 let length_squared = segment.dot(segment);
365 let parameter = if length_squared > 0.0 {
366 (self.sub(start).dot(segment) / length_squared).clamp(0.0, 1.0)
367 } else {
368 0.0
369 };
370 let residual = self.sub(start.add(segment.scale(parameter)));
371 residual.dot(residual)
372 }
373
374 pub fn new(x: f64, y: f64, z: f64) -> Self {
375 Self { x, y, z }
376 }
377 pub fn sub(self, rhs: Self) -> Self {
378 Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
379 }
380 pub fn add(self, rhs: Self) -> Self {
381 Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
382 }
383 pub fn scale(self, factor: f64) -> Self {
384 Self::new(self.x * factor, self.y * factor, self.z * factor)
385 }
386 pub fn cross(self, rhs: Self) -> Self {
387 Self::new(
388 self.y * rhs.z - self.z * rhs.y,
389 self.z * rhs.x - self.x * rhs.z,
390 self.x * rhs.y - self.y * rhs.x,
391 )
392 }
393 pub fn dot(self, rhs: Self) -> f64 {
394 self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
395 }
396 pub fn length(self) -> f64 {
397 self.dot(self).sqrt()
398 }
399 pub fn length_squared(self) -> f64 {
400 self.dot(self)
401 }
402 pub fn normalized(self) -> Result<Self, String> {
403 let length = self.length();
404 if length <= 1e-12 {
405 return Err("Vec3.normalized: zero-length vector".into());
406 }
407 Ok(self.scale(1.0 / length))
408 }
409 pub fn perpendicular(self) -> Result<Self, String> {
410 let x = self.x.abs();
411 let y = self.y.abs();
412 let z = self.z.abs();
413 let axis = if x <= y && x <= z {
414 Self::new(1.0, 0.0, 0.0)
415 } else if y <= z {
416 Self::new(0.0, 1.0, 0.0)
417 } else {
418 Self::new(0.0, 0.0, 1.0)
419 };
420 self.cross(axis).normalized()
421 }
422}
423
424#[derive(Clone, Debug, Default, Deserialize, Serialize)]
425pub struct Mesh {
426 pub positions: Vec<f64>,
427 pub normals: Vec<f64>,
428 pub indices: Vec<u32>,
429 pub face_ids: Vec<u32>,
430}
431
432impl Mesh {
433 fn push_vertex(&mut self, point: Vec3, normal: Vec3) -> u32 {
434 self.positions.extend([point.x, point.y, point.z]);
435 self.normals.extend([normal.x, normal.y, normal.z]);
436 (self.positions.len() / 3 - 1) as u32
437 }
438
439 fn quad(&mut self, points: [Vec3; 4], normal: Vec3, face_id: u32) {
440 let base = self.push_vertex(points[0], normal);
441 for point in points.iter().skip(1) {
442 self.push_vertex(*point, normal);
443 }
444 self.indices
445 .extend([base, base + 1, base + 2, base, base + 2, base + 3]);
446 self.face_ids.extend([face_id, face_id]);
447 }
448
449 pub fn signed_volume(&self) -> f64 {
450 self.indices
451 .chunks_exact(3)
452 .map(|tri| {
453 let point = |index: u32| {
454 let i = index as usize * 3;
455 Vec3::new(
456 self.positions[i],
457 self.positions[i + 1],
458 self.positions[i + 2],
459 )
460 };
461 point(tri[0]).dot(point(tri[1]).cross(point(tri[2]))) / 6.0
462 })
463 .sum()
464 }
465
466 pub fn validate(&self) -> Result<(), String> {
467 if self.positions.len() % 3 != 0 || self.normals.len() != self.positions.len() {
468 return Err("invalid vertex or normal buffer".into());
469 }
470 self.validate_geometry()
471 }
472
473 pub fn validate_geometry(&self) -> Result<(), String> {
478 if self.positions.len() % 3 != 0 {
479 return Err("invalid vertex buffer".into());
480 }
481 if self.indices.len() % 3 != 0 {
482 return Err("index buffer is not triangular".into());
483 }
484 let count = self.positions.len() / 3;
485 if self.indices.iter().any(|index| *index as usize >= count) {
486 return Err("index outside vertex buffer".into());
487 }
488 if !self.positions.iter().all(|value| value.is_finite()) {
489 return Err("non-finite vertex".into());
490 }
491 Ok(())
492 }
493}
494
495pub fn make_box(size_x: f64, size_y: f64, size_z: f64) -> Mesh {
496 let x = size_x.abs();
497 let y = size_y.abs();
498 let z = size_z.abs();
499 let p = [
500 Vec3::new(0.0, 0.0, 0.0),
501 Vec3::new(x, 0.0, 0.0),
502 Vec3::new(x, y, 0.0),
503 Vec3::new(0.0, y, 0.0),
504 Vec3::new(0.0, 0.0, z),
505 Vec3::new(x, 0.0, z),
506 Vec3::new(x, y, z),
507 Vec3::new(0.0, y, z),
508 ];
509 let mut mesh = Mesh::default();
510 mesh.quad([p[0], p[3], p[2], p[1]], Vec3::new(0.0, 0.0, -1.0), 0);
511 mesh.quad([p[4], p[5], p[6], p[7]], Vec3::new(0.0, 0.0, 1.0), 1);
512 mesh.quad([p[0], p[1], p[5], p[4]], Vec3::new(0.0, -1.0, 0.0), 2);
513 mesh.quad([p[3], p[7], p[6], p[2]], Vec3::new(0.0, 1.0, 0.0), 3);
514 mesh.quad([p[0], p[4], p[7], p[3]], Vec3::new(-1.0, 0.0, 0.0), 4);
515 mesh.quad([p[1], p[2], p[6], p[5]], Vec3::new(1.0, 0.0, 0.0), 5);
516 mesh
517}
518
519pub fn make_cylinder(radius: f64, height: f64, segments: usize) -> Mesh {
520 let radius = radius.abs();
521 let height = height.abs();
522 let segments = segments.max(3);
523 let mut mesh = Mesh::default();
524 for i in 0..segments {
525 let a = std::f64::consts::TAU * i as f64 / segments as f64;
526 let b = std::f64::consts::TAU * (i + 1) as f64 / segments as f64;
527 let (sa, ca) = a.sin_cos();
528 let (sb, cb) = b.sin_cos();
529 let pa = Vec3::new(radius * ca, 0.0, -radius * sa);
530 let pb = Vec3::new(radius * cb, 0.0, -radius * sb);
531 let ta = Vec3::new(pa.x, height, pa.z);
532 let tb = Vec3::new(pb.x, height, pb.z);
533 mesh.quad(
534 [pa, pb, tb, ta],
535 Vec3::new((ca + cb) * 0.5, 0.0, -(sa + sb) * 0.5),
536 0,
537 );
538 let bottom = mesh.push_vertex(Vec3::default(), Vec3::new(0.0, -1.0, 0.0));
539 let bi = mesh.push_vertex(pb, Vec3::new(0.0, -1.0, 0.0));
540 let bj = mesh.push_vertex(pa, Vec3::new(0.0, -1.0, 0.0));
541 mesh.indices.extend([bottom, bi, bj]);
542 mesh.face_ids.push(1);
543 let top = mesh.push_vertex(Vec3::new(0.0, height, 0.0), Vec3::new(0.0, 1.0, 0.0));
544 let ti = mesh.push_vertex(ta, Vec3::new(0.0, 1.0, 0.0));
545 let tj = mesh.push_vertex(tb, Vec3::new(0.0, 1.0, 0.0));
546 mesh.indices.extend([top, ti, tj]);
547 mesh.face_ids.push(2);
548 }
549 mesh
550}
551
552#[path = "healing/curve_helpers.rs"]
553#[doc(hidden)]
554pub mod curve_helpers;
555
556#[path = "geometry/eigen2.rs"]
557#[doc(hidden)]
558pub mod eigen2;
559
560#[path = "geometry/union_find.rs"]
561#[doc(hidden)]
562pub mod union_find;
563pub use union_find::UnionFind;
564
565#[path = "geometry/poly.rs"]
566#[doc(hidden)]
567pub mod poly;
568
569pub use analytic_surface::{circumcenter, circumcenter_2d, recognize_circle, RecognizedCircle};
570
571#[path = "geometry/triangle.rs"]
572#[doc(hidden)]
573pub mod triangle;
574pub use triangle::closest_point_on_triangle;
575
576pub use tolerance::RIGID_TOL;
577
578impl std::ops::Index<usize> for Vec3 {
579 type Output = f64;
580 fn index(&self, index: usize) -> &Self::Output {
581 match index { 0 => &self.x, 1 => &self.y, 2 => &self.z, _ => panic!("Vec3 index out of bounds: {index}") }
582 }
583}
584impl std::ops::IndexMut<usize> for Vec3 {
585 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
586 match index { 0 => &mut self.x, 1 => &mut self.y, 2 => &mut self.z, _ => panic!("Vec3 index out of bounds: {index}") }
587 }
588}
589
590#[doc(hidden)]
592pub const SOURCE_HASH: Option<&str> = option_env!("BREP_KERNEL_SOURCE_HASH");
593#[doc(hidden)]
594#[path = "instrument/work_counts.rs"]
595pub mod work_counts;
596#[doc(hidden)]
597#[path = "instrument/reuse_profile.rs"]
598pub mod reuse_profile;
599#[doc(hidden)]
600#[path = "instrument/carried_uv.rs"]
601pub mod carried_uv;
602#[doc(hidden)]
603#[path = "instrument/blend_timing.rs"]
604pub mod blend_timing;
605#[doc(hidden)]
606#[path = "geometry/identity_hash.rs"]
607pub mod identity_hash;
608#[doc(hidden)]
609#[path = "geometry/trimmed_curve.rs"]
610pub mod trimmed_curve;