kernel_api/lib.rs
1//! printCAD's geometry interface: meshes, profiles and solid operations
2//! as plain data, with no geometry code beyond what a three-point arc
3//! means ([`arc`]), which every reader of a profile shares, and the
4//! nearest point of a segment or a triangle ([`nearest`]), which every
5//! pick and snap shares, where a ray crosses a triangle ([`ray`]), a
6//! polygon's area and what it holds ([`polygon`]), and the box around a
7//! mesh moved by a placement ([`Aabb`]). The app's
8//! kernel adapter answers it, and workbench packages build their solids
9//! as [`SolidOp`] plans through `printcad-bench-api`.
10//!
11//! Versions follow `printcad-bench-api`'s: 0.1.x speaks the workbench
12//! contract `printcad:workbench@0.1`, and a patch release only adds.
13//! [`FaceSurface`] and [`ProfileSegment`], which the app sends a package,
14//! are `#[non_exhaustive]` unless the `exhaustive` feature (printCAD's
15//! own) is on; [`SolidOp`] and the enums inside it keep their variants'
16//! fields within a minor version.
17
18pub mod aabb;
19pub mod arc;
20pub mod naming;
21pub mod nearest;
22pub mod pmi;
23pub mod polygon;
24pub mod ray;
25pub use aabb::Aabb;
26pub use arc::ThreePointArc;
27pub use naming::TopoName;
28pub use pmi::{PmiDatum, PmiDimension, PmiTolerance, SemanticPmi};
29
30/// How near two ends of curves are one point, in millimetres, wherever
31/// curves cross between the kernel and its callers: consecutive segments
32/// of a [`ProfileWire`] meet within it, the kernel chains a section's
33/// curves ([`SectionRun`]) within it, and ends read off a solid's edges
34/// (sectioned, borrowed or projected) that lie within it are one point.
35/// A solid's edges meet at a vertex only within its tolerance, which an
36/// imported or fuzzily joined solid widens, and points read off its mesh
37/// are single precision. A caller deciding what connects on its own
38/// terms, as the sketcher's regions do, joins no wider than this, so
39/// whatever it closes the kernel closes too.
40pub const JOIN_MM: f64 = 1e-3;
41
42mod chain;
43mod fit;
44mod import;
45mod kernel;
46mod mesh;
47mod mesh_op;
48mod point_match;
49mod queries;
50mod query;
51mod solid_op;
52
53pub use chain::*;
54pub use fit::*;
55pub use import::*;
56pub use kernel::*;
57pub use mesh::*;
58pub use mesh_op::*;
59pub use point_match::*;
60pub use queries::*;
61pub use query::*;
62pub use solid_op::*;
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 fn pipe_json(frame: &str) -> String {
69 let plane = r#"{"origin":[0,0,0],"x_axis":[1,0,0],"y_axis":[0,1,0],"normal":[0,0,1]}"#;
70 let profile = format!(
71 r#"{{"plane":{plane},"wires":[{{"segments":[{{"Circle":{{"center":[0,0],"radius":1}}}}]}}]}}"#
72 );
73 format!(r#"{{"Pipe":{{"profile":{profile},"spine":{profile},{frame}"op":"NewSolid"}}}}"#)
74 }
75
76 fn frame_of(json: &str) -> PipeFrame {
77 match serde_json::from_str::<SolidOp>(json).unwrap() {
78 SolidOp::Pipe { frame, .. } => frame,
79 other => panic!("not a pipe: {other:?}"),
80 }
81 }
82
83 #[test]
84 fn a_pipe_written_with_the_frenet_flag_reads_as_its_frame() {
85 assert_eq!(frame_of(&pipe_json(r#""frenet":true,"#)), PipeFrame::Frenet);
86 assert_eq!(
87 frame_of(&pipe_json(r#""frenet":false,"#)),
88 PipeFrame::RotationMinimizing
89 );
90 assert_eq!(frame_of(&pipe_json("")), PipeFrame::RotationMinimizing);
91 }
92
93 #[test]
94 fn a_pipe_frame_round_trips() {
95 let frame = PipeFrame::Binormal {
96 direction: [0.0, 0.0, 1.0],
97 };
98 let text = serde_json::to_string(&frame).unwrap();
99 assert_eq!(frame_of(&pipe_json(&format!(r#""frame":{text},"#))), frame);
100 }
101}