Skip to content

Commit

Permalink
geo2d/delaunay
Browse files Browse the repository at this point in the history
  • Loading branch information
teapotd committed Apr 4, 2024
1 parent 3f3b8d0 commit 72234df
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/geo2d/delaunay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "../template.h"
#include "vector.h"
#include "../geo3d/convex_hull.h"

// Delaunay triangulation using 3D convex hull.
// Faces are in CCW order. Doesn't work if
// all points are colinear or on a same circle!
// time and memory: O(n log n)
//! Source: https://github.com/kth-competitive-programming/kactl/blob/main/content/geometry/DelaunayTriangulation.h
vector<Triple> delaunay(vector<vec>& p) {
assert(sz(p) >= 3);
if (sz(p) == 3) {
int d = ((p[1]-p[0]).cross(p[2]-p[0]) < 0);
return {{0, 1+d, 2-d}};
}

vector<vec3> p3;
each(e, p) p3.pb({e.x, e.y, e.len2()});

auto hull = convexHull(p3);
erase_if(hull, [&](auto& t) {
vec a = p[t[0]], b = p[t[1]], c = p[t[2]];
swap(t[1], t[2]);
return (b-a).cross(c-a) > 0;
});
return hull;
}
19 changes: 19 additions & 0 deletions tests/own/geo2d/delaunay.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include "../../../src/geo2d/delaunay.h"
#include "common.hpp"

void deterministic() {
}

void fuzz() {
}

void benchmark() {
auto points = randVecsFromSquare(1e5, 1e4-10, 1e4);

measure("delaunay N=1e5", 1, [&] {
auto faces = delaunay(points);
deb(sz(faces));
consume(&faces);
});
}
2 changes: 2 additions & 0 deletions tests/own/geo2d/float/delaunay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define FLOATING_POINT_GEOMETRY 1
#include "../delaunay.hpp"
2 changes: 2 additions & 0 deletions tests/own/geo2d/int/delaunay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define FLOATING_POINT_GEOMETRY 0
#include "../delaunay.hpp"

0 comments on commit 72234df

Please sign in to comment.