Skip to content

Commit

Permalink
chore(refactor): split files
Browse files Browse the repository at this point in the history
  • Loading branch information
nash1111 committed Feb 4, 2024
1 parent 5249f86 commit 52843fa
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 116 deletions.
6 changes: 1 addition & 5 deletions examples/2d_plot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ fn create_random_points(length: usize) -> Vec<meshing::Point2D> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {

let args: Vec<String> = env::args().collect();
if args.len() != 2 {
eprintln!("Usage: cargo run --example 2d_plot [number_of_points]");
Expand All @@ -68,10 +67,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
root.fill(&WHITE)?;

let mut chart = ChartBuilder::on(&root)
.caption(
format!("Delaunay for {} points", num_points),
"sans-serif",
)
.caption(format!("Delaunay for {} points", num_points), "sans-serif")
.build_cartesian_2d(-2.0..2.0, -2.0..2.0)?;

let num_triangle = res.len();
Expand Down
25 changes: 20 additions & 5 deletions examples/simple_2d_triangulation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
fn main() {

// Start indexing from 1
let square = vec![
meshing::Point2D { index: 1, x: 0.0, y: 0.0 },
meshing::Point2D { index: 2, x: 1.0, y: 0.0 },
meshing::Point2D { index: 3, x: 0.0, y: 1.0 },
meshing::Point2D { index: 4, x: 1.0, y: 1.0 },
meshing::Point2D {
index: 1,
x: 0.0,
y: 0.0,
},
meshing::Point2D {
index: 2,
x: 1.0,
y: 0.0,
},
meshing::Point2D {
index: 3,
x: 0.0,
y: 1.0,
},
meshing::Point2D {
index: 4,
x: 1.0,
y: 1.0,
},
];
let res = meshing::bowyer_watson(square);
println!("{:?}", res);
Expand Down
72 changes: 72 additions & 0 deletions src/geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use crate::{Edge, Point2D, Triangle};

pub fn create_super_triangle(points: &Vec<Point2D>) -> Triangle {
match points.is_empty() {
true => panic!("The input points vector should not be empty."),
false => {}
}

let index = 0;
let mut min_x = f64::MAX;
let mut min_y = f64::MAX;
let mut max_x = f64::MIN;
let mut max_y = f64::MIN;

for point in points {
if point.x < min_x {
min_x = point.x;
}
if point.y < min_y {
min_y = point.y;
}
if point.x > max_x {
max_x = point.x;
}
if point.y > max_y {
max_y = point.y;
}
}

let margin = 100.0;

let a = Point2D {
index,
x: min_x - margin,
y: min_y - margin,
};
let b = Point2D {
index,
x: max_x + margin,
y: min_y - margin,
};
let c = Point2D {
index,
x: (min_x + max_x) / 2.0,
y: max_y + margin,
};

Triangle { a, b, c }
}

pub fn edge_is_shared_by_triangles(edge: &Edge, triangles: &Vec<Triangle>) -> bool {
for triangle in triangles {
let edges_of_triangle = triangle.edges();
for edge_of_triangle in edges_of_triangle {
if edge_of_triangle == *edge {
return true;
}
if edge_of_triangle.reverse() == *edge {
return true;
}
}
}
false
}

pub fn retriangulate(edge: &Edge, point: &Point2D) -> Triangle {
Triangle {
a: edge.start,
b: edge.end,
c: *point,
}
}
109 changes: 4 additions & 105 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use geometry::{create_super_triangle, edge_is_shared_by_triangles, retriangulate};
pub use model::{Edge, Point2D, Triangle};
use triangle_utils::remove_triangles_with_vertices_from_super_triangle;

mod geometry;
mod model;
mod triangle_utils;

pub fn bowyer_watson(points: Vec<Point2D>) -> Vec<Triangle> {
let mut triangulation: Vec<Triangle> = Vec::new();
Expand Down Expand Up @@ -46,111 +50,6 @@ pub fn bowyer_watson(points: Vec<Point2D>) -> Vec<Triangle> {
remove_triangles_with_vertices_from_super_triangle(&mut triangulation, &super_triangle)
}

fn create_super_triangle(points: &Vec<Point2D>) -> Triangle {
match points.is_empty() {
true => panic!("The input points vector should not be empty."),
false => {}
}

let index = 0;
let mut min_x = f64::MAX;
let mut min_y = f64::MAX;
let mut max_x = f64::MIN;
let mut max_y = f64::MIN;

for point in points {
if point.x < min_x {
min_x = point.x;
}
if point.y < min_y {
min_y = point.y;
}
if point.x > max_x {
max_x = point.x;
}
if point.y > max_y {
max_y = point.y;
}
}

let margin = 100.0;

let a = Point2D {
index,
x: min_x - margin,
y: min_y - margin,
};
let b = Point2D {
index,
x: max_x + margin,
y: min_y - margin,
};
let c = Point2D {
index,
x: (min_x + max_x) / 2.0,
y: max_y + margin,
};

Triangle { a, b, c }
}

fn edge_is_shared_by_triangles(edge: &Edge, triangles: &Vec<Triangle>) -> bool {
for triangle in triangles {
let edges_of_triangle = triangle.edges();
for edge_of_triangle in edges_of_triangle {
if edge_of_triangle == *edge {
return true;
}
if edge_of_triangle.reverse() == *edge {
return true;
}
}
}
false
}

fn retriangulate(edge: &Edge, point: &Point2D) -> Triangle {
Triangle {
a: edge.start,
b: edge.end,
c: *point,
}
}

fn triangle_contains_vertex_from_super_triangle(
triangle: &Triangle,
super_triangle: &Triangle,
) -> bool {
let super_triangle_vertices = super_triangle.vertices();
let triangle_vertices = triangle.vertices();
for super_triangle_vertex in super_triangle_vertices {
if super_triangle_vertex == triangle_vertices[0] {
return true;
}
if super_triangle_vertex == triangle_vertices[1] {
return true;
}
if super_triangle_vertex == triangle_vertices[2] {
return true;
}
}
false
}

fn remove_triangles_with_vertices_from_super_triangle(
triangles: &Vec<Triangle>,
super_triangle: &Triangle,
) -> Vec<Triangle> {
let mut res: Vec<Triangle> = Vec::new();

for triangle in triangles {
if !triangle_contains_vertex_from_super_triangle(triangle, super_triangle) {
res.push(*triangle);
}
}
res
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
6 changes: 5 additions & 1 deletion src/model/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ impl Triangle {
* (self.b.x - self.c.x))
/ d;

Point2D { index: i64::MAX, x: ux, y: uy }
Point2D {
index: i64::MAX,
x: ux,
y: uy,
}
}

pub fn generate_circumcircle(&self) -> Circle {
Expand Down
35 changes: 35 additions & 0 deletions src/triangle_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::Triangle;

pub fn triangle_contains_vertex_from_super_triangle(
triangle: &Triangle,
super_triangle: &Triangle,
) -> bool {
let super_triangle_vertices = super_triangle.vertices();
let triangle_vertices = triangle.vertices();
for super_triangle_vertex in super_triangle_vertices {
if super_triangle_vertex == triangle_vertices[0] {
return true;
}
if super_triangle_vertex == triangle_vertices[1] {
return true;
}
if super_triangle_vertex == triangle_vertices[2] {
return true;
}
}
false
}

pub fn remove_triangles_with_vertices_from_super_triangle(
triangles: &Vec<Triangle>,
super_triangle: &Triangle,
) -> Vec<Triangle> {
let mut res: Vec<Triangle> = Vec::new();

for triangle in triangles {
if !triangle_contains_vertex_from_super_triangle(triangle, super_triangle) {
res.push(*triangle);
}
}
res
}

0 comments on commit 52843fa

Please sign in to comment.