CCHMapTreeUtils.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // CCHMapTreeUtils.m
  3. // CCHMapClusterController
  4. //
  5. // Copyright (C) 2013 Theodore Calmes
  6. // Copyright (C) 2013 Claus Höfele
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. //
  26. #import "CCHMapTreeUtils.h"
  27. #pragma mark - Unsafe Mutable Array
  28. @interface CCHMapTreeUnsafeMutableArray()
  29. @property (nonatomic, assign) id __unsafe_unretained *objects;
  30. @property (nonatomic) NSUInteger numObjects;
  31. @property (nonatomic) NSUInteger capacity;
  32. @end
  33. @implementation CCHMapTreeUnsafeMutableArray
  34. - (instancetype)initWithCapacity:(NSUInteger)capacity
  35. {
  36. self = [super init];
  37. if (self) {
  38. _objects = (__unsafe_unretained id *)malloc(capacity * sizeof(id));
  39. _numObjects = 0;
  40. _capacity = capacity ? capacity : 1;
  41. }
  42. return self;
  43. }
  44. - (void)dealloc
  45. {
  46. free(_objects);
  47. }
  48. - (void)addObject:(__unsafe_unretained id)object
  49. {
  50. if (_numObjects >= _capacity) {
  51. _capacity *= 2;
  52. _objects = (__unsafe_unretained id *)realloc(_objects, _capacity * sizeof(id));
  53. }
  54. _objects[_numObjects++] = object;
  55. }
  56. @end
  57. #pragma mark - Constructors
  58. CCHMapTreeNode *CCHMapTreeNodeMake(CCHMapTreeBoundingBox boundary, unsigned long bucketCapacity)
  59. {
  60. CCHMapTreeNode* node = malloc(sizeof(CCHMapTreeNode));
  61. node->northWest = NULL;
  62. node->northEast = NULL;
  63. node->southWest = NULL;
  64. node->southEast = NULL;
  65. node->boundingBox = boundary;
  66. node->count = 0;
  67. node->points = malloc(sizeof(CCHMapTreeNodeData) * bucketCapacity);
  68. return node;
  69. }
  70. #pragma mark - Bounding Box Functions
  71. static inline bool CCHMapTreeBoundingBoxContainsData(CCHMapTreeBoundingBox box, CCHMapTreeNodeData data)
  72. {
  73. return (box.x0 <= data.x && data.x <= box.xf && box.y0 <= data.y && data.y <= box.yf);
  74. }
  75. static inline bool CCHMapTreeBoundingBoxIntersectsBoundingBox(CCHMapTreeBoundingBox b1, CCHMapTreeBoundingBox b2)
  76. {
  77. return (b1.x0 <= b2.xf && b1.xf >= b2.x0 && b1.y0 <= b2.yf && b1.yf >= b2.y0);
  78. }
  79. #pragma mark - Quad Tree Functions
  80. void CCHMapTreeNodeSubdivide(CCHMapTreeNode *node, unsigned long bucketCapacity)
  81. {
  82. CCHMapTreeBoundingBox box = node->boundingBox;
  83. double xMid = (box.xf + box.x0) / 2.0;
  84. double yMid = (box.yf + box.y0) / 2.0;
  85. CCHMapTreeBoundingBox northWest = CCHMapTreeBoundingBoxMake(box.x0, box.y0, xMid, yMid);
  86. node->northWest = CCHMapTreeNodeMake(northWest, bucketCapacity);
  87. CCHMapTreeBoundingBox northEast = CCHMapTreeBoundingBoxMake(xMid, box.y0, box.xf, yMid);
  88. node->northEast = CCHMapTreeNodeMake(northEast, bucketCapacity);
  89. CCHMapTreeBoundingBox southWest = CCHMapTreeBoundingBoxMake(box.x0, yMid, xMid, box.yf);
  90. node->southWest = CCHMapTreeNodeMake(southWest, bucketCapacity);
  91. CCHMapTreeBoundingBox southEast = CCHMapTreeBoundingBoxMake(xMid, yMid, box.xf, box.yf);
  92. node->southEast = CCHMapTreeNodeMake(southEast, bucketCapacity);
  93. }
  94. CCHMapTreeNode *CCHMapTreeBuildWithData(CCHMapTreeNodeData *data, unsigned long count, CCHMapTreeBoundingBox boundingBox, unsigned long bucketCapacity)
  95. {
  96. CCHMapTreeNode *root = CCHMapTreeNodeMake(boundingBox, bucketCapacity);
  97. for (unsigned long i = 0; i < count; i++) {
  98. CCHMapTreeNodeInsertData(root, data[i], bucketCapacity);
  99. }
  100. return root;
  101. }
  102. bool CCHMapTreeNodeInsertData(CCHMapTreeNode *node, CCHMapTreeNodeData data, unsigned long bucketCapacity)
  103. {
  104. if (!CCHMapTreeBoundingBoxContainsData(node->boundingBox, data)) {
  105. return false;
  106. }
  107. if (node->count < bucketCapacity) {
  108. node->points[node->count++] = data;
  109. return true;
  110. }
  111. if (node->northWest == NULL) {
  112. CCHMapTreeNodeSubdivide(node, bucketCapacity);
  113. }
  114. if (CCHMapTreeNodeInsertData(node->northWest, data, bucketCapacity)) return true;
  115. if (CCHMapTreeNodeInsertData(node->northEast, data, bucketCapacity)) return true;
  116. if (CCHMapTreeNodeInsertData(node->southWest, data, bucketCapacity)) return true;
  117. if (CCHMapTreeNodeInsertData(node->southEast, data, bucketCapacity)) return true;
  118. return false;
  119. }
  120. bool CCHMapTreeNodeRemoveData(CCHMapTreeNode *node, CCHMapTreeNodeData data)
  121. {
  122. if (!CCHMapTreeBoundingBoxContainsData(node->boundingBox, data)) {
  123. return false;
  124. }
  125. for (unsigned long i = 0; i < node->count; i++) {
  126. CCHMapTreeNodeData *nodeData = &node->points[i];
  127. if (nodeData->data == data.data) {
  128. node->points[i] = node->points[node->count - 1];
  129. node->count--;
  130. return true;
  131. }
  132. }
  133. if (node->northWest == NULL) {
  134. return false;
  135. }
  136. if (CCHMapTreeNodeRemoveData(node->northWest, data)) return true;
  137. if (CCHMapTreeNodeRemoveData(node->northEast, data)) return true;
  138. if (CCHMapTreeNodeRemoveData(node->southWest, data)) return true;
  139. if (CCHMapTreeNodeRemoveData(node->southEast, data)) return true;
  140. return false;
  141. }
  142. void CCHMapTreeGatherDataInRange(CCHMapTreeNode *node, CCHMapTreeBoundingBox range, TBDataReturnBlock block)
  143. {
  144. if (!CCHMapTreeBoundingBoxIntersectsBoundingBox(node->boundingBox, range)) {
  145. return;
  146. }
  147. for (unsigned long i = 0; i < node->count; i++) {
  148. if (CCHMapTreeBoundingBoxContainsData(range, node->points[i])) {
  149. block(node->points[i]);
  150. }
  151. }
  152. if (node->northWest == NULL) {
  153. return;
  154. }
  155. CCHMapTreeGatherDataInRange(node->northWest, range, block);
  156. CCHMapTreeGatherDataInRange(node->northEast, range, block);
  157. CCHMapTreeGatherDataInRange(node->southWest, range, block);
  158. CCHMapTreeGatherDataInRange(node->southEast, range, block);
  159. }
  160. void CCHMapTreeGatherDataInRange2(CCHMapTreeNode *node, CCHMapTreeBoundingBox range, __unsafe_unretained NSMutableSet *annotations)
  161. {
  162. if (!CCHMapTreeBoundingBoxIntersectsBoundingBox(node->boundingBox, range)) {
  163. return;
  164. }
  165. for (unsigned long i = 0; i < node->count; i++) {
  166. if (CCHMapTreeBoundingBoxContainsData(range, node->points[i])) {
  167. [annotations addObject:(__bridge id)node->points[i].data];
  168. }
  169. }
  170. if (node->northWest == NULL) {
  171. return;
  172. }
  173. CCHMapTreeGatherDataInRange2(node->northWest, range, annotations);
  174. CCHMapTreeGatherDataInRange2(node->northEast, range, annotations);
  175. CCHMapTreeGatherDataInRange2(node->southWest, range, annotations);
  176. CCHMapTreeGatherDataInRange2(node->southEast, range, annotations);
  177. }
  178. void CCHMapTreeGatherDataInRange3(CCHMapTreeNode *node, CCHMapTreeBoundingBox range, __unsafe_unretained CCHMapTreeUnsafeMutableArray *annotations)
  179. {
  180. if (!CCHMapTreeBoundingBoxIntersectsBoundingBox(node->boundingBox, range)) {
  181. return;
  182. }
  183. for (unsigned long i = 0; i < node->count; i++) {
  184. if (CCHMapTreeBoundingBoxContainsData(range, node->points[i])) {
  185. [annotations addObject:(__bridge id)node->points[i].data];
  186. }
  187. }
  188. if (node->northWest == NULL) {
  189. return;
  190. }
  191. CCHMapTreeGatherDataInRange3(node->northWest, range, annotations);
  192. CCHMapTreeGatherDataInRange3(node->northEast, range, annotations);
  193. CCHMapTreeGatherDataInRange3(node->southWest, range, annotations);
  194. CCHMapTreeGatherDataInRange3(node->southEast, range, annotations);
  195. }
  196. void CCHMapTreeTraverse(CCHMapTreeNode *node, CCHMapTreeTraverseBlock block)
  197. {
  198. block(node);
  199. if (node->northWest == NULL) {
  200. return;
  201. }
  202. CCHMapTreeTraverse(node->northWest, block);
  203. CCHMapTreeTraverse(node->northEast, block);
  204. CCHMapTreeTraverse(node->southWest, block);
  205. CCHMapTreeTraverse(node->southEast, block);
  206. }
  207. void CCHMapTreeFreeQuadTreeNode(CCHMapTreeNode *node)
  208. {
  209. if (node->northWest != NULL) CCHMapTreeFreeQuadTreeNode(node->northWest);
  210. if (node->northEast != NULL) CCHMapTreeFreeQuadTreeNode(node->northEast);
  211. if (node->southWest != NULL) CCHMapTreeFreeQuadTreeNode(node->southWest);
  212. if (node->southEast != NULL) CCHMapTreeFreeQuadTreeNode(node->southEast);
  213. free(node->points);
  214. free(node);
  215. }