use std::fmt;
use fj_math::{Point, Scalar};
use crate::objects::{Curve, Vertex};
pub fn validate_vertex(
vertex: &Vertex,
max_distance: impl Into<Scalar>,
) -> Result<(), CoherenceIssues> {
let max_distance = max_distance.into();
let local = vertex.position();
let local_as_surface = vertex.curve().path().point_from_path_coords(local);
let local_as_global = vertex
.curve()
.surface()
.point_from_surface_coords(local_as_surface);
let global = vertex.global_form().position();
let distance = (local_as_global - global).magnitude();
if distance > max_distance {
Err(VertexCoherenceMismatch {
local,
local_as_global,
global,
})?
}
Ok(())
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug, thiserror::Error)]
pub enum CoherenceIssues {
#[error("Mismatch between surface and global forms of curve")]
Curve(#[from] CurveCoherenceMismatch),
#[error("Mismatch between local and global coordinates of vertex")]
Vertex(#[from] VertexCoherenceMismatch),
}
#[derive(Debug, thiserror::Error)]
pub struct CurveCoherenceMismatch {
pub point_curve: Point<1>,
pub point_surface: Point<2>,
pub point_surface_as_global: Point<3>,
pub point_global: Point<3>,
pub curve: Curve,
}
impl fmt::Display for CurveCoherenceMismatch {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"local: {:?} (converted to surface: {:?}; to global: {:?}), global: {:?},",
self.point_curve, self.point_surface, self.point_surface_as_global, self.point_global,
)
}
}
#[derive(Debug, Default, thiserror::Error)]
pub struct VertexCoherenceMismatch {
pub local: Point<1>,
pub local_as_global: Point<3>,
pub global: Point<3>,
}
impl fmt::Display for VertexCoherenceMismatch {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"local: {:?} (converted to global: {:?}), global: {:?},",
self.local, self.local_as_global, self.global,
)
}
}