summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2020-06-27 17:55:18 -0400
committerTavian Barnes <tavianator@tavianator.com>2020-06-27 17:56:57 -0400
commit66bd7ee3a3d83306614d1096a06ec1e284b51680 (patch)
tree108671aed82950636dfa2914555d0b47681c2ae3
parent49be082296367f90ca717a6cd4100d57823d8b78 (diff)
downloadacap-66bd7ee3a3d83306614d1096a06ec1e284b51680.tar.xz
cos: Add a TryFrom<T> impl for AngularDistance<T>
-rw-r--r--src/cos.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/cos.rs b/src/cos.rs
index 99a08f4..e7f0a87 100644
--- a/src/cos.rs
+++ b/src/cos.rs
@@ -7,6 +7,7 @@ use num_traits::real::Real;
use num_traits::{one, zero};
use std::cmp::Ordering;
+use std::convert::TryFrom;
/// Compute the [cosine *similarity*] between two points.
///
@@ -397,6 +398,7 @@ where
///
/// # use acap::distance::Distance;
/// # use acap::cos::AngularDistance;
+/// # use std::convert::TryFrom;
/// let zero = AngularDistance::from_cos(1.0);
/// let pi_2 = AngularDistance::from_cos(0.0);
/// let pi = AngularDistance::from_cos(-1.0);
@@ -425,8 +427,25 @@ impl<T: PartialOrd> PartialOrd for AngularDistance<T> {
}
}
+/// Error type for failed conversions from angles outside of `$[0, \pi]$` to [`AngularDistance`].
+#[derive(Debug)]
+pub struct InvalidAngleError;
+
macro_rules! impl_distance {
- ($f:ty) => {
+ ($f:ident) => {
+ impl TryFrom<$f> for AngularDistance<$f> {
+ type Error = InvalidAngleError;
+
+ #[inline]
+ fn try_from(value: $f) -> Result<Self, Self::Error> {
+ if value >= 0.0 && value <= std::$f::consts::PI {
+ Ok(Self(value.cos()))
+ } else {
+ Err(InvalidAngleError)
+ }
+ }
+ }
+
impl From<AngularDistance<$f>> for $f {
#[inline]
fn from(value: AngularDistance<$f>) -> $f {