boostorg / boostorg/geometry

Geographic Intersection of Line with Box yields inconsistent behavior of bg::intersects and bg::intersection

Open
#864 9 comments 0 reactions 1 assignee Claimed by @vissarion View on GitHub
bug
Dominant language
C++
Stars
517
Forks
232
PR merge metrics
No merged PRs in 30d

Description

Dear maintainers of boost::geometry,

thank you for taking care of #851 so quickly. 😄

We have found another issue that is potentially related.
We use the same line from Stuttgart to Berlin from the setup of #851.

A box that intersects the geodesic is correctly reported as `bg::intersects`.
However, the resulting `bg::intersect` set with the box that intersects the geodesic is empty.

If we move the box towards the East so that it intersects the direct Cartesian line between the two coordinates, `bg::intersect` returns a non-empty set, however.

This is the code (modified example from #851):

``` C++
#include
#include
#include
#include

namespace bg = boost::geometry;
namespace bm = bg::model::d2;
namespace srs = bg::srs;

using Pt = bm::point_xy >;
using Box = bg::model::box;
using LineString = bg::model::linestring;

using PtCart = bm::point_xy;
using BoxCart = bg::model::box;
using LineStringCart = bg::model::linestring;

Pt Stuttgart() { return { 9.18, 48.78}; }
Pt Berlin() { return { 13.4, 52.52 }; }

PtCart StuttgartCart() { return { 9.18, 48.78}; }
PtCart BerlinCart() { return { 13.4, 52.52 }; }

Box boxWest() {
return { {11.030273, 50.493164}
, {11.052246, 50.515136}};
}

Box boxEast() {
return { {11.118164, 50.493164}
, {11.140136, 50.515136}};
}

BoxCart boxEastCart() {
return { {11.118164, 50.493164}
, {11.140136, 50.515136}};
}

LineString StuttgartBerlin() {
return {Stuttgart(), Berlin()};
}

LineStringCart StuttgartBerlinCart() {
return {StuttgartCart(), BerlinCart()};
}

int main() {
bool const intersectsWest = bg::intersects (StuttgartBerlin(), boxWest());
bool const intersectsEast = bg::intersects (StuttgartBerlin(), boxEast());
std::vector intersectedGeomWest{};
std::vector intersectedGeomEast{};
bg::intersection (StuttgartBerlin(), boxWest(), intersectedGeomWest);
bg::intersection (StuttgartBerlin(), boxEast(), intersectedGeomEast);

std::cout << "West: " << intersectsWest << ", " << intersectedGeomWest.size() << std::endl;
std::cout << "East: " << intersectsEast << ", " << intersectedGeomEast.size() << std::endl;

std::vector intersectedGeomCartMiddle{};
bool const intersectsCartesianMiddle = bg::intersects (StuttgartBerlinCart(), boxEastCart());
bg::intersection (StuttgartBerlinCart(), boxEastCart(), intersectedGeomCartMiddle);
std::cout << "Cartesian East: " << intersectsCartesianMiddle << ", " << intersectedGeomCartMiddle.size() << std::endl;
}

```

With version 1.76.0 (also when incorporating the patch from #851), the result is as follows:
```
West: 1, 0
East: 0, 1
Cartesian East: 1, 1
```
- https://godbolt.org/z/rTn3nKbf9

Please note that in older versions of boost, e.g. 1.64.0, boost will use the Cartesian intersection in any case:
```
West: 0, 0
East: 1, 1
Cartesian East: 1, 1
```
- https://godbolt.org/z/bb3xTxMoK

What I actually expect the program to print is
```
West: 1, 1
East: 0, 0
Cartesian East: 1, 1
```

Thank you in advance!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.