-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
119 lines (98 loc) · 3.28 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// #![warn(missing_docs)]
//! Using <https://crates.io/crates/clap> for command line arguments
//! Using <https://crates.io/crates/spade> for 2D Delaunay triangulation
use clap::Parser;
use spade::InsertionError;
use spade::Triangulation;
mod triangulations {
pub mod spade_triangulations;
pub mod triangulation_2;
}
use triangulations::spade_triangulations::generate_random_delaunay2;
/// Contains utility functions for the `cdt-rs` crate.
pub mod utilities;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
/// Configuration options for the `cdt-rs` crate.
pub struct Config {
/// Dimensionality of the triangulation
#[arg(short, long, value_parser = clap::value_parser!(u32).range(2..4))]
dimension: Option<u32>,
/// Number of vertices
#[arg(short, long, value_parser = clap::value_parser!(u32).range(3..))]
vertices: u32,
/// Number of timeslices
#[arg(short, long, value_parser = clap::value_parser!(u32).range(1..))]
timeslices: u32,
}
impl Config {
/// Builds a new instance of `Config`.
pub fn build() -> Self {
Self::parse()
}
}
// pub fn run(config: Config) -> Result<Vec<triangulation::Triangle>, Box<dyn std::error::Error>> {
pub fn run(
config: &Config,
) -> Result<spade::DelaunayTriangulation<spade::Point2<f64>>, InsertionError> {
let vertices = config.vertices;
let timeslices = config.timeslices;
if config.dimension.is_some_and(|d| d != 2) {
eprintln!("Only 2D triangulations are supported right now.");
std::process::exit(1);
}
println!("Dimensionality: {}", config.dimension.unwrap_or(2));
println!("Number of vertices: {}", vertices);
println!("Number of timeslices: {}", timeslices);
let triangulation = generate_random_delaunay2(vertices)?;
println!("Number of triangles: {}", triangulation.num_inner_faces());
// let scale = 10.0; // The size of the grid
// let mut points = Vec::new();
// for _n in 1..vertices {
// let x = utilities::generate_random_float() * scale;
// let y = utilities::generate_random_float() * scale;
// points.push(triangulation::Point { x, y });
// }
// let triangulation = triangulation::bowyer_watson(points);
// for triangle in &triangulation {
// println!(
// "Triangle: {:?} Center: {:?}",
// triangle.vertices,
// triangle.center()
// );
// }
for vertex in triangulation.vertices() {
println!("Vertex: {:?}", vertex.position());
}
for triangle in triangulation.inner_faces() {
println!("Triangle: {:?}", triangle.positions());
}
Ok(triangulation)
}
#[cfg(test)]
mod lib_tests {
use super::*;
#[test]
fn test_run() {
let config = Config {
dimension: Some(2),
vertices: 32,
timeslices: 3,
};
assert!(config.dimension.is_some());
assert!(run(&config).is_ok());
}
#[test]
fn points_is_number_of_vertices() {
let config = Config {
dimension: Some(2),
vertices: 32,
timeslices: 3,
};
let triangulation = run(&config).unwrap();
assert_eq!(
triangulation.num_vertices(),
config.vertices.try_into().unwrap()
);
}
}