-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phase.m
596 lines (554 loc) · 20.5 KB
/
Phase.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
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
//
// Phase.m
// ModeMaker
//
// Created by David Hirsch on 9/21/09.
// Copyright 2009 Western Washington University. All rights reserved.
//
#import "Phase.h"
#import "Particle.h"
#import "ModeMakerDoc.h"
#import "OvalParticle.h"
#import "RectangleParticle.h"
#import "BlobParticle.h"
#import "PolygonParticle.h"
#import "constants.h"
@implementation Phase
- (id) initByNumber: (short) phaseNum {
self = [super init];
if (self) {
[self setParticles: [NSArray array]]; // create empty array
if (!particles) { // if array was not created
[self release];
return nil;
}
// Defaults for new phases:
modeTargetError = 1;
aspectRatio = 1; // the target aspect ratio (for rectangles, blobs, and polygons)
aspectRatioSD = 0;
complexity = 5; // target number of control points on the path (for blobs and polygons) - does not include bezier handles
complexitySD = 2;
allowReentrants = YES; // whether a blob/polygon may be concave
fabricStrength = 0;
lastPosition = -1;
switch (phaseNum) {
case 0:
[self setName: @"Phase A"];
[self setColor: [NSColor blackColor]];
size = 50;
sizeSD = 10;
allowReentrants = NO; // whether a blob/polygon may be concave
overlapping = NO;
shape = 3; // Polygon, defined in ModeMakerDoc
modeTarget = 20;
break;
case 1:
[self setName: @"Phase B"];
[self setColor: [NSColor redColor]];
size = 20;
sizeSD = 1;
modeTarget = 10;
overlapping = YES;
aspectRatio = 1; // the target aspect ratio (for rectangles, blobs, and polygons)
aspectRatioSD = 0;
shape = 1; // Rect, defined in ModeMakerDoc
break;
case 2:
[self setName: @"Phase C"];
[self setColor: [NSColor greenColor]];
size = 7;
sizeSD = 0.1;
modeTarget = 10;
overlapping = YES;
shape = 1; // Rectangle, defined in ModeMakerDoc
aspectRatio = 3; // the target aspect ratio (for rectangles, blobs, and polygons)
aspectRatioSD = 1;
break;
case 3:
[self setName: @"Phase D"];
[self setColor: [NSColor blueColor]];
size = 80;
sizeSD = 30;
modeTarget = 10;
overlapping = NO;
aspectRatio = 1; // the target aspect ratio (for rectangles, blobs, and polygons)
aspectRatioSD = 0;
shape = 0; // Oval, defined in ModeMakerDoc
break;
case 4:
[self setName: @"Phase E"];
[self setColor: [NSColor purpleColor]];
size = 8;
sizeSD = 1;
modeTarget = 6;
overlapping = YES;
shape = 2; // Blob, defined in ModeMakerDoc
break;
default:
break;
}
}
return self;
}
- (ModeMakerDoc *) doc {return doc;}
- (void) setDoc: (ModeMakerDoc*)input {
doc = input; // don't retain this, since we don't really own the document
}
- (NSString *) name {return name;}
- (void) setName: (NSString*)input {
if (input != name) {
NSUndoManager *undoer = [doc undoManager];
[undoer registerUndoWithTarget:self
selector:@selector(setName:)
object:name];
[undoer setActionName: @"Name Change"];
}
[name autorelease];
name = [input retain];
}
- (NSColor*) color {return color;}
- (void) setColor: (NSColor*)input {
if (input != color) {
NSUndoManager *undoer = [doc undoManager];
[undoer registerUndoWithTarget:self
selector:@selector(setColor:)
object:color];
[undoer setActionName: @"Phase Color Change"];
}
[color autorelease];
color = [input retain];
}
- (NSArray*) particles {return particles;}
- (void) setParticles: (NSArray*)input {
[particles autorelease];
particles = [input retain];
}
- (float) modeTarget {return modeTarget;}
- (void) setModeTarget: (float)input {
if (input != modeTarget) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setModeTarget:modeTarget];
[undoer setActionName: @"Mode Target Change"];
modeTarget = input;
}
}
- (float) mode {return mode;}
- (void) setMode: (float)input {
[self willChangeValueForKey:@"mode"];
mode = input;
[self didChangeValueForKey:@"mode"];
}
- (BOOL) overlapping {return overlapping;}
- (void) setOverlapping: (BOOL)input {
if (input != overlapping) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setOverlapping:overlapping];
[undoer setActionName: @"Overlap Setting Change"];
overlapping = input;
}
}
- (int) shape {return shape;}
- (void) setShape: (int) input {
if (input != shape) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setShape:shape];
[undoer setActionName: @"Phase Shape Change"];
shape = input;
}
}
- (float) modeTargetError {return modeTargetError;}
- (void) setModeTargetError: (float) input {
if (input != modeTargetError) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setModeTargetError:modeTargetError];
[undoer setActionName: @"Mode Target Error Change"];
modeTargetError = input;
}
}
- (float) size {return size;}
- (void) setPhaseSize: (float) input {
if (input != size) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setPhaseSize:size];
[undoer setActionName: @"Size Change"];
size = input;
}
}
- (float) sizeSD {return sizeSD;}
- (void) setSizeSD: (float) input {
if (input != sizeSD) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setSizeSD:sizeSD];
[undoer setActionName: @"Size Std. Dev. Change"];
sizeSD = input;
}
}
- (float) aspectRatio {return aspectRatio;}
- (void) setPhaseAR: (float) input {
if (input != aspectRatio) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setPhaseAR:aspectRatio];
[undoer setActionName: @"Aspect Ratio Change"];
aspectRatio = input;
}
}
- (float) aspectRatioSD {return aspectRatioSD;}
- (void) setAspectRatioSD: (float) input {
if (input != aspectRatioSD) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setAspectRatioSD:aspectRatioSD];
[undoer setActionName: @"Aspect Ratio Std. Dev. Change"];
aspectRatioSD = input;
}
}
- (short) complexity {return complexity;}
- (void) setComplexity: (short) input {
if (input != complexity) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setComplexity:complexity];
[undoer setActionName: @"Complexity Change"];
complexity = input;
}
}
- (float) complexitySD {return complexitySD;}
- (void) setComplexitySD: (float) input {
if (input != complexitySD) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setComplexitySD:complexitySD];
[undoer setActionName: @"Complexity Std. Dev. Change"];
complexitySD = input;
}
}
- (float) fabricStrength {return fabricStrength;}
- (void) setFabricStrength: (float) input {
if (input != fabricStrength) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setFabricStrength:fabricStrength];
[undoer setActionName: @"Fabric Strength Change"];
fabricStrength = input;
}
}
- (BOOL) allowReentrants {return allowReentrants;}
- (void) setAllowReentrants: (BOOL) input {
if (input != allowReentrants) {
NSUndoManager *undoer = [doc undoManager];
[[undoer prepareWithInvocationTarget:self] setAllowReentrants:allowReentrants];
[undoer setActionName: @"Reentrancy Change"];
allowReentrants = input;
}
}
- (int) lastPosition {return lastPosition;}
- (void) setLastPosition: (int) input {lastPosition = input;}
- (void) dealloc {
[name release];
[color release];
[particles release];
[super dealloc];
}
- (void) draw
{
[color setFill];
long numParticles = [particles count];
for (long i=0; i < numParticles; i++) {
Particle *thisParticle = [particles objectAtIndex:i];
[thisParticle draw];
}
}
- (BOOL) settingsHaveChanged {
if ((shape != l_shape) ||
(size != l_size) ||
(overlapping != l_overlapping) ||
(sizeSD != l_sizeSD) ||
(l_aspectRatio != aspectRatio) ||
(l_aspectRatioSD != aspectRatioSD) ||
(l_complexity != complexity) ||
(l_complexitySD != complexitySD) ||
(l_fabricStrength != fabricStrength) ||
(l_allowReentrants != allowReentrants))
return YES;
// check to see if the measured mode is still within acceptable bounds - if the user expanded the error, then nothing needs to be done, for example
if (fabs(mode - modeTarget) > modeTargetError) return YES;
return NO;
}
- (BOOL) makeParticles {
NSSize canvasSize = [doc getCanvasSize];
float marginFactor = 0.1;
NSRect boundsRect = NSMakeRect(-(canvasSize.width) * marginFactor , -(canvasSize.height) * marginFactor, canvasSize.width * (1 + 2*marginFactor) , canvasSize.height * (1 + 2*marginFactor));
float totalArea = boundsRect.size.width * boundsRect.size.height;
[particles autorelease];
float modeOccupied = [doc getRoughModeUpTo: self];
int numberOfParticles = round(modeTarget * 0.01 * totalArea / ((size * size) * (1.0 - modeOccupied * 0.01)));
particles = [NSMutableArray array];
[particles retain];
BOOL foundUnplaceable = YES;
for (int loopNum = 0; (loopNum < kNumRetries) && foundUnplaceable; loopNum++) {
foundUnplaceable = NO;
for (int i=0; (i < numberOfParticles) && !foundUnplaceable; i++) {
foundUnplaceable = ![self couldPlaceOneParticleRandomlyWithin: boundsRect totalArea: totalArea];
} // end for numberOfParticles
if (foundUnplaceable) {
// failed to place a particle, so try removing the first 10%, and adding them back in new random spots
long numToRemove = floor([particles count] * 0.1);
[particles removeObjectsInRange: NSMakeRange(0, numToRemove)];
for (int i=0; i < numToRemove; i++) {
[self couldPlaceOneParticleRandomlyWithin: boundsRect totalArea: totalArea];
} // end for numToRemove
}
}
if (!foundUnplaceable) {
// record settings used to make particles
l_shape = shape;
l_modeTarget = modeTarget;
l_modeTargetError = modeTargetError;
l_size = size;
l_sizeSD = sizeSD;
l_overlapping = overlapping;
l_aspectRatio = aspectRatio;
l_aspectRatioSD = aspectRatioSD;
l_complexity = complexity;
l_complexitySD = complexitySD;
l_fabricStrength = fabricStrength;
l_allowReentrants = allowReentrants;
}
return !foundUnplaceable;
}
- (BOOL) couldPlaceOneParticleRandomlyWithin: (NSRect) boundsRect totalArea: (float) inTotalArea {
Particle *thisParticle;
switch (shape) {
case 0: { // Oval
thisParticle = [[OvalParticle alloc] initWithBoundsRect:boundsRect
size:(size)
sizeSD:(sizeSD)
aspectRatio: aspectRatio
aspectRatioSD: aspectRatioSD
complexity : complexity
complexitySD : complexitySD
fabricStrength: fabricStrength
allowReentrants : allowReentrants];
} break;
case 1: { // Rectangle
thisParticle = [[RectangleParticle alloc] initWithBoundsRect:boundsRect
size:(size)
sizeSD:(sizeSD)
aspectRatio: aspectRatio
aspectRatioSD: aspectRatioSD
complexity : complexity
complexitySD : complexitySD
fabricStrength: fabricStrength
allowReentrants : allowReentrants ];
} break;
case 2: { // Blob
thisParticle = [[BlobParticle alloc] initWithBoundsRect:boundsRect
size:(size)
sizeSD:(sizeSD)
aspectRatio: aspectRatio
aspectRatioSD: aspectRatioSD
complexity : complexity
complexitySD : complexitySD
fabricStrength: fabricStrength
allowReentrants : allowReentrants ];
} break;
case 3: { // Polygon
thisParticle = [[PolygonParticle alloc] initWithBoundsRect:boundsRect
size:(size)
sizeSD:(sizeSD)
aspectRatio: aspectRatio
aspectRatioSD: aspectRatioSD
complexity : complexity
complexitySD : complexitySD
fabricStrength: fabricStrength
allowReentrants : allowReentrants ];
} break;
}
if (!overlapping) { // no overlaps allowed
bool goodPlacing = false;
for (long j=0; (j < kNumParticlePlacingAttempts) && (!goodPlacing); j++) {
[thisParticle initWithBoundsRect:boundsRect
size:(size)
sizeSD:(sizeSD)
aspectRatio: aspectRatio
aspectRatioSD: aspectRatioSD
complexity : complexity
complexitySD : complexitySD
fabricStrength: fabricStrength
allowReentrants : allowReentrants ];
goodPlacing = [self noOverlapWithParticle: thisParticle];
}
if (!goodPlacing) {
// failure to place particle - clean up
[particles removeAllObjects]; //TODO Check this - perhaps just report up the chain, but don't remove all particles?
return NO;
}
}
[particles addObject:thisParticle];
return YES;
}
- (BOOL) isModeCorrect {
return (fabs(mode - modeTarget) <= modeTargetError);
}
- (float) shapeFactor {
switch (shape) {
case 1: // Rectangle
return 1.0;
break;
case 0: // Oval
case 2: // Blob
case 3: // Polygon
return pi / 4; // the area of one of these is not size^2, but less
}
return -1; // should never get here
}
- (BOOL)adjustModeAtIteration: (int) iterationNum {
// based on the difference between the (measured) mode and the target
// mode, either add or remove particles
float additionalModeNeeded = modeTarget - mode;
NSSize canvasSize = [doc getCanvasSize];
NSRect boundsRect = NSMakeRect(-(canvasSize.width) * 0.1 , -(canvasSize.height) * 0.1 , canvasSize.width * 1.2 , canvasSize.height * 1.2);
float boundsArea = boundsRect.size.width * boundsRect.size.height;
float modeOccupied = [doc getRoughModeUpTo: self];
float additionalAreaNeeded = additionalModeNeeded * boundsArea / 100.0;
additionalAreaNeeded *= 3.8 - (3 * erff((iterationNum - 5.0) / 3.0)); // this factor makes things change faster earlier in the iterations, and slower later (converges to ~0.8 factor after iteration 10 or so)
float oneParticleArea = size * size * [self shapeFactor];
oneParticleArea *= (100.0 - modeOccupied) / 100.0; // the modeOccupied fraction of each particle will be covered, and will not contribute to the particle's final mode
short additionalParticlesNeeded = floor(additionalAreaNeeded / oneParticleArea); // this assumes that the shape is a square
additionalParticlesNeeded = (additionalParticlesNeeded > kMaxParticlesToAddAtOnce) ? kMaxParticlesToAddAtOnce : additionalParticlesNeeded;
if ([particles count] == 0 && additionalParticlesNeeded == 0) {
// if the particles are big and the modeTarget is small, then we might get here. We can't use the size changing branch below, since there
// are no existing particles whose size we can alter
additionalParticlesNeeded = 1;
}
if (additionalParticlesNeeded > 0) {
BOOL foundUnplaceable = YES;
// loop kNumRetries times as long as there was a problem; If foundUnplaceable is NO, then we've succeeded
for (int loopNum = 0; (loopNum < kNumRetries) && foundUnplaceable; loopNum++) {
NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init];
foundUnplaceable = NO;
for (int i=0; (i < additionalParticlesNeeded) && !foundUnplaceable; i++) {
foundUnplaceable = ![self couldPlaceOneParticleRandomlyWithin: boundsRect totalArea: boundsArea];
} // end for numberOfParticles
if (foundUnplaceable) {
// failed to place a particle, so try removing the first 10%, and adding them back in new random spots
long numToRemove = floor([particles count] * 0.1);
[particles removeObjectsInRange: NSMakeRange(0, numToRemove)];
for (int i=0; i < numToRemove; i++) {
[self couldPlaceOneParticleRandomlyWithin: boundsRect totalArea: boundsArea];
} // end for numToRemove
}
[subPool release];
}
if (foundUnplaceable) {
// we were unable to place all needed particles, even with looping kNumRetries times - doc will post an alert
return NO;
}
} else if (additionalParticlesNeeded < 0) {
if (-additionalParticlesNeeded > [particles count] / 2.0) { // if we are trying to remove more than half of the existing number of particles
additionalParticlesNeeded = -([particles count]/2.0);
}
if (additionalParticlesNeeded == 0) // there was only a single (big) particle, and the previous statement said we could only remove half of that one. Just remove it here.
additionalParticlesNeeded = -1;
for (int i = 0; i < -additionalParticlesNeeded; i++) {
[particles removeLastObject];
}
} else {
// fewer than 1 additional Particles needed - try adjusting sizes of particles
float areaAdjustment = additionalAreaNeeded / [particles count];
// algorithm was exploding here I think; this should calm things down
float currentArea = (size*size);
if (areaAdjustment < -(currentArea))
areaAdjustment = -(currentArea)/2.0;
if (areaAdjustment > (currentArea))
areaAdjustment = (currentArea)/2.0;
float areaFactor = (currentArea + areaAdjustment) / currentArea;
for (int i=0; i < [particles count]; i++) {
[(Particle *)[particles objectAtIndex:i] adjustAreaByFactor: areaFactor];
}
}
return YES;
}
- (BOOL) containsPoint: (NSPoint) inPoint {
for (int i=0; i < [particles count]; i++) {
if ([(Particle *)[particles objectAtIndex:i] containsPoint: inPoint])
return true;
}
return false;
}
- (BOOL) noOverlapWithParticle: (Particle *) thisParticle {
for (int i=0; i < [particles count]; i++) {
if ([(Particle *)[particles objectAtIndex:i] overlapsWith: thisParticle])
return false;
}
return true;
}
#define name_key @"name"
#define doc_key @"doc"
#define shape_key @"shape"
#define modeTarget_key @"modeTarget"
#define mode_key @"mode"
#define modeTargetError_key @"modeTargetError"
#define size_key @"size"
#define sizeSD_key @"sizeSD"
#define aspectRatio_key @"aspectRatio"
#define aspectRatioSD_key @"aspectRatioSD"
#define complexity_key @"complexity"
#define complexitySD_key @"complexitySD"
#define allowReentrants_key @"allowReentrants"
#define overlapping_key @"overlapping"
#define color_key @"color"
#define particles_key @"particles"
#define fabricStrength_key @"fabricStrength"
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:name forKey:name_key];
[coder encodeConditionalObject:doc forKey:doc_key];
[coder encodeInt:shape forKey:shape_key];
[coder encodeFloat:modeTarget forKey:modeTarget_key];
[coder encodeFloat:mode forKey:mode_key];
[coder encodeFloat:modeTargetError forKey:modeTargetError_key];
[coder encodeFloat:size forKey:size_key];
[coder encodeFloat:sizeSD forKey:sizeSD_key];
[coder encodeFloat:aspectRatio forKey:aspectRatio_key];
[coder encodeFloat:aspectRatioSD forKey:aspectRatioSD_key];
[coder encodeInt:complexity forKey:complexity_key];
[coder encodeFloat:complexitySD forKey:complexitySD_key];
[coder encodeFloat:fabricStrength forKey:fabricStrength_key];
[coder encodeBool:allowReentrants forKey:allowReentrants_key];
[coder encodeBool:overlapping forKey:overlapping_key];
[coder encodeObject:color forKey:color_key];
[coder encodeObject:particles forKey:particles_key];
// don't need to encode the l_ variables - they don't need to be saved
}
- (id)initWithCoder:(NSCoder *)decoder {
//self = [super initWithCoder:decoder];
name = [[decoder decodeObjectForKey:name_key] retain];
doc = [decoder decodeObjectForKey:doc_key];
shape = [decoder decodeIntForKey:shape_key];
modeTarget = [decoder decodeFloatForKey:modeTarget_key];
mode = [decoder decodeFloatForKey:mode_key];
modeTargetError = [decoder decodeFloatForKey:modeTargetError_key];
size = [decoder decodeFloatForKey:size_key];
sizeSD = [decoder decodeFloatForKey:sizeSD_key];
aspectRatio = [decoder decodeFloatForKey:aspectRatio_key];
aspectRatioSD = [decoder decodeFloatForKey:aspectRatioSD_key];
complexity = [decoder decodeIntForKey:complexity_key];
complexitySD = [decoder decodeFloatForKey:complexitySD_key];
fabricStrength = [decoder decodeFloatForKey:fabricStrength_key];
allowReentrants = [decoder decodeBoolForKey:allowReentrants_key];
overlapping = [decoder decodeBoolForKey:overlapping_key];
color = [[decoder decodeObjectForKey:color_key] retain];
particles = [[decoder decodeObjectForKey:particles_key] retain];
// set the last-used parameters to the loaded parameters
l_shape = shape;
l_modeTarget = modeTarget;
l_mode = mode;
l_modeTargetError = modeTargetError;
l_size = size;
l_sizeSD = sizeSD;
l_aspectRatio = aspectRatio;
l_aspectRatioSD = aspectRatioSD;
l_complexity = complexity;
l_complexitySD = complexitySD;
l_allowReentrants = allowReentrants;
l_overlapping = overlapping;
l_fabricStrength = fabricStrength;
return self;
}
@end