Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Use geometry.hpp types for shape annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jun 2, 2016
1 parent aa1a54c commit 8985b13
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 142 deletions.
11 changes: 4 additions & 7 deletions include/mbgl/annotation/shape_annotation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
#include <mbgl/annotation/annotation.hpp>
#include <mbgl/style/types.hpp>

#include <mbgl/util/geo.hpp>
#include <mbgl/util/geometry.hpp>
#include <mbgl/util/variant.hpp>

namespace mbgl {

using AnnotationSegment = std::vector<LatLng>;
using AnnotationSegments = std::vector<AnnotationSegment>;

struct FillAnnotationProperties {
float opacity = 1;
Color color = {{ 0, 0, 0, 1 }};
Expand All @@ -30,10 +27,10 @@ class ShapeAnnotation {
LineAnnotationProperties, // creates a line annotation
std::string>; // creates an annotation whose type and properties are sourced from a style layer

ShapeAnnotation(const AnnotationSegments& segments_, const Properties& properties_)
: segments(segments_), properties(properties_) {}
ShapeAnnotation(const Geometry<double>& geometry_, const Properties& properties_)
: geometry(geometry_), properties(properties_) {}

const AnnotationSegments segments;
const Geometry<double> geometry;
const Properties properties;
};

Expand Down
4 changes: 3 additions & 1 deletion include/mbgl/platform/default/glfw_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <mbgl/mbgl.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/timer.hpp>
#include <mbgl/util/geometry.hpp>

#ifdef MBGL_USE_GLES2
#define GLFW_INCLUDE_ES2
Expand Down Expand Up @@ -43,7 +44,8 @@ class GLFWView : public mbgl::View {
void report(float duration);

private:
mbgl::LatLng makeRandomPoint() const;
mbgl::LatLng makeRandomLatLng() const;
mbgl::Point<double> makeRandomPoint() const;
static std::shared_ptr<const mbgl::SpriteImage>
makeSpriteImage(int width, int height, float pixelRatio);

Expand Down
74 changes: 33 additions & 41 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,35 +248,6 @@ jni::jarray<jlong>* std_vector_uint_to_jobject(JNIEnv *env, std::vector<uint32_t
return &jarray;
}

mbgl::AnnotationSegment annotation_segment_from_latlng_jlist(JNIEnv *env, jni::jobject* jlist) {
mbgl::AnnotationSegment segment;

NullCheck(*env, jlist);
jni::jarray<jni::jobject>* jarray =
reinterpret_cast<jni::jarray<jni::jobject>*>(jni::CallMethod<jni::jobject*>(*env, jlist, *listToArrayId));

NullCheck(*env, jarray);
std::size_t len = jni::GetArrayLength(*env, *jarray);

segment.reserve(len);

for (std::size_t i = 0; i < len; i++) {
jni::jobject* latLng = reinterpret_cast<jni::jobject*>(jni::GetObjectArrayElement(*env, *jarray, i));
NullCheck(*env, latLng);

jdouble latitude = jni::GetField<jdouble>(*env, latLng, *latLngLatitudeId);
jdouble longitude = jni::GetField<jdouble>(*env, latLng, *latLngLongitudeId);

segment.push_back(mbgl::LatLng(latitude, longitude));
jni::DeleteLocalRef(*env, latLng);
}

jni::DeleteLocalRef(*env, jarray);
jarray = nullptr;

return segment;
}

static std::vector<uint8_t> metadata_from_java(JNIEnv* env, jni::jarray<jbyte>& j) {
mbgl::Log::Debug(mbgl::Event::JNI, "metadata_from_java");
std::size_t length = jni::GetArrayLength(*env, j);
Expand Down Expand Up @@ -756,6 +727,33 @@ static mbgl::Color toColor(jint color) {
return {{ r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f }};
}

template <class Geometry>
Geometry toGeometry(JNIEnv *env, jni::jobject* jlist) {
NullCheck(*env, jlist);
jni::jarray<jni::jobject>* jarray =
reinterpret_cast<jni::jarray<jni::jobject>*>(jni::CallMethod<jni::jobject*>(*env, jlist, *listToArrayId));
NullCheck(*env, jarray);

Geometry geometry;
geometry.reserve(jni::GetArrayLength(*env, *jarray));

for (std::size_t i = 0; i < geometry.size(); i++) {
jni::jobject* latLng = reinterpret_cast<jni::jobject*>(jni::GetObjectArrayElement(*env, *jarray, i));
NullCheck(*env, latLng);

geometry.push_back(mbgl::Point<double>(
jni::GetField<jdouble>(*env, latLng, *latLngLongitudeId),
jni::GetField<jdouble>(*env, latLng, *latLngLatitudeId)));

jni::DeleteLocalRef(*env, latLng);
}

jni::DeleteLocalRef(*env, jarray);
jni::DeleteLocalRef(*env, jlist);

return geometry;
}

jni::jarray<jlong>* nativeAddPolylines(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jarray<jni::jobject>* jarray) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeAddPolylines");
assert(nativeMapViewPtr != 0);
Expand All @@ -776,10 +774,7 @@ jni::jarray<jlong>* nativeAddPolylines(JNIEnv *env, jni::jobject* obj, jlong nat
lineProperties.width = jni::GetField<jfloat>(*env, polyline, *polylineWidthId);

jni::jobject* points = jni::GetField<jni::jobject*>(*env, polyline, *polylinePointsId);
mbgl::AnnotationSegment segment = annotation_segment_from_latlng_jlist(env, points);
jni::DeleteLocalRef(*env, points);

shapes.emplace_back(mbgl::AnnotationSegments { segment }, lineProperties);
shapes.emplace_back(toGeometry<mbgl::LineString<double>>(env, points), lineProperties);

jni::DeleteLocalRef(*env, polyline);
}
Expand Down Expand Up @@ -807,10 +802,7 @@ jni::jarray<jlong>* nativeAddPolygons(JNIEnv *env, jni::jobject* obj, jlong nati
fillProperties.color = toColor(jni::GetField<jint>(*env, polygon, *polygonFillColorId));

jni::jobject* points = jni::GetField<jni::jobject*>(*env, polygon, *polygonPointsId);
mbgl::AnnotationSegment segment = annotation_segment_from_latlng_jlist(env, points);
jni::DeleteLocalRef(*env, points);

shapes.emplace_back(mbgl::AnnotationSegments { segment }, fillProperties);
shapes.emplace_back(mbgl::Polygon<double> { toGeometry<mbgl::LinearRing<double>>(env, points) }, fillProperties);

jni::DeleteLocalRef(*env, polygon);
}
Expand Down Expand Up @@ -899,17 +891,17 @@ void nativeSetVisibleCoordinateBounds(JNIEnv *env, jni::jobject* obj, jlong nati
std::size_t count = jni::GetArrayLength(*env, *coordinates);

mbgl::EdgeInsets mbglInsets = {top, left, bottom, right};
mbgl::AnnotationSegment segment;
segment.reserve(count);
std::vector<mbgl::LatLng> latLngs;
latLngs.reserve(count);

for (std::size_t i = 0; i < count; i++) {
jni::jobject* latLng = jni::GetObjectArrayElement(*env, *coordinates, i);
jdouble latitude = jni::GetField<jdouble>(*env, latLng, *latLngLatitudeId);
jdouble longitude = jni::GetField<jdouble>(*env, latLng, *latLngLongitudeId);
segment.push_back(mbgl::LatLng(latitude, longitude));
latLngs.push_back(mbgl::LatLng(latitude, longitude));
}

mbgl::CameraOptions cameraOptions = nativeMapView->getMap().cameraForLatLngs(segment, mbglInsets);
mbgl::CameraOptions cameraOptions = nativeMapView->getMap().cameraForLatLngs(latLngs, mbglInsets);
if (direction >= 0) {
// convert from degrees to radians
cameraOptions.angle = (-direction * M_PI) / 180;
Expand Down
16 changes: 0 additions & 16 deletions platform/darwin/src/MGLMultiPoint.mm
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,6 @@ - (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds
return MGLLatLngBoundsFromCoordinateBounds(_bounds).intersects(MGLLatLngBoundsFromCoordinateBounds(overlayBounds));
}

- (mbgl::AnnotationSegments)annotationSegments {
NSUInteger count = self.pointCount;
CLLocationCoordinate2D *coordinates = self.coordinates;

mbgl::AnnotationSegment segment;
segment.reserve(count);
for (NSUInteger i = 0; i < count; i++) {
segment.push_back(MGLLatLngFromLocationCoordinate2D(coordinates[i]));
}
return { segment };
}

- (mbgl::ShapeAnnotation::Properties)shapeAnnotationPropertiesObjectWithDelegate:(__unused id <MGLMultiPointDelegate>)delegate {
return mbgl::ShapeAnnotation::Properties();
}

- (NSString *)description
{
return [NSString stringWithFormat:@"<%@: %p; count = %lu; bounds = %@>",
Expand Down
7 changes: 2 additions & 5 deletions platform/darwin/src/MGLMultiPoint_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count;
- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds;

/** Returns the shape’s annotation segments. */
- (mbgl::AnnotationSegments)annotationSegments;

/** Constructs a shape annotation properties object by asking the delegate for style values. */
- (mbgl::ShapeAnnotation::Properties)shapeAnnotationPropertiesObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate;
/** Constructs a shape annotation object, asking the delegate for style values. */
- (mbgl::ShapeAnnotation)shapeAnnotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate;

@end

Expand Down
31 changes: 18 additions & 13 deletions platform/darwin/src/MGLPolygon.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,31 @@ - (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUI
return self;
}

- (mbgl::AnnotationSegments)annotationSegments {
auto segments = super.annotationSegments;
for (MGLPolygon *polygon in self.interiorPolygons) {
auto interiorSegments = polygon.annotationSegments;
segments.push_back(interiorSegments.front());
- (mbgl::LinearRing<double>)ring {
NSUInteger count = self.pointCount;
CLLocationCoordinate2D *coordinates = self.coordinates;

mbgl::LinearRing<double> result;
result.reserve(self.pointCount);
for (NSUInteger i = 0; i < count; i++) {
result.push_back(mbgl::Point<double>(coordinates[i].longitude, coordinates[i].latitude));
}
return segments;
return result;
}

- (mbgl::ShapeAnnotation::Properties)shapeAnnotationPropertiesObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
mbgl::ShapeAnnotation::Properties shapeProperties = [super shapeAnnotationPropertiesObjectWithDelegate:delegate];

- (mbgl::ShapeAnnotation)shapeAnnotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
mbgl::FillAnnotationProperties fillProperties;
fillProperties.opacity = [delegate alphaForShapeAnnotation:self];
fillProperties.outlineColor = [delegate strokeColorForShapeAnnotation:self];
fillProperties.color = [delegate fillColorForPolygonAnnotation:self];

shapeProperties.set<mbgl::FillAnnotationProperties>(fillProperties);

return shapeProperties;

mbgl::Polygon<double> geometry;
geometry.push_back(self.ring);
for (MGLPolygon *polygon in self.interiorPolygons) {
geometry.push_back(polygon.ring);
}

return mbgl::ShapeAnnotation(geometry, fillProperties);
}

@end
Expand Down
19 changes: 12 additions & 7 deletions platform/darwin/src/MGLPolyline.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ + (instancetype)polylineWithCoordinates:(CLLocationCoordinate2D *)coords
return [[self alloc] initWithCoordinates:coords count:count];
}

- (mbgl::ShapeAnnotation::Properties)shapeAnnotationPropertiesObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
mbgl::ShapeAnnotation::Properties shapeProperties = [super shapeAnnotationPropertiesObjectWithDelegate:delegate];

- (mbgl::ShapeAnnotation)shapeAnnotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
mbgl::LineAnnotationProperties lineProperties;
lineProperties.opacity = [delegate alphaForShapeAnnotation:self];
lineProperties.color = [delegate strokeColorForShapeAnnotation:self];
lineProperties.width = [delegate lineWidthForPolylineAnnotation:self];

shapeProperties.set<mbgl::LineAnnotationProperties>(lineProperties);

return shapeProperties;

NSUInteger count = self.pointCount;
CLLocationCoordinate2D *coordinates = self.coordinates;

mbgl::LineString<double> geometry;
geometry.reserve(self.pointCount);
for (NSUInteger i = 0; i < count; i++) {
geometry.push_back(mbgl::Point<double>(coordinates[i].longitude, coordinates[i].latitude));
}

return mbgl::ShapeAnnotation(geometry, lineProperties);
}

@end
Expand Down
23 changes: 11 additions & 12 deletions platform/default/glfw_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,17 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action,
}
}

mbgl::LatLng GLFWView::makeRandomPoint() const {
mbgl::LatLng GLFWView::makeRandomLatLng() const {
const double x = width * double(std::rand()) / RAND_MAX;
const double y = height * double(std::rand()) / RAND_MAX;
return map->latLngForPixel({ x, y });
}

mbgl::Point<double> GLFWView::makeRandomPoint() const {
mbgl::LatLng latLng = makeRandomLatLng();
return { latLng.longitude, latLng.latitude };
}

std::shared_ptr<const mbgl::SpriteImage>
GLFWView::makeSpriteImage(int width, int height, float pixelRatio) {
const int r = 255 * (double(std::rand()) / RAND_MAX);
Expand Down Expand Up @@ -261,7 +266,7 @@ void GLFWView::addRandomCustomPointAnnotations(int count) {
const auto name = std::string{ "marker-" } + mbgl::util::toString(spriteID++);
map->addAnnotationIcon(name, makeSpriteImage(22, 22, 1));
spriteIDs.push_back(name);
points.emplace_back(makeRandomPoint(), name);
points.emplace_back(makeRandomLatLng(), name);
}

auto newIDs = map->addPointAnnotations(points);
Expand All @@ -272,7 +277,7 @@ void GLFWView::addRandomPointAnnotations(int count) {
std::vector<mbgl::PointAnnotation> points;

for (int i = 0; i < count; i++) {
points.emplace_back(makeRandomPoint(), "default_marker");
points.emplace_back(makeRandomLatLng(), "default_marker");
}

auto newIDs = map->addPointAnnotations(points);
Expand All @@ -286,15 +291,9 @@ void GLFWView::addRandomShapeAnnotations(int count) {
properties.opacity = .1;

for (int i = 0; i < count; i++) {
mbgl::AnnotationSegment triangle;
triangle.push_back(makeRandomPoint());
triangle.push_back(makeRandomPoint());
triangle.push_back(makeRandomPoint());

mbgl::AnnotationSegments segments;
segments.push_back(triangle);

shapes.emplace_back(segments, properties);
mbgl::Polygon<double> triangle;
triangle.push_back({ makeRandomPoint(), makeRandomPoint(), makeRandomPoint() });
shapes.emplace_back(triangle, properties);
}

auto newIDs = map->addShapeAnnotations(shapes);
Expand Down
10 changes: 5 additions & 5 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2309,14 +2309,14 @@ - (void)_setVisibleCoordinates:(CLLocationCoordinate2D *)coordinates count:(NSUI
[self willChangeValueForKey:@"visibleCoordinateBounds"];
mbgl::EdgeInsets padding = MGLEdgeInsetsFromNSEdgeInsets(insets);
padding += MGLEdgeInsetsFromNSEdgeInsets(self.contentInset);
mbgl::AnnotationSegment segment;
segment.reserve(count);
std::vector<mbgl::LatLng> latLngs;
latLngs.reserve(count);
for (NSUInteger i = 0; i < count; i++)
{
segment.push_back({coordinates[i].latitude, coordinates[i].longitude});
latLngs.push_back({coordinates[i].latitude, coordinates[i].longitude});
}

mbgl::CameraOptions cameraOptions = _mbglMap->cameraForLatLngs(segment, padding);
mbgl::CameraOptions cameraOptions = _mbglMap->cameraForLatLngs(latLngs, padding);
if (direction >= 0)
{
cameraOptions.angle = MGLRadiansFromDegrees(-direction);
Expand Down Expand Up @@ -2820,7 +2820,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
if (!multiPoint.pointCount) {
continue;
}
shapes.emplace_back(multiPoint.annotationSegments, [multiPoint shapeAnnotationPropertiesObjectWithDelegate:self]);
shapes.emplace_back([multiPoint shapeAnnotationObjectWithDelegate:self]);
[userShapes addObject:annotation];
}
else if ([annotation isKindOfClass:[MGLMultiPolyline class]]
Expand Down
2 changes: 1 addition & 1 deletion platform/osx/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations {
if (!multiPoint.pointCount) {
continue;
}
shapes.emplace_back(multiPoint.annotationSegments, [multiPoint shapeAnnotationPropertiesObjectWithDelegate:self]);
shapes.emplace_back([multiPoint shapeAnnotationObjectWithDelegate:self]);
[userShapes addObject:annotation];
} else if ([annotation isKindOfClass:[MGLMultiPolyline class]]
|| [annotation isKindOfClass:[MGLMultiPolygon class]]
Expand Down
Loading

0 comments on commit 8985b13

Please sign in to comment.