summaryrefslogtreecommitdiff
path: root/platform/darwin/src/MGLMultiPoint.mm
blob: 17a61ed081601699aa96ff7fbd4a94b4b8d8e050 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#import "MGLMultiPoint_Private.h"
#import "MGLGeometry_Private.h"
#import "MGLTypes.h"

#import <mbgl/util/geo.hpp>

mbgl::Color MGLColorObjectFromCGColorRef(CGColorRef cgColor)
{
    if (!cgColor) return { 0, 0, 0, 0 };
    NSCAssert(CGColorGetNumberOfComponents(cgColor) >= 4, @"Color must have at least 4 components");
    const CGFloat *components = CGColorGetComponents(cgColor);
    return { (float)components[0], (float)components[1], (float)components[2], (float)components[3] };
}

@implementation MGLMultiPoint
{
    size_t _count;
    MGLCoordinateBounds _bounds;
}

- (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count
{
    self = [super init];

    if (self)
    {
        [self setupWithCoordinates:coords count:count];
    }

    return self;
}

- (void)setupWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUInteger)count
{
    if (_coordinates) free(_coordinates);

    _count = count;
    _coordinates = (CLLocationCoordinate2D *)malloc(_count * sizeof(CLLocationCoordinate2D));

    mbgl::LatLngBounds bounds = mbgl::LatLngBounds::empty();

    for (NSUInteger i = 0; i < _count; i++)
    {
        _coordinates[i] = coords[i];
        bounds.extend(mbgl::LatLng(coords[i].latitude, coords[i].longitude));
    }

    _bounds = MGLCoordinateBoundsFromLatLngBounds(bounds);
}

- (void)dealloc
{
    free(_coordinates);
}

- (CLLocationCoordinate2D)coordinate
{
    assert(_count > 0);

    return CLLocationCoordinate2DMake(_coordinates[0].latitude, _coordinates[0].longitude);
}

- (NSUInteger)pointCount
{
    return _count;
}

+ (NS_SET_OF(NSString *) *)keyPathsForValuesAffectingPointCount
{
    return [NSSet setWithObjects:@"coordinates", nil];
}

- (void)getCoordinates:(CLLocationCoordinate2D *)coords range:(NSRange)range
{
    if (range.location + range.length > _count)
    {
        [NSException raise:NSRangeException
                    format:@"Invalid coordinate range %@ extends beyond current coordinate count of %zu",
                        NSStringFromRange(range), _count];
    }

    NSUInteger index = 0;

    for (NSUInteger i = range.location; i < range.location + range.length; i++)
    {
        coords[index] = _coordinates[i];
        index++;
    }
}

- (void)replaceCoordinatesInRange:(NSRange)range withCoordinates:(CLLocationCoordinate2D *)coords
{
    if ((coords >= _coordinates && coords < _coordinates + _count) ||
        (coords + range.length >= _coordinates && coords + range.length < _coordinates + _count))
    {
        [NSException raise:NSRangeException format:@"Reuse of existing coordinates array %p not supported", coords];
    }
    else if (range.length == 0)
    {
        [NSException raise:NSRangeException format:@"Empty coordinate range %@", NSStringFromRange(range)];
    }
    else if (range.location > _count)
    {
        [NSException raise:NSRangeException
                    format:@"Invalid range %@ for existing coordinate count %zu",
                        NSStringFromRange(range), _count];
    }

    [self willChangeValueForKey:@"coordinates"];
    if (NSMaxRange(range) <= _count)
    {
        // replacing existing coordinate(s)
        memcpy(_coordinates + range.location, coords, range.length * sizeof(CLLocationCoordinate2D));
    }
    else
    {
        // appending new coordinate(s)
        NSUInteger newCount = NSMaxRange(range);
        CLLocationCoordinate2D *newCoordinates = (CLLocationCoordinate2D *)malloc(newCount * sizeof(CLLocationCoordinate2D));
        memcpy(newCoordinates, _coordinates, fmin(_count, range.location) * sizeof(CLLocationCoordinate2D));
        memcpy(newCoordinates + range.location, coords, range.length * sizeof(CLLocationCoordinate2D));
        [self setupWithCoordinates:newCoordinates count:newCount];
        free(newCoordinates);
    }
    [self didChangeValueForKey:@"coordinates"];
}

- (MGLCoordinateBounds)overlayBounds
{
    return _bounds;
}

- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds
{
    return MGLLatLngBoundsFromCoordinateBounds(_bounds).intersects(MGLLatLngBoundsFromCoordinateBounds(overlayBounds));
}

- (mbgl::Annotation)annotationObjectWithDelegate:(__unused id <MGLMultiPointDelegate>)delegate
{
    NSAssert(NO, @"Cannot add an annotation from an instance of %@", NSStringFromClass([self class]));
    return mbgl::SymbolAnnotation({mbgl::Point<double>()});
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"<%@: %p; count = %lu; bounds = %@>",
               NSStringFromClass([self class]), (void *)self, (unsigned long)_count, MGLStringFromCoordinateBounds(_bounds)];
}

@end