Pergunta

I have a very stupid question about CGAL::do_intersec() function.

Here is my code:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Boolean_set_operations_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_2                                   Point_2;
typedef CGAL::Polygon_2<Kernel>                           Polygon_2;

int main ()
{
  Polygon_2 P;
  P.push_back (Point_2 (-1,1));
  P.push_back (Point_2 (0,-1));
  P.push_back (Point_2 (1,1));

  /* DOESN'T WORK!!!! WHY???????
  P.push_back (Point_2 (-1,1));
  P.push_back (Point_2 (1,1));
  P.push_back (Point_2 (1,-1));
  P.push_back (Point_2 (-1,-1));
  */


  Polygon_2 Q;
  Q.push_back(Point_2 (-1,-1));
  Q.push_back(Point_2 (1,-1));
  Q.push_back(Point_2 (0,1));
  if ((CGAL::do_intersect (P, Q)))
    std::cout << "The two polygons intersect in their interior." << std::endl;
  else
    std::cout << "The two polygons do not intersect." << std::endl;
  return 0;
}

So, P is a triangle now, and the program works fine. But if it is changed to a rectangle, it doesn't work.

My question is how can we test two rectangle and know if they intersect? How about rectangle-triangle, rectangle-circle?

Thanks a lot!


Thanks sloriot, problem solved. However, new problem comes.....

These code failed again. Could you help me to have a look at it? All the point values are correct. I have checked that.

Point points1[] = {Point(mPointsOfSkt1[0].x, mPointsOfSkt1[0].y), Point(mPointsOfSkt1[1].x, mPointsOfSkt1[1].y),Point(mPointsOfSkt1[2].x, mPointsOfSkt1[2].y), Point(mPointsOfSkt1[3].x, mPointsOfSkt1[3].y)};
Point points2[] = {Point(mPointsOfSkt2[0].x, mPointsOfSkt2[0].y), Point(mPointsOfSkt2[1].x, mPointsOfSkt2[1].y),Point(mPointsOfSkt2[2].x, mPointsOfSkt2[2].y), Point(mPointsOfSkt2[3].x, mPointsOfSkt2[3].y)};

Polygon2d a = Polygon2d(points1, points1+4);
Polygon2d b = Polygon2d(points2, points2+4);


return CGAL::do_intersect( a, b );

The error message is:

Line       : 313
Explanation: The polygon has a wrong orientation.
Refer to the bug-reporting instructions at http://www.cgal.org/bug_report.html
terminate called after throwing an instance of 'CGAL::Precondition_exception'
  what():  CGAL ERROR: precondition violation!
Expr: is_valid_unknown_polygon(p, t)
File: /usr/include/CGAL/General_polygon_set_on_surface_2.h
Line: 44

Aha! I figured it out myself! The order of vertices of a polygon_2 has to be counter-clockwise. Thank you so much, man!

Foi útil?

Solução

You need to use a kernel with exact constructions like CGAL::Exact_predicates_exact_constructions_kernel to use this function (it is misleading but it's not a function from the kernel)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top