Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Vector into struct #197

Merged
merged 5 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/kernel/geometry/curves/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ impl Circle {

#[must_use]
pub fn transform(self, transform: &Transform) -> Self {
let radius = vector![self.radius.x, self.radius.y, 0.];
let radius = vector![self.radius.x(), self.radius.y(), 0.].into();
let radius = transform.transform_vector(&radius);
let radius = vector![radius.x(), radius.y()].into();

Self {
center: transform.transform_point(&self.center),
radius: transform.transform_vector(&radius).xy(),
radius,
}
}

Expand All @@ -55,20 +57,20 @@ impl Circle {

/// Convert a point on the curve into model coordinates
pub fn point_curve_to_model(&self, point: &Point<1>) -> Point<3> {
self.center + self.vector_curve_to_model(&point.coords)
self.center + self.vector_curve_to_model(&point.coords.into())
}

/// Convert a vector on the curve into model coordinates
pub fn vector_curve_to_model(&self, point: &Vector<1>) -> Vector<3> {
let radius = self.radius.magnitude();
let angle = point.x;
let angle = point.x();

let (sin, cos) = angle.sin_cos();

let x = cos * radius;
let y = sin * radius;

vector![x, y, 0.]
vector![x, y, 0.].into()
}

pub fn approx(&self, tolerance: f64, out: &mut Vec<Point<3>>) {
Expand Down Expand Up @@ -111,7 +113,7 @@ mod tests {
fn point_model_to_curve() {
let circle = Circle {
center: point![1., 2., 3.],
radius: vector![1., 0.],
radius: vector![1., 0.].into(),
};

assert_eq!(
Expand Down
20 changes: 11 additions & 9 deletions src/kernel/geometry/curves/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ impl Line {
let p = point - self.origin;

// scalar projection
let t = p.dot(&self.direction.normalize()) / self.direction.magnitude();
let t = p.dot(&self.direction.normalize().to_na())
/ self.direction.magnitude();

point![t]
}

/// Convert a point on the curve into model coordinates
pub fn point_curve_to_model(&self, point: &Point<1>) -> Point<3> {
self.origin + self.vector_curve_to_model(&point.coords)
self.origin + self.vector_curve_to_model(&point.coords.into())
}

/// Convert a vector on the curve into model coordinates
pub fn vector_curve_to_model(&self, point: &Vector<1>) -> Vector<3> {
let t = point.x;
let t = point.x();
self.direction * t
}
}
Expand All @@ -83,21 +84,22 @@ mod tests {
use nalgebra::{point, vector, UnitQuaternion};
use parry3d_f64::math::{Isometry, Translation};

use crate::kernel::math::Vector;

use super::Line;

#[test]
fn transform() {
let line = Line {
origin: point![1., 0., 0.],
direction: vector![0., 1., 0.],
direction: vector![0., 1., 0.].into(),
};

let line = line.transform(
&Isometry::from_parts(
Translation::from([1., 2., 3.]),
UnitQuaternion::from_axis_angle(&Vector::z_axis(), FRAC_PI_2),
UnitQuaternion::from_axis_angle(
&nalgebra::Vector::z_axis(),
FRAC_PI_2,
),
)
.into(),
);
Expand All @@ -106,7 +108,7 @@ mod tests {
line,
Line {
origin: point![1., 3., 3.],
direction: vector![-1., 0., 0.],
direction: vector![-1., 0., 0.].into(),
},
epsilon = 1e-8,
);
Expand All @@ -116,7 +118,7 @@ mod tests {
fn point_model_to_curve() {
let line = Line {
origin: point![1., 0., 0.],
direction: vector![2., 0., 0.],
direction: vector![2., 0., 0.].into(),
};

verify(line, -1.);
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/geometry/points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ impl Sub<Self> for SurfacePoint {
type Output = Vector<2>;

fn sub(self, rhs: Self) -> Self::Output {
self.value.sub(rhs.value)
self.value.sub(rhs.value).into()
}
}

impl Sub<Point<2>> for SurfacePoint {
type Output = Vector<2>;

fn sub(self, rhs: Point<2>) -> Self::Output {
self.value.sub(rhs)
self.value.sub(rhs).into()
}
}
4 changes: 2 additions & 2 deletions src/kernel/geometry/surfaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ impl Surface {
Self::Swept(Swept {
curve: Curve::Line(Line {
origin: Point::origin(),
direction: vector![1., 0., 0.],
direction: vector![1., 0., 0.].into(),
}),
path: vector![0., 1., 0.],
path: vector![0., 1., 0.].into(),
})
}

Expand Down
24 changes: 12 additions & 12 deletions src/kernel/geometry/surfaces/swept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Swept {
let p = point - self.curve.origin();

let u = self.curve.point_model_to_curve(point).x;
let v = p.dot(&self.path.normalize()) / self.path.magnitude();
let v = p.dot(&self.path.normalize().to_na()) / self.path.magnitude();

point![u, v]
}
Expand All @@ -44,10 +44,10 @@ impl Swept {

/// Convert a vector in surface coordinates to model coordinates
pub fn vector_surface_to_model(&self, vector: &Vector<2>) -> Vector<3> {
let u = vector.x;
let v = vector.y;
let u = vector.x();
let v = vector.y();

self.curve.vector_curve_to_model(&vector![u]) + self.path * v
self.curve.vector_curve_to_model(&vector![u].into()) + self.path * v
}
}

Expand All @@ -67,9 +67,9 @@ mod tests {
let swept = Swept {
curve: Curve::Line(Line {
origin: point![1., 0., 0.],
direction: vector![0., 2., 0.],
direction: vector![0., 2., 0.].into(),
}),
path: vector![0., 0., 2.],
path: vector![0., 0., 2.].into(),
};

verify(&swept, point![-1., -1.]);
Expand All @@ -90,9 +90,9 @@ mod tests {
let swept = Swept {
curve: Curve::Line(Line {
origin: point![1., 0., 0.],
direction: vector![0., 2., 0.],
direction: vector![0., 2., 0.].into(),
}),
path: vector![0., 0., 2.],
path: vector![0., 0., 2.].into(),
};

assert_eq!(
Expand All @@ -106,14 +106,14 @@ mod tests {
let swept = Swept {
curve: Curve::Line(Line {
origin: point![1., 0., 0.],
direction: vector![0., 2., 0.],
direction: vector![0., 2., 0.].into(),
}),
path: vector![0., 0., 2.],
path: vector![0., 0., 2.].into(),
};

assert_eq!(
swept.vector_surface_to_model(&vector![2., 4.]),
vector![0., 4., 8.],
swept.vector_surface_to_model(&vector![2., 4.].into()),
vector![0., 4., 8.].into(),
);
}
}
5 changes: 5 additions & 0 deletions src/kernel/math/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod point;
pub mod transform;
pub mod vector;

pub use self::{point::Point, transform::Transform, vector::Vector};
13 changes: 13 additions & 0 deletions src/kernel/math/point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::ops;

use super::Vector;

pub type Point<const D: usize> = nalgebra::Point<f64, D>;

impl<const D: usize> ops::Add<Vector<D>> for Point<D> {
type Output = Self;

fn add(self, rhs: Vector<D>) -> Self::Output {
self.add(rhs.to_na())
}
}
5 changes: 2 additions & 3 deletions src/kernel/math.rs → src/kernel/math/transform.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub type Point<const D: usize> = nalgebra::Point<f64, D>;
pub type Vector<const D: usize> = nalgebra::SVector<f64, D>;
use super::{Point, Vector};

/// A transform
pub struct Transform(parry3d_f64::math::Isometry<f64>);
Expand All @@ -12,7 +11,7 @@ impl Transform {

/// Transform the given vector
pub fn transform_vector(&self, vector: &Vector<3>) -> Vector<3> {
self.0.transform_vector(vector)
self.0.transform_vector(&vector.to_na()).into()
}
}

Expand Down
75 changes: 75 additions & 0 deletions src/kernel/math/vector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::ops;

use approx::AbsDiffEq;

/// An n-dimensional vector
///
/// The dimensionality is defined by the const generic argument `D`.
///
/// # Implementation Note
///
/// The goal of this type is to eventually implement `Eq` and `Hash`, making it
/// easier to work with vectors. This is a work in progress.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Vector<const D: usize>([f64; D]);

impl<const D: usize> Vector<D> {
/// Convert the vector into an nalgebra vector
pub fn to_na(&self) -> nalgebra::SVector<f64, D> {
self.0.into()
}

/// Access the vector's x coordinate
pub fn x(&self) -> f64 {
self.0[0]
}

/// Access the vector's y coordinate
pub fn y(&self) -> f64 {
self.0[1]
}

/// Compute the magnitude of the vector
pub fn magnitude(&self) -> f64 {
self.to_na().magnitude()
}

/// Compute a normalized version of the vector
pub fn normalize(&self) -> Self {
self.to_na().normalize().into()
}
}

impl<const D: usize> From<nalgebra::SVector<f64, D>> for Vector<D> {
fn from(v: nalgebra::SVector<f64, D>) -> Self {
Self(v.into())
}
}

impl<const D: usize> ops::Add<Self> for Vector<D> {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
self.to_na().add(rhs.to_na()).into()
}
}

impl<const D: usize> ops::Mul<f64> for Vector<D> {
type Output = Self;

fn mul(self, rhs: f64) -> Self::Output {
self.to_na().mul(rhs).into()
}
}

impl<const D: usize> AbsDiffEq for Vector<D> {
type Epsilon = <f64 as AbsDiffEq>::Epsilon;

fn default_epsilon() -> Self::Epsilon {
f64::default_epsilon()
}

fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
self.0.abs_diff_eq(&other.0, epsilon)
}
}
2 changes: 1 addition & 1 deletion src/kernel/shapes/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl Shape for fj::Sketch {

let line = Curve::Line(Line {
origin: *a.location(),
direction: *b.location() - *a.location(),
direction: (*b.location() - *a.location()).into(),
});
let edge = Edge::new(line, Some([a, b]));

Expand Down
10 changes: 7 additions & 3 deletions src/kernel/shapes/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use parry3d_f64::{bounding_volume::AABB, math::Isometry};
use crate::{
debug::DebugInfo,
kernel::{
math::{Transform, Vector},
math::Transform,
topology::{edges::Edges, faces::Faces, vertices::Vertices},
Shape,
},
Expand Down Expand Up @@ -32,6 +32,10 @@ impl Shape for fj::Transform {
}

fn transform(transform: &fj::Transform) -> Transform {
let axis = Vector::from(transform.axis).normalize();
Isometry::new(Vector::from(transform.offset), axis * transform.angle).into()
let axis = nalgebra::Vector::from(transform.axis).normalize();
Isometry::new(
nalgebra::Vector::from(transform.offset),
axis * transform.angle,
)
.into()
}
2 changes: 1 addition & 1 deletion src/kernel/topology/edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Edge {
Self {
curve: Curve::Circle(Circle {
center: Point::origin(),
radius: vector![radius, 0.],
radius: vector![radius, 0.].into(),
}),
vertices: None,
reverse: false,
Expand Down
4 changes: 3 additions & 1 deletion src/kernel/topology/faces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ impl Face {
};
let mut check = TriangleEdgeCheck::new(Ray3 {
origin: surface.point_surface_to_model(&ray.origin),
dir: surface.vector_surface_to_model(&ray.dir),
dir: surface
.vector_surface_to_model(&ray.dir.into())
.to_na(),
});

// We need to keep track of where our ray hits the
Expand Down