Skip to content

Commit

Permalink
Merge pull request hannobraun#216 from hannobraun/decorum
Browse files Browse the repository at this point in the history
Remove dependency on Decorum
  • Loading branch information
hannobraun authored Feb 19, 2022
2 parents 9152664 + 875eb4a commit 9fb0f40
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 110 deletions.
45 changes: 6 additions & 39 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ categories = ["mathematics", "rendering"]
anyhow = "1.0.53"
approx = "0.5.1"
bytemuck = "1.7.3"
decorum = "0.3.1"
futures = "0.3.21"
libloading = "0.7.2"
nalgebra = "0.30.0"
Expand Down
8 changes: 1 addition & 7 deletions src/graphics/vertices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nalgebra::{vector, Point};
use crate::{
debug::DebugInfo,
math::Triangle,
mesh::{HashVector, Index, MeshMaker},
mesh::{Index, MeshMaker},
};

#[derive(Debug)]
Expand Down Expand Up @@ -57,12 +57,6 @@ impl From<&Vec<Triangle>> for Vertices {

let normal = (b - a).cross(&(c - a)).normalize();

let a = HashVector::from(&a.to_na());
let b = HashVector::from(&b.to_na());
let c = HashVector::from(&c.to_na());

let normal = HashVector::from(&normal.to_na());

mesh.push((a, normal));
mesh.push((b, normal));
mesh.push((c, normal));
Expand Down
20 changes: 4 additions & 16 deletions src/kernel/approximation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{cmp::Ordering, collections::HashSet};

use decorum::R64;

use crate::math::{Point, Scalar, Segment};

use super::topology::edges::{Cycle, Edge, Edges};
Expand Down Expand Up @@ -149,20 +147,18 @@ impl Approximation {
// Verify that there are no duplicate points
let mut points = HashSet::new();
for &point in &self.points {
let point_r64 = point_to_r64(point);

if points.contains(&point_r64) {
if points.contains(&point) {
duplicate_points.push(point);
}

points.insert(point_r64);
points.insert(point);
}

let mut segments = HashSet::new();
for &segment @ Segment { a, b } in &self.segments {
// Verify that there are no duplicate segments
let ab = [point_to_r64(a), point_to_r64(b)];
let ba = [point_to_r64(b), point_to_r64(a)];
let ab = [a, b];
let ba = [b, a];
if segments.contains(&ab) {
duplicate_segments.push(segment);
}
Expand Down Expand Up @@ -213,14 +209,6 @@ pub struct ValidationError {
pub segments_with_invalid_points: Vec<Segment<3>>,
}

fn point_to_r64(point: Point<3>) -> [R64; 3] {
[
point.x().into_f64().into(),
point.y().into_f64().into(),
point.z().into_f64().into(),
]
}

#[cfg(test)]
mod tests {
use std::cell::RefCell;
Expand Down
15 changes: 6 additions & 9 deletions src/kernel/topology/faces.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::collections::BTreeSet;

use decorum::R64;
use parry2d_f64::query::{Ray as Ray2, RayCast as _};
use parry3d_f64::query::Ray as Ray3;

Expand Down Expand Up @@ -193,11 +192,10 @@ impl Face {
let edge =
Segment::from(edge.map(|point| point.value));

let intersection = edge.to_parry().cast_local_ray(
&ray,
f64::INFINITY,
true,
);
let intersection = edge
.to_parry()
.cast_local_ray(&ray, f64::INFINITY, true)
.map(|t| Scalar::from_f64(t));

if let Some(t) = intersection {
// Due to slight inaccuracies, we might get
Expand All @@ -206,9 +204,8 @@ impl Face {
let eps = 1_000_000.0;
let t = (t * eps).round() / eps;

let t_r64: R64 = t.into();
if hits.insert(t_r64) {
check.hits.push(t);
if hits.insert(t) {
check.hits.push(t.into_f64());
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
debug::DebugInfo,
graphics::{DrawConfig, Renderer},
kernel::Shape as _,
mesh::{HashVector, MeshMaker},
mesh::MeshMaker,
model::Model,
window::Window,
};
Expand Down Expand Up @@ -97,7 +97,7 @@ fn main() -> anyhow::Result<()> {

for triangle in triangles {
for vertex in triangle.vertices() {
mesh_maker.push(HashVector::from(&vertex.to_na()));
mesh_maker.push(vertex);
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/math/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ impl<const D: usize> From<nalgebra::Point<f64, D>> for Point<D> {
}
}

impl<const D: usize> From<Point<D>> for [f32; D] {
fn from(point: Point<D>) -> Self {
point.0.map(|scalar| scalar.into_f32())
}
}

impl<const D: usize> From<Point<D>> for [f64; D] {
fn from(point: Point<D>) -> Self {
point.0.map(|scalar| scalar.into_f64())
}
}

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

Expand Down
12 changes: 11 additions & 1 deletion src/math/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ impl Scalar {
Self::from_f64(scalar as f64)
}

/// Convert the scalar into a `f64`
/// Convert the scalar into an `f32`
pub fn into_f32(self) -> f32 {
self.0 as f32
}

/// Convert the scalar into an `f64`
pub fn into_f64(self) -> f64 {
self.0
}
Expand All @@ -87,6 +92,11 @@ impl Scalar {
self.0.ceil().into()
}

/// Round the scalar
pub fn round(self) -> Self {
self.0.round().into()
}

/// Compute the cosine
pub fn cos(self) -> Self {
self.0.cos().into()
Expand Down
6 changes: 6 additions & 0 deletions src/math/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ impl<const D: usize> From<nalgebra::SVector<f64, D>> for Vector<D> {
}
}

impl<const D: usize> From<Vector<D>> for [f32; D] {
fn from(vector: Vector<D>) -> Self {
vector.0.map(|scalar| scalar.into_f32())
}
}

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

Expand Down
35 changes: 0 additions & 35 deletions src/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::{collections::HashMap, hash::Hash};

use decorum::R64;
use nalgebra::{Point, SVector};

/// API for creating a mesh
pub struct MeshMaker<V> {
vertices: Vec<V>,
Expand Down Expand Up @@ -47,37 +44,5 @@ where
}
}

/// A point/vector type that can be used as a [`HashMap`] key
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct HashVector(pub [R64; 3]);

impl From<&Point<f64, 3>> for HashVector {
fn from(point: &Point<f64, 3>) -> Self {
Self([R64::from(point.x), R64::from(point.y), R64::from(point.z)])
}
}

impl From<&SVector<f64, 3>> for HashVector {
fn from(vector: &SVector<f64, 3>) -> Self {
Self([
R64::from(vector.x),
R64::from(vector.y),
R64::from(vector.z),
])
}
}

impl From<HashVector> for [f32; 3] {
fn from(hash_vector: HashVector) -> Self {
hash_vector.0.map(|coord| coord.into_inner() as f32)
}
}

impl From<HashVector> for [f64; 3] {
fn from(hash_vector: HashVector) -> Self {
hash_vector.0.map(|coord| coord.into_inner())
}
}

/// An index that refers to a vertex in a mesh
pub type Index = u32;

0 comments on commit 9fb0f40

Please sign in to comment.