-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
352 lines (288 loc) · 9.66 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
#include <iostream>
#include <windows.h>
#include <queue>
#include <Kinect.h>
#include <list>
#include "scene.h"
#include "SpringsBuilder.h"
#include "Simulator.h"
#include "parameter.h"
#include "./bvh/BVHAccel.h"
#include "generateBody.h"
#include "detectBody.h"
#include "KinectJointFilter.h"
#include "Mesh.h"
#include "VAOMesh.h"
#include "Cloth.h"
#include "ObjLoader.h"
// #define VLD_FORCE_ENABLE
// #include<vld.h>
using namespace std;
extern int StartNum = 100;
extern int StopNum = 300;
int NameofBody = StartNum;
string clothfile = "../smooth/2/";
queue<Mesh> BodyQueue;
queue<Vec4s> VQueue;
queue<Vec3s> NQueue;
HANDLE hMutex = NULL;
list<mat> smoothList;
list<mat>::iterator iter;
DoudouHead_Merge DoudouHead_Solver;
//Thread1 for Getting Every Frames Body
DWORD WINAPI GetBodyData(LPVOID pParam)
{
for (int i = StartNum + 1; i <= StopNum; i++)
{
string file_name;
char num[4];
_itoa_s(i, num, 10);
string s = num;
file_name = clothfile + s + ".obj";
ObjLoader loader;
Mesh new_body;
loader.load(new_body, file_name);
new_body.scale(0.30f);
new_body.translate(0.0f, 1.0f, 0.0f);
BodyQueue.push(new_body);
}
return 0;
}
//Thread1 for Getting Every Frames Body
DWORD WINAPI DtoG(LPVOID pParam)
{
//Initiate Template
SMPL bodyTemplate = SMPL(MALE);
cout << "SMPL::initial finished!" << endl;
//Initiate Sensor
IKinectSensor *pSensor;
HRESULT hResult = S_OK;
hResult = GetDefaultKinectSensor(&pSensor);
hResult = pSensor->Open();
if (FAILED(hResult)) {
std::cerr << "Error : IKinectSensor::Open()" << std::endl;
return -1;
}
IBodyFrameSource *pBodySource;
hResult = pSensor->get_BodyFrameSource(&pBodySource);
if (FAILED(hResult)) {
std::cerr << "Error : IKinectSensor::get_BodyFrameSource()" << std::endl;
return -1;
}
IBodyFrameReader *pBodyReader;
hResult = pBodySource->OpenReader(&pBodyReader);
if (FAILED(hResult)) {
std::cerr << "Error : IBodyFrameSource::OpenReader()" << std::endl;
return -1;
}
//mat pp = zeros(24, 3);
//mat result=bodyTemplate.gen_pose_model(pp, TRUE);
//bodyTemplate.write_to_obj(result, "MALE.obj");
// Holt Double Exponential Smoothing Filter
Sample::FilterDoubleExponential filter[BODY_COUNT];
// Option : Setting Smoothing Parameter
for (int count = 0; count < BODY_COUNT; count++) {
float smoothing = 0.5f; // [0..1], lower values closer to raw data
float correction = 0.5f; // [0..1], lower values slower to correct towards the raw data
float prediction = 0.5f; // [0..n], the number of frames to predict into the future
float jitterRadius = 0.05f; // The radius in meters for jitter reduction
float maxDeviationRadius = 0.04f; // The maximum radius in meters that filtered positions are allowed to deviate from raw data
filter[count].Init(smoothing, correction, prediction, jitterRadius, maxDeviationRadius);
}
//The label number of the first body detected by Kinect
int BODY_LABEL = -1;
StopWatch time;
time.start();
int counter = 1;
bool tag = TRUE;
bool first = TRUE;
while (counter)
{
Vec4s vertex;
Vec3s normal;
//Obj new_body = BodyQueue.front();
mat trans_joint;
//bool judge = detectJoint(hResult, pBodyReader, joint);
IBodyFrame *pBodyFrame = nullptr;
hResult = pBodyReader->AcquireLatestFrame(&pBodyFrame);
if (SUCCEEDED(hResult)) {
IBody *pBody[BODY_COUNT] = { 0 };
hResult = pBodyFrame->GetAndRefreshBodyData(BODY_COUNT, pBody);
if (SUCCEEDED(hResult)) {
for (int count = 0; count < BODY_COUNT; count++) {
BOOLEAN bTracked = false;
hResult = pBody[count]->get_IsTracked(&bTracked);
if (bTracked&&SUCCEEDED(hResult) && BODY_LABEL == -1)
BODY_LABEL = count;
if (SUCCEEDED(hResult) && bTracked && count == BODY_LABEL) {
//counter--;
Joint joint[JointType::JointType_Count];
/////////////////////////////
hResult = pBody[count]->GetJoints(JointType::JointType_Count, joint);//joint
//////////////////////// Filtered Joint//////////////////////////////////
filter[count].Update(joint);
const DirectX::XMVECTOR *vec = filter[count].GetFilteredJoints();
for (int type = 0; type < JointType::JointType_Count; type++) {
if (joint[type].TrackingState != TrackingState::TrackingState_NotTracked) {
float x = 0.0f, y = 0.0f, z = 0.0f;
DirectX::XMVectorGetXPtr(&x, vec[type]);
DirectX::XMVectorGetYPtr(&y, vec[type]);
DirectX::XMVectorGetZPtr(&z, vec[type]);
}
}
////////////////////////////////////////////////////////////////////////
//Get joint for genBody from kinect joint
trans_joint = JointTransform(joint);
////////////////Transition from T-pose to first frame///////////////////////////////////
if (first == TRUE) {
mat pose = bodyTemplate.J_to_pose(trans_joint);
float coefficient = 0.04f / max(max(pose));
cout << coefficient << endl;
mat transition = zeros(24, 3);
int num = 0;
while (max(max(abs(transition))) < max(max(abs(pose))))
{
//transition.print("t:");
genFirstBody(transition, vertex, normal, bodyTemplate);
transition += pose*coefficient;
VQueue.push(vertex);
NQueue.push(normal);
num++;
}
cout << num << endl;
first = FALSE;
}
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////////Smooth by List////////////////////////////////////////
mat sum = zeros(24, 3);
if (smoothList.size() < 5)
smoothList.push_back(trans_joint);
else {
for (iter = smoothList.begin(); iter != smoothList.end(); ++iter)
{
sum += (*iter);
}
sum = sum / 5;
smoothList.pop_front();
smoothList.push_back(trans_joint);
///////////////////////////////////////////////////////////////////////////
genBodyVector(sum, vertex, normal, bodyTemplate);
cout << "A new pose has been detected!" << endl;
if (tag == TRUE) {
VQueue.push(vertex);
NQueue.push(normal);
tag = FALSE;
cout << "num:" << VQueue.size() << endl;
}
else tag = TRUE;
time.stop();
cout << "cost:" << time.elapsed_ms() << endl;
time.restart();
}
//return TRUE;
}
}
}
for (int count = 0; count < BODY_COUNT; count++) {
SafeRelease(pBody[count]);
}
}
SafeRelease(pBodyFrame);
//if (judge)
//{
// genBody(joint, new_body);
// cout << "A new pose has been detected!" << endl;
//}
//else continue;
//new_body.scale_translate(0.30, 0, 1.0, 0);
//new_body.unified();
//BodyQueue.push(new_body);
}
SafeRelease(pBodySource);
SafeRelease(pBodyReader);;
if (pSensor) {
pSensor->Close();
}
SafeRelease(pSensor);
return 0;
}
int main(int argc, char* *argv)
{
Scene::instance().initialize(argc, argv); //initialize opengl
ObjLoader loader;
Cloth cloth(SINGLE_LAYER_NOB);
////²âÊÔÒ·þ
//Obj cloth("../cloth/cloth.obj"); //pose0
//cloth.scale_translate(0.31, 0, 1.95, 0.02);
//cloth.unified();
//Obj cloth("../cloth_no_boundary/dress2/dress2-iso.obj",SINGLE_LAYER_NOB);
//cloth.rotation(90, X); //
//cloth.scale_translate(0.24, 0, 1.2, 0.02);
//cloth.unified();
//Obj cloth("../cloth_no_boundary/dress3/dress3.obj",SINGLE_LAYER_NOB);
//cloth.rotation(90, X);
//cloth.scale_translate(0.24, 0, 0.45, 0.02);
//cloth.unified();
//Obj cloth("../cloth_no_boundary/dress-asymmetric/dress-asymmetric.obj", SINGLE_LAYER_NOB);
//cloth.rotation(90, X); //
//cloth.scale_translate(0.25, 0, 1.10, 0.02);
//cloth.unified();
//Obj cloth("../cloth_no_boundary/dress-victor/dress-victor.obj", SINGLE_LAYER_NOB);
//cloth.rotation(90, X);
//cloth.scale_translate(0.25, 0, 1.60, 0.02);
//cloth.unified();
//Obj cloth("../cloth_no_boundary/robe/robe.obj", SINGLE_LAYER_NOB);
//cloth.rotation(90, X); //
//cloth.scale_translate(0.3, 0, 0.5, 0.0);
//cloth.unified();
//Obj cloth("../cloth_no_boundary/tshirt/tshirt.obj", SINGLE_LAYER_NOB);
//cloth.rotation(90, X);
//cloth.rotation(-5, Z);
//cloth.scale_translate(0.26, 0, 1.18, -0.1);
//cloth.unified();
//Obj cloth("../cloth_no_boundary/shirt/shirt.obj", SINGLE_LAYER_NOB);
//cloth.rotation(90, X);
//cloth.rotation(-4, Z);
//cloth.scale_translate(0.27, 0, 2.1, 0.15);
//cloth.unified();
//Obj cloth("../cloth_no_boundary/skirt/skirt.obj", SINGLE_LAYER_NOB);
//cloth.rotation(90, X);
//cloth.scale_translate(0.29, 0, 0.5, 0);
//cloth.unified();
loader.load(cloth, "../cloth_no_boundary/tshirt2/tshirt2.obj");
cloth.rotation(90, 0, 0);
cloth.rotation(0, 0, -4);
cloth.scale(0.28f);
cloth.translate(0, 0.9f, -2.2f);
//Obj cloth("../cloth_no_boundary/shorts/shorts.obj", SINGLE_LAYER_NOB);
//cloth.rotation(90, X); //
//cloth.scale_translate(0.29, 0, 0.5, 0);
//cloth.unified();
//Obj cloth("../cloth_no_boundary/vest/vest.obj", SINGLE_LAYER_NOB);
//cloth.rotation(90, X);
//cloth.scale_translate(0.28, 0, 1.4, 0.02);
//cloth.unified();
//string file;
//char num_s[4];
//_itoa_s(StartNum, num_s, 10);
//string ss = num_s;
//file = clothfile + ss + ".obj";
//Obj body(file);
VAOMesh body, head;
loader.load(body, "../Template/MALE.obj");
body.scale(0.3f);
body.translate(0.0f, 0.6f, 0.0f);
loader.load(head, "../DoudouHead/HeadColored.obj");
DoudouHead_Solver.Init(head);
Scene::instance().add(cloth);
Scene::instance().add(body);
Scene::instance().add(head);
Scene::instance().initiate_body_template(body);
Scene::instance().update_simulating_cloth(cloth);
Scene::instance().update_simulating_body(body);
HANDLE hThread1 = CreateThread(NULL, 0, DtoG, NULL, 0, NULL);
CloseHandle(hThread1);
hMutex = CreateMutex(NULL, FALSE, NULL);
Scene::instance().render();
return 0;
}