2013-11-22 21:38:30 +00:00
|
|
|
#include "Geometry.hpp"
|
2013-11-23 22:21:59 +00:00
|
|
|
#include "clipper.hpp"
|
2013-11-22 21:38:30 +00:00
|
|
|
#include <algorithm>
|
2013-11-23 20:39:05 +00:00
|
|
|
#include <map>
|
2013-12-12 19:19:33 +00:00
|
|
|
#include <vector>
|
2013-11-22 21:38:30 +00:00
|
|
|
|
2013-11-23 20:54:56 +00:00
|
|
|
namespace Slic3r { namespace Geometry {
|
2013-11-22 21:38:30 +00:00
|
|
|
|
|
|
|
static bool
|
|
|
|
sort_points (Point a, Point b)
|
|
|
|
{
|
|
|
|
return (a.x < b.x) || (a.x == b.x && a.y < b.y);
|
|
|
|
}
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
/* This implementation is based on Andrew's monotone chain 2D convex hull algorithm */
|
2013-11-22 21:38:30 +00:00
|
|
|
void
|
2013-12-12 19:19:33 +00:00
|
|
|
convex_hull(Points &points, Polygon* hull)
|
2013-11-22 21:38:30 +00:00
|
|
|
{
|
2013-11-24 21:42:52 +00:00
|
|
|
assert(points.size() >= 3);
|
2013-11-22 21:38:30 +00:00
|
|
|
// sort input points
|
|
|
|
std::sort(points.begin(), points.end(), sort_points);
|
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
int n = points.size(), k = 0;
|
|
|
|
hull->points.resize(2*n);
|
|
|
|
|
|
|
|
// Build lower hull
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
while (k >= 2 && points[i].ccw(hull->points[k-2], hull->points[k-1]) <= 0) k--;
|
|
|
|
hull->points[k++] = points[i];
|
2013-11-22 21:38:30 +00:00
|
|
|
}
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
// Build upper hull
|
|
|
|
for (int i = n-2, t = k+1; i >= 0; i--) {
|
|
|
|
while (k >= t && points[i].ccw(hull->points[k-2], hull->points[k-1]) <= 0) k--;
|
|
|
|
hull->points[k++] = points[i];
|
2013-11-22 21:38:30 +00:00
|
|
|
}
|
2013-12-12 19:19:33 +00:00
|
|
|
|
|
|
|
hull->points.resize(k);
|
2013-11-22 21:38:30 +00:00
|
|
|
|
2013-12-12 19:19:33 +00:00
|
|
|
assert( hull->points.front().coincides_with(hull->points.back()) );
|
|
|
|
hull->points.pop_back();
|
2013-11-22 21:38:30 +00:00
|
|
|
}
|
|
|
|
|
2013-11-23 20:39:05 +00:00
|
|
|
/* accepts an arrayref of points and returns a list of indices
|
|
|
|
according to a nearest-neighbor walk */
|
|
|
|
void
|
|
|
|
chained_path(Points &points, std::vector<Points::size_type> &retval, Point start_near)
|
|
|
|
{
|
|
|
|
PointPtrs my_points;
|
|
|
|
std::map<Point*,Points::size_type> indices;
|
|
|
|
my_points.reserve(points.size());
|
|
|
|
for (Points::iterator it = points.begin(); it != points.end(); ++it) {
|
|
|
|
my_points.push_back(&*it);
|
|
|
|
indices[&*it] = it - points.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
retval.reserve(points.size());
|
|
|
|
while (!my_points.empty()) {
|
|
|
|
Points::size_type idx = start_near.nearest_point_index(my_points);
|
|
|
|
start_near = *my_points[idx];
|
|
|
|
retval.push_back(indices[ my_points[idx] ]);
|
|
|
|
my_points.erase(my_points.begin() + idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
chained_path(Points &points, std::vector<Points::size_type> &retval)
|
|
|
|
{
|
|
|
|
if (points.empty()) return; // can't call front() on empty vector
|
|
|
|
chained_path(points, retval, points.front());
|
|
|
|
}
|
|
|
|
|
2013-11-23 22:21:59 +00:00
|
|
|
/* retval and items must be different containers */
|
|
|
|
template<class T>
|
|
|
|
void
|
|
|
|
chained_path_items(Points &points, T &items, T &retval)
|
|
|
|
{
|
|
|
|
std::vector<Points::size_type> indices;
|
|
|
|
chained_path(points, indices);
|
|
|
|
for (std::vector<Points::size_type>::const_iterator it = indices.begin(); it != indices.end(); ++it)
|
|
|
|
retval.push_back(items[*it]);
|
|
|
|
}
|
|
|
|
template void chained_path_items(Points &points, ClipperLib::PolyNodes &items, ClipperLib::PolyNodes &retval);
|
|
|
|
|
2013-11-23 20:54:56 +00:00
|
|
|
} }
|