-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
401 lines (359 loc) · 11.6 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
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
#include <GLFW/glfw3.h>
#include "draw_delegate.h"
#include "particle_system.h"
#include "scene.h"
#include <imgui.h>
#include "imgui_impl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <vector>
#include <string>
namespace {
std::vector<std::string> modelList;
bool mloaded = false;
void LoadModelList() {
modelList.clear();
char buffer[1000];
DIR *dir;
struct dirent *ent;
if ((dir = opendir(".")) != NULL) {
//print all the files and directories within directory
while ((ent = readdir(dir)) != NULL) {
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..") ) continue;
if (ent->d_type != DT_REG) continue;
int len = strlen(ent->d_name);
if (strncmp(ent->d_name + len - 4, ".ply", 5) != 0) {
//printf("Found %s not ply file\n", ent->d_name);
continue;
}
modelList.push_back(ent->d_name);
}
closedir(dir);
mloaded = true;
return;
}
}
void error_callback(int error, const char* description) {
fprintf(stderr, "%s\n", description);
}
bool drawSimulation = true;
bool solveWithguess = true;
bool corotational = true;
bool fByF = false; //frame by frame mode
int typeOfGround = 5;
Scene* scene_p;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (action == GLFW_PRESS) {
switch(key) {
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GL_TRUE);
break;
case 'A':
scene_p->ToggleLimitFps();
break;
case 'S':
if (drawSimulation) drawSimulation = false;
else drawSimulation = true;
printf("Implicit: %d\n", (int) drawSimulation);
break;
case 'D':
if (solveWithguess) solveWithguess = false;
else solveWithguess = true;
printf("Solvewith guess: %d\n", (int) solveWithguess);
break;
case 'I':
scene_p->walkForward = true;
break;
case 'K':
scene_p->walkBack = true;
break;
case 'J':
scene_p->walkLeft = true;
break;
case 'L':
scene_p->walkRight = true;
break;
case 'U':
scene_p->walkUp = true;
break;
case 'O':
scene_p->walkDown = true;
break;
case 'Z':
scene_p->drawMode = (scene_p->drawMode + 1)%5;
break;
case 'X':
scene_p->slowMode = scene_p->slowMode ? false : true;
break;
case 'C':
corotational = corotational ? false : true;
break;
case 'G':
typeOfGround++;
printf("Type of ground : %i\n", typeOfGround);
break;
case 'F':
fByF = !fByF;
break;
case 'T':
scene_p->prevMode = !scene_p->prevMode;
break;
}
} else if (action == GLFW_RELEASE) {
switch(key) {
case 'I':
scene_p->walkForward = false;
break;
case 'K':
scene_p->walkBack = false;
break;
case 'J':
scene_p->walkLeft = false;
break;
case 'L':
scene_p->walkRight = false;
break;
case 'U':
scene_p->walkUp = false;
break;
case 'O':
scene_p->walkDown = false;
break;
}
}
}
}; // namespace
int main(int argc, const char **argv) {
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(DDWIDTH, DDHEIGHT, "Springs", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(0);
DrawDelegate::SetupOpenGL();
ImGui_ImplGlfw_Init(window, true);
const char*meshFilename = "Armadillo_simple2.ply";
// Particle system setup
ParticleSystem m;
double strainSize = 2;
if (argc >= 4) {
m.SetSpringProperties(atof(argv[1]), .4, atof(argv[2]), 9.8f, atof(argv[3]), 1000, false);
if (argc >= 5) {
strainSize = atof(argv[4]);
}
if (argc >= 6) {
meshFilename = argv[5];
}
}
//m.SetupArmadillo();
//m.SetupSingleSpring();
m.SetupBendingBar();
//m.SetupTriangle();
//m.SetupMouseSpring(5);
Scene scene;
scene.InitTime();
scene_p = &scene;
double curTime;
double simulatetime = 0;
double drawtime = 0;
int frames = 0;
glfwSetKeyCallback(window, key_callback);
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
ImGui_ImplGlfw_NewFrame();
// Update m
double timestep = scene.GetTimestep();
double tempTime = 0;
double realTimeStep = timestep;
if (timestep < .5 && !fByF || fByF && frames % 5 == 0 &&glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
if (fByF) timestep = 1.0/60.0;
double mouse_x, mouse_y;
glfwGetCursorPos(window, &mouse_x, &mouse_y);
bool mousePressed = glfwGetMouseButton(window, 0);
static bool pressedLastFrame = false;
if (mousePressed) {
Eigen::Vector3d origin, ray;
scene.GetCameraRay(mouse_x, mouse_y, &origin, &ray);
if (pressedLastFrame) {
m.onMouseDrag(origin, ray, timestep);
} else {
m.onMousePress(origin, ray);
m.onMouseDrag(origin, ray, timestep);
}
pressedLastFrame = true;
} else {
pressedLastFrame = false;
}
curTime = glfwGetTime();
m.Update(timestep, solveWithguess, corotational, typeOfGround);
tempTime = glfwGetTime();
simulatetime += tempTime - curTime;
curTime = tempTime;
}
// Update scene pos
scene.Update(realTimeStep);
// Draw
scene.DrawScene(&m, strainSize, drawSimulation);
{
ImGui::Text("Change configuration");
static int selected_config = 2;
const char* names[] = { "Single Spring", "Armadillo", "Bending Bar", "Load Mesh File" };
int nameLength = 4;
const char* groundTypes[] = {
"No Ground",
"Only Penalty",
"Snap to floor and penalty",
"Snap to prev intersection and penalty",
"Snap to prev intersection and penalty plus friction penalty",
"Snap to floor and infinite friction",
"Snap to floor and implicit penalty"
};
int groundTypeLength = 7;
if (ImGui::Button("Select Type.."))
ImGui::OpenPopup("select");
ImGui::SameLine();
ImGui::Text(selected_config == -1 ? "<None>" : names[selected_config]);
if (ImGui::BeginPopup("select"))
{
for (int i = 0; i < 4; i++)
if (ImGui::Selectable(names[i]))
selected_config = i;
ImGui::EndPopup();
}
if (selected_config == 3) {
if (ImGui::Button("Select File.."))
ImGui::OpenPopup("select_file");
ImGui::SameLine();
ImGui::Text((meshFilename != NULL && meshFilename[0] != 0) ? meshFilename : "<None>");
if (ImGui::BeginPopup("select_file")) {
if (!mloaded) LoadModelList();
for (int i = 0; i < modelList.size(); i++) {
if (ImGui::Selectable(modelList[i].c_str())) {
meshFilename = modelList[i].c_str();
}
}
ImGui::Separator();
if (ImGui::Selectable("Reload List")) {
LoadModelList();
meshFilename = NULL;
}
ImGui::EndPopup();
}
}
ImGui::Separator();
if (ImGui::Button("Select Ground Type.."))
ImGui::OpenPopup("select_ground");
ImGui::SameLine();
ImGui::Text(typeOfGround < 0 && typeOfGround >= groundTypeLength ? "Undefined ground" : groundTypes[typeOfGround]);
if (ImGui::BeginPopup("select_ground"))
{
for (int i = 0; i < groundTypeLength; i++)
if (ImGui::Selectable(groundTypes[i]))
typeOfGround = i;
ImGui::EndPopup();
}
ImGui::Separator();
const char* drawModeTypes[] = {
"Normal",
"Lines between tets",
"Accumulated stress on surface points",
"Tet center with strain",
"All tets with strain"
};
int drawModeLength = 5;
if (ImGui::Button("Select Draw Mode.."))
ImGui::OpenPopup("select_drawMode");
ImGui::SameLine();
ImGui::Text(scene_p->drawMode < 0 && scene_p->drawMode >= drawModeLength ? "Undefined draw mode" : drawModeTypes[scene_p->drawMode]);
if (ImGui::BeginPopup("select_drawMode"))
{
for (int i = 0; i < drawModeLength; i++)
if (ImGui::Selectable(drawModeTypes[i]))
scene_p->drawMode = i;
ImGui::EndPopup();
}
ImGui::Separator();
static float stiffness = 1000.0f;
static float volumeConservation = 0.4f;
static float damping = 0.5f;
static float gravity = 9.6f;
static float strainDisplaySize = strainSize;
static float groundStiffness = 1000.0f;
static float mouseStiffness = 10000.0f;
ImGui::Text("Stiffness (Young's modulus)");
ImGui::SliderFloat("##stiffness", &stiffness, 0.0f, 10000.0f, "%.3f", 2.0);
ImGui::Text("Volume Conservation (Poisson's ratio)");
ImGui::SliderFloat("##vol", &volumeConservation, 0.0f, .49999f);
ImGui::Text("Damping (Not supported with FEM yet)");
ImGui::SliderFloat("##damp", &damping, 0.0f, 100.0f, "%.3f", 2.0);
ImGui::Text("Gravity m/s^2");
ImGui::SliderFloat("##grav", &gravity, 0.0f, 50.0f);
ImGui::Text("Strain display ratio");
ImGui::SliderFloat("##strainSize", &strainDisplaySize, 0.0f, 100.0f, "%.3f", 4.0);
ImGui::Text("Ground stiffness (size of penalty forces)");
ImGui::SliderFloat("##groundstiffness", &groundStiffness, 0.0f, 10000.0f);
ImGui::Text("Mouse spring stiffness");
ImGui::SliderFloat("##mousestiffness", &mouseStiffness, 0.0f, 100000.0f);
static bool useRollback = false;
ImGui::Checkbox("Use rollback col system?", &useRollback);
if (ImGui::Button("Apply Changes")) {
m.SetSpringProperties(stiffness, volumeConservation, damping, gravity, groundStiffness, mouseStiffness, useRollback);
strainSize = strainDisplaySize;
switch (selected_config) {
case 0:
m.SetupSingleSpring();
break;
case 1:
m.SetupArmadillo();
break;
case 2:
m.SetupBendingBar();
break;
case 3:
if (meshFilename != NULL && meshFilename[0] != 0)
m.SetupMeshFile(meshFilename);
break;
}
}
}
if (scene_p->fpsVec.size() != 0) {
int windowSize = 20;
int offset = 0;
if (scene_p->fpsVec.size() > 40) {
offset = scene_p->fpsVec.size() - 40;
}
ImGui::PlotLines("Frames Per Second##Graph", scene_p->fpsVec.data() + offset, scene_p->fpsVec.size() - offset, 0, NULL, 0.0, 200.0f, ImVec2(0,150));
char buffer[1000];
snprintf(buffer, 1000, "Current FPS: %.3f", scene_p->fpsVec[scene_p->fpsVec.size() - 1]);
ImGui::Text(buffer);
ImGui::Checkbox("Limit to 60 FPS?", &(scene_p->limitFps));
}
ImGui::Render();
tempTime = glfwGetTime();
drawtime += tempTime - curTime;
curTime = tempTime;
glfwSwapBuffers(window);
frames++;
scene.EndOfFrame();
}
printf("Simulate time %f\n", simulatetime/frames);
printf("Draw time %f\n", drawtime/frames);
printf("Frames %d\n", frames);
double triplet, fromtriplet, solve, setup;
m.GetProfileInfo(triplet, fromtriplet, solve, setup);
printf("Triplet %f, from %f, solve %f, setup %f\n", triplet/frames, fromtriplet/frames, solve/frames, setup/frames);
printf("Total %f\n", triplet + fromtriplet + solve + setup);
ImGui_ImplGlfw_Shutdown();
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}