Skip to content

Commit

Permalink
Merge pull request #374 from hannobraun/math
Browse files Browse the repository at this point in the history
Extract math code into dedicated crate
  • Loading branch information
hannobraun authored Mar 17, 2022
2 parents 4aeaa38 + 967d035 commit 15294c2
Show file tree
Hide file tree
Showing 43 changed files with 318 additions and 239 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"fj",
"fj-app",
"fj-math",

"models/cuboid",
"models/group",
Expand All @@ -11,4 +12,7 @@ members = [

"release-operator",
]
default-members = ["fj-app"]
default-members = [
"fj-app",
"fj-math",
]
8 changes: 5 additions & 3 deletions fj-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.5.0"
edition = "2021"

description = "The world needs another CAD program."
readme = "README.md"
readme = "../README.md"
repository = "https://github.com/hannobraun/fornjot"
license = "0BSD"
keywords = ["cad", "programmatic", "code-cad"]
Expand All @@ -15,13 +15,11 @@ categories = ["mathematics", "rendering"]
anyhow = "1.0.56"
approx = "0.5.1"
bytemuck = "1.8.0"
decorum = "0.3.1"
futures = "0.3.21"
libloading = "0.7.2"
map-macro = "0.2.0"
nalgebra = "0.30.0"
notify = "5.0.0-pre.14"
num-traits = "0.2.14"
parking_lot = "0.12.0"
parry2d-f64 = "0.8.0"
parry3d-f64 = "0.8.0"
Expand All @@ -45,6 +43,10 @@ features = ["env", "toml"]
version = "0.5.0"
path = "../fj"

[dependencies.fj-math]
version = "0.5.0"
path = "../fj-math"

[dependencies.serde]
version = "1.0.136"
features = ["derive"]
Expand Down
6 changes: 2 additions & 4 deletions fj-app/src/camera.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::f64::consts::FRAC_PI_2;

use fj_math::{Aabb, Scalar, Triangle};
use nalgebra::{Point, TAffine, Transform, Translation, Vector};
use parry3d_f64::query::{Ray, RayCast as _};
use winit::dpi::PhysicalPosition;

use crate::{
math::{Aabb, Scalar, Triangle},
window::Window,
};
use crate::window::Window;

/// The camera abstraction
///
Expand Down
5 changes: 2 additions & 3 deletions fj-app/src/graphics/config_ui.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::collections::HashMap;

use fj_math::Aabb;
use wgpu::util::StagingBelt;
use wgpu_glyph::{
ab_glyph::{FontArc, InvalidFont},
GlyphBrush, GlyphBrushBuilder, Section, Text,
};

use crate::math::Aabb;

use super::draw_config::DrawConfig;

#[derive(Debug)]
Expand Down Expand Up @@ -70,7 +69,7 @@ impl ConfigUi {
}

