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
138#[doc(hidden)]
143pub mod panic_hook;
144
145#[doc(hidden)]
148pub mod text;
149
150#[path = "geometry/tolerance.rs"]
151#[doc(hidden)]
152pub mod tolerance;
153pub use tolerance::{
154 curve_model_scale, model_scale, report_scale_migration, SolidScale,
155 offset_construction_band, vertex_tolerance_from_edges, KernelTolerances,
156 MeasuredTolerance, OFFSET_CONSTRUCTION_FLOOR, OFFSET_CONSTRUCTION_REL,
157 VERTEX_MATCH_FLOOR, VOLUME_DRIFT_ABS, VOLUME_DRIFT_REL,
158};
159#[path = "props/diagnostics.rs"]
160#[doc(hidden)]
161pub mod diagnostics;
162pub use diagnostics::{
163 Approximation, ApproximationBudget, Backing, BudgetMechanism, BudgetReason, MeasuredComponent, DiagnosticEvent, DiagnosticSeverity, EdgeOrigin, KernelDiagnostics,
164 KernelOutcome, KernelRefusal, OffCarrierEdge,
165 KernelStage, OrRefuse, RefusalClass, SoundnessDefect, UndecidedCause,
166};
167#[path = "geometry/polygon.rs"]
168#[doc(hidden)]
169pub mod polygon;
170#[path = "geometry/curve.rs"]
171#[doc(hidden)]
172pub mod curve;
173pub use curve::{
174 make_arc, make_circle, make_hyperbola, make_line, make_parabola, uniform_clamped_knots,
175 KnotVector, NurbsCurve, Vec4, KNOT_DEDUP_EPS, KNOT_IDENTITY_TOL,
176};
177#[path = "geometry/fit.rs"]
178#[doc(hidden)]
179pub mod fit;
180pub use fit::{
181 fit_polyline, fit_polyline_at, fit_polyline_of_degree, interpolate_curve, interpolate_curve_closed,
182 interpolate_curve_local, interpolate_curve_thinned, interpolate_curve_with_end_tangents,
183 simplify_polyline, simplify_polyline_indices, solve_banded, solve_dense, PolylineFit,
184 PolylineFitExit, PolylineFitLedger, PolylineFitReport, PolylineFitScope, MAX_FIT_STATIONS,
185};
186#[path = "geometry/image_curve.rs"]
187#[doc(hidden)]
188pub mod image_curve;
189pub use image_curve::{affine_image_curve, image_curve, image_curve_pair, ImageCurve, ImageCurveTier};
190#[path = "geometry/surface.rs"]
191#[doc(hidden)]
192pub mod surface;
193pub use surface::{
194 carrier_preview_patch, make_cone_surface, make_cylinder_surface, make_extrusion, make_plane,
195 make_revolution, make_revolution_one_span, make_sphere_surface, make_sphere_surface_framed, make_torus_surface,
196 ExtendRefusal, KnotDirection, KnotRefinementRefusal, NurbsSurface, SurfaceSide, WeightSite,
197 MAXIMUM_FOLD_ANGLE, MAXIMUM_GROWTH_RATIO,
198};
199#[path = "geometry/deform.rs"]
200#[doc(hidden)]
201pub mod deform;
202pub use deform::{
203 deform_surface, deform_surface_where, deform_surfaces_tied, DeformPick, PoleSupport, SurfaceTie, TieContinuity,
204 TiedResult, TiedSurface, DeformRefusal, DeformResult, DeformSide, DeformSpec,
205 EdgeHold,
206};
207#[path = "geometry/bezier_triangle.rs"]
208#[doc(hidden)]
209pub mod bezier_triangle;
210pub use bezier_triangle::{BezierTriangle, BezierTriangleRefusal, TrianglePiece, TriangleSide};
211#[path = "geometry/t_spline.rs"]
212#[doc(hidden)]
213pub mod t_spline;
214pub use t_spline::{TAnchor, TMesh, TMeshSegment, TSpline, TSplineRefusal};
215#[path = "geometry/coons.rs"]
216#[doc(hidden)]
217pub mod coons;
218pub use coons::{
219 coons_bilinear, coons_bilinear_reported, coons_with_ribbons, CompatWork, CoonsRefusal,
220 CoonsReport, RibbonReport,
221};
222#[path = "geometry/gordon.rs"]
223#[doc(hidden)]
224pub mod gordon;
225pub use gordon::{gordon_surface, gordon_surface_reported, GordonRefusal, GordonReport};
226#[path = "geometry/n_sided.rs"]
227#[doc(hidden)]
228pub mod n_sided;
229pub use n_sided::{n_sided_fill, n_sided_fill_centre_solved, n_sided_fill_shortening_reach, n_sided_fill_shortening_reach_spokes_off_sides, reentrant_corners as n_sided_reentrant_corners, NSidedRefusal, NSidedReport};
230#[path = "geometry/regularity.rs"]
231#[doc(hidden)]
232pub mod regularity;
233pub use regularity::{certify_regular, Irregularity, RegularityReport};
234#[path = "geometry/analytic_surface.rs"]
235#[doc(hidden)]
236pub mod analytic_surface;
237pub use analytic_surface::{
238 circle_angle_to_parameter, intersect_analytic_pair, plane_ruled_section_arc,
239 revolution_structure, AnalyticSurface, RevolutionFrame, RevolutionStructure,
240};
241pub use analytic_surface::{
242 intersect_analytic_pair_with, ruled_gate,
243 ruled_gate_census, RuledGate,
244};
245#[path = "geometry/sphere_chart.rs"]
246#[doc(hidden)]
247pub mod sphere_chart;
248pub use sphere_chart::{
249 canonicalize_points, chart_grid_divisions, chart_grid_parameters, corner_index,
250 cube_edge_corner, cube_edge_divisions, cube_edge_index, cube_edge_parameters,
251 cube_edge_parts, Chart, ChartSegment, ChartSide, SphereAtlas,
252 SphericalRegion, CHART_COUNT, CUBE_CORNER_COUNT, CUBE_EDGE_COUNT,
253};
254#[path = "geometry/projection.rs"]
255#[doc(hidden)]
256pub mod projection;
257#[path = "geometry/projection_profile.rs"]
258#[doc(hidden)]
259pub mod projection_profile;
260#[doc(hidden)]
261pub use projection_profile::report as projection_profile_report;
262#[path = "geometry/affine_sphere.rs"]
263#[doc(hidden)]
264pub mod affine_sphere;
265pub use projection::{
266 project_point_to_curve, project_point_to_surface, project_point_to_surface_seeded,
267 CurveProjection, SurfaceProjection,
268};
269#[doc(hidden)]
270pub use projection::{
271 project_point_to_surface_basin_census, project_point_to_surface_general_lanes, BasinCensus,
272 GlobalStats,
273};
274#[path = "intersect/curve_surface_intersection.rs"]
275#[doc(hidden)]
276pub mod curve_surface_intersection;
277pub use curve_surface_intersection::{intersect_curve_surface, CurveSurfaceIntersection};
278#[path = "intersect/exact_line_surface.rs"]
279#[doc(hidden)]
280pub mod exact_line_surface;
281#[path = "intersect/curve_clear.rs"]
282#[doc(hidden)]
283pub mod curve_clear;
284#[path = "intersect/curve_curve_intersection.rs"]
285#[doc(hidden)]
286pub mod curve_curve_intersection;
287pub use curve_curve_intersection::{intersect_curves, CurveCurveIntersection};
288#[path = "intersect/surface_surface_intersection.rs"]
289#[doc(hidden)]
290pub mod surface_surface_intersection;
291pub use surface_surface_intersection::{
292 intersect_surfaces, intersect_surfaces_supplemental, SurfaceIntersectionCurve,
293 SurfaceIntersectionOptions,
294};
295pub use surface_surface_intersection::TRANSVERSE_SEED_CROSS;
296pub use surface_surface_intersection::{carriers_proved_apart, hull_sets_proved_apart, separation_prefilter_enabled, SpanHullSet};
297#[path = "intersect/tangent_curve.rs"]
298#[doc(hidden)]
299pub mod tangent_curve;
300#[path = "intersect/crossing_node.rs"]
301#[doc(hidden)]
302pub mod crossing_node;
303pub use crossing_node::{crossing_node_split_enabled, split_at_crossing_nodes};
304pub use tangent_curve::{
305 classify_tangent_contact_at, find_tangent_contact, trace_tangent_curve, TangentContact,
306 EXTENDED_RANK_FLOOR,
307};
308#[path = "geometry/spatial.rs"]
309#[doc(hidden)]
310pub mod spatial;
311pub use spatial::{Aabb, Bvh};
312#[path = "intersect/arrangement.rs"]
313#[doc(hidden)]
314pub mod arrangement;
315pub use arrangement::{
316 arrange_segments, point_in_polygon, segment_intersection, ArrangementPiece, ArrangementRegion,
317 CycleUse, Segment2, Vec2,
318};
319#[path = "geometry/pcurve.rs"]
320#[doc(hidden)]
321pub mod pcurve;
322pub use pcurve::{
323 build_pcurve_on_surface, build_pcurve_on_surface_marched, build_pcurve_on_surface_range,
324 build_pcurve_on_surface_range_dense, build_pcurve_on_surface_stations, fit_pcurve_on_surface,
325 fit_pcurve_on_surface_marched, fit_pcurve_on_surface_range, fit_pcurve_on_surface_range_dense,
326 fit_pcurve_on_surface_stations, PcurveFit, PcurveFitExit, PcurveFitLedger, PcurveFitReport,
327 PcurveFitScope, PCURVE_OFF_BAR, PCURVE_REFINEMENT_TOLERANCE,
328};
329#[path = "offset/point.rs"]
334#[doc(hidden)]
335pub mod offset_point;
336pub use offset_point::{OffsetEvaluator, OffsetNormal, OffsetSample};
337#[path = "offset/analytic_pair.rs"]
344#[doc(hidden)]
345pub mod offset_analytic_pair;
346#[path = "solvers/linear_algebra.rs"]
347#[doc(hidden)]
348pub mod solver_linear_algebra;
349
350#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
351pub struct Vec3 {
352 pub x: f64,
353 pub y: f64,
354 pub z: f64,
355}
356
357impl Vec3 {
358 pub const ZERO: Self = Self { x: 0.0, y: 0.0, z: 0.0 };
359 pub const X: Self = Self { x: 1.0, y: 0.0, z: 0.0 };
360 pub const Y: Self = Self { x: 0.0, y: 1.0, z: 0.0 };
361 pub const Z: Self = Self { x: 0.0, y: 0.0, z: 1.0 };
362
363 pub fn normalized_or(self, fallback: Self) -> Self {
365 self.normalized().unwrap_or(fallback)
366 }
367
368 #[inline]
370 pub fn rotate_about(self, axis: Self, angle: f64) -> Self {
371 let (sin, cos) = angle.sin_cos();
372 self.scale(cos)
373 .add(axis.cross(self).scale(sin))
374 .add(axis.scale(axis.dot(self) * (1.0 - cos)))
375 }
376
377 #[inline]
379 pub fn distance_to_segment(self, start: Self, end: Self, guard: f64) -> f64 {
380 let segment = end.sub(start);
381 let length_squared = segment.dot(segment);
382 if length_squared <= guard {
383 return self.sub(start).length();
384 }
385 let parameter = (self.sub(start).dot(segment) / length_squared).clamp(0.0, 1.0);
386 self.sub(start.add(segment.scale(parameter))).length()
387 }
388
389 #[inline]
391 pub fn distance_to_segment_squared(self, start: Self, end: Self) -> f64 {
392 let segment = end.sub(start);
393 let length_squared = segment.dot(segment);
394 let parameter = if length_squared > 0.0 {
395 (self.sub(start).dot(segment) / length_squared).clamp(0.0, 1.0)
396 } else {
397 0.0
398 };
399 let residual = self.sub(start.add(segment.scale(parameter)));
400 residual.dot(residual)
401 }
402
403 pub fn new(x: f64, y: f64, z: f64) -> Self {
404 Self { x, y, z }
405 }
406 pub fn sub(self, rhs: Self) -> Self {
407 Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
408 }
409 pub fn add(self, rhs: Self) -> Self {
410 Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
411 }
412 pub fn scale(self, factor: f64) -> Self {
413 Self::new(self.x * factor, self.y * factor, self.z * factor)
414 }
415 pub fn cross(self, rhs: Self) -> Self {
416 Self::new(
417 self.y * rhs.z - self.z * rhs.y,
418 self.z * rhs.x - self.x * rhs.z,
419 self.x * rhs.y - self.y * rhs.x,
420 )
421 }
422 pub fn dot(self, rhs: Self) -> f64 {
423 self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
424 }
425 pub fn length(self) -> f64 {
426 self.dot(self).sqrt()
427 }
428 pub fn length_squared(self) -> f64 {
429 self.dot(self)
430 }
431 pub fn normalized(self) -> Result<Self, String> {
432 let length = self.length();
433 if length <= 1e-12 {
434 return Err("Vec3.normalized: zero-length vector".into());
435 }
436 Ok(self.scale(1.0 / length))
437 }
438 pub fn perpendicular(self) -> Result<Self, String> {
439 let x = self.x.abs();
440 let y = self.y.abs();
441 let z = self.z.abs();
442 let axis = if x <= y && x <= z {
443 Self::new(1.0, 0.0, 0.0)
444 } else if y <= z {
445 Self::new(0.0, 1.0, 0.0)
446 } else {
447 Self::new(0.0, 0.0, 1.0)
448 };
449 self.cross(axis).normalized()
450 }
451}
452
453#[derive(Clone, Debug, Default, Deserialize, Serialize)]
454pub struct Mesh {
455 pub positions: Vec<f64>,
456 pub normals: Vec<f64>,
457 pub indices: Vec<u32>,
458 pub face_ids: Vec<u32>,
459}
460
461impl Mesh {
462 fn push_vertex(&mut self, point: Vec3, normal: Vec3) -> u32 {
463 self.positions.extend([point.x, point.y, point.z]);
464 self.normals.extend([normal.x, normal.y, normal.z]);
465 (self.positions.len() / 3 - 1) as u32
466 }
467
468 fn quad(&mut self, points: [Vec3; 4], normal: Vec3, face_id: u32) {
469 let base = self.push_vertex(points[0], normal);
470 for point in points.iter().skip(1) {
471 self.push_vertex(*point, normal);
472 }
473 self.indices
474 .extend([base, base + 1, base + 2, base, base + 2, base + 3]);
475 self.face_ids.extend([face_id, face_id]);
476 }
477
478 pub fn signed_volume(&self) -> f64 {
479 self.indices
480 .chunks_exact(3)
481 .map(|tri| {
482 let point = |index: u32| {
483 let i = index as usize * 3;
484 Vec3::new(
485 self.positions[i],
486 self.positions[i + 1],
487 self.positions[i + 2],
488 )
489 };
490 point(tri[0]).dot(point(tri[1]).cross(point(tri[2]))) / 6.0
491 })
492 .sum()
493 }
494
495 pub fn validate(&self) -> Result<(), String> {
496 if self.positions.len() % 3 != 0 || self.normals.len() != self.positions.len() {
497 return Err("invalid vertex or normal buffer".into());
498 }
499 self.validate_geometry()
500 }
501
502 pub fn validate_geometry(&self) -> Result<(), String> {
507 if self.positions.len() % 3 != 0 {
508 return Err("invalid vertex buffer".into());
509 }
510 if self.indices.len() % 3 != 0 {
511 return Err("index buffer is not triangular".into());
512 }
513 let count = self.positions.len() / 3;
514 if self.indices.iter().any(|index| *index as usize >= count) {
515 return Err("index outside vertex buffer".into());
516 }
517 if !self.positions.iter().all(|value| value.is_finite()) {
518 return Err("non-finite vertex".into());
519 }
520 Ok(())
521 }
522}
523
524pub fn make_box(size_x: f64, size_y: f64, size_z: f64) -> Mesh {
525 let x = size_x.abs();
526 let y = size_y.abs();
527 let z = size_z.abs();
528 let p = [
529 Vec3::new(0.0, 0.0, 0.0),
530 Vec3::new(x, 0.0, 0.0),
531 Vec3::new(x, y, 0.0),
532 Vec3::new(0.0, y, 0.0),
533 Vec3::new(0.0, 0.0, z),
534 Vec3::new(x, 0.0, z),
535 Vec3::new(x, y, z),
536 Vec3::new(0.0, y, z),
537 ];
538 let mut mesh = Mesh::default();
539 mesh.quad([p[0], p[3], p[2], p[1]], Vec3::new(0.0, 0.0, -1.0), 0);
540 mesh.quad([p[4], p[5], p[6], p[7]], Vec3::new(0.0, 0.0, 1.0), 1);
541 mesh.quad([p[0], p[1], p[5], p[4]], Vec3::new(0.0, -1.0, 0.0), 2);
542 mesh.quad([p[3], p[7], p[6], p[2]], Vec3::new(0.0, 1.0, 0.0), 3);
543 mesh.quad([p[0], p[4], p[7], p[3]], Vec3::new(-1.0, 0.0, 0.0), 4);
544 mesh.quad([p[1], p[2], p[6], p[5]], Vec3::new(1.0, 0.0, 0.0), 5);
545 mesh
546}
547
548pub fn make_cylinder(radius: f64, height: f64, segments: usize) -> Mesh {
549 let radius = radius.abs();
550 let height = height.abs();
551 let segments = segments.max(3);
552 let mut mesh = Mesh::default();
553 for i in 0..segments {
554 let a = std::f64::consts::TAU * i as f64 / segments as f64;
555 let b = std::f64::consts::TAU * (i + 1) as f64 / segments as f64;
556 let (sa, ca) = a.sin_cos();
557 let (sb, cb) = b.sin_cos();
558 let pa = Vec3::new(radius * ca, 0.0, -radius * sa);
559 let pb = Vec3::new(radius * cb, 0.0, -radius * sb);
560 let ta = Vec3::new(pa.x, height, pa.z);
561 let tb = Vec3::new(pb.x, height, pb.z);
562 mesh.quad(
563 [pa, pb, tb, ta],
564 Vec3::new((ca + cb) * 0.5, 0.0, -(sa + sb) * 0.5),
565 0,
566 );
567 let bottom = mesh.push_vertex(Vec3::default(), Vec3::new(0.0, -1.0, 0.0));
568 let bi = mesh.push_vertex(pb, Vec3::new(0.0, -1.0, 0.0));
569 let bj = mesh.push_vertex(pa, Vec3::new(0.0, -1.0, 0.0));
570 mesh.indices.extend([bottom, bi, bj]);
571 mesh.face_ids.push(1);
572 let top = mesh.push_vertex(Vec3::new(0.0, height, 0.0), Vec3::new(0.0, 1.0, 0.0));
573 let ti = mesh.push_vertex(ta, Vec3::new(0.0, 1.0, 0.0));
574 let tj = mesh.push_vertex(tb, Vec3::new(0.0, 1.0, 0.0));
575 mesh.indices.extend([top, ti, tj]);
576 mesh.face_ids.push(2);
577 }
578 mesh
579}
580
581#[path = "healing/curve_helpers.rs"]
582#[doc(hidden)]
583pub mod curve_helpers;
584
585#[path = "geometry/eigen2.rs"]
586#[doc(hidden)]
587pub mod eigen2;
588
589#[path = "geometry/union_find.rs"]
590#[doc(hidden)]
591pub mod union_find;
592pub use union_find::UnionFind;
593
594#[path = "geometry/poly.rs"]
595#[doc(hidden)]
596pub mod poly;
597
598pub use analytic_surface::{circumcenter, circumcenter_2d, recognize_circle, RecognizedCircle};
599
600#[path = "geometry/triangle.rs"]
601#[doc(hidden)]
602pub mod triangle;
603pub use triangle::closest_point_on_triangle;
604
605pub use tolerance::RIGID_TOL;
606
607impl std::ops::Index<usize> for Vec3 {
608 type Output = f64;
609 fn index(&self, index: usize) -> &Self::Output {
610 match index { 0 => &self.x, 1 => &self.y, 2 => &self.z, _ => panic!("Vec3 index out of bounds: {index}") }
611 }
612}
613impl std::ops::IndexMut<usize> for Vec3 {
614 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
615 match index { 0 => &mut self.x, 1 => &mut self.y, 2 => &mut self.z, _ => panic!("Vec3 index out of bounds: {index}") }
616 }
617}
618
619#[doc(hidden)]
621pub const SOURCE_HASH: Option<&str> = option_env!("BREP_KERNEL_SOURCE_HASH");
622#[doc(hidden)]
623#[path = "instrument/work_counts.rs"]
624pub mod work_counts;
625#[doc(hidden)]
626#[path = "instrument/reuse_profile.rs"]
627pub mod reuse_profile;
628#[doc(hidden)]
629#[path = "instrument/carried_uv.rs"]
630pub mod carried_uv;
631#[doc(hidden)]
632#[path = "instrument/blend_timing.rs"]
633pub mod blend_timing;
634#[doc(hidden)]
635#[path = "geometry/identity_hash.rs"]
636pub mod identity_hash;
637#[doc(hidden)]
638#[path = "geometry/trimmed_curve.rs"]
639pub mod trimmed_curve;