Repository navigation
Conversation
|
Thanks for the PR, @LeoValque. I tested c5c5535 using Hilbert_sort_middle_2<Simple_cartesian> with limit=1 and found two issues. equal_2_object()(*begin, *end) dereferences the past-the-end iterator. Two identical points already trigger an ASan heap-buffer-overflow. The coincidence check needs to cover the whole range: replacing end with end - 1 still stops prematurely for (1,1), (2,2), (1,1), (64,64). There is also a termination issue in skip_empty_cells: (0,0), (1,0), (2,0), (64,0) causes a stack overflow, even after fixing the coincidence check. Returning to sort requires an actual separation under the partition comparator. Subdivision also needs to stop when the midpoints can no longer progress. These checks are implemented in compute_bounds() and skip_empty_cells() in the candidate implementation. I suggest reusing those checks, retaining the existing empty-range guard, and adding these examples as regression tests. |
|
Hello, @LeoValque. I retested In |
|
Hi, @pedro-machado-manhaes-de-castro , I didn't reproduce this stackoverflow problem (I add tests with nextafter too), could you give more details? I consider add a tolerance parameter to stop sorting if the width of a cell become smaller than this tolerance, what do you think about? |
|
@LeoValque I think the new 2D test passes because its #include <CGAL/Simple_cartesian.h>
#include <CGAL/hilbert_sort.h>
#include <vector>
int main() {
using K = CGAL::Simple_cartesian<double>;
std::vector<K::Point_2> points{{0,0}, {1,0}, {2,0}, {64,0}};
CGAL::hilbert_sort(points.begin(), points.end(), CGAL::Hilbert_sort_middle_policy());
}On Windows with MSVC 14.51, this exits with stack overflow ( A width tolerance alone seems not good enough here: the |
|
@LeoValque. I retested |
|
Sorry for the leftover assertion. At this point, xmin == xmax because a dimension is now simply ignored when the points are axis-aligned. So I removed the assertion. |
#include <CGAL/Simple_cartesian.h>
#include <CGAL/hilbert_sort.h>
#include <vector>
int main() {
using K = CGAL::Simple_cartesian<double>;
std::vector<K::Point_2> points{{0, 8}, {8, 0}, {2, 2}, {3, 2}};
CGAL::hilbert_sort(points.begin(), points.end(), CGAL::Hilbert_sort_middle_policy());
}With GCC 15.2 ( Also I suggest the following patch: diff --git a/Spatial_sorting/include/CGAL/Hilbert_sort_middle_2.h b/Spatial_sorting/include/CGAL/Hilbert_sort_middle_2.h
index b23fcaf454ecfff622b6158ddc7a5ea22475b24b..8f2ba96eba214dc7799f8c5fd21922e8cf9bd3ea 100644
--- a/Spatial_sorting/include/CGAL/Hilbert_sort_middle_2.h
+++ b/Spatial_sorting/include/CGAL/Hilbert_sort_middle_2.h
@@ -94,12 +94,6 @@
: _k(k), _limit (limit)
{}
- // Hilbert sort use double representations so two points not strictly equal will treat identically by hilbert sort
- bool have_equal_representations(Point a, Point b) const{
- return (to_double(_k.compute_x_2_object()(a)) == to_double(_k.compute_x_2_object()(b))) &&
- (to_double(_k.compute_y_2_object()(a)) == to_double(_k.compute_y_2_object()(b)));
- }
-
template <int x, bool upx, bool upy, class RandomAccessIterator>
void skip_empty_cells (RandomAccessIterator begin, RandomAccessIterator end,
double xmin, double ymin, double xmax, double ymax, Bbox_2 bb) const
@@ -109,19 +103,19 @@
double ymed= (ymin+ymax)/2;
const bool x_degen = (xmin == xmed || xmed == xmax);
const bool y_degen = (ymin == ymed || ymed == ymax);
- const bool x_lower = (x==0) ? xmed<bb.xmin() : xmed<bb.ymin();
+ const bool x_split = (x==0) ? (bb.xmin()<xmed && xmed<=bb.xmax())
+ : (bb.ymin()<xmed && xmed<=bb.ymax());
+ const bool y_split = (x==0) ? (bb.ymin()<ymed && ymed<=bb.ymax())
+ : (bb.xmin()<ymed && ymed<=bb.xmax());
const bool x_upper = (x==0) ? xmed>bb.xmax() : xmed>bb.ymax();
- const bool y_lower = (x==0) ? ymed<bb.ymin() : ymed<bb.xmin();
const bool y_upper = (x==0) ? ymed>bb.ymax() : ymed>bb.xmax();
- if(x_degen && y_degen)
- return;
-
- if( (!x_degen && !x_lower && !x_upper) || (!y_degen && !y_lower && !y_upper)){
- // Not contain in one quarter
+ if(x_split || y_split){
sort<x, upx, upy> (begin, end, xmin, ymin, xmax, ymax);
return;
}
+ if(x_degen && y_degen)
+ return;
if( x_upper != upx )
{
@@ -159,14 +153,15 @@
((m2 == m0) && ((m3 == m2) || (m3 == m4))) )
{
// All elements are in one quarter
- bool all_equal = true;
- for(auto it=begin; it!=end-1; ++it)
- if(!have_equal_representations(*it, *(it+1))){
- all_equal = false;
- break;
- }
- if(all_equal) return;
- Bbox_2 bb=bbox_2(begin, end, Spatial_sorting::internal::Kernel_wrapper_2<Kernel>(_k));
+ const double px = to_double(_k.compute_x_2_object()(*begin));
+ const double py = to_double(_k.compute_y_2_object()(*begin));
+ Bbox_2 bb(px, py, px, py);
+ for(auto it=begin+1; it!=end; ++it){
+ const double point_x = to_double(_k.compute_x_2_object()(*it));
+ const double point_y = to_double(_k.compute_y_2_object()(*it));
+ bb += Bbox_2(point_x, point_y, point_x, point_y);
+ }
+ if(bb.xmin()==bb.xmax() && bb.ymin()==bb.ymax()) return;
skip_empty_cells<x, upx, upy> (begin, end, xmin, ymin, xmax, ymax, bb);
}
else |
|
tashimir is me (sorry to post with the wrong account) |
|
@LeoValque there is a last little thing that seems wrong that I noticed. In Using |
|
Successfully tested in CGAL-6.3-Ic-137 |
Summary of Changes
Hilber_sort_middle_2 (and 3) stops if all elements are contained in one quarter.
Release Management