-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
357 lines (287 loc) · 9.88 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
#include "simulation.h"
#include "ConsistencyTests.h"
#include <igl/viewer/Viewer.h>
#include <igl/copyleft/tetgen/tetrahedralize.h>
#include <igl/writeOBJ.h>
#include <igl/barycenter.h>
#include <igl/readOFF.h>
#include <igl/readOBJ.h>
#include <igl/slice.h>
ofstream momentumFile;
ofstream energyFile;
ofstream strainEnergyFile;
ofstream kineticEnergyFile;
ofstream gravityEnergyFile;
ofstream optimizationFile;
double rayleighCoeff;
double gravity;
string material_model;
bool headless;
double youngs;
double poissons;
string tetgen_code;
string solver;
string objectName;
// Input polygon
Eigen::MatrixXd V;
Eigen::MatrixXi F;
Eigen::MatrixXd B;
// Tetrahedralized interior
Eigen::MatrixXd ForcesTV;
Eigen::MatrixXd FixedTV;
Eigen::MatrixXd TV;
Eigen::MatrixXi TT;
Eigen::MatrixXi TF;
MatrixXi TT_One_G;
MatrixXd TV_One_G;
Simulation Sim;
bool drawLoopTest(igl::viewer::Viewer& viewer){
cout << "In Draw Loop Test" << endl;
viewer.data.clear();
Sim.render();
cout << "Post Render" << endl;
viewer.data.add_points(Sim.integrator->TV, RowVector3d(1,0,0));
viewer.data.add_edges(Sim.integrator->TV.row(0), Sim.integrator->TV.row(1), RowVector3d(1,0,0));
viewer.data.add_edges(Sim.integrator->TV.row(0), Sim.integrator->TV.row(2), RowVector3d(1,0,0));
viewer.data.add_edges(Sim.integrator->TV.row(0), Sim.integrator->TV.row(3), RowVector3d(1,0,0));
viewer.data.add_edges(Sim.integrator->TV.row(1), Sim.integrator->TV.row(2), RowVector3d(1,0,0));
viewer.data.add_edges(Sim.integrator->TV.row(1), Sim.integrator->TV.row(3), RowVector3d(1,0,0));
viewer.data.add_edges(Sim.integrator->TV.row(2), Sim.integrator->TV.row(3), RowVector3d(1,0,0));
viewer.data.add_edges(Sim.integrator->TV.row(4), Sim.integrator->TV.row(3), RowVector3d(0,1,0));
viewer.data.add_edges(Sim.integrator->TV.row(4), Sim.integrator->TV.row(0), RowVector3d(0,0,1));
viewer.data.add_edges(Sim.integrator->TV.row(4), Sim.integrator->TV.row(2), RowVector3d(0,0,0));
// viewer.data.add_edges(Sim.integrator->TV.row(5), Sim.integrator->TV.row(3), RowVector3d(0,1,0));
// viewer.data.add_edges(Sim.integrator->TV.row(5), Sim.integrator->TV.row(0), RowVector3d(0,0,1));
// viewer.data.add_edges(Sim.integrator->TV.row(5), Sim.integrator->TV.row(1), RowVector3d(0,0,0));
// viewer.data.add_edges(Sim.integrator->TV.row(6), Sim.integrator->TV.row(3), RowVector3d(0,1,0));
// viewer.data.add_edges(Sim.integrator->TV.row(6), Sim.integrator->TV.row(0), RowVector3d(0,0,1));
// viewer.data.add_edges(Sim.integrator->TV.row(6), Sim.integrator->TV.row(5), RowVector3d(0,0,0));
viewer.data.add_edges(RowVector3d(0,0,0), RowVector3d(100,0,0), RowVector3d(1,1,1));
viewer.data.add_edges(RowVector3d(0,0,0), RowVector3d(0,100,0), RowVector3d(0,0,0));
//viewer.data.add_edges(RowVector3d(0,0,0), RowVector3d(0,0,100), RowVector3d(1,1,1));
return false;
}
bool drawLoop(igl::viewer::Viewer& viewer){
cout << "In Draw Loop" << endl;
Sim.render();
cout << "Post Render" << endl;
double refinement = 9;
double t = ((refinement - 1)+1) / 9.0;
VectorXd v = B.col(2).array() - B.col(2).minCoeff();
v /= v.col(0).maxCoeff();
vector<int> s;
for (unsigned i=0; i<v.size();++i){
if (v(i) < t){
s.push_back(i);
}
}
MatrixXd V_temp(s.size()*4,3);
MatrixXi F_temp(s.size()*4,3);
for (unsigned i=0; i<s.size();++i)
{
V_temp.row(i*4+0) = Sim.integrator->TV.row(Sim.integrator->TT(s[i],0));
V_temp.row(i*4+1) = Sim.integrator->TV.row(Sim.integrator->TT(s[i],1));
V_temp.row(i*4+2) = Sim.integrator->TV.row(Sim.integrator->TT(s[i],2));
V_temp.row(i*4+3) = Sim.integrator->TV.row(Sim.integrator->TT(s[i],3));
F_temp.row(i*4+0) << (i*4)+0, (i*4)+1, (i*4)+3;
F_temp.row(i*4+1) << (i*4)+0, (i*4)+2, (i*4)+1;
F_temp.row(i*4+2) << (i*4)+3, (i*4)+2, (i*4)+0;
F_temp.row(i*4+3) << (i*4)+1, (i*4)+2, (i*4)+3;
}
viewer.data.clear();
viewer.data.add_edges(RowVector3d(0,0,0), RowVector3d(20,0,0), RowVector3d(1,0,0));
viewer.data.add_edges(RowVector3d(0,0,0), RowVector3d(0,40,0), RowVector3d(0,1,0));
viewer.data.add_edges(RowVector3d(0,0,0), RowVector3d(0,0,60), RowVector3d(0,0,1));
int totalRows = Sim.integrator->TV.rows();
for (int i=0; i<Sim.putForceOnTheseVerts.rows(); i++) {
ForcesTV.row(i) = Sim.integrator->TV.row(Sim.putForceOnTheseVerts(i));
}
//for (int i=0;i<Sim.integrator->fixedVerts.size(); i++) {
// FixedTV.row(i) = Sim.integrator->TV.row(Sim.integrator->fixedVerts[i]);
// //Sim.integrator->TV.row(Sim.integrator->fixedVerts[i]);
// //FixedTV.row(i);
//}
viewer.data.add_points(ForcesTV, RowVector3d(1,0,0));
viewer.data.add_points(FixedTV, RowVector3d(0,1,0));
viewer.data.set_mesh(V_temp,F_temp);
viewer.data.set_face_based(true);
return false;
}
void useFullObject(bool headless, double timestep, int iterations, char method){
// Load a surface mesh
cout << "PRE TET" << endl;
cout << objectName << endl;
igl::readOFF(TUTORIAL_SHARED_PATH "shared/"+objectName+".off", V, F);
cout << "Off Verts" << V.size() << endl;
//igl::readOBJ(TUTORIAL_SHARED_PATH "shared/"+objectName+".obj", V, F);
// Tetrahedralize the interior
igl::copyleft::tetgen::tetrahedralize(V,F, tetgen_code, TV,TT,TF);
cout << "TV Verts" << TV.size() << endl;
cout << "POST TET" << endl;
vector<int> moveVertices;
vector<int> fixedVertices;
// fix vertices
//for(int i=0; i<TV.rows(); i++){
// if(TV.row(i)[1]<=41 && TV.row(i)[1]>30){
// fixedVertices.push_back(i);
// }
//}
//***************************
Sim.initializeSimulation(timestep,iterations, method, TT, TV, B, moveVertices, fixedVertices, youngs, poissons);
ForcesTV.resize(Sim.putForceOnTheseVerts.rows(), 3);
cout<<Sim.putForceOnTheseVerts.rows()<<endl;
FixedTV.resize(Sim.integrator->fixedVerts.size(), 3);
FixedTV.setZero();
// ReSetting Forces
for(int i=0; i<Sim.external_force.rows()/3; i++){
if(abs(Sim.external_force(3*i)+ Sim.external_force(3*i+1)+ Sim.external_force(3*i+2))>0.001)
{ ForcesTV.row(i) = Sim.integrator->TV.row(i);
//cout<<Sim.integrator->TV.row(i)<<endl;
}
}
//cout<<"Forces"<<endl;
//cout<<ForcesTV<<endl;
// this for loop causes segfault
//for(int i=0; i<Sim.integrator->fixedVerts.size(); i++){
// FixedTV.row(i) = Sim.integrator->TV.row(Sim.integrator->fixedVerts[i]);
//}
// cout<<"Fixed"<<endl;
// cout<<FixedTV<<endl;
if(headless){
cout << "Running Headless" << endl;
Sim.headless();
}else{
cout << "RUNNING VIEW" << endl;
igl::viewer::Viewer viewer;
cout << "WHAT" << endl;
viewer.callback_pre_draw = &drawLoop;
cout << "PRE LAUNCH" << endl;
viewer.launch();
cout << "FINISHED LAUNCHING" << endl;
}
}
void useMyObject(bool headless, double timestep, int iterations, char method){
// TT_One_G.resize(1, 4);
// TT_One_G<< 1, 2, 3, 0;
// TV_One_G.resize(4, 3);
// TV_One_G << 0, 0, 10, //affect
// 10, 0, 0,
// 0, 10, 0,
// 0, 0, 0;
TT_One_G.resize(2, 4);
TT_One_G<< 0, 1, 2, 3,
4, 0, 2, 3;
TV_One_G.resize(5, 3);
TV_One_G << 10, 10, 0, //affect
0, 20, 0,
0, 10, 10,
0, 10, 0,
0, 0, 0;
// TV_One_G << 0, 0, 0, //affect
// 1, 10, 0,
// 2, 0, 10,
// 3, 0, 0,
// 4, -10, 0;
vector<int> moveVertices;
vector<int> fixedVertices;
// move vertices
//moveVertices.push_back(0);
// fix vertices
// fixedVertices.push_back(1);
Sim.initializeSimulation(timestep, iterations, method, TT_One_G, TV_One_G, B, moveVertices, fixedVertices, youngs, poissons);
if(headless){
Sim.headless();
}else{
igl::viewer::Viewer viewer;
viewer.core.is_animating = true;
viewer.callback_pre_draw = &drawLoopTest;
viewer.launch();
}
}
void consistencyTests( double timestep, int iterations, char method){
ConsistencyTest c;
c.replaceWithMain();
}
int main(int argc, char *argv[])
{
double timestep = 0;
char method;
int object = 0;
char runHeadless;
int iterations;
string line;
ifstream configFile (HOME_SAVED_PATH "config.txt");
if(configFile.is_open()){
getline(configFile, line);
timestep = stod(line.c_str());
cout<<timestep<<endl;
getline(configFile, line);
runHeadless = line.c_str()[0];
cout<<line<<endl;
getline(configFile, line);
method = line.c_str()[0];
cout<<method<<endl;
getline(configFile, line);
iterations = atoi(line.c_str());
cout<<iterations<<endl;
getline(configFile, line);
rayleighCoeff = stod(line.c_str());
cout<<rayleighCoeff<<endl;
getline(configFile, line);
gravity = stod(line.c_str());
cout<<gravity<<endl;
getline(configFile, line);
object = atoi(line.c_str());
cout<<object<<endl;
getline(configFile, line);
youngs = stod(line.c_str());
cout<<youngs<<endl;
getline(configFile, line);
poissons = stod(line.c_str());
cout<<poissons<<endl;
getline(configFile, line);
tetgen_code = line.c_str();
cout<<tetgen_code<<endl;
getline(configFile, line);
material_model = line.c_str();
cout<<material_model<<endl;
getline(configFile, line);
solver = line.c_str();
cout<<solver<<endl;
getline(configFile, line);
objectName = line.c_str();
cout<<objectName<<endl;
}else{
cout<<"Check yo self: Config file not found"<<endl;
return 0;
}
///////////////////
cout<<"###########################My Code ###################"<<endl;
headless = false;
if(runHeadless=='t'){
headless = true;
}
momentumFile.open("../Scripts/momentum.txt");
energyFile.open("../Scripts/energy.txt");
strainEnergyFile.open("../Scripts/senergy.txt");
kineticEnergyFile.open("../Scripts/kenergy.txt");
gravityEnergyFile.open("../Scripts/genergy.txt");
optimizationFile.open("../TestsResults/opt.txt");
if(object ==0){
useMyObject(headless, timestep, iterations, method);
}else if(object ==1){
useFullObject(headless, timestep, iterations, method);
}else if(object == 2){
consistencyTests(timestep, iterations, method);
}else{
cout<<"What do you want to run?"<<endl;
}
energyFile.close();
strainEnergyFile.close();
kineticEnergyFile.close();
gravityEnergyFile.close();
momentumFile.close();
optimizationFile.close();
cout<<"###########################My Code ###################"<<endl;
return 0;
}