intersection(Iso_rectangle_2, Triangle_2) returns the whole rectangle when the triangle only touches a corner
Issue Details
When a vertex of the triangle lies on a corner of the iso rectangle and both triangle edges at that vertex go away from the rectangle, the intersection is just that corner point. CGAL::intersection returns the whole rectangle as a polygon instead. This happens with exact kernels too, so it isn't a precision issue.
The same wrong polygon appears whenever an edge of the triangle enters or leaves the rectangle exactly at a corner and the result would otherwise be smaller: comparing against clipping the triangle by the four half-planes of the rectangle (rational arithmetic), about 0.3–0.6% of random small-integer cases with non-degenerate rectangles and triangles get a wrong result on 6.1.x and main.
The cause seems to be in Iso_rectangle_2_Triangle_2.h: an intersection point at a corner lies on two sides of the rectangle, so the loops over the sides find it twice. The second hit is taken as the edge crossing another side, and the corners in between are added to the result.
Source Code
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/intersections.h>
#include <iostream>
typedef CGAL::Exact_predicates_exact_constructions_kernel K;
typedef K::Point_2 P;
int main()
{
K::Iso_rectangle_2 r(P(-3, -1), P(-2, 1));
K::Triangle_2 t(P(0, -3), P(-2, 1), P(2, 3)); // (-2, 1) is the upper right corner of r
auto res = CGAL::intersection(r, t);
if (const std::vector<P>* pol = std::get_if<std::vector<P>>(&*res))
{
for (const P& p : *pol)
std::cout << "(" << p << ") ";
std::cout << std::endl;
}
}
This prints (-2 1) (-3 1) (-3 -1) (-2 -1), the whole rectangle, while the expected result is the point (-2 1). Two more cases:
Iso_rectangle_2(P(-2, -2), P(-1, 0)),Triangle_2(P(0, 3), P(-1, 0), P(3, 0))returns the whole rectangle instead of(-1 0)Iso_rectangle_2(P(-3, -3), P(-2, -1)),Triangle_2(P(-1, 1), P(-1, 2), P(-2, -1))returns the whole rectangle instead of(-2 -1)
Environment
- Operating system (Windows/Mac/Linux, 32/64 bits): Linux 64 bits
- Compiler: GCC 13.3
- Release or debug mode: both
- Specific flags used (if any): -frounding-math
- CGAL version: 6.1.x-branch and main
- Boost version: 1.83
- Other libraries versions if used (Eigen, TBB, etc.): GMP