-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTLPhotoLayout.m
321 lines (275 loc) · 10.5 KB
/
TLPhotoLayout.m
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
//
// TLPhotoLayout.m
// Mercatalog
//
// Created by Nathan Vander Wilt on 12/24/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "TLPhotoLayout.h"
#import "TLPhoto.h"
#import "TLLocation.h"
#include "TLGeometry.h"
#include "TLFloat.h"
#import "TLCocoaToolbag.h"
@class TLItemLayoutBox;
static TLItemLayoutBox* TLItemLayoutBoxesFindBiggest(NSSet* layoutBoxes, CGFloat minSize);
static NSArray* TLItemLayoutBoxesSortMostFirst(NSSet* layoutBoxes);
static void TLPhotoLayoutSetFrameForPhoto(NSMapTable* frames, CGRect frame, TLPhoto* photo);
static TLPhoto* TLPhotoLayoutFindClosest(NSSet* photos, CGRect rect, TLProjectionRef proj);
@interface TLItemLayoutBox : NSObject {
@private
CGRect box;
NSUInteger itemCount;
}
+ (id)layoutBoxWithRect:(CGRect)theBox;
@property (nonatomic, readonly) NSUInteger itemCount;
- (CGRect)rectForItem:(NSUInteger)itemIdx;
- (void)addItem;
- (CGFloat)sizeAfterNextItem;
@end
@implementation TLPhotoLayout
#pragma mark Lifecycle
- (id)initWithFrames:(NSMapTable*)theFrames anchors:(NSMapTable*)theAnchors {
self = [super init];
if (self) {
frames = [theFrames copy];
anchors = [theAnchors copy];
}
return self;
}
- (void)dealloc {
[frames release];
[anchors release];
[super dealloc];
}
#pragma mark Accessors
- (BOOL)photoHasLayout:(TLPhoto*)photo {
return TLBooleanCast([frames objectForKey:photo]);
}
- (CGRect)frameForPhoto:(TLPhoto*)photo {
NSValue* frameValue = [frames objectForKey:photo];
CGRect photoFrame = CGRectNull;
if (frameValue) {
photoFrame = NSRectToCGRect([frameValue rectValue]);
}
return photoFrame;
}
- (CGPoint)anchorForPhoto:(TLPhoto*)photo {
NSValue* anchorValue = [anchors objectForKey:photo];
CGPoint photoAnchor = CGPointZero;
if (anchorValue) {
photoAnchor = NSPointToCGPoint([anchorValue pointValue]);
}
return photoAnchor;
}
#pragma mark Actual geometry
+ (NSSet*)layoutBoxesForPhotos:(NSSet*)photos
inBounds:(CGRect)bounds
withProjection:(TLProjectionRef)proj
badPhotos:(NSSet**)outOfBoundsPhotosPtr
{
CGRect anchorBounds = CGRectNull;
NSMutableSet* outOfBoundsPhotos = [NSMutableSet set];
for (TLPhoto* photo in photos) {
TLProjectionError err = TLProjectionErrorNone;
CGPoint photoPoint = TLProjectionProjectCoordinate(proj, [[photo location] coordinate], &err);
if (err || !CGRectContainsPoint(bounds, photoPoint)) {
[outOfBoundsPhotos addObject:photo];
}
else {
anchorBounds = TLCGRectExpandToIncludePoint(anchorBounds, photoPoint);
}
}
if (CGRectIsNull(anchorBounds)) {
return nil;
}
CGFloat leftWidth = CGRectGetMinX(anchorBounds) - CGRectGetMinX(bounds);
CGFloat rightWidth = CGRectGetMaxX(bounds) - CGRectGetMaxX(anchorBounds);
CGFloat bottomHeight = CGRectGetMinY(anchorBounds) - CGRectGetMinY(bounds);
CGFloat topHeight = CGRectGetMaxY(bounds) - CGRectGetMaxY(anchorBounds);
enum { top = 0, bottom = 1, left = 2, right = 3 };
CGRect frameArray[4] = { CGRectNull, CGRectNull, CGRectNull, CGRectNull };
if (CGRectGetWidth(anchorBounds) > CGRectGetHeight(anchorBounds)) {
frameArray[top] = CGRectMake(CGRectGetMinX(bounds), CGRectGetMaxY(anchorBounds),
CGRectGetWidth(bounds), topHeight);
frameArray[bottom] = CGRectMake(CGRectGetMinX(bounds), CGRectGetMinY(bounds),
CGRectGetWidth(bounds), bottomHeight);
CGFloat sideBaseY = CGRectGetMinY(bounds) + bottomHeight;
CGFloat sideHeight = CGRectGetHeight(bounds) - bottomHeight - topHeight;
frameArray[left] = CGRectMake(CGRectGetMinX(bounds), sideBaseY,
leftWidth, sideHeight);
frameArray[right] = CGRectMake(CGRectGetMaxX(bounds) - rightWidth, sideBaseY,
rightWidth, sideHeight);
}
else {
frameArray[left] = CGRectMake(CGRectGetMinX(bounds), CGRectGetMinY(bounds),
leftWidth, CGRectGetHeight(bounds));
frameArray[right] = CGRectMake(CGRectGetMaxX(bounds) - rightWidth, CGRectGetMinY(bounds),
rightWidth, CGRectGetHeight(bounds));
CGFloat capBaseX = CGRectGetMinX(bounds) + leftWidth;
CGFloat capWidth = CGRectGetWidth(bounds) - leftWidth - rightWidth;
frameArray[top] = CGRectMake(capBaseX, CGRectGetMaxY(bounds) - topHeight,
capWidth, topHeight);
frameArray[bottom] = CGRectMake(capBaseX, CGRectGetMinY(bounds),
capWidth, bottomHeight);
}
NSUInteger numFrames = sizeof(frameArray) / sizeof(*frameArray);
NSMutableSet* boxes = [NSMutableSet setWithCapacity:numFrames];
for (NSUInteger frameIdx = 0; frameIdx < numFrames; ++frameIdx) {
TLItemLayoutBox* box = [TLItemLayoutBox layoutBoxWithRect:frameArray[frameIdx]];
[boxes addObject:box];
}
if (outOfBoundsPhotosPtr) *outOfBoundsPhotosPtr = outOfBoundsPhotos;
return boxes;
}
+ (NSMapTable*)framePhotos:(NSSet*)photos
inBox:(TLItemLayoutBox*)layoutBox
projection:(TLProjectionRef)proj
{
NSMapTable* boxFrames = [NSMapTable mapTableWithStrongToStrongObjects];
NSMutableSet* remainingPhotos = [NSMutableSet setWithSet:photos];
NSUInteger numItems = [layoutBox itemCount];
for (NSUInteger itemIdx = 0; itemIdx < numItems; ++itemIdx) {
CGRect photoFrame = [layoutBox rectForItem:itemIdx];
TLPhoto* closestPhoto = TLPhotoLayoutFindClosest(remainingPhotos, photoFrame, proj);
TLPhotoLayoutSetFrameForPhoto(boxFrames, photoFrame, closestPhoto);
[remainingPhotos removeObject:closestPhoto];
}
return boxFrames;
}
+ (id)photoLayoutForPhotos:(NSSet*)photos
inBounds:(CGRect)bounds
minDimension:(CGFloat)minSize
projection:(TLProjectionRef)proj
{
NSSet* outOfBoundsPhotos = nil;
NSSet* layoutBoxes = [self layoutBoxesForPhotos:photos
inBounds:bounds
withProjection:proj
badPhotos:&outOfBoundsPhotos];
// add each photo to the "biggest" box in turn, until min size is reached
NSUInteger numPhotosRemaining = [photos count] - [outOfBoundsPhotos count];
while (numPhotosRemaining) {
TLItemLayoutBox* bestBox = TLItemLayoutBoxesFindBiggest(layoutBoxes, minSize);
if (!bestBox) break;
[bestBox addItem];
--numPhotosRemaining;
}
// add nearest photos to each box
NSMapTable* photoFrames = [NSMapTable mapTableWithStrongToStrongObjects];
NSArray* orderedBoxes = TLItemLayoutBoxesSortMostFirst(layoutBoxes);
NSMutableSet* leftoverPhotos = [NSMutableSet setWithSet:photos];
[leftoverPhotos minusSet:outOfBoundsPhotos];
for (TLItemLayoutBox* box in orderedBoxes) {
NSMapTable* boxFrames = [self framePhotos:leftoverPhotos inBox:box projection:proj];
TLNSMapTableSetWithMapTable(photoFrames, boxFrames);
[leftoverPhotos minusSet:TLNSMapTableAllKeys(boxFrames)];
}
// put leftover photos at minSize on anchor
[leftoverPhotos unionSet:outOfBoundsPhotos];
for (TLPhoto* photo in leftoverPhotos) {
TLProjectionError err = TLProjectionErrorNone;
CGPoint photoPoint = TLProjectionProjectCoordinate(proj, [[photo location] coordinate], &err);
if (err) continue;
CGRect photoRect = TLCGRectMakeAroundPoint(photoPoint, minSize, minSize);
TLPhotoLayoutSetFrameForPhoto(photoFrames, photoRect, photo);
}
NSMapTable* photoAnchors = [NSMapTable mapTableWithStrongToStrongObjects];
for (TLPhoto* photo in photos) {
TLProjectionError err = TLProjectionErrorNone;
CGPoint photoPoint = TLProjectionProjectCoordinate(proj, [[photo location] coordinate], &err);
if (err) continue;
NSValue* pointValue = [NSValue valueWithPoint:NSPointFromCGPoint(photoPoint)];
[photoAnchors setObject:pointValue forKey:photo];
}
TLPhotoLayout* photoLayout = [[self alloc] initWithFrames:photoFrames
anchors:photoAnchors];
return [photoLayout autorelease];
}
@end
@implementation TLItemLayoutBox
#pragma mark Lifecycle
- (id)initWithRect:(CGRect)theBox {
self = [super init];
if (self) {
box = theBox;
}
return self;
}
+ (id)layoutBoxWithRect:(CGRect)theBox {
TLItemLayoutBox* layoutBox = [[[self class] alloc] initWithRect:theBox];
return [layoutBox autorelease];
}
@synthesize itemCount;
- (void)addItem {
++itemCount;
}
- (CGFloat)sizeWithCount:(NSUInteger)theItemCount {
CGFloat maxDimension = (CGFloat)fmax(CGRectGetWidth(box), CGRectGetHeight(box));
CGFloat minDimension = (CGFloat)fmin(CGRectGetWidth(box), CGRectGetHeight(box));
return (CGFloat)fmin(maxDimension / theItemCount, minDimension);
}
- (CGRect)rectForItem:(NSUInteger)itemIdx {
CGFloat size = [self sizeWithCount:[self itemCount]];
CGFloat boxWidth = CGRectGetWidth(box);
CGFloat boxHeight = CGRectGetHeight(box);
if (boxWidth > boxHeight) {
// center height
CGFloat frameBaseY = CGRectGetMinY(box) + (boxHeight - size) / 2.0f;
// pad width
CGFloat paddedWidth = boxWidth / [self itemCount];
CGFloat frameBaseX = CGRectGetMinX(box) + (paddedWidth - size) / 2.0f;
return CGRectMake(frameBaseX + itemIdx * paddedWidth, frameBaseY, size, size);
}
else {
// center width
CGFloat frameBaseX = CGRectGetMinX(box) + (boxWidth - size) / 2.0f;
// pad height
CGFloat paddedHeight = boxHeight / [self itemCount];
CGFloat frameBaseY = CGRectGetMinY(box) + (paddedHeight - size) / 2.0f;
return CGRectMake(frameBaseX, frameBaseY + itemIdx * paddedHeight, size, size);
}
}
- (CGFloat)sizeAfterNextItem {
return [self sizeWithCount:([self itemCount] + 1)];
}
@end
NSArray* TLItemLayoutBoxesSortMostFirst(NSSet* layoutBoxes) {
NSSortDescriptor* countSort = [[NSSortDescriptor alloc] initWithKey:@"itemCount" ascending:NO];
NSArray* descriptors = [NSArray arrayWithObject:countSort];
[countSort release];
return [[layoutBoxes allObjects] sortedArrayUsingDescriptors:descriptors];
}
TLItemLayoutBox* TLItemLayoutBoxesFindBiggest(NSSet* layoutBoxes, CGFloat minSize) {
TLItemLayoutBox* bestBox = nil;
CGFloat bestSize = NAN;
for (TLItemLayoutBox* box in layoutBoxes) {
CGFloat boxSize = [box sizeAfterNextItem];
if (boxSize < minSize) continue;
if (!bestBox || boxSize > bestSize) {
bestBox = box;
bestSize = boxSize;
}
}
return bestBox;
}
void TLPhotoLayoutSetFrameForPhoto(NSMapTable* frames, CGRect frame, TLPhoto* photo) {
NSValue* frameValue = [NSValue valueWithRect:NSRectFromCGRect(frame)];
[frames setObject:frameValue forKey:photo];
}
TLPhoto* TLPhotoLayoutFindClosest(NSSet* photos, CGRect rect, TLProjectionRef proj) {
CGPoint targetPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
TLPhoto* closestPhoto = nil;
CGFloat closestDistanceSqd = NAN;
for (TLPhoto* photo in photos) {
TLProjectionError err = TLProjectionErrorNone;
CGPoint photoPoint = TLProjectionProjectCoordinate(proj, [[photo location] coordinate], &err);
if (err) continue;
CGFloat photoDistanceSqd = TLPointDistanceSquared(photoPoint, targetPoint);
if (!closestPhoto || photoDistanceSqd < closestDistanceSqd) {
closestPhoto = photo;
closestDistanceSqd = photoDistanceSqd;
}
}
return closestPhoto;
}