diff --git a/crates/fj-math/src/circle.rs b/crates/fj-math/src/circle.rs index 303e8fdd0..f50cd3b0a 100644 --- a/crates/fj-math/src/circle.rs +++ b/crates/fj-math/src/circle.rs @@ -1,6 +1,6 @@ use approx::AbsDiffEq; -use crate::{Point, Scalar, Vector}; +use crate::{Aabb, Point, Scalar, Vector}; /// An n-dimensional circle /// @@ -151,6 +151,16 @@ impl Circle { self.a * cos + self.b * sin } + + /// Calculate an AABB for the circle + pub fn aabb(&self) -> Aabb { + let center_to_min_max = Vector::from_component(self.radius()); + + Aabb { + min: self.center() - center_to_min_max, + max: self.center() + center_to_min_max, + } + } } impl approx::AbsDiffEq for Circle { diff --git a/crates/fj-math/src/vector.rs b/crates/fj-math/src/vector.rs index d30bd745f..a982dd274 100644 --- a/crates/fj-math/src/vector.rs +++ b/crates/fj-math/src/vector.rs @@ -17,23 +17,13 @@ pub struct Vector { } impl Vector { - /// Construct a `Vector` from `f64` components - /// - /// # Panics - /// - /// Panics, if the components can not be converted to [`Scalar`]. See - /// [`Scalar::from_f64`], which this method uses internally. - pub fn from_components_f64(components: [f64; D]) -> Self { + /// Create a vector whose components are all equal + pub fn from_component(scalar: impl Into) -> Self { Self { - components: components.map(Scalar::from_f64), + components: [scalar.into(); D], } } - /// Construct a `Vector` from an nalgebra vector - pub fn from_na(vector: nalgebra::SVector) -> Self { - Self::from_components_f64(vector.into()) - } - /// Convert the vector into an nalgebra vector pub fn to_na(self) -> nalgebra::SVector { self.components.map(Scalar::into_f64).into() @@ -240,21 +230,18 @@ impl Default for Vector { } } -impl From<[Scalar; D]> for Vector { - fn from(components: [Scalar; D]) -> Self { - Self { components } - } -} - -impl From<[f64; D]> for Vector { - fn from(components: [f64; D]) -> Self { - Self::from_components_f64(components) +impl, const D: usize> From<[S; D]> for Vector { + fn from(components: [S; D]) -> Self { + Self { + components: components.map(Into::into), + } } } impl From> for Vector { fn from(vector: nalgebra::SVector) -> Self { - Self::from_na(vector) + let components: [f64; D] = vector.into(); + Vector::from(components) } } diff --git a/crates/fj-viewer/src/input/rotation.rs b/crates/fj-viewer/src/input/rotation.rs index 35ee9591f..45c428085 100644 --- a/crates/fj-viewer/src/input/rotation.rs +++ b/crates/fj-viewer/src/input/rotation.rs @@ -33,10 +33,10 @@ impl Rotation { fn up_vector(rotation: &Transform) -> Vector<3> { let d = rotation.data(); - Vector::from_components_f64([d[4], d[5], d[6]]) + Vector::from([d[4], d[5], d[6]]) } fn right_vector(rotation: &Transform) -> Vector<3> { let d = rotation.data(); - Vector::from_components_f64([d[0], d[1], d[2]]) + Vector::from([d[0], d[1], d[2]]) }