/usr/include/CGAL/connect_holes.h is in libcgal-dev 4.2-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | // Copyright (c) 2007 Tel-Aviv University (Israel).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
// You can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL$
// $Id$
//
// Author(s) : Ron Wein <wein@post.tau.ac.il>
// Guy Zucker <guyzucke@post.tau.ac.il>
#ifndef CGAL_CONNECT_HOLES_H
#define CGAL_CONNECT_HOLES_H
#include <CGAL/basic.h>
#include <CGAL/Polygon_with_holes_2.h>
#include <CGAL/Arrangement_2.h>
#include <CGAL/Arr_segment_traits_2.h>
#include <CGAL/Arr_vertical_decomposition_2.h>
#include <CGAL/Boolean_set_operations_2/Gps_default_dcel.h>
#include <CGAL/General_polygon_set_2.h>
#include <CGAL/Gps_segment_traits_2.h>
#include <list>
#include <set>
#include <CGAL/Unique_hash_map.h>
#include <iostream>
namespace CGAL {
template <class HANDLE>
struct _Less_handle
{
bool operator() (const HANDLE& vh1, const HANDLE& vh2) const
{
return (&(*vh1) < &(*vh2));
}
};
/*bool is_adjacent_hole(const Halfedge_handle he, Arrangement_2 arr) {
return ((!he->twin()->face()->contained())
&& (he->twin()->face() != arr.unbounded_face()))
}*/
/*!
* Connect the given polygon with holes, turning it into a sequence of
* points, where the holes are connceted to the outer boundary using
* zero-width passages.
* For example:
* Input Output
* +----------------------------+ +-----*---------------*------+
* | | | | | |
* | +------+ +--+ | | +------+ +--+ |
* | | | | | | | | | | | |
* | +------+ \ | | | +----*-+ \ | |
* | \| | | | \| |
* | +----+ | | +----+ |
* | / | | | / | |
* | +------+ | | +------+ |
* | | | |
* +----------------------------+ +----------------------------+
*
* \param pwh The polygon with holes.
* \param oi Output: An output iterator for the points.
* \pre The polygons has an outer boundary.
* \return A past-the-end iterator of the points.
*/
template <class Kernel, class Container, class OutputIterator>
OutputIterator connect_holes(const Polygon_with_holes_2<Kernel,
Container>& pwh,
OutputIterator oi)
{
typedef Polygon_2<Kernel,Container> Polygon_2;
typedef Arr_segment_traits_2<Kernel> Traits_2;
typedef typename Kernel::Point_2 Point_2;
typedef typename Traits_2::X_monotone_curve_2 Segment_2;
typedef CGAL::Gps_default_dcel<Traits_2> dcel;
typedef General_polygon_set_2<Gps_segment_traits_2<Kernel, Container, Traits_2> , dcel>
General_polygon_set_2;
typedef Arrangement_2<Gps_segment_traits_2<Kernel, Container, Traits_2>, dcel> Arrangement_2;
typedef typename Arrangement_2::Vertex_handle Vertex_handle;
typedef typename Arrangement_2::Vertex_const_handle Vertex_const_handle;
typedef typename Arrangement_2::Halfedge_handle Halfedge_handle;
typedef typename Arrangement_2::Halfedge_const_handle Halfedge_const_handle;
typedef typename Arrangement_2::Face_handle Face_handle;
CGAL_precondition (! pwh.is_unbounded());
// In case the polygon has not holes, just go over its outer boundary
// and report the points along it.
const Polygon_2& outer_pgn = pwh.outer_boundary();
typename Polygon_2::Vertex_circulator first_v, curr_v, next_v;
if (! pwh.has_holes())
{
first_v = curr_v = outer_pgn.vertices_circulator();
do
{
*oi = *curr_v;
++oi;
++curr_v;
} while (curr_v != first_v);
return (oi);
}
// Construct the arrangement of all segments.
General_polygon_set_2 gps(pwh);
Arrangement_2 arr = gps.arrangement();
// The resulting arrangment contains a single hole in the unbounded face,
// which comprises a face f, with several holes in its interior.
// Go over these holes and pick the topmost vertex in each hole.
const Face_handle uf = arr.unbounded_face();
typename Arrangement_2::Hole_iterator f_hole_it = uf->holes_begin();
const Face_handle f = (*f_hole_it)->twin()->face();
typename Arrangement_2::Ccb_halfedge_circulator
first, circ;
Kernel ker;
typename Kernel::Compare_y_2 comp_y = ker.compare_y_2_object();
typename Kernel::Compare_x_2 comp_x = ker.compare_x_2_object();
Vertex_handle v_top;
Comparison_result res;
//construct vertex hash map (default data is false) and insert top vertices
//handles as keys (key vertex data value is true)
typedef typename CGAL::Unique_hash_map<Vertex_const_handle, bool> V_map;
V_map top_vertices(false ,arr.number_of_faces());
/*traversal of arrangement face holes - a hole in the face
in arranements is disjoint from the outer boundary (different from BOP
where the hole can have vertices along the outer boundary). We look for
holes only inside faces that are part of the point set. This
guarantees that if the input PWH had holes with vertices on the outer
boundary they will not be connected by additional vertical segments to
the outer boundary or another hole's vertex/edge.
*/
typename Arrangement_2::Face_const_handle fit;
//std::cout << arr.number_of_faces() << " faces:" << std::endl;
typename Arrangement_2::Hole_const_iterator f_hole_itc;
for (fit = arr.faces_begin(); fit != arr.faces_end() ; ++fit)
if (fit->contained()) {
for (f_hole_itc = fit->holes_begin(); f_hole_itc != fit->holes_end(); ++f_hole_itc){
// Locate the topmost vertex in the current hole. In case of two (or more)
// vertices with maximal y-coordinate, select the leftmost one.
first = circ = (arr.non_const_handle(*f_hole_itc));
v_top = circ->target();
do
{
++circ;
res = comp_y (circ->target()->point(), v_top->point());
if (res == CGAL::LARGER ||
(res == CGAL::EQUAL &&
comp_x (circ->target()->point(), v_top->point()) == CGAL::SMALLER))
{
v_top = circ->target();
}
} while (circ != first);
// std::cout << "inserted top vertex at " << v_top->point() <<std::endl;
top_vertices[Vertex_const_handle (v_top)]=true;
}
}
// Perform "vertical ray shooting" from each arrangement vertex, locating
// the features that lie below and above it.
typedef std::list<std::pair<Vertex_const_handle,
std::pair<Object, Object> > > Ray_shoot_list;
Ray_shoot_list vrs_list;
typename Ray_shoot_list::iterator vrs_iter;
decompose (arr, std::back_inserter (vrs_list));
// Go over the results of the batched vertical ray-shooting query.
Vertex_const_handle v;
Vertex_handle v_above;
Halfedge_const_handle he;
Halfedge_handle he_above;
typename Kernel::Direction_2 dir_up (0, 1);
typename Kernel::Construct_ray_2 ray = ker.construct_ray_2_object();
typename Kernel::Construct_segment_2 segment =
ker.construct_segment_2_object();
typename Kernel::Intersect_2 intersect = ker.intersect_2_object();
Object obj;
Point_2 ip;
bool assign_success;
for (vrs_iter = vrs_list.begin(); vrs_iter != vrs_list.end(); ++vrs_iter)
{
if (!top_vertices.is_defined(vrs_iter->first))
continue;
v_top = arr.non_const_handle (vrs_iter->first);
// In case the current vertex is a top vertex in one of the holes,
// add a vertical segment connecting it with the feature above it.
if (CGAL::assign (v, vrs_iter->second.second))
{
// v_top lies below a vertex v_above: Connect these two vertices.
v_above = arr.non_const_handle (v);
arr.insert_at_vertices (Segment_2 (v_top->point(), v_above->point()),
v_top, v_above);
//std::cout << "connected ((" << v_top->point() << "),( " << v_above->point() << "))" <<std::endl;
}
else if (CGAL::assign (he, vrs_iter->second.second))
{
// v_top lies below the interior of the hafledge he_above:
// Find the intersection of this halfegde with a vertical ray
// emanating from v_top.
he_above = arr.non_const_handle (he);
obj = intersect (ray (v_top->point(), dir_up),
segment (he_above->source()->point(),
he_above->target()->point()));
assign_success = CGAL::assign (ip, obj);
CGAL_assertion (assign_success);
if (assign_success)
{
// Split he_above at the computed intersection point.
arr.split_edge (he_above,
Segment_2 (he_above->source()->point(), ip),
Segment_2 (ip, he_above->target()->point()));
// Now he_above is split such that it becomes the predecessor
// halfedge for the insertion of the vertical segment connecting
// v_top and ip.
arr.insert_at_vertices (Segment_2 (v_top->point(), ip),
he_above, v_top);
//added for debugging
//std::cout << "connected ((" << v_top->point() << "),( " << ip << "))" <<std::endl;
}
}
else
{
// We should never reach here.
CGAL_error_msg("top vertex is located in an unbounded face.");
}
}
// The holes of the face f are now all connected to its outer boundary.
// Go over this boundary and report the vertices along it.
// Note that we start with a vertex located on the original PWH outer boundary.
typedef typename Arrangement_2::Halfedge_const_iterator
Halfedge_const_iterator;
/*For the traversal we switch between two states using key vertices.
A key vertex is a vertex on the outer boundary of the PWH that has a degree>2
(note that this includes new vertices added by the vertical ray shooting).
In other words, it is a vertice that leads to holes. One state is searching for a key vertice that
leads to a hole that has not been traversed. The other state is traversing (marking) a hole completely
(starting and finishing at the same key vertice). Once the hole traversal is completed
return to search state*/
//start state is search state
bool marking_hole_state = false;
/*flags used for printing the edges for debugging purposes
bool skip_print = false;
bool antenna_trav = false; */
//marker of first vertex of hole that is being traversed
Vertex_handle hole_start, empty_handle;
/*create a hash map container for outer boundary key vertices (degree>2
and on outer boundary. Hash map is constructed with a
size parameter so we'll traverse once around the outer boundary to
count the key vertices and once to insert them*/
f_hole_it = uf->holes_begin();
Halfedge_const_handle he_han = *f_hole_it;
if (he_han->face() != uf)
he_han = he_han->twin();
CGAL_assertion(he_han->face() == uf);
//std::cout << "outer boundary:" <<std::endl;
//std::cout << "(" << he_han->target()->point() << ")" <<std::endl;
//calculate num of vertices on outer boundary for hash map creation.
std::size_t size = 1;
Halfedge_const_handle begin = he_han;
he_han = he_han->next();
CGAL_assertion(he_han->face() == uf);
while (he_han != begin) {
//std::cout << "(" << he_han->target()->point() << ")" <<std::endl;
if (he_han->target()->degree()>2)
size++;
he_han = he_han->next();
CGAL_assertion(he_han->face() == uf);
}
//construct vertex hash map (default data is false) and insert vertex
//handles as keys (key vertex data value is true)
V_map ver_map(false ,size);
if (he_han->target()->degree()>2)
ver_map[he_han->target()]=true;
//std::cout << "(" << he_han->target()->point() << ")" <<std::endl;
he_han = he_han->next();
while (he_han != begin) {
CGAL_assertion(he_han->face() == uf);
if (he_han->target()->degree()>2)
ver_map[he_han->target()]=true;
//std::cout << "(" << he_han->target()->point() << ")" <<std::endl;
he_han = he_han->next();
}
//std::cout << "outer boundary finished" <<std::endl;
//get iterator to edge on outer boundary
first = f->outer_ccb();
//std::cout << "first edge is ((" << first->source()->point() << "),(" << first->target()->point() << "))" <<std::endl;
Halfedge_const_iterator start, curr, next;
start = first;
while (start->twin()->face() != uf)
start = start->next();
//std::cout << "start edge is ((" << start->source()->point() << "),(" << start->target()->point() << "))" <<std::endl;
curr = start;
do
{
/*traverse_hole (next for any vertice besides vertices on
the outer boundary with deg>2. check hole on every step and
mark new holes (if two holes are connected by a vertex)
This is true also for traversing starting with an antenna halfedge*/
if (marking_hole_state) {
/*Add current hole to search structure.
we traverse the hole fully an if we "incidently" traverse
another hole as well (joined by a vertice) both will be traversed
completely*/
//case we are starting to traverse an antenna
if (curr->face() == curr->twin()->face()) {
//antenna_trav = true ;
//std::cout << "curr edge is ((" << curr->source()->point() << "),( " << curr->target()->point() << "))" <<std::endl;
*oi = curr->target()->point();
++oi;
curr = curr->next();
}
Face_handle curr_face;
//Traversal of the hole
while (hole_start != arr.non_const_handle(curr->target())) {
curr_face = arr.non_const_handle(curr->twin()->face());
/*mark the hole as a hole that has been traversed to save
multiple traversals*/
curr_face->set_visited(true);
//std::cout << "curr edge is ((" << curr->source()->point() << "),( " << curr->target()->point() << "))" <<std::endl;
*oi = curr->target()->point();
++oi;
//"turn inside" instead of next if target is located on outer boundary
//(a key vertex encountered while traversing a hole)
if (ver_map.is_defined(curr->target())) {
curr = curr->twin()->prev()->twin();
} else {//regular advance
curr = curr->next();
}
} //exited loop target is the hole marking start vertex
hole_start=empty_handle;
marking_hole_state=false;
/* next iteration willcheck all of the edges which have (curr->target())
as their source to determine next move -
does curr->target() have more holes to traverse or do we
return to searching the outer boundary*/
} else
{//search for next hole to traverse
/*Treatment of 4 possible cases (can be narrowed to 3) - the
first two cannot co-exist here as vertices with degree of 2 that are
not on the unbounded face will be encountered when traversing holes
(different state) */
next = curr->next();
//unified 2 simple cases of traversal along outer boundary
/* case target() is a simple vertice with a single possible edge on path.
add it to output and continue searching for a hole. This is when traversing
along the outer polygon boundary*/
/*the case the next half edge is on the outer boundary meaning we finished
handling holes connected to the target vertex.
Add the target to the output, and continue searching for next hole*/
/*if (curr->target()->degree()==2) {
//std::cout << "curr edge is ((" << curr->source()->point() << "),( " << curr->target()->point() << "))" <<std::endl;
//insert target point to result output iterator
*oi = curr->target()->point();
++oi;
curr = curr->next();
//maintain the same state
continue;
}*/
if ((curr->target()->degree()==2) ||(next->twin()->face() == uf)) {
/*if (!skip_print)
std::cout << "curr edge is ((" << curr->source()->point() << "),( " << curr->target()->point() << "))" <<std::endl;
else {
skip_print=false;
antenna_trav=false;
}*/
*oi = curr->target()->point();
++oi;
curr = curr->next();
//maintain the same state
continue;
}
/*if next is a boundary of a hole that has not been traversed,
or an antenna (which should also be completely traversed until
returning to the source vertex). an antenna to the outer boundary
occurs only for polygons that are detached from the outer boundary
and therefore these holes are connected to the boundary only by a
single vertex and will be travrsed only once.
Target must be added to output and state changed.
*/
if ((!next->twin()->face()->visited())
//case of antenna
|| (next->face()==next->twin()->face())) {
/*if (!skip_print)
std::cout << "curr edge is ((" << curr->source()->point() << "),( " << curr->target()->point() << "))" <<std::endl;
else {
skip_print=false;
//can be reached after an antenna
antenna_trav=false;
}*/
//insert target point to result output iterator
*oi = curr->target()->point();
++oi;
hole_start = arr.non_const_handle(curr->target());
marking_hole_state=true;
curr = curr->next();
continue;
} else {
/*the case where target() is a part of several holes, and next is a
boundary of a hole that has been traversed. This requires
to continue "traversal alongside all edges whose target is also
curr->target() to keep looking for a hole that hasn't been traversed.
we do not insert the target to the output set to avoid duplication
with cases 2 and 3*/
/*bypass traversed hole from inside*/
/*print debugging comment:
the current edge needs to be printed now before moving on, but the
current (target) vertex will be inserted to output iterator in case 2*/
/*if (traversed_holes.find(arr.non_const_handle(curr->twin()->face())) == traversed_holes.end())
std::cout << "curr edge is ((" << curr->source()->point() << "),( " << curr->target()->point() << "))" <<std::endl;
*/
//case this is the last half edge of an antenna
/*if (antenna_trav == true) {
std::cout << "curr edge is ((" << curr->source()->point() << "),( " << curr->target()->point() << "))" <<std::endl;
antenna_trav = false;
}
skip_print = true; */
curr = next->twin()->next()->twin();
//maintain the same state
continue;
/*now curr->target() has remained the same but curr->next() will
be adjacent to a different hole or curr->next->twin->face()
will be the unbounded face*/
}
}
} while (curr != start);
return (oi);
}
} //namespace CGAL
#endif
|