-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
475 lines (393 loc) · 14.9 KB
/
Copy pathmain.cpp
File metadata and controls
475 lines (393 loc) · 14.9 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
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <random>
#include <time.h>
#include <utility>
#include <fstream>
#include <chrono>
#include <iomanip>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "Vector3d.hpp"
#define N_POINTS 11
#define POWER_OF_THREE 177147UL//59049UL //
std::ofstream file;
struct Trajectory {
std::vector<Vector3d> points;
Trajectory() {
points = std::vector<Vector3d>();
}
};
struct Train {
double speed; // en m/s
std::vector<double> times;
std::vector<std::vector<Vector3d>> positionDerivatives;
Train() : speed(1) {}
void calculateMovement(const Trajectory &trajectory) {
positionDerivatives.clear();
positionDerivatives.push_back(trajectory.points);
size_t N = trajectory.points.size();
times.clear();
times.push_back(0.0);
std::vector<double> doubleTimes;
doubleTimes.push_back(0.0);
double travelDistance = 0;
for (unsigned int j = 0; j < N - 1; j++) {
Vector3d dOM = positionDerivatives[0][j + 1] - positionDerivatives[0][j];
travelDistance += dOM.norm();
doubleTimes.push_back((travelDistance - dOM.norm()/2) / speed);
doubleTimes.push_back(travelDistance / speed);
times.push_back(travelDistance / speed);
}
for (unsigned j = 1; j < 4; j++) {
positionDerivatives.push_back(std::vector<Vector3d>());
for (unsigned i = 0; i < N - j; i++) {
positionDerivatives[j].push_back((positionDerivatives[j - 1][i + 1] - positionDerivatives[j - 1][i]) / (doubleTimes[j - 1 + 2 * (i + 1)] - doubleTimes[j - 1 + 2 * i]));
}
}
}
std::vector<std::vector<Vector3d>> getPositionDerivatives() {
return positionDerivatives;
}
};
void printPoints(const std::vector<Vector3d> &points) {
std::vector<double> X, Y;
for (Vector3d Point:points) {
std::cout << "(" << Point.x << ", " << Point.y << ")" << std::endl;
X.push_back(Point.x);
Y.push_back(Point.y);
}
for (auto x : X) {
std::cout << /*std::setprecision(4) <<*/ x << " ";
}
std::cout << "\n";
for (auto y : Y) {
std::cout << y << " ";
}
std::cout.flush();
}
void writeToFile(double x) {
file << x << ", ";
}
void printForPython(const std::vector<Vector3d> &points) {
std::vector<double> X, Y;
for (Vector3d Point:points) {
X.push_back(Point.x);
Y.push_back(Point.y);
}
std::cout << "[";
for (double x : X) {
std::cout << x << ",";
file << x << ",";
}
std::cout << "]\n[";
file << "\n";
for (double y : Y) {
std::cout << y << ",";
file << y << ",";
}
std::cout << "]\n" << std::endl;
}
/////
// Utilisation du code de Gray
// This function converts an unsigned binary number to reflected binary Gray code.
void toTernaryGray(unsigned value, unsigned gray[N_POINTS])
{
unsigned baseN[N_POINTS]; // Stores the ordinary base-N number, one digit per entry
unsigned i; // The loop variable
// Put the normal baseN number into the baseN array. For base 10, 109
// would be stored as [9,0,1]
for (i = 0; i < N_POINTS; i++) {
baseN[i] = value % 3;
value = value / 3;
}
// Convert the normal baseN number into the Gray code equivalent. Note that
// the loop starts at the most significant digit and goes down.
unsigned shift = 0;
while (i--) {
// The Gray digit gets shifted down by the sum of the higher
// digits.
gray[i] = (baseN[i] + shift) % 3;
shift = shift + 3 - gray[i]; // Subtract from base so shift is positive
}
}
std::pair<size_t, int> ternaryGrayDifference(unsigned n)
{
unsigned gray1[N_POINTS];
unsigned gray2[N_POINTS];
toTernaryGray(n, gray1);
toTernaryGray(n + 1, gray2);
unsigned change = -1; // Donne une valeur invalide
for (int i = 0; i < N_POINTS; i++) {
if (gray1[i] != gray2[i]) {
change = i;
}
}
int dir = ((int) gray2[change]) - ((int) gray1[change]);
return std::make_pair(change, dir);
}
// On utilise une heuristique en choisissant un chemin proche
void getNextTrajectory(Trajectory &traj, uint n, double step) {
auto ch = ternaryGrayDifference(n);
Vector3d dir = (traj.points[traj.points.size() - 3] - traj.points[2]);
Vector3d normal = {dir.y, -dir.x, 0.0};
normal = normal / normal.norm();
traj.points[3 + ch.first] = traj.points[3 + ch.first] + normal * step * ch.second;
}
double valueFunction(std::vector<std::vector<Vector3d>> positionDerivatives, const Train& train) {
auto& times = train.times;
std::vector<double> curvatureDer;
for (unsigned int i = 0; i < positionDerivatives[2].size() - 1; i++) {
curvatureDer.push_back((positionDerivatives[2][i + 1].norm() - positionDerivatives[2][i].norm())/(times[i + 2] - times[i + 1]));
}
double maxCurDer = 0;
for (unsigned int i = 0; i < curvatureDer.size(); i++) {
double sn = curvatureDer[i] * ((times[i + 2] - times[i + 1]));
if (abs(sn) > maxCurDer) {
maxCurDer = abs(sn);
}
}
//double maxAcc = 0;
double sumAcc = 0;
for (unsigned int i = 0; i < positionDerivatives[2].size(); i++) {
double sn = positionDerivatives[2][i].norm() * ((times[i + 2] - times[i]) / 2);
sumAcc += sn;
//if (sn > maxAcc) {
// maxAcc = sn;
//}
}
double sumJerk = 0;
double maxJerk = 0;
for (unsigned int i = 0; i < positionDerivatives[3].size(); i++) {
double sn = positionDerivatives[3][i].norm() * (times[i + 2] - times[i + 1]);
sumJerk += sn;
if (sn > maxJerk) {
maxJerk = sn;
}
}
return /*3 * maxAcc + sumAcc + 2 * maxJerk + sumJerk*/ sumJerk + sumAcc + maxJerk + 10*maxCurDer;
}
Trajectory getStraightTrajectory(Vector3d endpoints[6], int N) {
Trajectory traj;
traj.points.push_back(endpoints[0]);
traj.points.push_back(endpoints[1]);
traj.points.push_back(endpoints[2]);
Vector3d dir = (endpoints[3] - endpoints[2]) / (N + 1);
for (int i = 1; i < N + 1; i++) {
traj.points.push_back(endpoints[2] + (dir * i));
}
traj.points.push_back(endpoints[3]);
traj.points.push_back(endpoints[4]);
traj.points.push_back(endpoints[5]);
//std::cout << traj.points.size();
return traj;
}
// Decale la trajectoire de depart d'une distance step sur le côté pour pouvoir utiliser le codage ternaire de gray
void slide(Trajectory &traj, double step) {
Vector3d dir = (traj.points[traj.points.size() - 3] - traj.points[2]);
Vector3d normal = {dir.y, -dir.x, 0.0};
normal = normal / normal.norm();
for (unsigned int i = 3; i < traj.points.size() - 3; i++) {
traj.points[i] = traj.points[i] + normal * (-1) * step;
}
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window) {
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
const char* vertexShaderSource = "#version 460 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()"
"{"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);"
"}";
const char* fragmentShaderSource = "#version 460 core\n"
"out vec4 FragColor;\n"
"void main()"
"{"
"FragColor = vec4(0.9f, 0.0f, 0.0f, 1.0f);"
"}";
unsigned int genShaderProgram() {
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
int success;
char infoLog[512];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cout << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
}
void draw(unsigned int& VAO, unsigned int& VBO, unsigned int& shaderProgram, unsigned int n, unsigned int m) {
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_LINE_STRIP, 0, n);
//glDrawArrays(GL_LINES, n, m);
}
void setup(unsigned int& VAO, unsigned int& VBO, unsigned int& shaderProgram) {
shaderProgram = genShaderProgram();
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
}
void writeTrajectoryBuffer(unsigned int& VAO, unsigned int& VBO, Trajectory& optimalTraj, std::vector<Vector3d> jerkVectors) {
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
auto n = optimalTraj.points.size();
float traj[3 * (n + jerkVectors.size())];
for (unsigned int i = 0; i < optimalTraj.points.size(); i++) {
traj[3*i] = optimalTraj.points[i].x;
traj[3*i + 1] = optimalTraj.points[i].y;
traj[3*i + 2] = optimalTraj.points[i].z;
}
for (unsigned int i = 0; i < jerkVectors.size(); i++) {
traj[3 * n + 3*i] = jerkVectors[i].x;
traj[3 * n + 3*i + 1] = jerkVectors[i].y;
traj[3 * n + 3*i + 2] = jerkVectors[i].z;
}
glBufferData(GL_ARRAY_BUFFER, sizeof(traj), traj, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
}
int main() {
srand(time(NULL));
file.open("data.csv");
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(1000, 1000, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, 1000, 1000);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
unsigned int VAO, VBO, shaderProgram;
setup(VAO, VBO, shaderProgram);
////////// Train logic
Trajectory traj;
Trajectory optimalTraj;
double optimalValue = -INFINITY;
double s = 0.9;
Vector3d endpoints[6] = {{-1.0*s, -1.05*s, 0.0*s}, {-1.0*s, -1.0*s, 0.0*s}, {-1.0*s, -0.95*s, 0.0*s}, {1.0*s, 0.95*s, 0.0*s}, {1.0*s, 1.0*s, 0.0*s}, {1.0*s, 1.05*s, 0.0*s}};
traj = getStraightTrajectory(endpoints, N_POINTS);
Train train;
double step = 0.1;
train.calculateMovement(traj);
optimalValue = valueFunction(train.getPositionDerivatives(), train);
optimalTraj = traj;
Trajectory tempTraj;
double tempValue = 0;
std::vector<std::vector<Vector3d>> positionDerivatives;
double tolerance = 0.00000001;
bool valueImproved = true;
///////////
auto start = std::chrono::steady_clock::now();
std::vector<Vector3d> jerkVectors;
while (!glfwWindowShouldClose(window))
{
processInput(window);
glClear(GL_COLOR_BUFFER_BIT);
auto now = std::chrono::steady_clock::now();
std::chrono::duration<double> delay = now - start;
if (step > tolerance) {
if (valueImproved) {
if (delay.count() > 0) {
tempTraj = optimalTraj;
slide(tempTraj, step);
valueImproved = false;
double values[POWER_OF_THREE];
for (unsigned int i = 0; i < POWER_OF_THREE; i++) {
//std::cout << tempTraj.points[tempTraj.points.size() - 3] << std::endl;
getNextTrajectory(tempTraj, i, step);
train.calculateMovement(tempTraj);
tempValue = valueFunction(train.getPositionDerivatives(), train);
values[i] = tempValue;
// std::cout << -tempValue << ", " << step << std::endl;
if (tempValue < optimalValue - 0.000000001 /*&& (rand()%20) == 0*/) {
optimalTraj = tempTraj;
optimalValue = tempValue;
valueImproved = true;
std::cout << "Better value: " << optimalValue << std::endl;
std::cout << "Step: " << step << std::endl;
}
}
writeToFile(optimalValue);
start = std::chrono::steady_clock::now();
}
}
else {
std::cout << "Best value at this step: " << optimalValue << std::endl;
std::cout << "Step: " << step << std::endl;
valueImproved = true;
step /= 2;
}
jerkVectors.clear();
train.calculateMovement(optimalTraj);
auto pos = train.getPositionDerivatives();
double max = 0;
for (unsigned int i = 0; i < pos[2].size(); i++) {
double norm = pos[2][i].norm();
if (norm > max) {
max = norm;
}
}
for (int i = 0; i < pos[2].size(); i++) {
auto mid = pos[0][i + 1];// + (pos[0][i + 2] - pos[0][i + 1]) / 2;
jerkVectors.push_back(mid);
jerkVectors.push_back(mid + pos[2][i] / (5*max));
}
writeTrajectoryBuffer(VAO, VBO, optimalTraj, jerkVectors);
}
draw(VAO, VBO, shaderProgram, optimalTraj.points.size(), jerkVectors.size());
////////////////
glfwSwapBuffers(window);
glfwPollEvents();
}
std::cout << "The best value: " << optimalValue << std::endl;
train.calculateMovement(optimalTraj);
printPoints(train.getPositionDerivatives()[0]);
std::cout << std::endl;
std::cout << valueFunction(train.getPositionDerivatives(), train) << std::endl;
file.close();
glfwTerminate();
return 0;
}