Skip to content

Commit

Permalink
Merge pull request #1887 from hannobraun/math
Browse files Browse the repository at this point in the history
Add `Vector::from_component` and `Circle::aabb`
  • Loading branch information
hannobraun authored Jun 19, 2023
2 parents a914c73 + 01a7dfc commit db40c13
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
12 changes: 11 additions & 1 deletion crates/fj-math/src/circle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use approx::AbsDiffEq;

use crate::{Point, Scalar, Vector};
use crate::{Aabb, Point, Scalar, Vector};

/// An n-dimensional circle
///
Expand Down Expand Up @@ -151,6 +151,16 @@ impl<const D: usize> Circle<D> {

self.a * cos + self.b * sin
}

/// Calculate an AABB for the circle
pub fn aabb(&self) -> Aabb<D> {
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<const D: usize> approx::AbsDiffEq for Circle<D> {
Expand Down
33 changes: 10 additions & 23 deletions crates/fj-math/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,13 @@ pub struct Vector<const D: usize> {
}

impl<const D: usize> Vector<D> {
/// 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<Scalar>) -> 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<f64, D>) -> Self {
Self::from_components_f64(vector.into())
}

/// Convert the vector into an nalgebra vector
pub fn to_na(self) -> nalgebra::SVector<f64, D> {
self.components.map(Scalar::into_f64).into()
Expand Down Expand Up @@ -240,21 +230,18 @@ impl<const D: usize> Default for Vector<D> {
}
}

impl<const D: usize> From<[Scalar; D]> for Vector<D> {
fn from(components: [Scalar; D]) -> Self {
Self { components }
}
}

impl<const D: usize> From<[f64; D]> for Vector<D> {
fn from(components: [f64; D]) -> Self {
Self::from_components_f64(components)
impl<S: Into<Scalar>, const D: usize> From<[S; D]> for Vector<D> {
fn from(components: [S; D]) -> Self {
Self {
components: components.map(Into::into),
}
}
}

impl<const D: usize> From<nalgebra::SVector<f64, D>> for Vector<D> {
fn from(vector: nalgebra::SVector<f64, D>) -> Self {
Self::from_na(vector)
let components: [f64; D] = vector.into();
Vector::from(components)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/fj-viewer/src/input/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]])
}

0 comments on commit db40c13

Please sign in to comment.