I'm creating a S2Polyline with antipodal vertices and in most cases the polyline is valid although it should be invalid, this seems to be related to a floating point precision, could you please advice how to overcome this?
In all examples below, only points2 is invalid
std::vector<S2LatLng> points1;
points1.emplace_back(S2LatLng::FromDegrees(-90,0));
points1.emplace_back(S2LatLng::FromDegrees(90,0));
std::vector<S2LatLng> points2;
points2.emplace_back(S2LatLng::FromDegrees(0, -50));
points2.emplace_back(S2LatLng::FromDegrees(0, 130));
std::vector<S2LatLng> points3;
points3.emplace_back(S2LatLng::FromDegrees(0, -10.125));
points3.emplace_back(S2LatLng::FromDegrees(0, 169.875));
std::vector<S2LatLng> points4;
points4.emplace_back(S2LatLng::FromDegrees(0, -180));
points4.emplace_back(S2LatLng::FromDegrees(0, 0));
std::vector<S2LatLng> points5;
points5.emplace_back(S2LatLng::FromDegrees(0, 0));
points5.emplace_back(S2LatLng::FromDegrees(0, 180));
S2Polyline polyline(points3, S2Debug::DISABLE);
if (polyline.IsValid())
{
cout << "valid" << endl;
}
else
{
cout << "invalid" << endl;
}
For example, if I'm looking at points3 (and all others), the invalidation of polyline must happen here:
bool S2Polyline::FindValidationError(S2Error* error) const {
......
if (vertex(i - 1) == -vertex(i)) {
error->Init(S2Error::ANTIPODAL_VERTICES,
"Vertices %d and %d are antipodal", i - 1, i);
return true;
}
}
return false;
}
Careful inspection shows slight difference in the values, while they should be identical:
-0.17579627993435451
0.17579627993435473
Thanks a lot !