Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "Geometry.hpp" | ||
#include <algorithm> | ||
|
||
namespace Slic3r { | ||
|
||
static bool | ||
sort_points (Point a, Point b) | ||
{ | ||
return (a.x < b.x) || (a.x == b.x && a.y < b.y); | ||
} | ||
|
||
void | ||
convex_hull(Points points, Polygon &hull) | ||
{ | ||
assert(points.size() >= 2); | ||
// 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; | ||
} | ||
|
||
/* 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; | ||
} | ||
|
||
// we assume hull is empty | ||
hull.points.reserve(k); | ||
for (size_t i = 0; i < k; ++i) { | ||
hull.points.push_back(*(out_hull[i])); | ||
} | ||
|
||
free(out_hull); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef slic3r_Geometry_hpp_ | ||
#define slic3r_Geometry_hpp_ | ||
|
||
#include "Polygon.hpp" | ||
|
||
namespace Slic3r { | ||
|
||
void convex_hull(Points points, Polygon &hull); | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/usr/bin/perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
use Slic3r::XS; | ||
use Test::More tests => 2; | ||
|
||
{ | ||
my @points = ( | ||
Slic3r::Point->new(100,100), | ||
Slic3r::Point->new(100,200), | ||
Slic3r::Point->new(200,200), | ||
Slic3r::Point->new(200,100), | ||
Slic3r::Point->new(150,150), | ||
); | ||
my $hull = Slic3r::Geometry::convex_hull(\@points); | ||
isa_ok $hull, 'Slic3r::Polygon', 'convex_hull returns a Polygon'; | ||
is scalar(@$hull), 4, 'convex_hull returns the correct number of points'; | ||
} | ||
|
||
__END__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
%module{Slic3r::XS}; | ||
|
||
%{ | ||
#include <myinit.h> | ||
#include "Geometry.hpp" | ||
%} | ||
|
||
%package{Slic3r::Geometry}; | ||
|
||
%{ | ||
|
||
Polygon* | ||
convex_hull(points) | ||
Points points | ||
PREINIT: | ||
const char* CLASS = "Slic3r::Polygon"; | ||
CODE: | ||
RETVAL = new Polygon (); | ||
convex_hull(points, *RETVAL); | ||
OUTPUT: | ||
RETVAL | ||
|
||
%} |