diff --git a/.gitattributes b/.gitattributes index 2011a6df713..3e56f233319 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2585,6 +2585,8 @@ Minkowski_sum_2/benchmark/Minkowski_sum_2/data/random.dat -text Minkowski_sum_2/benchmark/Minkowski_sum_2/data/random2.dat -text Minkowski_sum_2/benchmark/Minkowski_sum_2/data/star.dat -text Minkowski_sum_2/benchmark/Minkowski_sum_2/sum_by_decomposition.cpp -text +Minkowski_sum_2/benchmark/print_utils.h -text +Minkowski_sum_2/benchmark/sum_by_decomposition.cpp -text Minkowski_sum_2/doc_tex/Minkowski_sum_2/fig/Minkowski_sum_2.png -text Minkowski_sum_2/doc_tex/Minkowski_sum_2/fig/approx_offset.fig -text svneol=unset#application/octet-stream Minkowski_sum_2/doc_tex/Minkowski_sum_2/fig/approx_offset.gif -text svneol=unset#image/gif diff --git a/Minkowski_sum_2/benchmark/print_utils.h b/Minkowski_sum_2/benchmark/print_utils.h new file mode 100644 index 00000000000..3b030eb7bac --- /dev/null +++ b/Minkowski_sum_2/benchmark/print_utils.h @@ -0,0 +1,53 @@ +#ifndef _PRINT_UTILS_H_ +#define _PRINT_UTILS_H_ + +#include +#include + +//----------------------------------------------------------------------------- +// Pretty-print a CGAL polygon. +// +template +void print_polygon (const CGAL::Polygon_2& P) +{ + typename CGAL::Polygon_2::Vertex_const_iterator vit; + + std::cout << "[ " << P.size() << " vertices:"; + for (vit = P.vertices_begin(); vit != P.vertices_end(); ++vit) + std::cout << " (" << *vit << ')'; + std::cout << " ]" << std::endl; + + return; +} + +//----------------------------------------------------------------------------- +// Pretty-print a polygon with holes. +// +template +void print_polygon_with_holes + (const CGAL::Polygon_with_holes_2& pwh) +{ + if (! pwh.is_unbounded()) + { + std::cout << "{ Outer boundary = "; + print_polygon (pwh.outer_boundary()); + } + else + std::cout << "{ Unbounded polygon." << std::endl; + + typename CGAL::Polygon_with_holes_2:: + Hole_const_iterator hit; + unsigned int k = 1; + + std::cout << " " << pwh.number_of_holes() << " holes:" << std::endl; + for (hit = pwh.holes_begin(); hit != pwh.holes_end(); ++hit, ++k) + { + std::cout << " Hole #" << k << " = "; + print_polygon (*hit); + } + std::cout << " }" << std::endl; + + return; +} + +#endif diff --git a/Minkowski_sum_2/benchmark/sum_by_decomposition.cpp b/Minkowski_sum_2/benchmark/sum_by_decomposition.cpp new file mode 100644 index 00000000000..4015b9d0d93 --- /dev/null +++ b/Minkowski_sum_2/benchmark/sum_by_decomposition.cpp @@ -0,0 +1,50 @@ +//! \file examples/Minkowski_sum_2/sum_by_decomposition.cpp +// Computing the Minkowski sum of two non-convex polygons read from a file +// using the small-side angle-bisector decomposition strategy. + +#include +#include +#include +#include +#include +#include +#include "print_utils.h" + +struct Kernel : public CGAL::Exact_predicates_exact_constructions_kernel {}; + +typedef Kernel::Point_2 Point_2; +typedef CGAL::Polygon_2 Polygon_2; +typedef CGAL::Polygon_with_holes_2 Polygon_with_holes_2; + +int main () +{ + CGAL::Timer t_mink_sum; + // Open the input file. + std::ifstream in_file ("data/random2.dat"); + + if (! in_file.is_open()) + { + std::cerr << "Failed to open the input file." << std::endl; + return (1); + } + + // Read the two polygons from the file and compute their Minkowski sum. + Polygon_2 P, Q; + + in_file >> P >> Q; + in_file.close(); + + + // Compute the Minkowski sum using the decomposition approach. + CGAL::Small_side_angle_bisector_decomposition_2 ssab_decomp; + + t_mink_sum.start(); + + Polygon_with_holes_2 sum = minkowski_sum_2 (P, Q, ssab_decomp); + + t_mink_sum.stop(); + + std::cout << "Done! Time:" << t_mink_sum.time() << " seconds\n P (+) Q = "; print_polygon_with_holes (sum); + + return (0); +}