-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_gramiam_varifolds.cpp
375 lines (318 loc) · 10.2 KB
/
compute_gramiam_varifolds.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
// Created on 17/08/2016 by Pietro Gori, Inria
//
// C++ function that takes as input a fiber bundle in .VTK format and computes the
// gramiam matrix between all streamlines. Every cell of the gramiam matrix is
// the inner product between two streamlines in the framework of weighted
// currents.
//
// Usage: Gramiam FiberBundle dimension lambdaW Lambda_Start Lambda_End
//
// Input Parameters:
// - FiberBundle: filename fiber bundle in .vtk format
// - dimension: dimension of the points of the stremlines (i.e. 3 for 3D)
// - lambdaW: bandwidth of the geometric kernel of usual currents
// - Lambda_Start: bandwidth of kernel relative to the starting points
// - Lambda_End: bandwidth of kernel relative to the ending points
// To note: Streamlines must have a consistent orientation ! For instance they
// should all have the same starting and ending ROIs (Region Of Interest)
//
// Outputs:
// 3 binary files, let N be equal to the number of streamlines
// - graph.diag: it is a vector [Nx1] with the squared norm of each streamline.
// Every value is saved as a char of 4 bits
// - graph.bin: It is a vector of char. If first writes the number of Nodes
// (i.e. number fo streamlines) as a char of 4 bits. Then it writes
// the cumulative degree sequence, which means that for each
// streamline i it writes the number of streamlines that have an
// inner product greater than 0 as a char of 8 bits. Then it writes
// the numbers of all these streamlines as a char of 4 bits.
// - graph.weights: A vector with the inner products different from 0 between
// the streamlines. They are chars of 4 bits. The squared norm
// of each streamline is not considered.
//
// To note, this is the style accepted in the function community.
//
// Example: Gram matrix is [2 0 2; 3 4 6; 0 0 2].
// graph.diag contains: 2 4 2 (squared norms, diagonal)
// graph.bin contains: 3 (number streamlines) 1 2 0 (number entries different from 0)
// 2 (last column) 0 2 (first and last columns) (no value there are only zeros)
// graph.weights contains: 2 3 6 (the inner products different from zero)
#include <cmath>
#include <cstdio>
#include <time.h>
#include <sys/time.h>
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <stdexcept>
#include <vector>
#include <algorithm>
#include <Eigen/Eigen>
#include <Eigen/Sparse>
#include <Eigen/Core>
#include <Eigen/Dense>
#include "vtkPolyData.h"
#include <vtkSmartPointer.h>
#include "vtkPolyDataWriter.h"
#include "vtkPointData.h"
#include "vtkCellData.h"
#include "vtkIdList.h"
#include "vtkWindowedSincPolyDataFilter.h"
#include "vtkPoints.h"
#include "vtkPolyDataReader.h"
#include <vtkCellArray.h>
#include <vtkDataArray.h>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include "itksys/SystemTools.hxx"
#ifdef _OPENMP
#include <omp.h>
#endif
using namespace std;
using namespace Eigen;
int main(int argc, char** argv)
{
if (argc < 6 )
{
std::cerr << "Usage: " << argv[0] << " FiberBundle dimension lambdaW Lambda_Start Lambda_End" << std::endl;
return -1;
}
// Code to see how many threads there in the PC
int nThreads, tid;
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num();
if (tid == 0)
{
nThreads = omp_get_num_threads();
printf("Total number of threads: %d\n", nThreads);
}
}
omp_set_num_threads(nThreads); // Code uses all threads available
// Reading Parameters
char* Bundle_name = argv[1];
int Dimension = atoi(argv[2]);
double lambdaW = atof(argv[3]);
double lambdaA = atof(argv[4]);
double lambdaB = atof(argv[5]);
cout << "Bundle to analyse: " << Bundle_name << endl;
cout << "Lambda geometry: " << lambdaW << "\nLambda Start (basal): " << lambdaA << "\nLambda End (cortical): " << lambdaB << endl;
//Creating polydata
vtkSmartPointer<vtkPolyDataReader> reader = vtkSmartPointer<vtkPolyDataReader>::New();
reader->SetFileName(Bundle_name);
reader->Update();
vtkSmartPointer<vtkPolyData> polyData = reader->GetOutput();
// Initialisation variables
vector<vector<pair<int,float> > > links;
int NumFibers;
int NumberOfPoints;
vtkIdType *indices;
vtkIdType numberOfPoints;
unsigned int lineCount;
vtkCellArray* Lines;
VectorXi NumberPointsPerFiber;
VectorXf WeightFibers;
MatrixXf Points;
vector< MatrixXf > ListPointsFibers;
vector< MatrixXf > Centers;
vector< MatrixXf > Tangents;
MatrixXf First;
MatrixXf Last;
MatrixXf c1; // Matrix of double
MatrixXf t1;
VectorXf f1; // Vector of double
VectorXf l1;
MatrixXf c2;
MatrixXf t2;
VectorXf f2;
VectorXf l2;
RowVectorXf point;
RowVectorXf p0;
RowVectorXf p1;
// Reading the polydata
NumFibers = polyData->GetNumberOfLines();
cout << "Number fibers: " << NumFibers << endl;
links.resize(NumFibers);
NumberOfPoints= polyData->GetNumberOfPoints();
cout << "Number points: " << NumberOfPoints << endl;
Lines = polyData->GetLines();
NumberPointsPerFiber.resize(NumFibers);
WeightFibers.resize(NumFibers);
lineCount = 0;
for (Lines->InitTraversal(); Lines->GetNextCell(numberOfPoints, indices); lineCount++)
{
NumberPointsPerFiber(lineCount)=numberOfPoints;
}
//cout << "NumberPointsPerFiber: " << NumberPointsPerFiber << endl;
if( NumberPointsPerFiber.sum() != NumberOfPoints )
throw runtime_error("Total Number of Points Mismatched!");
Points.resize(NumberOfPoints,Dimension);
for (unsigned int i = 0; i < NumberOfPoints; i++)
{
double p[3];
polyData->GetPoint(i, p);
for (int dim = 0; dim < Dimension; dim++)
{
float pf = (float)(p[dim]);
Points(i, dim) = pf;
}
}
// List Points per Fiber
unsigned int temp = 0;
ListPointsFibers.resize(NumFibers);
for (unsigned int lineCount = 0; lineCount<NumFibers; lineCount++)
{
ListPointsFibers[lineCount].resize(NumberPointsPerFiber(lineCount), Dimension);
for (unsigned int i = 0; i < NumberPointsPerFiber(lineCount,0); i++)
{
point = Points.row(temp);
ListPointsFibers[lineCount].row(i)=point;
temp++;
}
}
// Computing centers, tangents, first and last points
Centers.resize(NumFibers);
Tangents.resize(NumFibers);
First.resize(NumFibers,Dimension);
Last.resize(NumFibers,Dimension);
for (unsigned int i = 0; i < NumFibers; i++)
{
Centers[i].resize(NumberPointsPerFiber(i)-1, Dimension);
Tangents[i].resize(NumberPointsPerFiber(i)-1, Dimension);
First.row(i)=ListPointsFibers[i].row(0);
Last.row(i)=ListPointsFibers[i].row(NumberPointsPerFiber(i)-1);
for (unsigned int j = 0; j < NumberPointsPerFiber(i)-1; j++)
{
p0 = ListPointsFibers[i].row(j);
p1 = ListPointsFibers[i].row(j+1);
Centers[i].row(j) = (p0 + p1) / 2.0;
Tangents[i].row(j) = p1 - p0;
}
}
struct timeval start, end;
double delta;
VectorXf Diagonal;
Diagonal.resize(NumFibers);
// Multi-Threading
gettimeofday(&start, NULL);
for(unsigned int i=0; i<NumFibers; i++)
{
c1.setZero(NumberPointsPerFiber(i)-1, Dimension);
t1.setZero(NumberPointsPerFiber(i)-1, Dimension);
f1.setZero(Dimension);
l1.setZero(Dimension);
c1 = Centers[i];
t1 = Tangents[i];
f1 = First.row(i);
l1 = Last.row(i);
#pragma omp parallel for private(c2,t2,f2,l2) shared(links,Diagonal,i,t1,c1,f1,l1,lambdaW,lambdaA,lambdaB)
for(unsigned int j=i; j<NumFibers; j++)
{
c2.setZero(NumberPointsPerFiber(j)-1, Dimension);
t2.setZero(NumberPointsPerFiber(j)-1, Dimension);
f2.setZero(Dimension);
l2.setZero(Dimension);
c2 = Centers[j];
t2 = Tangents[j];
f2 = First.row(j);
l2 = Last.row(j);
// Computation norm usual currents
float norm2 = 0;
float res_tang;
float res_center;
float tLen1;
float tLen2;
for (int p=0; p<NumberPointsPerFiber(i)-1; p++)
{
tLen1 = (float)sqrt(t1.row(p)*t1.row(p).transpose());
for (int q=0; q<NumberPointsPerFiber(j)-1; q++)
{
tLen2 = (float)sqrt(t2.row(q)*t2.row(q).transpose());
res_tang = t1.row(p)*t2.row(q).transpose();
res_center = ( c1.row(p)-c2.row(q) ) * ( (c1.row(p)-c2.row(q)).transpose() );
norm2 = norm2+res_tang*res_tang*exp(-res_center/(lambdaW*lambdaW))/(tLen1*tLen2);
}
}
// Computation of the other two kernels, only if the norm of usual currents
// is greater than 1e-7, otherwise it writes 0
float norm2_f = 0;
float res_f;
float res_l;
if (abs(norm2)>1e-7)
{
//res_f = (f1-f2).transpose()*(f1-f2);
//res_l = (l1-l2).transpose()*(l1-l2);
//norm2_f = norm2 * exp(-res_f/(lambdaA*lambdaA) - res_l/(lambdaB*lambdaB));
norm2_f = norm2 ;
// If the norm is smaller than 1e-7, it writes 0
if (abs(norm2_f)>1e-7)
{
if (i==j)
{
Diagonal[i]=norm2_f;
}
else
{
#pragma omp critical // This is important, otherwise there is an error of Segmentation Fault
{
links[i].push_back(make_pair(j,norm2_f));
links[j].push_back(make_pair(i,norm2_f));
}
}
}
}
} // end for j
if (remainder(i,10000)==0)
cout << "Iter " << i << endl;
} // end for i
// Diagonal Element
ofstream Diag;
Diag.open("graph.diag", fstream::out | fstream::binary);
float element = 0;;
for(unsigned int i=0; i<NumFibers; i++)
{
element = Diagonal[i];
Diag.write((char *)(&element),4);
}
Diag.close();
// Gramiam
ofstream Gram;
Gram.open("graph.bin", fstream::out | fstream::binary);
ofstream Weights;
Weights.open("graph.weights", fstream::out | fstream::binary);
unsigned int s = links.size();
if (s!=NumFibers)
cerr << "PROBABLY THERE IS AN ERROR! Number of nodes should be equal to the number of fibers" << endl;
// outputs number of nodes
Gram.write((char *)(&s),4);
// outputs cumulative degree sequence
long tot=0;
for (unsigned int i=0 ; i<s ; i++) {
tot+=(long)links[i].size();
Gram.write((char *)(&tot),8);
}
// outputs links
for (unsigned int i=0 ; i<s ; i++) {
for (unsigned int j=0 ; j<links[i].size() ; j++)
{
int dest = links[i][j].first;
float weight = links[i][j].second;
Gram.write((char *)(&dest),4);
Weights.write((char *)(&weight),4);
}
}
Gram.close();
Weights.close();
// TIMER
gettimeofday(&end, NULL);
delta = double(end.tv_sec - start.tv_sec) + double(end.tv_usec - start.tv_usec) / 1.e6;
printf ("It took %f seconds \n",delta);
return 0;
}