Add a function that allows to simplify an iterator range of points

This commit is contained in:
Andreas Fabri 2014-08-25 16:27:19 +02:00
parent c365d5b67a
commit 17e5d01052
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,35 @@
#include <iostream>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Polyline_simplification_2/simplify.h>
#include <list>
#include <vector>
namespace PS = CGAL::Polyline_simplification_2;
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef std::list<Point_2> Polyline;
typedef PS::Stop_above_cost_threshold Stop;
typedef PS::Squared_distance_cost Cost;
int main( )
{
Polyline polyline;
int n;
Point_2 p;
std::cin >> n;
while(std::cin >> p){
polyline.push_back(p);
}
Cost cost;
std::vector<Point_2> result;
PS::simplify(polyline.begin(), polyline.end(), cost, Stop(0.5), std::back_inserter(result));
std::cout.precision(12);
for(int i=0; i < result.size(); ++i){
std::cout << result[i] << std::endl;
}
return 0;
}

View File

@ -353,6 +353,55 @@ template <class PolygonTraits_2, class Container, class CostFunction, class Stop
return result;
}
/*!
\ingroup PkgPolylineSimplification2Functions
Simplifies an open or closed polyline given as an iterator range of 2D \cgal points.
\tparam PointIterator must be an iterator with value type `CGAL::Kernel::Point_2`.
\tparam CostFunction must be a model of `PolylineSimplificationCostFunction`
\tparam StopFunction must be a model of `PolylineSimplificationStopPredicate`
\tparam OutputIterator must be an output iterator to which `CGAL::Kernel::Point_2` can be assigned.
*/
template <class PointIterator, class CostFunction, class StopFunction, class OutputIterator>
OutputIterator
simplify(PointIterator b, PointIterator e,
CostFunction cost,
StopFunction stop,
OutputIterator out,
bool close = false)
{
typedef typename std::iterator_traits<PointIterator>::value_type Point_2;
typedef typename CGAL::Kernel_traits<Point_2>::type K;
typedef Vertex_base_2< K > Vb;
typedef CGAL::Constrained_triangulation_face_base_2<K> Fb;
typedef CGAL::Triangulation_data_structure_2<Vb,Fb> TDS;
typedef CGAL::Constrained_Delaunay_triangulation_2<K, TDS, CGAL::Exact_predicates_tag> CDT;
typedef CGAL::Constrained_triangulation_plus_2<CDT> PCT;
typedef typename PCT::Constraint_id Constraint_id;
typedef typename PCT::Vertices_in_constraint_iterator Vertices_in_constraint_iterator;
PCT pct;
Constraint_id cid = pct.insert_constraint(b,e, close);
bool keep_points = false;
Polyline_simplification_2<PCT, CostFunction, StopFunction> simplifier(pct, cost, stop);
while(simplifier()){}
Vertices_in_constraint_iterator beg = pct.vertices_in_constraint_begin(cid);
Vertices_in_constraint_iterator end = pct.vertices_in_constraint_end(cid);
for(; beg!=end;){
Point_2 p = (*beg)->point();
++beg;
if((!close) || (beg!=end)){
*out++ = p;
}
}
return out;
}
/*!
\ingroup PkgPolylineSimplification2Functions