par_triangle.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. // TRIANGLE :: https://github.com/prideout/par
  2. // Consumes polygons, produces constrained Delaunay triangulations, etc.
  3. //
  4. // This implements "An improved incremental algorithm for constructing
  5. // restricted Delaunay triangulations" by Marc Vigo Anglada. We might
  6. // eventually replace this with Shewchuk's randomized method, which is
  7. // probably much faster.
  8. //
  9. // Here's an example that tessellates a pentagon with a triangular hole:
  10. //
  11. // const uint16_t lengths[2] = {5, 3};
  12. // const float points[] = {
  13. // 0, 0, 6, 0, 6, 4, 3, 7, 0, 4, // CCW pentagon
  14. // 3, 3, 4, 1, 2, 1, // CW triangle
  15. // };
  16. // par_triangle_path* path = par_triangle_path_create(lengths, 2, points, 0);
  17. // par_triangle_mesh* mesh = par_triangle_mesh_create_cdt(path);
  18. // ...draw mesh here...
  19. // par_triangle_mesh_free(mesh);
  20. // par_triangle_path_free(path);
  21. //
  22. // TODO items
  23. // --------------
  24. // (0) CDT via Anglada's algorithm
  25. // https://www.dropbox.com/s/nkqsi1u7ey1bec7/Incremental_CDT.pdf
  26. // (1) Refinement via Chew's Second Algorithm
  27. // https://en.wikipedia.org/wiki/Chew%27s_second_algorithm
  28. // (2) Extrusion (and path reversal)
  29. // useful for msquares coastline (and a lighttransport/nanort demo)
  30. // (3) Ear clipping
  31. // https://github.com/mapbox/earcut.hpp (uses boost)
  32. // https://github.com/prideout/polygon.js
  33. // (4) Improve CDT generation via Shewchuk's randomized method
  34. // Fast Segment Insertion and Incremental Construction of
  35. // Constrained Delaunay Triangulations
  36. //
  37. // The MIT License
  38. // Copyright (c) 2016 Philip Rideout
  39. #ifndef PAR_TRIANGLE_H
  40. #define PAR_TRIANGLE_H
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. #include <stdint.h>
  45. // -----------------------------------------------------------------------------
  46. // BEGIN PUBLIC API
  47. // -----------------------------------------------------------------------------
  48. // Planar straight-line graph composed of one or more "loops" (polygons)
  49. // whereby counterclockwise loops are solid and clockwise loops are holes.
  50. // When serializing to SVG, all loops can be aggregated into a single <path>,
  51. // provided they each terminate with "Z" and use the default fill rule.
  52. typedef struct {
  53. float* points; // list of XY vertex coordinates
  54. int npoints; // number of 2-tuples in "points"
  55. float** loops; // list of pointers to the start of each loop
  56. uint16_t* lengths; // list of loop lengths
  57. int nloops; // number of loops
  58. } par_triangle_path;
  59. typedef struct par_triangle_mesh_s {
  60. float const* points; // Flat list of 2-tuples (X Y X Y...)
  61. int npoints; // Number of points
  62. uint16_t const* triangles; // Flat list of 3-tuples (I J K I J K...)
  63. int ntriangles; // Number of triangles
  64. } par_triangle_mesh;
  65. // Create a planar straight-line graph or resize an existing graph. When
  66. // creating a brand new graph, clients should pass null to "old". The lengths
  67. // argument is a client-owned array that specifies the desired number of
  68. // two-tuples in each loop. The provided points array can be null, in which
  69. // case the client is expected to populate the coordinates after construction.
  70. par_triangle_path* par_triangle_path_create(uint16_t const* lengths,
  71. int nloops, float const* points, par_triangle_path* old);
  72. // Free all memory associated with a planar straight-line graph.
  73. void par_triangle_path_free(par_triangle_path*);
  74. // Triangulate the given polygon set using constrained Delaunay tessellation.
  75. par_triangle_mesh* par_triangle_mesh_create_cdt(par_triangle_path const*);
  76. // Free all memory associated with a 2D triangle mesh.
  77. void par_triangle_mesh_free(par_triangle_mesh*);
  78. // Scale, then translate, every point in the given path.
  79. void par_triangle_path_transform(par_triangle_path* path, float sx, float sy,
  80. float tx, float ty);
  81. // Find the triangle that contains the given point, otherwise return -1.
  82. int par_triangle_mesh_find_triangle(par_triangle_mesh const* mesh, float x,
  83. float y);
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. // -----------------------------------------------------------------------------
  88. // END PUBLIC API
  89. // -----------------------------------------------------------------------------
  90. #ifdef PAR_TRIANGLE_IMPLEMENTATION
  91. #include <stdlib.h>
  92. #include <memory.h>
  93. #include <assert.h>
  94. #include <float.h>
  95. #include <stdbool.h>
  96. #ifndef PAR_PI
  97. #define PAR_PI (3.14159265359)
  98. #define PAR_MIN(a, b) (a > b ? b : a)
  99. #define PAR_MAX(a, b) (a > b ? a : b)
  100. #define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
  101. #define PAR_SWAP(T, A, B) { T tmp = B; B = A; A = tmp; }
  102. #define PAR_SQR(a) ((a) * (a))
  103. #endif
  104. #ifndef PAR_MALLOC
  105. #define PAR_MALLOC(T, N) ((T*) malloc(N * sizeof(T)))
  106. #define PAR_CALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
  107. #define PAR_REALLOC(T, BUF, N) ((T*) realloc(BUF, sizeof(T) * (N)))
  108. #define PAR_FREE(BUF) free(BUF)
  109. #endif
  110. #ifndef pa_free
  111. #define pa_free(a) ((a) ? PAR_FREE(pa___raw(a)), 0 : 0)
  112. #define pa_push(a, v) (pa___maybegrow(a, 1), (a)[pa___n(a)++] = (v))
  113. #define pa_pop(a) ((a)[--pa___n(a)])
  114. #define pa_count(a) ((a) ? pa___n(a) : 0)
  115. #define pa_add(a, n) (pa___maybegrow(a, n), pa___n(a) += (n))
  116. #define pa_last(a) ((a)[pa___n(a) - 1])
  117. #define pa_end(a) (a + pa_count(a))
  118. #define pa_clear(arr) if (arr) pa___n(arr) = 0
  119. #define pa___raw(a) ((int*) (a) -2)
  120. #define pa___m(a) pa___raw(a)[0]
  121. #define pa___n(a) pa___raw(a)[1]
  122. #define pa___needgrow(a, n) ((a) == 0 || pa___n(a) + (n) >= pa___m(a))
  123. #define pa___maybegrow(a, n) (pa___needgrow(a, (n)) ? pa___grow(a, n) : 0)
  124. #define pa___grow(a, n) (*((void**)& (a)) = pa___growf((void*) (a), (n), \
  125. sizeof(*(a))))
  126. static void* pa___growf(void* arr, int increment, int itemsize)
  127. {
  128. int dbl_cur = arr ? 2 * pa___m(arr) : 0;
  129. int min_needed = pa_count(arr) + increment;
  130. int m = dbl_cur > min_needed ? dbl_cur : min_needed;
  131. int* p = PAR_REALLOC(int, arr ? pa___raw(arr) : 0,
  132. itemsize * m / sizeof(int) + 2);
  133. if (p) {
  134. if (!arr) {
  135. p[1] = 0;
  136. }
  137. p[0] = m;
  138. return p + 2;
  139. }
  140. return (void*) (2 * sizeof(int));
  141. }
  142. #endif
  143. par_triangle_path* par_triangle_path_create(uint16_t const* lengths,
  144. int nloops, float const* points, par_triangle_path* old)
  145. {
  146. par_triangle_path* path = old;
  147. if (!path) {
  148. path = PAR_CALLOC(par_triangle_path, 1);
  149. }
  150. path->nloops = nloops;
  151. path->lengths = PAR_REALLOC(uint16_t, path->lengths, nloops);
  152. memcpy(path->lengths, lengths, sizeof(uint16_t) * nloops);
  153. path->npoints = 0;
  154. for (int i = 0; i < nloops; i++) {
  155. path->npoints += lengths[i];
  156. }
  157. path->points = PAR_REALLOC(float, path->points, path->npoints * 2);
  158. if (points) {
  159. memcpy(path->points, points, path->npoints * 2 * sizeof(float));
  160. }
  161. path->loops = PAR_REALLOC(float*, path->loops, nloops);
  162. float* pt = path->points;
  163. for (int i = 0; i < nloops; i++) {
  164. path->loops[i] = pt;
  165. pt += 2 * lengths[i];
  166. }
  167. return path;
  168. }
  169. void par_triangle_path_free(par_triangle_path* path)
  170. {
  171. PAR_FREE(path->points);
  172. PAR_FREE(path->loops);
  173. PAR_FREE(path->lengths);
  174. PAR_FREE(path);
  175. }
  176. typedef struct par_triangle__vert_t {
  177. float x;
  178. float y;
  179. struct par_triangle__edge_t* outgoing;
  180. } par_triangle__vert;
  181. typedef struct par_triangle__face_t {
  182. struct par_triangle__edge_t* edge;
  183. } par_triangle__face;
  184. typedef struct par_triangle__edge_t {
  185. struct par_triangle__vert_t* end;
  186. struct par_triangle__face_t* face;
  187. struct par_triangle__edge_t* next;
  188. struct par_triangle__edge_t* pair;
  189. bool fixed;
  190. } par_triangle__edge;
  191. typedef struct {
  192. // Public data.
  193. float* points;
  194. int npoints;
  195. uint16_t* triangles;
  196. int ntriangles;
  197. // Private data; includes a half-edge data structure.
  198. par_triangle__vert* verts;
  199. par_triangle__face* faces;
  200. par_triangle__edge* edges;
  201. // Scratch space for the addpt algorithm to alleviate allocations.
  202. int* stack;
  203. } par_triangle__mesh;
  204. // static float par_triangle__dot(float x1, float y1, float x2, float y2)
  205. // {
  206. // return x1 * x2 + y1 * y2;
  207. // }
  208. static float par_triangle__cross(float x1, float y1, float x2, float y2)
  209. {
  210. return x1 * y2 - y1 * x2;
  211. }
  212. // Assumes CCW ordering.
  213. static bool par_triangle__contains(float px, float py, float x1, float y1,
  214. float x2, float y2, float x3, float y3)
  215. {
  216. float vx1 = x2 - x1, vy1 = y2 - y1;
  217. float px1 = px - x1, py1 = py - y1;
  218. float vx2 = x3 - x2, vy2 = y3 - y2;
  219. float px2 = px - x2, py2 = py - y2;
  220. float vx3 = x1 - x3, vy3 = y1 - y3;
  221. float px3 = px - x3, py3 = py - y3;
  222. float c1 = par_triangle__cross(px1, py1, vx1, vy1);
  223. float c2 = par_triangle__cross(px2, py2, vx2, vy2);
  224. float c3 = par_triangle__cross(px3, py3, vx3, vy3);
  225. return c1 <= 0 && c2 <= 0 && c3 <= 0;
  226. }
  227. int par_triangle_mesh_find_triangle(par_triangle_mesh const* m, float x,
  228. float y)
  229. {
  230. par_triangle__mesh const* mesh = (par_triangle__mesh const*) m;
  231. int nfaces = pa_count(mesh->faces);
  232. par_triangle__face const* face = mesh->faces;
  233. for (int f = 0; f < nfaces; f++, face++) {
  234. par_triangle__edge const* edge = face->edge;
  235. par_triangle__vert const* a = edge->end;
  236. par_triangle__vert const* b = edge->next->end;
  237. par_triangle__vert const* c = edge->next->next->end;
  238. if (par_triangle__contains(x, y, a->x, a->y, b->x, b->y, c->x, c->y)) {
  239. return f;
  240. }
  241. }
  242. return -1;
  243. }
  244. // Populate a brand new mesh with one triangle that wholly encompasses the unit
  245. // square in [0, +1].
  246. static par_triangle__mesh* par_triangle__mesh_create()
  247. {
  248. par_triangle__mesh* result = PAR_CALLOC(par_triangle__mesh, 1);
  249. pa_add(result->verts, 3);
  250. pa_add(result->faces, 1);
  251. pa_add(result->edges, 3);
  252. const par_triangle__vert bigtri[] = {
  253. {-1.0, -1.0, 0},
  254. {+2.0, -1.0, 0},
  255. {+0.5, +3.0, 0}
  256. };
  257. memcpy(result->verts, bigtri, sizeof(bigtri));
  258. result->edges[0].end = result->verts + 1;
  259. result->edges[0].face = result->faces + 0;
  260. result->edges[0].next = result->edges + 1;
  261. result->edges[0].pair = 0;//result->edges + 3;
  262. result->edges[0].fixed = true;
  263. result->edges[1].end = result->verts + 2;
  264. result->edges[1].face = result->faces + 0;
  265. result->edges[1].next = result->edges + 2;
  266. result->edges[1].pair = 0;//result->edges + 4;
  267. result->edges[1].fixed = true;
  268. result->edges[2].end = result->verts + 0;
  269. result->edges[2].face = result->faces + 0;
  270. result->edges[2].next = result->edges + 0;
  271. result->edges[2].pair = 0;//result->edges + 5;
  272. result->edges[2].fixed = true;
  273. result->faces[0].edge = result->edges + 0;
  274. result->verts[0].outgoing = result->edges + 0;
  275. result->verts[1].outgoing = result->edges + 1;
  276. result->verts[2].outgoing = result->edges + 2;
  277. // result->edges[3].end = result->verts + 0;
  278. // result->edges[3].face = 0;
  279. // result->edges[3].next = result->edges + 2;
  280. // result->edges[3].pair = result->edges + 0;
  281. // result->edges[4].end = result->verts + 1;
  282. // result->edges[4].face = 0;
  283. // result->edges[4].next = result->edges + 0;
  284. // result->edges[4].pair = result->edges + 1;
  285. // result->edges[5].end = result->verts + 2;
  286. // result->edges[5].face = 0;
  287. // result->edges[5].next = result->edges + 1;
  288. // result->edges[5].pair = result->edges + 2;
  289. return result;
  290. }
  291. static void par_triangle__mesh_validate(par_triangle__mesh* mesh)
  292. {
  293. int nfaces = mesh->ntriangles = pa_count(mesh->faces);
  294. par_triangle__face const* face = mesh->faces;
  295. for (int f = 0; f < nfaces; f++, face++) {
  296. par_triangle__edge const* e = face->edge;
  297. par_triangle__vert const* a = e->end;
  298. par_triangle__vert const* b = e->next->end;
  299. par_triangle__vert const* c = e->next->next->end;
  300. float ab[2] = {b->x - a->x, b->y - a->y};
  301. float ac[2] = {c->x - a->x, c->y - a->y};
  302. assert(par_triangle__cross(ab[0], ab[1], ac[0], ac[1]) > 0);
  303. assert(!e->pair || e->pair->pair == e);
  304. assert(!e->next->pair || e->next->pair->pair == e->next);
  305. assert(!e->next->next->pair ||
  306. e->next->next->pair->pair == e->next->next);
  307. assert(e->face == face);
  308. assert(e->next->face == face);
  309. assert(e->next->next->face == face);
  310. }
  311. int nedges = pa_count(mesh->edges);
  312. int nborders = 0;
  313. par_triangle__edge const* edge = mesh->edges;
  314. for (int e = 0; e < nedges; e++, edge++) {
  315. if (!edge->pair) {
  316. nborders++;
  317. continue;
  318. }
  319. assert(edge->next->next->end == edge->pair->end);
  320. }
  321. assert(nborders == 3);
  322. }
  323. // Consume the half-edge data structure and produce data for the public fields.
  324. static void par_triangle__mesh_finalize(par_triangle__mesh* mesh)
  325. {
  326. // Produce "points" by consuming XY coordinates from the half-edge mesh.
  327. int nverts = mesh->npoints = pa_count(mesh->verts);
  328. float* point = mesh->points = PAR_MALLOC(float, 2 * nverts);
  329. par_triangle__vert const* vert = mesh->verts;
  330. for (int v = 0; v < nverts; v++, vert++, point += 2) {
  331. point[0] = vert->x;
  332. point[1] = vert->y;
  333. }
  334. // Produce "triangles" by consuming vertex pointers in the half-edge mesh.
  335. int nfaces = mesh->ntriangles = pa_count(mesh->faces);
  336. uint16_t* tri = mesh->triangles = PAR_MALLOC(uint16_t, 3 * nfaces);
  337. par_triangle__face const* face = mesh->faces;
  338. for (int f = 0; f < nfaces; f++, face++, tri += 3) {
  339. par_triangle__edge const* edge = face->edge;
  340. tri[0] = edge->end - mesh->verts;
  341. tri[1] = edge->next->end - mesh->verts;
  342. tri[2] = edge->next->next->end - mesh->verts;
  343. }
  344. }
  345. void par_triangle_path_transform(par_triangle_path* path, float sx, float sy,
  346. float tx, float ty)
  347. {
  348. float* pt = path->points;
  349. for (int p = 0; p < path->npoints; p++, pt++) {
  350. pt[0] = pt[0] * sx + tx;
  351. pt[1] = pt[1] * sy + ty;
  352. }
  353. }
  354. static void par_triangle__mesh_transform(par_triangle__mesh* mesh,
  355. float sx, float sy, float tx, float ty)
  356. {
  357. int nverts = pa_count(mesh->verts);
  358. par_triangle__vert* vert = mesh->verts;
  359. for (int v = 0; v < nverts; v++, vert++) {
  360. vert->x = vert->x * sx + tx;
  361. vert->y = vert->y * sy + ty;
  362. }
  363. }
  364. static void par_triangle__mesh_grow(par_triangle__mesh* mesh, int nverts,
  365. int nedges, int nfaces)
  366. {
  367. // Reallocate verts and repair all pointers to verts.
  368. par_triangle__vert* verts = mesh->verts;
  369. pa_add(mesh->verts, nverts);
  370. if (verts != mesh->verts) {
  371. par_triangle__edge* edge = mesh->edges;
  372. for (int i = 0; i < pa_count(mesh->edges); i++, edge++) {
  373. edge->end = mesh->verts + (edge->end - verts);
  374. }
  375. }
  376. // Reallocate edges and repair all pointers to edges.
  377. par_triangle__edge* edges = mesh->edges;
  378. pa_add(mesh->edges, nedges);
  379. if (edges != mesh->edges) {
  380. par_triangle__edge* edge = mesh->edges;
  381. for (int i = 0; i < pa_count(mesh->edges) - nedges; i++, edge++) {
  382. edge->next = mesh->edges + (edge->next - edges);
  383. if (edge->pair) {
  384. edge->pair = mesh->edges + (edge->pair - edges);
  385. }
  386. }
  387. par_triangle__face* face = mesh->faces;
  388. for (int i = 0; i < pa_count(mesh->faces); i++, face++) {
  389. face->edge = mesh->edges + (face->edge - edges);
  390. }
  391. par_triangle__vert* vert = mesh->verts;
  392. for (int i = 0; i < pa_count(mesh->verts) - nverts; i++, vert++) {
  393. vert->outgoing = mesh->edges + (vert->outgoing - edges);
  394. }
  395. }
  396. // Reallocate faces and repair all pointers to faces.
  397. par_triangle__face* faces = mesh->faces;
  398. pa_add(mesh->faces, nfaces);
  399. if (faces != mesh->faces) {
  400. par_triangle__edge* edge = mesh->edges;
  401. for (int i = 0; i < pa_count(mesh->edges) - nedges; i++, edge++) {
  402. if (edge->face) {
  403. edge->face = mesh->faces + (edge->face - faces);
  404. }
  405. }
  406. }
  407. }
  408. // Change all edge pointers that were pointing to "from".
  409. static void par_triangle__mesh_remap(par_triangle__edge* from,
  410. par_triangle__edge* to)
  411. {
  412. if (from->next->next->end->outgoing == from) {
  413. from->next->next->end->outgoing = to;
  414. }
  415. if (from->pair) {
  416. from->pair->pair = to;
  417. }
  418. if (from->face && from->face->edge == from) {
  419. from->face->edge = to;
  420. }
  421. if (to) {
  422. *to = *from;
  423. from->next->next->next = to;
  424. }
  425. }
  426. // Remove a face and its three interior half-edges.
  427. static void par_triangle__mesh_remove(par_triangle__mesh* mesh, int iface)
  428. {
  429. int nedges = pa_count(mesh->edges);
  430. int nfaces = pa_count(mesh->faces);
  431. // Stash the edges that we're about to kill.
  432. par_triangle__edge* edgea0 = mesh->faces[iface].edge;
  433. par_triangle__edge* edgeb0 = edgea0->next;
  434. par_triangle__edge* edgec0 = edgeb0->next;
  435. // Stash the edges that we're about to move.
  436. par_triangle__edge* edgea1 = mesh->edges + nedges - 3;
  437. par_triangle__edge* edgeb1 = mesh->edges + nedges - 2;
  438. par_triangle__edge* edgec1 = mesh->edges + nedges - 1;
  439. // Move the last face into the slot currently occupied by the dead face.
  440. par_triangle__face* face0 = mesh->faces + iface;
  441. par_triangle__face* face1 = mesh->faces + nfaces - 1;
  442. face1->edge->face = face0;
  443. face1->edge->next->face = face0;
  444. face1->edge->next->next->face = face0;
  445. face0->edge = face1->edge;
  446. pa___n(mesh->faces) -= 1;
  447. // Remap all edge pointers appropriately.
  448. par_triangle__mesh_remap(edgea0, 0);
  449. par_triangle__mesh_remap(edgeb0, 0);
  450. par_triangle__mesh_remap(edgec0, 0);
  451. par_triangle__mesh_remap(edgea1, edgea0);
  452. par_triangle__mesh_remap(edgeb1, edgeb0);
  453. par_triangle__mesh_remap(edgec1, edgec0);
  454. pa___n(mesh->edges) -= 3;
  455. }
  456. // This implements 1-into-3 triangle subdivision according to Anglada. First,
  457. // it removes the given triangle using swap-and-shrink. Next, it adds 1 new
  458. // vertex, 9 new edges, and 3 new triangles. Clients can expect all the new
  459. // primitives to live at the end of their respective arrays.
  460. static void par_triangle__mesh_subdivide(par_triangle__mesh* mesh, int face,
  461. float const* pt)
  462. {
  463. // Stash the three vertices for the face that we're subdividing.
  464. par_triangle__edge* e = mesh->faces[face].edge;
  465. int av = e->end - mesh->verts;
  466. int bv = e->next->end - mesh->verts;
  467. int cv = e->next->next->end - mesh->verts;
  468. // Remove the face and its three half-edges.
  469. par_triangle__mesh_remove(mesh, face);
  470. // Add space for 1 new vertex, 9 new edges, and 3 new triangles.
  471. par_triangle__mesh_grow(mesh, 1, 9, 3);
  472. int nverts = pa_count(mesh->verts);
  473. int nedges = pa_count(mesh->edges);
  474. int nfaces = pa_count(mesh->faces);
  475. par_triangle__vert* vert = mesh->verts + nverts - 1;
  476. par_triangle__edge* edges = mesh->edges + nedges - 9;
  477. par_triangle__face* faces = mesh->faces + nfaces - 3;
  478. par_triangle__vert* v0 = mesh->verts + av;
  479. par_triangle__vert* v1 = mesh->verts + bv;
  480. par_triangle__vert* v2 = mesh->verts + cv;
  481. // New vertex.
  482. vert->x = pt[0];
  483. vert->y = pt[1];
  484. vert->outgoing = edges + 2;
  485. // New Face 0
  486. faces[0].edge = edges + 0;
  487. edges[0].face = faces + 0;
  488. edges[0].next = edges + 1;
  489. edges[0].end = v0;
  490. edges[1].face = faces + 0;
  491. edges[1].next = edges + 2;
  492. edges[1].end = vert;
  493. edges[2].face = faces + 0;
  494. edges[2].next = edges + 0;
  495. edges[2].end = v2;
  496. edges += 3;
  497. // New Face 1
  498. faces[1].edge = edges + 0;
  499. edges[0].face = faces + 1;
  500. edges[0].next = edges + 1;
  501. edges[0].end = v1;
  502. edges[1].face = faces + 1;
  503. edges[1].next = edges + 2;
  504. edges[1].end = vert;
  505. edges[2].face = faces + 1;
  506. edges[2].next = edges + 0;
  507. edges[2].end = v0;
  508. edges += 3;
  509. // New Face 2
  510. faces[2].edge = edges + 0;
  511. edges[0].face = faces + 2;
  512. edges[0].next = edges + 1;
  513. edges[0].end = v2;
  514. edges[1].face = faces + 2;
  515. edges[1].next = edges + 2;
  516. edges[1].end = vert;
  517. edges[2].face = faces + 2;
  518. edges[2].next = edges + 0;
  519. edges[2].end = v1;
  520. edges -= 6;
  521. // Populate the pair pointers.
  522. edges[0].pair = 0;
  523. edges[1].pair = edges + 5; edges[1].fixed = edges[1].pair->fixed;
  524. edges[2].pair = edges + 7; edges[2].fixed = edges[2].pair->fixed;
  525. edges[3].pair = 0;
  526. edges[4].pair = edges + 8; edges[4].fixed = edges[4].pair->fixed;
  527. edges[5].pair = edges + 1; edges[5].fixed = edges[5].pair->fixed;
  528. edges[6].pair = 0;
  529. edges[7].pair = edges + 2; edges[7].fixed = edges[7].pair->fixed;
  530. edges[8].pair = edges + 4; edges[8].fixed = edges[8].pair->fixed;
  531. par_triangle__edge* edge = mesh->edges;
  532. for (int i = 0; i < pa_count(mesh->edges) - 9; i++, edge++) {
  533. if (edge->end == v0 && edge->next->end == v2) {
  534. edge->next->pair = edges + 0;
  535. edges[0].pair = edge->next;
  536. edges[0].fixed = edges[0].pair->fixed;
  537. continue;
  538. }
  539. if (edge->end == v1 && edge->next->end == v0) {
  540. edge->next->pair = edges + 3;
  541. edges[3].pair = edge->next;
  542. edges[3].fixed = edges[3].pair->fixed;
  543. continue;
  544. }
  545. if (edge->end == v2 && edge->next->end == v1) {
  546. edge->next->pair = edges + 6;
  547. edges[6].pair = edge->next;
  548. edges[6].fixed = edges[6].pair->fixed;
  549. continue;
  550. }
  551. }
  552. }
  553. // Finds the triangle that is adjacent to the given triangle, and that does not
  554. // share the given vertex. Returns the shared half-edge of the adjacent face.
  555. static par_triangle__edge* par_triangle__mesh_opposed(par_triangle__mesh* mesh,
  556. int face, int vertex)
  557. {
  558. par_triangle__vert* v = mesh->verts + vertex;
  559. par_triangle__edge* e0 = mesh->faces[face].edge;
  560. par_triangle__edge* e1 = e0->next;
  561. par_triangle__edge* e2 = e1->next;
  562. if (e0->end != v && e2->end != v && e0->pair && e0->pair->face) {
  563. return e0->pair;
  564. }
  565. if (e1->end != v && e0->end != v && e1->pair && e1->pair->face) {
  566. return e1->pair;
  567. }
  568. if (e2->end != v && e1->end != v && e2->pair && e2->pair->face) {
  569. return e2->pair;
  570. }
  571. return 0;
  572. }
  573. // Checks if the given point is in the circumcircle of the given edge.
  574. bool par_triangle__in_edgecircle(par_triangle__edge* edge, float x, float y)
  575. {
  576. par_triangle__vert* v0 = edge->next->next->end;
  577. par_triangle__vert* v1 = edge->end;
  578. float cx = 0.5 * (v0->x + v1->x);
  579. float cy = 0.5 * (v0->y + v1->y);
  580. float dx = cx - v1->x;
  581. float dy = cy - v1->y;
  582. float r2 = dx * dx + dy * dy;
  583. return (x - cx) * (x - cx) + (y - cy) * (y - cy) < r2;
  584. }
  585. // This is a copy of par_bubbles_touch_three_points, but without the sqrt.
  586. static void par_triangle__circumcircle(float const* xy, float* xyr)
  587. {
  588. // Many thanks to Stephen Schmitts:
  589. // http://www.abecedarical.com/zenosamples/zs_circle3pts.html
  590. float p1x = xy[0], p1y = xy[1];
  591. float p2x = xy[2], p2y = xy[3];
  592. float p3x = xy[4], p3y = xy[5];
  593. float a = p2x - p1x, b = p2y - p1y;
  594. float c = p3x - p1x, d = p3y - p1y;
  595. float e = a * (p2x + p1x) * 0.5 + b * (p2y + p1y) * 0.5;
  596. float f = c * (p3x + p1x) * 0.5 + d * (p3y + p1y) * 0.5;
  597. float det = a*d - b*c;
  598. float cx = xyr[0] = (d*e - b*f) / det;
  599. float cy = xyr[1] = (-c*e + a*f) / det;
  600. xyr[2] = (p1x - cx) * (p1x - cx) + (p1y - cy) * (p1y - cy);
  601. }
  602. // Checks if the given point is in the circumcircle of the given face.
  603. static bool par_triangle__in_circle(par_triangle__face* face, float x, float y)
  604. {
  605. par_triangle__edge* e0 = face->edge, *e1 = e0->next, *e2 = e1->next;
  606. float xy[6] = {
  607. e0->end->x, e0->end->y,
  608. e1->end->x, e1->end->y,
  609. e2->end->x, e2->end->y,
  610. };
  611. float xyr[3];
  612. par_triangle__circumcircle(xy, xyr);
  613. float dx = xyr[0] - x;
  614. float dy = xyr[1] - y;
  615. return dx * dx + dy * dy + 1e-6 < xyr[2];
  616. }
  617. // Take the shared edge between two adjacent triangles and flip it such that
  618. // it connects the opposing vertices.
  619. static void par_triangle__swapedge(par_triangle__edge* edge)
  620. {
  621. par_triangle__edge* e0 = edge,
  622. *e1 = e0->next,
  623. *e2 = e1->next,
  624. *e3 = e0->pair,
  625. *e4 = e3->next,
  626. *e5 = e4->next;
  627. par_triangle__face* f0 = e0->face,
  628. *f1 = e3->face;
  629. par_triangle__vert* v0 = e2->end,
  630. *v1 = e0->end,
  631. *v2 = e1->end,
  632. *v3 = e4->end;
  633. assert(v0 == e3->end);
  634. assert(v1 == e5->end);
  635. assert(e2->face == f0 && e5->face == f1);
  636. assert(e3->end == v0 && e3->pair == e0);
  637. assert(f0 && f1);
  638. e2->face = f1;
  639. e5->face = f0;
  640. e0->next = e5;
  641. e5->next = e1;
  642. e1->next = e0;
  643. e3->next = e2;
  644. e2->next = e4;
  645. e4->next = e3;
  646. e3->end = v2;
  647. e0->end = v3;
  648. // if (f0->edge == e2) {
  649. f0->edge = e0;
  650. // }
  651. // if (f1->edge == e5) {
  652. f1->edge = e3;
  653. // }
  654. // if (v0->outgoing == e0) {
  655. v0->outgoing = e4;
  656. // }
  657. // if (v1->outgoing == e3) {
  658. v1->outgoing = e1;
  659. // }
  660. }
  661. // This is an implementation of Anglada's AddPointCDT function.
  662. static void par_triangle__mesh_addpt(par_triangle__mesh* mesh, float const* pt)
  663. {
  664. float x = pt[0];
  665. float y = pt[1];
  666. par_triangle_mesh* public_mesh = (par_triangle_mesh*) mesh;
  667. int tri = par_triangle_mesh_find_triangle(public_mesh, x, y);
  668. par_triangle__mesh_subdivide(mesh, tri, pt);
  669. int nfaces = pa_count(mesh->faces);
  670. int new_vertex = pa_count(mesh->verts) - 1;
  671. pa_push(mesh->stack, nfaces - 3);
  672. pa_push(mesh->stack, nfaces - 2);
  673. pa_push(mesh->stack, nfaces - 1);
  674. assert(pa_count(mesh->stack) == 3);
  675. while (pa_count(mesh->stack) > 0) {
  676. int face = pa_pop(mesh->stack);
  677. par_triangle__edge* oppedge;
  678. oppedge = par_triangle__mesh_opposed(mesh, face, new_vertex);
  679. if (oppedge && !oppedge->fixed &&
  680. par_triangle__in_circle(oppedge->face, x, y)) {
  681. int oppface = oppedge->face - mesh->faces;
  682. par_triangle__swapedge(oppedge);
  683. pa_push(mesh->stack, face);
  684. pa_push(mesh->stack, oppface);
  685. }
  686. }
  687. }
  688. par_triangle_mesh* par_triangle_mesh_create_cdt(par_triangle_path const* path)
  689. {
  690. par_triangle__mesh* mesh = par_triangle__mesh_create();
  691. par_triangle_mesh* result = (par_triangle_mesh*) mesh;
  692. float minx = FLT_MAX, maxx = -FLT_MAX;
  693. float miny = FLT_MAX, maxy = -FLT_MAX;
  694. float const* pt = path->points;
  695. for (int p = 0; p < path->npoints; p++, pt += 2) {
  696. minx = PAR_MIN(pt[0], minx);
  697. miny = PAR_MIN(pt[1], miny);
  698. maxx = PAR_MAX(pt[0], maxx);
  699. maxy = PAR_MAX(pt[1], maxy);
  700. }
  701. float width = maxx - minx;
  702. float height = maxy - miny;
  703. par_triangle__mesh_transform(mesh, width, height, minx, miny);
  704. pt = path->points;
  705. for (int p = 0; p < path->npoints; p++, pt += 2) {
  706. par_triangle__mesh_addpt(mesh, pt);
  707. par_triangle__mesh_validate(mesh);
  708. }
  709. par_triangle__mesh_finalize(mesh);
  710. return result;
  711. }
  712. void par_triangle_mesh_free(par_triangle_mesh* m)
  713. {
  714. par_triangle__mesh* mesh = (par_triangle__mesh*) m;
  715. PAR_FREE(mesh->points);
  716. PAR_FREE(mesh->triangles);
  717. pa_free(mesh->verts);
  718. pa_free(mesh->faces);
  719. pa_free(mesh->edges);
  720. PAR_FREE(mesh);
  721. }
  722. #endif // PAR_TRIANGLE_IMPLEMENTATION
  723. #endif // PAR_TRIANGLE_H