/
main.cpp
150 lines (123 loc) · 3.52 KB
/
main.cpp
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
/**
* simDER
* simDER stands for "[sim]plified [D]iscrete [E]lastic [R]ods"
* Dec 2017
* This code is based on previous iterations.
* */
// This line is for mac
//#include <GLUT/glut.h>
// This is for linux
#include <GL/glut.h>
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
#include "eigenIncludes.h"
// Rod and stepper are included in the world
#include "world.h"
#include "setInput.h"
#include "cppflow/cppflow.h"
world myWorld;
int NPTS;
ofstream outfile;
static void Key(unsigned char key, int x, int y)
{
switch (key) // ESCAPE to quit
{
case 27:
exit(0);
}
}
/* Initialize OpenGL Graphics */
void initGL()
{
glClearColor(0.7f, 0.7f, 0.7f, 0.0f); // Set background color to black and opaque
glClearDepth(2.0f); // Set background depth to farthest
// glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
// glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections
glLoadIdentity();
gluLookAt(0.1, 0.1, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 10);
// void gluLookAt( GLdouble eyeX, GLdouble eyeY, GLdouble eyeZ,
// GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX,
// GLdouble upY, GLdouble upZ);
glPushMatrix();
// glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
while (myWorld.simulationRunning() > 0)
{
// Clear screen and Z-buffer
glClear(GL_COLOR_BUFFER_BIT);
// draw axis
double axisLen = 2.0;
glLineWidth(0.5);
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(axisLen, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, axisLen, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, axisLen);
glEnd();
// draw a line
glColor3f(0.1, 0.1, 0.1);
glLineWidth(3.0);
glBegin(GL_LINES);
for (int j = 0; j < NPTS - 1; j++)
{
glVertex3f(myWorld.getScaledCoordinate(4 * j), myWorld.getScaledCoordinate(4 * j + 1), myWorld.getScaledCoordinate(4 * j + 2));
glVertex3f(myWorld.getScaledCoordinate(4 * (j + 1)), myWorld.getScaledCoordinate(4 * (j + 1) + 1), myWorld.getScaledCoordinate(4 * (j + 1) + 2));
}
glEnd();
glFlush();
// Update step
myWorld.updateTimeStep();
myWorld.CoutData(outfile);
}
exit(1);
}
int main(int argc, char *argv[])
{
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
setInput inputData;
inputData = setInput();
inputData.LoadOptions(argv[1]);
inputData.LoadOptions(argc, argv);
// read input parameters from txt file and cmd
myWorld = world(inputData);
myWorld.setRodStepper();
myWorld.OpenFile(outfile);
bool render = myWorld.isRender();
if (render) // if OpenGL visualization is on
{
NPTS = myWorld.numPoints();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(150, 100);
glutCreateWindow("simDER");
initGL();
glutKeyboardFunc(Key);
glutDisplayFunc(display);
glutMainLoop();
}
else
{
while (myWorld.simulationRunning() > 0)
{
myWorld.updateTimeStep(); // update time step
myWorld.CoutData(outfile); // write data to file
}
}
// Close (if necessary) the data file
myWorld.CloseFile(outfile);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;
return 0;
}