This repository was archived by the owner on Jan 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathDKBSPDirectObjectStorage.m
More file actions
651 lines (501 loc) · 15.8 KB
/
DKBSPDirectObjectStorage.m
File metadata and controls
651 lines (501 loc) · 15.8 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/**
@author Contributions from the community; see CONTRIBUTORS.md
@date 2005-2016
@copyright MPL2; see LICENSE.txt
*/
#import "DKBSPDirectObjectStorage.h"
// if this is set to 1, various iterations are done using the much faster CFArrayApplyFunction and CFArraySortValues methods
#define USE_CF_APPLIER 0
// utility functions:
static inline NSUInteger depthForObjectCount(NSUInteger n)
{
return (n > 0 ? MAX((NSUInteger)ceil(log((CGFloat)n)) / log(2.0), kDKMinimumDepth) : 0);
}
//static inline NSUInteger childNodeAtIndex(NSUInteger nodeIndex)
//{
// return (nodeIndex << 1) + 1;
//}
@interface DKBSPDirectObjectStorage ()
- (void)sortObjectsByZ:(NSMutableArray*)objects;
- (void)renumberObjectsFromIndex:(NSUInteger)indx;
- (void)unmarkAll:(NSArray*)objects;
- (BOOL)checkForTreeRebuild;
- (void)loadBSPTree;
- (void)setAutoRebuildEnable:(BOOL)enable;
@end
#pragma mark -
@implementation DKBSPDirectObjectStorage
- (void)setTreeDepth:(NSUInteger)aDepth
{
// intended to be set when the storage is created. Defaults to 0, meaning that the tree is dynamically rebuilt when needed
// as the number of objects changes. If this is set to a value other than 0, the tree is rebuilt immediately, otherwise it
// is rebuilt if needed when objects are added/removed.
if (aDepth != mTreeDepth) {
mTreeDepth = aDepth;
if (mTreeDepth > 0) {
[mTree setDepth:mTreeDepth];
[self loadBSPTree];
}
}
}
- (id)tree
{
return mTree;
}
- (NSArray*)objectsIntersectingRect:(NSRect)aRect inView:(NSView*)aView options:(DKObjectStorageOptions)options
{
#pragma unused(options)
NSMutableArray* results;
if (aView) {
const NSRect* rects;
NSInteger count;
[aView getRectsBeingDrawn:&rects
count:&count];
results = [mTree objectsIntersectingRects:rects
count:count
inView:aView];
} else
results = [mTree objectsIntersectingRect:aRect];
// the final results need to be sorted into Z-order unless 'relaxed' flag set
if ((options & kDKZOrderMayBeRelaxed) == 0)
[self sortObjectsByZ:results];
[self unmarkAll:results];
//NSLog(@"returning %d object(s)", [results count]);
// warning, the results returned is the actual mutable array owned by the tree. This is for performance reasons. The client should not
// expect the array content to remain stable across each event loop. The client must make a copy if they wish to keep this list (in practice unlikely).
return results;
}
- (NSArray*)objectsContainingPoint:(NSPoint)aPoint
{
NSMutableArray* objects = [mTree objectsIntersectingPoint:aPoint];
[self sortObjectsByZ:objects];
[self unmarkAll:objects];
return objects;
}
- (void)setObjects:(NSArray*)objects
{
[[self objects] makeObjectsPerformSelector:@selector(setStorage:)
withObject:nil];
[super setObjects:objects];
[self loadBSPTree];
}
- (void)insertObject:(id<DKStorableObject>)obj inObjectsAtIndex:(NSUInteger)indx
{
NSAssert(obj != nil, @"can't insert a nil object");
if ([obj conformsToProtocol:@protocol(DKStorableObject)]) {
[super insertObject:obj
inObjectsAtIndex:indx];
[self renumberObjectsFromIndex:indx];
[obj setStorage:self];
if (![self checkForTreeRebuild])
[mTree insertItem:obj
withRect:[obj bounds]];
//NSLog(@"inserted %@, index = %d", obj, indx );
}
}
- (void)removeObjectFromObjectsAtIndex:(NSUInteger)indx
{
id<DKStorableObject> obj = [self objectInObjectsAtIndex:indx];
if (obj) {
//NSLog(@"will remove %@, index = %d", obj, indx );
NSAssert1([obj index] == indx, @"index mismatch when removing object from storage, obj = %@", obj);
[super removeObjectFromObjectsAtIndex:indx];
[self renumberObjectsFromIndex:indx];
[obj setStorage:nil];
if (![self checkForTreeRebuild])
[mTree removeItem:obj
withRect:[obj bounds]];
}
}
- (void)replaceObjectInObjectsAtIndex:(NSUInteger)indx withObject:(id<DKStorableObject>)obj
{
NSAssert(obj != nil, @"cannot replace an object with nil");
id<DKStorableObject> old = [self objectInObjectsAtIndex:indx];
if ((old != obj) && [obj conformsToProtocol:@protocol(DKStorableObject)]) {
if (old) {
NSAssert1([old index] == indx, @"index mismatch when replacing object in storage, obj = %@", old);
[mTree removeItem:old
withRect:[old bounds]];
}
[obj setIndex:indx];
[super replaceObjectInObjectsAtIndex:indx
withObject:obj];
[mTree insertItem:obj
withRect:[obj bounds]];
}
}
- (void)insertObjects:(NSArray*)objs atIndexes:(NSIndexSet*)set
{
NSAssert(objs != nil, @"objects were nil in insertObjects:atIndexes");
NSAssert(set != nil, @"set was nil in insertObjects:atIndexes");
NSAssert([objs count] == [set count], @"objects and set counts do not agree");
// because this could potentially insert a lot of objects, the BSP tree needs to be sized properly in advance, rather than
// rely on dynamic resizing which can add up.
NSUInteger neededDepth = MAX(depthForObjectCount([self countOfObjects] + [set count]), kDKMinimumDepth);
[mTree setDepth:neededDepth];
[self loadBSPTree];
mLastItemCount = [self countOfObjects] + [set count];
[self setAutoRebuildEnable:NO];
NSUInteger ix, k = 0;
id<DKStorableObject> obj;
ix = [set firstIndex];
while (ix != NSNotFound) {
obj = [objs objectAtIndex:k++];
[self insertObject:obj
inObjectsAtIndex:ix];
ix = [set indexGreaterThanIndex:ix];
}
[self setAutoRebuildEnable:YES];
}
- (void)removeObjectsAtIndexes:(NSIndexSet*)set
{
NSAssert(set != nil, @"indexes were nil");
if ([set count] > 0) {
//NSLog(@"removing objects at indexes: %@", set );
NSUInteger ix = [set firstIndex];
id<DKStorableObject> obj;
while (ix != NSNotFound) {
obj = [[self objects] objectAtIndex:ix];
[obj setStorage:nil];
[mTree removeItem:obj
withRect:[obj bounds]];
ix = [set indexGreaterThanIndex:ix];
}
[super removeObjectsAtIndexes:set];
[self renumberObjectsFromIndex:[set firstIndex]];
}
}
- (BOOL)containsObject:(id<DKStorableObject>)object
{
// for a quick answer, return YES if the storage is set to self
return [object storage] == self;
}
- (void)moveObject:(id<DKStorableObject>)obj toIndex:(NSUInteger)indx
{
NSUInteger oldIndex = [obj index];
if (oldIndex != indx) {
[super moveObject:obj
toIndex:indx];
[self renumberObjectsFromIndex:MIN(oldIndex, indx)];
}
}
- (void)object:(id<DKStorableObject>)obj didChangeBoundsFrom:(NSRect)oldBounds
{
[mTree removeItem:obj
withRect:oldBounds];
[mTree insertItem:obj
withRect:[obj bounds]];
}
- (void)setCanvasSize:(NSSize)size
{
// rebuilds the BSP tree entirely. Note that this is the only method that creates the tree - it must be called when the storage
// is first created, and whenever the canvas size changes. Because the tree is the sole storage for the objects, we retain them in a list
// then set them again to reload the tree.
if (!NSEqualSizes(size, [mTree canvasSize])) {
NSArray* objects = [self objects];
NSUInteger depth = (mTreeDepth == 0 ? depthForObjectCount([objects count]) : mTreeDepth);
mTree = [[DKBSPDirectTree alloc] initWithCanvasSize:size
depth:MAX(depth, kDKMinimumDepth)];
[self setObjects:objects];
}
}
#pragma mark -
#if USE_CF_APPLIER
static NSComparisonResult zComparisonFunc(id<DKStorableObject> a, id<DKStorableObject> b, void* context)
{
#pragma unused(context)
NSUInteger ia = [a index];
NSUInteger ib = [b index];
if (ia < ib)
return NSOrderedAscending;
else if (ia > ib)
return NSOrderedDescending;
else
return NSOrderedSame;
}
#endif
- (void)sortObjectsByZ:(NSMutableArray*)objects
{
#if USE_CF_APPLIER
if (objects)
CFArraySortValues((CFMutableArrayRef)objects, CFRangeMake(0, [objects count]), (CFComparatorFunction)zComparisonFunc, NULL);
#else
[objects sortUsingComparator:^NSComparisonResult(id<DKStorableObject> a, id<DKStorableObject> b) {
NSUInteger ia = [a index];
NSUInteger ib = [b index];
if (ia < ib)
return NSOrderedAscending;
else if (ia > ib)
return NSOrderedDescending;
else
return NSOrderedSame;
}];
#endif
}
#if USE_CF_APPLIER
static void renumberFunc(const void* value, void* context)
{
id<DKStorableObject> obj = (__bridge id<DKStorableObject>)value;
[obj setIndex:*(NSUInteger*)context];
(*(NSUInteger*)context)++;
}
static void unmarkFunc(const void* value, void* context)
{
#pragma unused(context)
[(__bridge id<DKStorableObject>)value setMarked:NO];
}
#endif
- (void)renumberObjectsFromIndex:(NSUInteger)indx
{
// renumbers the index value of objects starting from <indx>
if (indx > [self countOfObjects])
return;
NSUInteger i = indx;
#if USE_CF_APPLIER
CFArrayApplyFunction((CFArrayRef)[self objects], CFRangeMake(indx, [self countOfObjects] - indx), renumberFunc, &i);
#else
for (id<DKStorableObject> obj in [self.objects subarrayWithRange:NSMakeRange(indx, [self countOfObjects] - indx)]) {
obj.index = i++;
}
#endif
}
- (void)unmarkAll:(NSArray*)objects
{
#if USE_CF_APPLIER
if (objects)
CFArrayApplyFunction((CFArrayRef)objects, CFRangeMake(0, [objects count]), unmarkFunc, NULL);
#else
for (id<DKStorableObject> obj in objects) {
obj.marked = NO;
}
#endif
}
- (NSBezierPath*)debugStorageDivisions
{
// for debugging purposes, returns a path consisting of the BSP rects
return [mTree debugStorageDivisions];
}
- (BOOL)checkForTreeRebuild
{
// calculates an optimal tree depth given the current number of items stored. This is done if the depth is
// initialised to 0. If the depth is different and the item count exceeds the slack value, then the tree is
// rebuilt with the new depth. If the tree depth is preset to a fixed value, this dynamic resizing is never done.
// return YES if the tree was rebuilt, NO otherwise
if (mTreeDepth == 0 && mAutoRebuild) {
NSUInteger oldDepth = MAX(depthForObjectCount(mLastItemCount), kDKMinimumDepth);
NSUInteger neuDepth = MAX(depthForObjectCount([self countOfObjects]), kDKMinimumDepth);
if ([mTree countOfLeaves] == 0 || (oldDepth != neuDepth && ABS((NSInteger)mLastItemCount - (NSInteger)[self countOfObjects]) > kDKBSPSlack)) {
// sufficient cause to rebuild the tree
[mTree setDepth:neuDepth];
[self loadBSPTree];
return YES;
}
}
return NO;
}
- (void)loadBSPTree
{
[mTree removeAllObjects];
// reload the tree
NSUInteger z = 0;
for (id<DKStorableObject> obj in [self objects]) {
if ([obj conformsToProtocol:@protocol(DKStorableObject)]) {
[obj setIndex:z++];
[obj setStorage:self];
[mTree insertItem:obj
withRect:[obj bounds]];
}
}
mLastItemCount = z;
//NSLog(@"loaded BSP tree with %d indexes (tree = %@)", k, mTree );
}
- (void)setAutoRebuildEnable:(BOOL)enable
{
mAutoRebuild = enable;
}
#pragma mark -
#pragma mark - as a NSObject
- (instancetype)init
{
self = [super init];
if (self) {
mAutoRebuild = YES;
}
return self;
}
- (instancetype)initWithCoder:(NSCoder*)coder
{
// this method is here solely to support backward compatibility with b5; storage is no longer archived.
if (self = [super initWithCoder:coder]) {
mTreeDepth = [coder decodeIntegerForKey:@"DKBSPDirectStorage_treeDepth"];
[self setCanvasSize:[coder decodeSizeForKey:@"DKBSPDirectStorage_canvasSize"]];
mAutoRebuild = YES;
}
return self;
}
@end
#pragma mark -
@interface DKBSPDirectTree (DKBSPIndexTreePrivate)
// these are implemented by DKBSPIndexTree as private methods, re-prototyped here so
// we can make use of them in this subclass
- (void)recursivelySearchWithRect:(NSRect)rect index:(NSUInteger)indx;
- (void)recursivelySearchWithPoint:(NSPoint)pt index:(NSUInteger)indx;
- (void)operateOnLeaf:(id)leaf;
- (void)removeObject:(id<DKStorableObject>)obj;
@end
#pragma mark -
@implementation DKBSPDirectTree
- (void)insertItem:(id<DKStorableObject>)obj withRect:(NSRect)rect
{
if ([mNodes count] == 0)
return;
if (obj && !NSIsEmptyRect(rect)) {
mOp = kDKOperationInsert;
mObj = obj;
[self recursivelySearchWithRect:rect
index:0];
++mObjectCount;
} else
NSLog(@"didn't insert object '%@' - bad rect (%@)", obj, NSStringFromRect(rect));
//NSLog(@"inserted obj = %@, bounds = %@", obj, NSStringFromRect( rect ));
}
- (void)removeItem:(id<DKStorableObject>)obj withRect:(NSRect)rect
{
#pragma unused(rect)
/*
if ([mNodes count] == 0)
return;
if( obj && !NSIsEmptyRect( rect ))
{
[obj setMarked:NO];
mOp = kDKOperationDelete;
mObj = obj;
[self recursivelySearchWithRect:rect index:0];
if( mObjectCount > 0 )
--mObjectCount;
}
*/
[self removeObject:obj];
//NSLog(@"removed %@", obj );
}
- (void)removeAllObjects
{
for (NSMutableArray* leaf in mLeaves)
[leaf removeAllObjects];
mObjectCount = 0;
}
- (NSMutableArray*)objectsIntersectingRects:(const NSRect*)rects count:(NSUInteger)count inView:(NSView*)aView
{
// this may be used in conjunction with NSView's -getRectsBeingDrawn:count: to find those objects that intersect the non-rectangular update region.
if ([mNodes count] == 0)
return nil;
mViewRef = aView;
mOp = kDKOperationAccumulate;
[mFoundObjects removeAllObjects];
for (NSUInteger i = 0; i < count; ++i)
[self recursivelySearchWithRect:rects[i]
index:0];
return mFoundObjects;
}
- (NSMutableArray*)objectsIntersectingRect:(NSRect)rect
{
if ([mNodes count] == 0)
return nil;
mRect = rect;
mViewRef = nil;
mOp = kDKOperationAccumulate;
[mFoundObjects removeAllObjects];
[self recursivelySearchWithRect:rect
index:0];
return mFoundObjects;
}
- (NSMutableArray*)objectsIntersectingPoint:(NSPoint)point
{
if ([mNodes count] == 0)
return nil;
mRect = NSMakeRect(point.x, point.y, 1e-3, 1e-3);
mOp = kDKOperationAccumulate;
[mFoundObjects removeAllObjects];
[self recursivelySearchWithPoint:point
index:0];
return mFoundObjects;
}
- (NSUInteger)count
{
// returns the number of unique objects in the tree. Note that this value can be unreliable if the client didn't take care (i.e. calling removeItem with a bad object).
return mObjectCount;
}
#pragma mark -
#pragma mark - as a DKBSPIndexTree
+ (Class)leafClass
{
return [NSMutableArray class];
}
- (instancetype)initWithCanvasSize:(NSSize)size depth:(NSUInteger)depth
{
self = [super initWithCanvasSize:size
depth:depth];
if (self) {
mFoundObjects = [[NSMutableArray alloc] init];
}
return self;
}
#if USE_CF_APPLIER
static void addValueToFoundObjects(const void* value, void* context)
{
id<DKStorableObject> obj = (__bridge id<DKStorableObject>)value;
if (![obj isMarked] && [obj visible]) {
DKBSPDirectTree* tree = (__bridge DKBSPDirectTree*)context;
NSView* view = tree->mViewRef;
// double-check that the view really needs to draw this
if ((view == nil && NSIntersectsRect([obj bounds], tree->mRect)) || [view needsToDrawRect:[obj bounds]]) {
[obj setMarked:YES];
CFArrayAppendValue((CFMutableArrayRef)tree->mFoundObjects, value);
}
}
}
#endif
- (void)operateOnLeaf:(id)leaf
{
// <leaf> is a pointer to the NSMutableArray at the leaf
switch (mOp) {
case kDKOperationInsert:
[leaf addObject:mObj];
break;
case kDKOperationDelete:
[leaf removeObject:mObj];
break;
case kDKOperationAccumulate: {
#if USE_CF_APPLIER
CFArrayApplyFunction((CFArrayRef)leaf, CFRangeMake(0, [leaf count]), addValueToFoundObjects, (__bridge void*)(self));
#else
for (id<DKStorableObject> anObject in leaf) {
if (![anObject isMarked] && [anObject visible]) {
if ((mViewRef == nil && NSIntersectsRect([anObject bounds], mRect)) || [mViewRef needsToDrawRect:[anObject bounds]]) {
[anObject setMarked:YES];
[mFoundObjects addObject:anObject];
}
}
}
#endif
} break;
default:
break;
}
}
- (void)removeObject:(id<DKStorableObject>)obj
{
// removes all references to <obj> from the tree. Ignores its bounds and simply iterates over the leaves removing the object.
for (NSMutableArray* leaf in mLeaves) {
[leaf removeObject:obj];
}
if (mObjectCount > 0)
mObjectCount--;
}
#pragma mark -
#pragma mark - as a NSObject
- (NSString*)description
{
return [NSString stringWithFormat:@"<%@ %p>, %ld leaves = %@", NSStringFromClass([self class]), self, (long)[self countOfLeaves], mLeaves];
}
@end