-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[core] Guard against camera's minScale
being NaN
#5693
Conversation
@@ -509,7 +509,7 @@ CameraOptions Map::cameraForLatLngs(const std::vector<LatLng>& latLngs, optional | |||
scaleX -= (padding->left + padding->right) / width; | |||
scaleY -= (padding->top + padding->bottom) / height; | |||
} | |||
double minScale = ::fmin(scaleX, scaleY); | |||
double minScale = (isnan(scaleX) && isnan(scaleY)) ? INFINITY : ::fmin(scaleX, scaleY); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of checking for NaN here, I think perhaps we should condition some of the logic above on whether width
or height
is zero.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, this is probably better:
// Calculate the zoom level.
double minScale = INFINITY;
if (width > 0 || height > 0) {
double scaleX = getWidth() / width;
double scaleY = getHeight() / height;
if (padding && *padding) {
scaleX -= (padding->left + padding->right) / width;
scaleY -= (padding->top + padding->bottom) / height;
}
minScale = ::fmin(scaleX, scaleY);
}
double zoom = util::log2(getScale() * minScale);
zoom = util::clamp(zoom, getMinZoom(), getMaxZoom());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
`-showAnnotations:animated:` is broken for a single point: mapbox/mapbox-gl-native#5693.
scaleX -= (padding->left + padding->right) / width; | ||
scaleY -= (padding->top + padding->bottom) / height; | ||
} | ||
minScale = ::fmin(scaleX, scaleY); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: We have a generic util::min
in mbgl/math/minmax.hpp
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switched in b92a2f3.
Good catch @friedbunny 👍 |
Fixes a bug where calculating the padded bounds for a single point would cause division by zero, resulting in NaN for `minScale`. This invalid `minScale` would then be used to create an invalid padded `centerPixel`.
b92a2f3
to
09221ec
Compare
For posterity, the symptoms of this bug were: using |
Fixes a bug where calculating a camera's padded bounds for a single point would cause division by zero, resulting in NaN for
minScale
. This invalidminScale
would then be used to create an invalid paddedcenterPixel
.This was introduced by 8e30a4a in #4285, where the scale and padding calculations were split.
The “Add Custom Callout Point” option in ios-app exercises this bug, where the map zooms in on a single annotation, à la:
/cc @brunoabinader @1ec5