1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup bli
19  */
20 
21 #include <algorithm>
22 #include <fstream>
23 #include <iostream>
24 #include <sstream>
25 
26 #include "BLI_array.hh"
27 #include "BLI_double2.hh"
28 #include "BLI_linklist.h"
29 #include "BLI_math_boolean.hh"
30 #include "BLI_math_mpq.hh"
31 #include "BLI_mpq2.hh"
32 #include "BLI_vector.hh"
33 
34 #include "BLI_delaunay_2d.h"
35 
36 namespace blender::meshintersect {
37 
38 /* Throughout this file, template argument T will be an
39  * arithmetic-like type, like float, double, or mpq_class. */
40 
math_abs(const T v)41 template<typename T> T math_abs(const T v)
42 {
43   return (v < 0) ? -v : v;
44 }
45 
46 #ifdef WITH_GMP
math_abs(const mpq_class v)47 template<> mpq_class math_abs<mpq_class>(const mpq_class v)
48 {
49   return abs(v);
50 }
51 #endif
52 
math_abs(const double v)53 template<> double math_abs<double>(const double v)
54 {
55   return fabs(v);
56 }
57 
math_to_double(const T UNUSED (v))58 template<typename T> double math_to_double(const T UNUSED(v))
59 {
60   BLI_assert(false); /* Need implementation for other type. */
61   return 0.0;
62 }
63 
64 #ifdef WITH_GMP
math_to_double(const mpq_class v)65 template<> double math_to_double<mpq_class>(const mpq_class v)
66 {
67   return v.get_d();
68 }
69 #endif
70 
math_to_double(const double v)71 template<> double math_to_double<double>(const double v)
72 {
73   return v;
74 }
75 
76 /**
77  * Define a templated 2D arrangement of vertices, edges, and faces.
78  * The #SymEdge data structure is the basis for a structure that allows
79  * easy traversal to neighboring (by topology) geometric elements.
80  * Each of #CDTVert, #CDTEdge, and #CDTFace have an input_id linked list,
81  * whose nodes contain integers that keep track of which input verts, edges,
82  * and faces, respectively, that the element was derived from.
83  *
84  * While this could be cleaned up some, it is usable by other routines in Blender
85  * that need to keep track of a 2D arrangement, with topology.
86  */
87 template<typename Arith_t> struct CDTVert;
88 template<typename Arith_t> struct CDTEdge;
89 template<typename Arith_t> struct CDTFace;
90 
91 template<typename Arith_t> struct SymEdge {
92   /** Next #SymEdge in face, doing CCW traversal of face. */
93   SymEdge<Arith_t> *next{nullptr};
94   /** Next #SymEdge CCW around vert. */
95   SymEdge<Arith_t> *rot{nullptr};
96   /** Vert at origin. */
97   CDTVert<Arith_t> *vert{nullptr};
98   /** Un-directed edge this is for. */
99   CDTEdge<Arith_t> *edge{nullptr};
100   /** Face on left side. */
101   CDTFace<Arith_t> *face{nullptr};
102 
103   SymEdge() = default;
104 };
105 
106 /**
107  * Return other #SymEdge for same #CDTEdge as \a se.
108  */
sym(const SymEdge<T> * se)109 template<typename T> inline SymEdge<T> *sym(const SymEdge<T> *se)
110 {
111   return se->next->rot;
112 }
113 
114 /** Return #SymEdge whose next is \a se. */
prev(const SymEdge<T> * se)115 template<typename T> inline SymEdge<T> *prev(const SymEdge<T> *se)
116 {
117   return se->rot->next->rot;
118 }
119 
120 template<typename Arith_t> struct CDTVert {
121   /** Coordinate. */
122   vec2<Arith_t> co;
123   /** Some edge attached to it. */
124   SymEdge<Arith_t> *symedge{nullptr};
125   /** List of corresponding vertex input ids. */
126   LinkNode *input_ids{nullptr};
127   /** Index into array that #CDTArrangement keeps. */
128   int index{-1};
129   /** Index of a CDTVert that this has merged to. -1 if no merge. */
130   int merge_to_index{-1};
131   /** Used by algorithms operating on CDT structures. */
132   int visit_index{0};
133 
134   CDTVert() = default;
135   explicit CDTVert(const vec2<Arith_t> &pt);
136 };
137 
138 template<typename Arith_t> struct CDTEdge {
139   /** List of input edge ids that this is part of. */
140   LinkNode *input_ids{nullptr};
141   /** The directed edges for this edge. */
142   SymEdge<Arith_t> symedges[2]{SymEdge<Arith_t>(), SymEdge<Arith_t>()};
143 
144   CDTEdge() = default;
145 };
146 
147 template<typename Arith_t> struct CDTFace {
148   /** A symedge in face; only used during output, so only valid then. */
149   SymEdge<Arith_t> *symedge{nullptr};
150   /** List of input face ids that this is part of. */
151   LinkNode *input_ids{nullptr};
152   /** Used by algorithms operating on CDT structures. */
153   int visit_index{0};
154   /** Marks this face no longer used. */
155   bool deleted{false};
156 
157   CDTFace() = default;
158 };
159 
160 template<typename Arith_t> struct CDTArrangement {
161   /* The arrangement owns the memory pointed to by the pointers in these vectors.
162    * They are pointers instead of actual structures because these vectors may be resized and
163    * other elements refer to the elements by pointer. */
164 
165   /** The verts. Some may be merged to others (see their merge_to_index). */
166   Vector<CDTVert<Arith_t> *> verts;
167   /** The edges. Some may be deleted (SymEdge next and rot pointers are null). */
168   Vector<CDTEdge<Arith_t> *> edges;
169   /** The faces. Some may be deleted (see their delete member). */
170   Vector<CDTFace<Arith_t> *> faces;
171   /** Which CDTFace is the outer face. */
172   CDTFace<Arith_t> *outer_face{nullptr};
173 
174   CDTArrangement() = default;
175   ~CDTArrangement();
176 
177   /** Hint to how much space to reserve in the Vectors of the arrangement,
178    * based on these counts of input elements. */
179   void reserve(int num_verts, int num_edges, int num_faces);
180 
181   /**
182    * Add a new vertex to the arrangement, with the given 2D coordinate.
183    * It will not be connected to anything yet.
184    */
185   CDTVert<Arith_t> *add_vert(const vec2<Arith_t> &pt);
186 
187   /**
188    * Add an edge from v1 to v2. The edge will have a left face and a right face,
189    * specified by \a fleft and \a fright. The edge will not be connected to anything yet.
190    * If the vertices do not yet have a #SymEdge pointer,
191    * their pointer is set to the #SymEdge in this new edge.
192    */
193   CDTEdge<Arith_t> *add_edge(CDTVert<Arith_t> *v1,
194                              CDTVert<Arith_t> *v2,
195                              CDTFace<Arith_t> *fleft,
196                              CDTFace<Arith_t> *fright);
197 
198   /**
199    * Add a new face. It is disconnected until an add_edge makes it the
200    * left or right face of an edge.
201    */
202   CDTFace<Arith_t> *add_face();
203 
204   /** Make a new edge from v to se->vert, splicing it in. */
205   CDTEdge<Arith_t> *add_vert_to_symedge_edge(CDTVert<Arith_t> *v, SymEdge<Arith_t> *se);
206 
207   /**
208    * Assuming s1 and s2 are both #SymEdge's in a face with > 3 sides and one is not the next of the
209    * other, Add an edge from `s1->v` to `s2->v`, splitting the face in two. The original face will
210    * be the one that s1 has as left face, and a new face will be added and made s2 and its
211    * next-cycle's left face.
212    */
213   CDTEdge<Arith_t> *add_diagonal(SymEdge<Arith_t> *s1, SymEdge<Arith_t> *s2);
214 
215   /**
216    * Connect the verts of se1 and se2, assuming that currently those two #SymEdge's are on the
217    * outer boundary (have face == outer_face) of two components that are isolated from each other.
218    */
219   CDTEdge<Arith_t> *connect_separate_parts(SymEdge<Arith_t> *se1, SymEdge<Arith_t> *se2);
220 
221   /**
222    * Split se at fraction lambda, and return the new #CDTEdge that is the new second half.
223    * Copy the edge input_ids into the new one.
224    */
225   CDTEdge<Arith_t> *split_edge(SymEdge<Arith_t> *se, Arith_t lambda);
226 
227   /**
228    * Delete an edge. The new combined face on either side of the deleted edge will be the one that
229    * was e's face. There will now be an unused face, which will be marked deleted, and an unused
230    * #CDTEdge, marked by setting the next and rot pointers of its #SymEdge's to #nullptr.
231    */
232   void delete_edge(SymEdge<Arith_t> *se);
233 
234   /**
235    * If the vertex with index i in the vert array has not been merge, return it.
236    * Else return the one that it has merged to.
237    */
get_vert_resolve_mergeblender::meshintersect::CDTArrangement238   CDTVert<Arith_t> *get_vert_resolve_merge(int i)
239   {
240     CDTVert<Arith_t> *v = this->verts[i];
241     if (v->merge_to_index != -1) {
242       v = this->verts[v->merge_to_index];
243     }
244     return v;
245   }
246 };
247 
248 template<typename T> class CDT_state {
249  public:
250   CDTArrangement<T> cdt;
251   /** How many verts were in input (will be first in vert_array). */
252   int input_vert_tot;
253   /** Used for visiting things without having to initialized their visit fields. */
254   int visit_count;
255   /**
256    * Edge ids for face start with this, and each face gets this much index space
257    * to encode positions within the face.
258    */
259   int face_edge_offset;
260   /** How close before coords considered equal. */
261   T epsilon;
262 
263   explicit CDT_state(int num_input_verts, int num_input_edges, int num_input_faces, T epsilon);
~CDT_state()264   ~CDT_state()
265   {
266   }
267 };
268 
~CDTArrangement()269 template<typename T> CDTArrangement<T>::~CDTArrangement()
270 {
271   for (int i : this->verts.index_range()) {
272     CDTVert<T> *v = this->verts[i];
273     BLI_linklist_free(v->input_ids, nullptr);
274     delete v;
275     this->verts[i] = nullptr;
276   }
277   for (int i : this->edges.index_range()) {
278     CDTEdge<T> *e = this->edges[i];
279     BLI_linklist_free(e->input_ids, nullptr);
280     delete e;
281     this->edges[i] = nullptr;
282   }
283   for (int i : this->faces.index_range()) {
284     CDTFace<T> *f = this->faces[i];
285     BLI_linklist_free(f->input_ids, nullptr);
286     delete f;
287     this->faces[i] = nullptr;
288   }
289 }
290 
291 #define DEBUG_CDT
292 #ifdef DEBUG_CDT
293 /* Some functions to aid in debugging. */
vertname(const CDTVert<T> * v)294 template<typename T> std::string vertname(const CDTVert<T> *v)
295 {
296   std::stringstream ss;
297   ss << "[" << v->index << "]";
298   return ss.str();
299 }
300 
301 /* Abbreviated pointer value is easier to read in dumps. */
trunc_ptr(const void * p)302 static std::string trunc_ptr(const void *p)
303 {
304   constexpr int TRUNC_PTR_MASK = 0xFFFF;
305   std::stringstream ss;
306   ss << std::hex << (POINTER_AS_INT(p) & TRUNC_PTR_MASK);
307   return ss.str();
308 }
309 
sename(const SymEdge<T> * se)310 template<typename T> std::string sename(const SymEdge<T> *se)
311 {
312   std::stringstream ss;
313   ss << "{" << trunc_ptr(se) << "}";
314   return ss.str();
315 }
316 
operator <<(std::ostream & os,const SymEdge<T> & se)317 template<typename T> std::ostream &operator<<(std::ostream &os, const SymEdge<T> &se)
318 {
319   if (se.next) {
320     os << vertname(se.vert) << "(" << se.vert->co << "->" << se.next->vert->co << ")"
321        << vertname(se.next->vert);
322   }
323   else {
324     os << vertname(se.vert) << "(" << se.vert->co << "->NULL)";
325   }
326   return os;
327 }
328 
operator <<(std::ostream & os,const SymEdge<T> * se)329 template<typename T> std::ostream &operator<<(std::ostream &os, const SymEdge<T> *se)
330 {
331   os << *se;
332   return os;
333 }
334 
short_se_dump(const SymEdge<T> * se)335 template<typename T> std::string short_se_dump(const SymEdge<T> *se)
336 {
337   if (se == nullptr) {
338     return std::string("NULL");
339   }
340   return vertname(se->vert) +
341          (se->next == nullptr ? std::string("[NULL]") : vertname(se->next->vert));
342 }
343 
operator <<(std::ostream & os,const CDT_state<T> & cdt_state)344 template<typename T> std::ostream &operator<<(std::ostream &os, const CDT_state<T> &cdt_state)
345 {
346   os << "\nCDT\n\nVERTS\n";
347   for (const CDTVert<T> *v : cdt_state.cdt.verts) {
348     os << vertname(v) << " " << trunc_ptr(v) << ": " << v->co
349        << " symedge=" << trunc_ptr(v->symedge);
350     if (v->merge_to_index == -1) {
351       os << "\n";
352     }
353     else {
354       os << " merge to " << vertname(cdt_state.cdt.verts[v->merge_to_index]) << "\n";
355     }
356     const SymEdge<T> *se = v->symedge;
357     int cnt = 0;
358     constexpr int print_count_limit = 25;
359     if (se) {
360       os << "  edges out:\n";
361       do {
362         if (se->next == NULL) {
363           os << "    [NULL] next/rot symedge, se=" << trunc_ptr(se) << "\n";
364           break;
365         }
366         if (se->next->next == NULL) {
367           os << "    [NULL] next-next/rot symedge, se=" << trunc_ptr(se) << "\n";
368           break;
369         }
370         const CDTVert<T> *vother = sym(se)->vert;
371         os << "    " << vertname(vother) << "(e=" << trunc_ptr(se->edge)
372            << ", se=" << trunc_ptr(se) << ")\n";
373         se = se->rot;
374         cnt++;
375       } while (se != v->symedge && cnt < print_count_limit);
376       os << "\n";
377     }
378   }
379   os << "\nEDGES\n";
380   for (const CDTEdge<T> *e : cdt_state.cdt.edges) {
381     if (e->symedges[0].next == nullptr) {
382       continue;
383     }
384     os << trunc_ptr(&e) << ":\n";
385     for (int i = 0; i < 2; ++i) {
386       const SymEdge<T> *se = &e->symedges[i];
387       os << "  se[" << i << "] @" << trunc_ptr(se) << " next=" << trunc_ptr(se->next)
388          << ", rot=" << trunc_ptr(se->rot) << ", vert=" << trunc_ptr(se->vert) << " "
389          << vertname(se->vert) << " " << se->vert->co << ", edge=" << trunc_ptr(se->edge)
390          << ", face=" << trunc_ptr(se->face) << "\n";
391     }
392   }
393   os << "\nFACES\n";
394   os << "outer_face=" << trunc_ptr(cdt_state.cdt.outer_face) << "\n";
395   /* Only after prepare_output do faces have non-null symedges. */
396   if (cdt_state.cdt.outer_face->symedge != nullptr) {
397     for (const CDTFace<T> *f : cdt_state.cdt.faces) {
398       if (!f->deleted) {
399         os << trunc_ptr(f) << " symedge=" << trunc_ptr(f->symedge) << "\n";
400       }
401     }
402   }
403   return os;
404 }
405 
cdt_draw(const std::string & label,const CDTArrangement<T> & cdt)406 template<typename T> void cdt_draw(const std::string &label, const CDTArrangement<T> &cdt)
407 {
408   static bool append = false; /* Will be set to true after first call. */
409 
410 /* Would like to use #BKE_tempdir_base() here, but that brings in dependence on kernel library.
411  * This is just for developer debugging anyway, and should never be called in production Blender.
412  */
413 #  ifdef _WIN32
414   const char *drawfile = "./debug_draw.html";
415 #  else
416   const char *drawfile = "/tmp/debug_draw.html";
417 #  endif
418   constexpr int max_draw_width = 1800;
419   constexpr int max_draw_height = 1600;
420   constexpr double margin_expand = 0.05;
421   constexpr int thin_line = 1;
422   constexpr int thick_line = 4;
423   constexpr int vert_radius = 3;
424   constexpr bool draw_vert_labels = true;
425   constexpr bool draw_edge_labels = false;
426 
427   if (cdt.verts.size() == 0) {
428     return;
429   }
430   vec2<double> vmin(DBL_MAX, DBL_MAX);
431   vec2<double> vmax(-DBL_MAX, -DBL_MAX);
432   for (const CDTVert<T> *v : cdt.verts) {
433     for (int i = 0; i < 2; ++i) {
434       double dvi = math_to_double(v->co[i]);
435       if (dvi < vmin[i]) {
436         vmin[i] = dvi;
437       }
438       if (dvi > vmax[i]) {
439         vmax[i] = dvi;
440       }
441     }
442   }
443   double draw_margin = ((vmax.x - vmin.x) + (vmax.y - vmin.y)) * margin_expand;
444   double minx = vmin.x - draw_margin;
445   double maxx = vmax.x + draw_margin;
446   double miny = vmin.y - draw_margin;
447   double maxy = vmax.y + draw_margin;
448 
449   double width = maxx - minx;
450   double height = maxy - miny;
451   double aspect = height / width;
452   int view_width = max_draw_width;
453   int view_height = static_cast<int>(view_width * aspect);
454   if (view_height > max_draw_height) {
455     view_height = max_draw_height;
456     view_width = static_cast<int>(view_height / aspect);
457   }
458   double scale = view_width / width;
459 
460 #  define SX(x) ((math_to_double(x) - minx) * scale)
461 #  define SY(y) ((maxy - math_to_double(y)) * scale)
462 
463   std::ofstream f;
464   if (append) {
465     f.open(drawfile, std::ios_base::app);
466   }
467   else {
468     f.open(drawfile);
469   }
470   if (!f) {
471     std::cout << "Could not open file " << drawfile << "\n";
472     return;
473   }
474 
475   f << "<div>" << label << "</div>\n<div>\n"
476     << "<svg version=\"1.1\" "
477        "xmlns=\"http://www.w3.org/2000/svg\" "
478        "xmlns:xlink=\"http://www.w3.org/1999/xlink\" "
479        "xml:space=\"preserve\"\n"
480     << "width=\"" << view_width << "\" height=\"" << view_height << "\">n";
481 
482   for (const CDTEdge<T> *e : cdt.edges) {
483     if (e->symedges[0].next == nullptr) {
484       continue;
485     }
486     const CDTVert<T> *u = e->symedges[0].vert;
487     const CDTVert<T> *v = e->symedges[1].vert;
488     const vec2<T> &uco = u->co;
489     const vec2<T> &vco = v->co;
490     int strokew = e->input_ids == nullptr ? thin_line : thick_line;
491     f << "<line fill=\"none\" stroke=\"black\" stroke-width=\"" << strokew << "\" x1=\""
492       << SX(uco[0]) << "\" y1=\"" << SY(uco[1]) << "\" x2=\"" << SX(vco[0]) << "\" y2=\""
493       << SY(vco[1]) << "\">\n";
494     f << "  <title>" << vertname(u) << vertname(v) << "</title>\n";
495     f << "</line>\n";
496     if (draw_edge_labels) {
497       f << "<text x=\"" << SX((uco[0] + vco[0]) / 2) << "\" y=\"" << SY((uco[1] + vco[1]) / 2)
498         << "\" font-size=\"small\">";
499       f << vertname(u) << vertname(v) << sename(&e->symedges[0]) << sename(&e->symedges[1])
500         << "</text>\n";
501     }
502   }
503 
504   int i = 0;
505   for (const CDTVert<T> *v : cdt.verts) {
506     f << "<circle fill=\"black\" cx=\"" << SX(v->co[0]) << "\" cy=\"" << SY(v->co[1]) << "\" r=\""
507       << vert_radius << "\">\n";
508     f << "  <title>[" << i << "]" << v->co << "</title>\n";
509     f << "</circle>\n";
510     if (draw_vert_labels) {
511       f << "<text x=\"" << SX(v->co[0]) + vert_radius << "\" y=\"" << SY(v->co[1]) - vert_radius
512         << "\" font-size=\"small\">[" << i << "]</text>\n";
513     }
514     ++i;
515   }
516 
517   append = true;
518 #  undef SX
519 #  undef SY
520 }
521 #endif
522 
523 /**
524  * Return true if `a -- b -- c` are in that order, assuming they are on a straight line according
525  * to #orient2d and we know the order is either `abc` or `bac`.
526  * This means `ab . ac` and `bc . ac` must both be non-negative.
527  */
in_line(const vec2<T> & a,const vec2<T> & b,const vec2<T> & c)528 template<typename T> bool in_line(const vec2<T> &a, const vec2<T> &b, const vec2<T> &c)
529 {
530   vec2<T> ab = b - a;
531   vec2<T> bc = c - b;
532   vec2<T> ac = c - a;
533   if (vec2<T>::dot(ab, ac) < 0) {
534     return false;
535   }
536   return vec2<T>::dot(bc, ac) >= 0;
537 }
538 
CDTVert(const vec2<T> & pt)539 template<typename T> CDTVert<T>::CDTVert(const vec2<T> &pt)
540 {
541   this->co = pt;
542   this->input_ids = nullptr;
543   this->symedge = nullptr;
544   this->index = -1;
545   this->merge_to_index = -1;
546   this->visit_index = 0;
547 }
548 
add_vert(const vec2<T> & pt)549 template<typename T> CDTVert<T> *CDTArrangement<T>::add_vert(const vec2<T> &pt)
550 {
551   CDTVert<T> *v = new CDTVert<T>(pt);
552   int index = this->verts.append_and_get_index(v);
553   v->index = index;
554   return v;
555 }
556 
557 template<typename T>
add_edge(CDTVert<T> * v1,CDTVert<T> * v2,CDTFace<T> * fleft,CDTFace<T> * fright)558 CDTEdge<T> *CDTArrangement<T>::add_edge(CDTVert<T> *v1,
559                                         CDTVert<T> *v2,
560                                         CDTFace<T> *fleft,
561                                         CDTFace<T> *fright)
562 {
563   CDTEdge<T> *e = new CDTEdge<T>();
564   this->edges.append(e);
565   SymEdge<T> *se = &e->symedges[0];
566   SymEdge<T> *sesym = &e->symedges[1];
567   se->edge = sesym->edge = e;
568   se->face = fleft;
569   sesym->face = fright;
570   se->vert = v1;
571   if (v1->symedge == nullptr) {
572     v1->symedge = se;
573   }
574   sesym->vert = v2;
575   if (v2->symedge == nullptr) {
576     v2->symedge = sesym;
577   }
578   se->next = sesym->next = se->rot = sesym->rot = nullptr;
579   return e;
580 }
581 
add_face()582 template<typename T> CDTFace<T> *CDTArrangement<T>::add_face()
583 {
584   CDTFace<T> *f = new CDTFace<T>();
585   this->faces.append(f);
586   return f;
587 }
588 
reserve(int num_verts,int num_edges,int num_faces)589 template<typename T> void CDTArrangement<T>::reserve(int num_verts, int num_edges, int num_faces)
590 {
591   /* These reserves are just guesses; OK if they aren't exactly right since vectors will resize. */
592   this->verts.reserve(2 * num_verts);
593   this->edges.reserve(3 * num_verts + 2 * num_edges + 3 * 2 * num_faces);
594   this->faces.reserve(2 * num_verts + 2 * num_edges + 2 * num_faces);
595 }
596 
597 template<typename T>
CDT_state(int num_input_verts,int num_input_edges,int num_input_faces,T epsilon)598 CDT_state<T>::CDT_state(int num_input_verts, int num_input_edges, int num_input_faces, T epsilon)
599 {
600   this->input_vert_tot = num_input_verts;
601   this->cdt.reserve(num_input_verts, num_input_edges, num_input_faces);
602   this->cdt.outer_face = this->cdt.add_face();
603   this->epsilon = epsilon;
604   this->visit_count = 0;
605 }
606 
id_in_list(const LinkNode * id_list,int id)607 static bool id_in_list(const LinkNode *id_list, int id)
608 {
609   const LinkNode *ln;
610 
611   for (ln = id_list; ln != nullptr; ln = ln->next) {
612     if (POINTER_AS_INT(ln->link) == id) {
613       return true;
614     }
615   }
616   return false;
617 }
618 
619 /* Is any id in (range_start, range_start+1, ... , range_end) in id_list? */
id_range_in_list(const LinkNode * id_list,int range_start,int range_end)620 static bool id_range_in_list(const LinkNode *id_list, int range_start, int range_end)
621 {
622   const LinkNode *ln;
623   int id;
624 
625   for (ln = id_list; ln != nullptr; ln = ln->next) {
626     id = POINTER_AS_INT(ln->link);
627     if (id >= range_start && id <= range_end) {
628       return true;
629     }
630   }
631   return false;
632 }
633 
add_to_input_ids(LinkNode ** dst,int input_id)634 static void add_to_input_ids(LinkNode **dst, int input_id)
635 {
636   if (!id_in_list(*dst, input_id)) {
637     BLI_linklist_prepend(dst, POINTER_FROM_INT(input_id));
638   }
639 }
640 
add_list_to_input_ids(LinkNode ** dst,const LinkNode * src)641 static void add_list_to_input_ids(LinkNode **dst, const LinkNode *src)
642 {
643   const LinkNode *ln;
644 
645   for (ln = src; ln != nullptr; ln = ln->next) {
646     add_to_input_ids(dst, POINTER_AS_INT(ln->link));
647   }
648 }
649 
is_border_edge(const CDTEdge<T> * e,const CDT_state<T> * cdt)650 template<typename T> inline bool is_border_edge(const CDTEdge<T> *e, const CDT_state<T> *cdt)
651 {
652   return e->symedges[0].face == cdt->outer_face || e->symedges[1].face == cdt->outer_face;
653 }
654 
is_constrained_edge(const CDTEdge<T> * e)655 template<typename T> inline bool is_constrained_edge(const CDTEdge<T> *e)
656 {
657   return e->input_ids != NULL;
658 }
659 
is_deleted_edge(const CDTEdge<T> * e)660 template<typename T> inline bool is_deleted_edge(const CDTEdge<T> *e)
661 {
662   return e->symedges[0].next == NULL;
663 }
664 
is_original_vert(const CDTVert<T> * v,CDT_state<T> * cdt)665 template<typename T> inline bool is_original_vert(const CDTVert<T> *v, CDT_state<T> *cdt)
666 {
667   return (v->index < cdt->input_vert_tot);
668 }
669 
670 /* Return the Symedge that goes from v1 to v2, if it exists, else return nullptr. */
671 template<typename T>
find_symedge_between_verts(const CDTVert<T> * v1,const CDTVert<T> * v2)672 SymEdge<T> *find_symedge_between_verts(const CDTVert<T> *v1, const CDTVert<T> *v2)
673 {
674   SymEdge<T> *t = v1->symedge;
675   SymEdge<T> *tstart = t;
676   do {
677     if (t->next->vert == v2) {
678       return t;
679     }
680   } while ((t = t->rot) != tstart);
681   return nullptr;
682 }
683 
684 /**
685  * Return the SymEdge attached to v that has face f, if it exists, else return nullptr.
686  */
find_symedge_with_face(const CDTVert<T> * v,const CDTFace<T> * f)687 template<typename T> SymEdge<T> *find_symedge_with_face(const CDTVert<T> *v, const CDTFace<T> *f)
688 {
689   SymEdge<T> *t = v->symedge;
690   SymEdge<T> *tstart = t;
691   do {
692     if (t->face == f) {
693       return t;
694     }
695   } while ((t = t->rot) != tstart);
696   return nullptr;
697 }
698 
699 /**
700  * Is there already an edge between a and b?
701  */
exists_edge(const CDTVert<T> * v1,const CDTVert<T> * v2)702 template<typename T> inline bool exists_edge(const CDTVert<T> *v1, const CDTVert<T> *v2)
703 {
704   return find_symedge_between_verts(v1, v2) != nullptr;
705 }
706 
707 /**
708  * Is the vertex v incident on face f?
709  */
vert_touches_face(const CDTVert<T> * v,const CDTFace<T> * f)710 template<typename T> bool vert_touches_face(const CDTVert<T> *v, const CDTFace<T> *f)
711 {
712   SymEdge<T> *se = v->symedge;
713   do {
714     if (se->face == f) {
715       return true;
716     }
717   } while ((se = se->rot) != v->symedge);
718   return false;
719 }
720 
721 /**
722  * Assume s1 and s2 are both #SymEdges in a face with > 3 sides,
723  * and one is not the next of the other.
724  * Add an edge from `s1->v` to `s2->v`, splitting the face in two.
725  * The original face will continue to be associated with the sub-face
726  * that has s1, and a new face will be made for s2's new face.
727  * Return the new diagonal's #CDTEdge pointer.
728  */
add_diagonal(SymEdge<T> * s1,SymEdge<T> * s2)729 template<typename T> CDTEdge<T> *CDTArrangement<T>::add_diagonal(SymEdge<T> *s1, SymEdge<T> *s2)
730 {
731   CDTFace<T> *fold = s1->face;
732   CDTFace<T> *fnew = this->add_face();
733   SymEdge<T> *s1prev = prev(s1);
734   SymEdge<T> *s1prevsym = sym(s1prev);
735   SymEdge<T> *s2prev = prev(s2);
736   SymEdge<T> *s2prevsym = sym(s2prev);
737   CDTEdge<T> *ediag = this->add_edge(s1->vert, s2->vert, fnew, fold);
738   SymEdge<T> *sdiag = &ediag->symedges[0];
739   SymEdge<T> *sdiagsym = &ediag->symedges[1];
740   sdiag->next = s2;
741   sdiagsym->next = s1;
742   s2prev->next = sdiagsym;
743   s1prev->next = sdiag;
744   s1->rot = sdiag;
745   sdiag->rot = s1prevsym;
746   s2->rot = sdiagsym;
747   sdiagsym->rot = s2prevsym;
748   for (SymEdge<T> *se = s2; se != sdiag; se = se->next) {
749     se->face = fnew;
750   }
751   add_list_to_input_ids(&fnew->input_ids, fold->input_ids);
752   return ediag;
753 }
754 
755 template<typename T>
add_vert_to_symedge_edge(CDTVert<T> * v,SymEdge<T> * se)756 CDTEdge<T> *CDTArrangement<T>::add_vert_to_symedge_edge(CDTVert<T> *v, SymEdge<T> *se)
757 {
758   SymEdge<T> *se_rot = se->rot;
759   SymEdge<T> *se_rotsym = sym(se_rot);
760   /* TODO: check: I think last arg in next should be sym(se)->face. */
761   CDTEdge<T> *e = this->add_edge(v, se->vert, se->face, se->face);
762   SymEdge<T> *new_se = &e->symedges[0];
763   SymEdge<T> *new_se_sym = &e->symedges[1];
764   new_se->next = se;
765   new_se_sym->next = new_se;
766   new_se->rot = new_se;
767   new_se_sym->rot = se_rot;
768   se->rot = new_se_sym;
769   se_rotsym->next = new_se_sym;
770   return e;
771 }
772 
773 /**
774  * Connect the verts of se1 and se2, assuming that currently those two #SymEdge's are on
775  * the outer boundary (have face == outer_face) of two components that are isolated from
776  * each other.
777  */
778 template<typename T>
connect_separate_parts(SymEdge<T> * se1,SymEdge<T> * se2)779 CDTEdge<T> *CDTArrangement<T>::connect_separate_parts(SymEdge<T> *se1, SymEdge<T> *se2)
780 {
781   BLI_assert(se1->face == this->outer_face && se2->face == this->outer_face);
782   SymEdge<T> *se1_rot = se1->rot;
783   SymEdge<T> *se1_rotsym = sym(se1_rot);
784   SymEdge<T> *se2_rot = se2->rot;
785   SymEdge<T> *se2_rotsym = sym(se2_rot);
786   CDTEdge<T> *e = this->add_edge(se1->vert, se2->vert, this->outer_face, this->outer_face);
787   SymEdge<T> *new_se = &e->symedges[0];
788   SymEdge<T> *new_se_sym = &e->symedges[1];
789   new_se->next = se2;
790   new_se_sym->next = se1;
791   new_se->rot = se1_rot;
792   new_se_sym->rot = se2_rot;
793   se1->rot = new_se;
794   se2->rot = new_se_sym;
795   se1_rotsym->next = new_se;
796   se2_rotsym->next = new_se_sym;
797   return e;
798 }
799 
800 /**
801  * Split se at fraction lambda,
802  * and return the new #CDTEdge that is the new second half.
803  * Copy the edge input_ids into the new one.
804  */
split_edge(SymEdge<T> * se,T lambda)805 template<typename T> CDTEdge<T> *CDTArrangement<T>::split_edge(SymEdge<T> *se, T lambda)
806 {
807   /* Split e at lambda. */
808   const vec2<T> *a = &se->vert->co;
809   const vec2<T> *b = &se->next->vert->co;
810   SymEdge<T> *sesym = sym(se);
811   SymEdge<T> *sesymprev = prev(sesym);
812   SymEdge<T> *sesymprevsym = sym(sesymprev);
813   SymEdge<T> *senext = se->next;
814   CDTVert<T> *v = this->add_vert(vec2<T>::interpolate(*a, *b, lambda));
815   CDTEdge<T> *e = this->add_edge(v, se->next->vert, se->face, sesym->face);
816   sesym->vert = v;
817   SymEdge<T> *newse = &e->symedges[0];
818   SymEdge<T> *newsesym = &e->symedges[1];
819   se->next = newse;
820   newsesym->next = sesym;
821   newse->next = senext;
822   newse->rot = sesym;
823   sesym->rot = newse;
824   senext->rot = newsesym;
825   newsesym->rot = sesymprevsym;
826   sesymprev->next = newsesym;
827   if (newsesym->vert->symedge == sesym) {
828     newsesym->vert->symedge = newsesym;
829   }
830   add_list_to_input_ids(&e->input_ids, se->edge->input_ids);
831   return e;
832 }
833 
834 /**
835  * Delete an edge from the structure. The new combined face on either side of
836  * the deleted edge will be the one that was e's face.
837  * There will be now an unused face, marked by setting its deleted flag,
838  * and an unused #CDTEdge, marked by setting the next and rot pointers of
839  * its #SymEdges to #nullptr.
840  * <pre>
841  *        .  v2               .
842  *       / \                 / \
843  *      /f|j\               /   \
844  *     /  |  \             /     \
845  *        |
846  *      A |  B                A
847  *    \  e|   /           \       /
848  *     \  | /              \     /
849  *      \h|i/               \   /
850  *        .  v1               .
851  * </pre>
852  * Also handle variant cases where one or both ends
853  * are attached only to e.
854  */
delete_edge(SymEdge<T> * se)855 template<typename T> void CDTArrangement<T>::delete_edge(SymEdge<T> *se)
856 {
857   SymEdge<T> *sesym = sym(se);
858   CDTVert<T> *v1 = se->vert;
859   CDTVert<T> *v2 = sesym->vert;
860   CDTFace<T> *aface = se->face;
861   CDTFace<T> *bface = sesym->face;
862   SymEdge<T> *f = se->next;
863   SymEdge<T> *h = prev(se);
864   SymEdge<T> *i = sesym->next;
865   SymEdge<T> *j = prev(sesym);
866   SymEdge<T> *jsym = sym(j);
867   SymEdge<T> *hsym = sym(h);
868   bool v1_isolated = (i == se);
869   bool v2_isolated = (f == sesym);
870 
871   if (!v1_isolated) {
872     h->next = i;
873     i->rot = hsym;
874   }
875   if (!v2_isolated) {
876     j->next = f;
877     f->rot = jsym;
878   }
879   if (!v1_isolated && !v2_isolated && aface != bface) {
880     for (SymEdge<T> *k = i; k != f; k = k->next) {
881       k->face = aface;
882     }
883   }
884 
885   /* If e was representative symedge for v1 or v2, fix that. */
886   if (v1_isolated) {
887     v1->symedge = nullptr;
888   }
889   else if (v1->symedge == se) {
890     v1->symedge = i;
891   }
892   if (v2_isolated) {
893     v2->symedge = nullptr;
894   }
895   else if (v2->symedge == sesym) {
896     v2->symedge = f;
897   }
898 
899   /* Mark SymEdge as deleted by setting all its pointers to NULL. */
900   se->next = se->rot = nullptr;
901   sesym->next = sesym->rot = nullptr;
902   if (!v1_isolated && !v2_isolated && aface != bface) {
903     bface->deleted = true;
904     if (this->outer_face == bface) {
905       this->outer_face = aface;
906     }
907   }
908 }
909 
910 template<typename T> class SiteInfo {
911  public:
912   CDTVert<T> *v;
913   int orig_index;
914 };
915 
916 /**
917  * Compare function for lexicographic sort: x, then y, then index.
918  */
site_lexicographic_sort(const SiteInfo<T> & a,const SiteInfo<T> & b)919 template<typename T> bool site_lexicographic_sort(const SiteInfo<T> &a, const SiteInfo<T> &b)
920 {
921   const vec2<T> &co_a = a.v->co;
922   const vec2<T> &co_b = b.v->co;
923   if (co_a[0] < co_b[0]) {
924     return true;
925   }
926   if (co_a[0] > co_b[0]) {
927     return false;
928   }
929   if (co_a[1] < co_b[1]) {
930     return true;
931   }
932   if (co_a[1] > co_b[1]) {
933     return false;
934   }
935   return a.orig_index < b.orig_index;
936 }
937 
938 /**
939  * Find series of equal vertices in the sorted sites array
940  * and use the vertices merge_to_index to indicate that
941  * all vertices after the first merge to the first.
942  */
find_site_merges(Array<SiteInfo<T>> & sites)943 template<typename T> void find_site_merges(Array<SiteInfo<T>> &sites)
944 {
945   int n = sites.size();
946   for (int i = 0; i < n - 1; ++i) {
947     int j = i + 1;
948     while (j < n && sites[j].v->co == sites[i].v->co) {
949       sites[j].v->merge_to_index = sites[i].orig_index;
950       ++j;
951     }
952     if (j - i > 1) {
953       i = j - 1; /* j-1 because loop head will add another 1. */
954     }
955   }
956 }
957 
vert_left_of_symedge(CDTVert<T> * v,SymEdge<T> * se)958 template<typename T> inline bool vert_left_of_symedge(CDTVert<T> *v, SymEdge<T> *se)
959 {
960   return orient2d(v->co, se->vert->co, se->next->vert->co) > 0;
961 }
962 
vert_right_of_symedge(CDTVert<T> * v,SymEdge<T> * se)963 template<typename T> inline bool vert_right_of_symedge(CDTVert<T> *v, SymEdge<T> *se)
964 {
965   return orient2d(v->co, se->next->vert->co, se->vert->co) > 0;
966 }
967 
968 /* Is se above basel? */
969 template<typename T>
dc_tri_valid(SymEdge<T> * se,SymEdge<T> * basel,SymEdge<T> * basel_sym)970 inline bool dc_tri_valid(SymEdge<T> *se, SymEdge<T> *basel, SymEdge<T> *basel_sym)
971 {
972   return orient2d(se->next->vert->co, basel_sym->vert->co, basel->vert->co) > 0;
973 }
974 
975 /**
976  * Delaunay triangulate sites[start} to sites[end-1].
977  * Assume sites are lexicographically sorted by coordinate.
978  * Return #SymEdge of CCW convex hull at left-most point in *r_le
979  * and that of right-most point of cw convex null in *r_re.
980  */
981 template<typename T>
dc_tri(CDTArrangement<T> * cdt,Array<SiteInfo<T>> & sites,int start,int end,SymEdge<T> ** r_le,SymEdge<T> ** r_re)982 void dc_tri(CDTArrangement<T> *cdt,
983             Array<SiteInfo<T>> &sites,
984             int start,
985             int end,
986             SymEdge<T> **r_le,
987             SymEdge<T> **r_re)
988 {
989   constexpr int dbg_level = 0;
990   if (dbg_level > 0) {
991     std::cout << "DC_TRI start=" << start << " end=" << end << "\n";
992   }
993   int n = end - start;
994   if (n <= 1) {
995     *r_le = nullptr;
996     *r_re = nullptr;
997     return;
998   }
999 
1000   /* Base case: if n <= 3, triangulate directly. */
1001   if (n <= 3) {
1002     CDTVert<T> *v1 = sites[start].v;
1003     CDTVert<T> *v2 = sites[start + 1].v;
1004     CDTEdge<T> *ea = cdt->add_edge(v1, v2, cdt->outer_face, cdt->outer_face);
1005     ea->symedges[0].next = &ea->symedges[1];
1006     ea->symedges[1].next = &ea->symedges[0];
1007     ea->symedges[0].rot = &ea->symedges[0];
1008     ea->symedges[1].rot = &ea->symedges[1];
1009     if (n == 2) {
1010       *r_le = &ea->symedges[0];
1011       *r_re = &ea->symedges[1];
1012       return;
1013     }
1014     CDTVert<T> *v3 = sites[start + 2].v;
1015     CDTEdge<T> *eb = cdt->add_vert_to_symedge_edge(v3, &ea->symedges[1]);
1016     int orient = orient2d(v1->co, v2->co, v3->co);
1017     if (orient > 0) {
1018       cdt->add_diagonal(&eb->symedges[0], &ea->symedges[0]);
1019       *r_le = &ea->symedges[0];
1020       *r_re = &eb->symedges[0];
1021     }
1022     else if (orient < 0) {
1023       cdt->add_diagonal(&ea->symedges[0], &eb->symedges[0]);
1024       *r_le = ea->symedges[0].rot;
1025       *r_re = eb->symedges[0].rot;
1026     }
1027     else {
1028       /* Collinear points. Just return a line. */
1029       *r_le = &ea->symedges[0];
1030       *r_re = &eb->symedges[0];
1031     }
1032     return;
1033   }
1034   /* Recursive case. Do left (L) and right (R) halves separately, then join. */
1035   int n2 = n / 2;
1036   BLI_assert(n2 >= 2 && end - (start + n2) >= 2);
1037   SymEdge<T> *ldo;
1038   SymEdge<T> *ldi;
1039   SymEdge<T> *rdi;
1040   SymEdge<T> *rdo;
1041   dc_tri(cdt, sites, start, start + n2, &ldo, &ldi);
1042   dc_tri(cdt, sites, start + n2, end, &rdi, &rdo);
1043   if (dbg_level > 0) {
1044     std::cout << "\nDC_TRI merge step for start=" << start << ", end=" << end << "\n";
1045     std::cout << "ldo " << ldo << "\n"
1046               << "ldi " << ldi << "\n"
1047               << "rdi " << rdi << "\n"
1048               << "rdo " << rdo << "\n";
1049     if (dbg_level > 1) {
1050       std::string lab = "dc_tri (" + std::to_string(start) + "," + std::to_string(start + n2) +
1051                         ")(" + std::to_string(start + n2) + "," + std::to_string(end) + ")";
1052       cdt_draw(lab, *cdt);
1053     }
1054   }
1055   /* Find lower common tangent of L and R. */
1056   for (;;) {
1057     if (vert_left_of_symedge(rdi->vert, ldi)) {
1058       ldi = ldi->next;
1059     }
1060     else if (vert_right_of_symedge(ldi->vert, rdi)) {
1061       rdi = sym(rdi)->rot; /* Previous edge to rdi with same right face. */
1062     }
1063     else {
1064       break;
1065     }
1066   }
1067   if (dbg_level > 0) {
1068     std::cout << "common lower tangent in between\n"
1069               << "rdi " << rdi << "\n"
1070               << "ldi" << ldi << "\n";
1071   }
1072 
1073   CDTEdge<T> *ebasel = cdt->connect_separate_parts(sym(rdi)->next, ldi);
1074   SymEdge<T> *basel = &ebasel->symedges[0];
1075   SymEdge<T> *basel_sym = &ebasel->symedges[1];
1076   if (dbg_level > 1) {
1077     std::cout << "basel " << basel;
1078     cdt_draw("after basel made", *cdt);
1079   }
1080   if (ldi->vert == ldo->vert) {
1081     ldo = basel_sym;
1082   }
1083   if (rdi->vert == rdo->vert) {
1084     rdo = basel;
1085   }
1086 
1087   /* Merge loop. */
1088   for (;;) {
1089     /* Locate the first point lcand->next->vert encountered by rising bubble,
1090      * and delete L edges out of basel->next->vert that fail the circle test. */
1091     SymEdge<T> *lcand = basel_sym->rot;
1092     SymEdge<T> *rcand = basel_sym->next;
1093     if (dbg_level > 1) {
1094       std::cout << "\ntop of merge loop\n";
1095       std::cout << "lcand " << lcand << "\n"
1096                 << "rcand " << rcand << "\n"
1097                 << "basel " << basel << "\n";
1098     }
1099     if (dc_tri_valid(lcand, basel, basel_sym)) {
1100       if (dbg_level > 1) {
1101         std::cout << "found valid lcand\n";
1102         std::cout << "  lcand" << lcand << "\n";
1103       }
1104       while (incircle(basel_sym->vert->co,
1105                       basel->vert->co,
1106                       lcand->next->vert->co,
1107                       lcand->rot->next->vert->co) > 0.0) {
1108         if (dbg_level > 1) {
1109           std::cout << "incircle says to remove lcand\n";
1110           std::cout << "  lcand" << lcand << "\n";
1111         }
1112         SymEdge<T> *t = lcand->rot;
1113         cdt->delete_edge(sym(lcand));
1114         lcand = t;
1115       }
1116     }
1117     /* Symmetrically, locate first R point to be hit and delete R edges. */
1118     if (dc_tri_valid(rcand, basel, basel_sym)) {
1119       if (dbg_level > 1) {
1120         std::cout << "found valid rcand\n";
1121         std::cout << "  rcand" << rcand << "\n";
1122       }
1123       while (incircle(basel_sym->vert->co,
1124                       basel->vert->co,
1125                       rcand->next->vert->co,
1126                       sym(rcand)->next->next->vert->co) > 0.0) {
1127         if (dbg_level > 0) {
1128           std::cout << "incircle says to remove rcand\n";
1129           std::cout << "  rcand" << rcand << "\n";
1130         }
1131         SymEdge<T> *t = sym(rcand)->next;
1132         cdt->delete_edge(rcand);
1133         rcand = t;
1134       }
1135     }
1136     /* If both lcand and rcand are invalid, then basel is the common upper tangent. */
1137     bool valid_lcand = dc_tri_valid(lcand, basel, basel_sym);
1138     bool valid_rcand = dc_tri_valid(rcand, basel, basel_sym);
1139     if (dbg_level > 0) {
1140       std::cout << "after bubbling up, valid_lcand=" << valid_lcand
1141                 << ", valid_rand=" << valid_rcand << "\n";
1142       std::cout << "lcand" << lcand << "\n"
1143                 << "rcand" << rcand << "\n";
1144     }
1145     if (!valid_lcand && !valid_rcand) {
1146       break;
1147     }
1148     /* The next cross edge to be connected is to either `lcand->next->vert` or `rcand->next->vert`;
1149      * if both are valid, choose the appropriate one using the #incircle test. */
1150     if (!valid_lcand ||
1151         (valid_rcand &&
1152          incircle(lcand->next->vert->co, lcand->vert->co, rcand->vert->co, rcand->next->vert->co) >
1153              0)) {
1154       if (dbg_level > 0) {
1155         std::cout << "connecting rcand\n";
1156         std::cout << "  se1=basel_sym" << basel_sym << "\n";
1157         std::cout << "  se2=rcand->next" << rcand->next << "\n";
1158       }
1159       ebasel = cdt->add_diagonal(rcand->next, basel_sym);
1160     }
1161     else {
1162       if (dbg_level > 0) {
1163         std::cout << "connecting lcand\n";
1164         std::cout << "  se1=sym(lcand)" << sym(lcand) << "\n";
1165         std::cout << "  se2=basel_sym->next" << basel_sym->next << "\n";
1166       }
1167       ebasel = cdt->add_diagonal(basel_sym->next, sym(lcand));
1168     }
1169     basel = &ebasel->symedges[0];
1170     basel_sym = &ebasel->symedges[1];
1171     BLI_assert(basel_sym->face == cdt->outer_face);
1172     if (dbg_level > 2) {
1173       cdt_draw("after adding new crossedge", *cdt);
1174     }
1175   }
1176   *r_le = ldo;
1177   *r_re = rdo;
1178   BLI_assert(sym(ldo)->face == cdt->outer_face && rdo->face == cdt->outer_face);
1179 }
1180 
1181 /* Guibas-Stolfi Divide-and_Conquer algorithm. */
dc_triangulate(CDTArrangement<T> * cdt,Array<SiteInfo<T>> & sites)1182 template<typename T> void dc_triangulate(CDTArrangement<T> *cdt, Array<SiteInfo<T>> &sites)
1183 {
1184   /* Compress sites in place to eliminted verts that merge to others. */
1185   int i = 0;
1186   int j = 0;
1187   int nsites = sites.size();
1188   while (j < nsites) {
1189     /* Invariante: sites[0..i-1] have non-merged verts from 0..(j-1) in them. */
1190     sites[i] = sites[j++];
1191     if (sites[i].v->merge_to_index < 0) {
1192       i++;
1193     }
1194   }
1195   int n = i;
1196   if (n == 0) {
1197     return;
1198   }
1199   SymEdge<T> *le;
1200   SymEdge<T> *re;
1201   dc_tri(cdt, sites, 0, n, &le, &re);
1202 }
1203 
1204 /**
1205  * Do a Delaunay Triangulation of the points in cdt.verts.
1206  * This  is only a first step in the Constrained Delaunay triangulation,
1207  * because it doesn't yet deal with the segment constraints.
1208  * The algorithm used is the Divide & Conquer algorithm from the
1209  * Guibas-Stolfi "Primitives for the Manipulation of General Subdivision
1210  * and the Computation of Voronoi Diagrams" paper.
1211  * The data structure here is similar to but not exactly the same as
1212  * the quad-edge structure described in that paper.
1213  * If T is not exact arithmetic, incircle and CCW tests are done using
1214  * Shewchuk's exact primitives, so that this routine is robust.
1215  *
1216  * As a preprocessing step, we want to merge all vertices that the same.
1217  * This is accomplished by lexicographically
1218  * sorting the coordinates first (which is needed anyway for the D&C algorithm).
1219  * The CDTVerts with merge_to_index not equal to -1 are after this regarded
1220  * as having been merged into the vertex with the corresponding index.
1221  */
initial_triangulation(CDTArrangement<T> * cdt)1222 template<typename T> void initial_triangulation(CDTArrangement<T> *cdt)
1223 {
1224   int n = cdt->verts.size();
1225   if (n <= 1) {
1226     return;
1227   }
1228   Array<SiteInfo<T>> sites(n);
1229   for (int i = 0; i < n; ++i) {
1230     sites[i].v = cdt->verts[i];
1231     sites[i].orig_index = i;
1232   }
1233   std::sort(sites.begin(), sites.end(), site_lexicographic_sort<T>);
1234   find_site_merges(sites);
1235   dc_triangulate(cdt, sites);
1236 }
1237 
1238 /**
1239  * Re-triangulates, assuring constrained delaunay condition,
1240  * the pseudo-polygon that cycles from se.
1241  * "pseudo" because a vertex may be repeated.
1242  * See Anglada paper, "An Improved incremental algorithm
1243  * for constructing restricted Delaunay triangulations".
1244  */
re_delaunay_triangulate(CDTArrangement<T> * cdt,SymEdge<T> * se)1245 template<typename T> static void re_delaunay_triangulate(CDTArrangement<T> *cdt, SymEdge<T> *se)
1246 {
1247   if (se->face == cdt->outer_face || sym(se)->face == cdt->outer_face) {
1248     return;
1249   }
1250   /* 'se' is a diagonal just added, and it is base of area to retriangulate (face on its left) */
1251   int count = 1;
1252   for (SymEdge<T> *ss = se->next; ss != se; ss = ss->next) {
1253     count++;
1254   }
1255   if (count <= 3) {
1256     return;
1257   }
1258   /* First and last are the SymEdges whose verts are first and last off of base,
1259    * continuing from 'se'. */
1260   SymEdge<T> *first = se->next->next;
1261   /* We want to make a triangle with 'se' as base and some other c as 3rd vertex. */
1262   CDTVert<T> *a = se->vert;
1263   CDTVert<T> *b = se->next->vert;
1264   CDTVert<T> *c = first->vert;
1265   SymEdge<T> *cse = first;
1266   for (SymEdge<T> *ss = first->next; ss != se; ss = ss->next) {
1267     CDTVert<T> *v = ss->vert;
1268     if (incircle(a->co, b->co, c->co, v->co) > 0) {
1269       c = v;
1270       cse = ss;
1271     }
1272   }
1273   /* Add diagonals necessary to make abc a triangle. */
1274   CDTEdge<T> *ebc = nullptr;
1275   CDTEdge<T> *eca = nullptr;
1276   if (!exists_edge(b, c)) {
1277     ebc = cdt->add_diagonal(se->next, cse);
1278   }
1279   if (!exists_edge(c, a)) {
1280     eca = cdt->add_diagonal(cse, se);
1281   }
1282   /* Now recurse. */
1283   if (ebc) {
1284     re_delaunay_triangulate(cdt, &ebc->symedges[1]);
1285   }
1286   if (eca) {
1287     re_delaunay_triangulate(cdt, &eca->symedges[1]);
1288   }
1289 }
1290 
tri_orient(const SymEdge<T> * t)1291 template<typename T> inline int tri_orient(const SymEdge<T> *t)
1292 {
1293   return orient2d(t->vert->co, t->next->vert->co, t->next->next->vert->co);
1294 }
1295 
1296 /**
1297  * The #CrossData class defines either an endpoint or an intermediate point
1298  * in the path we will take to insert an edge constraint.
1299  * Each such point will either be
1300  * (a) a vertex or
1301  * (b) a fraction lambda (0 < lambda < 1) along some #SymEdge.]
1302  *
1303  * In general, lambda=0 indicates case a and lambda != 0 indicates case be.
1304  * The 'in' edge gives the destination attachment point of a diagonal from the previous crossing,
1305  * and the 'out' edge gives the origin attachment point of a diagonal to the next crossing.
1306  * But in some cases, 'in' and 'out' are undefined or not needed, and will be NULL.
1307  *
1308  * For case (a), 'vert' will be the vertex, and lambda will be 0, and 'in' will be the #SymEdge
1309  * from 'vert' that has as face the one that you go through to get to this vertex. If you go
1310  * exactly along an edge then we set 'in' to NULL, since it won't be needed. The first crossing
1311  * will have 'in' = NULL. We set 'out' to the #SymEdge that has the face we go through to get to the
1312  * next crossing, or, if the next crossing is a case (a), then it is the edge that goes to that
1313  * next vertex. 'out' will be NULL for the last one.
1314  *
1315  * For case (b), vert will be NULL at first, and later filled in with the created split vertex,
1316  * and 'in' will be the #SymEdge that we go through, and lambda will be between 0 and 1,
1317  * the fraction from in's vert to in->next's vert to put the split vertex.
1318  * 'out' is not needed in this case, since the attachment point will be the sym of the first
1319  * half of the split edge.
1320  */
1321 template<typename T> class CrossData {
1322  public:
1323   T lambda = T(0);
1324   CDTVert<T> *vert;
1325   SymEdge<T> *in;
1326   SymEdge<T> *out;
1327 
CrossData()1328   CrossData() : lambda(T(0)), vert(nullptr), in(nullptr), out(nullptr)
1329   {
1330   }
CrossData(T l,CDTVert<T> * v,SymEdge<T> * i,SymEdge<T> * o)1331   CrossData(T l, CDTVert<T> *v, SymEdge<T> *i, SymEdge<T> *o) : lambda(l), vert(v), in(i), out(o)
1332   {
1333   }
CrossData(const CrossData & other)1334   CrossData(const CrossData &other)
1335       : lambda(other.lambda), vert(other.vert), in(other.in), out(other.out)
1336   {
1337   }
CrossData(CrossData && other)1338   CrossData(CrossData &&other) noexcept
1339       : lambda(std::move(other.lambda)),
1340         vert(std::move(other.vert)),
1341         in(std::move(other.in)),
1342         out(std::move(other.out))
1343   {
1344   }
1345   ~CrossData() = default;
operator =(const CrossData & other)1346   CrossData &operator=(const CrossData &other)
1347   {
1348     if (this != &other) {
1349       lambda = other.lambda;
1350       vert = other.vert;
1351       in = other.in;
1352       out = other.out;
1353     }
1354     return *this;
1355   }
operator =(CrossData && other)1356   CrossData &operator=(CrossData &&other) noexcept
1357   {
1358     lambda = std::move(other.lambda);
1359     vert = std::move(other.vert);
1360     in = std::move(other.in);
1361     out = std::move(other.out);
1362     return *this;
1363   }
1364 };
1365 
1366 template<typename T>
1367 bool get_next_crossing_from_vert(CDT_state<T> *cdt_state,
1368                                  CrossData<T> *cd,
1369                                  CrossData<T> *cd_next,
1370                                  const CDTVert<T> *v2);
1371 
1372 /**
1373  * As part of finding crossings, we found a case where the next crossing goes through vert v.
1374  * If it came from a previous vert in cd, then cd_out is the edge that leads from that to v.
1375  * Else cd_out can be NULL, because it won't be used.
1376  * Set *cd_next to indicate this. We can set 'in' but not 'out'.  We can set the 'out' of the
1377  * current cd.
1378  */
1379 template<typename T>
fill_crossdata_for_through_vert(CDTVert<T> * v,SymEdge<T> * cd_out,CrossData<T> * cd,CrossData<T> * cd_next)1380 void fill_crossdata_for_through_vert(CDTVert<T> *v,
1381                                      SymEdge<T> *cd_out,
1382                                      CrossData<T> *cd,
1383                                      CrossData<T> *cd_next)
1384 {
1385   SymEdge<T> *se;
1386 
1387   cd_next->lambda = T(0);
1388   cd_next->vert = v;
1389   cd_next->in = NULL;
1390   cd_next->out = NULL;
1391   if (cd->lambda == 0) {
1392     cd->out = cd_out;
1393   }
1394   else {
1395     /* One of the edges in the triangle with edge sym(cd->in) contains v. */
1396     se = sym(cd->in);
1397     if (se->vert != v) {
1398       se = se->next;
1399       if (se->vert != v) {
1400         se = se->next;
1401       }
1402     }
1403     BLI_assert(se->vert == v);
1404     cd_next->in = se;
1405   }
1406 }
1407 
1408 /**
1409  * As part of finding crossings, we found a case where orient tests say that the next crossing
1410  * is on the #SymEdge t, while intersecting with the ray from \a curco to \a v2.
1411  * Find the intersection point and fill in the #CrossData for that point.
1412  * It may turn out that when doing the intersection, we get an answer that says that
1413  * this case is better handled as through-vertex case instead, so we may do that.
1414  * In the latter case, we want to avoid a situation where the current crossing is on an edge
1415  * and the next will be an endpoint of the same edge. When that happens, we "rewrite history"
1416  * and turn the current crossing into a vert one, and then extend from there.
1417  *
1418  * We cannot fill cd_next's 'out' edge yet, in the case that the next one ends up being a vert
1419  * case. We need to fill in cd's 'out' edge if it was a vert case.
1420  */
1421 template<typename T>
fill_crossdata_for_intersect(const vec2<T> & curco,const CDTVert<T> * v2,SymEdge<T> * t,CrossData<T> * cd,CrossData<T> * cd_next,const T epsilon)1422 void fill_crossdata_for_intersect(const vec2<T> &curco,
1423                                   const CDTVert<T> *v2,
1424                                   SymEdge<T> *t,
1425                                   CrossData<T> *cd,
1426                                   CrossData<T> *cd_next,
1427                                   const T epsilon)
1428 {
1429   CDTVert<T> *va = t->vert;
1430   CDTVert<T> *vb = t->next->vert;
1431   CDTVert<T> *vc = t->next->next->vert;
1432   SymEdge<T> *se_vcvb = sym(t->next);
1433   SymEdge<T> *se_vcva = t->next->next;
1434   BLI_assert(se_vcva->vert == vc && se_vcva->next->vert == va);
1435   BLI_assert(se_vcvb->vert == vc && se_vcvb->next->vert == vb);
1436   UNUSED_VARS_NDEBUG(vc);
1437   auto isect = vec2<T>::isect_seg_seg(va->co, vb->co, curco, v2->co);
1438   T &lambda = isect.lambda;
1439   switch (isect.kind) {
1440     case vec2<T>::isect_result::LINE_LINE_CROSS: {
1441 #ifdef WITH_GMP
1442       if (!std::is_same<T, mpq_class>::value) {
1443 #else
1444       if (true) {
1445 #endif
1446         T len_ab = vec2<T>::distance(va->co, vb->co);
1447         if (lambda * len_ab <= epsilon) {
1448           fill_crossdata_for_through_vert(va, se_vcva, cd, cd_next);
1449         }
1450         else if ((1 - lambda) * len_ab <= epsilon) {
1451           fill_crossdata_for_through_vert(vb, se_vcvb, cd, cd_next);
1452         }
1453         else {
1454           *cd_next = CrossData<T>(lambda, nullptr, t, nullptr);
1455           if (cd->lambda == 0) {
1456             cd->out = se_vcva;
1457           }
1458         }
1459       }
1460       else {
1461         *cd_next = CrossData<T>(lambda, nullptr, t, nullptr);
1462         if (cd->lambda == 0) {
1463           cd->out = se_vcva;
1464         }
1465       }
1466       break;
1467     }
1468     case vec2<T>::isect_result::LINE_LINE_EXACT: {
1469       if (lambda == 0) {
1470         fill_crossdata_for_through_vert(va, se_vcva, cd, cd_next);
1471       }
1472       else if (lambda == 1) {
1473         fill_crossdata_for_through_vert(vb, se_vcvb, cd, cd_next);
1474       }
1475       else {
1476         *cd_next = CrossData<T>(lambda, nullptr, t, nullptr);
1477         if (cd->lambda == 0) {
1478           cd->out = se_vcva;
1479         }
1480       }
1481       break;
1482     }
1483     case vec2<T>::isect_result::LINE_LINE_NONE: {
1484 #ifdef WITH_GMP
1485       if (std::is_same<T, mpq_class>::value) {
1486         BLI_assert(false);
1487       }
1488 #endif
1489       /* It should be very near one end or other of segment. */
1490       const T middle_lambda = 0.5;
1491       if (lambda <= middle_lambda) {
1492         fill_crossdata_for_through_vert(va, se_vcva, cd, cd_next);
1493       }
1494       else {
1495         fill_crossdata_for_through_vert(vb, se_vcvb, cd, cd_next);
1496       }
1497       break;
1498     }
1499     case vec2<T>::isect_result::LINE_LINE_COLINEAR: {
1500       if (vec2<T>::distance_squared(va->co, v2->co) <= vec2<T>::distance_squared(vb->co, v2->co)) {
1501         fill_crossdata_for_through_vert(va, se_vcva, cd, cd_next);
1502       }
1503       else {
1504         fill_crossdata_for_through_vert(vb, se_vcvb, cd, cd_next);
1505       }
1506       break;
1507     }
1508   }
1509 }  // namespace blender::meshintersect
1510 
1511 /**
1512  * As part of finding the crossings of a ray to v2, find the next crossing after 'cd', assuming
1513  * 'cd' represents a crossing that goes through a vertex.
1514  *
1515  * We do a rotational scan around cd's vertex, looking for the triangle where the ray from cd->vert
1516  * to v2 goes between the two arms from cd->vert, or where it goes along one of the edges.
1517  */
1518 template<typename T>
1519 bool get_next_crossing_from_vert(CDT_state<T> *cdt_state,
1520                                  CrossData<T> *cd,
1521                                  CrossData<T> *cd_next,
1522                                  const CDTVert<T> *v2)
1523 {
1524   SymEdge<T> *tstart = cd->vert->symedge;
1525   SymEdge<T> *t = tstart;
1526   CDTVert<T> *vcur = cd->vert;
1527   bool ok = false;
1528   do {
1529     /* The ray from `vcur` to v2 has to go either between two successive
1530      * edges around `vcur` or exactly along them. This time through the
1531      * loop, check to see if the ray goes along `vcur-va`
1532      * or between `vcur-va` and `vcur-vb`, where va is the end of t
1533      * and vb is the next vertex (on the next rot edge around vcur, but
1534      * should also be the next vert of triangle starting with `vcur-va`. */
1535     if (t->face != cdt_state->cdt.outer_face && tri_orient(t) < 0) {
1536       BLI_assert(false); /* Shouldn't happen. */
1537     }
1538     CDTVert<T> *va = t->next->vert;
1539     CDTVert<T> *vb = t->next->next->vert;
1540     int orient1 = orient2d(t->vert->co, va->co, v2->co);
1541     if (orient1 == 0 && in_line<T>(vcur->co, va->co, v2->co)) {
1542       fill_crossdata_for_through_vert(va, t, cd, cd_next);
1543       ok = true;
1544       break;
1545     }
1546     if (t->face != cdt_state->cdt.outer_face) {
1547       int orient2 = orient2d(vcur->co, vb->co, v2->co);
1548       /* Don't handle orient2 == 0 case here: next rotation will get it. */
1549       if (orient1 > 0 && orient2 < 0) {
1550         /* Segment intersection. */
1551         t = t->next;
1552         fill_crossdata_for_intersect(vcur->co, v2, t, cd, cd_next, cdt_state->epsilon);
1553         ok = true;
1554         break;
1555       }
1556     }
1557   } while ((t = t->rot) != tstart);
1558   return ok;
1559 }
1560 
1561 /**
1562  * As part of finding the crossings of a ray to `v2`, find the next crossing after 'cd', assuming
1563  * 'cd' represents a crossing that goes through a an edge, not at either end of that edge.
1564  *
1565  * We have the triangle `vb-va-vc`, where `va` and vb are the split edge and `vc` is the third
1566  * vertex on that new side of the edge (should be closer to `v2`).
1567  * The next crossing should be through `vc` or intersecting `vb-vc` or `va-vc`.
1568  */
1569 template<typename T>
1570 void get_next_crossing_from_edge(CrossData<T> *cd,
1571                                  CrossData<T> *cd_next,
1572                                  const CDTVert<T> *v2,
1573                                  const T epsilon)
1574 {
1575   CDTVert<T> *va = cd->in->vert;
1576   CDTVert<T> *vb = cd->in->next->vert;
1577   vec2<T> curco = vec2<T>::interpolate(va->co, vb->co, cd->lambda);
1578   SymEdge<T> *se_ac = sym(cd->in)->next;
1579   CDTVert<T> *vc = se_ac->next->vert;
1580   int orient = orient2d(curco, v2->co, vc->co);
1581   if (orient < 0) {
1582     fill_crossdata_for_intersect<T>(curco, v2, se_ac->next, cd, cd_next, epsilon);
1583   }
1584   else if (orient > 0.0) {
1585     fill_crossdata_for_intersect(curco, v2, se_ac, cd, cd_next, epsilon);
1586   }
1587   else {
1588     *cd_next = CrossData<T>{0.0, vc, se_ac->next, nullptr};
1589   }
1590 }
1591 
1592 constexpr int inline_crossings_size = 128;
1593 template<typename T>
1594 void dump_crossings(const Vector<CrossData<T>, inline_crossings_size> &crossings)
1595 {
1596   std::cout << "CROSSINGS\n";
1597   for (int i = 0; i < crossings.size(); ++i) {
1598     std::cout << i << ": ";
1599     const CrossData<T> &cd = crossings[i];
1600     if (cd.lambda == 0) {
1601       std::cout << "v" << cd.vert->index;
1602     }
1603     else {
1604       std::cout << "lambda=" << cd.lambda;
1605     }
1606     if (cd.in != nullptr) {
1607       std::cout << " in=" << short_se_dump(cd.in);
1608       std::cout << " out=" << short_se_dump(cd.out);
1609     }
1610     std::cout << "\n";
1611   }
1612 }
1613 
1614 /**
1615  * Add a constrained edge between v1 and v2 to cdt structure.
1616  * This may result in a number of #CDTEdges created, due to intersections
1617  * and partial overlaps with existing cdt vertices and edges.
1618  * Each created #CDTEdge will have input_id added to its input_ids list.
1619  *
1620  * If \a r_edges is not NULL, the #CDTEdges generated or found that go from
1621  * v1 to v2 are put into that linked list, in order.
1622  *
1623  * Assumes that #blender_constrained_delaunay_get_output has not been called yet.
1624  */
1625 template<typename T>
1626 void add_edge_constraint(
1627     CDT_state<T> *cdt_state, CDTVert<T> *v1, CDTVert<T> *v2, int input_id, LinkNode **r_edges)
1628 {
1629   constexpr int dbg_level = 0;
1630   if (dbg_level > 0) {
1631     std::cout << "\nADD EDGE CONSTRAINT\n" << vertname(v1) << " " << vertname(v2) << "\n";
1632   }
1633   LinkNodePair edge_list = {NULL, NULL};
1634 
1635   if (r_edges) {
1636     *r_edges = NULL;
1637   }
1638 
1639   /*
1640    * Handle two special cases first:
1641    * 1) The two end vertices are the same (can happen because of merging).
1642    * 2) There is already an edge between v1 and v2.
1643    */
1644   if (v1 == v2) {
1645     return;
1646   }
1647   SymEdge<T> *t = find_symedge_between_verts(v1, v2);
1648   if (t != nullptr) {
1649     /* Segment already there. */
1650     add_to_input_ids(&t->edge->input_ids, input_id);
1651     if (r_edges != NULL) {
1652       BLI_linklist_append(&edge_list, t->edge);
1653       *r_edges = edge_list.list;
1654     }
1655     return;
1656   }
1657 
1658   /*
1659    * Fill crossings array with CrossData points for intersection path from v1 to v2.
1660    *
1661    * At every point, the crossings array has the path so far, except that
1662    * the .out field of the last element of it may not be known yet -- if that
1663    * last element is a vertex, then we won't know the output edge until we
1664    * find the next crossing.
1665    *
1666    * To protect against infinite loops, we keep track of which vertices
1667    * we have visited by setting their visit_index to a new visit epoch.
1668    *
1669    * We check a special case first: where the segment is already there in
1670    * one hop. Saves a bunch of orient2d tests in that common case.
1671    */
1672   int visit = ++cdt_state->visit_count;
1673   Vector<CrossData<T>, inline_crossings_size> crossings;
1674   crossings.append(CrossData<T>(T(0), v1, nullptr, nullptr));
1675   int n;
1676   while (!((n = crossings.size()) > 0 && crossings[n - 1].vert == v2)) {
1677     crossings.append(CrossData<T>());
1678     CrossData<T> *cd = &crossings[n - 1];
1679     CrossData<T> *cd_next = &crossings[n];
1680     bool ok;
1681     if (crossings[n - 1].lambda == 0) {
1682       ok = get_next_crossing_from_vert(cdt_state, cd, cd_next, v2);
1683     }
1684     else {
1685       get_next_crossing_from_edge(cd, cd_next, v2, cdt_state->epsilon);
1686       ok = true;
1687     }
1688     constexpr int unreasonably_large_crossings = 100000;
1689     if (!ok || crossings.size() == unreasonably_large_crossings) {
1690       /* Shouldn't happen but if does, just bail out. */
1691       BLI_assert(false);
1692       return;
1693     }
1694     if (crossings[n].lambda == 0) {
1695       if (crossings[n].vert->visit_index == visit) {
1696         /* Shouldn't happen but if it does, just bail out. */
1697         BLI_assert(false);
1698         return;
1699       }
1700       crossings[n].vert->visit_index = visit;
1701     }
1702   }
1703 
1704   if (dbg_level > 0) {
1705     dump_crossings(crossings);
1706   }
1707 
1708   /*
1709    * Post-process crossings.
1710    * Some crossings may have an intersection crossing followed
1711    * by a vertex crossing that is on the same edge that was just
1712    * intersected. We prefer to go directly from the previous
1713    * crossing directly to the vertex. This may chain backwards.
1714    *
1715    * This loop marks certain crossings as "deleted", by setting
1716    * their lambdas to -1.0.
1717    */
1718   int ncrossings = crossings.size();
1719   for (int i = 2; i < ncrossings; ++i) {
1720     CrossData<T> *cd = &crossings[i];
1721     if (cd->lambda == 0.0) {
1722       CDTVert<T> *v = cd->vert;
1723       int j;
1724       CrossData<T> *cd_prev;
1725       for (j = i - 1; j > 0; --j) {
1726         cd_prev = &crossings[j];
1727         if ((cd_prev->lambda == 0.0 && cd_prev->vert != v) ||
1728             (cd_prev->lambda != 0.0 && cd_prev->in->vert != v && cd_prev->in->next->vert != v)) {
1729           break;
1730         }
1731         cd_prev->lambda = -1.0; /* Mark cd_prev as 'deleted'. */
1732       }
1733       if (j < i - 1) {
1734         /* Some crossings were deleted. Fix the in and out edges across gap. */
1735         cd_prev = &crossings[j];
1736         SymEdge<T> *se;
1737         if (cd_prev->lambda == 0.0) {
1738           se = find_symedge_between_verts(cd_prev->vert, v);
1739           if (se == NULL) {
1740             return;
1741           }
1742           cd_prev->out = se;
1743           cd->in = NULL;
1744         }
1745         else {
1746           se = find_symedge_with_face(v, sym(cd_prev->in)->face);
1747           if (se == NULL) {
1748             return;
1749           }
1750           cd->in = se;
1751         }
1752       }
1753     }
1754   }
1755 
1756   /*
1757    * Insert all intersection points on constrained edges.
1758    */
1759   for (int i = 0; i < ncrossings; ++i) {
1760     CrossData<T> *cd = &crossings[i];
1761     if (cd->lambda != 0.0 && cd->lambda != -1.0 && is_constrained_edge(cd->in->edge)) {
1762       CDTEdge<T> *edge = cdt_state->cdt.split_edge(cd->in, cd->lambda);
1763       cd->vert = edge->symedges[0].vert;
1764     }
1765   }
1766 
1767   /*
1768    * Remove any crossed, non-intersected edges.
1769    */
1770   for (int i = 0; i < ncrossings; ++i) {
1771     CrossData<T> *cd = &crossings[i];
1772     if (cd->lambda != 0.0 && cd->lambda != -1.0 && !is_constrained_edge(cd->in->edge)) {
1773       cdt_state->cdt.delete_edge(cd->in);
1774     }
1775   }
1776 
1777   /*
1778    * Insert segments for v1->v2.
1779    */
1780   SymEdge<T> *tstart = crossings[0].out;
1781   for (int i = 1; i < ncrossings; i++) {
1782     CrossData<T> *cd = &crossings[i];
1783     if (cd->lambda == -1.0) {
1784       continue; /* This crossing was deleted. */
1785     }
1786     t = NULL;
1787     SymEdge<T> *tnext = t;
1788     CDTEdge<T> *edge;
1789     if (cd->lambda != 0.0) {
1790       if (is_constrained_edge(cd->in->edge)) {
1791         t = cd->vert->symedge;
1792         tnext = sym(t)->next;
1793       }
1794     }
1795     else if (cd->lambda == 0.0) {
1796       t = cd->in;
1797       tnext = cd->out;
1798       if (t == NULL) {
1799         /* Previous non-deleted crossing must also have been a vert, and segment should exist. */
1800         int j;
1801         CrossData<T> *cd_prev;
1802         for (j = i - 1; j >= 0; j--) {
1803           cd_prev = &crossings[j];
1804           if (cd_prev->lambda != -1.0) {
1805             break;
1806           }
1807         }
1808         BLI_assert(cd_prev->lambda == 0.0);
1809         BLI_assert(cd_prev->out->next->vert == cd->vert);
1810         edge = cd_prev->out->edge;
1811         add_to_input_ids(&edge->input_ids, input_id);
1812         if (r_edges != NULL) {
1813           BLI_linklist_append(&edge_list, edge);
1814         }
1815       }
1816     }
1817     if (t != NULL) {
1818       if (tstart->next->vert == t->vert) {
1819         edge = tstart->edge;
1820       }
1821       else {
1822         edge = cdt_state->cdt.add_diagonal(tstart, t);
1823       }
1824       add_to_input_ids(&edge->input_ids, input_id);
1825       if (r_edges != NULL) {
1826         BLI_linklist_append(&edge_list, edge);
1827       }
1828       /* Now retriangulate upper and lower gaps. */
1829       re_delaunay_triangulate(&cdt_state->cdt, &edge->symedges[0]);
1830       re_delaunay_triangulate(&cdt_state->cdt, &edge->symedges[1]);
1831     }
1832     if (i < ncrossings - 1) {
1833       if (tnext != NULL) {
1834         tstart = tnext;
1835       }
1836     }
1837   }
1838 
1839   if (r_edges) {
1840     *r_edges = edge_list.list;
1841   }
1842 }
1843 
1844 /**
1845  * Incrementally add edge input edge as a constraint. This may cause the graph structure
1846  * to change, in cases where the constraints intersect existing edges.
1847  * The code will ensure that #CDTEdge's created will have ids that tie them back
1848  * to the original edge constraint index.
1849  */
1850 template<typename T> void add_edge_constraints(CDT_state<T> *cdt_state, const CDT_input<T> &input)
1851 {
1852   int ne = input.edge.size();
1853   int nv = input.vert.size();
1854   for (int i = 0; i < ne; i++) {
1855     int iv1 = input.edge[i].first;
1856     int iv2 = input.edge[i].second;
1857     if (iv1 < 0 || iv1 >= nv || iv2 < 0 || iv2 >= nv) {
1858       /* Ignore invalid indices in edges. */
1859       continue;
1860     }
1861     CDTVert<T> *v1 = cdt_state->cdt.get_vert_resolve_merge(iv1);
1862     CDTVert<T> *v2 = cdt_state->cdt.get_vert_resolve_merge(iv2);
1863     add_edge_constraint(cdt_state, v1, v2, i, nullptr);
1864   }
1865   cdt_state->face_edge_offset = ne;
1866 }
1867 
1868 /**
1869  * Add face_id to the input_ids lists of all #CDTFace's on the interior of the input face with that
1870  * id. face_symedge is on edge of the boundary of the input face, with assumption that interior is
1871  * on the left of that #SymEdge.
1872  *
1873  * The algorithm is: starting from the #CDTFace for face_symedge, add the face_id and then
1874  * process all adjacent faces where the adjacency isn't across an edge that was a constraint added
1875  * for the boundary of the input face.
1876  * fedge_start..fedge_end is the inclusive range of edge input ids that are for the given face.
1877  *
1878  * Note: if the input face is not CCW oriented, we'll be labeling the outside, not the inside.
1879  * Note 2: if the boundary has self-crossings, this method will arbitrarily pick one of the
1880  * contiguous set of faces enclosed by parts of the boundary, leaving the other such un-tagged.
1881  * This may be a feature instead of a bug if the first contiguous section is most of the face and
1882  * the others are tiny self-crossing triangles at some parts of the boundary.
1883  * On the other hand, if decide we want to handle these in full generality, then will need a more
1884  * complicated algorithm (using "inside" tests and a parity rule) to decide on the interior.
1885  */
1886 template<typename T>
1887 void add_face_ids(
1888     CDT_state<T> *cdt_state, SymEdge<T> *face_symedge, int face_id, int fedge_start, int fedge_end)
1889 {
1890   /* Can't loop forever since eventually would visit every face. */
1891   cdt_state->visit_count++;
1892   int visit = cdt_state->visit_count;
1893   Vector<SymEdge<T> *> stack;
1894   stack.append(face_symedge);
1895   while (!stack.is_empty()) {
1896     SymEdge<T> *se = stack.pop_last();
1897     CDTFace<T> *face = se->face;
1898     if (face->visit_index == visit) {
1899       continue;
1900     }
1901     face->visit_index = visit;
1902     add_to_input_ids(&face->input_ids, face_id);
1903     SymEdge<T> *se_start = se;
1904     for (se = se->next; se != se_start; se = se->next) {
1905       if (!id_range_in_list(se->edge->input_ids, fedge_start, fedge_end)) {
1906         SymEdge<T> *se_sym = sym(se);
1907         CDTFace<T> *face_other = se_sym->face;
1908         if (face_other->visit_index != visit) {
1909           stack.append(se_sym);
1910         }
1911       }
1912     }
1913   }
1914 }
1915 
1916 /* Return a power of 10 that is greater than or equal to x. */
1917 static int power_of_10_greater_equal_to(int x)
1918 {
1919   if (x <= 0) {
1920     return 1;
1921   }
1922   int ans = 1;
1923   BLI_assert(x < INT_MAX / 10);
1924   while (ans < x) {
1925     ans *= 10;
1926   }
1927   return ans;
1928 }
1929 
1930 /**
1931    Incrementally each edge of each input face as an edge constraint.
1932  * The code will ensure that the #CDTEdge's created will have ids that tie them
1933  * back to the original face edge (using a numbering system for those edges
1934  * that starts with cdt->face_edge_offset, and continues with the edges in
1935  * order around each face in turn. And then the next face starts at
1936  * cdt->face_edge_offset beyond the start for the previous face.
1937  */
1938 template<typename T> void add_face_constraints(CDT_state<T> *cdt_state, const CDT_input<T> &input)
1939 {
1940   int nv = input.vert.size();
1941   int nf = input.face.size();
1942   int fstart = 0;
1943   SymEdge<T> *face_symedge0 = nullptr;
1944   CDTArrangement<T> *cdt = &cdt_state->cdt;
1945   int maxflen = 0;
1946   for (int f = 0; f < nf; f++) {
1947     maxflen = max_ii(maxflen, input.face[f].size());
1948   }
1949   /* For convenience in debugging, make face_edge_offset be a power of 10. */
1950   cdt_state->face_edge_offset = power_of_10_greater_equal_to(
1951       max_ii(maxflen, cdt_state->face_edge_offset));
1952   /* The original_edge encoding scheme doesn't work if the following is false.
1953    * If we really have that many faces and that large a max face length that when multiplied
1954    * together the are >= INT_MAX, then the Delaunay calculation will take unreasonably long anyway.
1955    */
1956   BLI_assert(INT_MAX / cdt_state->face_edge_offset > nf);
1957   for (int f = 0; f < nf; f++) {
1958     int flen = input.face[f].size();
1959     if (flen <= 2) {
1960       /* Ignore faces with fewer than 3 vertices. */
1961       fstart += flen;
1962       continue;
1963     }
1964     int fedge_start = (f + 1) * cdt_state->face_edge_offset;
1965     for (int i = 0; i < flen; i++) {
1966       int face_edge_id = fedge_start + i;
1967       int iv1 = input.face[f][i];
1968       int iv2 = input.face[f][(i + 1) % flen];
1969       if (iv1 < 0 || iv1 >= nv || iv2 < 0 || iv2 >= nv) {
1970         /* Ignore face edges with invalid vertices. */
1971         continue;
1972       }
1973       CDTVert<T> *v1 = cdt->get_vert_resolve_merge(iv1);
1974       CDTVert<T> *v2 = cdt->get_vert_resolve_merge(iv2);
1975       LinkNode *edge_list;
1976       add_edge_constraint(cdt_state, v1, v2, face_edge_id, &edge_list);
1977       /* Set a new face_symedge0 each time since earlier ones may not
1978        * survive later symedge splits. Really, just want the one when
1979        * i == flen -1, but this code guards against that one somehow
1980        * being null.
1981        */
1982       if (edge_list != nullptr) {
1983         CDTEdge<T> *face_edge = static_cast<CDTEdge<T> *>(edge_list->link);
1984         face_symedge0 = &face_edge->symedges[0];
1985         if (face_symedge0->vert != v1) {
1986           face_symedge0 = &face_edge->symedges[1];
1987           BLI_assert(face_symedge0->vert == v1);
1988         }
1989       }
1990       BLI_linklist_free(edge_list, nullptr);
1991     }
1992     int fedge_end = fedge_start + flen - 1;
1993     if (face_symedge0 != nullptr) {
1994       add_face_ids(cdt_state, face_symedge0, f, fedge_start, fedge_end);
1995     }
1996     fstart += flen;
1997   }
1998 }
1999 
2000 /* Delete_edge but try not to mess up outer face.
2001  * Also faces have symedges now, so make sure not
2002  * to mess those up either. */
2003 template<typename T> void dissolve_symedge(CDT_state<T> *cdt_state, SymEdge<T> *se)
2004 {
2005   CDTArrangement<T> *cdt = &cdt_state->cdt;
2006   SymEdge<T> *symse = sym(se);
2007   if (symse->face == cdt->outer_face) {
2008     se = sym(se);
2009     symse = sym(se);
2010   }
2011   if (cdt->outer_face->symedge == se || cdt->outer_face->symedge == symse) {
2012     /* Advancing by 2 to get past possible 'sym(se)'. */
2013     if (se->next->next == se) {
2014       cdt->outer_face->symedge = NULL;
2015     }
2016     else {
2017       cdt->outer_face->symedge = se->next->next;
2018     }
2019   }
2020   else {
2021     if (se->face->symedge == se) {
2022       se->face->symedge = se->next;
2023     }
2024     if (symse->face->symedge == symse) {
2025       symse->face->symedge = symse->next;
2026     }
2027   }
2028   cdt->delete_edge(se);
2029 }
2030 
2031 /**
2032  * Remove all non-constraint edges.
2033  */
2034 template<typename T> void remove_non_constraint_edges(CDT_state<T> *cdt_state)
2035 {
2036   for (CDTEdge<T> *e : cdt_state->cdt.edges) {
2037     SymEdge<T> *se = &e->symedges[0];
2038     if (!is_deleted_edge(e) && !is_constrained_edge(e)) {
2039       dissolve_symedge(cdt_state, se);
2040     }
2041   }
2042 }
2043 
2044 /*
2045  * Remove the non-constraint edges, but leave enough of them so that all of the
2046  * faces that would be #BMesh faces (that is, the faces that have some input representative)
2047  * are valid: they can't have holes, they can't have repeated vertices, and they can't have
2048  * repeated edges.
2049  *
2050  * Not essential, but to make the result look more aesthetically nice,
2051  * remove the edges in order of decreasing length, so that it is more likely that the
2052  * final remaining support edges are short, and therefore likely to make a fairly
2053  * direct path from an outer face to an inner hole face.
2054  */
2055 
2056 /**
2057  * For sorting edges by decreasing length (squared).
2058  */
2059 template<typename T> struct EdgeToSort {
2060   T len_squared = T(0);
2061   CDTEdge<T> *e{nullptr};
2062 
2063   EdgeToSort() = default;
2064   EdgeToSort(const EdgeToSort &other) : len_squared(other.len_squared), e(other.e)
2065   {
2066   }
2067   EdgeToSort(EdgeToSort &&other) noexcept : len_squared(std::move(other.len_squared)), e(other.e)
2068   {
2069   }
2070   ~EdgeToSort() = default;
2071   EdgeToSort &operator=(const EdgeToSort &other)
2072   {
2073     if (this != &other) {
2074       len_squared = other.len_squared;
2075       e = other.e;
2076     }
2077     return *this;
2078   }
2079   EdgeToSort &operator=(EdgeToSort &&other)
2080   {
2081     len_squared = std::move(other.len_squared);
2082     e = other.e;
2083     return *this;
2084   }
2085 };
2086 
2087 template<typename T> void remove_non_constraint_edges_leave_valid_bmesh(CDT_state<T> *cdt_state)
2088 {
2089   CDTArrangement<T> *cdt = &cdt_state->cdt;
2090   size_t nedges = cdt->edges.size();
2091   if (nedges == 0) {
2092     return;
2093   }
2094   Vector<EdgeToSort<T>> dissolvable_edges;
2095   dissolvable_edges.reserve(cdt->edges.size());
2096   int i = 0;
2097   for (CDTEdge<T> *e : cdt->edges) {
2098     if (!is_deleted_edge(e) && !is_constrained_edge(e)) {
2099       dissolvable_edges.append(EdgeToSort<T>());
2100       dissolvable_edges[i].e = e;
2101       const vec2<T> &co1 = e->symedges[0].vert->co;
2102       const vec2<T> &co2 = e->symedges[1].vert->co;
2103       dissolvable_edges[i].len_squared = vec2<T>::distance_squared(co1, co2);
2104       i++;
2105     }
2106   }
2107   std::sort(dissolvable_edges.begin(),
2108             dissolvable_edges.end(),
2109             [](const EdgeToSort<T> &a, const EdgeToSort<T> &b) -> bool {
2110               return (a.len_squared < b.len_squared);
2111             });
2112   for (EdgeToSort<T> &ets : dissolvable_edges) {
2113     CDTEdge<T> *e = ets.e;
2114     SymEdge<T> *se = &e->symedges[0];
2115     bool dissolve = true;
2116     CDTFace<T> *fleft = se->face;
2117     CDTFace<T> *fright = sym(se)->face;
2118     if (fleft != cdt->outer_face && fright != cdt->outer_face &&
2119         (fleft->input_ids != nullptr || fright->input_ids != nullptr)) {
2120       /* Is there another #SymEdge with same left and right faces?
2121        * Or is there a vertex not part of e touching the same left and right faces? */
2122       for (SymEdge<T> *se2 = se->next; dissolve && se2 != se; se2 = se2->next) {
2123         if (sym(se2)->face == fright ||
2124             (se2->vert != se->next->vert && vert_touches_face(se2->vert, fright))) {
2125           dissolve = false;
2126         }
2127       }
2128     }
2129 
2130     if (dissolve) {
2131       dissolve_symedge(cdt_state, se);
2132     }
2133   }
2134 }
2135 
2136 template<typename T> void remove_outer_edges_until_constraints(CDT_state<T> *cdt_state)
2137 {
2138   // LinkNode *fstack = NULL;
2139   // SymEdge *se, *se_start;
2140   // CDTFace *f, *fsym;
2141   int visit = ++cdt_state->visit_count;
2142 
2143   cdt_state->cdt.outer_face->visit_index = visit;
2144   /* Walk around outer face, adding faces on other side of dissolvable edges to stack. */
2145   Vector<CDTFace<T> *> fstack;
2146   SymEdge<T> *se_start = cdt_state->cdt.outer_face->symedge;
2147   SymEdge<T> *se = se_start;
2148   do {
2149     if (!is_constrained_edge(se->edge)) {
2150       CDTFace<T> *fsym = sym(se)->face;
2151       if (fsym->visit_index != visit) {
2152         fstack.append(fsym);
2153       }
2154     }
2155   } while ((se = se->next) != se_start);
2156 
2157   while (!fstack.is_empty()) {
2158     LinkNode *to_dissolve = nullptr;
2159     bool dissolvable;
2160     CDTFace<T> *f = fstack.pop_last();
2161     if (f->visit_index == visit) {
2162       continue;
2163     }
2164     BLI_assert(f != cdt_state->cdt.outer_face);
2165     f->visit_index = visit;
2166     se_start = se = f->symedge;
2167     do {
2168       dissolvable = !is_constrained_edge(se->edge);
2169       if (dissolvable) {
2170         CDTFace<T> *fsym = sym(se)->face;
2171         if (fsym->visit_index != visit) {
2172           fstack.append(fsym);
2173         }
2174         else {
2175           BLI_linklist_prepend(&to_dissolve, se);
2176         }
2177       }
2178       se = se->next;
2179     } while (se != se_start);
2180     while (to_dissolve != NULL) {
2181       se = static_cast<SymEdge<T> *>(BLI_linklist_pop(&to_dissolve));
2182       if (se->next != NULL) {
2183         dissolve_symedge(cdt_state, se);
2184       }
2185     }
2186   }
2187 }
2188 
2189 /**
2190  * Remove edges and merge faces to get desired output, as per options.
2191  * \note the cdt cannot be further changed after this.
2192  */
2193 template<typename T>
2194 void prepare_cdt_for_output(CDT_state<T> *cdt_state, const CDT_output_type output_type)
2195 {
2196   CDTArrangement<T> *cdt = &cdt_state->cdt;
2197   if (cdt->edges.is_empty()) {
2198     return;
2199   }
2200 
2201   /* Make sure all non-deleted faces have a symedge. */
2202   for (CDTEdge<T> *e : cdt->edges) {
2203     if (!is_deleted_edge(e)) {
2204       if (e->symedges[0].face->symedge == nullptr) {
2205         e->symedges[0].face->symedge = &e->symedges[0];
2206       }
2207       if (e->symedges[1].face->symedge == nullptr) {
2208         e->symedges[1].face->symedge = &e->symedges[1];
2209       }
2210     }
2211   }
2212 
2213   if (output_type == CDT_CONSTRAINTS) {
2214     remove_non_constraint_edges(cdt_state);
2215   }
2216   else if (output_type == CDT_CONSTRAINTS_VALID_BMESH) {
2217     remove_non_constraint_edges_leave_valid_bmesh(cdt_state);
2218   }
2219   else if (output_type == CDT_INSIDE) {
2220     remove_outer_edges_until_constraints(cdt_state);
2221   }
2222 }
2223 
2224 template<typename T>
2225 CDT_result<T> get_cdt_output(CDT_state<T> *cdt_state,
2226                              const CDT_input<T> UNUSED(input),
2227                              CDT_output_type output_type)
2228 {
2229   prepare_cdt_for_output(cdt_state, output_type);
2230   CDT_result<T> result;
2231   CDTArrangement<T> *cdt = &cdt_state->cdt;
2232   result.face_edge_offset = cdt_state->face_edge_offset;
2233 
2234   /* All verts without a merge_to_index will be output.
2235    * vert_to_output_map[i] will hold the output vertex index
2236    * corresponding to the vert in position i in cdt->verts.
2237    * This first loop sets vert_to_output_map for un-merged verts. */
2238   int verts_size = cdt->verts.size();
2239   Array<int> vert_to_output_map(verts_size);
2240   int nv = 0;
2241   for (int i = 0; i < verts_size; ++i) {
2242     CDTVert<T> *v = cdt->verts[i];
2243     if (v->merge_to_index == -1) {
2244       vert_to_output_map[i] = nv;
2245       ++nv;
2246     }
2247   }
2248   if (nv <= 0) {
2249     return result;
2250   }
2251   /* Now we can set vert_to_output_map for merged verts,
2252    * and also add the input indices of merged verts to the input_ids
2253    * list of the merge target if they were an original input id. */
2254   if (nv < verts_size) {
2255     for (int i = 0; i < verts_size; ++i) {
2256       CDTVert<T> *v = cdt->verts[i];
2257       if (v->merge_to_index != -1) {
2258         if (i < cdt_state->input_vert_tot) {
2259           add_to_input_ids(&cdt->verts[v->merge_to_index]->input_ids, i);
2260         }
2261         vert_to_output_map[i] = vert_to_output_map[v->merge_to_index];
2262       }
2263     }
2264   }
2265   result.vert = Array<vec2<T>>(nv);
2266   result.vert_orig = Array<Vector<int>>(nv);
2267   int i_out = 0;
2268   for (int i = 0; i < verts_size; ++i) {
2269     CDTVert<T> *v = cdt->verts[i];
2270     if (v->merge_to_index == -1) {
2271       result.vert[i_out] = v->co;
2272       if (i < cdt_state->input_vert_tot) {
2273         result.vert_orig[i_out].append(i);
2274       }
2275       for (LinkNode *ln = v->input_ids; ln; ln = ln->next) {
2276         result.vert_orig[i_out].append(POINTER_AS_INT(ln->link));
2277       }
2278       ++i_out;
2279     }
2280   }
2281 
2282   /* All non-deleted edges will be output. */
2283   int ne = std::count_if(cdt->edges.begin(), cdt->edges.end(), [](const CDTEdge<T> *e) -> bool {
2284     return !is_deleted_edge(e);
2285   });
2286   result.edge = Array<std::pair<int, int>>(ne);
2287   result.edge_orig = Array<Vector<int>>(ne);
2288   int e_out = 0;
2289   for (const CDTEdge<T> *e : cdt->edges) {
2290     if (!is_deleted_edge(e)) {
2291       int vo1 = vert_to_output_map[e->symedges[0].vert->index];
2292       int vo2 = vert_to_output_map[e->symedges[1].vert->index];
2293       result.edge[e_out] = std::pair<int, int>(vo1, vo2);
2294       for (LinkNode *ln = e->input_ids; ln; ln = ln->next) {
2295         result.edge_orig[e_out].append(POINTER_AS_INT(ln->link));
2296       }
2297       ++e_out;
2298     }
2299   }
2300 
2301   /* All non-deleted, non-outer faces will be output. */
2302   int nf = std::count_if(cdt->faces.begin(), cdt->faces.end(), [=](const CDTFace<T> *f) -> bool {
2303     return !f->deleted && f != cdt->outer_face;
2304   });
2305   result.face = Array<Vector<int>>(nf);
2306   result.face_orig = Array<Vector<int>>(nf);
2307   int f_out = 0;
2308   for (const CDTFace<T> *f : cdt->faces) {
2309     if (!f->deleted && f != cdt->outer_face) {
2310       SymEdge<T> *se = f->symedge;
2311       BLI_assert(se != nullptr);
2312       SymEdge<T> *se_start = se;
2313       do {
2314         result.face[f_out].append(vert_to_output_map[se->vert->index]);
2315         se = se->next;
2316       } while (se != se_start);
2317       for (LinkNode *ln = f->input_ids; ln; ln = ln->next) {
2318         result.face_orig[f_out].append(POINTER_AS_INT(ln->link));
2319       }
2320       ++f_out;
2321     }
2322   }
2323   return result;
2324 }
2325 
2326 /**
2327  * Add all the input verts into cdt. This will deduplicate,
2328  * setting vertices merge_to_index to show merges.
2329  */
2330 template<typename T> void add_input_verts(CDT_state<T> *cdt_state, const CDT_input<T> &input)
2331 {
2332   for (int i = 0; i < cdt_state->input_vert_tot; ++i) {
2333     cdt_state->cdt.add_vert(input.vert[i]);
2334   }
2335 }
2336 
2337 template<typename T>
2338 CDT_result<T> delaunay_calc(const CDT_input<T> &input, CDT_output_type output_type)
2339 {
2340   int nv = input.vert.size();
2341   int ne = input.edge.size();
2342   int nf = input.face.size();
2343   CDT_state<T> cdt_state(nv, ne, nf, input.epsilon);
2344   add_input_verts(&cdt_state, input);
2345   initial_triangulation(&cdt_state.cdt);
2346   add_edge_constraints(&cdt_state, input);
2347   add_face_constraints(&cdt_state, input);
2348   return get_cdt_output(&cdt_state, input, output_type);
2349 }
2350 
2351 blender::meshintersect::CDT_result<double> delaunay_2d_calc(const CDT_input<double> &input,
2352                                                             CDT_output_type output_type)
2353 {
2354   return delaunay_calc(input, output_type);
2355 }
2356 
2357 #ifdef WITH_GMP
2358 blender::meshintersect::CDT_result<mpq_class> delaunay_2d_calc(const CDT_input<mpq_class> &input,
2359                                                                CDT_output_type output_type)
2360 {
2361   return delaunay_calc(input, output_type);
2362 }
2363 #endif
2364 
2365 } /* namespace blender::meshintersect */
2366 
2367 /* C interface. */
2368 
2369 /**
2370    This function uses the double version of #CDT::delaunay_calc.
2371  * Almost all of the work here is to convert between C++ #Arrays<Vector<int>>
2372  * and a C version that linearizes all the elements and uses a "start"
2373  * and "len" array to say where the individual vectors start and how
2374  * long they are.
2375  */
BLI_delaunay_2d_cdt_calc(const::CDT_input * input,const CDT_output_type output_type)2376 extern "C" ::CDT_result *BLI_delaunay_2d_cdt_calc(const ::CDT_input *input,
2377                                                   const CDT_output_type output_type)
2378 {
2379   blender::meshintersect::CDT_input<double> in;
2380   in.vert = blender::Array<blender::meshintersect::vec2<double>>(input->verts_len);
2381   in.edge = blender::Array<std::pair<int, int>>(input->edges_len);
2382   in.face = blender::Array<blender::Vector<int>>(input->faces_len);
2383   for (int v = 0; v < input->verts_len; ++v) {
2384     double x = static_cast<double>(input->vert_coords[v][0]);
2385     double y = static_cast<double>(input->vert_coords[v][1]);
2386     in.vert[v] = blender::meshintersect::vec2<double>(x, y);
2387   }
2388   for (int e = 0; e < input->edges_len; ++e) {
2389     in.edge[e] = std::pair<int, int>(input->edges[e][0], input->edges[e][1]);
2390   }
2391   for (int f = 0; f < input->faces_len; ++f) {
2392     in.face[f] = blender::Vector<int>(input->faces_len_table[f]);
2393     int fstart = input->faces_start_table[f];
2394     for (int j = 0; j < input->faces_len_table[f]; ++j) {
2395       in.face[f][j] = input->faces[fstart + j];
2396     }
2397   }
2398   in.epsilon = static_cast<double>(input->epsilon);
2399 
2400   blender::meshintersect::CDT_result<double> res = blender::meshintersect::delaunay_2d_calc(
2401       in, output_type);
2402 
2403   ::CDT_result *output = static_cast<::CDT_result *>(MEM_mallocN(sizeof(*output), __func__));
2404   int nv = output->verts_len = res.vert.size();
2405   int ne = output->edges_len = res.edge.size();
2406   int nf = output->faces_len = res.face.size();
2407   int tot_v_orig = 0;
2408   int tot_e_orig = 0;
2409   int tot_f_orig = 0;
2410   int tot_f_lens = 0;
2411   for (int v = 0; v < nv; ++v) {
2412     tot_v_orig += res.vert_orig[v].size();
2413   }
2414   for (int e = 0; e < ne; ++e) {
2415     tot_e_orig += res.edge_orig[e].size();
2416   }
2417   for (int f = 0; f < nf; ++f) {
2418     tot_f_orig += res.face_orig[f].size();
2419     tot_f_lens += res.face[f].size();
2420   }
2421 
2422   output->vert_coords = static_cast<decltype(output->vert_coords)>(
2423       MEM_malloc_arrayN(nv, sizeof(output->vert_coords[0]), __func__));
2424   output->verts_orig = static_cast<int *>(MEM_malloc_arrayN(tot_v_orig, sizeof(int), __func__));
2425   output->verts_orig_start_table = static_cast<int *>(
2426       MEM_malloc_arrayN(nv, sizeof(int), __func__));
2427   output->verts_orig_len_table = static_cast<int *>(MEM_malloc_arrayN(nv, sizeof(int), __func__));
2428   output->edges = static_cast<decltype(output->edges)>(
2429       MEM_malloc_arrayN(ne, sizeof(output->edges[0]), __func__));
2430   output->edges_orig = static_cast<int *>(MEM_malloc_arrayN(tot_e_orig, sizeof(int), __func__));
2431   output->edges_orig_start_table = static_cast<int *>(
2432       MEM_malloc_arrayN(ne, sizeof(int), __func__));
2433   output->edges_orig_len_table = static_cast<int *>(MEM_malloc_arrayN(ne, sizeof(int), __func__));
2434   output->faces = static_cast<int *>(MEM_malloc_arrayN(tot_f_lens, sizeof(int), __func__));
2435   output->faces_start_table = static_cast<int *>(MEM_malloc_arrayN(nf, sizeof(int), __func__));
2436   output->faces_len_table = static_cast<int *>(MEM_malloc_arrayN(nf, sizeof(int), __func__));
2437   output->faces_orig = static_cast<int *>(MEM_malloc_arrayN(tot_f_orig, sizeof(int), __func__));
2438   output->faces_orig_start_table = static_cast<int *>(
2439       MEM_malloc_arrayN(nf, sizeof(int), __func__));
2440   output->faces_orig_len_table = static_cast<int *>(MEM_malloc_arrayN(nf, sizeof(int), __func__));
2441 
2442   int v_orig_index = 0;
2443   for (int v = 0; v < nv; ++v) {
2444     output->vert_coords[v][0] = static_cast<float>(res.vert[v][0]);
2445     output->vert_coords[v][1] = static_cast<float>(res.vert[v][1]);
2446     int this_start = v_orig_index;
2447     output->verts_orig_start_table[v] = this_start;
2448     for (int j : res.vert_orig[v].index_range()) {
2449       output->verts_orig[v_orig_index++] = res.vert_orig[v][j];
2450     }
2451     output->verts_orig_len_table[v] = v_orig_index - this_start;
2452   }
2453   int e_orig_index = 0;
2454   for (int e = 0; e < ne; ++e) {
2455     output->edges[e][0] = res.edge[e].first;
2456     output->edges[e][1] = res.edge[e].second;
2457     int this_start = e_orig_index;
2458     output->edges_orig_start_table[e] = this_start;
2459     for (int j : res.edge_orig[e].index_range()) {
2460       output->edges_orig[e_orig_index++] = res.edge_orig[e][j];
2461     }
2462     output->edges_orig_len_table[e] = e_orig_index - this_start;
2463   }
2464   int f_orig_index = 0;
2465   int f_index = 0;
2466   for (int f = 0; f < nf; ++f) {
2467     output->faces_start_table[f] = f_index;
2468     int flen = res.face[f].size();
2469     output->faces_len_table[f] = flen;
2470     for (int j = 0; j < flen; ++j) {
2471       output->faces[f_index++] = res.face[f][j];
2472     }
2473     int this_start = f_orig_index;
2474     output->faces_orig_start_table[f] = this_start;
2475     for (int k : res.face_orig[f].index_range()) {
2476       output->faces_orig[f_orig_index++] = res.face_orig[f][k];
2477     }
2478     output->faces_orig_len_table[f] = f_orig_index - this_start;
2479   }
2480   return output;
2481 }
2482 
BLI_delaunay_2d_cdt_free(::CDT_result * result)2483 extern "C" void BLI_delaunay_2d_cdt_free(::CDT_result *result)
2484 {
2485   MEM_freeN(result->vert_coords);
2486   MEM_freeN(result->edges);
2487   MEM_freeN(result->faces);
2488   MEM_freeN(result->faces_start_table);
2489   MEM_freeN(result->faces_len_table);
2490   MEM_freeN(result->verts_orig);
2491   MEM_freeN(result->verts_orig_start_table);
2492   MEM_freeN(result->verts_orig_len_table);
2493   MEM_freeN(result->edges_orig);
2494   MEM_freeN(result->edges_orig_start_table);
2495   MEM_freeN(result->edges_orig_len_table);
2496   MEM_freeN(result->faces_orig);
2497   MEM_freeN(result->faces_orig_start_table);
2498   MEM_freeN(result->faces_orig_len_table);
2499   MEM_freeN(result);
2500 }
2501