-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRigidAlignmentImpl.cpp
439 lines (376 loc) · 10.8 KB
/
RigidAlignmentImpl.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#include "AABB.h"
#include "RigidAlignmentImpl.h"
RigidAlignment::RigidAlignment(void)
{
}
RigidAlignment::RigidAlignment(const char *landmarkDir, vector<char *> landmarkList, const char *sphere, const char *outdir, bool lmCoordType)
{
strcpy(m_spherename, sphere);
cout << "Loading Files..\n";
if (!lmCoordType) setup(landmarkDir, landmarkList, sphere);
else setup3f(landmarkDir, landmarkList, sphere);
update();
cout << "Optimziation\n";
optimization();
if (outdir != NULL)
{
cout << "Saving Aligned Spheres..\n";
saveSphere(outdir);
}
}
RigidAlignment::RigidAlignment(std::map<std::string, std::vector<int> > landmarksMap, const char *sphere, const char *outdir, bool lmCoordType)
{
strcpy(m_spherename, sphere);
cout << "Loading Files..\n";
setup(landmarksMap, sphere);
update();
cout << "Optimziation\n";
optimization();
if (outdir != NULL)
{
cout << "Saving Aligned Spheres..\n";
saveSphere(outdir);
}
}
RigidAlignment::~RigidAlignment(void)
{
delete m_sphere;
delete [] m_rot;
delete [] fpoint;
delete [] fmean;
delete [] faxis;
}
void RigidAlignment::setup(const char *landmarkDir, vector<char *> landmarkList, const char *sphere)
{
m_sphere = new Mesh();
m_sphere->openFile(sphere);
m_nSubj = landmarkList.size();
for (int i = 0; i < m_nSubj; i++)
{
char fullpath[1024];
sprintf(fullpath, "%s/%s", landmarkDir, landmarkList[i]);
cout << "[" << i << "] " << landmarkList[i] << endl;
readPoint(fullpath);
m_filename.push_back(landmarkList[i]);
}
// rotation angle
m_rot = new float[m_nSubj * 3];
memset(m_rot, 0, sizeof(float) * m_nSubj * 3);
m_nLM = m_point[0].size();
// workspace
fpoint = new float[m_nSubj * m_nLM * 3];
fmean = new float[m_nLM * 3];
faxis = new float[m_nSubj * 3];
// axis
memset(faxis, 0, sizeof(float) * m_nSubj * 3);
for (int i = 0; i < m_nSubj; i++)
{
float *axis = &faxis[i * 3];
for (int j = 0; j < m_nLM; j++)
{
float *p = &fpoint[(i * m_nLM + j) * 3];
int id = m_point[i][j];
memcpy(p, m_sphere->vertex(id)->fv(), sizeof(float) * 3);
for (int k = 0; k < 3; k++) axis[k] += p[k];
}
// axis
float norm = axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2];
norm = sqrt(norm);
for (int k = 0; k < 3; k++) axis[k] /= norm;
}
}
void RigidAlignment::setup(std::map<std::string, std::vector<int> > landmarksMap, const char *sphere)
{
m_sphere = new Mesh();
m_sphere->openFile(sphere);
m_nSubj = landmarksMap.size();
int i = 0;
std::map<std::string, std::vector<int> >::iterator it = landmarksMap.begin(), it_end = landmarksMap.end();
for (; it != it_end; it++)
{
// string name = ;
cout << "[" << i << "] " << it->first << endl;
// readPoint(fullpath);
if (!(it->second).empty()) m_point.push_back((it->second));
m_filename.push_back((it->first).c_str());
i++;
}
// rotation angle
m_rot = new float[m_nSubj * 3];
memset(m_rot, 0, sizeof(float) * m_nSubj * 3);
m_nLM = m_point[0].size();
// workspace
fpoint = new float[m_nSubj * m_nLM * 3];
fmean = new float[m_nLM * 3];
faxis = new float[m_nSubj * 3];
// axis
memset(faxis, 0, sizeof(float) * m_nSubj * 3);
for (int i = 0; i < m_nSubj; i++)
{
float *axis = &faxis[i * 3];
for (int j = 0; j < m_nLM; j++)
{
float *p = &fpoint[(i * m_nLM + j) * 3];
int id = m_point[i][j];
memcpy(p, m_sphere->vertex(id)->fv(), sizeof(float) * 3);
for (int k = 0; k < 3; k++) axis[k] += p[k];
}
// axis
float norm = axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2];
norm = sqrt(norm);
for (int k = 0; k < 3; k++) axis[k] /= norm;
}
}
void RigidAlignment::setup3f(const char *landmarkDir, vector<char *> landmarkList, const char *sphere)
{
m_sphere = new Mesh();
m_sphere->openFile(sphere);
m_nSubj = landmarkList.size();
for (int i = 0; i < m_nSubj; i++)
{
char fullpath[1024];
sprintf(fullpath, "%s/%s", landmarkDir, landmarkList[i]);
cout << "[" << i << "] " << landmarkList[i] << endl;
readPoint3f(fullpath);
m_filename.push_back(landmarkList[i]);
}
// rotation angle
m_rot = new float[m_nSubj * 3];
memset(m_rot, 0, sizeof(float) * m_nSubj * 3);
m_nLM = m_point[0].size();
// workspace
fpoint = new float[m_nSubj * m_nLM * 3];
fmean = new float[m_nLM * 3];
faxis = new float[m_nSubj * 3];
// axis
memset(faxis, 0, sizeof(float) * m_nSubj * 3);
for (int i = 0; i < m_nSubj; i++)
{
float *axis = &faxis[i * 3];
for (int j = 0; j < m_nLM; j++)
{
float *p = &fpoint[(i * m_nLM + j) * 3];
int id = m_point[i][j];
memcpy(p, m_sphere->vertex(id)->fv(), sizeof(float) * 3);
for (int k = 0; k < 3; k++) axis[k] += p[k];
}
// axis
float norm = axis[0] * axis[0] + axis[1] * axis[1] + axis[2] * axis[2];
norm = sqrt(norm);
for (int k = 0; k < 3; k++) axis[k] /= norm;
}
}
void RigidAlignment::readPoint(const char *filename)
{
int i = 0;
FILE *fp = fopen(filename,"r");
vector<int> point;
while (!feof(fp))
{
int id;
fscanf(fp, "%d", &id);
point.push_back(id);
}
point.pop_back();
fclose(fp);
if (!point.empty()) m_point.push_back(point);
}
void RigidAlignment::readPoint3f(const char *filename)
{
AABB *tree = new AABB(m_sphere);
int i = 0;
FILE *fp = fopen(filename,"r");
vector<int> point;
while (!feof(fp))
{
float v[3];
fscanf(fp, "%f %f %f", &v[0], &v[1], &v[2]);
float coeffs[3];
// find the closest face
int fid = tree->closestFace(v, coeffs, 0.01);
Face *f = (Face *)m_sphere->face(fid);
const int *vid = f->list();
int id = vid[0];
// find the closest vertex
if (coeffs[1] >= coeffs[2] && coeffs[1] >= coeffs[0]) id = vid[1];
else if (coeffs[2] >= coeffs[0] && coeffs[2] >= coeffs[1]) id = vid[2];
point.push_back(id);
}
point.pop_back();
fclose(fp);
if (!point.empty()) m_point.push_back(point);
delete tree;
}
float RigidAlignment::landmarkVariance(void)
{
float cost = 0;
for (int i = 0; i < m_nLM; i++)
{
float sd = 0;
for (int j = 0; j < m_nSubj; j++)
{
float inner = 0;
float norm1 = 0;
float norm2 = 0;
for (int k = 0; k < 3; k++)
{
inner += fmean[i * 3 + k] * fpoint[(j * m_nLM + i) * 3 + k];
norm1 += fmean[i * 3 + k] * fmean[i * 3 + k];
norm2 += fpoint[(j * m_nLM + i) * 3 + k] * fpoint[(j * m_nLM + i) * 3 + k];
}
norm1 = sqrt(norm1);
norm2 = sqrt(norm2);
if (norm1 * norm2 == 0) {
continue;
}
inner /= norm1 * norm2;
if (inner > 1) inner = 1;
else if (inner < -1) inner = -1;
float arclen = acos(inner);
sd += arclen * arclen;
}
cost += sqrt(sd);
}
return cost;
}
void RigidAlignment::update(void)
{
// new point
for (int i = 0; i < m_nSubj; i++)
{
const float *axis = &faxis[i * 3];
// new axis
float axis2[3];
updateAxis(m_rot[i * 3 + 1], m_rot[i * 3 + 2], axis, axis2);
Vector ax(axis), ax2(axis2);
float inner = ax * ax2;
if (inner > 1) inner = 1;
else if (inner < -1) inner = -1;
float deg = acos(inner);
// matrix
float mat[9];
Vector ax3 = ax.cross(ax2); ax3.unit();
if (ax3.norm() != 0)
{
Coordinate::rotation(ax3.fv(), deg, mat);
// axis rotation
for (int j = 0; j < m_nLM; j++)
{
float newp[3];
float *p = &fpoint[(i * m_nLM + j) * 3];
int id = m_point[i][j];
memcpy(p, m_sphere->vertex(id)->fv(), sizeof(float) * 3);
Coordinate::rotPoint(p, mat, newp);
memcpy(p, newp, sizeof(float) * 3);
}
}
// matrix
Coordinate::rotation(axis2, m_rot[i * 3], mat);
// rotation
for (int j = 0; j < m_nLM; j++)
{
float newp[3];
float *p = &fpoint[(i * m_nLM + j) * 3];
Coordinate::rotPoint(p, mat, newp);
memcpy(p, newp, sizeof(float) * 3);
}
}
// mean
memset(fmean, 0, sizeof(float) * m_nLM * 3);
for (int i = 0; i < m_nLM; i++)
for (int j = 0; j < m_nSubj; j++)
for (int k = 0; k < 3; k++)
fmean[i * 3 + k] += fpoint[(j * m_nLM + i) * 3 + k] / m_nSubj;
for (int i = 0; i < m_nLM; i++)
{
float norm = fmean[i * 3] * fmean[i * 3] + fmean[i * 3 + 1] * fmean[i * 3 + 1] + fmean[i * 3 + 2] * fmean[i * 3 + 2];
norm = sqrt(norm);
if (norm != 0) {
for (int j = 0; j < 3; j++)
fmean[i * 3 + j] /= norm;
} else {
std::cerr << "The mean of landmark " << i
<< " is at the origin. This is a degenerate case. This landmark point will be skipped during optimization for iteration " << nIter << "." << std::endl;
}
}
/*for (int i = 0; i < m_nLM; i++)
cout << fmean[i * 3] << " " << fmean[i * 3 + 1] << " " << fmean[i * 3 + 2] << endl;*/
}
void RigidAlignment::optimization(void)
{
cost_function costFunc(this);
nIter = 0;
min_newuoa(m_nSubj * 3, m_rot, costFunc, (float)M_PI, 1e-6f, 20000);
}
float RigidAlignment::cost(float *coeff)
{
nIter++;
update();
float cost = landmarkVariance();
//
if (nIter % 10 == 0)
cout << "[" << nIter << "] " << cost << endl;
return cost;
}
const float * RigidAlignment::rot(void)
{
return m_rot;
}
void RigidAlignment::saveSphere(const char *dir)
{
for (int i = 0; i < m_nSubj; i++)
{
const float *axis = &faxis[i * 3];
float axis2[3];
updateAxis(m_rot[i * 3 + 1], m_rot[i * 3 + 2], axis, axis2);
Vector ax(axis), ax2(axis2);
float inner = ax * ax2;
if (inner > 1) inner = 1;
else if (inner < -1) inner = -1;
float deg = acos(inner);
Mesh *sphere = new Mesh();
sphere->openFile(m_spherename);
// matrix
Vector ax3 = ax.cross(ax2); ax3.unit();
if (ax3.norm() != 0)
sphere->rotation(ax3.fv(), deg);
sphere->rotation(axis2, m_rot[i * 3]);
// output
// char filename[1024];
// sprintf(filename, "%s/%s.vtk", dir, m_filename[i]);
string filename;
string temp = "/";
string temp2 = "_rotSphere.vtk";
filename = dir + temp + m_filename[i] + temp2;
// cout << "m_filename " << i << " :: " << m_filename[i] << endl;
sphere->saveFile(filename.c_str(), "vtk");
delete sphere;
}
}
void RigidAlignment::saveLM(const char *dir)
{
for (int i = 0; i < m_nSubj; i++)
{
// char filename[1024];
// sprintf(filename, "%s/%s.txt", dir, m_filename[i]);
string filename;
string temp = "/";
string temp2 = ".txt";
filename = dir + temp + m_filename[i] + temp2;
FILE *fp = fopen(filename.c_str(), "w");
for (int j = 0; j < m_nLM; j++)
{
int id = m_point[i][j];
fprintf(fp, "%d\n", id);
}
fclose(fp);
}
}
void RigidAlignment::updateAxis(const float phi, const float theta, const float *axis_old, float *axis_new)
{
float phi_, theta_;
Coordinate::cart2sph(axis_old, &phi_, &theta_);
phi_ += phi;
theta_ += theta;
Coordinate::sph2cart(phi_, theta_, axis_new);
}