/* Render size of model bounding box */
let bbsize = aabb.size().components();
let bbsize = aabb.size().components;
let info = format!(
"Model bounding box size: {:0.1} {:0.1} {:0.1}",
bbsize[0].into_f32(),
Expand Down
2 changes: 1 addition & 1 deletion fj-app/src/graphics/geometries.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::math::Aabb;
use std::convert::TryInto;

use fj_math::Aabb;
use wgpu::util::DeviceExt;

use super::vertices::{Vertex, Vertices};
Expand Down
3 changes: 2 additions & 1 deletion fj-app/src/graphics/renderer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{io, mem::size_of};

use fj_math::{Aabb, Point};
use thiserror::Error;
use tracing::debug;
use wgpu::util::DeviceExt as _;
use wgpu_glyph::ab_glyph::InvalidFont;
use winit::dpi::PhysicalSize;

use crate::{camera::Camera, math::Aabb, math::Point, window::Window};
use crate::{camera::Camera, window::Window};

use super::{
config_ui::ConfigUi, draw_config::DrawConfig, drawables::Drawables,
Expand Down
2 changes: 1 addition & 1 deletion fj-app/src/graphics/vertices.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bytemuck::{Pod, Zeroable};
use fj_math::Triangle;
use nalgebra::{vector, Point};

use crate::{
debug::DebugInfo,
math::Triangle,
mesh::{Index, MeshMaker},
};

Expand Down
2 changes: 1 addition & 1 deletion fj-app/src/input/handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Instant;

use fj_math::Triangle;
use winit::{
dpi::PhysicalPosition,
event::{
Expand All @@ -10,7 +11,6 @@ use winit::{

use crate::{
camera::{Camera, FocusPoint},
math::Triangle,
window::Window,
};

Expand Down
25 changes: 11 additions & 14 deletions fj-app/src/kernel/algorithms/approximation.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::collections::HashSet;

use crate::{
kernel::topology::{
edges::{Cycle, Edge},
faces::Face,
vertices::Vertex,
},
math::{Point, Scalar, Segment},
use fj_math::{Point, Scalar, Segment};

use crate::kernel::topology::{
edges::{Cycle, Edge},
faces::Face,
vertices::Vertex,
};

/// An approximation of an edge, multiple edges, or a face
Expand Down Expand Up @@ -132,15 +131,13 @@ fn approximate_edge(

#[cfg(test)]
mod tests {
use fj_math::{Point, Scalar, Segment};
use map_macro::set;

use crate::{
kernel::{
geometry::Surface,
shape::Shape,
topology::{edges::Cycle, faces::Face, vertices::Vertex},
},
math::{Point, Scalar, Segment},
use crate::kernel::{
geometry::Surface,
shape::Shape,
topology::{edges::Cycle, faces::Face, vertices::Vertex},
};

use super::{approximate_edge, Approximation};
Expand Down
32 changes: 15 additions & 17 deletions fj-app/src/kernel/algorithms/sweep.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::collections::HashMap;

use crate::{
kernel::{
geometry::{surfaces::Swept, Surface},
shape::{Handle, Shape},
topology::{
edges::{Cycle, Edge},
faces::Face,
vertices::Vertex,
},
use fj_math::{Scalar, Transform, Triangle, Vector};

use crate::kernel::{
geometry::{surfaces::Swept, Surface},
shape::{Handle, Shape},
topology::{
edges::{Cycle, Edge},
faces::Face,
vertices::Vertex,
},
math::{Scalar, Transform, Triangle, Vector},
};

use super::approximation::Approximation;
Expand Down Expand Up @@ -322,13 +321,12 @@ impl Relation {

#[cfg(test)]
mod tests {
use crate::{
kernel::{
geometry::{surfaces::Swept, Surface},
shape::{Handle, Shape},
topology::{edges::Cycle, faces::Face, vertices::Vertex},
},
math::{Point, Scalar, Vector},
use fj_math::{Point, Scalar, Vector};

use crate::kernel::{
geometry::{surfaces::Swept, Surface},
shape::{Handle, Shape},
topology::{edges::Cycle, faces::Face, vertices::Vertex},
};

use super::sweep_shape;
Expand Down
3 changes: 2 additions & 1 deletion fj-app/src/kernel/algorithms/triangulation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use fj_math::Scalar;
use parry2d_f64::utils::point_in_triangle::{corner_direction, Orientation};
use spade::HasPosition;

use crate::{kernel::geometry, math::Scalar};
use crate::kernel::geometry;

/// Create a Delaunay triangulation of all points
pub fn triangulate(
Expand Down
4 changes: 2 additions & 2 deletions fj-app/src/kernel/geometry/curves/circle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::f64::consts::PI;

use crate::math::{Point, Scalar, Transform, Vector};
use fj_math::{Point, Scalar, Transform, Vector};

/// A circle
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
Expand Down Expand Up @@ -109,7 +109,7 @@ impl Circle {
mod tests {
use std::f64::consts::{FRAC_PI_2, PI};

use crate::math::{Point, Scalar, Vector};
use fj_math::{Point, Scalar, Vector};

use super::Circle;

Expand Down
5 changes: 2 additions & 3 deletions fj-app/src/kernel/geometry/curves/line.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::math::{Point, Transform, Vector};
use fj_math::{Point, Transform, Vector};

/// A line, defined by a point and a vector
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
Expand Down Expand Up @@ -83,11 +83,10 @@ mod tests {
use std::f64::consts::FRAC_PI_2;

use approx::assert_abs_diff_eq;
use fj_math::{Point, Vector};
use nalgebra::UnitQuaternion;
use parry3d_f64::math::{Isometry, Translation};

use crate::math::{Point, Vector};

use super::Line;

#[test]
Expand Down
2 changes: 1 addition & 1 deletion fj-app/src/kernel/geometry/curves/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod circle;
mod line;

use crate::math::{Point, Scalar, Transform, Vector};
use fj_math::{Point, Scalar, Transform, Vector};

pub use self::{circle::Circle, line::Line};

Expand Down
25 changes: 14 additions & 11 deletions fj-app/src/kernel/geometry/points.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::{Add, Sub};

use crate::math::{self, Vector};
use fj_math::Vector;

/// A point that can be losslessly converted into its canonical form
///
Expand All @@ -13,39 +13,42 @@ pub struct Point<const D: usize> {
/// The native form of the point is its representation in its native
/// coordinate system. This could be a 1-dimensional curve, 2-dimensional
/// surface, or 3-dimensional model coordinate system.
native: math::Point<D>,
native: fj_math::Point<D>,

/// The canonical form of the point
///
/// This is always the 3D representation of the point. Since this is always
/// kept here, unchanged, as the point is converted into other coordinate
/// systems, it allows for a lossless conversion back into 3D coordinates,
/// unaffected by floating point accuracy issues.
canonical: math::Point<3>,
canonical: fj_math::Point<3>,
}

impl<const D: usize> Point<D> {
/// Construct a new instance
///
/// Both the native and the canonical form must be provide. The caller must
/// guarantee that both of them match.
pub fn new(native: math::Point<D>, canonical: math::Point<3>) -> Self {
pub fn new(
native: fj_math::Point<D>,
canonical: fj_math::Point<3>,
) -> Self {
Self { native, canonical }
}

/// Access the point's native form
pub fn native(&self) -> math::Point<D> {
pub fn native(&self) -> fj_math::Point<D> {
self.native
}

/// Access the point's canonical form
pub fn canonical(&self) -> math::Point<3> {
pub fn canonical(&self) -> fj_math::Point<3> {
self.canonical
}
}

impl From<math::Point<3>> for Point<3> {
fn from(point: math::Point<3>) -> Self {
impl From<fj_math::Point<3>> for Point<3> {
fn from(point: fj_math::Point<3>) -> Self {
Self::new(point, point)
}
}
Expand All @@ -54,7 +57,7 @@ impl From<math::Point<3>> for Point<3> {
// `Point`, or the conversion back to 3D would be broken.

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

fn add(self, rhs: Vector<D>) -> Self::Output {
self.native.add(rhs)
Expand All @@ -69,10 +72,10 @@ impl<const D: usize> Sub<Self> for Point<D> {
}
}

impl<const D: usize> Sub<math::Point<D>> for Point<D> {
impl<const D: usize> Sub<fj_math::Point<D>> for Point<D> {
type Output = Vector<D>;

fn sub(self, rhs: math::Point<D>) -> Self::Output {
fn sub(self, rhs: fj_math::Point<D>) -> Self::Output {
self.native.sub(rhs)
}
}
Loading

0 comments on commit 15294c2

Please sign in to comment.