From dafa6c5e7c02bfe26ed738a89ee3a9db11bcc1f2 Mon Sep 17 00:00:00 2001 From: Fernando Cacciola Date: Fri, 30 Apr 2010 19:01:58 +0000 Subject: [PATCH] Weighted straight skeleton API update --- .gitattributes | 2 + .../Create_partial_straight_skeleton_2.cpp | 32 +++++++ .../Create_straight_skeleton_2.cpp | 8 +- .../Create_weighted_straight_skeleton_2.cpp | 41 +++++++++ .../Straight_skeleton_2/dump_to_eps.h | 89 ++++++++++++++----- .../include/CGAL/create_straight_skeleton_2.h | 12 +-- ...aight_skeleton_from_polygon_with_holes_2.h | 59 ++++++++++-- 7 files changed, 205 insertions(+), 38 deletions(-) create mode 100644 Straight_skeleton_2/examples/Straight_skeleton_2/Create_partial_straight_skeleton_2.cpp create mode 100644 Straight_skeleton_2/examples/Straight_skeleton_2/Create_weighted_straight_skeleton_2.cpp diff --git a/.gitattributes b/.gitattributes index dabc5d3849f..d514c38d384 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3247,11 +3247,13 @@ Straight_skeleton_2/doc_tex/Straight_skeleton_2_ref/Straight_skeleton_converter_ Straight_skeleton_2/doc_tex/Straight_skeleton_2_ref/Straight_skeleton_face_base_2.tex -text Straight_skeleton_2/doc_tex/Straight_skeleton_2_ref/Straight_skeleton_items_converter_2.tex -text Straight_skeleton_2/examples/Straight_skeleton_2/Create_offset_polygons_2.cpp -text +Straight_skeleton_2/examples/Straight_skeleton_2/Create_partial_straight_skeleton_2.cpp -text Straight_skeleton_2/examples/Straight_skeleton_2/Create_skeleton_and_offset_polygons_2.cpp -text Straight_skeleton_2/examples/Straight_skeleton_2/Create_skeleton_and_offset_polygons_from_polygon_with_holes_2.cpp -text Straight_skeleton_2/examples/Straight_skeleton_2/Create_skeleton_and_offset_polygons_with_holes_2.cpp -text Straight_skeleton_2/examples/Straight_skeleton_2/Create_straight_skeleton_2.cpp -text Straight_skeleton_2/examples/Straight_skeleton_2/Create_straight_skeleton_from_polygon_with_holes_2.cpp -text +Straight_skeleton_2/examples/Straight_skeleton_2/Create_weighted_straight_skeleton_2.cpp -text Straight_skeleton_2/examples/Straight_skeleton_2/print.h -text Straight_skeleton_2/examples/Straight_skeleton_2/sample_1.dat -text Straight_skeleton_2/examples/Straight_skeleton_2/sample_2.dat -text diff --git a/Straight_skeleton_2/examples/Straight_skeleton_2/Create_partial_straight_skeleton_2.cpp b/Straight_skeleton_2/examples/Straight_skeleton_2/Create_partial_straight_skeleton_2.cpp new file mode 100644 index 00000000000..1d5fb3bedf4 --- /dev/null +++ b/Straight_skeleton_2/examples/Straight_skeleton_2/Create_partial_straight_skeleton_2.cpp @@ -0,0 +1,32 @@ +#include + +#include +#include +#include + +#include "dump_to_eps.h" + +typedef CGAL::Exact_predicates_inexact_constructions_kernel K ; + +typedef K::Point_2 Point ; +typedef CGAL::Polygon_2 Polygon ; +typedef CGAL::Polygon_with_holes_2 Polygon_with_holes ; +typedef CGAL::Straight_skeleton_2 Ss ; + + +typedef boost::shared_ptr SsPtr ; + +int main() +{ + Polygon_with_holes input ; + + std::ifstream is("bee.poly") ; + is >> input ; + + SsPtr ss = CGAL::create_partial_interior_straight_skeleton_2(input,2.5); + + dump_to_eps(input,*ss,"partial_skeleton.eps"); + + + return 0; +} diff --git a/Straight_skeleton_2/examples/Straight_skeleton_2/Create_straight_skeleton_2.cpp b/Straight_skeleton_2/examples/Straight_skeleton_2/Create_straight_skeleton_2.cpp index 20a862bbc76..05b0e0efd12 100644 --- a/Straight_skeleton_2/examples/Straight_skeleton_2/Create_straight_skeleton_2.cpp +++ b/Straight_skeleton_2/examples/Straight_skeleton_2/Create_straight_skeleton_2.cpp @@ -4,7 +4,7 @@ #include #include -#include "print.h" +#include "dump_to_eps.h" typedef CGAL::Exact_predicates_inexact_constructions_kernel K ; @@ -32,9 +32,9 @@ int main() // Or you can pass the polygon directly, as below. SsPtr oss = CGAL::create_exterior_straight_skeleton_2(poly); - - print_straight_skeleton(*iss); - print_straight_skeleton(*oss); + + dump_to_eps(poly,*iss,"interior_skeleton.eps"); + dump_to_eps(poly,*oss,"exterior_skeleton.eps"); return 0; } diff --git a/Straight_skeleton_2/examples/Straight_skeleton_2/Create_weighted_straight_skeleton_2.cpp b/Straight_skeleton_2/examples/Straight_skeleton_2/Create_weighted_straight_skeleton_2.cpp new file mode 100644 index 00000000000..dc0326158b8 --- /dev/null +++ b/Straight_skeleton_2/examples/Straight_skeleton_2/Create_weighted_straight_skeleton_2.cpp @@ -0,0 +1,41 @@ +#include + +#include +#include +#include + +#include "dump_to_eps.h" + +typedef CGAL::Exact_predicates_inexact_constructions_kernel K ; + +typedef K::Point_2 Point ; +typedef CGAL::Polygon_2 Polygon ; +typedef CGAL::Straight_skeleton_2 Ss ; + +typedef boost::shared_ptr SsPtr ; + +int main() +{ + Polygon poly ; + + poly.push_back( Point(-1,-1) ) ; + poly.push_back( Point(0,-12) ) ; + poly.push_back( Point(1,-1) ) ; + poly.push_back( Point(12,0) ) ; + poly.push_back( Point(1,1) ) ; + poly.push_back( Point(0,12) ) ; + poly.push_back( Point(-1,1) ) ; + poly.push_back( Point(-12,0) ) ; + + SsPtr ss0 = CGAL::create_straight_skeleton_2(poly, -1.5); + + dump_to_eps(poly,*ss0,"uniform_weights_skeleton.eps"); + + double weights[] = { -1.0, -1.0, -1.5, -1.0, -0.5, 1.0, 1.5, -1.0 } ; + + SsPtr ss1 = CGAL::create_straight_skeleton_2(poly.vertices_begin(), poly.vertices_end(), weights, weights+8); + + dump_to_eps(poly,*ss1,"mixed_weights_skeleton.eps"); + + return 0; +} diff --git a/Straight_skeleton_2/examples/Straight_skeleton_2/dump_to_eps.h b/Straight_skeleton_2/examples/Straight_skeleton_2/dump_to_eps.h index 8d81c950fbd..e0b35edaeca 100644 --- a/Straight_skeleton_2/examples/Straight_skeleton_2/dump_to_eps.h +++ b/Straight_skeleton_2/examples/Straight_skeleton_2/dump_to_eps.h @@ -1,37 +1,38 @@ +#include #include #include namespace CGAL { +template Bbox_2 bbox_2 ( Poly const& aPoly ) { return bbox_2(aPoly.begin(), aPoly.end()); } + +template Bbox_2 bbox_2 ( Polygon_2 const& aPoly ) { return bbox_2(aPoly.vertices_begin(), aPoly.vertices_end()); } + template Bbox_2 bbox_2 ( Polygon_with_holes_2 const& aPolyWH ) { - Bbox_2 rBbox = bbox_2(aPolyWH.outer_boundary().vertices_begin(), aPolyWH.outer_boundary().vertices_end()); + Bbox_2 rBbox = bbox_2(aPolyWH.outer_boundary()); for ( typename Polygon_with_holes_2::Hole_const_iterator hit = aPolyWH.holes_begin() ; hit != aPolyWH.holes_end() ; ++ hit ) - rBbox = rBbox + bbox_2(hit->vertices_begin(), hit->vertices_end()); + rBbox = rBbox + bbox_2(*hit); return rBbox ; } } -template -void dump_to_eps( CGAL::Polygon_2 const& aPoly, char const* aType, double aScale, std::ostream& rOut ) +template +void dump_to_eps( InputIterator aBegin, InputIterator aEnd, char const* aType, double aScale, std::ostream& rOut ) { - typedef typename CGAL::Polygon_2::const_iterator vertex_const_iterator ; - - vertex_const_iterator begin = aPoly.vertices_begin() ; - vertex_const_iterator end = aPoly.vertices_end () ; - vertex_const_iterator last = end - 1 ; + InputIterator lLast = aEnd - 1 ; - for( vertex_const_iterator curr = begin ; curr != end ; ++ curr ) + for( InputIterator curr = aBegin ; curr != aEnd ; ++ curr ) { - vertex_const_iterator next = curr == last ? begin : curr + 1 ; + InputIterator next = curr == lLast ? aBegin : curr + 1 ; rOut << aType << std::endl << aScale * curr->x() @@ -45,6 +46,18 @@ void dump_to_eps( CGAL::Polygon_2 const& aPoly, char const* aType, double aSc } } +template +void dump_to_eps( Poly const& aPoly, char const* aType, double aScale, std::ostream& rOut ) +{ + dump_to_eps(aPoly.begin(), aPoly.end(), aType, aScale, rOut); +} + +template +void dump_to_eps( CGAL::Polygon_2 const& aPoly, char const* aType, double aScale, std::ostream& rOut ) +{ + dump_to_eps(aPoly.vertices_begin(), aPoly.vertices_end(), aType, aScale, rOut); +} + template void dump_to_eps( CGAL::Polygon_with_holes_2 const& aPWH, char const* aType, double aScale, std::ostream& rOut ) { @@ -82,17 +95,17 @@ void dump_to_eps( CGAL::Straight_skeleton_2 const& aSkeleton, char const* aTy } } -template -void dump_to_eps ( CGAL::Polygon_with_holes_2 const& aInput - , std::vector< boost::shared_ptr< CGAL::Polygon_with_holes_2 > > const& aOutput - , std::ostream& rOut +template +void dump_to_eps ( Poly const& aInput + , std::vector< boost::shared_ptr > const& aOutput + , std::ostream& rOut ) { - typedef std::vector< boost::shared_ptr< CGAL::Polygon_with_holes_2 > > PolyWH_vector ; + typedef std::vector< boost::shared_ptr > Poly_vector ; CGAL::Bbox_2 lBbox = CGAL::bbox_2(aInput); - for( typename PolyWH_vector::const_iterator it = aOutput.begin() ; it != aOutput.end(); ++ it ) + for( typename Poly_vector::const_iterator it = aOutput.begin() ; it != aOutput.end(); ++ it ) lBbox = lBbox + CGAL::bbox_2(**it); double lScale = 1000 / (lBbox.xmax() - lBbox.xmin()) ; @@ -122,15 +135,15 @@ void dump_to_eps ( CGAL::Polygon_with_holes_2 const& dump_to_eps(aInput,"input",lScale,rOut); - for( typename PolyWH_vector::const_iterator it = aOutput.begin() ; it != aOutput.end(); ++ it ) + for( typename Poly_vector::const_iterator it = aOutput.begin() ; it != aOutput.end(); ++ it ) dump_to_eps(**it,"output",lScale,rOut); rOut << "grestore\nshowpage" << std::endl; } -template -void dump_to_eps ( CGAL::Polygon_with_holes_2 const& aInput +template +void dump_to_eps ( Input const& aInput , CGAL::Straight_skeleton_2 const& aSkeleton , std::ostream& rOut ) @@ -169,3 +182,39 @@ void dump_to_eps ( CGAL::Polygon_with_holes_2 const& aInput rOut << "grestore\nshowpage" << std::endl; } + +template +void dump_to_eps ( Input const& aInput + , CGAL::Straight_skeleton_2 const& aSkeleton + , std::string aFilename + ) +{ + std::ofstream eps(aFilename.c_str()) ; + if ( eps ) + { + std::cerr << "Result: " << aFilename << std::endl ; + dump_to_eps(aInput,aSkeleton,eps); + } + else + { + std::cerr << "Could not open result file: " << aFilename << std::endl ; + } +} + +template +void dump_to_eps ( Input const& aInput + , Output const& aOutput + , std::string aFilename + ) +{ + std::ofstream eps(aFilename.c_str()) ; + if ( eps ) + { + std::cerr << "Result: " << aFilename << std::endl ; + dump_to_eps(aInput,aOutput,eps); + } + else + { + std::cerr << "Could not open result file: " << aFilename << std::endl ; + } +} \ No newline at end of file diff --git a/Straight_skeleton_2/include/CGAL/create_straight_skeleton_2.h b/Straight_skeleton_2/include/CGAL/create_straight_skeleton_2.h index 8cc647f734e..2abacff6d48 100644 --- a/Straight_skeleton_2/include/CGAL/create_straight_skeleton_2.h +++ b/Straight_skeleton_2/include/CGAL/create_straight_skeleton_2.h @@ -78,11 +78,11 @@ create_straight_skeleton_2 ( PointIterator aOuterContour_VerticesBe SsBuilder ssb(aMaxTime) ; - ssb.enter_contour( aOuterContour_VerticesBegin, aOuterContour_VerticesEnd, aOuterContour_WeightsBegin, aOuterContour_WeightsEnd, false, Point_converter ) ; + ssb.enter_contour( aOuterContour_VerticesBegin, aOuterContour_VerticesEnd, aOuterContour_WeightsBegin, aOuterContour_WeightsEnd, true, Point_converter ) ; WeightSequenceIterator whi = aHolesWeightBegin ; for ( HoleIterator hi = aHolesBegin ; hi != aHolesEnd ; ++ hi, ++ whi ) - ssb.enter_contour( CGAL_SS_i::vertices_begin(*hi), CGAL_SS_i::vertices_end(*hi), whi->begin(), whi->end(), false, Point_converter ) ; + ssb.enter_contour( CGAL_SS_i::vertices_begin(*hi), CGAL_SS_i::vertices_end(*hi), whi->begin(), whi->end(), true, Point_converter ) ; return ssb.construct_skeleton(); } @@ -139,10 +139,10 @@ create_straight_skeleton_2 ( PointIterator aOuterContour_VerticesBe SsBuilder ssb(aMaxTime) ; - ssb.enter_contour( aOuterContour_VerticesBegin, aOuterContour_VerticesEnd, aWeight, false, Point_converter ) ; + ssb.enter_contour( aOuterContour_VerticesBegin, aOuterContour_VerticesEnd, aWeight, true, Point_converter ) ; for ( HoleIterator hi = aHolesBegin ; hi != aHolesEnd ; ++ hi ) - ssb.enter_contour( CGAL_SS_i::vertices_begin(*hi), CGAL_SS_i::vertices_end(*hi), aWeight, false, Point_converter ) ; + ssb.enter_contour( CGAL_SS_i::vertices_begin(*hi), CGAL_SS_i::vertices_end(*hi), aWeight, true, Point_converter ) ; return ssb.construct_skeleton(); } @@ -153,8 +153,8 @@ create_straight_skeleton_2 ( PointIterator aOuterContour_Vertic , PointIterator aOuterContour_VerticesEnd , HoleIterator aHolesBegin , HoleIterator aHolesEnd - , double aWeight = 1.0 - , boost::optional const& aMaxTime = boost::optional() + , double aWeight + , boost::optional const& aMaxTime ) { return create_straight_skeleton_2(aOuterContour_VerticesBegin diff --git a/Straight_skeleton_2/include/CGAL/create_straight_skeleton_from_polygon_with_holes_2.h b/Straight_skeleton_2/include/CGAL/create_straight_skeleton_from_polygon_with_holes_2.h index c5c5bb31bfe..c570dd2c790 100644 --- a/Straight_skeleton_2/include/CGAL/create_straight_skeleton_from_polygon_with_holes_2.h +++ b/Straight_skeleton_2/include/CGAL/create_straight_skeleton_from_polygon_with_holes_2.h @@ -24,19 +24,62 @@ CGAL_BEGIN_NAMESPACE -template + +template boost::shared_ptr< Straight_skeleton_2 > inline -create_interior_straight_skeleton_2 ( Polygon_with_holes_2 const& aPolyWithHoles ) +create_straight_skeleton_2 ( Polygon_with_holes_2 const& aPolyWithHoles, NT aWeight, boost::optional const& aMaxTime, K const& k ) { - return create_interior_straight_skeleton_2(aPolyWithHoles.outer_boundary().vertices_begin() - ,aPolyWithHoles.outer_boundary().vertices_end () - ,aPolyWithHoles.holes_begin () - ,aPolyWithHoles.holes_end () - ,K() - ); + return create_straight_skeleton_2( aPolyWithHoles.outer_boundary().vertices_begin() + , aPolyWithHoles.outer_boundary().vertices_end () + , aPolyWithHoles.holes_begin () + , aPolyWithHoles.holes_end () + , aWeight + , aMaxTime + , k + ); } +boost::shared_ptr< Straight_skeleton_2 > +inline +create_straight_skeleton_2 ( Polygon_with_holes_2 const& aPolyWithHoles + , double aWeight + , boost::optional const& aMaxTime + ) +{ + return create_straight_skeleton_2(aPolyWithHoles, aWeight, aMaxTime, Exact_predicates_inexact_constructions_kernel() ); +} + +boost::shared_ptr< Straight_skeleton_2 > +inline +create_partial_interior_straight_skeleton_2 ( Polygon_with_holes_2 const& aPolyWithHoles, boost::optional const& aMaxTime ) +{ + return create_straight_skeleton_2(aPolyWithHoles, 1.0, aMaxTime, Exact_predicates_inexact_constructions_kernel() ); +} + +boost::shared_ptr< Straight_skeleton_2 > +inline +create_interior_straight_skeleton_2 ( Polygon_with_holes_2 const& aPolyWithHoles ) +{ + return create_straight_skeleton_2(aPolyWithHoles, 1.0, boost::optional (), Exact_predicates_inexact_constructions_kernel() ); +} + +boost::shared_ptr< Straight_skeleton_2 > +inline +create_partial_exterior_straight_skeleton_2 ( Polygon_with_holes_2 const& aPolyWithHoles, boost::optional const& aMaxTime ) +{ + return create_straight_skeleton_2(aPolyWithHoles, -1.0, aMaxTime, Exact_predicates_inexact_constructions_kernel() ); +} + +boost::shared_ptr< Straight_skeleton_2 > +inline +create_exterior_straight_skeleton_2 ( Polygon_with_holes_2 const& aPolyWithHoles ) +{ + return create_straight_skeleton_2(aPolyWithHoles, -1.0, boost::optional (), Exact_predicates_inexact_constructions_kernel() ); +} + + + CGAL_END_NAMESPACE