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

Commit

Permalink
Use array of structs rather than parallel arrays for annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jun 29, 2015
1 parent f675d23 commit 42c06e7
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 171 deletions.
22 changes: 22 additions & 0 deletions include/mbgl/annotation/point_annotation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef MBGL_ANNOTATION_POINT_ANNOTATION
#define MBGL_ANNOTATION_POINT_ANNOTATION

#include <mbgl/util/geo.hpp>

#include <string>

namespace mbgl {

class PointAnnotation {
public:
inline PointAnnotation(const LatLng& position_, const std::string& icon_ = "")

This comment has been minimized.

Copy link
@hallahan

hallahan Jul 13, 2015

Contributor

What if we had icon_ = "default_marker" so that a marker gets drawn by default?

: position(position_), icon(icon_) {
}

const LatLng position;
const std::string icon;
};

}

#endif
24 changes: 24 additions & 0 deletions include/mbgl/annotation/shape_annotation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef MBGL_ANNOTATION_SHAPE_ANNOTATION
#define MBGL_ANNOTATION_SHAPE_ANNOTATION

#include <mbgl/util/geo.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/style/style_properties.hpp>

#include <string>

namespace mbgl {

class ShapeAnnotation {
public:
inline ShapeAnnotation(const AnnotationSegments& segments_, const StyleProperties& styleProperties_)
: segments(segments_), styleProperties(styleProperties_) {
}

const AnnotationSegments segments;
const StyleProperties styleProperties;
};

}

#endif
19 changes: 10 additions & 9 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <mbgl/util/chrono.hpp>
#include <mbgl/map/update.hpp>
#include <mbgl/map/mode.hpp>
#include <mbgl/style/style_properties.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/vec.hpp>
Expand All @@ -14,7 +13,6 @@
#include <functional>
#include <vector>
#include <memory>
#include <unordered_map>

