use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum SamplingMode {
TriangleCentroids,
Vertices,
CentroidsAndVertices,
}
#[doc(hidden)]
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
pub enum HypothesisPolicy {
Classical,
Adaptive(AdaptiveSampling),
}
impl Default for HypothesisPolicy {
fn default() -> Self {
Self::Classical
}
}
#[doc(hidden)]
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
#[serde(default)]
#[non_exhaustive]
pub struct AdaptiveSampling {
pub seed_exploration: f64,
pub seed_temperature: f64,
pub kind_floor: f64,
pub probe_floor: f64,
}
impl Default for AdaptiveSampling {
fn default() -> Self {
Self {
seed_exploration: 0.5,
seed_temperature: 1.0,
kind_floor: 0.5,
probe_floor: 0.1,
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(default)]
#[non_exhaustive]
pub struct RecognitionOptions {
pub distance_tolerance: f64,
pub relative_tolerance: f64,
pub normal_tolerance: f64,
pub minimum_support: usize,
pub minimum_support_area: f64,
pub confidence: f64,
pub deterministic_seed: Option<u64>,
pub max_hypotheses: usize,
pub max_refinement_iterations: usize,
pub feature_angle: f64,
pub respect_features: bool,
pub discover_regions: bool,
pub allow_disconnected_same_surface: bool,
pub collect_phase_timings: bool,
pub sampling: SamplingMode,
#[doc(hidden)]
pub hypothesis_policy: HypothesisPolicy,
pub threads: usize,
pub torus_fast: bool,
pub probe_crease_gate: bool,
pub hypothesis_probe_triangles: usize,
}
impl Default for RecognitionOptions {
fn default() -> Self {
Self {
distance_tolerance: 1.0e-6,
relative_tolerance: 1.0e-8,
normal_tolerance: 5.0_f64.to_radians(),
minimum_support: 6,
minimum_support_area: 0.0,
confidence: 0.999,
deterministic_seed: Some(0x4341_4452_414e_5341),
max_hypotheses: 512,
max_refinement_iterations: 160,
feature_angle: 30.0_f64.to_radians(),
respect_features: true,
discover_regions: true,
allow_disconnected_same_surface: false,
collect_phase_timings: false,
sampling: SamplingMode::CentroidsAndVertices,
hypothesis_policy: HypothesisPolicy::Classical,
threads: 0,
torus_fast: false,
probe_crease_gate: false,
hypothesis_probe_triangles: 128,
}
}
}
impl RecognitionOptions {
pub(crate) fn validate(&self) -> Result<(), crate::RecognitionError> {
if !self.distance_tolerance.is_finite() || self.distance_tolerance <= 0.0 {
return Err(crate::RecognitionError::InvalidOptions(
"distance_tolerance must be finite and positive".into(),
));
}
if !self.relative_tolerance.is_finite() || self.relative_tolerance < 0.0 {
return Err(crate::RecognitionError::InvalidOptions(
"relative_tolerance must be finite and non-negative".into(),
));
}
if !(0.0..=std::f64::consts::PI).contains(&self.normal_tolerance) {
return Err(crate::RecognitionError::InvalidOptions(
"normal_tolerance must be in [0, pi]".into(),
));
}
if self.minimum_support == 0 {
return Err(crate::RecognitionError::InvalidOptions(
"minimum_support must be non-zero".into(),
));
}
if !self.minimum_support_area.is_finite() || self.minimum_support_area < 0.0 {
return Err(crate::RecognitionError::InvalidOptions(
"minimum_support_area must be finite and non-negative".into(),
));
}
if !(0.0..1.0).contains(&self.confidence) {
return Err(crate::RecognitionError::InvalidOptions(
"confidence must be in [0, 1)".into(),
));
}
if self.max_hypotheses == 0 || self.max_refinement_iterations == 0 {
return Err(crate::RecognitionError::InvalidOptions(
"iteration limits must be non-zero".into(),
));
}
if !(0.0..=std::f64::consts::PI).contains(&self.feature_angle) {
return Err(crate::RecognitionError::InvalidOptions(
"feature_angle must be finite and in [0, pi]".into(),
));
}
if let HypothesisPolicy::Adaptive(adaptive) = self.hypothesis_policy {
if !(adaptive.seed_exploration > 0.0 && adaptive.seed_exploration <= 1.0) {
return Err(crate::RecognitionError::InvalidOptions(
"seed_exploration must be in (0, 1]".into(),
));
}
if !(adaptive.seed_temperature.is_finite() && adaptive.seed_temperature > 0.0) {
return Err(crate::RecognitionError::InvalidOptions(
"seed_temperature must be finite and positive".into(),
));
}
if !(0.0..=1.0).contains(&adaptive.kind_floor)
|| !(0.0..=1.0).contains(&adaptive.probe_floor)
{
return Err(crate::RecognitionError::InvalidOptions(
"kind_floor and probe_floor must be in [0, 1]".into(),
));
}
}
Ok(())
}
}