From ee1f7fcef72e4b4bdac67384c583d0f2cb58f745 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:04:58 +0100 Subject: [PATCH 01/18] Make fields of `Point` and `Vector` public I initially had the misguided notion that the fields need to be protected, but all the protection that's required already happens in `Scalar`. --- src/math/point.rs | 2 +- src/math/vector.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/math/point.rs b/src/math/point.rs index f3c7c258c..b774b0b5d 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -13,7 +13,7 @@ use super::{Scalar, Vector}; /// 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, Eq, Hash, PartialEq, Ord, PartialOrd)] -pub struct Point([Scalar; D]); +pub struct Point(pub [Scalar; D]); impl Point { /// Construct a `Point` at the origin of the coordinate system diff --git a/src/math/vector.rs b/src/math/vector.rs index 7c6dbd203..95d2ae0c3 100644 --- a/src/math/vector.rs +++ b/src/math/vector.rs @@ -13,7 +13,7 @@ use super::Scalar; /// 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, Eq, Hash, PartialEq, Ord, PartialOrd)] -pub struct Vector([Scalar; D]); +pub struct Vector(pub [Scalar; D]); impl Vector { /// Construct a `Vector` from an array From 6ee45e2655b77ab56d301e5135304dae68456d2b Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:14:54 +0100 Subject: [PATCH 02/18] Implement more conversions from `Vector` --- src/math/vector.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/math/vector.rs b/src/math/vector.rs index 95d2ae0c3..b25af2ddf 100644 --- a/src/math/vector.rs +++ b/src/math/vector.rs @@ -127,6 +127,18 @@ impl From> for [f32; D] { } } +impl From> for [f64; D] { + fn from(vector: Vector) -> Self { + vector.0.map(|scalar| scalar.into_f64()) + } +} + +impl From> for nalgebra::SVector { + fn from(vector: Vector) -> Self { + vector.to_na() + } +} + impl ops::Add for Vector { type Output = Self; From c7ac6b84466b6b195c98a6ceec51e42983d7b1ec Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:15:20 +0100 Subject: [PATCH 03/18] Add `Vector::<3>::z` --- src/math/vector.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/math/vector.rs b/src/math/vector.rs index b25af2ddf..7262820a2 100644 --- a/src/math/vector.rs +++ b/src/math/vector.rs @@ -97,6 +97,11 @@ impl Vector<3> { self.0[1] } + /// Access the vector's z coordinate + pub fn z(&self) -> Scalar { + self.0[2] + } + /// Construct a new vector from this vector's x and y components pub fn xy(&self) -> Vector<2> { Vector::from([self.x(), self.y()]) From 9fd9f398262a52239c995f8232f26a91eebced73 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:16:32 +0100 Subject: [PATCH 04/18] Update `Point` to use `Vector` internally This brings `Point` closer to `nalgebra::Point` and consolidates some duplicated code between `Point` and `Vector`. --- src/math/point.rs | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/math/point.rs b/src/math/point.rs index b774b0b5d..379c7e255 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -13,7 +13,9 @@ use super::{Scalar, Vector}; /// 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, Eq, Hash, PartialEq, Ord, PartialOrd)] -pub struct Point(pub [Scalar; D]); +pub struct Point { + pub coords: Vector, +} impl Point { /// Construct a `Point` at the origin of the coordinate system @@ -23,74 +25,84 @@ impl Point { /// Construct a `Point` from an array pub fn from_array(array: [f64; D]) -> Self { - Self(array.map(Scalar::from_f64)) + Self { + coords: array.map(Scalar::from_f64).into(), + } } /// Construct a `Point` from an nalgebra vector pub fn from_na(point: nalgebra::Point) -> Self { - Self(point.coords.data.0[0].map(Scalar::from_f64)) + Self { + coords: point.coords.into(), + } } /// Convert the point into an nalgebra point pub fn to_na(&self) -> nalgebra::Point { - self.0.map(Scalar::into_f64).into() + nalgebra::Point { + coords: self.coords.into(), + } } /// Convert to a 1-dimensional point pub fn to_t(&self) -> Point<1> { - Point([self.0[0]]) + Point { + coords: self.coords.to_t(), + } } /// Access a mutable reference to the point's z coordinate pub fn z_mut(&mut self) -> &mut Scalar { - &mut self.0[2] + &mut self.coords.0[2] } /// Access the point's coordinates as a vector pub fn coords(&self) -> Vector { - Vector::from(self.0) + self.coords } } impl Point<1> { /// Access the curve point's t coordinate pub fn t(&self) -> Scalar { - self.0[0] + self.coords.t() } } impl Point<2> { /// Access the point's x coordinate pub fn u(&self) -> Scalar { - self.0[0] + self.coords.u() } /// Access the point's y coordinate pub fn v(&self) -> Scalar { - self.0[1] + self.coords.v() } } impl Point<3> { /// Access the point's x coordinate pub fn x(&self) -> Scalar { - self.0[0] + self.coords.x() } /// Access the point's y coordinate pub fn y(&self) -> Scalar { - self.0[1] + self.coords.y() } /// Access the point's z coordinate pub fn z(&self) -> Scalar { - self.0[2] + self.coords.z() } } impl From<[Scalar; D]> for Point { fn from(array: [Scalar; D]) -> Self { - Self(array) + Self { + coords: array.into(), + } } } @@ -108,13 +120,13 @@ impl From> for Point { impl From> for [f32; D] { fn from(point: Point) -> Self { - point.0.map(|scalar| scalar.into_f32()) + point.coords.into() } } impl From> for [f64; D] { fn from(point: Point) -> Self { - point.0.map(|scalar| scalar.into_f64()) + point.coords.into() } } @@ -166,6 +178,6 @@ impl AbsDiffEq for Point { } fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { - self.0.abs_diff_eq(&other.0, epsilon) + self.coords.abs_diff_eq(&other.coords, epsilon) } } From a048f72bdd15eb9951e99ee2f58beda450e8460f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:17:41 +0100 Subject: [PATCH 05/18] Remove redundant method --- src/kernel/geometry/curves/circle.rs | 2 +- src/kernel/geometry/curves/line.rs | 2 +- src/math/point.rs | 5 ----- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/kernel/geometry/curves/circle.rs b/src/kernel/geometry/curves/circle.rs index 8a2eee9b7..e7c223823 100644 --- a/src/kernel/geometry/curves/circle.rs +++ b/src/kernel/geometry/curves/circle.rs @@ -59,7 +59,7 @@ 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) } /// Convert a vector on the curve into model coordinates diff --git a/src/kernel/geometry/curves/line.rs b/src/kernel/geometry/curves/line.rs index ea59f2c55..2aade32bb 100644 --- a/src/kernel/geometry/curves/line.rs +++ b/src/kernel/geometry/curves/line.rs @@ -50,7 +50,7 @@ impl Line { /// 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) } /// Convert a vector on the curve into model coordinates diff --git a/src/math/point.rs b/src/math/point.rs index 379c7e255..1e05658f0 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -55,11 +55,6 @@ impl Point { pub fn z_mut(&mut self) -> &mut Scalar { &mut self.coords.0[2] } - - /// Access the point's coordinates as a vector - pub fn coords(&self) -> Vector { - self.coords - } } impl Point<1> { From 708eaa33d32348d56a72b5745a2d7d65fe275b48 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:18:35 +0100 Subject: [PATCH 06/18] Use `Epsilon` of underlying type in `approx` impls --- src/math/point.rs | 2 +- src/math/vector.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/math/point.rs b/src/math/point.rs index 1e05658f0..eb1345dca 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -166,7 +166,7 @@ impl ops::Mul for Point { } impl AbsDiffEq for Point { - type Epsilon = ::Epsilon; + type Epsilon = as AbsDiffEq>::Epsilon; fn default_epsilon() -> Self::Epsilon { f64::default_epsilon() diff --git a/src/math/vector.rs b/src/math/vector.rs index 7262820a2..cff6e30f6 100644 --- a/src/math/vector.rs +++ b/src/math/vector.rs @@ -169,7 +169,7 @@ impl ops::Div for Vector { } impl AbsDiffEq for Vector { - type Epsilon = ::Epsilon; + type Epsilon = ::Epsilon; fn default_epsilon() -> Self::Epsilon { f64::default_epsilon() From 44ffc4fc26b53ea676816c144e37589e406da816 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:20:29 +0100 Subject: [PATCH 07/18] Clean up imports I don't think they are used often enough to justify them. Qualifying them where they're used also makes the code more clear. --- src/kernel/geometry/curves/line.rs | 6 ++---- src/math/point.rs | 6 ++---- src/math/scalar.rs | 6 ++---- src/math/vector.rs | 6 ++---- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/kernel/geometry/curves/line.rs b/src/kernel/geometry/curves/line.rs index 2aade32bb..58e1dd6d1 100644 --- a/src/kernel/geometry/curves/line.rs +++ b/src/kernel/geometry/curves/line.rs @@ -1,5 +1,3 @@ -use approx::AbsDiffEq; - use crate::math::{Point, Transform, Vector}; /// A line, defined by a point and a vector @@ -59,8 +57,8 @@ impl Line { } } -impl AbsDiffEq for Line { - type Epsilon = ::Epsilon; +impl approx::AbsDiffEq for Line { + type Epsilon = ::Epsilon; fn default_epsilon() -> Self::Epsilon { f64::default_epsilon() diff --git a/src/math/point.rs b/src/math/point.rs index eb1345dca..fd337e030 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -1,7 +1,5 @@ use std::ops; -use approx::AbsDiffEq; - use super::{Scalar, Vector}; /// An n-dimensional point @@ -165,8 +163,8 @@ impl ops::Mul for Point { } } -impl AbsDiffEq for Point { - type Epsilon = as AbsDiffEq>::Epsilon; +impl approx::AbsDiffEq for Point { + type Epsilon = as approx::AbsDiffEq>::Epsilon; fn default_epsilon() -> Self::Epsilon { f64::default_epsilon() diff --git a/src/math/scalar.rs b/src/math/scalar.rs index ed0644078..fae6d94d2 100644 --- a/src/math/scalar.rs +++ b/src/math/scalar.rs @@ -1,7 +1,5 @@ use std::{cmp, f64::consts::PI, hash::Hash, ops}; -use approx::AbsDiffEq; - /// A rational, finite scalar value /// /// This is a wrapper around `f64`. On construction, it checks that the `f64` @@ -272,8 +270,8 @@ impl num_traits::Signed for Scalar { } } -impl AbsDiffEq for Scalar { - type Epsilon = ::Epsilon; +impl approx::AbsDiffEq for Scalar { + type Epsilon = ::Epsilon; fn default_epsilon() -> Self::Epsilon { f64::default_epsilon() diff --git a/src/math/vector.rs b/src/math/vector.rs index cff6e30f6..bbf40f160 100644 --- a/src/math/vector.rs +++ b/src/math/vector.rs @@ -1,7 +1,5 @@ use std::ops; -use approx::AbsDiffEq; - use super::Scalar; /// An n-dimensional vector @@ -168,8 +166,8 @@ impl ops::Div for Vector { } } -impl AbsDiffEq for Vector { - type Epsilon = ::Epsilon; +impl approx::AbsDiffEq for Vector { + type Epsilon = ::Epsilon; fn default_epsilon() -> Self::Epsilon { f64::default_epsilon() From f1a82fcea16d5836e3381304af0d91e451f0a7a5 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:33:17 +0100 Subject: [PATCH 08/18] Add types to represent coordinates These will be used to improve the usability of `Point` and `Vector`. --- src/math/coordinates.rs | 24 ++++++++++++++++++++++++ src/math/mod.rs | 1 + 2 files changed, 25 insertions(+) create mode 100644 src/math/coordinates.rs diff --git a/src/math/coordinates.rs b/src/math/coordinates.rs new file mode 100644 index 000000000..c9201a865 --- /dev/null +++ b/src/math/coordinates.rs @@ -0,0 +1,24 @@ +#![allow(unused)] + +use super::Scalar; + +/// 1-dimensional curve coordinates +#[repr(C)] +pub struct T { + pub t: Scalar, +} + +/// 2-dimensional surface coordinates +#[repr(C)] +pub struct Uv { + pub u: Scalar, + pub v: Scalar, +} + +/// 3-dimensional model coordinates +#[repr(C)] +pub struct Xyz { + pub x: Scalar, + pub y: Scalar, + pub z: Scalar, +} diff --git a/src/math/mod.rs b/src/math/mod.rs index 2cb6afa01..ff312f9cc 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -1,4 +1,5 @@ pub mod aabb; +pub mod coordinates; pub mod point; pub mod scalar; pub mod segment; From 07993cb5c1453f74d23827b8c0d1efe1c6a6e2d4 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:36:08 +0100 Subject: [PATCH 09/18] Make access to vector coordinates more convenient --- src/math/vector.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/src/math/vector.rs b/src/math/vector.rs index bbf40f160..7ad369429 100644 --- a/src/math/vector.rs +++ b/src/math/vector.rs @@ -1,6 +1,9 @@ use std::ops; -use super::Scalar; +use super::{ + coordinates::{Uv, Xyz, T}, + Scalar, +}; /// An n-dimensional vector /// @@ -106,6 +109,72 @@ impl Vector<3> { } } +impl ops::Deref for Vector<1> { + type Target = T; + + fn deref(&self) -> &Self::Target { + let ptr = self.0.as_ptr() as *const Self::Target; + + // This is sound. We've created this pointer from a valid instance, that + // has the same size and layout as the target. + unsafe { &*ptr } + } +} + +impl ops::Deref for Vector<2> { + type Target = Uv; + + fn deref(&self) -> &Self::Target { + let ptr = self.0.as_ptr() as *const Self::Target; + + // This is sound. We've created this pointer from a valid instance, that + // has the same size and layout as the target. + unsafe { &*ptr } + } +} + +impl ops::Deref for Vector<3> { + type Target = Xyz; + + fn deref(&self) -> &Self::Target { + let ptr = self.0.as_ptr() as *const Self::Target; + + // This is sound. We've created this pointer from a valid instance, that + // has the same size and layout as the target. + unsafe { &*ptr } + } +} + +impl ops::DerefMut for Vector<1> { + fn deref_mut(&mut self) -> &mut Self::Target { + let ptr = self.0.as_mut_ptr() as *mut Self::Target; + + // This is sound. We've created this pointer from a valid instance, that + // has the same size and layout as the target. + unsafe { &mut *ptr } + } +} + +impl ops::DerefMut for Vector<2> { + fn deref_mut(&mut self) -> &mut Self::Target { + let ptr = self.0.as_mut_ptr() as *mut Self::Target; + + // This is sound. We've created this pointer from a valid instance, that + // has the same size and layout as the target. + unsafe { &mut *ptr } + } +} + +impl ops::DerefMut for Vector<3> { + fn deref_mut(&mut self) -> &mut Self::Target { + let ptr = self.0.as_mut_ptr() as *mut Self::Target; + + // This is sound. We've created this pointer from a valid instance, that + // has the same size and layout as the target. + unsafe { &mut *ptr } + } +} + impl From<[Scalar; D]> for Vector { fn from(array: [Scalar; D]) -> Self { Self(array) From 05fb6b619086e63c8b04020732936b5146234e21 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:38:03 +0100 Subject: [PATCH 10/18] Un-suppress warning --- src/math/coordinates.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/math/coordinates.rs b/src/math/coordinates.rs index c9201a865..fff693a6e 100644 --- a/src/math/coordinates.rs +++ b/src/math/coordinates.rs @@ -1,5 +1,3 @@ -#![allow(unused)] - use super::Scalar; /// 1-dimensional curve coordinates From 8256b5574a2a70fe7eff73fe50d1e025642de5ad Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:38:33 +0100 Subject: [PATCH 11/18] Remove redundant access to vector model coords --- src/kernel/geometry/curves/circle.rs | 2 +- src/math/point.rs | 6 +++--- src/math/vector.rs | 17 +---------------- 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/src/kernel/geometry/curves/circle.rs b/src/kernel/geometry/curves/circle.rs index e7c223823..0f9aafa93 100644 --- a/src/kernel/geometry/curves/circle.rs +++ b/src/kernel/geometry/curves/circle.rs @@ -48,7 +48,7 @@ impl Circle { /// error. pub fn point_model_to_curve(&self, point: &Point<3>) -> Point<1> { let v = point - self.center; - let atan = Scalar::atan2(v.y(), v.x()); + let atan = Scalar::atan2(v.y, v.x); let coord = if atan >= Scalar::ZERO { atan } else { diff --git a/src/math/point.rs b/src/math/point.rs index fd337e030..6dac297e4 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -77,17 +77,17 @@ impl Point<2> { impl Point<3> { /// Access the point's x coordinate pub fn x(&self) -> Scalar { - self.coords.x() + self.coords.x } /// Access the point's y coordinate pub fn y(&self) -> Scalar { - self.coords.y() + self.coords.y } /// Access the point's z coordinate pub fn z(&self) -> Scalar { - self.coords.z() + self.coords.z } } diff --git a/src/math/vector.rs b/src/math/vector.rs index 7ad369429..de42c4f0b 100644 --- a/src/math/vector.rs +++ b/src/math/vector.rs @@ -88,24 +88,9 @@ impl Vector<2> { } impl Vector<3> { - /// Access the vector's x coordinate - pub fn x(&self) -> Scalar { - self.0[0] - } - - /// Access the vector's y coordinate - pub fn y(&self) -> Scalar { - self.0[1] - } - - /// Access the vector's z coordinate - pub fn z(&self) -> Scalar { - self.0[2] - } - /// Construct a new vector from this vector's x and y components pub fn xy(&self) -> Vector<2> { - Vector::from([self.x(), self.y()]) + Vector::from([self.x, self.y]) } } From ab19d3f72a1b7aad7c4ba46ec79089cf363d2864 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:39:42 +0100 Subject: [PATCH 12/18] Remove redundant access to vector surface coords --- src/kernel/geometry/surfaces/swept.rs | 3 +-- src/math/point.rs | 4 ++-- src/math/vector.rs | 12 +----------- 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/kernel/geometry/surfaces/swept.rs b/src/kernel/geometry/surfaces/swept.rs index c37721ecb..d85bdea6f 100644 --- a/src/kernel/geometry/surfaces/swept.rs +++ b/src/kernel/geometry/surfaces/swept.rs @@ -38,8 +38,7 @@ impl Swept { /// Convert a vector in surface coordinates to model coordinates pub fn vector_surface_to_model(&self, vector: &Vector<2>) -> Vector<3> { - self.curve.vector_curve_to_model(&vector.to_t()) - + self.path * vector.v() + self.curve.vector_curve_to_model(&vector.to_t()) + self.path * vector.v } } diff --git a/src/math/point.rs b/src/math/point.rs index 6dac297e4..3a3099806 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -65,12 +65,12 @@ impl Point<1> { impl Point<2> { /// Access the point's x coordinate pub fn u(&self) -> Scalar { - self.coords.u() + self.coords.u } /// Access the point's y coordinate pub fn v(&self) -> Scalar { - self.coords.v() + self.coords.v } } diff --git a/src/math/vector.rs b/src/math/vector.rs index de42c4f0b..cef23dda8 100644 --- a/src/math/vector.rs +++ b/src/math/vector.rs @@ -71,19 +71,9 @@ impl Vector<1> { } impl Vector<2> { - /// Access the surface vector's u coordinate - pub fn u(&self) -> Scalar { - self.0[0] - } - - /// Access the surface vector's v coordinate - pub fn v(&self) -> Scalar { - self.0[1] - } - /// Extend a 2-dimensional vector into a 3-dimensional one pub fn to_xyz(&self, z: Scalar) -> Vector<3> { - Vector::from([self.u(), self.v(), z]) + Vector::from([self.u, self.v, z]) } } From 71f62809c55fe3610c7bf44adafcca058a6d3c01 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:41:03 +0100 Subject: [PATCH 13/18] Remove redundant access to vector curve coords --- src/kernel/geometry/curves/circle.rs | 2 +- src/kernel/geometry/curves/line.rs | 2 +- src/math/point.rs | 2 +- src/math/vector.rs | 7 ------- 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/kernel/geometry/curves/circle.rs b/src/kernel/geometry/curves/circle.rs index 0f9aafa93..436bc038b 100644 --- a/src/kernel/geometry/curves/circle.rs +++ b/src/kernel/geometry/curves/circle.rs @@ -65,7 +65,7 @@ impl Circle { /// 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.t(); + let angle = point.t; let (sin, cos) = angle.sin_cos(); diff --git a/src/kernel/geometry/curves/line.rs b/src/kernel/geometry/curves/line.rs index 58e1dd6d1..d7af97b32 100644 --- a/src/kernel/geometry/curves/line.rs +++ b/src/kernel/geometry/curves/line.rs @@ -53,7 +53,7 @@ impl Line { /// Convert a vector on the curve into model coordinates pub fn vector_curve_to_model(&self, point: &Vector<1>) -> Vector<3> { - self.direction * point.t() + self.direction * point.t } } diff --git a/src/math/point.rs b/src/math/point.rs index 3a3099806..0664479d7 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -58,7 +58,7 @@ impl Point { impl Point<1> { /// Access the curve point's t coordinate pub fn t(&self) -> Scalar { - self.coords.t() + self.coords.t } } diff --git a/src/math/vector.rs b/src/math/vector.rs index cef23dda8..86f31eaf7 100644 --- a/src/math/vector.rs +++ b/src/math/vector.rs @@ -63,13 +63,6 @@ impl Vector { } } -impl Vector<1> { - /// Access the curve vector's t coordinate - pub fn t(&self) -> Scalar { - self.0[0] - } -} - impl Vector<2> { /// Extend a 2-dimensional vector into a 3-dimensional one pub fn to_xyz(&self, z: Scalar) -> Vector<3> { From fee466e3e5a5d7398fd6eb4efd9e38f4232361cb Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 12:41:54 +0100 Subject: [PATCH 14/18] Fix incorrect variable names --- src/kernel/geometry/curves/circle.rs | 4 ++-- src/kernel/geometry/curves/line.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/kernel/geometry/curves/circle.rs b/src/kernel/geometry/curves/circle.rs index 436bc038b..3fd7cb21f 100644 --- a/src/kernel/geometry/curves/circle.rs +++ b/src/kernel/geometry/curves/circle.rs @@ -63,9 +63,9 @@ impl Circle { } /// Convert a vector on the curve into model coordinates - pub fn vector_curve_to_model(&self, point: &Vector<1>) -> Vector<3> { + pub fn vector_curve_to_model(&self, vector: &Vector<1>) -> Vector<3> { let radius = self.radius.magnitude(); - let angle = point.t; + let angle = vector.t; let (sin, cos) = angle.sin_cos(); diff --git a/src/kernel/geometry/curves/line.rs b/src/kernel/geometry/curves/line.rs index d7af97b32..71c103513 100644 --- a/src/kernel/geometry/curves/line.rs +++ b/src/kernel/geometry/curves/line.rs @@ -52,8 +52,8 @@ impl Line { } /// Convert a vector on the curve into model coordinates - pub fn vector_curve_to_model(&self, point: &Vector<1>) -> Vector<3> { - self.direction * point.t + pub fn vector_curve_to_model(&self, vector: &Vector<1>) -> Vector<3> { + self.direction * vector.t } } From 1e33846f934be096a61e926ebb21d0e1bcb47c65 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 13:30:46 +0100 Subject: [PATCH 15/18] Make access to point coordinates more convenient --- src/math/point.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/math/point.rs b/src/math/point.rs index 0664479d7..f43620eb7 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -1,6 +1,9 @@ use std::ops; -use super::{Scalar, Vector}; +use super::{ + coordinates::{Uv, Xyz, T}, + Scalar, Vector, +}; /// An n-dimensional point /// @@ -91,6 +94,48 @@ impl Point<3> { } } +impl ops::Deref for Point<1> { + type Target = T; + + fn deref(&self) -> &Self::Target { + self.coords.deref() + } +} + +impl ops::Deref for Point<2> { + type Target = Uv; + + fn deref(&self) -> &Self::Target { + self.coords.deref() + } +} + +impl ops::Deref for Point<3> { + type Target = Xyz; + + fn deref(&self) -> &Self::Target { + self.coords.deref() + } +} + +impl ops::DerefMut for Point<1> { + fn deref_mut(&mut self) -> &mut Self::Target { + self.coords.deref_mut() + } +} + +impl ops::DerefMut for Point<2> { + fn deref_mut(&mut self) -> &mut Self::Target { + self.coords.deref_mut() + } +} + +impl ops::DerefMut for Point<3> { + fn deref_mut(&mut self) -> &mut Self::Target { + self.coords.deref_mut() + } +} + impl From<[Scalar; D]> for Point { fn from(array: [Scalar; D]) -> Self { Self { From bff0d20f833fb5013074a6223dc5376cdf74a21e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 13:32:58 +0100 Subject: [PATCH 16/18] Remove redundant access to point model coords --- src/camera.rs | 28 ++++++++++++---------------- src/kernel/approximation.rs | 12 ++++++------ src/kernel/shapes/sweep.rs | 2 +- src/math/point.rs | 22 ---------------------- 4 files changed, 19 insertions(+), 45 deletions(-) diff --git a/src/camera.rs b/src/camera.rs index bd8bb8366..f612eb92e 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -45,19 +45,15 @@ impl Camera { // // To do that, first compute the model's highest point, as well as // the furthest point from the origin, in x and y. - let highest_point = aabb.max.z(); - let furthest_point = [ - aabb.min.x().abs(), - aabb.max.x(), - aabb.min.y().abs(), - aabb.max.y(), - ] - .into_iter() - .reduce(Scalar::max) - // `reduce` can only return `None`, if there are no items in - // the iterator. And since we're creating an array full of - // items above, we know this can't panic. - .unwrap(); + let highest_point = aabb.max.z; + let furthest_point = + [aabb.min.x.abs(), aabb.max.x, aabb.min.y.abs(), aabb.max.y] + .into_iter() + .reduce(Scalar::max) + // `reduce` can only return `None`, if there are no items in + // the iterator. And since we're creating an array full of + // items above, we know this can't panic. + .unwrap(); // The actual furthest point is not far enough. We don't want the // model to fill the whole screen. @@ -74,7 +70,7 @@ impl Camera { let initial_offset = { let mut offset = aabb.center(); - *offset.z_mut() = Scalar::ZERO; + offset.z = Scalar::ZERO; -offset }; @@ -84,8 +80,8 @@ impl Camera { rotation: Transform::identity(), translation: Translation::from([ - initial_offset.x().into_f64(), - initial_offset.y().into_f64(), + initial_offset.x.into_f64(), + initial_offset.y.into_f64(), -initial_distance.into_f64(), ]), } diff --git a/src/kernel/approximation.rs b/src/kernel/approximation.rs index a84b52c03..22abc06a7 100644 --- a/src/kernel/approximation.rs +++ b/src/kernel/approximation.rs @@ -90,22 +90,22 @@ impl Approximation { // As this is a cycle, neighboring edges are going to share vertices. // Let's remove all those duplicates. points.sort_by(|a, b| { - if a.x() < b.x() { + if a.x < b.x { return Ordering::Less; } - if a.x() > b.x() { + if a.x > b.x { return Ordering::Greater; } - if a.y() < b.y() { + if a.y < b.y { return Ordering::Less; } - if a.y() > b.y() { + if a.y > b.y { return Ordering::Greater; } - if a.z() < b.z() { + if a.z < b.z { return Ordering::Less; } - if a.z() > b.z() { + if a.z > b.z { return Ordering::Greater; } diff --git a/src/kernel/shapes/sweep.rs b/src/kernel/shapes/sweep.rs index fcbd80684..13be91908 100644 --- a/src/kernel/shapes/sweep.rs +++ b/src/kernel/shapes/sweep.rs @@ -20,7 +20,7 @@ use crate::{ impl Shape for fj::Sweep { fn bounding_volume(&self) -> Aabb<3> { let mut aabb = self.shape.bounding_volume(); - *aabb.max.z_mut() = self.length.into(); + aabb.max.z = self.length.into(); aabb } diff --git a/src/math/point.rs b/src/math/point.rs index f43620eb7..a77f77922 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -51,11 +51,6 @@ impl Point { coords: self.coords.to_t(), } } - - /// Access a mutable reference to the point's z coordinate - pub fn z_mut(&mut self) -> &mut Scalar { - &mut self.coords.0[2] - } } impl Point<1> { @@ -77,23 +72,6 @@ impl Point<2> { } } -impl Point<3> { - /// Access the point's x coordinate - pub fn x(&self) -> Scalar { - self.coords.x - } - - /// Access the point's y coordinate - pub fn y(&self) -> Scalar { - self.coords.y - } - - /// Access the point's z coordinate - pub fn z(&self) -> Scalar { - self.coords.z - } -} - impl ops::Deref for Point<1> { type Target = T; From e1edb64c6f4993cf5f39882b021189a6a5448ab7 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 13:34:01 +0100 Subject: [PATCH 17/18] Remove redundant access to point surface coords --- src/kernel/geometry/surfaces/swept.rs | 2 +- src/kernel/triangulation.rs | 4 ++-- src/math/point.rs | 12 ------------ 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/kernel/geometry/surfaces/swept.rs b/src/kernel/geometry/surfaces/swept.rs index d85bdea6f..be2c49fee 100644 --- a/src/kernel/geometry/surfaces/swept.rs +++ b/src/kernel/geometry/surfaces/swept.rs @@ -33,7 +33,7 @@ impl Swept { /// Convert a point in surface coordinates to model coordinates pub fn point_surface_to_model(&self, point: &Point<2>) -> Point<3> { - self.curve.point_curve_to_model(&point.to_t()) + self.path * point.v() + self.curve.point_curve_to_model(&point.to_t()) + self.path * point.v } /// Convert a vector in surface coordinates to model coordinates diff --git a/src/kernel/triangulation.rs b/src/kernel/triangulation.rs index 3d42f769f..71ef56fb4 100644 --- a/src/kernel/triangulation.rs +++ b/src/kernel/triangulation.rs @@ -40,8 +40,8 @@ impl HasPosition for SurfacePoint { fn position(&self) -> spade::Point2 { spade::Point2 { - x: self.value.u(), - y: self.value.v(), + x: self.value.u, + y: self.value.v, } } } diff --git a/src/math/point.rs b/src/math/point.rs index a77f77922..2dafbc30d 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -60,18 +60,6 @@ impl Point<1> { } } -impl Point<2> { - /// Access the point's x coordinate - pub fn u(&self) -> Scalar { - self.coords.u - } - - /// Access the point's y coordinate - pub fn v(&self) -> Scalar { - self.coords.v - } -} - impl ops::Deref for Point<1> { type Target = T; From d4faf2973d4bf4bb786184390a8c91c97519390a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Sun, 20 Feb 2022 13:34:39 +0100 Subject: [PATCH 18/18] Remove redundant access to point curve coords --- src/kernel/geometry/surfaces/swept.rs | 2 +- src/math/point.rs | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/kernel/geometry/surfaces/swept.rs b/src/kernel/geometry/surfaces/swept.rs index be2c49fee..ecf5c2bb5 100644 --- a/src/kernel/geometry/surfaces/swept.rs +++ b/src/kernel/geometry/surfaces/swept.rs @@ -24,7 +24,7 @@ impl Swept { /// Convert a point in model coordinates to surface coordinates pub fn point_model_to_surface(&self, point: &Point<3>) -> Point<2> { - let u = self.curve.point_model_to_curve(point).t(); + let u = self.curve.point_model_to_curve(point).t; let v = (point - self.curve.origin()).dot(&self.path.normalize()) / self.path.magnitude(); diff --git a/src/math/point.rs b/src/math/point.rs index 2dafbc30d..c9883c560 100644 --- a/src/math/point.rs +++ b/src/math/point.rs @@ -53,13 +53,6 @@ impl Point { } } -impl Point<1> { - /// Access the curve point's t coordinate - pub fn t(&self) -> Scalar { - self.coords.t - } -} - impl ops::Deref for Point<1> { type Target = T;