cgal icon indicating copy to clipboard operation
cgal copied to clipboard

Incorrect Orthogonal Polygon Visibility

Open gfonsecabr opened this issue 4 years ago • 2 comments

Issue Details

I run a visibility query in a simple orthogonal polygon where the query point is a vertex of the polygon. In release mode I get the wrong visibility region, while in debug mode I get the following assertion exception:

terminate called after throwing an instance of 'CGAL::Assertion_exception'
  what():  CGAL ERROR: assertion violation!
Expr: !he->face()->is_unbounded()
File: /home/fonseca/Documents/src/socg23challenge/cgal/Visibility_2/include/CGAL/Triangular_expansion_visibility_2.h
Line: 269

In release mode I get:

Regularized visibility region of q has 6 edges.
Boundary edges of the visibility region:
[3 2 -> 3 1]
[3 1 -> 2 1]
[2 1 -> 2 2]
[2 2 -> -0 -0]
[-0 -0 -> 4 2]
[4 2 -> 3 2]

The correct answer would be:

Regularized visibility region of q has 5 edges.
Boundary edges of the visibility region:
[3 2 -> 3 1]
[3 1 -> 3 0]
[3 0 -> 4 0]
[4 0 -> 4 2]
[4 2 -> 3 2]

A figure to make it easier to see: visbug

Source Code

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Triangular_expansion_visibility_2.h>
#include <CGAL/Arr_segment_traits_2.h>
#include <CGAL/Arrangement_2.h>
#include <iostream>
#include <vector>
// Define the used kernel and arrangement
typedef CGAL::Exact_predicates_exact_constructions_kernel       Kernel;
typedef Kernel::Point_2                                         Point_2;
typedef Kernel::Segment_2                                       Segment_2;
typedef CGAL::Arr_segment_traits_2<Kernel>                      Traits_2;
typedef CGAL::Arrangement_2<Traits_2>                           Arrangement_2;
typedef Arrangement_2::Halfedge_const_handle                    Halfedge_const_handle;
typedef Arrangement_2::Face_handle                              Face_handle;
// Define the used visibility class
typedef CGAL::Triangular_expansion_visibility_2<Arrangement_2>  TEV;
int main() {
  // Defining the input geometry
  Point_2 p1(1,0), p2(4, 0), p3(4,2), p4(3,2), p5(3,1), p6(2,1), p7(2,2), p8(1,2);
  std::vector<Segment_2> segments;
  segments.push_back(Segment_2(p1,p2));
  segments.push_back(Segment_2(p2,p3));
  segments.push_back(Segment_2(p3,p4));
  segments.push_back(Segment_2(p4,p5));
  segments.push_back(Segment_2(p5,p6));
  segments.push_back(Segment_2(p6,p7));
  segments.push_back(Segment_2(p7,p8));
  segments.push_back(Segment_2(p8,p1));
  // insert geometry into the arrangement
  Arrangement_2 env;
  CGAL::insert_non_intersecting_curves(env,segments.begin(),segments.end());
  //Find the halfedge whose target is the query point.
  //(usually you may know that already by other means)
  Point_2 query_point = p4;
  Halfedge_const_handle he = env.halfedges_begin();
  while (he->target()->point() != query_point)
    he++;
  //visibility query
  Arrangement_2 output_arr;
  TEV tev(env);
  Face_handle fh = tev.compute_visibility(query_point, he, output_arr);
  //print out the visibility region.
  std::cout << "Regularized visibility region of q has "
            << output_arr.number_of_edges()
            << " edges." << std::endl;
  std::cout << "Boundary edges of the visibility region:" << std::endl;
  Arrangement_2::Ccb_halfedge_circulator curr = fh->outer_ccb();
  std::cout << "[" << curr->source()->point() << " -> " << curr->target()->point() << "]" << std::endl;
  while (++curr != fh->outer_ccb())
    std::cout << "[" << curr->source()->point() << " -> " << curr->target()->point() << "]"<< std::endl;
  return 0;
}

Environment

  • Operating system (Windows/Mac/Linux, 32/64 bits): Fedora Linux
  • Compiler: g++ (GCC) 12.2.1 20220819 (Red Hat 12.2.1-2)
  • Release or debug mode: Both
  • CGAL version: Master

gfonsecabr avatar Oct 29 '22 17:10 gfonsecabr

I found a workaround for this example, but it may indicate a bug in the arrangement insertion. If I replace the line

CGAL::insert_non_intersecting_curves(env,segments.begin(),segments.end());

by

for(Segment_2 s : segments)
  CGAL::insert(env,s);

then it works fine! Also, the following does not work (same problem as the original):

CGAL::insert(env,segments.begin(),segments.end());

Which makes me wonder if this is a reliable work around or just a coincidence.

gfonsecabr avatar Oct 30 '22 20:10 gfonsecabr

Sorry, I believe this is not a bug. The while loop should have assured that the half edge is the inside one, not the outside one:

while (he->target()->point() != query_point || he->source()->point() != p3)
    he++;

gfonsecabr avatar Oct 31 '22 15:10 gfonsecabr