Refactoring. Use Model class for representing the plate in GUI

This commit is contained in:
Alessandro Ranellucci 2013-12-12 20:19:33 +01:00
parent f55e057504
commit 0e8a0ef1ca
19 changed files with 610 additions and 620 deletions

View file

@ -1,4 +1,5 @@
#include "ExPolygonCollection.hpp"
#include "Geometry.hpp"
namespace Slic3r {
@ -57,4 +58,13 @@ ExPolygonCollection::simplify(double tolerance)
this->expolygons = expp;
}
void
ExPolygonCollection::convex_hull(Polygon* hull) const
{
Points pp;
for (ExPolygons::const_iterator it = this->expolygons.begin(); it != this->expolygons.end(); ++it)
pp.insert(pp.end(), it->contour.points.begin(), it->contour.points.end());
Slic3r::Geometry::convex_hull(pp, hull);
}
}

View file

@ -16,6 +16,7 @@ class ExPolygonCollection
void rotate(double angle, Point* center);
bool contains_point(const Point* point) const;
void simplify(double tolerance);
void convex_hull(Polygon* hull) const;
};
}

View file

@ -2,6 +2,7 @@
#include "clipper.hpp"
#include <algorithm>
#include <map>
#include <vector>
namespace Slic3r { namespace Geometry {
@ -11,45 +12,33 @@ sort_points (Point a, Point b)
return (a.x < b.x) || (a.x == b.x && a.y < b.y);
}
/* This implementation is based on Steffen Mueller's work for
the Perl module Math::ConvexHull::MonotoneChain (available
on CPAN under the GPL terms) */
/* This implementation is based on Andrew's monotone chain 2D convex hull algorithm */
void
convex_hull(Points points, Polygon &hull)
convex_hull(Points &points, Polygon* hull)
{
assert(points.size() >= 3);
// sort input points
std::sort(points.begin(), points.end(), sort_points);
typedef const Point* PointPtr;
PointPtr* out_hull = (PointPtr*)malloc(points.size()*2*sizeof(PointPtr));
/* lower hull */
size_t k = 0;
for (Points::const_iterator it = points.begin(); it != points.end(); ++it) {
while (k >= 2 && it->ccw(out_hull[k-2], out_hull[k-1]) <= 0) --k;
Point pz = *&*it;
out_hull[k++] = &*it;
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];
}
/* upper hull */
size_t t = k+1;
for (Points::const_iterator it = points.end() - 2; it != points.begin(); --it) {
while (k >= t && it->ccw(out_hull[k-2], out_hull[k-1]) <= 0) --k;
out_hull[k++] = &*it;
// 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];
}
hull->points.resize(k);
// we assume hull is empty
hull.points.reserve(k);
for (size_t i = 0; i < k; ++i) {
hull.points.push_back(*(out_hull[i]));
}
// not sure why this happens randomly
if (hull.points.front().coincides_with(hull.points.back()))
hull.points.pop_back();
free(out_hull);
assert( hull->points.front().coincides_with(hull->points.back()) );
hull->points.pop_back();
}
/* accepts an arrayref of points and returns a list of indices

View file

@ -5,7 +5,7 @@
namespace Slic3r { namespace Geometry {
void convex_hull(Points points, Polygon &hull);
void convex_hull(Points &points, Polygon* hull);
void chained_path(Points &points, std::vector<Points::size_type> &retval, Point start_near);
void chained_path(Points &points, std::vector<Points::size_type> &retval);
template<class T> void chained_path_items(Points &points, T &items, T &retval);

View file

@ -109,6 +109,9 @@ Point::distance_to(const Line &line) const
/* Three points are a counter-clockwise turn if ccw > 0, clockwise if
* ccw < 0, and collinear if ccw = 0 because ccw is a determinant that
* gives the signed area of the triangle formed by p1, p2 and this point.
* In other words it is the 2D cross product of p1-p2 and p1-this, i.e.
* z-component of their 3D cross product.
* We return double because it must be big enough to hold 2*max(|coordinate|)^2
*/
double
Point::ccw(const Point &p1, const Point &p2) const

View file

@ -603,7 +603,7 @@ TriangleMesh::horizontal_projection(ExPolygons &retval) const
}
void
TriangleMesh::convex_hull(Polygon &hull)
TriangleMesh::convex_hull(Polygon* hull)
{
if (this->stl.v_shared == NULL) stl_generate_shared_vertices(&(this->stl));
Points pp;

View file

@ -33,7 +33,7 @@ class TriangleMesh
TriangleMeshPtrs split() const;
void merge(const TriangleMesh* mesh);
void horizontal_projection(ExPolygons &retval) const;
void convex_hull(Polygon &hull);
void convex_hull(Polygon* hull);
stl_file stl;
bool repaired;