diff --git a/.gitignore b/.gitignore index 18477d495..5d70c8566 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,7 @@ Cargo.lock *.vtk !/assets/*.vtk !/examples/shape.vtk +!/examples/quads.vtk # ignore coverage-related files *.profraw @@ -36,4 +37,4 @@ perf.data.old # ignore bench result directories /fixed /grid -/thread \ No newline at end of file +/thread diff --git a/benches/Cargo.toml b/benches/Cargo.toml index f5ea610dd..358e4795d 100644 --- a/benches/Cargo.toml +++ b/benches/Cargo.toml @@ -81,3 +81,8 @@ harness = false name = "grisubal_grid_size" path = "benches/grisubal/grid_size.rs" harness = false + +[[bench]] +name = "triangulate_quads" +path = "benches/triangulate/quads.rs" +harness = false diff --git a/benches/benches/triangulate/quads.rs b/benches/benches/triangulate/quads.rs new file mode 100644 index 000000000..500de48a8 --- /dev/null +++ b/benches/benches/triangulate/quads.rs @@ -0,0 +1,93 @@ +// ------ IMPORTS + +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use honeycomb::prelude::{ + triangulation::{earclip_cell, fan_cell, TriangulateError}, + CMap2, CMapBuilder, DartIdentifier, Orbit2, OrbitPolicy, +}; +use honeycomb_benches::FloatType; + +// ------ CONTENT + +const PATH: &str = "../examples/quads.vtk"; + +fn fan_bench() -> Result<(), TriangulateError> { + let mut map: CMap2 = CMapBuilder::default().vtk_file(PATH).build().unwrap(); + + // prealloc darts + let faces = map.fetch_faces().identifiers.clone(); + let n_darts_per_face: Vec<_> = faces + .iter() + .map(|id| (Orbit2::new(&map, OrbitPolicy::Face, *id as DartIdentifier).count() - 3) * 2) + .collect(); + let n_tot: usize = n_darts_per_face.iter().sum(); + let tmp = map.add_free_darts(n_tot) as usize; + // the prefix sum gives an offset that corresponds to the starting index of each slice, minus + // the location of the allocated dart block (given by `tmp`) + // end of the slice is deduced using these values and the number of darts the current seg needs + let prefix_sum = n_darts_per_face.iter().scan(0, |state, &n_d| { + *state += n_d; + Some(*state - n_d) // we want an offset, not the actual sum + }); + #[allow(clippy::cast_possible_truncation)] + let dart_slices: Vec> = n_darts_per_face + .iter() + .zip(prefix_sum) + .map(|(n_d, start)| { + ((tmp + start) as DartIdentifier..(tmp + start + n_d) as DartIdentifier) + .collect::>() + }) + .collect(); + + for (face_id, new_darts) in faces.iter().zip(dart_slices.iter()) { + fan_cell(&mut map, *face_id, new_darts)? + } + + Ok(()) +} + +fn earclip_bench() -> Result<(), TriangulateError> { + let mut map: CMap2 = CMapBuilder::default().vtk_file(PATH).build().unwrap(); + + // prealloc darts + let faces = map.fetch_faces().identifiers.clone(); + let n_darts_per_face: Vec<_> = faces + .iter() + .map(|id| (Orbit2::new(&map, OrbitPolicy::Face, *id as DartIdentifier).count() - 3) * 2) + .collect(); + let n_tot: usize = n_darts_per_face.iter().sum(); + let tmp = map.add_free_darts(n_tot) as usize; + // the prefix sum gives an offset that corresponds to the starting index of each slice, minus + // the location of the allocated dart block (given by `tmp`) + // end of the slice is deduced using these values and the number of darts the current seg needs + let prefix_sum = n_darts_per_face.iter().scan(0, |state, &n_d| { + *state += n_d; + Some(*state - n_d) // we want an offset, not the actual sum + }); + #[allow(clippy::cast_possible_truncation)] + let dart_slices: Vec> = n_darts_per_face + .iter() + .zip(prefix_sum) + .map(|(n_d, start)| { + ((tmp + start) as DartIdentifier..(tmp + start + n_d) as DartIdentifier) + .collect::>() + }) + .collect(); + + for (face_id, new_darts) in faces.iter().zip(dart_slices.iter()) { + earclip_cell(&mut map, *face_id, new_darts)? + } + + Ok(()) +} + +pub fn criterion_benchmark(c: &mut Criterion) { + let mut group = c.benchmark_group("triangulation"); + + group.bench_function("fan", |b| b.iter(|| black_box(fan_bench()))); + group.bench_function("earclip", |b| b.iter(|| black_box(earclip_bench()))); + group.finish(); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/examples/quads.vtk b/examples/quads.vtk new file mode 100644 index 000000000..183b48836 --- /dev/null +++ b/examples/quads.vtk @@ -0,0 +1,484 @@ +# vtk DataFile Version 2.0 +untitled, Created by Gmsh 4.12.2 +ASCII +DATASET UNSTRUCTURED_GRID +POINTS 131 double +10 0 0 +-2.5 1 0 +-2.5 -1 0 +2.5 -1 0 +2.5 1 0 +9.629172873477993 2.697967711570246 0 +8.544194045464881 5.195839500354344 0 +6.82553143218653 7.30835964278125 0 +4.600650377311505 8.878852184023762 0 +2.03456013052632 9.790840876823232 0 +-0.6824241336467243 9.976687691905392 0 +-3.348796121709874 9.4226092211882 0 +-5.766803221148685 8.169698930104412 0 +-7.75711290704421 6.310879443260513 0 +-9.172113015054537 3.984010898462398 0 +-9.906859460363309 1.361666490962449 0 +-9.906859460363307 -1.361666490962477 0 +-9.172113015054526 -3.984010898462424 0 +-7.757112907044192 -6.310879443260536 0 +-5.766803221148665 -8.169698930104424 0 +-3.348796121709856 -9.422609221188207 0 +-0.6824241336467046 -9.976687691905394 0 +2.034560130526342 -9.790840876823228 0 +4.600650377311524 -8.878852184023751 0 +6.825531432186548 -7.308359642781234 0 +8.544194045464886 -5.195839500354335 0 +9.629172873477994 -2.697967711570235 0 +-2.5 0 0 +-1.5 -1 0 +-0.5 -1 0 +0.5 -1 0 +1.5 -1 0 +2.5 0 0 +1.5 1 0 +0.5 1 0 +-0.5 1 0 +-1.5 1 0 +0.3728048208646338 2.066659968480072 0 +0.1261220767271206 -1.80269887495897 0 +-2.354359258840281 2.238819067163469 0 +-1.735605398554745 -2.129363293353606 0 +2.131407021858652 -2.264332048800795 0 +2.245202786976523 2.04812311538078 0 +3.339133794012826 -0.3227697753464903 0 +-3.487174087216339 -0.4217907714287415 0 +-3.609396535925779 1.854694978900065 0 +3.14936304600271 2.123521734842096 0 +3.132451388079787 3.278731208864496 0 +-2.800029761666594 3.224162729360289 0 +1.139474314084282 -7.754165529255421 0 +4.442473481921682 3.436870472115175 0 +4.450666179079271 1.831315724103073 0 +-4.066187368338756 3.082557541525211 0 +-4.797942682489611 1.748060992194018 0 +-3.142267225050228 -6.564381829574472 0 +-3.177405178259593 -1.675003475410282 0 +-4.624134383560666 -0.8047756426701418 0 +-4.272855199206196 -2.124601548826043 0 +3.209205182212934 -1.825320696034343 0 +2.727288115043585 -3.495759716004268 0 +4.390549777054545 -1.13446687550975 0 +0.7503656262682972 7.405747356633579 0 +2.987793712797877 4.610490851681655 0 +-3.748502607522179 4.660738357374049 0 +-1.940715106889463 4.368476914771648 0 +1.719168613579789 4.346211260209105 0 +-5.893680733790123 -1.023500353151857 0 +5.779662860459534 -5.525913512893179 0 +-5.545630840148739 3.420488710580971 0 +-6.343833963250405 1.929811807328918 0 +-1.728580055144047 6.166873014886744 0 +5.828354531994154 3.622101278526697 0 +5.962938202760824 2.135946586939129 0 +-2.44626732271333 -3.140852042076971 0 +-1.153487094567841 -3.143383068545411 0 +-3.973738260948044 -4.007791046636162 0 +3.987945793615225 -2.821804563768411 0 +3.2487671594423 -4.485619107415586 0 +1.33019595015068 -4.03718861635856 0 +4.889796218456202 0.3304749433332382 0 +6.239178801965752 -1.735132968206066 0 +2.822950923175244 6.089231964057689 0 +4.31912683806445 6.313195009276646 0 +-3.521094366647774 6.228385905760486 0 +-1.891897565297533 -4.776166636048858 0 +-0.6285058873813254 -4.248701795209271 0 +0.159928267639019 -3.720111589864945 0 +2.631312349868499 -5.732922314368036 0 +4.326529765073314 -6.20923425203184 0 +-0.4590908919849294 6.111420464025108 0 +3.672755044829858 0.6060181308488961 0 +5.831356638379467 5.370383869461748 0 +1.04996938015695 -2.756249046230249 0 +-1.112200888704094 3.113897586221583 0 +-0.1482957607586822 5.015555403746275 0 +7.180601316313277 3.079618737156363 0 +-6.605662432144488 -4.722750450398384 0 +-1.027591087652993 -1.770429033190804 0 +0.9586804356967136 -1.713940359891295 0 +-0.908730117283995 1.871736782152127 0 +1.394731777929891 1.975692211016491 0 +-3.586944531951742 0.7574565181217254 0 +-4.784037919079966 0.5235448467619824 0 +-7.578750083772327 0.4148881974788532 0 +-0.8386579291269731 -8.111242683697167 0 +8.002925723011359 -1.000666923470346 0 +6.949592254844632 -3.806334370721589 0 +-4.905384419471186 -5.890849143613694 0 +5.340731739126521 -3.223294242778745 0 +-7.635304707771384 -1.599844339814415 0 +-7.735208762465506 3.025224845965902 0 +-1.256439551486992 7.8525240888966 0 +-6.675669616721741 4.288575494602214 0 +0.3756602107951903 3.493857036445223 0 +8.122195777037394 1.712448317714107 0 +0.4437538870655245 -5.739984561156819 0 +2.63498806103219 7.307474537080323 0 +-6.023993345970753 0.2751699943558172 0 +-3.22753156958497 7.786702858425569 0 +-5.263173327675425 4.789805426807984 0 +2.616442896795175 -7.38557710821688 0 +4.268506747134915 4.768523628130585 0 +1.865098427569817 3.006364432165414 0 +-1.389138994198317 -6.411359849056756 0 +6.604450258788559 0.475876678401435 0 +-5.805397687117566 -2.89580803561103 0 +-2.9730997055837 -5.126812479014085 0 +1.319218930313794 5.60306230223949 0 +-5.297852128253735 6.158119552007297 0 +4.436954388894863 -4.427632416843581 0 +-0.15116034247185 -2.634398410235008 0 + +CELLS 172 736 +1 0 +1 1 +1 2 +1 3 +1 4 +2 0 5 +2 5 6 +2 6 7 +2 7 8 +2 8 9 +2 9 10 +2 10 11 +2 11 12 +2 12 13 +2 13 14 +2 14 15 +2 15 16 +2 16 17 +2 17 18 +2 18 19 +2 19 20 +2 20 21 +2 21 22 +2 22 23 +2 23 24 +2 24 25 +2 25 26 +2 26 0 +2 1 27 +2 27 2 +2 2 28 +2 28 29 +2 29 30 +2 30 31 +2 31 3 +2 3 32 +2 32 4 +2 4 33 +2 33 34 +2 34 35 +2 35 36 +2 36 1 +3 80 108 106 +3 73 74 40 +3 51 90 79 +3 15 110 14 +3 12 118 11 +3 113 122 65 +3 58 76 60 +3 18 96 17 +3 28 97 29 +3 35 99 36 +3 7 91 6 +3 30 98 31 +3 103 117 69 +3 109 125 66 +3 0 105 26 +3 3 43 32 +3 26 106 25 +3 21 104 20 +3 17 109 16 +3 52 53 45 +3 105 124 80 +3 41 98 92 +3 39 99 93 +3 5 114 0 +3 82 121 91 +3 78 115 87 +3 4 46 42 +3 13 128 12 +3 72 95 71 +3 41 58 3 +3 68 119 112 +3 76 129 108 +3 67 129 88 +3 57 125 75 +3 42 122 100 +4 66 125 57 56 +4 81 127 65 62 +4 75 126 84 73 +4 74 73 84 85 +4 49 120 87 115 +4 79 60 80 124 +4 75 125 96 107 +4 10 111 61 9 +4 102 56 44 101 +4 97 130 38 29 +4 101 44 27 1 +4 88 23 24 67 +4 79 124 72 51 +4 82 91 7 8 +4 77 129 76 59 +4 89 70 64 94 +4 94 64 93 113 +4 119 68 52 63 +4 86 130 74 85 +4 86 85 115 78 +4 55 44 56 57 +4 42 100 33 4 +4 16 109 103 15 +4 54 107 19 20 +4 40 97 28 2 +4 100 37 34 33 +4 70 83 63 64 +4 108 80 60 76 +4 50 121 62 47 +4 47 122 42 46 +4 49 104 21 22 +4 48 39 93 64 +4 110 69 68 112 +4 49 115 123 104 +4 81 116 61 127 +4 71 50 51 72 +4 59 41 92 78 +4 84 126 54 123 +4 93 99 37 113 +4 31 98 41 3 +4 65 127 94 113 +4 11 118 111 10 +4 114 124 105 0 +4 37 99 35 34 +4 106 26 105 80 +4 69 117 102 53 +4 100 122 113 37 +4 45 39 48 52 +4 74 130 97 40 +4 32 43 90 4 +4 70 111 118 83 +4 58 60 43 3 +4 58 41 59 76 +4 72 124 114 95 +4 36 99 39 1 +4 90 51 46 4 +4 111 70 89 61 +4 92 98 38 130 +4 38 98 30 29 +4 63 52 48 64 +4 108 129 67 106 +4 96 125 109 17 +4 112 13 14 110 +4 53 102 101 45 +4 44 55 2 27 +4 117 66 56 102 +4 53 52 68 69 +4 118 12 128 83 +4 78 87 77 59 +4 110 15 103 69 +4 40 2 55 73 +4 62 121 82 81 +4 95 6 91 71 +4 88 129 77 87 +4 107 96 18 19 +4 86 78 92 130 +4 128 13 112 119 +4 50 47 46 51 +4 65 122 47 62 +4 95 114 5 6 +4 88 87 120 23 +4 57 75 73 55 +4 119 63 83 128 +4 79 90 43 60 +4 25 106 67 24 +4 103 109 66 117 +4 1 39 45 101 +4 104 123 54 20 +4 121 50 71 91 +4 116 81 82 8 +4 127 61 89 94 +4 116 8 9 61 +4 123 115 85 84 +4 22 23 120 49 +4 107 54 126 75 + +CELL_TYPES 172 +1 +1 +1 +1 +1 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +3 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +5 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9 +9