|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Parse, LLC. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +#import "PFPolygon.h" |
| 11 | +#import "PFPolygonPrivate.h" |
| 12 | + |
| 13 | +#import <math.h> |
| 14 | + |
| 15 | +#import "PFAssert.h" |
| 16 | +#import "PFCoreManager.h" |
| 17 | +#import "PFHash.h" |
| 18 | +#import "PFLocationManager.h" |
| 19 | +#import "Parse_Private.h" |
| 20 | + |
| 21 | +@implementation PFPolygon |
| 22 | + |
| 23 | +///-------------------------------------- |
| 24 | +#pragma mark - Init |
| 25 | +///-------------------------------------- |
| 26 | + |
| 27 | ++ (instancetype)polygonWithCoordinates:(NSArray *)coordinates { |
| 28 | + PFPolygon *polygon = [[self alloc] init]; |
| 29 | + polygon.coordinates = coordinates; |
| 30 | + return polygon; |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +///-------------------------------------- |
| 35 | +#pragma mark - Accessors |
| 36 | +///-------------------------------------- |
| 37 | + |
| 38 | +- (void)setCoordinates:(NSArray *)coordinates { |
| 39 | + PFParameterAssert([coordinates isKindOfClass:[NSArray class]], |
| 40 | + @"`coordinates` must be a NSArray: %@", coordinates); |
| 41 | + |
| 42 | + PFParameterAssert(coordinates.count > 3, |
| 43 | + @"`Polygon` must have at least 3 GeoPoints or Points %@", coordinates); |
| 44 | + |
| 45 | + NSMutableArray* points = [[NSMutableArray alloc] init]; |
| 46 | + PFGeoPoint *geoPoint = [PFGeoPoint geoPoint]; |
| 47 | + |
| 48 | + for (int i = 0; i < coordinates.count; i += 1) { |
| 49 | + id coord = coordinates[i]; |
| 50 | + if ([coord isKindOfClass:[PFGeoPoint class]]) { |
| 51 | + geoPoint = coord; |
| 52 | + } else if ([coord isKindOfClass:[NSArray class]] && ((NSArray*)coord).count == 2) { |
| 53 | + NSArray* arr = (NSArray*)coord; |
| 54 | + double latitude = [arr[0] doubleValue]; |
| 55 | + double longitude = [arr[1] doubleValue]; |
| 56 | + geoPoint = [PFGeoPoint geoPointWithLatitude:latitude longitude:longitude]; |
| 57 | + } else if ([coord isKindOfClass:[CLLocation class]]) { |
| 58 | + geoPoint = [PFGeoPoint geoPointWithLocation:coord]; |
| 59 | + } else { |
| 60 | + PFParameterAssertionFailure(@"Coordinates must be an Array of GeoPoints or Points: %@", coord); |
| 61 | + } |
| 62 | + [points addObject:@[@(geoPoint.latitude), @(geoPoint.longitude)]]; |
| 63 | + } |
| 64 | + |
| 65 | + _coordinates = points; |
| 66 | +} |
| 67 | + |
| 68 | +- (BOOL)containsPoint:(PFGeoPoint *)point { |
| 69 | + double minX = [_coordinates[0][0] doubleValue]; |
| 70 | + double maxX = [_coordinates[0][0] doubleValue]; |
| 71 | + double minY = [_coordinates[0][1] doubleValue]; |
| 72 | + double maxY = [_coordinates[0][1] doubleValue]; |
| 73 | + for ( int i = 1; i < _coordinates.count; i += 1) { |
| 74 | + NSArray *p = _coordinates[i]; |
| 75 | + minX = fmin( [p[0] doubleValue], minX ); |
| 76 | + maxX = fmax( [p[0] doubleValue], maxX ); |
| 77 | + minY = fmin( [p[1] doubleValue], minY ); |
| 78 | + maxY = fmax( [p[1] doubleValue], maxY ); |
| 79 | + } |
| 80 | + |
| 81 | + if (point.latitude < minX || point.latitude > maxX || point.longitude < minY || point.longitude > maxY) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + bool inside = false; |
| 86 | + for ( int i = 0, j = (int)_coordinates.count - 1 ; i < _coordinates.count; j = i++) { |
| 87 | + double startX = [_coordinates[i][0] doubleValue]; |
| 88 | + double startY = [_coordinates[i][1] doubleValue]; |
| 89 | + double endX = [_coordinates[j][0] doubleValue]; |
| 90 | + double endY = [_coordinates[j][1] doubleValue]; |
| 91 | + if ( ( startY > point.longitude ) != ( endY > point.longitude ) && |
| 92 | + point.latitude < ( endX - startX ) * ( point.longitude - startY ) / ( endY - startY ) + startX ) { |
| 93 | + inside = !inside; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return inside; |
| 98 | +} |
| 99 | + |
| 100 | +///-------------------------------------- |
| 101 | +#pragma mark - Encoding |
| 102 | +///-------------------------------------- |
| 103 | + |
| 104 | +static NSString *const PFPolygonCodingTypeKey = @"__type"; |
| 105 | +static NSString *const PFPolygonCodingCoordinatesKey = @"coordinates"; |
| 106 | + |
| 107 | +- (NSDictionary *)encodeIntoDictionary { |
| 108 | + return @{ |
| 109 | + PFPolygonCodingTypeKey : @"Polygon", |
| 110 | + PFPolygonCodingCoordinatesKey : self.coordinates |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | ++ (instancetype)polygonWithDictionary:(NSDictionary *)dictionary { |
| 115 | + return [[self alloc] initWithEncodedDictionary:dictionary]; |
| 116 | +} |
| 117 | + |
| 118 | +- (instancetype)initWithEncodedDictionary:(NSDictionary *)dictionary { |
| 119 | + self = [self init]; |
| 120 | + if (!self) return nil; |
| 121 | + |
| 122 | + id coordObj = dictionary[PFPolygonCodingCoordinatesKey]; |
| 123 | + PFParameterAssert([coordObj isKindOfClass:[NSArray class]], @"Invalid coordinates type passed: %@", coordObj); |
| 124 | + |
| 125 | + _coordinates = coordObj; |
| 126 | + |
| 127 | + return self; |
| 128 | +} |
| 129 | + |
| 130 | +///-------------------------------------- |
| 131 | +#pragma mark - NSObject |
| 132 | +///-------------------------------------- |
| 133 | + |
| 134 | +- (BOOL)isEqual:(id)object { |
| 135 | + if (self == object) { |
| 136 | + return YES; |
| 137 | + } |
| 138 | + |
| 139 | + if (![object isKindOfClass:[PFPolygon class]]) { |
| 140 | + return NO; |
| 141 | + } |
| 142 | + |
| 143 | + PFPolygon *polygon = object; |
| 144 | + |
| 145 | + return ([_coordinates isEqualToArray:polygon.coordinates]); |
| 146 | +} |
| 147 | + |
| 148 | +- (NSString *)description { |
| 149 | + return [NSString stringWithFormat:@"<%@: %p, coordinates: %@>", |
| 150 | + [self class], |
| 151 | + self, |
| 152 | + _coordinates]; |
| 153 | +} |
| 154 | + |
| 155 | +///-------------------------------------- |
| 156 | +#pragma mark - NSCopying |
| 157 | +///-------------------------------------- |
| 158 | + |
| 159 | +- (instancetype)copyWithZone:(NSZone *)zone { |
| 160 | + PFPolygon *polygon = [[self class] polygonWithCoordinates:self.coordinates]; |
| 161 | + return polygon; |
| 162 | +} |
| 163 | + |
| 164 | +///-------------------------------------- |
| 165 | +#pragma mark - NSCoding |
| 166 | +///-------------------------------------- |
| 167 | + |
| 168 | +- (instancetype)initWithCoder:(NSCoder *)coder { |
| 169 | + NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; |
| 170 | + dictionary[PFPolygonCodingTypeKey] = [coder decodeObjectForKey:PFPolygonCodingTypeKey]; |
| 171 | + dictionary[PFPolygonCodingCoordinatesKey] = [coder decodeObjectForKey:PFPolygonCodingCoordinatesKey]; |
| 172 | + return [self initWithEncodedDictionary:dictionary]; |
| 173 | +} |
| 174 | + |
| 175 | +- (void)encodeWithCoder:(NSCoder *)coder { |
| 176 | + NSDictionary *dictionary = [self encodeIntoDictionary]; |
| 177 | + [dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { |
| 178 | + [coder encodeObject:obj forKey:key]; |
| 179 | + }]; |
| 180 | +} |
| 181 | + |
| 182 | +@end |
0 commit comments