From 773f0edb4aca3d7cf7699d346c25b46e466dd276 Mon Sep 17 00:00:00 2001 From: Connor Lennox Date: Fri, 6 May 2022 13:06:32 -0400 Subject: [PATCH 1/9] correct triangle Winding and imports --- .../src/algorithms/triangulation/delaunay.rs | 6 --- crates/fj-math/src/triangle.rs | 42 +++++++++---------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/crates/fj-kernel/src/algorithms/triangulation/delaunay.rs b/crates/fj-kernel/src/algorithms/triangulation/delaunay.rs index b41593062..8cabf2f8f 100644 --- a/crates/fj-kernel/src/algorithms/triangulation/delaunay.rs +++ b/crates/fj-kernel/src/algorithms/triangulation/delaunay.rs @@ -22,12 +22,6 @@ pub fn triangulate( let triangle = match orientation { Winding::Ccw => [v0, v1, v2], Winding::Cw => [v0, v2, v1], - Winding::None => { - panic!( - "Triangle returned from triangulation isn't actually a \ - triangle" - ); - } }; triangles.push(triangle); diff --git a/crates/fj-math/src/triangle.rs b/crates/fj-math/src/triangle.rs index 2ae6c1c56..e9ef4169d 100644 --- a/crates/fj-math/src/triangle.rs +++ b/crates/fj-math/src/triangle.rs @@ -1,29 +1,9 @@ -use crate::Vector; - -use super::{Point, Scalar}; - use parry2d_f64::utils::point_in_triangle::{corner_direction, Orientation}; use parry3d_f64::query::{Ray, RayCast as _}; -/// Winding direction of a triangle. -pub enum Winding { - /// Counter-clockwise - Ccw, - /// Clockwise - Cw, - /// Neither (straight lines) - None, -} +use crate::Vector; -impl From for Winding { - fn from(o: Orientation) -> Self { - match o { - Orientation::Ccw => Winding::Ccw, - Orientation::Cw => Winding::Cw, - Orientation::None => Winding::None, - } - } -} +use super::{Point, Scalar}; /// A triangle /// @@ -116,6 +96,24 @@ where } } +/// Winding direction of a triangle. +pub enum Winding { + /// Counter-clockwise + Ccw, + /// Clockwise + Cw, +} + +impl From for Winding { + fn from(o: Orientation) -> Self { + match o { + Orientation::Ccw => Winding::Ccw, + Orientation::Cw => Winding::Cw, + Orientation::None => unreachable!("not a triangle"), + } + } +} + #[cfg(test)] mod tests { use crate::Point; From f00b094fde311bdd837115be366969d78aac60e2 Mon Sep 17 00:00:00 2001 From: Connor Lennox Date: Fri, 6 May 2022 13:07:14 -0400 Subject: [PATCH 2/9] more efficient winding direction calculation --- crates/fj-math/src/triangle.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/fj-math/src/triangle.rs b/crates/fj-math/src/triangle.rs index e9ef4169d..b9c2ce640 100644 --- a/crates/fj-math/src/triangle.rs +++ b/crates/fj-math/src/triangle.rs @@ -59,8 +59,8 @@ impl Triangle { impl Triangle<2> { /// Returns the direction of the line through the points of the triangle. pub fn winding_direction(&self) -> Winding { - let [v0, v1, v2] = self.points; - corner_direction(&v0.to_na(), &v1.to_na(), &v2.to_na()).into() + let [v0, v1, v2] = self.points.map(|point| point.to_na()); + corner_direction(&v0, &v1, &v2).into() } } From 0863aab3207b41e81a6aec144465a4a5218824a5 Mon Sep 17 00:00:00 2001 From: Connor Lennox Date: Fri, 6 May 2022 13:08:03 -0400 Subject: [PATCH 3/9] correct comments/function naming --- crates/fj-math/src/transform.rs | 6 +++--- crates/fj-viewer/src/graphics/transform.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/fj-math/src/transform.rs b/crates/fj-math/src/transform.rs index 2f12abcd1..6b47c5b1b 100644 --- a/crates/fj-math/src/transform.rs +++ b/crates/fj-math/src/transform.rs @@ -4,7 +4,7 @@ use nalgebra::Perspective3; use super::{Aabb, Point, Segment, Triangle, Vector}; -/// A transform +/// An affine transform #[repr(C)] #[derive(Debug, Clone, Copy)] pub struct Transform(nalgebra::Transform); @@ -81,9 +81,9 @@ impl Transform { )) } - /// Project transform according to camera specfication, return data as a slice. + /// Project transform according to camera specfication, return data as an array. /// Used primarily for graphics code. - pub fn project_to_slice( + pub fn project_to_array( &self, aspect_ratio: f64, fovy: f64, diff --git a/crates/fj-viewer/src/graphics/transform.rs b/crates/fj-viewer/src/graphics/transform.rs index b384d63cb..d7588b0eb 100644 --- a/crates/fj-viewer/src/graphics/transform.rs +++ b/crates/fj-viewer/src/graphics/transform.rs @@ -18,7 +18,7 @@ impl Transform { pub fn for_vertices(camera: &Camera, aspect_ratio: f64) -> Self { let field_of_view_in_y = camera.field_of_view_in_x() / aspect_ratio; - let transform = camera.camera_to_model().project_to_slice( + let transform = camera.camera_to_model().project_to_array( aspect_ratio, field_of_view_in_y, camera.near_plane(), From 60f14d37b58197bb65698b3afdfa3dae85c1ba59 Mon Sep 17 00:00:00 2001 From: Connor Lennox Date: Fri, 6 May 2022 13:09:19 -0400 Subject: [PATCH 4/9] switch f64 types to Scalar --- crates/fj-math/src/transform.rs | 17 +++++++++++------ crates/fj-math/src/triangle.rs | 6 ++++-- crates/fj-viewer/src/graphics/transform.rs | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/crates/fj-math/src/transform.rs b/crates/fj-math/src/transform.rs index 6b47c5b1b..164044f12 100644 --- a/crates/fj-math/src/transform.rs +++ b/crates/fj-math/src/transform.rs @@ -2,6 +2,8 @@ use std::ops; use nalgebra::Perspective3; +use crate::Scalar; + use super::{Aabb, Point, Segment, Triangle, Vector}; /// An affine transform @@ -89,13 +91,16 @@ impl Transform { fovy: f64, znear: f64, zfar: f64, - ) -> [f64; 16] { + ) -> [Scalar; 16] { let projection = Perspective3::new(aspect_ratio, fovy, znear, zfar); - let mut res = [0f64; 16]; - res.copy_from_slice( - (projection.to_projective() * self.0).matrix().as_slice(), - ); - res + (projection.to_projective() * self.0) + .matrix() + .as_slice() + .iter() + .map(|f| Scalar::from(*f)) + .collect::>() + .try_into() + .unwrap() } /// Transform the given axis-aligned bounding box diff --git a/crates/fj-math/src/triangle.rs b/crates/fj-math/src/triangle.rs index b9c2ce640..5775d9d6b 100644 --- a/crates/fj-math/src/triangle.rs +++ b/crates/fj-math/src/triangle.rs @@ -77,13 +77,15 @@ impl Triangle<3> { dir: Vector<3>, max_toi: f64, solid: bool, - ) -> Option { + ) -> Option { let ray = Ray { origin: origin.to_na(), dir: dir.to_na(), }; - self.to_parry().cast_local_ray(&ray, max_toi, solid) + self.to_parry() + .cast_local_ray(&ray, max_toi, solid) + .map(|f| f.into()) } } diff --git a/crates/fj-viewer/src/graphics/transform.rs b/crates/fj-viewer/src/graphics/transform.rs index d7588b0eb..6162a254a 100644 --- a/crates/fj-viewer/src/graphics/transform.rs +++ b/crates/fj-viewer/src/graphics/transform.rs @@ -25,7 +25,7 @@ impl Transform { camera.far_plane(), ); - Self(transform.map(|val| val as f32)) + Self(transform.map(|scalar| scalar.into_f32())) } /// Compute transform used for normals From 59cc33dc3c32bd02575714ebbaf0d07e612b89f9 Mon Sep 17 00:00:00 2001 From: Connor Lennox Date: Fri, 6 May 2022 13:09:46 -0400 Subject: [PATCH 5/9] simplify camera translation --- crates/fj-viewer/src/camera.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/fj-viewer/src/camera.rs b/crates/fj-viewer/src/camera.rs index 259c769c9..56dd0fef7 100644 --- a/crates/fj-viewer/src/camera.rs +++ b/crates/fj-viewer/src/camera.rs @@ -76,11 +76,11 @@ impl Camera { far_plane: Self::DEFAULT_FAR_PLANE, rotation: Transform::identity(), - translation: Transform::translation(Vector::from_components_f64([ - initial_offset.x.into_f64(), - initial_offset.y.into_f64(), - -initial_distance.into_f64(), - ])), + translation: Transform::translation([ + initial_offset.x, + initial_offset.y, + -initial_distance, + ]), } } From 75d5757dfae93d104a9b6a4083c0d8c8e661d35a Mon Sep 17 00:00:00 2001 From: Connor Lennox Date: Fri, 6 May 2022 13:10:32 -0400 Subject: [PATCH 6/9] tweak comment --- crates/fj-math/src/point.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/fj-math/src/point.rs b/crates/fj-math/src/point.rs index 4b06b462d..fcacddda8 100644 --- a/crates/fj-math/src/point.rs +++ b/crates/fj-math/src/point.rs @@ -59,7 +59,7 @@ impl Point { } } - /// Gives the distance between two Points. + /// Gives the distance between two points. pub fn distance(p1: &Point, p2: &Point) -> Scalar { (p1.coords - p2.coords).magnitude() } From d076af5810572431a330045a184673ebe1798546 Mon Sep 17 00:00:00 2001 From: Connor Lennox Date: Fri, 6 May 2022 13:11:08 -0400 Subject: [PATCH 7/9] use explicit type name --- crates/fj-viewer/src/graphics/transform.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/fj-viewer/src/graphics/transform.rs b/crates/fj-viewer/src/graphics/transform.rs index 6162a254a..f4d40d02e 100644 --- a/crates/fj-viewer/src/graphics/transform.rs +++ b/crates/fj-viewer/src/graphics/transform.rs @@ -1,7 +1,6 @@ use bytemuck::{Pod, Zeroable}; use crate::camera::Camera; -use fj_math::Transform as fjTransform; #[derive(Clone, Copy, Pod, Zeroable)] #[repr(transparent)] @@ -9,7 +8,7 @@ pub struct Transform(pub [f32; 16]); impl Transform { pub fn identity() -> Self { - Self::from(&fjTransform::identity()) + Self::from(&fj_math::Transform::identity()) } /// Compute transform used for vertices @@ -39,8 +38,8 @@ impl Transform { } } -impl From<&fjTransform> for Transform { - fn from(other: &fjTransform) -> Self { +impl From<&fj_math::Transform> for Transform { + fn from(other: &fj_math::Transform) -> Self { let mut native = [0.0; 16]; native.copy_from_slice(other.data()); From fcc2eac385fc4da11256c0912499326c3aedc066 Mon Sep 17 00:00:00 2001 From: Connor Lennox Date: Fri, 6 May 2022 13:11:40 -0400 Subject: [PATCH 8/9] shorten vertex coord calculation --- crates/fj-viewer/src/graphics/vertices.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/fj-viewer/src/graphics/vertices.rs b/crates/fj-viewer/src/graphics/vertices.rs index 416a98460..67aa3a4ce 100644 --- a/crates/fj-viewer/src/graphics/vertices.rs +++ b/crates/fj-viewer/src/graphics/vertices.rs @@ -34,11 +34,7 @@ impl Vertices { color: [f32; 4], ) { let line = line.into_iter().map(|point| Vertex { - position: [ - point.x.into_f32(), - point.y.into_f32(), - point.z.into_f32(), - ], + position: point.coords.components.map(|scalar| scalar.into_f32()), normal, color, }); From 5a75026cd587c4cd74532785aa82208ae485d79e Mon Sep 17 00:00:00 2001 From: Connor Lennox Date: Fri, 6 May 2022 13:12:17 -0400 Subject: [PATCH 9/9] simplify translation construction --- crates/fj-viewer/src/input/movement.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/fj-viewer/src/input/movement.rs b/crates/fj-viewer/src/input/movement.rs index 9377f8eae..c71f76160 100644 --- a/crates/fj-viewer/src/input/movement.rs +++ b/crates/fj-viewer/src/input/movement.rs @@ -1,4 +1,4 @@ -use fj_math::{Point, Transform, Vector}; +use fj_math::{Point, Scalar, Transform, Vector}; use winit::dpi::PhysicalPosition; use crate::{ @@ -49,11 +49,12 @@ impl Movement { let diff = (cursor - previous) * d2 / d1; let offset = camera.camera_to_model().transform_vector(&diff); - camera.translation = - camera.translation - * Transform::translation(Vector::from_components_f64( - [offset.x.into(), offset.y.into(), 0.0], - )); + camera.translation = camera.translation + * Transform::translation(Vector::from([ + offset.x, + offset.y, + Scalar::ZERO, + ])); } }