delaunay.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #ifndef __DELAUNAY_H
  2. #define __DELAUNAY_H
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6. #include <algorithm>
  7. #include <utility>
  8. #include <memory>
  9. #include <array>
  10. #include <math.h>
  11. namespace delaunay {
  12. template <typename T>
  13. struct point {
  14. T x;
  15. T y;
  16. point(T _x = 0, T _y = 0) : x(_x), y(_y) {}
  17. template <typename S>
  18. point(const point<S>& a) : x(a.x), y(a.y) {}
  19. template <typename S>
  20. point(const std::pair<S, S>& _pt) : x(_pt.first), y(_pt.second) {}
  21. template <typename S>
  22. point<T> operator+(const point<S>& a) const {
  23. return point<T>(x + a.x, y + a.y);
  24. }
  25. template <typename S>
  26. point<T> operator-(const point<S>& a) const {
  27. return point<T>(x - a.x, y - a.y);
  28. }
  29. template <typename POD>
  30. point<T> operator*(POD d) const {
  31. return point<T>(x * d, y * d);
  32. }
  33. template <typename POD>
  34. point<T> operator/(POD d) const {
  35. return point<T>(x / d, y / d);
  36. }
  37. bool operator<(const point<T>& a) const {
  38. if (x < a.x) return true;
  39. if (x == a.x && y < a.y) return true;
  40. return false;
  41. }
  42. bool operator==(const point<T>& a) const {
  43. return (x == a.x && y == a.y);
  44. }
  45. T dist2() const {
  46. return x*x + y*y;
  47. }
  48. template <typename S>
  49. T dist2(const point<S>& a) const {
  50. return (*this - a).dist2();
  51. }
  52. double dist() const {
  53. return ::sqrt(dist2());
  54. }
  55. template <typename S>
  56. double dist(const point<S>& a) const {
  57. return ::sqrt(dist2(a));
  58. }
  59. };
  60. typedef point<int> pt;
  61. struct circle {
  62. point<double> center;
  63. double r2;
  64. double r;
  65. circle() : r2(0), r(0) {}
  66. template <typename S, typename T>
  67. circle(const point<S>& _c, T _r2) : center(_c), r2(_r2), r(::sqrt(r2)) {}
  68. template <typename S, typename T>
  69. circle(const point<S>& _c, T _r, bool) : center(_c), r2(_r*_r), r(_r) {}
  70. bool null() const {
  71. return (r2 == 0);
  72. }
  73. template <typename T>
  74. bool has(const point<T>& p) const {
  75. return (center.dist2(p) <= r2);
  76. }
  77. bool has(const circle& c) const {
  78. return (center.dist(c.center) + c.r <= r);
  79. }
  80. bool intersects(const circle& c) const {
  81. return (center.dist(c.center) - c.r <= r);
  82. }
  83. bool operator<(const circle& c) const {
  84. if (center < c.center) return true;
  85. if (center == c.center && r2 < c.r2) return true;
  86. return false;
  87. }
  88. };
  89. struct tri {
  90. pt a;
  91. pt b;
  92. pt c;
  93. circle circ;
  94. tri() {}
  95. tri(const pt& _a, const pt& _b, const pt& _c) : a(_a), b(_b), c(_c) { circumscribe(); }
  96. void circumscribe() {
  97. int da = a.dist2();
  98. int db = b.dist2();
  99. int dc = c.dist2();
  100. pt a_b = a - b;
  101. pt b_c = b - c;
  102. pt c_a = c - a;
  103. // Small q or large radius means a degenerate non-triangle. (Three points that are almost collinear.)
  104. int q = (a.x * b_c.y + b.x * c_a.y + c.x * a_b.y);
  105. if (q == 0) {
  106. circ = circle(pt(0, 0), 0);
  107. return;
  108. }
  109. double D = 0.5 / q;
  110. point<double> center( D * (da * b_c.y + db * c_a.y + dc * a_b.y),
  111. -D * (da * b_c.x + db * c_a.x + dc * a_b.x));
  112. double radius = std::max(center.dist2(a), std::max(center.dist2(b), center.dist2(c)));
  113. circ = circle(center, radius);
  114. }
  115. bool operator<(const tri& t) const {
  116. if (a < t.a) return true;
  117. if (a == t.a && b < t.b) return true;
  118. if (a == t.a && b == t.b && c < t.c) return true;
  119. return false;
  120. }
  121. bool null() const {
  122. return circ.null();
  123. }
  124. };
  125. struct edge {
  126. pt a;
  127. pt b;
  128. edge() {}
  129. edge(const pt& _a, const pt& _b) {
  130. if (_a < _b) {
  131. a = _a;
  132. b = _b;
  133. } else {
  134. a = _b;
  135. b = _a;
  136. }
  137. };
  138. bool operator<(const edge& o) const {
  139. if (a < o.a) return true;
  140. if (a == o.a && b < o.b) return true;
  141. return false;
  142. }
  143. };
  144. struct flower_t;
  145. typedef std::unique_ptr<flower_t> flower;
  146. //
  147. // Circle-based spatial indexing tree.
  148. //
  149. // Based on this picture: http://www2.stetson.edu/~efriedma/circovcir/7.gif
  150. //
  151. // A circle of radius R can be completely covered by 7 circles of radii R/2,
  152. // one circle in the center and 6 centered on the midpoints of edges of the hexagon
  153. // inscribed in the larger (radius R) circle.
  154. //
  155. struct flower_t {
  156. std::vector<tri> data;
  157. std::array<flower, 7> bud;
  158. static circle petal(const circle& super, int i) {
  159. static double sqrt3_2 = 0.8660254037844386;
  160. static double centers[7][2] = {
  161. { 0, 0 },
  162. { sqrt3_2, 0 },
  163. { sqrt3_2 / 2, 0.75 },
  164. { sqrt3_2 / 2, -0.75 },
  165. { -sqrt3_2, 0 },
  166. { -sqrt3_2 / 2, 0.75 },
  167. { -sqrt3_2 / 2, -0.75 }
  168. };
  169. return circle(point<double>(super.center.x + centers[i][0] * super.r,
  170. super.center.y + centers[i][1] * super.r),
  171. super.r / 2, true);
  172. }
  173. static void insert(flower& root, const circle& super, const tri& t) {
  174. if (!root)
  175. root = flower(new flower_t);
  176. for (int i = 0; i < 7; ++i) {
  177. flower& f = root->bud[i];
  178. circle fc = petal(super, i);
  179. if (fc.has(t.circ)) {
  180. insert(f, fc, t);
  181. return;
  182. }
  183. }
  184. root->data.push_back(t);
  185. }
  186. static void query(const flower& root, const circle& super, const pt& q, std::vector<tri>& ret) {
  187. if (!root)
  188. return;
  189. for (int i = 0; i < 7; ++i) {
  190. const flower& f = root->bud[i];
  191. circle fc = petal(super, i);
  192. if (fc.has(q))
  193. query(f, fc, q, ret);
  194. }
  195. auto i = root->data.begin();
  196. while (i != root->data.end()) {
  197. if (i->circ.has(q)) {
  198. ret.push_back(*i);
  199. i = root->data.erase(i);
  200. } else {
  201. ++i;
  202. }
  203. }
  204. }
  205. static void result(const flower& root, std::vector<tri>& ret) {
  206. if (!root)
  207. return;
  208. for (int i = 0; i < 7; ++i) {
  209. const flower& f = root->bud[i];
  210. result(f, ret);
  211. }
  212. ret.insert(ret.end(), root->data.begin(), root->data.end());
  213. }
  214. };
  215. struct Triangulation {
  216. std::map< pt, std::map<pt,unsigned int> > res;
  217. void clear() {
  218. res.clear();
  219. }
  220. void init(unsigned int w, unsigned int h, const std::vector<pt>& points, bool do_print = false) {
  221. res.clear();
  222. if (points.empty())
  223. return;
  224. flower tree(new flower_t);
  225. pt fake1(0, 0);
  226. pt fake2(w-1, 0);
  227. pt fake3(w-1, h-1);
  228. pt fake4(0, h-1);
  229. circle super(point<double>(w / 2, h / 2), fake3.dist2() + 1);
  230. flower_t::insert(tree, super, tri(fake1, fake2, fake3));
  231. flower_t::insert(tree, super, tri(fake1, fake3, fake4));
  232. for (const pt& p : points) {
  233. std::vector<tri> bad;
  234. flower_t::query(tree, super, p, bad);
  235. while (1) {
  236. std::map<edge, size_t> loose_edges;
  237. for (const tri& t : bad) {
  238. loose_edges[edge(t.a, t.b)]++;
  239. loose_edges[edge(t.b, t.c)]++;
  240. loose_edges[edge(t.c, t.a)]++;
  241. }
  242. std::map< pt, std::set<pt> > hull;
  243. for (const auto& v : loose_edges) {
  244. if (v.second > 2)
  245. throw std::runtime_error("Sanity error: not a mesh.");
  246. // A double edge means this is an internal edge, so we delete it.
  247. if (v.second == 2)
  248. continue;
  249. hull[v.first.a].insert(v.first.b);
  250. hull[v.first.b].insert(v.first.a);
  251. }
  252. std::set<edge> hull_edges;
  253. bool try_again = false;
  254. for (const auto& v : hull) {
  255. if (v.second.size() != 2) {
  256. // We found a set of triangles that isn't a triangulation.
  257. // Oops, theoretically this should never happen, but it does.
  258. // (Rounding errors and such.)
  259. // Remove the triangles that are the furthest away,
  260. // hopefully this will get rid of non-connected triangles.
  261. // TODO.
  262. unsigned int minr2 = 0;
  263. std::set<tri> to_remove;
  264. for (const tri& t : bad) {
  265. unsigned int d = t.circ.r2 - t.circ.center.dist2(p);
  266. if (to_remove.empty() || d <= minr2) {
  267. if (d != minr2) {
  268. to_remove.clear();
  269. }
  270. to_remove.insert(t);
  271. minr2 = d;
  272. }
  273. }
  274. for (auto i = bad.begin(); i != bad.end();) {
  275. if (to_remove.count(*i) != 0) {
  276. i = bad.erase(i);
  277. } else {
  278. ++i;
  279. }
  280. }
  281. try_again = true;
  282. break;
  283. }
  284. const pt& a = v.first;
  285. const pt& b = *(v.second.begin());
  286. const pt& c = *(++v.second.begin());
  287. hull_edges.insert(edge(a, b));
  288. hull_edges.insert(edge(a, c));
  289. }
  290. if (try_again)
  291. continue;
  292. // hull_edges is only needed to make the inserted triangles unique.
  293. for (const edge& e : hull_edges) {
  294. tri tmp(e.a, e.b, p);
  295. if (!tmp.null())
  296. flower_t::insert(tree, super, tmp);
  297. }
  298. break;
  299. }
  300. if (do_print) {
  301. static unsigned int nn = 0;
  302. ++nn;
  303. if (nn != points.size())
  304. continue;
  305. std::stringstream ss;
  306. ss << "_tmp." << nn;
  307. std::ofstream of(ss.str().c_str());
  308. std::vector<tri> finaltri;
  309. flower_t::result(tree, finaltri);
  310. for (const tri& t : finaltri) {
  311. of << t.a.x << " " << t.a.y << std::endl;
  312. of << t.b.x << " " << t.b.y << std::endl;
  313. of << t.c.x << " " << t.c.y << std::endl;
  314. of << t.a.x << " " << t.a.y << std::endl << std::endl;
  315. }
  316. }
  317. }
  318. std::vector<tri> finaltri;
  319. flower_t::result(tree, finaltri);
  320. for (const auto& t : finaltri) {
  321. auto& ta = res[t.a];
  322. auto& tb = res[t.b];
  323. auto& tc = res[t.c];
  324. unsigned int a_b = t.a.dist2(t.b);
  325. unsigned int b_c = t.b.dist2(t.c);
  326. unsigned int c_a = t.c.dist2(t.a);
  327. ta[t.b] = a_b;
  328. ta[t.c] = c_a;
  329. tb[t.a] = a_b;
  330. tb[t.c] = b_c;
  331. tc[t.a] = c_a;
  332. tc[t.b] = b_c;
  333. }
  334. }
  335. struct neighbor_t {
  336. unsigned int dist2;
  337. unsigned int x;
  338. unsigned int y;
  339. neighbor_t(unsigned int d2 = 0, unsigned int _x = 0, unsigned int _y = 0) : dist2(d2), x(_x), y(_y) {}
  340. bool operator<(const neighbor_t& a) const {
  341. if (dist2 < a.dist2) return true;
  342. if (dist2 == a.dist2 && x < a.x) return true;
  343. if (dist2 == a.dist2 && x < a.x && y < a.y) return true;
  344. return false;
  345. }
  346. };
  347. std::set<neighbor_t> get(unsigned int x, unsigned int y, unsigned int d2max, bool& full_empty) const {
  348. std::set<neighbor_t> ret;
  349. pt p0(x, y);
  350. const auto i = res.find(p0);
  351. if (i == res.end()) {
  352. full_empty = true;
  353. return ret;
  354. }
  355. full_empty = false;
  356. for (const auto& p_d : i->second) {
  357. if (p_d.second < d2max) {
  358. ret.insert(neighbor_t(p_d.second, p_d.first.x, p_d.first.y));
  359. }
  360. }
  361. return ret;
  362. }
  363. };
  364. }
  365. #endif