Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#10658 closed Bugs (fixed)

sym_difference yields bad result for int polygons

Reported by: Volker Schöch <vschoech@…> Owned by: Barend Gehrels
Milestone: Boost 1.58.0 Component: geometry
Version: Boost 1.57.0 Severity: Regression
Keywords: sym_difference, polygon, large numbers, domain of definition, integer Cc:

Description

My "_TPolygon" type is actually a multi-polygon, based on a polygon type that is oriented counter-clockwise and open (not closed). Please consider the following example, which apparently works fine:

_TPolygon<double> polygonA; // _TPolygon<...> is actually a MULTIPOLYGON, with polygons oriented COUNTER-CLOCKWISE and OPEN (not closed).
boost::geometry::read_wkt("MULTIPOLYGON(((516 1608,1308 1932,2094 2466,2094 32767,516 32767,516 1608)))", polygonA); // does not throw
boost::geometry::correct(polygonA); // read_wkt(...) does not deal correctly with closing point

_TPolygon<double> polygonB;
boost::geometry::read_wkt("MULTIPOLYGON(((516 2484,1308 3066,2094 3150,2094 32767,516 32767,516 2484)))", polygonB); // does not throw
boost::geometry::correct(polygonB); // read_wkt(...) does not deal correctly with closing point

_TPolygon<double> polygonC;
boost::geometry::sym_difference(polygonA, polygonB, polygonC); // does not throw
// polygonC is now "MULTIPOLYGON(((516 2484,516 1608,1308 1932,2094 2466,2094 3150,1308 3066,516 2484)))"

Now, simply replace double by int:

_TPolygon<int> polygonA; // _TPolygon<...> is actually a MULTIPOLYGON, with polygons oriented COUNTER-CLOCKWISE and OPEN (not closed).
boost::geometry::read_wkt("MULTIPOLYGON(((516 1608,1308 1932,2094 2466,2094 32767,516 32767,516 1608)))", polygonA); // does not throw
boost::geometry::correct(polygonA); // read_wkt(...) does not deal correctly with closing point

_TPolygon<int> polygonB;
boost::geometry::read_wkt("MULTIPOLYGON(((516 2484,1308 3066,2094 3150,2094 32767,516 32767,516 2484)))", polygonB); // does not throw
boost::geometry::correct(polygonB); // read_wkt(...) does not deal correctly with closing point

_TPolygon<int> polygonC;
boost::geometry::sym_difference(polygonA, polygonB, polygonC); // does not throw
// polygonC is now "MULTIPOLYGON(((516 2484,516 1608,1308 1932,2094 2466,2094 *!*32725*!*,1308 3066,516 2484)))"

As you can see, one value is completely off. This seems to happen reliably whenever the input is based on int and contains large numbers. However, the particular example worked well with boost 1.55.0.

This issue may be related to Ticket #8380.

Change History (6)

comment:1 by Volker Schöch <vschoech@…>, 8 years ago

Note for think-cell: RT 10356

comment:2 by Volker Schöch <vschoech@…>, 8 years ago

Version: Boost 1.56.0Boost 1.57.0

Problem is still present in 1.57.0.

comment:3 by mkaravel, 8 years ago

Cc: mkaravel added
Milestone: To Be DeterminedBoost 1.58.0

The problem was in the computation of the intersection points. In particular, integer overflow was taking place, yielding the wrong results.

The problem has been fixed by locally promoting the coordinates to a number type with increased precision, thus avoiding the overflow.

comment:4 by mkaravel, 8 years ago

See also tickets #10835 and #11121.

comment:5 by mkaravel, 8 years ago

Cc: mkaravel removed
Resolution: fixed
Status: newclosed

comment:6 by vschoech@…, 7 years ago

A related issue came up in boost 1.59.0, see Ticket #11675.

Note: See TracTickets for help on using tickets.