Skip to content

Commit

Permalink
Major API changes (#5)
Browse files Browse the repository at this point in the history
* v0.2

* update utils3d
  • Loading branch information
ciscorn authored Mar 26, 2024
1 parent 0303da0 commit afcb0f0
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 371 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "earcut-rs"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Taku Fukada <[email protected]>", "MIERUNE Inc. <[email protected]>"]
license-file = "LICENSE.txt"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
A Rust port of the [mapbox/earcut](https://github.com/mapbox/earcut) polygon triangulation library, implemented from scratch with reference to [donbright/earcutr](https://github.com/donbright/earcutr).

- Based on the latest earcut 2.2.4 release.
- An additional utility `utils3d` can be used to project polygons from 3D to 2D space before triangulation.
- License: ISC

<p align="center">
<img src="./docs/image.png" width="300">
</p>
22 changes: 11 additions & 11 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use criterion::{criterion_group, criterion_main, Criterion};

use earcut_rs::Earcut;

fn load_fixture(name: &str) -> (Vec<f64>, Vec<usize>) {
fn load_fixture(name: &str) -> (Vec<[f64; 2]>, Vec<usize>) {
// load JSON
type Coords = Vec<Vec<[f64; 2]>>;
let s = fs::read_to_string("./tests/fixtures/".to_string() + name + ".json").unwrap();
let expected = serde_json::from_str::<Coords>(&s).unwrap();

// prepare input
let num_holes = expected.len();
let data: Vec<_> = expected.clone().into_iter().flatten().flatten().collect();
let data: Vec<_> = expected.clone().into_iter().flatten().collect();
let hole_indices: Vec<_> = expected
.iter()
.map(|x| x.len())
Expand All @@ -33,64 +33,64 @@ fn bench(c: &mut Criterion) {
c.bench_function("water", |b| {
let (data, hole_indices) = load_fixture("water");
b.iter(|| {
earcut.earcut(&data, &hole_indices, 2, &mut triangles);
earcut.earcut(&data, &hole_indices, &mut triangles);
})
});

c.bench_function("building", |b| {
let (data, hole_indices) = load_fixture("building");
b.iter(|| {
earcut.earcut(&data, &hole_indices, 2, &mut triangles);
earcut.earcut(&data, &hole_indices, &mut triangles);
})
});

c.bench_function("water2", |b| {
let (data, hole_indices) = load_fixture("water2");
b.iter(|| {
earcut.earcut(&data, &hole_indices, 2, &mut triangles);
earcut.earcut(&data, &hole_indices, &mut triangles);
})
});

c.bench_function("water3", |b| {
let (data, hole_indices) = load_fixture("water3");
b.iter(|| {
earcut.earcut(&data, &hole_indices, 2, &mut triangles);
earcut.earcut(&data, &hole_indices, &mut triangles);
})
});

c.bench_function("water3b", |b| {
let (data, hole_indices) = load_fixture("water3b");
b.iter(|| {
earcut.earcut(&data, &hole_indices, 2, &mut triangles);
earcut.earcut(&data, &hole_indices, &mut triangles);
})
});

c.bench_function("water-huge", |b| {
let (data, hole_indices) = load_fixture("water-huge");
b.iter(|| {
earcut.earcut(&data, &hole_indices, 2, &mut triangles);
earcut.earcut(&data, &hole_indices, &mut triangles);
assert_eq!(triangles.len(), 5177 * 3)
})
});

c.bench_function("water-huge2", |b| {
let (data, hole_indices) = load_fixture("water-huge2");
b.iter(|| {
earcut.earcut(&data, &hole_indices, 2, &mut triangles);
earcut.earcut(&data, &hole_indices, &mut triangles);
})
});

c.bench_function("rain", |b| {
let (data, hole_indices) = load_fixture("rain");
b.iter(|| {
earcut.earcut(&data, &hole_indices, 2, &mut triangles);
earcut.earcut(&data, &hole_indices, &mut triangles);
})
});

c.bench_function("hilbert", |b| {
let (data, hole_indices) = load_fixture("hilbert");
b.iter(|| {
earcut.earcut(&data, &hole_indices, 2, &mut triangles);
earcut.earcut(&data, &hole_indices, &mut triangles);
})
});
}
Expand Down
Binary file added docs/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn load_fixture(name: &str, num_triangles: usize, expected_deviation: f64) {

// prepare input
let num_holes = expected.len();
let vertices: Vec<_> = expected.clone().into_iter().flatten().flatten().collect();
let vertices: Vec<_> = expected.clone().into_iter().flatten().collect();
let hole_indices: Vec<_> = expected
.into_iter()
.map(|x| x.len() as u32)
Expand All @@ -25,13 +25,13 @@ fn load_fixture(name: &str, num_triangles: usize, expected_deviation: f64) {
let mut triangles = vec![];
let mut earcut = Earcut::new();
for _ in 0..500 {
earcut.earcut(&vertices, &hole_indices, 2, &mut triangles);
earcut.earcut(&vertices, &hole_indices, &mut triangles);
}

// check
assert!(triangles.len() == num_triangles * 3);
assert!(triangles.len() == num_triangles);
if !triangles.is_empty() {
assert!(deviation(&vertices, &hole_indices, 2, &triangles) <= expected_deviation);
assert!(deviation(&vertices, &hole_indices, &triangles) <= expected_deviation);
}
}

Expand Down
Loading

0 comments on commit afcb0f0

Please sign in to comment.