diff --git a/Cargo.lock b/Cargo.lock index 77fc43393..0d29d3eb6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -755,7 +755,6 @@ version = "0.6.0" dependencies = [ "anyhow", "anymap", - "approx 0.5.1", "fj-interop", "fj-math", "map-macro", diff --git a/crates/fj-kernel/Cargo.toml b/crates/fj-kernel/Cargo.toml index e6a9873f2..a52a838a3 100644 --- a/crates/fj-kernel/Cargo.toml +++ b/crates/fj-kernel/Cargo.toml @@ -15,7 +15,6 @@ categories = ["encoding", "mathematics", "rendering"] [dependencies] anyhow = "1.0.57" anymap = "1.0.0-beta.2" -approx = "0.5.1" map-macro = "0.2.0" parking_lot = "0.12.0" robust = "0.2.3" diff --git a/crates/fj-kernel/src/algorithms/intersection.rs b/crates/fj-kernel/src/algorithms/intersection.rs index 01f3e54fd..4d1b64a31 100644 --- a/crates/fj-kernel/src/algorithms/intersection.rs +++ b/crates/fj-kernel/src/algorithms/intersection.rs @@ -1,8 +1,8 @@ //! Intersection algorithms -use fj_math::{Point, Scalar, Vector}; +use fj_math::{Line, Point, Scalar, Vector}; -use crate::geometry::{Curve, Line, Surface}; +use crate::geometry::{Curve, Surface}; /// Test intersection between two surfaces pub fn surface(a: &Surface, b: &Surface) -> Option { diff --git a/crates/fj-kernel/src/geometry/curves/line.rs b/crates/fj-kernel/src/geometry/curves/line.rs deleted file mode 100644 index 3a95fd173..000000000 --- a/crates/fj-kernel/src/geometry/curves/line.rs +++ /dev/null @@ -1,133 +0,0 @@ -use fj_math::{Point, Transform, Vector}; - -/// A line, defined by a point and a vector -#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)] -pub struct Line { - /// The origin of the line's coordinate system - pub origin: Point<3>, - - /// The direction of the line - /// - /// The length of this vector defines the unit of the line's curve - /// coordinate system. The coordinate `1.` is always were the direction - /// vector points, from `origin`. - pub direction: Vector<3>, -} - -impl Line { - /// Create a line from two points - pub fn from_points([a, b]: [Point<3>; 2]) -> Self { - Self { - origin: a, - direction: b - a, - } - } - - /// Access the origin of the curve's coordinate system - pub fn origin(&self) -> Point<3> { - self.origin - } - - /// Create a new instance that is reversed - #[must_use] - pub fn reverse(mut self) -> Self { - self.direction = -self.direction; - self - } - - /// Create a new instance that is transformed by `transform` - #[must_use] - pub fn transform(self, transform: &Transform) -> Self { - Self { - origin: transform.transform_point(&self.origin), - direction: transform.transform_vector(&self.direction), - } - } - - /// Convert a point in model coordinates to curve coordinates - /// - /// Projects the point onto the line before computing curve coordinate. This - /// is done to make this method robust against floating point accuracy - /// issues. - /// - /// Callers are advised to be careful about the points they pass, as the - /// point not being on the line, intentional or not, will never result in an - /// error. - pub fn point_model_to_curve(&self, point: &Point<3>) -> Point<1> { - let t = (point - self.origin).scalar_projection_onto(&self.direction) - / self.direction.magnitude(); - Point::from([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) - } - - /// Convert a vector on the curve into model coordinates - pub fn vector_curve_to_model(&self, vector: &Vector<1>) -> Vector<3> { - self.direction * vector.t - } -} - -impl approx::AbsDiffEq for Line { - type Epsilon = ::Epsilon; - - fn default_epsilon() -> Self::Epsilon { - f64::default_epsilon() - } - - fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool { - self.origin.abs_diff_eq(&other.origin, epsilon) - && self.direction.abs_diff_eq(&other.direction, epsilon) - } -} - -#[cfg(test)] -mod tests { - use approx::assert_abs_diff_eq; - use fj_math::{Point, Scalar, Transform, Vector}; - - use super::Line; - - #[test] - fn transform() { - let line = Line { - origin: Point::from([1., 0., 0.]), - direction: Vector::from([0., 1., 0.]), - }; - - let transform = Transform::translation([1., 2., 3.]) - * Transform::rotation(Vector::unit_z() * (Scalar::PI / 2.)); - let line = line.transform(&transform); - - assert_abs_diff_eq!( - line, - Line { - origin: Point::from([1., 3., 3.]), - direction: Vector::from([-1., 0., 0.]), - }, - epsilon = 1e-8, - ); - } - - #[test] - fn point_model_to_curve() { - let line = Line { - origin: Point::from([1., 0., 0.]), - direction: Vector::from([2., 0., 0.]), - }; - - verify(line, -1.); - verify(line, 0.); - verify(line, 1.); - verify(line, 2.); - - fn verify(line: Line, t: f64) { - let point = line.point_curve_to_model(&Point::from([t])); - let t_result = line.point_model_to_curve(&point); - - assert_eq!(Point::from([t]), t_result); - } - } -} diff --git a/crates/fj-kernel/src/geometry/curves/mod.rs b/crates/fj-kernel/src/geometry/curves/mod.rs index 0bb26324c..1b70e5e02 100644 --- a/crates/fj-kernel/src/geometry/curves/mod.rs +++ b/crates/fj-kernel/src/geometry/curves/mod.rs @@ -1,9 +1,8 @@ mod circle; -mod line; -pub use self::{circle::Circle, line::Line}; +pub use self::circle::Circle; -use fj_math::{Point, Transform, Vector}; +use fj_math::{Line, Point, Transform, Vector}; /// A one-dimensional shape /// @@ -20,7 +19,7 @@ pub enum Curve { Circle(Circle), /// A line - Line(Line), + Line(Line<3>), } impl Curve { @@ -52,7 +51,7 @@ impl Curve { pub fn origin(&self) -> Point<3> { match self { Self::Circle(curve) => curve.origin(), - Self::Line(curve) => curve.origin(), + Self::Line(curve) => curve.origin, } } @@ -70,7 +69,7 @@ impl Curve { pub fn transform(self, transform: &Transform) -> Self { match self { Self::Circle(curve) => Self::Circle(curve.transform(transform)), - Self::Line(curve) => Self::Line(curve.transform(transform)), + Self::Line(curve) => Self::Line(transform.transform_line(&curve)), } } @@ -86,7 +85,7 @@ impl Curve { pub fn point_model_to_curve(&self, point: &Point<3>) -> Point<1> { match self { Self::Circle(curve) => curve.point_model_to_curve(point), - Self::Line(curve) => curve.point_model_to_curve(point), + Self::Line(curve) => curve.convert_point_to_line_coords(point), } } @@ -94,7 +93,7 @@ impl Curve { pub fn point_curve_to_model(&self, point: &Point<1>) -> Point<3> { match self { Self::Circle(curve) => curve.point_curve_to_model(point), - Self::Line(curve) => curve.point_curve_to_model(point), + Self::Line(curve) => curve.convert_point_from_line_coords(point), } } @@ -102,7 +101,7 @@ impl Curve { pub fn vector_curve_to_model(&self, point: &Vector<1>) -> Vector<3> { match self { Self::Circle(curve) => curve.vector_curve_to_model(point), - Self::Line(curve) => curve.vector_curve_to_model(point), + Self::Line(curve) => curve.convert_vector_from_line_coords(point), } } } diff --git a/crates/fj-kernel/src/geometry/mod.rs b/crates/fj-kernel/src/geometry/mod.rs index 4781fe43d..1455f48bd 100644 --- a/crates/fj-kernel/src/geometry/mod.rs +++ b/crates/fj-kernel/src/geometry/mod.rs @@ -10,7 +10,7 @@ mod points; mod surfaces; pub use self::{ - curves::{Circle, Curve, Line}, + curves::{Circle, Curve}, points::Point, surfaces::{Surface, SweptCurve}, }; diff --git a/crates/fj-kernel/src/geometry/surfaces/swept.rs b/crates/fj-kernel/src/geometry/surfaces/swept.rs index ae04a3886..c8d2b3513 100644 --- a/crates/fj-kernel/src/geometry/surfaces/swept.rs +++ b/crates/fj-kernel/src/geometry/surfaces/swept.rs @@ -16,7 +16,7 @@ impl SweptCurve { /// Construct a plane from 3 points #[cfg(test)] pub fn plane_from_points([a, b, c]: [Point<3>; 3]) -> Self { - use crate::geometry::Line; + use fj_math::Line; let curve = Curve::Line(Line::from_points([a, b])); let path = c - a; @@ -63,9 +63,9 @@ impl SweptCurve { #[cfg(test)] mod tests { - use fj_math::{Point, Vector}; + use fj_math::{Line, Point, Vector}; - use crate::geometry::{Curve, Line}; + use crate::geometry::Curve; use super::SweptCurve; diff --git a/crates/fj-kernel/src/topology/builder.rs b/crates/fj-kernel/src/topology/builder.rs index 6f17eef6d..66c72fb12 100644 --- a/crates/fj-kernel/src/topology/builder.rs +++ b/crates/fj-kernel/src/topology/builder.rs @@ -1,7 +1,7 @@ -use fj_math::{Point, Scalar, Vector}; +use fj_math::{Line, Point, Scalar, Vector}; use crate::{ - geometry::{Circle, Curve, Line, Surface}, + geometry::{Circle, Curve, Surface}, shape::{Handle, Shape, ValidationResult}, };