Skip to content

Commit

Permalink
feat(def): utilities for exporting def orientations (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulk29 authored Jul 11, 2024
1 parent e0c4516 commit 43a2b29
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 4 deletions.
19 changes: 19 additions & 0 deletions libs/geometry/src/orientation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::hash::Hash;

use approx::abs_diff_eq;
use serde::{Deserialize, Serialize};

use crate::transform::Transformation;
Expand Down Expand Up @@ -94,6 +95,17 @@ impl NamedOrientation {
pub fn into_orientation(self) -> Orientation {
Orientation::from(self)
}

/// Attempts to convert the given orientation to a named orientation.
///
/// The conversion is based on approximate equality.
/// If no named orientation is approximately equal to `orientation`,
/// returns [`None`].
pub fn from_orientation(orientation: Orientation) -> Option<Self> {
Self::all_rectangular()
.into_iter()
.find(|o| orientation.approx_eq(&o.into_orientation()))
}
}

impl From<NamedOrientation> for Orientation {
Expand Down Expand Up @@ -234,6 +246,13 @@ impl Orientation {
self
}

/// Compares the two orientations for approximate equality.
pub fn approx_eq(&self, other: &Self) -> bool {
let a = self.wrap_angle();
let b = other.wrap_angle();
a.reflect_vert == b.reflect_vert && abs_diff_eq!(a.angle(), b.angle())
}

/// Returns the orientation represented by the given transformation.
///
/// Captures the rotation and reflection encoded by the [`Transformation`],
Expand Down
Loading

0 comments on commit 43a2b29

Please sign in to comment.