Skip to content

Commit

Permalink
Backport from delaunator-js for
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenmuehlbauer committed Aug 5, 2019
1 parent c1521f6 commit f55ced6
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions include/delaunator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,12 @@ inline std::pair<double, double> circumcenter(

struct compare {

std::vector<double> const& dists;
std::vector<double> const& coords;
double cx;
double cy;

bool operator()(std::size_t i, std::size_t j) {
const double d1 = dist(coords[2 * i], coords[2 * i + 1], cx, cy);
const double d2 = dist(coords[2 * j], coords[2 * j + 1], cx, cy);
const double d1 = dists[i];
const double d2 = dists[j];
const double diff1 = d1 - d2;
const double diff2 = coords[2 * i] - coords[2 * j];
const double diff3 = coords[2 * i + 1] - coords[2 * j + 1];
Expand Down Expand Up @@ -302,8 +301,16 @@ Delaunator::Delaunator(std::vector<double> const& in_coords)

std::tie(m_center_x, m_center_y) = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y);

std::vector<double> dists;
dists.reserve(ids.size());

for (auto&& id : ids) {
const double d = dist(coords[2 * id], coords[2 * id + 1], m_center_x, m_center_y);
dists.push_back(d);
}

// sort the points by distance from the seed triangle circumcenter
std::sort(ids.begin(), ids.end(), compare{ coords, m_center_x, m_center_y });
std::sort(ids.begin(), ids.end(), compare{ dists, coords });

// initialize a hash table for storing edges of the advancing convex hull
m_hash_size = static_cast<std::size_t>(std::llround(std::ceil(std::sqrt(n))));
Expand Down Expand Up @@ -508,7 +515,7 @@ std::size_t Delaunator::legalize(std::size_t a) {
hull_tri[e] = a;
break;
}
e = hull_next[e];
e = hull_prev[e];
} while (e != hull_start);
}
link(a, hbl);
Expand Down

0 comments on commit f55ced6

Please sign in to comment.