-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFormation.cpp
More file actions
581 lines (498 loc) · 15.2 KB
/
Formation.cpp
File metadata and controls
581 lines (498 loc) · 15.2 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
//
// Filename: "Formation.cpp"
//
// Programmer: Ross Mead
// Last modified: 30Nov2009
//
// Description: This class implements a formation.
//
// preprocessor directives
#include "Formation.h"
// <constructors>
//
// Formation(f, r, sGrad, sID, fID, theta)
// Last modified: 04Sep2006
//
// Default constructor that initializes
// this formation to the parameterized values.
//
// Returns: <none>
// Parameters:
// f in the initial function of the formation
// r in the initial radius of the formation
// sGrad in the initial seed gradient of the formation
// sID in the initial seed ID of the formation
// fID in the initial ID of the formation
// theta in the initial heading of the formation
//
Formation::Formation(const Function f,
const GLfloat r,
const Vector sGrad,
const GLint sID,
const GLint fID,
const GLfloat theta)
{
setFunction(f);
setRadius(r);
setSeedGradient(sGrad);
setSeedID(sID);
setFormationID(fID);
setHeading(theta);
} // Formation(const..{Function, GLfloat, Vector, GLint, GLint, GLfloat})
//
// Formation(f, r, sGrad, sID, fID, theta)
// Last modified: 29Nov2009
//
// Default constructor that initializes
// the formation to the parameterized values.
//
// Returns: <none>
// Parameters:
// f in the initial set of functions of the formation
// r in the initial radius of the formation
// sGrad in the initial seed gradient of the formation
// sID in the initial seed ID of the formation
// fID in the initial ID of the formation
// theta in the initial heading of the formation
//
Formation::Formation(vector<Function> f,
const GLfloat r,
const Vector sGrad,
const GLint sID,
const GLint fID,
const GLfloat theta)
{
setFunctions(f);
setRadius(r);
setSeedGradient(sGrad);
setSeedID(sID);
setFormationID(fID);
setHeading(theta);
} // Formation(const..{LL<Function>, GLfloat, Vector, GLint..<2>, GLfloat})
//
// Formation(f)
// Last modified: 28Aug2006
//
// Copy constructor that copies the contents of
// the parameterized formation into this formation.
//
// Returns: <none>
// Parameters:
// f in/out the formation being copied
//
Formation::Formation(const Formation &f)
{
*this = f;
} // Formation(const Formation &)
// <public mutator functions>
//
// bool setFunction(f)
// Last modified: 28Aug2006
//
// Attempts to set the function to the parameterized function,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// f in/out the function to be set to
//
bool Formation::setFunction(const Function f)
{
clear();
return addFunction(f);
} // setFunction(const Function)
//
// bool setFunctions(f)
// Last modified: 29Nov2009
//
// Attempts to set the set of functions
// to the parameterized set of functions,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// f in/out the set of functions to be set to
//
bool Formation::setFunctions(const vector<Function> &f)
{
clear();
return addFunctions(f);
} // setFunctions(const vector<Function> &)
//
// bool addFunction(f)
// Last modified: 29Nov2009
//
// Attempts to add the parameterized function to the formation,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// f in the function being added
//
bool Formation::addFunction(const Function f)
{
if (f == NULL) return false;
push_back(f);
return true;
} // addFunction(const Function)
//
// bool addFunctions(f)
// Last modified: 29Nov2009
//
// Attempts to add the parameterized set of functions to the formation,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// f in/out the set of functions being added
//
bool Formation::addFunctions(const vector<Function> &f)
{
for (GLint i = 0; i < f.size(); ++i)
if (!addFunction(f[i])) return false;
return true;
} // addFunctions(const vector<Function> &)
//
// bool removeFunction(pos)
// Last modified: 29Nov2009
//
// Attempts to remove the function at the
// parameterized position from the formation,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// pos in the position to remove (default 0)
//
bool Formation::removeFunction(const GLint pos)
{
if ((pos < 0) || (pos >= size())) return false;
erase(begin() + pos);
return true;
} // removeFunction(const GLint)
//
// bool removeFunctions()
// Last modified: 28Aug2006
//
// Attempts to remove all of the functions from the formation,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// pos in the position to remove (default 0)
//
bool Formation::removeFunctions()
{
clear();
return true;
} // removeFunctions()
//
// bool setRadius(r)
// Last modified: 28Aug2006
//
// Attempts to set the radius to the parameterized radius,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// r in the radius to be set to
//
bool Formation::setRadius(const GLfloat r)
{
if (r <= 0.0f) return false;
radius = r;
return true;
} // setRadius(const GLfloat)
//
// bool setSeedGradient(sGrad)
// Last modified: 28Aug2006
//
// Attempts to set the seed gradient
// to the parameterized seed gradient,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// sGrad in/out the seed gradient to be set to
//
bool Formation::setSeedGradient(const Vector sGrad)
{
seedGradient = sGrad;
return true;
} // setSeedGradient(const Vector)
//
// bool setSeedID(sID)
// Last modified: 28Aug2006
//
// Attempts to set the seed ID to the parameterized seed ID,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// sID in/out the seed ID to be set to
//
bool Formation::setSeedID(const GLint sID)
{
if ((sID < 0) && (sID != ID_OPERATOR) && (sID != ID_BROADCAST))
return false;
seedID = sID;
return true;
} // setSeedID(const GLint)
//
// bool setFormationID(fID)
// Last modified: 28Aug2006
//
// Attempts to set the formation ID to the parameterized formation ID,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// fID in/out the formation ID to be set to
//
bool Formation::setFormationID(const GLint fID)
{
formationID = fID;
return true;
} // setFormationID(const GLint)
//
// bool setHeading(theta)
// Last modified: 28Aug2006
//
// Attempts to set the heading to the parameterized heading,
// returning true if successful, false otherwise.
//
// Returns: true if successful, false otherwise
// Parameters:
// theta in/out the heading to be set to
//
bool Formation::setHeading(const GLfloat theta)
{
heading = scaleDegrees(theta);
return true;
} // setHeading(const GLint)
// <public accessor functions>
//
// Function getFunction() const
// Last modified: 30Nov2009
//
// Returns the function at the parameterized position in this formation.
//
// Returns: the function at the parameterized position in this formation
// Parameters:
// pos in the position of the desired function (default 0)
//
Function Formation::getFunction(const int pos) const
{
if ((pos < 0) || (pos > this->size())) return NULL;
return at(pos);
} // getFunction(const int) const
//
// vector<Function> getFunctions() const
// Last modified: 29Nov2009
//
// Returns the set of functions of this formation.
//
// Returns: the set of functions of this formation
// Parameters: <none>
//
vector<Function> Formation::getFunctions() const
{
return *this;
} // getFunctions() const
//
// GLfloat getRadius() const
// Last modified: 28Aug2006
//
// Returns the radius of this formation.
//
// Returns: the radius of this formation
// Parameters: <none>
//
GLfloat Formation::getRadius() const
{
return radius;
} // getRadius() const
//
// Vector getSeedGradient() const
// Last modified: 28Aug2006
//
// Returns the seed gradient of this formation.
//
// Returns: the seed gradient of this formation
// Parameters: <none>
//
Vector Formation::getSeedGradient() const
{
return seedGradient;
} // getSeedGradient() const
//
// GLint getSeedID() const
// Last modified: 28Aug2006
//
// Returns the seed ID of this formation.
//
// Returns: the seed ID of this formation
// Parameters: <none>
//
GLint Formation::getSeedID() const
{
return seedID;
} // getSeedID() const
//
// GLint getFormationID() const
// Last modified: 28Aug2006
//
// Returns the formation ID of this formation.
//
// Returns: the formation ID of this formation
// Parameters: <none>
//
GLint Formation::getFormationID() const
{
return formationID;
} // getFormationID() const
//
// GLfloat getHeading() const
// Last modified: 28Aug2006
//
// Returns the heading of this formation.
//
// Returns: the heading of this formation
// Parameters: <none>
//
GLfloat Formation::getHeading() const
{
return heading;
} // getHeading() const
// <public utility functions>
//
// vector<Vector> getRelationships(c)
// Last modified: 29Nov2009
//
// Calculates the intersections of the set of functions of this formation
// and a circle centered at the parameterized vector position c with
// the appropriate radius, returning a list of these vectors.
//
// Returns: list of the desired relationship vectors
// Parameters:
// c in the position to be centered at
//
vector<Vector> Formation::getRelationships(const Vector c)
{
if (empty()) return vector<Vector>();
vector<Vector> rels;
Function curr = NULL;
for (GLint i = 0; i < size(); ++i)
{
curr = at(i);
rels.push_back(getRelationship(curr, -radius, c, heading));
rels.push_back(getRelationship(curr, radius, c, heading));
}
return rels;
} // getRelationships(const Vector)
//
// Vector getRelationship(f, r, c, theta)
// Last modified: 28Aug2006
//
// Uses the secant method to calculate the intersection of the function
// and a circle centered at the parameterized vector position c with
// the appropriate radius, returning a vector from c to this intersection.
//
// The secant method is defined by the following recurrence relation:
//
// x_(n + 1) = x_n - f(x_n) * (x_n - x_(n - 1)) / (f(x_n) - f(x_(n - 1))).
//
// Returns: vector from the parameterized vector position c
// to the intersection of the function and appropriate circle
// Parameters:
// f in the intersecting function
// r in the radius of the intersecting circle
// c in the position to be centered at
// theta in the rotation of the relationship (default 0)
//
Vector Formation::getRelationship(const Function f,
const GLfloat r,
const Vector c,
const GLfloat theta)
{
if (f == NULL) return Vector();
GLfloat xn = c.x + r + X_ROOT_THRESHOLD,
xn_1 = c.x + r - X_ROOT_THRESHOLD,
intersect = 0.0f, error = 0.0f;
for (int i = 0; i < X_N_ITERATIONS; ++i)
{
intersect = fIntersect(f, r, c, xn);
error = intersect * (xn - xn_1) /
(intersect - fIntersect(f, r, c, xn_1));
if (abs(error) <= X_ROOT_THRESHOLD) break;
xn_1 = xn;
xn -= error;
}
Vector rel = Vector(xn, f(xn)) - c;
rel.rotateRelative(-theta);
return rel;
} // getRelationship(const..{Function, GLfloat, Vector, GLfloat})
//
// Vector getRelationship(pos, r, c, theta)
// Last modified: 28Aug2006
//
// Calculates the intersection of the function at the parameterized position
// and a circle centered at the parameterized vector position c with
// the appropriate radius, returning a vector from c to this intersection.
//
// Returns: vector from the parameterized vector position c
// to the intersection of the function and appropriate circle
// Parameters:
// pos in the position of the desired function (default 0)
// r in the radius of the intersecting circle
// c in the position to be centered at
// theta in the rotation of the relationship (default 0)
//
Vector Formation::getRelationship(const GLint pos,
const GLfloat r,
const Vector c,
const GLfloat theta)
{
return getRelationship(getFunction(pos), r, c, theta);
} // getRelationship(const GLint, const GLfloat, const Vector, const GLfloat)
// <virtual overloaded operators>
//
// Formation& =(f)
// Last modified: 28Aug2006
//
// Copies the contents of the parameterized formation into this formation.
//
// Returns: this formation
// Parameters:
// f in/out the formation being copied
//
Formation& Formation::operator =(const Formation &f)
{
setFunctions(f);
setRadius(f.radius);
setSeedGradient(f.seedGradient);
setSeedID(f.seedID);
setFormationID(f.formationID);
setHeading(f.heading);
return *this;
} // =(const Formation &)
// <protected utility functions>
//
// GLfloat fIntersect(f, r, c, x)
// Last modified: 28Aug2006
//
// Used to determine the intersection of the parameterized function
// and a circle centered at the parameterized vector position c and
// offset by x with the appropriate radius.
//
// Returns: this formation
// Parameters:
// f in/out the formation being copied
//
GLfloat Formation::fIntersect(const Function f, const GLfloat r,
const Vector c, const GLfloat x)
{
GLfloat dx = x - c.x, dy = f(x) - c.y;
return dx * dx + dy * dy - r * r;
//return pow(x - c.x, 2.0f) + pow(f(x) - c.y, 2.0f) - pow(r, 2.0f);
} // fIntersect(const Function, const GLfloat, const Vector, const GLfloat)