intersection(Segment_2, Ray_2) returns the wrong part of the segment when they are collinear
Issue Details
When a Ray_2 and a Segment_2 are collinear and the ray starts inside the segment, CGAL::intersection() returns the wrong part of the segment if the ray points towards smaller coordinates: instead of the part from the ray's source to the end of the segment the ray points to, it returns the part behind the source, which is not on the ray at all. do_intersect() is not affected.
The cause is in Ray_2_Segment_2_pair::intersection_type() (Intersections_2/include/CGAL/Intersections_2/Ray_2_Segment_2.h). In the collinear case the endpoints of the segment are sorted along x (or y) into minpt and maxpt. When the ray points to larger coordinates the result correctly goes from the ray's source to maxpt, but when it points to smaller coordinates it also uses maxpt (lines 147 and 195) where it should use minpt.
The existing tests in test_intersections_2.cpp only cover a collinear ray pointing to larger coordinates, or a ray covering the whole segment.
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;
typedef K::Segment_2 S;
typedef K::Ray_2 R;
int main()
{
// collinear, the ray starts inside the segment and points to smaller x
S s(P(0, 0), P(10, 0));
R r(P(4, 0), P(-10, 0));
auto res = CGAL::intersection(s, r);
std::cout << *std::get_if<S>(&*res) << "\n"; // expected 4 0 0 0
}
This prints 4 0 10 0. The same happens in the y branch (e.g. the segment (1 -1, 1 3) and the ray from (1 1) through (1 -3) give (1 1, 1 3) instead of (1 1, 1 -1)), and with Epick and Simple_cartesian.
Environment
- Operating system (Windows/Mac/Linux, 32/64 bits): Linux (Ubuntu 24.04), 64 bits
- Compiler: GCC 13.3
- Release or debug mode: both
- Specific flags used (if any): -std=c++17
- CGAL version: main (cbc3b81d) and 6.1.x-branch (0490d81a)
- Boost version: 1.83
- Other libraries versions if used (Eigen, TBB, etc.): GMP 6.3.0, MPFR 4.2.1