-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAero.h
274 lines (215 loc) · 6.62 KB
/
Aero.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
/*******************************************************************
* Aero.h
* KPS
*
* Author: Kareem Omar
* kareem.omar@uah.edu
* https://github.com/komrad36
*
* Last updated Feb 27, 2016
* This application is entirely my own work.
*******************************************************************/
//
// Aero uses collision-based or analytical modes and operates on a satellite
// or body defined as a series of polygons
// in 3-D space. When supplied with air density and velocity (in the Body frame),
// it approximates the drag force and torque on the body by simulating
// collisions and accumulating impulses and angular impulses per unit time.
//
// Aero can also analytically compute these results by obtaining non-occluded frontal areas
// using the Clipper polygon clipping library.
//
// Note that net force and torque are returned in the Body frame.
//
// This module is intended for use by a 6 DoF orbital/attitude propagator which
// calls the aer() function from its integrators to obtain forces and/or torques,
// such as KPS.
//
#pragma once
#define CLIP_PAD (1e-4)
#define CLIP_MUL (1e16)
#define INV_CLIP_MUL (1e-16)
#include "glm_util.h"
#include "clipper.hpp"
#include <iostream>
#include <thread>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdint>
// pure virtual base class allowing branchless selection of Grid vs Analytical
class Aero {
public:
virtual void aer(vec3& f, vec3& t, const double rho, const vec3& v) = 0;
virtual ~Aero() {}
};
// functionoid to compare two vectors by their y-component
struct compare_y {
bool operator()(vec3& lhs, vec3& rhs) {
return lhs.y < rhs.y;
}
};
// functionoid to compare two vectors by their z-component
struct compare_z{
bool operator()(vec3& lhs, vec3& rhs) {
return lhs.z < rhs.z;
}
};
class Aero_Grid : public Aero {
// --- VARIABLES ---
private:
// loop step bounds determined by polygon bounds
int min_k;
int max_k;
int min_m;
int max_m;
// rotated satellite center of mass
vec3 CM_R;
// linear pitch between collisions
const double pitch;
const int num_poly;
// scalar constant (2A = 2*pitch^2) that multiplies force
// see KPS research paper
const double f_scalar;
const int total_pts;
// satellite center of mass
const vec3 CM;
// ptr to Body frame polygons
vec3* const P_s;
// ptr to polygon normal vectors
vec3* const N;
// ptr to precomputation results
double* const precomp;
// ptrs to polygon bounds
double* const min_y;
double* const max_y;
double* const min_z;
double* const max_z;
// ptr to rotated polygons
vec3* const P_rot;
// --- /VARIABLES ---
// --- METHODS ---
private:
// precompute some info for speed,
// including panel normals and some of collision location math
inline void precompute(const int i) {
vec3* P = P_rot + i*NUM_VTX;
N[i] = glm::normalize(glm::cross(P[1] - P[0], P[1] - P[2]));
precomp[i] = glm::dot(N[i], P[0]);
// find mins and maxes of y and z of each panel
std::pair<vec3*, vec3*> y_pair = std::minmax_element(P, P + NUM_VTX, compare_y());
std::pair<vec3*, vec3*> z_pair = std::minmax_element(P, P + NUM_VTX, compare_z());
min_y[i] = y_pair.first->y;
max_y[i] = y_pair.second->y;
min_z[i] = z_pair.first->z;
max_z[i] = z_pair.second->z;
}
void collide(vec3& f, vec3& t, const double rho, const double v_mag2);
public:
Aero_Grid(const double linear_pitch, const int num_polygons, const vec3* const poly, const vec3 sat_CM) :
pitch(linear_pitch),
num_poly(num_polygons),
f_scalar(2.0*linear_pitch*linear_pitch),
total_pts(num_polygons * NUM_VTX),
CM(sat_CM),
P_s(new vec3[num_polygons * NUM_VTX]),
N(new vec3[num_polygons]),
precomp(new double[num_polygons]),
min_y(new double[num_polygons]),
max_y(new double[num_polygons]),
min_z(new double[num_polygons]),
max_z(new double[num_polygons]),
P_rot(new vec3[num_polygons * NUM_VTX]) {
// copy polygon data into internal storage
for (int i = 0; i < total_pts; ++i) {
P_s[i] = poly[i];
}
}
~Aero_Grid() {
delete[] P_s;
delete[] N;
delete[] precomp;
delete[] min_y;
delete[] max_y;
delete[] min_z;
delete[] max_z;
delete[] P_rot;
}
void aer(vec3& f, vec3& t, const double rho, const vec3& v);
// --- /METHODS ---
};
class Aero_Analytical : public Aero {
// --- VARIABLES ---
private:
// rotated satellite center of mass
vec3 CM_R;
const int num_poly;
const int total_pts;
// satellite center of mass
const vec3 CM;
// ptr to Body frame polygons
vec3* const P_s;
// ptr to polygon normal vectors
vec3* const N;
// ptr to precomputation results
double* const precomp;
// ptrs to polygon bounds
double* const min_y;
double* const max_y;
double* const min_z;
double* const max_z;
// ptr to rotated polygons
vec3* const P_rot;
std::vector<bool> lower_idx_is_above;
ClipperLib::Clipper clipper;
// --- /VARIABLES ---
// --- METHODS ---
private:
// precompute some info for speed,
// including panel normals and extents
inline void precompute(const int i) {
vec3* P = P_rot + i*NUM_VTX;
N[i] = glm::normalize(glm::cross(P[1] - P[0], P[1] - P[2]));
precomp[i] = glm::dot(N[i], P[0]);
// find mins and maxes of y and z of each panel
std::pair<vec3*, vec3*> y_pair = std::minmax_element(P, P + NUM_VTX, compare_y());
std::pair<vec3*, vec3*> z_pair = std::minmax_element(P, P + NUM_VTX, compare_z());
min_y[i] = y_pair.first->y;
max_y[i] = y_pair.second->y;
min_z[i] = z_pair.first->z;
max_z[i] = z_pair.second->z;
}
public:
Aero_Analytical(const double linear_pitch, const int num_polygons, const vec3* const poly, const vec3 sat_CM) :
num_poly(num_polygons),
total_pts(num_polygons * NUM_VTX),
CM(sat_CM),
P_s(new vec3[num_polygons * NUM_VTX]),
N(new vec3[num_polygons]),
precomp(new double[num_polygons]),
min_y(new double[num_polygons]),
max_y(new double[num_polygons]),
min_z(new double[num_polygons]),
max_z(new double[num_polygons]),
P_rot(new vec3[num_polygons * NUM_VTX]),
lower_idx_is_above(num_polygons * num_polygons, false) {
// silence unused para meter
static_cast<void>(linear_pitch);
// copy polygon data into internal storage
for (int i = 0; i < total_pts; ++i) {
P_s[i] = poly[i];
}
}
~Aero_Analytical() {
delete[] P_s;
delete[] N;
delete[] precomp;
delete[] min_y;
delete[] max_y;
delete[] min_z;
delete[] max_z;
delete[] P_rot;
}
void aer(vec3& f, vec3& t, const double rho, const vec3& v);
// --- /METHODS ---
};