Skip to content

Instantly share code, notes, and snippets.

@bobergj
Created November 8, 2017 12:28
Show Gist options
  • Save bobergj/7d13865080e923b7017f4761358828ce to your computer and use it in GitHub Desktop.
Save bobergj/7d13865080e923b7017f4761358828ce to your computer and use it in GitHub Desktop.
CGAL::random_polygon_2 infinite loop
#define CGAL_DONT_SHUFFLE_IN_RANDOM_POLYGON_2
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/point_generators_2.h>
#include <CGAL/random_polygon_2.h>
#include <vector>
typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef std::vector<Point_2> Container;
typedef CGAL::Polygon_2<K, Container> Polygon_2;
// Point generator that let's us generate a point from a vector instead of randomly
class PointGeneratorFromSequence : public CGAL::Generator_base<Point_2> {
void generate_point();
private:
std::vector<Point_2> points;
size_t cur_index;
public:
typedef PointGeneratorFromSequence This;
PointGeneratorFromSequence(std::vector<Point_2> &points) : points(points), cur_index(0) { generate_point(); };
This& operator++() {
generate_point();
return *this;
}
This operator++(int) {
This tmp = *this;
++(*this);
return tmp;
}
};
typedef PointGeneratorFromSequence Point_generator;
void PointGeneratorFromSequence::generate_point() {
assert(this->points.size() > cur_index);
this->d_item = points[cur_index];
this->cur_index++;
}
int main() {
std::vector<Point_2> points;
points.push_back(Point_2(0,1));
points.push_back(Point_2(2,2));
points.push_back(Point_2(2,1));
points.push_back(Point_2(3,1));
// Note that the last point is not used, it's just needed here because random_polygon_2 eats one extra unused
// point from the point generator due to how the loop in random_polygon_2.h:copy_n_unique is written.
points.push_back(Point_2(0,0));
Polygon_2 polygon;
// The following call will never return:
CGAL::random_polygon_2(points.size() - 1, std::back_inserter(polygon), Point_generator(points));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment