use serde::{Deserialize, Serialize};
use crate::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum LinearDeflectionMode {
#[default]
BboxScaled,
AbsoluteMm,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TessellationSettings {
#[serde(default)]
pub linear_deflection_mode: LinearDeflectionMode,
#[serde(default = "default_mesh_deviation")]
pub mesh_deviation: f32,
pub chord_tolerance: f32,
pub angular_tolerance_deg: f32,
#[serde(default = "default_weld_cross_face")]
pub weld_cross_face: bool,
#[serde(default = "default_weld_angle_threshold_deg")]
pub weld_angle_threshold_deg: f32,
#[serde(default = "default_persist_brep_snapshot")]
pub persist_brep_snapshot: bool,
#[serde(default = "default_generate_boundary_edges")]
pub generate_boundary_edges: bool,
}
fn default_weld_cross_face() -> bool {
false
}
fn default_weld_angle_threshold_deg() -> f32 {
30.0
}
fn default_mesh_deviation() -> f32 {
0.2
}
fn default_persist_brep_snapshot() -> bool {
true
}
fn default_generate_boundary_edges() -> bool {
true
}
impl Default for TessellationSettings {
fn default() -> Self {
Self {
linear_deflection_mode: LinearDeflectionMode::default(),
mesh_deviation: default_mesh_deviation(),
chord_tolerance: 0.1,
angular_tolerance_deg: 28.65,
weld_cross_face: default_weld_cross_face(),
weld_angle_threshold_deg: default_weld_angle_threshold_deg(),
persist_brep_snapshot: default_persist_brep_snapshot(),
generate_boundary_edges: default_generate_boundary_edges(),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TriMesh {
pub positions: Vec<[f32; 3]>,
pub normals: Vec<[f32; 3]>,
pub indices: Vec<u32>,
#[serde(default)]
pub edges: Vec<u32>,
#[serde(default)]
pub colors: Vec<[f32; 3]>,
#[serde(default)]
pub faces: Vec<u32>,
#[serde(default)]
pub edge_ids: Vec<u32>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub face_surfaces: Vec<FaceSurface>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub face_names: Vec<TopoName>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub face_makers: Vec<TopoName>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub edge_faces: Vec<[TopoName; 2]>,
}
#[cfg_attr(not(feature = "exhaustive"), non_exhaustive)]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
pub enum FaceSurface {
Plane { origin: [f32; 3], normal: [f32; 3] },
Cylinder {
origin: [f32; 3],
axis: [f32; 3],
radius: f32,
},
Cone {
apex: [f32; 3],
axis: [f32; 3],
#[serde(default)]
half_angle: f32,
},
Sphere { center: [f32; 3], radius: f32 },
Torus {
center: [f32; 3],
axis: [f32; 3],
#[serde(default)]
major_radius: f32,
#[serde(default)]
minor_radius: f32,
},
#[default]
Other,
}
impl FaceSurface {
pub fn axis(&self) -> Option<([f32; 3], [f32; 3])> {
match *self {
FaceSurface::Cylinder { origin, axis, .. } => Some((origin, axis)),
FaceSurface::Cone { apex, axis, .. } => Some((apex, axis)),
FaceSurface::Torus { center, axis, .. } => Some((center, axis)),
_ => None,
}
}
pub fn moved(
&self,
point: impl Fn([f32; 3]) -> [f32; 3],
direction: impl Fn([f32; 3]) -> [f32; 3],
) -> Self {
match *self {
FaceSurface::Plane { origin, normal } => FaceSurface::Plane {
origin: point(origin),
normal: direction(normal),
},
FaceSurface::Cylinder {
origin,
axis,
radius,
} => FaceSurface::Cylinder {
origin: point(origin),
axis: direction(axis),
radius,
},
FaceSurface::Cone {
apex,
axis,
half_angle,
} => FaceSurface::Cone {
apex: point(apex),
axis: direction(axis),
half_angle,
},
FaceSurface::Sphere { center, radius } => FaceSurface::Sphere {
center: point(center),
radius,
},
FaceSurface::Torus {
center,
axis,
major_radius,
minor_radius,
} => FaceSurface::Torus {
center: point(center),
axis: direction(axis),
major_radius,
minor_radius,
},
FaceSurface::Other => FaceSurface::Other,
}
}
}
impl TriMesh {
pub fn bounds(&self) -> Option<([f32; 3], [f32; 3])> {
if self.positions.is_empty() {
return None;
}
let mut min = self.positions[0];
let mut max = self.positions[0];
for &[x, y, z] in &self.positions {
if x < min[0] {
min[0] = x;
}
if y < min[1] {
min[1] = y;
}
if z < min[2] {
min[2] = z;
}
if x > max[0] {
max[0] = x;
}
if y > max[1] {
max[1] = y;
}
if z > max[2] {
max[2] = z;
}
}
Some((min, max))
}
}