use serde::{Deserialize, Serialize};
use crate::*;
#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ProfileSegment {
Line {
start: [f64; 2],
end: [f64; 2],
},
Arc {
start: [f64; 2],
mid: [f64; 2],
end: [f64; 2],
},
Circle {
center: [f64; 2],
radius: f64,
},
Ellipse {
center: [f64; 2],
major: [f64; 2],
ratio: f64,
},
EllipseArc {
center: [f64; 2],
major: [f64; 2],
ratio: f64,
start_param: f64,
end_param: f64,
},
BSpline {
control_points: Vec<[f64; 2]>,
periodic: bool,
},
Nurbs {
degree: u32,
knots: Vec<f64>,
control_points: Vec<[f64; 2]>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
weights: Vec<f64>,
#[serde(default)]
periodic: bool,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PipePath {
Profile(Profile),
Edges(Vec<EdgeProbe>),
EdgesOf {
shape: Vec<u8>,
transform: Option<Box<[[f64; 4]; 4]>>,
edges: Vec<EdgeProbe>,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum LoftSection {
Profile(Profile),
Face(FaceProbe),
Point([f64; 3]),
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct ProfileWire {
pub segments: Vec<ProfileSegment>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub names: Vec<TopoName>,
}
impl ProfileWire {
pub fn new(segments: Vec<ProfileSegment>) -> Self {
Self {
segments,
names: Vec::new(),
}
}
pub fn name_of(&self, index: usize) -> Option<TopoName> {
self.names.get(index).copied().filter(|n| *n != 0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct ProfilePlane {
pub origin: [f64; 3],
pub x_axis: [f64; 3],
pub y_axis: [f64; 3],
pub normal: [f64; 3],
}
impl Default for ProfilePlane {
fn default() -> Self {
Self {
origin: [0.0; 3],
x_axis: [1.0, 0.0, 0.0],
y_axis: [0.0, 1.0, 0.0],
normal: [0.0, 0.0, 1.0],
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BooleanOp {
NewSolid,
Fuse,
Cut,
Common,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Profile {
pub plane: ProfilePlane,
pub wires: Vec<ProfileWire>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ExtrudeTermination {
Blind { distance: f64 },
ThroughAll,
UpToPlane {
point: [f64; 3],
normal: [f64; 3],
offset: f64,
},
UpToFace {
point: [f64; 3],
normal: [f64; 3],
offset: f64,
#[serde(default)]
name: TopoName,
},
ToFirst,
ToLast,
UpToShape { faces: Vec<FaceProbe>, offset: f64 },
UpToFaceOf {
shape: Vec<u8>,
transform: Option<Box<[[f64; 4]; 4]>>,
point: [f64; 3],
offset: f64,
},
UpToShapeOf {
faces: Vec<FaceProbe>,
others: Vec<FacesOf>,
offset: f64,
},
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct FacesOf {
pub shape: Vec<u8>,
pub transform: Option<Box<[[f64; 4]; 4]>>,
pub points: Vec<[f64; 3]>,
}
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub enum RevolveTermination {
#[default]
Angle,
ToFirst,
ToLast,
UpToFace(FaceProbe),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SweepKind {
Extrude {
termination: ExtrudeTermination,
second_side: Option<ExtrudeTermination>,
symmetric: bool,
reversed: bool,
taper_deg: f64,
direction: Option<[f64; 3]>,
},
Revolve {
axis_origin: [f64; 2],
axis_dir: [f64; 2],
angle_deg: f64,
second_angle_deg: Option<f64>,
midplane: bool,
reversed: bool,
#[serde(default)]
termination: RevolveTermination,
},
Helix {
axis_origin: [f64; 2],
axis_dir: [f64; 2],
pitch: f64,
height: f64,
left_handed: bool,
cone_angle_deg: f64,
reversed: bool,
#[serde(default)]
turns: Option<f64>,
#[serde(default)]
growth: Option<f64>,
},
HelixNormal {
axis_origin: [f64; 2],
pitch: f64,
height: f64,
left_handed: bool,
cone_angle_deg: f64,
reversed: bool,
#[serde(default)]
turns: Option<f64>,
#[serde(default)]
growth: Option<f64>,
},
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub enum PipeFrame {
#[default]
RotationMinimizing,
Frenet,
Auxiliary { path: Profile },
Binormal { direction: [f64; 3] },
Fixed,
}
impl PipeFrame {
pub fn from_frenet(frenet: bool) -> Self {
if frenet {
PipeFrame::Frenet
} else {
PipeFrame::RotationMinimizing
}
}
}
pub fn deserialize_pipe_frame<'de, D>(deserializer: D) -> Result<PipeFrame, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum Stored {
Flag(bool),
Frame(PipeFrame),
}
Ok(match Stored::deserialize(deserializer)? {
Stored::Flag(frenet) => PipeFrame::from_frenet(frenet),
Stored::Frame(frame) => frame,
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum PipeCorner {
#[default]
Transformed,
Right,
Round,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum PrimitiveKind {
Box {
length: f64,
width: f64,
height: f64,
},
Cylinder {
radius: f64,
height: f64,
angle_deg: f64,
},
Sphere {
radius: f64,
angle1_deg: f64,
angle2_deg: f64,
angle3_deg: f64,
},
Cone {
radius1: f64,
radius2: f64,
height: f64,
angle_deg: f64,
},
Torus {
radius1: f64,
radius2: f64,
angle1_deg: f64,
angle2_deg: f64,
angle3_deg: f64,
},
Ellipsoid {
radius1: f64,
radius2: f64,
radius3: f64,
#[serde(default = "south_pole")]
angle1_deg: f64,
#[serde(default = "north_pole")]
angle2_deg: f64,
#[serde(default = "full_turn")]
angle3_deg: f64,
},
Prism {
sides: u32,
circumradius: f64,
height: f64,
#[serde(default)]
skew_x_deg: f64,
#[serde(default)]
skew_y_deg: f64,
},
Tube { outer: f64, inner: f64, height: f64 },
Wedge {
xmin: f64,
xmax: f64,
ymin: f64,
ymax: f64,
zmin: f64,
zmax: f64,
x2min: f64,
x2max: f64,
z2min: f64,
z2max: f64,
},
}
fn south_pole() -> f64 {
-90.0
}
fn north_pole() -> f64 {
90.0
}
fn full_turn() -> f64 {
360.0
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Placement {
pub origin: [f64; 3],
pub x_axis: [f64; 3],
pub z_axis: [f64; 3],
}
impl Default for Placement {
fn default() -> Self {
Self {
origin: [0.0; 3],
x_axis: [1.0, 0.0, 0.0],
z_axis: [0.0, 0.0, 1.0],
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum EdgeSelection {
All,
OfFaces(Vec<[f64; 3]>),
OfPickedFaces(Vec<FaceProbe>),
Near(Vec<[f64; 3]>),
Picked(Vec<EdgeProbe>),
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
pub struct EdgeProbe {
pub point: [f64; 3],
pub direction: [f64; 3],
#[serde(default)]
pub faces: [TopoName; 2],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum ThicknessJoin {
#[default]
Intersection,
Arc,
}
impl ThicknessJoin {
pub const ALL: [ThicknessJoin; 2] = [ThicknessJoin::Intersection, ThicknessJoin::Arc];
pub fn label(&self) -> &'static str {
match self {
ThicknessJoin::Intersection => "Intersection",
ThicknessJoin::Arc => "Arc",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash, Serialize, Deserialize)]
pub enum FilletCorner {
#[default]
Setback,
RollingBall,
}
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub enum RoundProfile {
#[default]
Elliptical,
Conic { rho: f64 },
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct RoundDistances {
pub other: f64,
pub flip: bool,
pub profile: RoundProfile,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VariableRadius {
pub end: f64,
#[serde(default)]
pub between: Vec<[f64; 2]>,
#[serde(default)]
pub reversed: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash, Serialize, Deserialize)]
pub enum ChamferCorner {
#[default]
Point,
Facet,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum ChamferSpec {
EqualDistance {
distance: f64,
},
Auto {
distance: f64,
},
TwoDistances {
distance1: f64,
distance2: f64,
},
DistanceAngle {
distance: f64,
angle_deg: f64,
},
}
impl SolidOp {
pub fn size_limit_key(&self) -> Option<u64> {
use std::hash::{Hash, Hasher};
let sized = match self {
SolidOp::Fillet {
edges,
follow_tangent,
corner,
distances,
auto,
variable: None,
..
} => SolidOp::Fillet {
radius: 0.0,
edges: edges.clone(),
follow_tangent: *follow_tangent,
corner: *corner,
distances: distances.map(|d| RoundDistances { other: 0.0, ..d }),
auto: *auto,
variable: None,
},
SolidOp::Chamfer {
spec,
flip,
edges,
follow_tangent,
corner,
} => SolidOp::Chamfer {
spec: match *spec {
ChamferSpec::EqualDistance { .. } => {
ChamferSpec::EqualDistance { distance: 0.0 }
}
ChamferSpec::Auto { .. } => ChamferSpec::Auto { distance: 0.0 },
ChamferSpec::TwoDistances { .. } => ChamferSpec::TwoDistances {
distance1: 0.0,
distance2: 0.0,
},
ChamferSpec::DistanceAngle { angle_deg, .. } => ChamferSpec::DistanceAngle {
distance: 0.0,
angle_deg,
},
},
flip: *flip,
edges: edges.clone(),
follow_tangent: *follow_tangent,
corner: *corner,
},
_ => return None,
};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
format!("{sized:?}").hash(&mut hasher);
Some(hasher.finish())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BoolKind {
Fuse,
Cut,
Common,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SolidOp {
Shape { brep: Vec<u8> },
Sweep {
profile: Profile,
kind: SweepKind,
op: BooleanOp,
},
SweepFace {
face: FaceProbe,
kind: SweepKind,
op: BooleanOp,
},
SweepFaceOf {
shape: Vec<u8>,
transform: Option<Box<[[f64; 4]; 4]>>,
point: [f64; 3],
kind: SweepKind,
op: BooleanOp,
},
RevolveToFaceOf {
profile: Profile,
kind: SweepKind,
shape: Vec<u8>,
transform: Option<Box<[[f64; 4]; 4]>>,
point: [f64; 3],
op: BooleanOp,
},
LoftThrough {
sections: Vec<LoftSection>,
ruled: bool,
closed: bool,
op: BooleanOp,
},
Loft {
sections: Vec<Profile>,
ruled: bool,
closed: bool,
op: BooleanOp,
},
Pipe {
profile: Profile,
spine: Profile,
#[serde(default, alias = "frenet", deserialize_with = "deserialize_pipe_frame")]
frame: PipeFrame,
#[serde(default)]
corner: PipeCorner,
#[serde(default)]
sections: Vec<Profile>,
op: BooleanOp,
},
PipeThrough {
profile: LoftSection,
path: PipePath,
#[serde(default)]
frame: PipeFrame,
#[serde(default)]
corner: PipeCorner,
#[serde(default)]
sections: Vec<LoftSection>,
op: BooleanOp,
},
Primitive {
kind: PrimitiveKind,
placement: Placement,
op: BooleanOp,
},
Fillet {
radius: f64,
edges: EdgeSelection,
#[serde(default)]
follow_tangent: bool,
#[serde(default)]
corner: FilletCorner,
#[serde(default)]
distances: Option<RoundDistances>,
#[serde(default)]
auto: bool,
#[serde(default)]
variable: Option<VariableRadius>,
},
BlendFaces { radius: f64, faces: [FaceProbe; 2] },
Chamfer {
spec: ChamferSpec,
flip: bool,
edges: EdgeSelection,
#[serde(default)]
follow_tangent: bool,
#[serde(default)]
corner: ChamferCorner,
},
Draft {
angle_deg: f64,
neutral_point: [f64; 3],
neutral_normal: [f64; 3],
pull_dir: Option<[f64; 3]>,
faces: Vec<[f64; 3]>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
face_names: Vec<TopoName>,
},
Refine,
OffsetFaces {
faces: Vec<[f64; 3]>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
face_names: Vec<TopoName>,
distance: f64,
},
MoveFaces {
faces: Vec<[f64; 3]>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
face_names: Vec<TopoName>,
transform: [[f64; 4]; 4],
},
RemoveFaces {
faces: Vec<[f64; 3]>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
face_names: Vec<TopoName>,
},
Thickness {
value: f64,
open_faces: Vec<[f64; 3]>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
open_face_names: Vec<TopoName>,
inward: bool,
#[serde(default)]
join: ThicknessJoin,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
both_sides: bool,
},
Transform {
transforms: Vec<[[f64; 4]; 4]>,
originals: Vec<usize>,
},
TransformCopies {
transforms: Vec<[[f64; 4]; 4]>,
originals: Vec<usize>,
},
Pattern {
steps: Vec<PatternStep>,
originals: Vec<usize>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
skipped: Vec<u32>,
},
Boolean {
tool_brep: Vec<u8>,
kind: BoolKind,
#[serde(default)]
tool_transform: Option<[[f64; 4]; 4]>,
},
Surface(SurfaceOp),
WithTolerance { tolerance: f64, op: Box<SolidOp> },
Project {
profile: Profile,
faces: Vec<FaceProbe>,
mode: ProjectMode,
height: f64,
},
Wrap {
profile: Profile,
faces: Vec<FaceProbe>,
mode: ProjectMode,
height: f64,
},
HalfSpace { origin: [f64; 3], normal: [f64; 3] },
OffsetSolid { distance: f64 },
Scale {
factors: [f64; 3],
centre: Option<[f64; 3]>,
},
KeepPiece { near: [f64; 3] },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum ProjectMode {
#[default]
Split,
Emboss,
Deboss,
}
impl ProjectMode {
pub const ALL: [ProjectMode; 3] =
[ProjectMode::Split, ProjectMode::Emboss, ProjectMode::Deboss];
pub fn label(&self) -> &'static str {
match self {
ProjectMode::Split => "Split lines",
ProjectMode::Emboss => "Emboss",
ProjectMode::Deboss => "Deboss",
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum CurveSource {
Sketch {
plane: ProfilePlane,
wire: ProfileWire,
},
Edge(EdgeProbe),
BodyEdge {
brep: Vec<u8>,
edge: EdgeProbe,
#[serde(default)]
transform: Option<Box<[[f64; 4]; 4]>>,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum Continuity {
#[default]
G0,
G1,
G2,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SurfaceOp {
Extrude {
curves: Vec<CurveSource>,
direction: [f64; 3],
length: f64,
#[serde(default)]
symmetric: bool,
},
Revolve {
curves: Vec<CurveSource>,
origin: [f64; 3],
axis: [f64; 3],
angle_deg: f64,
},
PlanarFill { curves: Vec<CurveSource> },
Fill {
boundary: Vec<CurveSource>,
#[serde(default)]
continuity: Continuity,
},
Ruled {
first: CurveSource,
second: CurveSource,
},
Loft {
sections: Vec<CurveSource>,
#[serde(default)]
closed: bool,
},
Sweep {
profile: Vec<CurveSource>,
path: Vec<CurveSource>,
#[serde(default)]
frame: PipeFrame,
},
GuidedLoft {
sections: Vec<CurveSource>,
guides: Vec<CurveSource>,
},
SweepTwoRails {
profile: Vec<CurveSource>,
first_rail: Vec<CurveSource>,
second_rail: Vec<CurveSource>,
},
Offset {
faces: Vec<FaceProbe>,
distance: f64,
},
Extend {
edges: Vec<EdgeProbe>,
length: f64,
#[serde(default)]
continuity: Continuity,
},
Blend {
first: EdgeProbe,
second: EdgeProbe,
#[serde(default)]
continuity: Continuity,
},
Split {
faces: Vec<FaceProbe>,
curves: Vec<CurveSource>,
},
Sew {
#[serde(default)]
gap: f64,
},
Fillet { edges: Vec<EdgeProbe>, radius: f64 },
FilletFaces {
first: FaceProbe,
second: FaceProbe,
radius: f64,
#[serde(default)]
flip: [bool; 2],
},
Thicken {
thickness: f64,
#[serde(default)]
both_sides: bool,
},
TrimByPlane { origin: [f64; 3], normal: [f64; 3] },
Mirror { origin: [f64; 3], normal: [f64; 3] },
TrimByScan {
scan: Vec<[f32; 3]>,
triangles: Vec<u32>,
tolerance: f64,
share: f64,
},
FitScattered {
points: Vec<[f64; 3]>,
tolerance: f64,
},
FitNetwork {
curves: Vec<CurveSource>,
polylines: Vec<Vec<[f64; 3]>>,
#[serde(default)]
patches: Vec<[i32; 4]>,
#[serde(default)]
chained: Vec<[Vec<i32>; 4]>,
#[serde(default)]
corrections: u32,
points: Vec<[f64; 3]>,
tolerance: f64,
smoothing: f64,
creases: Vec<u32>,
},
}
impl SurfaceOp {
pub fn constructs(&self) -> bool {
!matches!(
self,
SurfaceOp::Sew { .. }
| SurfaceOp::Fillet { .. }
| SurfaceOp::FilletFaces { .. }
| SurfaceOp::Thicken { .. }
| SurfaceOp::TrimByPlane { .. }
| SurfaceOp::Mirror { .. }
| SurfaceOp::Split { .. }
| SurfaceOp::Extend { .. }
| SurfaceOp::TrimByScan { .. }
)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PatternStep {
Transforms(Vec<[[f64; 4]; 4]>),
Path(PathLayout),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PatternPath {
Wire {
plane: ProfilePlane,
wire: ProfileWire,
},
Edge(EdgeProbe),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PathLayout {
pub path: PatternPath,
pub count: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub spacing: Option<f64>,
#[serde(default)]
pub reversed: bool,
#[serde(default)]
pub align: bool,
}
impl SolidOp {
pub fn inner(&self) -> &SolidOp {
match self {
SolidOp::WithTolerance { op, .. } => op.inner(),
op => op,
}
}
pub fn boolean_tolerance(&self) -> Option<f64> {
match self {
SolidOp::WithTolerance { tolerance, .. } => Some(*tolerance),
_ => None,
}
}
pub fn with_boolean_tolerance(self, tolerance: Option<f64>) -> SolidOp {
match tolerance {
Some(tolerance) => SolidOp::WithTolerance {
tolerance,
op: Box::new(self),
},
None => self,
}
}
pub fn starts_shape(&self) -> bool {
match self.inner() {
SolidOp::Surface(op) => op.constructs(),
_ => self.boolean_op() == Some(BooleanOp::NewSolid),
}
}
pub fn boolean_op(&self) -> Option<BooleanOp> {
match self.inner() {
SolidOp::Sweep { op, .. }
| SolidOp::SweepFace { op, .. }
| SolidOp::SweepFaceOf { op, .. }
| SolidOp::RevolveToFaceOf { op, .. }
| SolidOp::Loft { op, .. }
| SolidOp::LoftThrough { op, .. }
| SolidOp::PipeThrough { op, .. }
| SolidOp::Pipe { op, .. }
| SolidOp::Primitive { op, .. } => Some(*op),
SolidOp::Shape { .. } => Some(BooleanOp::NewSolid),
_ => None,
}
}
}