-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.h
409 lines (360 loc) · 10.8 KB
/
utils.h
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
//
// Created by Jackson Hall on 5/1/2020.
//
#ifndef PART_2___GRAPHICS_ALTERNATE_UTILS_H
#define PART_2___GRAPHICS_ALTERNATE_UTILS_H
#include <utility>
#include <vector>
#include <cmath>
#include <iostream>
#include <memory>
#if __cplusplus < 201700
#include <experimental/optional>
#include <GL/gl.h>
using std::experimental::optional;
using std::experimental::nullopt;
using std::experimental::make_optional;
#else
#include <optional>
#endif
/** ========== Functions ========== */
/**
* Return the square of the given double value.
*/
double square(double n);
/**
* Return the given degrees value converted to radians.
*/
double rad(double deg);
/**
* Return the given radians value converted to degrees.
*/
double deg(double rad);
/**
* Return a % n, which is not natively defined for doubles.
*/
double mod(double a, double n);
/**
* Set a to a % n, which is not natively defined for doubles.
*/
void modEquals(double *a, double n);
/** ========== Structs ========== */
/**
* Represents a vector with any number of dimensional components.
*/
struct spatialVector {
/** Fields */
std::vector<double> components;
/** Constructors */
spatialVector();
spatialVector(const spatialVector& other);
explicit spatialVector(const std::vector<double>& components);
/** Other Methods */
/* Utility */
/**
* Add all of other's components to this's components.
*/
void plus(const spatialVector& other);
/**
* Subtract all of other's components from this's components.
*/
void minus(const spatialVector& other);
/**
* Multiply all components of a vector by the given scalar.
*/
void scale(double scalar);
/**
* Return the magnitude of the vector.
*/
double magnitude() const;
/**
* Compute the dot product of this vector and another.
*/
double dot(const spatialVector& other) const;
/**
* Return the cosine of the angle between this vector and another in
* radians.
*/
double cosOfAngleBetween(const spatialVector& other) const;
/**
* Return the scalar projection of this vector onto another.
*/
double scalarProjectOnto(const spatialVector& other) const;
/**
* Return the magnitude of the vector rejection of this from other.
*/
double scalarRejectFrom(const spatialVector& other) const;
};
/**
* Represents a point. Abstract, derived by point2d, point3d, and point4d.
*/
struct point {
/** Other Methods */
/* Movement */
virtual void move(const std::vector<double>& dPosition) = 0;
virtual void move(const spatialVector& dPosition) = 0;
};
/**
* Represents a point in 2 dimensions.
*/
struct point2d : public point {
/** Fields */
double x, y;
/** Constructors */
point2d();
point2d(const point2d& other);
point2d(double x, double y);
/** Other Methods */
/* Utility */
/**
* Return the Euclidean distance from this point to another.
*/
double distanceTo(const point2d& other) const;
/* Movement */
void moveX(double dX);
void moveY(double dY);
void move(double dX, double dY);
void move(const std::vector<double>& dPosition) override;
void move(const spatialVector& dPosition) override;
};
/**
* Represents a point in 3 dimensions.
*/
struct point3d : public point {
/** Fields */
double x, y, z;
/** Constructors */
point3d();
point3d(const point3d& other);
point3d(double x, double y, double z);
/** Other Methods */
/* Utility */
/**
* Return the Euclidean distance from this point to another.
*/
double distanceTo(const point3d& other) const;
/* Movement */
void moveX(double dX);
void moveY(double dY);
void moveZ(double dZ);
void move(double dX, double dY, double dZ);
void move(const std::vector<double>& dPosition) override;
void move(const spatialVector& dPosition) override;
};
/**
* Represents a point in 4 dimensions.
*/
struct point4d : public point {
/** Fields */
double x, y, z, a;
/** Constructors */
point4d();
point4d(const point4d& other);
point4d(double x, double y, double z, double a);
/** Other Methods */
/* Utility */
/**
* Return the Euclidean distance from this point to another.
*/
double distanceTo(const point4d& other) const;
/* Movement */
void moveX(double dX);
void moveY(double dY);
void moveZ(double dZ);
void moveA(double dA);
void move(double dX, double dY, double dZ, double dA);
void move(const std::vector<double>& dPosition) override;
void move(const spatialVector& dPosition) override;
};
/**
* Stores pairs of pointers to point objects.
*/
struct edge {
/** Virtual Destructor */
virtual ~edge() = 0;
/** Other Methods */
/**
* Return the Euclidean distance between the endpoints of this edge.
*/
virtual double length() const = 0;
};
/**
* Stores a pair of pointers to point2d objects.
*/
struct edge2d : edge {
/** Fields */
std::unique_ptr<point2d> p1, p2;
/** Constructors */
edge2d();
edge2d(const edge2d& other);
edge2d(const point2d& p1, const point2d& p2);
/** Other Methods */
/**
* Return the Euclidean distance between the endpoints of this edge.
*/
double length() const override;
/**
* Draws a line on the OpenGL screen between p1 and p2.
*/
void draw() const;
};
/**
* Stores a pair of pointers to point2d objects.
*/
struct edge3d : edge {
/** Fields */
std::unique_ptr<point3d> p1, p2;
/** Constructors */
edge3d();
edge3d(const edge3d& other);
edge3d(const point3d& p1, const point3d& p2);
/** Other Methods */
/**
* Return the Euclidean distance between the endpoints of this edge.
*/
double length() const override;
};
/**
* Stores a pair of pointers to point2d objects.
*/
struct edge4d : edge {
/** Fields */
std::unique_ptr<point4d> p1, p2;
/** Constructors */
edge4d();
edge4d(const edge4d& other);
edge4d(const point4d& p1, const point4d& p2);
/** Other Methods */
/**
* Return the Euclidean distance between the endpoints of this edge.
*/
double length() const override;
};
/**
* Stores n-1 angles that can be used to define any direction in
* n-dimensional space. Abstract, derived by sphericalAngle3d and
* sphericalAngle4d.
*/
struct sphericalAngle {
/// Note: All calculations done in degrees
/// Note: Left-handed coordinate system
/** Getters */
/**
* Return the unit vector in the direction defined by this object.
*/
virtual spatialVector getUnitVector() = 0;
virtual void rotate(std::vector<double> angles) = 0;
};
/**
* Stores 2 angles that can be used to define any direction in 3d space.
*/
struct sphericalAngle3d : public sphericalAngle {
/// Note: All calculations done in degrees
/// Note: Left-handed coordinate system
/** Fields */
// polarAngle rotates around the (vertical) z-axis, ranges from [0, 360)
// degrees and where 0 is towards the positive y-axis, and increasing
// rotates east (CCW rotation from top-down)
// azimuthAngle ranges from [0, 180] degrees where 0 is straight up and
// 180 is straight down (90 is forward)
double polarAngle, azimuthAngle;
/** Constructors */
sphericalAngle3d();
sphericalAngle3d(sphericalAngle3d const &other);
sphericalAngle3d(double polarAngle, double azimuthAngle);
/** Setters */
/**
* Set polar angle to given value, capping between [0, 360)
*/
void setPolar(double newPolarAngle);
/**
* Set azimuth angle to given value, capping between [0, 180]
*/
void setAzimuth(double newAzimuthAngle);
/** Other Methods */
/* Utility */
/**
* Return the unit vector pointing in the direction defined by this object.
*/
spatialVector getUnitVector() override;
/* Rotation */
/**
* Increase rotation of polarAngle by dPolarAngle degrees, staying within
* [0, 360).
*/
void rotatePolar(double dPolarAngle);
/**
* Increase rotation of azimuthAngle by dAzimuthAngle degrees, staying
* within [0, 180].
*/
void rotateAzimuth(double dAzimuthAngle);
/**
* Increase rotation of polarAngle and azimuthAngle by values of the given
* vector (must be of size() 2).
*/
void rotate(std::vector<double> dAngles) override;
};
/**
* Stores 3 angles that can be used to define any direction in 4d space.
*/
struct sphericalAngle4d : public sphericalAngle {
/// Note: All calculations done in degrees
/// Note: Left-handed coordinate system
/// Note: Read comments in sphericalAngle3d to make more sense of below
// polarAngle rotates around the (vertical) z-axis, ranges from [0, 180]
// degrees and where 0 is towards the positive y-axis, and increasing
// rotates east (CCW rotation from top-down)
// azimuthAngle ranges from [0, 180] degrees where 0 is straight up and
// 180 is straight down (90 is forward)
// phiAngle ranges from [0, 360) degrees, where 0 lies as it would in our 3
// dimensions, 90 lies orthogonally "outward" to the 3d dimension,
// 180 lies opposite to 0 would in our 3 dimensions, and 270 lies
// orthogonally "inward" to the 3rd dimension (to give arbitrary
// names to 4d directions)
double polarAngle, azimuthAngle, phiAngle;
/** Constructors */
sphericalAngle4d();
sphericalAngle4d(const sphericalAngle4d& other);
sphericalAngle4d(double polarAngle, double azimuthAngle, double phiAngle);
/** Setters */
/**
* Set polar angle to given value, capping between [0, 180]
*/
void setPolar(double newPolarAngle);
/**
* Set azimuth angle to given value, capping between [0, 180]
*/
void setAzimuth(double newAzimuthAngle);
/**
* Set phi angle to given value, capping between [0, 360)
*/
void setPhi(double newPhiAngle);
/** Other Methods */
/* Utility */
/**
* Return the vector pointing in the direction defined by this object.
*/
spatialVector getUnitVector() override;
/* Rotation */
/**
* Increase rotation of polarAngle by dPolarAngle degrees, staying within
* [0, 180].
*/
void rotatePolar(double dPolarAngle);
/**
* Increase rotation of azimuthAngle by dAzimuthAngle degrees, staying
* within [0, 180].
*/
void rotateAzimuth(double dAzimuthAngle);
/**
* Increase rotation of phiAngle by dPhiAngle degrees, staying within
* [0, 180].
*/
void rotatePhi(double dPhiAngle);
/**
* Increase rotation of polarAngle and azimuthAngle by values of the given
* vector (must be of size() 3).
*/
void rotate(std::vector<double> dAngles) override;
};
#endif //PART_2___GRAPHICS_ALTERNATE_UTILS_H