11
22#include " delaunator.h"
3+ #include < algorithm>
34#include < cstdio>
45#include < limits>
56#include < tuple>
@@ -103,90 +104,14 @@ namespace {
103104 return diff3;
104105 }
105106 }
106- // std::deque<long int>::iterator insert_node(
107- // const deque<DelaunatorPoint> &hull,
108- // const vector<double> &coords,
109- // long int i,
110- // std::deque<long int>::iterator prev
111- // ) {
112- // return hull.emplace(prev + 1, {
113- // .i = i,
114- // .x = coords[2 * i],
115- // .y = coords[2 * i + 1],
116- // .t = 0
117- // });
118- // }
119- // size_t insert_node(
120- // deque<DelaunatorPoint> &hull,
121- // const vector<double> &coords,
122- // long int i
123- // ) {
124- // DelaunatorPoint p = {
125- // .i = i,
126- // .x = coords[2 * i],
127- // .y = coords[2 * i + 1],
128- // .t = 0
129- // };
130- // size_t pos = hull.insert(hull.end(), p) - hull.begin();
131- // return pos;
132- // }
133- void quicksort (
134- unsigned long int ids[],
135- const vector<double > &coords,
136- unsigned int left,
137- unsigned int right,
138- double &cx,
139- double &cy
140- ) {
141- long int i;
142- long int j;
143- unsigned long int temp;
144-
145- if (right - left <= 20 ) {
146- for (i = left + 1 ; i <= right; i++) {
147- // printf("i=%lu\n", i);
148- temp = ids[i];
149- j = i - 1 ;
150- while (
151- j >= left &&
152- compare (coords, ids[j], temp, cx, cy) > 0
153- ) {
154- // printf("j=%lu\n", j);
155- ids[j + 1 ] = ids[j];
156- j--;
157- }
158- ids[j + 1 ] = temp;
159- }
160- } else {
161- throw runtime_error (" not implemented" );
162- }// else {
163- // const median = (left + right) >> 1;
164- // i = left + 1;
165- // j = right;
166- // swap(ids, median, i);
167- // if (compare(coords, ids[left], ids[right], cx, cy) > 0) swap(ids, left, right);
168- // if (compare(coords, ids[i], ids[right], cx, cy) > 0) swap(ids, i, right);
169- // if (compare(coords, ids[left], ids[i], cx, cy) > 0) swap(ids, left, i);
170-
171- // temp = ids[i];
172- // while (true) {
173- // do i++; while (compare(coords, ids[i], temp, cx, cy) < 0);
174- // do j--; while (compare(coords, ids[j], temp, cx, cy) > 0);
175- // if (j < i) break;
176- // swap(ids, i, j);
177- // }
178- // ids[left + 1] = ids[j];
179- // ids[j] = temp;
180-
181- // if (right - i + 1 >= j - left) {
182- // quicksort(ids, coords, i, right, cx, cy);
183- // quicksort(ids, coords, left, j - 1, cx, cy);
184- // } else {
185- // quicksort(ids, coords, left, j - 1, cx, cy);
186- // quicksort(ids, coords, i, right, cx, cy);
187- // }
188- // }
189- }
107+ struct sort_to_center {
108+ double cx;
109+ double cy;
110+ vector<double > &coords;
111+ bool operator () (long int i, long int j) {
112+ return compare (coords, i, j, cx, cy) < 0 ;
113+ }
114+ };
190115
191116 bool in_circle (
192117 double ax, double ay,
@@ -218,7 +143,8 @@ Delaunator::Delaunator(const vector<double> &in_coords) {
218143 double max_y = -1 * max_double;
219144 double min_x = max_double;
220145 double min_y = max_double;
221- unsigned long int ids[n];
146+ vector<long int > ids;
147+ ids.reserve (n);
222148 for (long int i = 0 ; i < n; i++) {
223149 const double x = coords[2 * i];
224150 const double y = coords[2 * i + 1 ];
@@ -228,7 +154,8 @@ Delaunator::Delaunator(const vector<double> &in_coords) {
228154 if (y < min_y) min_y = y;
229155 if (x > max_x) max_x = x;
230156 if (y > max_y) max_y = y;
231- ids[i] = i;
157+ // ids[i] = i;
158+ ids.push_back (i);
232159 }
233160 const double cx = (min_x + max_x) / 2 ;
234161 const double cy = (min_y + max_y) / 2 ;
@@ -304,7 +231,15 @@ Delaunator::Delaunator(const vector<double> &in_coords) {
304231 tie (m_center_x, m_center_y) = circumcenter (i0x, i0y, i1x, i1y, i2x, i2y);
305232
306233 // sort the points by distance from the seed triangle circumcenter
307- quicksort (ids, coords, 0 , n - 1 , m_center_x, m_center_y);
234+ // cerr << ids << endl;
235+ const sort_to_center s = {
236+ .cx = m_center_x,
237+ .cy = m_center_y,
238+ .coords = coords
239+ };
240+ sort (ids.begin (), ids.end (), s);
241+ // quicksort(ids, coords, 0, n - 1, m_center_x, m_center_y);
242+ // cerr << ids << endl;
308243
309244 m_hash_size = ceil (sqrt (n));
310245 m_hash.reserve (m_hash_size);
@@ -324,8 +259,6 @@ Delaunator::Delaunator(const vector<double> &in_coords) {
324259 hash_edge (e);
325260 m_hull[e].t = 2 ;
326261
327- // cout << "m_hash " << m_hash << endl;
328-
329262 const size_t max_triangles = 2 * n - 5 ;
330263 triangles.reserve (max_triangles * 3 );
331264 halfedges.reserve (max_triangles * 3 );
@@ -486,7 +419,7 @@ size_t Delaunator::legalize(size_t a) {
486419 legalize (a);
487420 return legalize (br);
488421 }
489-
422+ return ar;
490423}
491424
492425size_t Delaunator::insert_node (size_t i, size_t prev) {
0 commit comments