namespace mbgl {

Expand All @@ -24,6 +22,8 @@ class MapData;
class MapContext;
class StillImage;
class Transform;
class PointAnnotation;
class ShapeAnnotation;

namespace util {
template <class T> class Thread;
Expand Down Expand Up @@ -129,15 +129,16 @@ class Map : private util::noncopyable {
// Annotations
void setDefaultPointAnnotationSymbol(const std::string&);
double getTopOffsetPixelsForAnnotationSymbol(const std::string&);
uint32_t addPointAnnotation(const LatLng&, const std::string& symbol);
AnnotationIDs addPointAnnotations(const AnnotationSegment&,
const std::vector<std::string>& symbols);
uint32_t addShapeAnnotation(const AnnotationSegments&,
const StyleProperties&);
AnnotationIDs addShapeAnnotations(const std::vector<AnnotationSegments>&,
const std::vector<StyleProperties>&);

uint32_t addPointAnnotation(const PointAnnotation&);
AnnotationIDs addPointAnnotations(const std::vector<PointAnnotation>&);

uint32_t addShapeAnnotation(const ShapeAnnotation&);
AnnotationIDs addShapeAnnotations(const std::vector<ShapeAnnotation>&);

void removeAnnotation(uint32_t);
void removeAnnotations(const AnnotationIDs&);

AnnotationIDs getAnnotationsInBounds(const LatLngBounds&, const AnnotationType& = AnnotationType::Any);
LatLngBounds getBoundsForAnnotations(const AnnotationIDs&);

Expand Down
18 changes: 8 additions & 10 deletions platform/default/glfw_view.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <mbgl/annotation/point_annotation.hpp>
#include <mbgl/annotation/shape_annotation.hpp>
#include <mbgl/platform/default/glfw_view.hpp>
#include <mbgl/platform/gl.hpp>
#include <mbgl/platform/log.hpp>
Expand Down Expand Up @@ -149,21 +151,18 @@ mbgl::LatLng GLFWView::makeRandomPoint() const {


void GLFWView::addRandomPointAnnotations(int count) {
std::vector<mbgl::LatLng> points;
std::vector<std::string> markers;
std::vector<mbgl::PointAnnotation> points;

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

auto newIDs = map->addPointAnnotations(points, markers);
auto newIDs = map->addPointAnnotations(points);
annotationIDs.insert(annotationIDs.end(), newIDs.begin(), newIDs.end());
}

void GLFWView::addRandomShapeAnnotations(int count) {
std::vector<mbgl::AnnotationSegments> shapes;
std::vector<mbgl::StyleProperties> shapesProperties;
std::vector<mbgl::ShapeAnnotation> shapes;

mbgl::FillProperties fillProperties;
fillProperties.opacity = .1;
Expand All @@ -180,11 +179,10 @@ void GLFWView::addRandomShapeAnnotations(int count) {
mbgl::AnnotationSegments segments;
segments.push_back(triangle);

shapes.push_back(segments);
shapesProperties.push_back(properties);
shapes.emplace_back(segments, properties);
}

auto newIDs = map->addShapeAnnotations(shapes, shapesProperties);
auto newIDs = map->addShapeAnnotations(shapes);
annotationIDs.insert(annotationIDs.end(), newIDs.begin(), newIDs.end());
}

Expand Down
27 changes: 11 additions & 16 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#import <OpenGLES/EAGL.h>

#include <mbgl/mbgl.hpp>
#include <mbgl/annotation/point_annotation.hpp>
#include <mbgl/annotation/shape_annotation.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/platform/darwin/reachability.h>
#include <mbgl/storage/default_file_source.hpp>
Expand Down Expand Up @@ -1780,11 +1782,8 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
{
if ( ! annotations) return;

std::vector<mbgl::LatLng> points;
std::vector<std::string> symbols;

std::vector<mbgl::AnnotationSegments> shapes;
std::vector<mbgl::StyleProperties> shapesProperties;
std::vector<mbgl::PointAnnotation> points;
std::vector<mbgl::ShapeAnnotation> shapes;

BOOL delegateImplementsSymbolLookup = [self.delegate respondsToSelector:@selector(mapView:symbolNameForAnnotation:)];
BOOL delegateImplementsAlphaForShape = [self.delegate respondsToSelector:@selector(mapView:alphaForShapeAnnotation:)];
Expand Down Expand Up @@ -1851,43 +1850,39 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations
userInfo:nil] raise];
}

shapesProperties.push_back(shapeProperties);

NSUInteger count = [(MGLMultiPoint *)annotation pointCount];

CLLocationCoordinate2D *coordinates = (CLLocationCoordinate2D *)malloc(count * sizeof(CLLocationCoordinate2D));
[(MGLMultiPoint *)annotation getCoordinates:coordinates range:NSMakeRange(0, count)];

mbgl::AnnotationSegment shape;
shape.reserve(count);
mbgl::AnnotationSegment segment;
segment.reserve(count);

for (NSUInteger i = 0; i < count; i++)
{
shape.push_back(mbgl::LatLng(coordinates[i].latitude, coordinates[i].longitude));
segment.push_back(mbgl::LatLng(coordinates[i].latitude, coordinates[i].longitude));
}

free(coordinates);

shapes.push_back({{ shape }});
shapes.emplace_back(mbgl::AnnotationSegments {{ segment }}, shapeProperties);
}
else
{
points.push_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate));

NSString *symbolName = nil;

if (delegateImplementsSymbolLookup)
{
symbolName = [self.delegate mapView:self symbolNameForAnnotation:annotation];
}

symbols.push_back((symbolName ? [symbolName UTF8String] : ""));
points.emplace_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate), symbolName ? [symbolName UTF8String] : "");
}
}

if (points.size())
{
std::vector<uint32_t> pointAnnotationIDs = _mbglMap->addPointAnnotations(points, symbols);
std::vector<uint32_t> pointAnnotationIDs = _mbglMap->addPointAnnotations(points);

for (size_t i = 0; i < pointAnnotationIDs.size(); ++i)
{
Expand All @@ -1898,7 +1893,7 @@ - (void)addAnnotations:(NS_ARRAY_OF(id <MGLAnnotation>) *)annotations

if (shapes.size())
{
std::vector<uint32_t> shapeAnnotationIDs = _mbglMap->addShapeAnnotations(shapes, shapesProperties);
std::vector<uint32_t> shapeAnnotationIDs = _mbglMap->addShapeAnnotations(shapes);

for (size_t i = 0; i < shapeAnnotationIDs.size(); ++i)
{
Expand Down
Loading

0 comments on commit 42c06e7

Please sign in to comment.