Skip to content

Commit

Permalink
Add unit tests to expose #7
Browse files Browse the repository at this point in the history
  • Loading branch information
hmeyer committed Jan 3, 2021
1 parent e1d1eec commit 47fe5c2
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/manifold_dual_contouring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ fn get_connected_edges(edge: Edge, cell: BitSet) -> BitSet {
panic!("Did not find edge_set for {:?} and {:?}", edge, cell);
}

// Returns all BitSets containing egdes connected to one of edge_set in this cell.
// Returns all BitSets containing egdes connected to one of edge_set in this cell.
fn get_connected_edges_from_edge_set(edge_set: BitSet, cell: BitSet) -> Vec<BitSet> {
let mut result = Vec::new();
for &cell_edge_set in CELL_CONFIGS[cell.as_u32() as usize].iter() {
Expand Down Expand Up @@ -994,6 +994,7 @@ impl<'a, S: From<f32> + RealField + Float + AsUSize> ManifoldDualContouring<'a,
mod tests {
use super::get_connected_edges_from_edge_set;
use crate::bitset::BitSet;
use nalgebra as na;
// Corner indexes
//
// 6---------------7
Expand Down Expand Up @@ -1031,4 +1032,58 @@ mod tests {
assert!(connected_edges.contains(&BitSet::from_4bits(5, 5, 6, 10)));
assert!(connected_edges.contains(&BitSet::from_4bits(3, 3, 4, 11)));
}

struct UnitSphere {
bbox: super::BoundingBox<f64>,
}
impl UnitSphere {
fn new() -> UnitSphere {
UnitSphere {
bbox: super::BoundingBox::new(
&na::Point3::new(-1., -1., -1.),
&na::Point3::new(1., 1., 1.),
),
}
}
}

impl super::ImplicitFunction<f64> for UnitSphere {
fn bbox(&self) -> &super::BoundingBox<f64> {
&self.bbox
}
fn value(&self, p: &na::Point3<f64>) -> f64 {
return na::Vector3::new(p.x, p.y, p.z).norm() - 1.0;
}
fn normal(&self, p: &na::Point3<f64>) -> na::Vector3<f64> {
return na::Vector3::new(p.x, p.y, p.z).normalize();
}
}

#[test]
fn unit_sphere_without_simplification() -> Result<(), crate::mesh::MeshError> {
let sphere = UnitSphere::new();
let mut mdc = super::ManifoldDualContouring::new(&sphere, 0.2, 0.0);
let mesh = mdc.tessellate().unwrap();
println!(
"mesh hash {} vertices and {} faces",
mesh.vertices.len(),
mesh.faces.len()
);
mesh.is_closed()
}

#[test]
#[ignore]
// This test exposes https://github.com/hmeyer/tessellation/issues/7
fn unit_sphere_with_simplification() -> Result<(), crate::mesh::MeshError> {
let sphere = UnitSphere::new();
let mut mdc = super::ManifoldDualContouring::new(&sphere, 0.2, 0.1);
let mesh = mdc.tessellate().unwrap();
println!(
"mesh hash {} vertices and {} faces",
mesh.vertices.len(),
mesh.faces.len()
);
mesh.is_closed()
}
}
51 changes: 51 additions & 0 deletions src/mesh.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
use alga::general::RealField;
use nalgebra as na;
use std::error::Error;
use std::fmt;
use std::fmt::Debug;

#[derive(Debug, PartialEq)]
pub struct MeshError {
msg: String,
}

impl fmt::Display for MeshError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MeshError {}", self.msg)
}
}

impl Error for MeshError {}

/// Mesh that will be returned from tessellate.
#[derive(Clone, Debug, PartialEq)]
pub struct Mesh<S> {
Expand Down Expand Up @@ -43,6 +58,42 @@ impl<S: RealField + Debug> Mesh<S> {
);
[v.0 as f32, v.1 as f32, v.2 as f32]
}
/// Returns whether or not the mesh is closed.
#[cfg(test)]
pub fn is_closed(&self) -> Result<(), MeshError>
where
f64: From<S>,
{
let mut edge_to_face = std::collections::HashMap::new();
for (face_index, face) in self.faces.iter().enumerate() {
for i in 0..3 {
if let Some(existing_index) =
edge_to_face.insert((face[i], face[(i + 1) % 3]), face_index)
{
return Err(MeshError {
msg: format!(
"Both face #{} and face #{} share edge {}->{}.",
existing_index,
face_index,
i,
(i + 1) % 3
),
});
}
}
}
for (edge, face_index) in edge_to_face.iter() {
if !edge_to_face.contains_key(&(edge.1, edge.0)) {
return Err(MeshError {
msg: format!(
"Unmachted edge {}->{} of face #{}.",
edge.0, edge.1, face_index
),
});
}
}
Ok(())
}
}

#[cfg(test)]
Expand Down

0 comments on commit 47fe5c2

Please sign in to comment.