[core] Improve LatLngBounds API

* Use "named constructors": empty, world, hull
* Make the two-argument constructor lenient (i.e., it is a hull operation)
* Add various accessors
* Enforce a single empty representation
This commit is contained in:
John Firebaugh
2016-02-02 15:04:21 -08:00
parent 032c8fba3c
commit 37c27f3a8f
10 changed files with 190 additions and 82 deletions

View File

@@ -44,21 +44,47 @@ public:
class LatLngBounds {
public:
LatLng sw = {-90, -180};
LatLng ne = {90, 180};
// Return a bounds covering the entire (unwrapped) world.
static LatLngBounds world() {
return LatLngBounds({-90, -180}, {90, 180});
}
LatLngBounds() {}
// Return the bounds consisting of the single point.
static LatLngBounds singleton(const LatLng& a) {
return LatLngBounds(a, a);
}
LatLngBounds(const LatLng& sw_, const LatLng& ne_)
: sw(sw_), ne(ne_) {}
// Return the convex hull of two points; the smallest bounds that contains both.
static LatLngBounds hull(const LatLng& a, const LatLng& b) {
LatLngBounds bounds(a, a);
bounds.extend(b);
return bounds;
}
static LatLngBounds getExtendable() {
LatLngBounds bounds;
return { bounds.ne, bounds.sw };
// Return a bounds that may serve as the identity element for the extend operation.
static LatLngBounds empty() {
LatLngBounds bounds = world();
std::swap(bounds.sw, bounds.ne);
return bounds;
}
// Constructs a LatLngBounds object with the tile's exact boundaries.
LatLngBounds(const TileID& id);
LatLngBounds(const TileID&);
double south() const { return sw.latitude; }
double west() const { return sw.longitude; }
double north() const { return ne.latitude; }
double east() const { return ne.longitude; }
LatLng southwest() const { return sw; }
LatLng northeast() const { return ne; }
LatLng southeast() const { return LatLng(south(), east()); }
LatLng northwest() const { return LatLng(north(), west()); }
LatLng center() const {
return LatLng((sw.latitude + ne.latitude) / 2,
(sw.longitude + ne.longitude) / 2);
}
void extend(const LatLng& point) {
if (point.latitude < sw.latitude) sw.latitude = point.latitude;
@@ -72,6 +98,11 @@ public:
extend(bounds.ne);
}
bool isEmpty() const {
return sw.latitude > ne.latitude ||
sw.longitude > ne.longitude;
}
bool contains(const LatLng& point) const {
return (point.latitude >= sw.latitude &&
point.latitude <= ne.latitude &&
@@ -85,6 +116,13 @@ public:
area.ne.longitude > sw.longitude &&
area.sw.longitude < ne.longitude);
}
private:
LatLng sw;
LatLng ne;
LatLngBounds(const LatLng& sw_, const LatLng& ne_)
: sw(sw_), ne(ne_) {}
};
class MetersBounds {

View File

@@ -1182,10 +1182,9 @@ jlongArray JNICALL nativeGetAnnotationsInBounds(JNIEnv *env, jobject obj, jlong
return nullptr;
}
mbgl::LatLngBounds bounds({ swLat, swLon }, { neLat, neLon });
// assume only points for now
std::vector<uint32_t> annotations = nativeMapView->getMap().getPointAnnotationsInBounds(bounds);
std::vector<uint32_t> annotations = nativeMapView->getMap().getPointAnnotationsInBounds(
mbgl::LatLngBounds::hull({ swLat, swLon }, { neLat, neLon }));
return std_vector_uint_to_jobject(env, annotations);
}

View File

@@ -21,13 +21,13 @@ NS_INLINE CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng
}
NS_INLINE MGLCoordinateBounds MGLCoordinateBoundsFromLatLngBounds(mbgl::LatLngBounds latLngBounds) {
return MGLCoordinateBoundsMake(MGLLocationCoordinate2DFromLatLng(latLngBounds.sw),
MGLLocationCoordinate2DFromLatLng(latLngBounds.ne));
return MGLCoordinateBoundsMake(MGLLocationCoordinate2DFromLatLng(latLngBounds.southwest()),
MGLLocationCoordinate2DFromLatLng(latLngBounds.northeast()));
}
NS_INLINE mbgl::LatLngBounds MGLLatLngBoundsFromCoordinateBounds(MGLCoordinateBounds coordinateBounds) {
return mbgl::LatLngBounds(MGLLatLngFromLocationCoordinate2D(coordinateBounds.sw),
MGLLatLngFromLocationCoordinate2D(coordinateBounds.ne));
return mbgl::LatLngBounds::hull(MGLLatLngFromLocationCoordinate2D(coordinateBounds.sw),
MGLLatLngFromLocationCoordinate2D(coordinateBounds.ne));
}
NS_INLINE BOOL MGLCoordinateInCoordinateBounds(CLLocationCoordinate2D coordinate, MGLCoordinateBounds coordinateBounds) {

View File

@@ -16,7 +16,7 @@ mbgl::Color MGLColorObjectFromCGColorRef(CGColorRef cgColor) {
{
CLLocationCoordinate2D *_coords;
size_t _count;
mbgl::LatLngBounds _bounds;
MGLCoordinateBounds _bounds;
}
- (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords
@@ -28,13 +28,16 @@ mbgl::Color MGLColorObjectFromCGColorRef(CGColorRef cgColor) {
{
_count = count;
_coords = (CLLocationCoordinate2D *)malloc(_count * sizeof(CLLocationCoordinate2D));
_bounds = mbgl::LatLngBounds::getExtendable();
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty();
for (NSUInteger i = 0; i < _count; i++)
{
_coords[i] = coords[i];
_bounds.extend(mbgl::LatLng(coords[i].latitude, coords[i].longitude));
bounds.extend(mbgl::LatLng(coords[i].latitude, coords[i].longitude));
}
_bounds = MGLCoordinateBoundsFromLatLngBounds(bounds);
}
return self;
@@ -93,20 +96,12 @@ mbgl::Color MGLColorObjectFromCGColorRef(CGColorRef cgColor) {
- (MGLCoordinateBounds)overlayBounds
{
return {
CLLocationCoordinate2DMake(_bounds.sw.latitude, _bounds.sw.longitude),
CLLocationCoordinate2DMake(_bounds.ne.latitude, _bounds.ne.longitude)
};
return _bounds;
}
- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds
{
mbgl::LatLngBounds area(
mbgl::LatLng(overlayBounds.sw.latitude, overlayBounds.sw.longitude),
mbgl::LatLng(overlayBounds.ne.latitude, overlayBounds.ne.longitude)
);
return _bounds.intersects(area);
return MGLLatLngBoundsFromCoordinateBounds(_bounds).intersects(MGLLatLngBoundsFromCoordinateBounds(overlayBounds));
}
- (void)addShapeAnnotationObjectToCollection:(std::vector<mbgl::ShapeAnnotation> &)shapes withDelegate:(id <MGLMultiPointDelegate>)delegate {

View File

@@ -2177,8 +2177,8 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration)
/// Converts a geographic bounding box to a rectangle in the view’s coordinate
/// system.
- (CGRect)convertLatLngBounds:(mbgl::LatLngBounds)bounds toRectToView:(nullable UIView *)view {
CGRect rect = { [self convertLatLng:bounds.sw toPointToView:view], CGSizeZero };
rect = MGLExtendRect(rect, [self convertLatLng:bounds.ne toPointToView:view]);
CGRect rect = { [self convertLatLng:bounds.southwest() toPointToView:view], CGSizeZero };
rect = MGLExtendRect(rect, [self convertLatLng:bounds.northeast() toPointToView:view]);
return rect;
}
@@ -2186,7 +2186,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration)
/// bounding box.
- (mbgl::LatLngBounds)convertRect:(CGRect)rect toLatLngBoundsFromView:(nullable UIView *)view
{
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::getExtendable();
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty();
bounds.extend([self convertPoint:rect.origin toLatLngFromView:view]);
bounds.extend([self convertPoint:{ CGRectGetMaxX(rect), CGRectGetMinY(rect) } toLatLngFromView:view]);
bounds.extend([self convertPoint:{ CGRectGetMaxX(rect), CGRectGetMaxY(rect) } toLatLngFromView:view]);
@@ -2195,26 +2195,26 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration)
// The world is wrapping if a point just outside the bounds is also within
// the rect.
mbgl::LatLng outsideLatLng;
if (bounds.sw.longitude > -180)
if (bounds.west() > -180)
{
outsideLatLng = {
(bounds.sw.latitude + bounds.ne.latitude) / 2,
bounds.sw.longitude - 1,
(bounds.south() + bounds.north()) / 2,
bounds.west() - 1,
};
}
else if (bounds.ne.longitude < 180)
else if (bounds.east() < 180)
{
outsideLatLng = {
(bounds.sw.latitude + bounds.ne.latitude) / 2,
bounds.ne.longitude + 1,
(bounds.south() + bounds.north()) / 2,
bounds.east() + 1,
};
}
// If the world is wrapping, extend the bounds to cover all longitudes.
if (CGRectContainsPoint(rect, [self convertLatLng:outsideLatLng toPointToView:view]))
{
bounds.sw.longitude = -180;
bounds.ne.longitude = 180;
bounds.extend(mbgl::LatLng(bounds.south(), -180));
bounds.extend(mbgl::LatLng(bounds.south(), 180));
}
return bounds;
@@ -2965,7 +2965,7 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration)
{
if ( ! annotations || ! annotations.count) return;
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::getExtendable();
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty();
for (id <MGLAnnotation> annotation in annotations)
{

View File

@@ -2180,8 +2180,8 @@ public:
/// Converts a geographic bounding box to a rectangle in the view’s coordinate
/// system.
- (NSRect)convertLatLngBounds:(mbgl::LatLngBounds)bounds toRectToView:(nullable NSView *)view {
NSRect rect = { [self convertLatLng:bounds.sw toPointToView:view], NSZeroSize };
rect = MGLExtendRect(rect, [self convertLatLng:bounds.ne toPointToView:view]);
NSRect rect = { [self convertLatLng:bounds.southwest() toPointToView:view], NSZeroSize };
rect = MGLExtendRect(rect, [self convertLatLng:bounds.northeast() toPointToView:view]);
return rect;
}
@@ -2192,7 +2192,7 @@ public:
/// Converts a rectangle in the given view’s coordinate system to a geographic
/// bounding box.
- (mbgl::LatLngBounds)convertRect:(NSRect)rect toLatLngBoundsFromView:(nullable NSView *)view {
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::getExtendable();
mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty();
bounds.extend([self convertPoint:rect.origin toLatLngFromView:view]);
bounds.extend([self convertPoint:{ NSMaxX(rect), NSMinY(rect) } toLatLngFromView:view]);
bounds.extend([self convertPoint:{ NSMaxX(rect), NSMaxY(rect) } toLatLngFromView:view]);
@@ -2201,22 +2201,22 @@ public:
// The world is wrapping if a point just outside the bounds is also within
// the rect.
mbgl::LatLng outsideLatLng;
if (bounds.sw.longitude > -180) {
if (bounds.west() > -180) {
outsideLatLng = {
(bounds.sw.latitude + bounds.ne.latitude) / 2,
bounds.sw.longitude - 1,
(bounds.south() + bounds.north()) / 2,
bounds.west() - 1,
};
} else if (bounds.ne.longitude < 180) {
} else if (bounds.northeast().longitude < 180) {
outsideLatLng = {
(bounds.sw.latitude + bounds.ne.latitude) / 2,
bounds.ne.longitude + 1,
(bounds.south() + bounds.north()) / 2,
bounds.east() + 1,
};
}
// If the world is wrapping, extend the bounds to cover all longitudes.
if (NSPointInRect([self convertLatLng:outsideLatLng toPointToView:view], rect)) {
bounds.sw.longitude = -180;
bounds.ne.longitude = 180;
bounds.extend(mbgl::LatLng(bounds.south(), -180));
bounds.extend(mbgl::LatLng(bounds.south(), 180));
}
return bounds;

View File

@@ -30,7 +30,7 @@
// Make Boost Geometry aware of our LatLng type
BOOST_GEOMETRY_REGISTER_POINT_2D(mbgl::LatLng, double, boost::geometry::cs::cartesian, longitude, latitude)
BOOST_GEOMETRY_REGISTER_BOX(mbgl::LatLngBounds, mbgl::LatLng, sw, ne)
BOOST_GEOMETRY_REGISTER_BOX(mbgl::LatLngBounds, mbgl::LatLng, southwest(), northeast())
namespace mbgl {

View File

@@ -242,10 +242,10 @@ void Map::setLatLngZoom(const LatLng& latLng, double zoom, const EdgeInsets& pad
CameraOptions Map::cameraForLatLngBounds(const LatLngBounds& bounds, const EdgeInsets& padding) {
AnnotationSegment segment = {
{bounds.ne.latitude, bounds.sw.longitude},
bounds.sw,
{bounds.sw.latitude, bounds.ne.longitude},
bounds.ne,
bounds.northwest(),
bounds.southwest(),
bounds.southeast(),
bounds.northeast(),
};
return cameraForLatLngs(segment, padding);
}

View File

@@ -142,18 +142,17 @@ TEST(Transform, PerspectiveProjection) {
TEST(Transform, ConstrainHeightOnly) {
MockView view;
LatLng loc;
LatLngBounds bounds;
Transform transform(view, ConstrainMode::HeightOnly);
transform.resize({{ 1000, 1000 }});
transform.setScale(1024);
transform.setLatLng(bounds.sw);
transform.setLatLng(LatLngBounds::world().southwest());
loc = transform.getState().pointToLatLng({ 500, 500 });
ASSERT_NEAR(-85.021422866378714, loc.latitude, 0.0001);
ASSERT_NEAR(180, std::abs(loc.longitude), 0.0001);
transform.setLatLng(bounds.ne);
transform.setLatLng(LatLngBounds::world().northeast());
loc = transform.getState().pointToLatLng({ 500, 500 });
ASSERT_NEAR(85.021422866378742, loc.latitude, 0.0001);
ASSERT_NEAR(180, std::abs(loc.longitude), 0.0001);
@@ -162,18 +161,17 @@ TEST(Transform, ConstrainHeightOnly) {
TEST(Transform, ConstrainWidthAndHeight) {
MockView view;
LatLng loc;
LatLngBounds bounds;
Transform transform(view, ConstrainMode::WidthAndHeight);
transform.resize({{ 1000, 1000 }});
transform.setScale(1024);
transform.setLatLng(bounds.sw);
transform.setLatLng(LatLngBounds::world().southwest());
loc = transform.getState().pointToLatLng({ 500, 500 });
ASSERT_NEAR(-85.021422866378714, loc.latitude, 0.0001);
ASSERT_NEAR(-179.65667724609375, loc.longitude, 0.0001);
transform.setLatLng(bounds.ne);
transform.setLatLng(LatLngBounds::world().northeast());
loc = transform.getState().pointToLatLng({ 500, 500 });
ASSERT_NEAR(85.021422866378742, loc.latitude, 0.0001);
ASSERT_NEAR(179.65667724609358, std::abs(loc.longitude), 0.0001);

View File

@@ -5,7 +5,86 @@
using namespace mbgl;
TEST(Geo, LatLngFromTileID) {
TEST(LatLngBounds, World) {
auto result = LatLngBounds::world();
ASSERT_DOUBLE_EQ(-90, result.south());
ASSERT_DOUBLE_EQ( 90, result.north());
ASSERT_DOUBLE_EQ(-180, result.west());
ASSERT_DOUBLE_EQ( 180, result.east());
}
TEST(LatLngBounds, Singleton) {
auto result = LatLngBounds::singleton({1, 2});
ASSERT_DOUBLE_EQ(1, result.south());
ASSERT_DOUBLE_EQ(1, result.north());
ASSERT_DOUBLE_EQ(2, result.west());
ASSERT_DOUBLE_EQ(2, result.east());
}
TEST(LatLngBounds, Hull) {
double s = 1, w = 2, n = 3, e = 4;
auto swne = LatLngBounds::hull({s, w}, {n, e});
ASSERT_DOUBLE_EQ(s, swne.south());
ASSERT_DOUBLE_EQ(n, swne.north());
ASSERT_DOUBLE_EQ(w, swne.west());
ASSERT_DOUBLE_EQ(e, swne.east());
auto nesw = LatLngBounds::hull({n, e}, {s, w});
ASSERT_DOUBLE_EQ(s, nesw.south());
ASSERT_DOUBLE_EQ(n, nesw.north());
ASSERT_DOUBLE_EQ(w, nesw.west());
ASSERT_DOUBLE_EQ(e, nesw.east());
auto senw = LatLngBounds::hull({s, e}, {n, w});
ASSERT_DOUBLE_EQ(s, senw.south());
ASSERT_DOUBLE_EQ(n, senw.north());
ASSERT_DOUBLE_EQ(w, senw.west());
ASSERT_DOUBLE_EQ(e, senw.east());
auto nwse = LatLngBounds::hull({n, w}, {s, e});
ASSERT_DOUBLE_EQ(s, nwse.south());
ASSERT_DOUBLE_EQ(n, nwse.north());
ASSERT_DOUBLE_EQ(w, nwse.west());
ASSERT_DOUBLE_EQ(e, nwse.east());
}
TEST(LatLngBounds, Empty) {
ASSERT_TRUE(LatLngBounds::empty().isEmpty());
ASSERT_FALSE(LatLngBounds::world().isEmpty());
}
TEST(LatLngBounds, Center) {
auto result = LatLngBounds::hull({1, 2}, {3, 4}).center();
ASSERT_DOUBLE_EQ(2, result.latitude);
ASSERT_DOUBLE_EQ(3, result.longitude);
}
TEST(LatLngBounds, Southwest) {
auto result = LatLngBounds::hull({1, 2}, {3, 4}).southwest();
ASSERT_DOUBLE_EQ(1, result.latitude);
ASSERT_DOUBLE_EQ(2, result.longitude);
}
TEST(LatLngBounds, Northeast) {
auto result = LatLngBounds::hull({1, 2}, {3, 4}).northeast();
ASSERT_DOUBLE_EQ(3, result.latitude);
ASSERT_DOUBLE_EQ(4, result.longitude);
}
TEST(LatLngBounds, Southeast) {
auto result = LatLngBounds::hull({1, 2}, {3, 4}).southeast();
ASSERT_DOUBLE_EQ(1, result.latitude);
ASSERT_DOUBLE_EQ(4, result.longitude);
}
TEST(LatLngBounds, Northwest) {
auto result = LatLngBounds::hull({1, 2}, {3, 4}).northwest();
ASSERT_DOUBLE_EQ(3, result.latitude);
ASSERT_DOUBLE_EQ(2, result.longitude);
}
TEST(LatLng, FromTileID) {
for (int i = 0; i < 20; i++) {
const LatLng ll{ TileID(i, 0, 0, 0) };
ASSERT_DOUBLE_EQ(-180, ll.longitude);
@@ -25,37 +104,36 @@ TEST(Geo, LatLngFromTileID) {
}
}
TEST(Geo, LatLngBoundsFromTileID) {
TEST(LatLngBounds, FromTileID) {
{
const LatLngBounds bounds{ TileID(0, 0, 0, 0) };
ASSERT_DOUBLE_EQ(-180, bounds.sw.longitude);
ASSERT_DOUBLE_EQ(-85.051128779806604, bounds.sw.latitude);
ASSERT_DOUBLE_EQ(180, bounds.ne.longitude);
ASSERT_DOUBLE_EQ(85.051128779806604, bounds.ne.latitude);
ASSERT_DOUBLE_EQ(-180, bounds.west());
ASSERT_DOUBLE_EQ(-85.051128779806604, bounds.south());
ASSERT_DOUBLE_EQ(180, bounds.east());
ASSERT_DOUBLE_EQ(85.051128779806604, bounds.north());
}
{
const LatLngBounds bounds{ TileID(1, 0, 1, 0) };
ASSERT_DOUBLE_EQ(-180, bounds.sw.longitude);
ASSERT_DOUBLE_EQ(-85.051128779806604, bounds.sw.latitude);
ASSERT_DOUBLE_EQ(0, bounds.ne.longitude);
ASSERT_DOUBLE_EQ(0, bounds.ne.latitude);
ASSERT_DOUBLE_EQ(-180, bounds.west());
ASSERT_DOUBLE_EQ(-85.051128779806604, bounds.south());
ASSERT_DOUBLE_EQ(0, bounds.east());
ASSERT_DOUBLE_EQ(0, bounds.north());
}
{
const LatLngBounds bounds{ TileID(1, 1, 1, 0) };
ASSERT_DOUBLE_EQ(0, bounds.sw.longitude);
ASSERT_DOUBLE_EQ(-85.051128779806604, bounds.sw.latitude);
ASSERT_DOUBLE_EQ(180, bounds.ne.longitude);
ASSERT_DOUBLE_EQ(0, bounds.ne.latitude);
ASSERT_DOUBLE_EQ(0, bounds.west());
ASSERT_DOUBLE_EQ(-85.051128779806604, bounds.south());
ASSERT_DOUBLE_EQ(180, bounds.east());
ASSERT_DOUBLE_EQ(0, bounds.north());
}
{
const LatLngBounds bounds{ TileID(1, 0, 0, 0) };
ASSERT_DOUBLE_EQ(-180, bounds.sw.longitude);
ASSERT_DOUBLE_EQ(0, bounds.sw.latitude);
ASSERT_DOUBLE_EQ(0, bounds.ne.longitude);
ASSERT_DOUBLE_EQ(85.051128779806604, bounds.ne.latitude);
ASSERT_DOUBLE_EQ(-180, bounds.west());
ASSERT_DOUBLE_EQ(0, bounds.south());
ASSERT_DOUBLE_EQ(0, bounds.east());
ASSERT_DOUBLE_EQ(85.051128779806604, bounds.north());
}
}