-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.cpp
235 lines (221 loc) · 7.74 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
/*
* Copyright (c) 2019 Nobuyuki Umetani
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <random>
#if defined(_WIN32) // windows
# define NOMINMAX // to remove min,max macro
# include <windows.h> // this should come before glfw3.h
#endif
#define GL_SILENCE_DEPRECATION
#include <GLFW/glfw3.h>
#include "delfem2/ls_solver_block_sparse.h"
#include "delfem2/ls_pentadiagonal.h"
#include "delfem2/msh_topology_uniform.h"
#include "delfem2/vec2.h"
#include "delfem2/femutil.h"
#include "delfem2/fem_rod2.h"
#include "delfem2/glfw/util.h"
#include "delfem2/glfw/viewer2.h"
#include "delfem2/opengl/old/mshuni.h"
namespace dfm2 = delfem2;
// ------------------------------------------------------
void DrawPolyline(const std::vector<double> &vtx_xy) {
::glBegin(GL_LINE_STRIP);
for (unsigned int ixy = 0; ixy < vtx_xy.size() / 2; ++ixy) {
::glVertex2dv(vtx_xy.data() + ixy * 2);
}
::glEnd();
}
void Draw(
std::vector<double> &vtx_xy_deformed,
const std::vector<double> &vtx_xy_initial){
::glLineWidth(1);
::glColor3d(0, 0, 0);
DrawPolyline(vtx_xy_initial);
::glPointSize(5);
delfem2::opengl::DrawPoints2d_Points(vtx_xy_initial);
//
::glColor3d(1, 0, 0);
DrawPolyline(vtx_xy_deformed);
delfem2::opengl::DrawPoints2d_Points(vtx_xy_deformed);
}
template<class LIN_SYS_SOLVER>
void StepTime(
std::vector<double> &vtx_xy_deformd,
std::vector<double> &vtx_uv,
const std::vector<double> &vtx_xy_initial,
double dt,
double stiff_stretch,
double stiff_bend,
double mass_point,
const double gravity[2],
LIN_SYS_SOLVER &sparse) {
auto np = static_cast<unsigned int>(vtx_xy_initial.size() / 2);
assert(np >= 3);
assert(sparse.nblk() == np && sparse.ndim() == 2);
for (unsigned int ip = 0; ip < np; ++ip) {
vtx_xy_deformd[ip * 2 + 0] += dt * vtx_uv[ip * 2 + 0];
vtx_xy_deformd[ip * 2 + 1] += dt * vtx_uv[ip * 2 + 1];
}
sparse.BeginMerge();
double W = 0.0;
for (unsigned int ihinge = 0; ihinge < np - 2; ++ihinge) {
const unsigned int aIP[3] = {ihinge, ihinge + 1, ihinge + 2};
double aP[3][2];
dfm2::FetchData<3, 2>(aP, aIP, vtx_xy_initial.data());
double ap[3][2];
dfm2::FetchData<3, 2>(ap, aIP, vtx_xy_deformd.data());
const double aL[2] = {
dfm2::Distance2(aP[0], aP[1]),
dfm2::Distance2(aP[1], aP[2])};
double We, dWe[3][2], ddWe[3][3][2][2];
dfm2::WdWddW_Rod2(
We, dWe, ddWe,
ap, aL, stiff_stretch, stiff_stretch, stiff_bend);
W += We;
for (int ino = 0; ino < 3; ++ino) {
sparse.vec_r[aIP[ino] * 2 + 0] -= dWe[ino][0];
sparse.vec_r[aIP[ino] * 2 + 1] -= dWe[ino][1];
}
sparse.template Merge<3, 3, 2, 2>(aIP, aIP, ddWe);
}
for (unsigned int ip = 0; ip < np; ++ip) {
sparse.AddValueToDiagonal(ip, 0, mass_point / (dt * dt));
sparse.AddValueToDiagonal(ip, 1, mass_point / (dt * dt));
}
for (unsigned int ip = 0; ip < np; ++ip) {
sparse.vec_r[ip * 2 + 0] += mass_point * gravity[0];
sparse.vec_r[ip * 2 + 1] += mass_point * gravity[1];
}
std::cout << W << std::endl;
sparse.Solve();
for (unsigned int ip = 0; ip < np; ++ip) {
vtx_xy_deformd[ip * 2 + 0] += sparse.vec_x[ip * 2 + 0];
vtx_xy_deformd[ip * 2 + 1] += sparse.vec_x[ip * 2 + 1];
vtx_uv[ip * 2 + 0] += sparse.vec_x[ip * 2 + 0] / dt;
vtx_uv[ip * 2 + 1] += sparse.vec_x[ip * 2 + 1] / dt;
}
}
int main() {
const double dt = 1.0 / 60.0;
const double stiff_stretch = 10.0;
const double stiff_bend = 0.001;
const double mass_point = 1.0e-5;
const double gravity[2] = {0, -10};
std::vector<double> vtx_xy_deformed; // vertices of polyline in the initial config
{
const unsigned int num_points = 11;
for (unsigned int i = 0; i < num_points; ++i) {
vtx_xy_deformed.push_back(double(i) / num_points);
vtx_xy_deformed.push_back(0.0);
}
}
const std::vector<double> vtx_xy_initial = vtx_xy_deformed;
std::mt19937 rndeng(std::random_device{}());
std::uniform_real_distribution<double> distm1p1(-1, +1);
// opengl set up
dfm2::glfw::InitGLOld();
dfm2::glfw::CViewer2 viewer;
{
viewer.view_height = 0.7f;
viewer.trans[0] = -0.2f;
viewer.trans[1] = +0.3f;
}
viewer.OpenWindow(); // opengl start here
while (true) {
{
glfwSetWindowTitle(viewer.window, "penta-diagonal solver free rotation");
delfem2::LinearSystemSolver_BlockPentaDiagonal<2> ls_solver;
ls_solver.Initialize(vtx_xy_deformed.size() / 2);
std::cout << "initial" << std::endl;
ls_solver.dof_bcflag[0] = 1;
ls_solver.dof_bcflag[1] = 1;
vtx_xy_deformed = vtx_xy_initial;
for (unsigned int i = 0; i < vtx_xy_deformed.size(); ++i) {
if (ls_solver.dof_bcflag[i] != 0) { continue; }
vtx_xy_deformed[i] += 0.1*distm1p1(rndeng);
}
std::vector<double> vtx_uv(vtx_xy_deformed.size(), 0.);
for(unsigned int iframe=0;iframe<100;++iframe){
StepTime(
vtx_xy_deformed, vtx_uv,
vtx_xy_initial,
dt, stiff_stretch, stiff_bend, mass_point, gravity,
ls_solver);
viewer.DrawBegin_oldGL();
Draw(vtx_xy_deformed, vtx_xy_initial);
viewer.SwapBuffers();
glfwPollEvents();
if (glfwWindowShouldClose(viewer.window)) { break; }
}
if (glfwWindowShouldClose(viewer.window)) { break; }
}
{
glfwSetWindowTitle(viewer.window, "penta-diagonal solver fix rotation");
delfem2::LinearSystemSolver_BlockPentaDiagonal<2> ls_solver;
ls_solver.Initialize(vtx_xy_deformed.size() / 2);
ls_solver.dof_bcflag[0] = 1;
ls_solver.dof_bcflag[1] = 1;
ls_solver.dof_bcflag[2] = 1;
ls_solver.dof_bcflag[3] = 1;
vtx_xy_deformed = vtx_xy_initial;
for (unsigned int i = 0; i < vtx_xy_deformed.size(); ++i) {
if (ls_solver.dof_bcflag[i] != 0) { continue; }
vtx_xy_deformed[i] += 0.1*distm1p1(rndeng);
}
std::vector<double> vtx_uv(vtx_xy_deformed.size(), 0.);
for(unsigned int iframe=0;iframe<100;++iframe){
StepTime(
vtx_xy_deformed, vtx_uv,
vtx_xy_initial,
dt, stiff_stretch, stiff_bend, mass_point, gravity,
ls_solver);
viewer.DrawBegin_oldGL();
Draw(vtx_xy_deformed, vtx_xy_initial);
viewer.SwapBuffers();
glfwPollEvents();
if (glfwWindowShouldClose(viewer.window)) { break; }
}
if (glfwWindowShouldClose(viewer.window)) { break; }
}
{
glfwSetWindowTitle(viewer.window, "block sparse solver free rotation");
delfem2::LinearSystemSolver_BlockSparse ls_solver;
{
std::vector<unsigned int> psup_ind, psup;
delfem2::JArray_PSuP_Hair(
psup_ind, psup,
{0, static_cast<unsigned int>(vtx_xy_deformed.size() / 2)});
ls_solver.Initialize(
vtx_xy_deformed.size() / 2, 2,
psup_ind, psup);
}
ls_solver.dof_bcflag[0] = 1;
ls_solver.dof_bcflag[1] = 1;
vtx_xy_deformed = vtx_xy_initial;
for (unsigned int i = 0; i < vtx_xy_deformed.size(); ++i) {
if (ls_solver.dof_bcflag[i] != 0) { continue; }
vtx_xy_deformed[i] += 0.1*distm1p1(rndeng);
}
std::vector<double> vtx_uv(vtx_xy_deformed.size(), 0.);
for(unsigned int iframe=0;iframe<100;++iframe){
StepTime(
vtx_xy_deformed, vtx_uv,
vtx_xy_initial,
dt, stiff_stretch, stiff_bend, mass_point, gravity,
ls_solver);
viewer.DrawBegin_oldGL();
Draw(vtx_xy_deformed, vtx_xy_initial);
viewer.SwapBuffers();
glfwPollEvents();
if (glfwWindowShouldClose(viewer.window)) { break; }
}
if (glfwWindowShouldClose(viewer.window)) { break; }
}
}
viewer.ExitIfClosed();
return 0;
}