From f2ca8973dcbd27d9e8d7897c60d307d79f28bfec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Loriot?= Date: Tue, 13 Dec 2011 15:50:34 +0000 Subject: [PATCH] add an example to print a cropped Voronoi diagram --- .gitattributes | 1 + .../Triangulation_2/draw_cropped_voronoi.cpp | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 Triangulation_2/examples/Triangulation_2/draw_cropped_voronoi.cpp diff --git a/.gitattributes b/.gitattributes index 3d2ed5d0659..58a3c44ceb4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4281,6 +4281,7 @@ Triangulation_2/doc_tex/Triangulation_2_ref/insert3.gif -text svneol=unset#image Triangulation_2/doc_tex/Triangulation_2_ref/neighbors.gif -text svneol=unset#image/gif Triangulation_2/doc_tex/Triangulation_2_ref/remove.gif -text svneol=unset#image/gif Triangulation_2/doc_tex/Triangulation_2_ref/walk.gif -text svneol=unset#image/gif +Triangulation_2/examples/Triangulation_2/draw_cropped_voronoi.cpp -text Triangulation_2/examples/Triangulation_2/info_insert_with_pair_iterator_2.cpp -text Triangulation_2/examples/Triangulation_2/info_insert_with_pair_iterator_regular_2.cpp -text Triangulation_2/examples/Triangulation_2/info_insert_with_transform_iterator_2.cpp -text diff --git a/Triangulation_2/examples/Triangulation_2/draw_cropped_voronoi.cpp b/Triangulation_2/examples/Triangulation_2/draw_cropped_voronoi.cpp new file mode 100644 index 00000000000..037ad68035e --- /dev/null +++ b/Triangulation_2/examples/Triangulation_2/draw_cropped_voronoi.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +typedef CGAL::Exact_predicates_inexact_constructions_kernel K; +typedef K::Point_2 Point_2; +typedef K::Iso_rectangle_2 Iso_rectangle_2; +typedef K::Segment_2 Segment_2; +typedef K::Ray_2 Ray_2; +typedef K::Line_2 Line_2; +typedef CGAL::Delaunay_triangulation_2 Delaunay_triangulation_2; + +//A class to recover Voronoi diagram from stream. +//Rays, lines and segments are cropped to a rectangle +//so that only segments are stored +struct Cropped_voronoi_from_delaunay{ + std::list m_cropped_vd; + Iso_rectangle_2 m_bbox; + + Cropped_voronoi_from_delaunay(const Iso_rectangle_2& bbox):m_bbox(bbox){} + + template + void crop_and_extract_segment(const RSL& rsl){ + CGAL::Object obj = CGAL::intersection(rsl,m_bbox); + const Segment_2* s=CGAL::object_cast(&obj); + if (s) m_cropped_vd.push_back(*s); + } + + void operator<<(const Ray_2& ray) { crop_and_extract_segment(ray); } + void operator<<(const Line_2& line) { crop_and_extract_segment(line); } + void operator<<(const Segment_2& seg){ crop_and_extract_segment(seg); } +}; + +int main(){ + //consider some points + std::vector points; + points.push_back(Point_2(0,0)); + points.push_back(Point_2(1,1)); + points.push_back(Point_2(0,1)); + + Delaunay_triangulation_2 dt2; + //insert points into the triangulation + dt2.insert(points.begin(),points.end()); + //construct a rectangle + Iso_rectangle_2 bbox(-1,-1,2,2); + Cropped_voronoi_from_delaunay vor(bbox); + //extract the cropped Voronoi diagram + dt2.draw_dual(vor); + //print the cropped Voronoi diagram as segments + std::copy(vor.m_cropped_vd.begin(),vor.m_cropped_vd.end(), + std::ostream_iterator(std::cout,"\n")); +} +