-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathalgorithm.cpp
More file actions
186 lines (142 loc) · 7.34 KB
/
algorithm.cpp
File metadata and controls
186 lines (142 loc) · 7.34 KB
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
#include "algorithm.h"
int mainAlgorithmLoop (double stiffness, double minStiffness, double stiffnessStep, double maxDistance,
vtkSmartPointer<vtkPoints> pointsTarget, vtkSmartPointer<vtkPoints> transformedPointsTemplate,
int pointCountTemplate, vtkSmartPointer<vtkOctreePointLocator> octreeResizedTarget,
vtkCellArray* linesTemplate,
vtkSmartPointer<vtkRenderWindow> window) {
int correspondences[pointCountTemplate];
int currentCorrespondence;
double distance = 0;
int discarded = 0;
int errorInt = 0;
double epsilon = 1;
MatrixXd Xmatrix(4*pointCountTemplate, 3);
Xmatrix.setZero();
// The loop should start here
std::cout << "Overall points in the mesh: " << pointCountTemplate << std::endl;
std::cout << "Processing..." << std::endl;
while (stiffness > minStiffness) {
int loop = 1;
while (loop) {
for(int i = 0; i < pointCountTemplate; ++i)
{
double point[3];
transformedPointsTemplate->GetPoint(i, point);
currentCorrespondence = octreeResizedTarget->FindClosestPoint( point[0], point[1], point[2], distance );
if (distance < maxDistance) {
correspondences[i] = currentCorrespondence;
}
else {
correspondences[i] = -1;
discarded++;
}
}
std::cout << "Discarded points in this round of finding correspondences: " << discarded << std::endl;
discarded = 0;
// Equation
// Use the extract edges
int linesCountTemplate = linesTemplate->GetNumberOfCells();
linesTemplate->InitTraversal();
vtkSmartPointer<vtkIdList> idList = vtkSmartPointer<vtkIdList>::New();
// Matrix A
typedef Eigen::Triplet<double> T;
std::vector<T> aTripletList;
aTripletList.reserve(linesCountTemplate*8 + 4*pointCountTemplate);
linesTemplate->GetNextCell(idList);
for (int i = 0; i < linesCountTemplate; i++) {
if (idList->GetId(0) < idList->GetId(1)) {
for (int j = 0; j < 4; j++) {
aTripletList.push_back(T(4*i + j, 4*idList->GetId(0) + j,-1*stiffness));
aTripletList.push_back(T(4*i + j, 4*idList->GetId(1) + j,1*stiffness));
}
}
else if (idList->GetId(1) < idList->GetId(0)) {
for (int j = 0; j < 4; j++) {
aTripletList.push_back(T(4*i + j, 4*idList->GetId(1) + j,-1*stiffness));
aTripletList.push_back(T(4*i + j, 4*idList->GetId(0) + j,1*stiffness));
}
}
linesTemplate->GetNextCell(idList);
}
for (int i = 0; i < pointCountTemplate; i++) {
double point[3];
int w = correspondences[i]!=-1 ? 1 : 0;
transformedPointsTemplate->GetPoint(i, point);
for (int j = 0; j < 3; j++) {
aTripletList.push_back(T(4*linesCountTemplate + i, i*4 + j, point[j]*w));
}
aTripletList.push_back(T(4*linesCountTemplate + i, i*4 + 3, 1*w));
}
SparseMatrix<double> Amatrix(4*linesCountTemplate + pointCountTemplate, 4*pointCountTemplate);
Amatrix.setFromTriplets(aTripletList.begin(), aTripletList.end());
SparseMatrix<double> AmatrixT(4*pointCountTemplate, 4*linesCountTemplate + pointCountTemplate);
AmatrixT = Amatrix.transpose();
SparseMatrix<double> AmatrixFinal(4*pointCountTemplate, 4*pointCountTemplate);
AmatrixFinal = AmatrixT * Amatrix;
// Matrix B
MatrixXd Bmatrix(4*linesCountTemplate + pointCountTemplate, 3);
Bmatrix.setZero();
for (int i = 0; i < pointCountTemplate; i++) {
double point[3];
int w = correspondences[i]!=-1 ? 1 : 0;
pointsTarget->GetPoint(correspondences[i], point);
for (int j = 0; j < 3; j++) {
Bmatrix(4*linesCountTemplate + i, j) = point[j]*w;
}
}
MatrixXd BmatrixFinal(4*pointCountTemplate, 3);
BmatrixFinal.setZero();
BmatrixFinal = AmatrixT * Bmatrix;
// Temporal Matrix X
MatrixXd TempXmatrix(4*pointCountTemplate, 3);
TempXmatrix = Xmatrix;
// Solver
clock_t start;
double duration;
start = clock();
// BiCGSTAB<SparseMatrix<double> > solver;
// AmatrixFinal.makeCompressed();
// solver.compute(AmatrixFinal);
// Xmatrix = solver.solve(BmatrixFinal);
SparseLU<SparseMatrix<double, ColMajor>, COLAMDOrdering<int> > solver;
solver.analyzePattern(AmatrixFinal);
solver.factorize(AmatrixFinal);
Xmatrix = solver.solve(BmatrixFinal);
duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;
std::cout<< "Duration of this solver round: " << duration << " seconds" << std::endl;
// Transformation
for (int i = 0; i < pointCountTemplate; i++) {
double point[3];
double result[3];
transformedPointsTemplate->GetPoint(i, point);
for (int j = 0; j < 3; j++) {
result[j] = point[0] * Xmatrix(i*4 + 0,j) + point[1] * Xmatrix(i*4 + 1,j) + point[2] * Xmatrix(i*4 + 2,j) + 1 * Xmatrix(i*4 + 3,j);
}
transformedPointsTemplate->InsertPoint(i, result);
}
// The visualization window is updated.
// The inner loop ends when the norm value is smaller than the epsilon value.
// The outer loop ends after some iterations with different stiffness weights.
double norm = (Xmatrix - TempXmatrix).norm();
std::cout << "The norm value in this round (inner loop): "<< norm << std::endl << std::endl;
//loop = 0;
if (norm < epsilon || norm != norm) {
loop = 0;
if (norm != norm) {
cout << "Bad Matrix Error!" << endl;
errorInt = 1;
}
}
window->Finalize();
window->Render();
window->Start();
}
std::cout << "The stiffness value in this round (outer loop): "<< stiffness << std::endl;
std::cout << "======================================================== "<< std::endl << std::endl;
stiffness -= stiffnessStep;
if (errorInt) {
break;
}
}
return 0;
}