CesiumGS / CesiumGS/cesium-native

`CesiumGeospatial::GlobeRectangle::computeIntersection` fails when one rectangle crosses the antimeridian and the other exactly touches it.

Open
#1,301 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
623
Forks
277
PR merge metrics
No merged PRs in 30d

Description

If I try to calculate an intersection of two rectangles with GlobeRectangle::computeIntersection, and one of those rectangles crosses the antimeridian and the other rectangle just touches the antimeridian, it will fail to find an intersection - even though they should obviously be overlapping.

You can [see a demonstration of this in CesiumJS](https://sandcastle.cesium.com/index.html#c=vZNNj9MwEIb/yiintEqcLGilpZutQKVCSHypIE65TOLZ1sLxVLbTahftf1/na6EFhLhwiO2ZvM9Y79hWzZ6thzmggxU51TZwa7mBMqr7qIyuS1ua7svmQE4r41OpHFaawHDamtaRTA9oHcyzSVqzcR4Oio5k4QYMHcfi4mufi6fyKzYelSFbRrPrUxpPwQ3VHs1WU/xMvEggzcVFGPt1WA7wAFZ/AtP36Hfi09uezq/C9Fzklx2fX53vbkk+gaHeYEWQ8corcgKljL93agCDDS1CvzYkwVs0Trd10IUKI15GyaB8yixgZAFqZiuVQU9uAZhM6SYkrEK9mFysWLMVm/VrcVR+90rvdxjn4nI2Eg/9/HDuYmuJzP/3Uf3Nx5vNev3hH5ywJqF5G58fqQi3kawLoWITYwLV7Ac4er1nbr5wfOZ8kEVJVDh/p2nZbf1SDY+htToWIvPU7HVnKKva+ht5UbsOM0U2IYVUB1Dy5je3GWqNzoU/t63Wn9V96N6yyIL+BNOMoWnbjweyGu86ye5i+W5ICiGKLIS/Up5ZV2h/qvgI), because the intersection code is the same. Even though the two rectangles are clearly overlapping in the image, the intersection calculation returns undefined. If you change the west coordinate of rectangle b to some other value (even one very close to -Math.PI, like -3.1415926535), suddenly it can correctly find the intersection.

It looks like normally what happens is that the intersection function adds 2pi to b.west because a crosses the antimeridian, and then later on `negativePiToPi` turns it back around. So -3.14 becomes 3.14 becomes -3.14 again and all works out. But with -Math.PI it becomes Math.PI from the addition of 2pi, which `negativePiToPi` takes to mean nothing needs to be done and returns the value as is. Changing the check from `angle <= Math::OnePi` to `angle < Math::OnePi` in `negativePiToPi` fixes the issue, though I'm not convinced this is the actual solution (especially because CesiumJS has been running with this exact code for 15 years).

Contributor guide

Open the contributing guide

Research direction

Start with CesiumGeospatial::GlobeRectangle::computeIntersection and the negativePiToPi handling described in the issue. Reproduce the case where one rectangle crosses the antimeridian and the other touches it at -Math.PI, then add a regression test and verify that the intersection is defined without breaking nearby boundary cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
computer-graphics
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.