df036f4c
David Mayerich
added the network...
|
1
2
3
|
#ifndef STIM_NETWORK_H
#define STIM_NETWORK_H
|
7f27eafa
David Mayerich
simplified the st...
|
4
|
#include <stdlib.h>
|
9c97e126
David Mayerich
added an axis-ali...
|
5
|
#include <assert.h>
|
7f27eafa
David Mayerich
simplified the st...
|
6
7
8
9
10
|
#include <sstream>
#include <fstream>
#include <algorithm>
#include <string.h>
#include <math.h>
|
308a743c
David Mayerich
fixed class compa...
|
11
|
#include <stim/math/vec3.h>
|
5687cf1b
Cherub P. Harder
Added #include <s...
|
12
|
#include <stim/visualization/obj.h>
|
1599ae65
Jiaming Guo
add swc.h and fun...
|
13
|
#include <stim/visualization/swc.h>
|
58ee2b21
David Mayerich
incorporated stim...
|
14
|
#include <stim/visualization/cylinder.h>
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
15
|
#include <stim/cuda/cudatools/timer.h>
|
5068402b
Jiaming Guo
add function to a...
|
16
|
#include <stim/cuda/cudatools/callable.h>
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
17
|
#include <stim/structures/kdtree.cuh>
|
5068402b
Jiaming Guo
add function to a...
|
18
19
20
|
//********************help function********************
// gaussian_function
CUDA_CALLABLE float gaussianFunction(float x, float std = 25) { return exp(-x / (2 * std*std)); } // std default sigma value is 25
|
df036f4c
David Mayerich
added the network...
|
21
|
|
5068402b
Jiaming Guo
add function to a...
|
22
|
// compute metric in parallel
|
c72184d1
Jiaming Guo
add many function...
|
23
|
#ifdef __CUDACC__
|
c72184d1
Jiaming Guo
add many function...
|
24
|
template <typename T>
|
5068402b
Jiaming Guo
add function to a...
|
25
|
__global__ void find_metric_parallel(T* M, size_t n, T* D, float sigma){
|
c72184d1
Jiaming Guo
add many function...
|
26
27
28
29
30
31
|
size_t x = blockDim.x * blockIdx.x + threadIdx.x;
if(x >= n) return;
M[x] = 1.0f - gaussianFunction(D[x], sigma);
}
//find the corresponding edge index from array index
|
5068402b
Jiaming Guo
add function to a...
|
32
|
__global__ void find_edge_index_parallel(size_t* I, size_t n, unsigned* R, size_t* E, size_t ne){
|
c72184d1
Jiaming Guo
add many function...
|
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
size_t x = blockDim.x * blockIdx.x + threadIdx.x;
if(x >= n) return;
unsigned i = 0;
size_t N = 0;
for(unsigned e = 0; e < ne; e++){
N += E[e];
if(I[x] < N){
R[x] = i;
break;
}
i++;
}
}
#endif
//hard-coded factor
|
5068402b
Jiaming Guo
add function to a...
|
49
|
int threshold_fac;
|
c72184d1
Jiaming Guo
add many function...
|
50
|
|
df036f4c
David Mayerich
added the network...
|
51
|
namespace stim{
|
7f27eafa
David Mayerich
simplified the st...
|
52
53
54
|
/** This is the a class that interfaces with gl_spider in order to store the currently
* segmented network. The following data is stored and can be extracted:
* 1)Network geometry and centerline.
|
5068402b
Jiaming Guo
add function to a...
|
55
|
* 2)Network connectivity (a graph of nodes and edges), reconstructed using kdtree.
|
7f27eafa
David Mayerich
simplified the st...
|
56
|
*/
|
df036f4c
David Mayerich
added the network...
|
57
|
|
df036f4c
David Mayerich
added the network...
|
58
59
60
|
template<typename T>
class network{
|
7f27eafa
David Mayerich
simplified the st...
|
61
62
|
///Each edge is a fiber with two nodes.
///Each node is an in index to the endpoint of the fiber in the nodes array.
|
58ee2b21
David Mayerich
incorporated stim...
|
63
|
class edge : public cylinder<T>
|
7f27eafa
David Mayerich
simplified the st...
|
64
65
|
{
public:
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
66
|
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
67
|
unsigned int v[2]; //unique id's designating the starting and ending
|
7c43b7f9
pranathivemuri
return resampled ...
|
68
|
// default constructor
|
eb554b48
David Mayerich
bug fixes for Net...
|
69
|
edge() : cylinder<T>() {
|
e8c9a82b
David Mayerich
fixed GCC errors ...
|
70
|
v[1] = (unsigned)(-1); v[0] = (unsigned)(-1);
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
71
|
}
|
7f27eafa
David Mayerich
simplified the st...
|
72
|
/// Constructor - creates an edge from a list of points by calling the stim::fiber constructor
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
73
|
/*
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
74
75
76
77
78
79
80
|
///@param v0, the starting index.
///@param v1, the ending index.
///@param sz, the number of point in the fiber.
edge(unsigned int v0, unsigned int v1, unsigned int sz) : cylinder<T>(
{
}
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
81
|
*/
|
296aa37d
Pavel Govyadinov
fixed parsing errors
|
82
|
edge(std::vector<stim::vec3<T> > p, std::vector<T> s)
|
49061f1d
Pavel Govyadinov
Modified centerli...
|
83
84
85
|
: cylinder<T>(p,s)
{
}
|
3800c27f
David Mayerich
added code to sub...
|
86
|
///@param p is an array of positions in space
|
ee17b90b
Jiaming Guo
make it compatibl...
|
87
|
edge(stim::centerline<T> p) : cylinder<T>(p){}
|
3800c27f
David Mayerich
added code to sub...
|
88
|
|
eb554b48
David Mayerich
bug fixes for Net...
|
89
|
/// Copy constructor creates an edge from a cylinder
|
58ee2b21
David Mayerich
incorporated stim...
|
90
|
edge(stim::cylinder<T> f) : cylinder<T>(f) {}
|
3800c27f
David Mayerich
added code to sub...
|
91
92
93
|
/// Resamples an edge by calling the fiber resampling function
edge resample(T spacing){
|
58ee2b21
David Mayerich
incorporated stim...
|
94
|
edge e(cylinder<T>::resample(spacing)); //call the fiber->edge constructor
|
3800c27f
David Mayerich
added code to sub...
|
95
96
97
98
99
|
e.v[0] = v[0]; //copy the vertex data
e.v[1] = v[1];
return e; //return the new edge
}
|
9c97e126
David Mayerich
added an axis-ali...
|
100
|
|
7f27eafa
David Mayerich
simplified the st...
|
101
|
/// Output the edge information as a string
|
df036f4c
David Mayerich
added the network...
|
102
103
|
std::string str(){
std::stringstream ss;
|
fbbc07be
Jiaming Guo
replace ANN by kd...
|
104
|
ss<<"("<<cylinder<T>::size()<<")\tl = "<<this->length()<<"\t"<<v[0]<<"----"<<v[1];
|
df036f4c
David Mayerich
added the network...
|
105
106
|
return ss.str();
}
|
df036f4c
David Mayerich
added the network...
|
107
|
|
c72184d1
Jiaming Guo
add many function...
|
108
109
110
111
112
113
114
115
116
117
118
119
|
std::vector<edge> split(unsigned int idx){
std::vector< stim::cylinder<T> > C;
C.resize(2);
C = (*this).cylinder<T>::split(idx);
std::vector<edge> E(C.size());
for(unsigned e = 0; e < E.size(); e++){
E[e] = C[e];
}
return E;
}
|
029c021a
Pavel Govyadinov
filled in the res...
|
120
|
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
121
|
/// operator for writing the edge information into a binary .nwt file.
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
122
|
friend std::ofstream& operator<<(std::ofstream& out, const edge& e)
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
123
124
125
|
{
out.write(reinterpret_cast<const char*>(&e.v[0]), sizeof(unsigned int)); ///write the starting point.
out.write(reinterpret_cast<const char*>(&e.v[1]), sizeof(unsigned int)); ///write the ending point.
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
126
|
unsigned int sz = e.size(); ///write the number of point in the edge.
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
127
128
129
130
|
out.write(reinterpret_cast<const char*>(&sz), sizeof(unsigned int));
for(int i = 0; i < sz; i++) ///write each point
{
stim::vec3<T> point = e[i];
|
029c021a
Pavel Govyadinov
filled in the res...
|
131
|
out.write(reinterpret_cast<const char*>(&point[0]), 3*sizeof(T));
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
132
133
|
// for(int j = 0; j < nmags(); j++) //future code for multiple mags
// {
|
029c021a
Pavel Govyadinov
filled in the res...
|
134
|
out.write(reinterpret_cast<const char*>(&e.R[i]), sizeof(T)); ///write the radius
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
135
|
//std::cout << point.str() << " " << e.R[i] << std::endl;
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
136
137
138
139
|
// }
}
return out; //return stream
}
|
c72184d1
Jiaming Guo
add many function...
|
140
|
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
141
|
/// operator for reading an edge from a binary .nwt file.
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
142
|
friend std::ifstream& operator>>(std::ifstream& in, edge& e)
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
143
|
{
|
029c021a
Pavel Govyadinov
filled in the res...
|
144
145
146
|
unsigned int v0, v1, sz;
in.read(reinterpret_cast<char*>(&v0), sizeof(unsigned int)); //read the staring point.
in.read(reinterpret_cast<char*>(&v1), sizeof(unsigned int)); //read the ending point
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
147
|
in.read(reinterpret_cast<char*>(&sz), sizeof(unsigned int)); //read the number of points in the edge
|
49061f1d
Pavel Govyadinov
Modified centerli...
|
148
149
150
151
|
// stim::centerline<T> temp = stim::centerline<T>(sz); //allocate the new edge
// e = edge(temp);
std::vector<stim::vec3<T> > p(sz);
std::vector<T> r(sz);
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
152
153
154
|
for(int i = 0; i < sz; i++) //set the points and radii to the newly read values
{
stim::vec3<T> point;
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
155
|
in.read(reinterpret_cast<char*>(&point[0]), 3*sizeof(T));
|
49061f1d
Pavel Govyadinov
Modified centerli...
|
156
|
p[i] = point;
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
157
|
T mag;
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
158
159
160
|
// for(int j = 0; j < nmags(); j++) ///future code for mags
// {
in.read(reinterpret_cast<char*>(&mag), sizeof(T));
|
49061f1d
Pavel Govyadinov
Modified centerli...
|
161
|
r[i] = mag;
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
162
|
//std::cout << point.str() << " " << mag << std::endl;
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
163
|
// }
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
164
|
}
|
49061f1d
Pavel Govyadinov
Modified centerli...
|
165
166
|
e = edge(p,r);
e.v[0] = v0; e.v[1] = v1;
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
167
168
|
return in;
}
|
9c97e126
David Mayerich
added an axis-ali...
|
169
170
|
};
|
7f27eafa
David Mayerich
simplified the st...
|
171
|
///Node class that stores the physical position of the node as well as the edges it is connected to (edges that connect to it), As well as any additional data necessary.
|
308a743c
David Mayerich
fixed class compa...
|
172
|
class vertex : public stim::vec3<T>
|
7f27eafa
David Mayerich
simplified the st...
|
173
174
|
{
public:
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
175
176
177
|
//std::vector<unsigned int> edges; //indices of edges connected to this node.
std::vector<unsigned int> e[2]; //indices of edges going out (e[0]) and coming in (e[1])
//stim::vec3<T> p; //position of this node in physical space.
|
029c021a
Pavel Govyadinov
filled in the res...
|
178
179
180
181
|
//default constructor
vertex() : stim::vec3<T>()
{
}
|
7f27eafa
David Mayerich
simplified the st...
|
182
|
//constructor takes a stim::vec
|
308a743c
David Mayerich
fixed class compa...
|
183
|
vertex(stim::vec3<T> p) : stim::vec3<T>(p){}
|
7f27eafa
David Mayerich
simplified the st...
|
184
185
|
/// Output the vertex information as a string
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
186
187
|
std::string
str(){
|
7f27eafa
David Mayerich
simplified the st...
|
188
|
std::stringstream ss;
|
308a743c
David Mayerich
fixed class compa...
|
189
|
ss<<"\t(x, y, z) = "<<stim::vec3<T>::str();
|
7f27eafa
David Mayerich
simplified the st...
|
190
191
192
193
194
195
196
197
198
199
200
|
if(e[0].size() > 0){
ss<<"\t> ";
for(unsigned int o = 0; o < e[0].size(); o++)
ss<<e[0][o]<<" ";
}
if(e[1].size() > 0){
ss<<"\t< ";
for(unsigned int i = 0; i < e[1].size(); i++)
ss<<e[1][i]<<" ";
}
|
df036f4c
David Mayerich
added the network...
|
201
|
|
7f27eafa
David Mayerich
simplified the st...
|
202
|
return ss.str();
|
df036f4c
David Mayerich
added the network...
|
203
|
}
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
204
|
///operator for writing the vector into the stream;
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
205
|
friend std::ofstream& operator<<(std::ofstream& out, const vertex& v)
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
206
|
{
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
207
208
209
|
unsigned int s0, s1;
s0 = v.e[0].size();
s1 = v.e[1].size();
|
029c021a
Pavel Govyadinov
filled in the res...
|
210
|
out.write(reinterpret_cast<const char*>(&v.ptr[0]), 3*sizeof(T)); ///write physical vertex location
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
211
212
213
214
215
216
|
out.write(reinterpret_cast<const char*>(&s0), sizeof(unsigned int)); ///write the number of "outgoing edges"
out.write(reinterpret_cast<const char*>(&s1), sizeof(unsigned int)); ///write the number of "incoming edges"
if (s0 != 0)
out.write(reinterpret_cast<const char*>(&v.e[0][0]), sizeof(unsigned int)*v.e[0].size()); ///write the "outgoing edges"
if (s1 != 0)
out.write(reinterpret_cast<const char*>(&v.e[1][0]), sizeof(unsigned int)*v.e[1].size()); ///write the "incoming edges"
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
217
218
219
|
return out;
}
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
220
|
///operator for reading the vector out of the stream;
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
221
|
friend std::ifstream& operator>>(std::ifstream& in, vertex& v)
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
222
|
{
|
029c021a
Pavel Govyadinov
filled in the res...
|
223
224
225
|
in.read(reinterpret_cast<char*>(&v[0]), 3*sizeof(T)); ///read the physical position
unsigned int s[2];
in.read(reinterpret_cast<char*>(&s[0]), 2*sizeof(unsigned int)); ///read the sizes of incoming and outgoing edge arrays
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
226
227
228
229
230
|
std::vector<unsigned int> one(s[0]);
std::vector<unsigned int> two(s[1]);
v.e[0] = one;
v.e[1] = two;
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
231
232
233
234
|
if (one.size() != 0)
in.read(reinterpret_cast<char*>(&v.e[0][0]), s[0] * sizeof(unsigned int)); ///read the arrays of "outgoing edges"
if (two.size() != 0)
in.read(reinterpret_cast<char*>(&v.e[1][0]), s[1] * sizeof(unsigned int)); ///read the arrays of "incoming edges"
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
235
236
|
return in;
}
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
237
|
|
df036f4c
David Mayerich
added the network...
|
238
239
|
};
|
9c97e126
David Mayerich
added an axis-ali...
|
240
|
protected:
|
df036f4c
David Mayerich
added the network...
|
241
|
|
7f27eafa
David Mayerich
simplified the st...
|
242
|
std::vector<edge> E; //list of edges
|
1599ae65
Jiaming Guo
add swc.h and fun...
|
243
|
std::vector<vertex> V; //list of vertices.
|
9c97e126
David Mayerich
added an axis-ali...
|
244
245
|
public:
|
df036f4c
David Mayerich
added the network...
|
246
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
247
248
249
250
251
252
253
254
255
256
257
258
|
///default constructor
network()
{
}
///constructor with a file to load.
network(std::string fileLocation)
{
load_obj(fileLocation);
}
|
7f27eafa
David Mayerich
simplified the st...
|
259
260
261
|
///Returns the number of edges in the network.
unsigned int edges(){
return E.size();
|
df036f4c
David Mayerich
added the network...
|
262
263
|
}
|
7f27eafa
David Mayerich
simplified the st...
|
264
265
266
|
///Returns the number of nodes in the network.
unsigned int vertices(){
return V.size();
|
df036f4c
David Mayerich
added the network...
|
267
268
|
}
|
eceb6571
Jiaming Guo
fixed bug when sw...
|
269
|
///Returns the radius at specific point in the edge
|
5068402b
Jiaming Guo
add function to a...
|
270
|
T get_r(unsigned e, unsigned i) {
|
eceb6571
Jiaming Guo
fixed bug when sw...
|
271
272
273
|
return E[e].r(i);
}
|
b7624d0d
Jiaming Guo
add function to h...
|
274
|
///Returns the average radius of specific edge
|
5068402b
Jiaming Guo
add function to a...
|
275
|
T get_average_r(unsigned e) {
|
b7624d0d
Jiaming Guo
add function to h...
|
276
277
278
279
280
281
282
283
|
T result = 0.0;
unsigned n = E[e].size();
for (unsigned p = 0; p < n; p++)
result += E[e].r(p);
return (T)result / n;
}
|
eceb6571
Jiaming Guo
fixed bug when sw...
|
284
|
///Returns the length of current edge
|
5068402b
Jiaming Guo
add function to a...
|
285
|
T get_l(unsigned e) {
|
eceb6571
Jiaming Guo
fixed bug when sw...
|
286
287
288
289
|
return E[e].length();
}
///Returns the start vertex of current edge
|
b7624d0d
Jiaming Guo
add function to h...
|
290
|
size_t get_start_vertex(unsigned e) {
|
eceb6571
Jiaming Guo
fixed bug when sw...
|
291
292
293
294
|
return E[e].v[0];
}
///Returns the end vertex of current edge
|
b7624d0d
Jiaming Guo
add function to h...
|
295
|
size_t get_end_vertex(unsigned e) {
|
eceb6571
Jiaming Guo
fixed bug when sw...
|
296
297
298
|
return E[e].v[1];
}
|
b7624d0d
Jiaming Guo
add function to h...
|
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
///Returns one vertex
stim::vec3<T> get_vertex(unsigned i) {
return V[i];
}
///Returns the boundary vertices' indices
std::vector<unsigned> get_boundary_vertex() {
std::vector<unsigned> result;
for (unsigned v = 0; v < V.size(); v++) {
if (V[v].e[0].size() + V[v].e[1].size() == 1) { // boundary vertex
result.push_back(v);
}
}
return result;
}
|
d9547496
Jiaming Guo
add function to c...
|
317
|
///Set radius
|
5068402b
Jiaming Guo
add function to a...
|
318
|
void set_r(unsigned e, std::vector<T> radius) {
|
d9547496
Jiaming Guo
add function to c...
|
319
320
321
|
E[e].cylinder<T>::copy_r(radius);
}
|
5068402b
Jiaming Guo
add function to a...
|
322
323
324
|
void set_r(unsigned e, T radius) {
for (size_t i = 0; i < E[e].size(); i++)
E[e].cylinder<T>::set_r(i, radius);
|
d9547496
Jiaming Guo
add function to c...
|
325
|
}
|
87d0a1d2
David Mayerich
minor changes for...
|
326
327
328
|
//scale the network by some constant value
// I don't think these work??????
/*std::vector<vertex> operator*(T s){
|
fe0c7539
pranathivemuri
added functions t...
|
329
330
331
332
333
334
335
336
337
338
339
340
341
|
for (unsigned i=0; i< vertices; i ++ ){
V[i] = V[i] * s;
}
return V;
}
std::vector<vertex> operator*(vec<T> s){
for (unsigned i=0; i< vertices; i ++ ){
for (unsigned dim = 0 ; dim< 3; dim ++){
V[i][dim] = V[i][dim] * s[dim];
}
}
return V;
|
87d0a1d2
David Mayerich
minor changes for...
|
342
|
}*/
|
fe0c7539
pranathivemuri
added functions t...
|
343
|
|
abce2443
pranathivemuri
average branching...
|
344
|
// Returns an average of branching index in the network
|
abce2443
pranathivemuri
average branching...
|
345
346
347
348
349
350
351
352
353
354
|
double BranchingIndex(){
double B=0;
for(unsigned v=0; v < V.size(); v ++){
B += ((V[v].e[0].size()) + (V[v].e[1].size()));
}
B = B / V.size();
return B;
}
|
15a53122
pranathivemuri
Added other simpl...
|
355
|
// Returns number of branch points in thenetwork
|
15a53122
pranathivemuri
Added other simpl...
|
356
357
358
359
360
361
362
363
364
365
366
367
368
|
unsigned int BranchP(){
unsigned int B=0;
unsigned int c;
for(unsigned v=0; v < V.size(); v ++){
c = ((V[v].e[0].size()) + (V[v].e[1].size()));
if (c > 2){
B += 1;}
}
return B;
}
// Returns number of end points (tips) in thenetwork
|
15a53122
pranathivemuri
Added other simpl...
|
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
|
unsigned int EndP(){
unsigned int B=0;
unsigned int c;
for(unsigned v=0; v < V.size(); v ++){
c = ((V[v].e[0].size()) + (V[v].e[1].size()));
if (c == 1){
B += 1;}
}
return B;
}
//// Returns a dictionary with the key as the vertex
//std::map<std::vector<vertex>,unsigned int> DegreeDict(){
// std::map<std::vector<vertex>,unsigned int> dd;
// unsigned int c = 0;
// for(unsigned v=0; v < V.size(); v ++){
// c = ((V[v].e[0].size()) + (V[v].e[1].size()));
// dd[V[v]] = c;
// }
// return dd;
//}
//// Return number of branching stems
//unsigned int Stems(){
// unsigned int s = 0;
// std::map<std::vector<vertex>,unsigned int> dd;
// dd = DegreeDict();
// //for(unsigned v=0; v < V.size(); v ++){
// // V[v].e[0].
// return s;
//}
|
87d0a1d2
David Mayerich
minor changes for...
|
402
|
//Calculate Metrics---------------------------------------------------
|
fe0c7539
pranathivemuri
added functions t...
|
403
404
|
// Returns an average of fiber/edge lengths in the network
double Lengths(){
|
1b2858a2
David Mayerich
fixed some commen...
|
405
406
|
stim::vec<T> L;
double sumLength = 0;
|
fe0c7539
pranathivemuri
added functions t...
|
407
408
409
410
411
412
413
414
415
416
417
418
419
|
for(unsigned e = 0; e < E.size(); e++){ //for each edge in the network
L.push_back(E[e].length()); //append the edge length
sumLength = sumLength + E[e].length();
}
double avg = sumLength / E.size();
return avg;
}
// Returns an average of tortuosities in the network
double Tortuosities(){
stim::vec<T> t;
stim::vec<T> id1, id2; // starting and ending vertices of the edge
|
fe0c7539
pranathivemuri
added functions t...
|
420
421
422
423
424
|
double distance;double tortuosity;double sumTortuosity = 0;
for(unsigned e = 0; e < E.size(); e++){ //for each edge in the network
id1 = E[e][0]; //get the edge starting point
id2 = E[e][E[e].size() - 1]; //get the edge ending point
distance = (id1 - id2).len(); //displacement between the starting and ending points
|
fe0c7539
pranathivemuri
added functions t...
|
425
426
427
428
429
430
431
432
433
|
if(distance > 0){
tortuosity = E[e].length()/ distance ; // tortuoisty = edge length / edge displacement
}
else{
tortuosity = 0;}
t.push_back(tortuosity);
sumTortuosity += tortuosity;
}
double avg = sumTortuosity / E.size();
|
fe0c7539
pranathivemuri
added functions t...
|
434
435
436
|
return avg;
}
|
15a53122
pranathivemuri
Added other simpl...
|
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
// Returns average contraction of the network
double Contractions(){
stim::vec<T> t;
stim::vec<T> id1, id2; // starting and ending vertices of the edge
double distance;double contraction;double sumContraction = 0;
for(unsigned e = 0; e < E.size(); e++){ //for each edge in the network
id1 = E[e][0]; //get the edge starting point
id2 = E[e][E[e].size() - 1]; //get the edge ending point
distance = (id1 - id2).len(); //displacement between the starting and ending points
contraction = distance / E[e].length(); // tortuoisty = edge length / edge displacement
t.push_back(contraction);
sumContraction += contraction;
}
double avg = sumContraction / E.size();
return avg;
}
|
fe0c7539
pranathivemuri
added functions t...
|
453
|
|
15a53122
pranathivemuri
Added other simpl...
|
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
// returns average fractal dimension of the branches of the network
double FractalDimensions(){
stim::vec<T> t;
stim::vec<T> id1, id2; // starting and ending vertices of the edge
double distance;double fract;double sumFractDim = 0;
for(unsigned e = 0; e < E.size(); e++){ //for each edge in the network
id1 = E[e][0]; //get the edge starting point
id2 = E[e][E[e].size() - 1]; //get the edge ending point
distance = (id1 - id2).len(); //displacement between the starting and ending points
fract = std::log(distance) / std::log(E[e].length()); // tortuoisty = edge length / edge displacement
t.push_back(sumFractDim);
sumFractDim += fract;
}
double avg = sumFractDim / E.size();
return avg;
}
|
87d0a1d2
David Mayerich
minor changes for...
|
470
471
472
473
|
//returns a cylinder represented a given fiber (based on edge index)
stim::cylinder<T> get_cylinder(unsigned e){
return E[e]; //return the specified edge (casting it to a fiber)
|
3800c27f
David Mayerich
added code to sub...
|
474
475
|
}
|
7f27eafa
David Mayerich
simplified the st...
|
476
477
|
//load a network from an OBJ file
void load_obj(std::string filename){
|
e376212f
David Mayerich
added support for...
|
478
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
479
480
|
stim::obj<T> O; //create an OBJ object
O.load(filename); //load the OBJ file as an object
|
df036f4c
David Mayerich
added the network...
|
481
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
482
|
std::vector<unsigned> id2vert; //this list stores the OBJ vertex ID associated with each network vertex
|
df036f4c
David Mayerich
added the network...
|
483
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
484
|
unsigned i[2]; //temporary, IDs associated with the first and last points in an OBJ line
|
df036f4c
David Mayerich
added the network...
|
485
|
|
7f27eafa
David Mayerich
simplified the st...
|
486
487
|
//for each line in the OBJ object
for(unsigned int l = 1; l <= O.numL(); l++){
|
df036f4c
David Mayerich
added the network...
|
488
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
489
|
std::vector< stim::vec<T> > c; //allocate an array of points for the vessel centerline
|
7f27eafa
David Mayerich
simplified the st...
|
490
|
O.getLine(l, c); //get the fiber centerline
|
df036f4c
David Mayerich
added the network...
|
491
|
|
eb554b48
David Mayerich
bug fixes for Net...
|
492
|
stim::centerline<T> c3(c.size());
|
308a743c
David Mayerich
fixed class compa...
|
493
494
|
for(size_t j = 0; j < c.size(); j++)
c3[j] = c[j];
|
0d0ef1a9
David Mayerich
Adapted classes t...
|
495
|
c3.update();
|
308a743c
David Mayerich
fixed class compa...
|
496
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
497
498
499
500
501
|
// edge new_edge = c3; ///This is dangerous.
edge new_edge(c3);
//create an edge from the given centerline
unsigned int I = new_edge.size(); //calculate the number of points on the centerline
|
df036f4c
David Mayerich
added the network...
|
502
|
|
7f27eafa
David Mayerich
simplified the st...
|
503
|
//get the first and last vertex IDs for the line
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
504
|
std::vector< unsigned > id; //create an array to store the centerline point IDs
|
9c97e126
David Mayerich
added an axis-ali...
|
505
|
O.getLinei(l, id); //get the list of point IDs for the line
|
7f27eafa
David Mayerich
simplified the st...
|
506
507
|
i[0] = id.front(); //get the OBJ ID for the first element of the line
i[1] = id.back(); //get the OBJ ID for the last element of the line
|
df036f4c
David Mayerich
added the network...
|
508
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
509
|
std::vector<unsigned>::iterator it; //create an iterator for searching the id2vert array
|
7f27eafa
David Mayerich
simplified the st...
|
510
|
unsigned it_idx; //create an integer for the id2vert entry index
|
df036f4c
David Mayerich
added the network...
|
511
|
|
7f27eafa
David Mayerich
simplified the st...
|
512
|
//find out if the nodes for this fiber have already been created
|
7f27eafa
David Mayerich
simplified the st...
|
513
|
it = find(id2vert.begin(), id2vert.end(), i[0]); //look for the first node
|
7f27eafa
David Mayerich
simplified the st...
|
514
|
if(it == id2vert.end()){ //if i[0] hasn't already been used
|
4df9ec0e
pranathivemuri
compare networks ...
|
515
|
vertex new_vertex = new_edge[0]; //create a new vertex, assign it a position
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
|
bool flag = false;
unsigned j = 0;
for (; j < V.size(); j++) { // check whether current vertex is already exist
if (new_vertex == V[j]) {
flag = true;
break;
}
}
if (!flag) { // unique one
new_vertex.e[0].push_back(E.size()); //add the current edge as outgoing
new_edge.v[0] = V.size(); //add the new edge to the edge
V.push_back(new_vertex); //add the new vertex to the vertex list
id2vert.push_back(i[0]); //add the ID to the ID->vertex conversion list
}
else {
V[j].e[0].push_back(E.size());
new_edge.v[0] = j;
}
|
df036f4c
David Mayerich
added the network...
|
534
|
}
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
535
|
else{ //if the vertex already exists
|
15a53122
pranathivemuri
Added other simpl...
|
536
|
it_idx = std::distance(id2vert.begin(), it);
|
df480014
pranathivemuri
added in comments...
|
537
|
V[it_idx].e[0].push_back(E.size()); //add the current edge as outgoing
|
3800c27f
David Mayerich
added code to sub...
|
538
|
new_edge.v[0] = it_idx;
|
df036f4c
David Mayerich
added the network...
|
539
540
|
}
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
541
|
it = find(id2vert.begin(), id2vert.end(), i[1]); //look for the second ID
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
542
543
|
if(it == id2vert.end()){ //if i[1] hasn't already been used
vertex new_vertex = new_edge[I-1]; //create a new vertex, assign it a position
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
bool flag = false;
unsigned j = 0;
for (; j < V.size(); j++) { // check whether current vertex is already exist
if (new_vertex == V[j]) {
flag = true;
break;
}
}
if (!flag) {
new_vertex.e[1].push_back(E.size()); //add the current edge as incoming
new_edge.v[1] = V.size(); //add the new vertex to the edge
V.push_back(new_vertex); //add the new vertex to the vertex list
id2vert.push_back(i[1]); //add the ID to the ID->vertex conversion list
}
else {
V[j].e[1].push_back(E.size());
new_edge.v[1] = j;
}
|
df036f4c
David Mayerich
added the network...
|
562
|
}
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
563
|
else{ //if the vertex already exists
|
15a53122
pranathivemuri
Added other simpl...
|
564
|
it_idx = std::distance(id2vert.begin(), it);
|
df480014
pranathivemuri
added in comments...
|
565
|
V[it_idx].e[1].push_back(E.size()); //add the current edge as incoming
|
3800c27f
David Mayerich
added code to sub...
|
566
|
new_edge.v[1] = it_idx;
|
df036f4c
David Mayerich
added the network...
|
567
|
}
|
9c97e126
David Mayerich
added an axis-ali...
|
568
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
569
|
E.push_back(new_edge); //push the edge to the list
|
df036f4c
David Mayerich
added the network...
|
570
|
|
df036f4c
David Mayerich
added the network...
|
571
|
}
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
// copy the radii information from OBJ
/*if (O.numVT()) {
unsigned k = 0;
for (unsigned i = 0; i < E.size(); i++) {
for (unsigned j = 0; j < E[i].size(); j++) {
E[i].cylinder<T>::set_r(j, O.getVT(k)[0] / 2);
k++;
}
}
}*/
// OBJ class assumes that in L the two values are equal
if (O.numVT()) {
std::vector< unsigned > id; //create an array to store the centerline point IDs
for (unsigned i = 0; i < O.numL(); i++) {
id.clear();
O.getLinei(i + 1, id); //get the list of point IDs for the line
for (unsigned j = 0; j < id.size(); j++)
E[i].cylinder<T>::set_r(j, O.getVT(id[j] - 1)[0] / 2);
}
}
|
e376212f
David Mayerich
added support for...
|
593
|
}
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
594
|
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
595
|
///loads a .nwt file. Reads the header and loads the data into the network according to the header.
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
596
|
void
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
597
598
|
loadNwt(std::string filename)
{
|
029c021a
Pavel Govyadinov
filled in the res...
|
599
600
601
|
int dims[2]; ///number of vertex, number of edges
readHeader(filename, &dims[0]); //read header
std::ifstream file;
|
296aa37d
Pavel Govyadinov
fixed parsing errors
|
602
|
file.open(filename.c_str(), std::ios::in | std::ios::binary); ///skip header information.
|
029c021a
Pavel Govyadinov
filled in the res...
|
603
604
605
606
607
608
609
610
|
file.seekg(14+58+4+4, file.beg);
vertex v;
for(int i = 0; i < dims[0]; i++) ///for every vertex, read vertex, add to network.
{
file >> v;
V.push_back(v);
// std::cout << i << " " << v.str() << std::endl;
}
|
e376212f
David Mayerich
added support for...
|
611
|
|
029c021a
Pavel Govyadinov
filled in the res...
|
612
613
614
615
616
617
|
std::cout << std::endl;
for(int i = 0; i < dims[1]; i++) ///for every edge, read edge, add to network.
{
edge e;
file >> e;
E.push_back(e);
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
618
|
//std::cout << i << " " << E[i].str() << std::endl; // not necessary?
|
029c021a
Pavel Govyadinov
filled in the res...
|
619
620
|
}
file.close();
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
621
622
623
|
}
///saves a .nwt file. Writes the header in raw text format, then saves the network as a binary file.
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
624
625
626
|
void
saveNwt(std::string filename)
{
|
029c021a
Pavel Govyadinov
filled in the res...
|
627
|
writeHeader(filename);
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
628
|
std::ofstream file;
|
296aa37d
Pavel Govyadinov
fixed parsing errors
|
629
|
file.open(filename.c_str(), std::ios::out | std::ios::binary | std::ios::app); ///since we have written the header we are not appending.
|
029c021a
Pavel Govyadinov
filled in the res...
|
630
|
for(int i = 0; i < V.size(); i++) ///look through the Vertices and write each one.
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
631
|
{
|
029c021a
Pavel Govyadinov
filled in the res...
|
632
|
// std::cout << i << " " << V[i].str() << std::endl;
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
633
634
|
file << V[i];
}
|
029c021a
Pavel Govyadinov
filled in the res...
|
635
|
for(int i = 0; i < E.size(); i++) ///loop through the Edges and write each one.
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
636
|
{
|
0e0feff0
Jiaming Guo
fixed bugs in mer...
|
637
|
//std::cout << i << " " << E[i].str() << std::endl; // not necesarry?
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
638
639
|
file << E[i];
}
|
029c021a
Pavel Govyadinov
filled in the res...
|
640
|
file.close();
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
641
642
|
}
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
643
644
|
///Writes the header information to a .nwt file.
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
645
646
647
|
void
writeHeader(std::string filename)
{
|
029c021a
Pavel Govyadinov
filled in the res...
|
648
649
650
651
652
|
std::string magicString = "nwtFileFormat "; ///identifier for the file.
std::string desc = "fileid(14B), desc(58B), #vertices(4B), #edges(4B): bindata";
int hNumVertices = V.size(); ///int byte header storing the number of vertices in the file
int hNumEdges = E.size(); ///int byte header storing the number of edges.
std::ofstream file;
|
296aa37d
Pavel Govyadinov
fixed parsing errors
|
653
|
file.open(filename.c_str(), std::ios::out | std::ios::binary);
|
029c021a
Pavel Govyadinov
filled in the res...
|
654
655
656
657
658
659
660
|
std::cout << hNumVertices << " " << hNumEdges << std::endl;
file.write(reinterpret_cast<const char*>(&magicString.c_str()[0]), 14); //write the file id
file.write(reinterpret_cast<const char*>(&desc.c_str()[0]), 58); //write the description
file.write(reinterpret_cast<const char*>(&hNumVertices), sizeof(int)); //write #vert.
file.write(reinterpret_cast<const char*>(&hNumEdges), sizeof(int)); //write #edges
// file << magicString.c_str() << desc.c_str() << hNumVertices << hNumEdges;
file.close();
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
661
662
|
}
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
663
664
665
|
///Reads the header information from a .nwt file.
void
|
029c021a
Pavel Govyadinov
filled in the res...
|
666
|
readHeader(std::string filename, int *dims)
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
667
|
{
|
029c021a
Pavel Govyadinov
filled in the res...
|
668
669
670
671
672
|
char magicString[14]; ///id
char desc[58]; ///description
int hNumVertices; ///#vert
int hNumEdges; ///#edges
std::ifstream file; ////create stream
|
296aa37d
Pavel Govyadinov
fixed parsing errors
|
673
|
file.open(filename.c_str(), std::ios::in | std::ios::binary);
|
029c021a
Pavel Govyadinov
filled in the res...
|
674
675
676
677
678
679
680
681
|
file.read(reinterpret_cast<char*>(&magicString[0]), 14); ///read the file id.
file.read(reinterpret_cast<char*>(&desc[0]), 58); ///read the description
file.read(reinterpret_cast<char*>(&hNumVertices), sizeof(int)); ///read the number of vertices
file.read(reinterpret_cast<char*>(&hNumEdges), sizeof(int)); ///read the number of edges
// std::cout << magicString << desc << hNumVertices << " " << hNumEdges << std::endl;
file.close(); ///close the file.
dims[0] = hNumVertices; ///fill the returned reference.
dims[1] = hNumEdges;
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
682
683
|
}
|
58674036
Jiaming Guo
fix minor error f...
|
684
|
//load a network from an SWC file
|
1599ae65
Jiaming Guo
add swc.h and fun...
|
685
686
687
688
|
void load_swc(std::string filename) {
stim::swc<T> S; // create swc variable
S.load(filename); // load the node information
S.create_tree(); // link those node according to their linking relationships as a tree
|
2b28db3e
Jiaming Guo
better render
|
689
|
S.resample();
|
1599ae65
Jiaming Guo
add swc.h and fun...
|
690
|
|
30b20d4f
Jiaming Guo
make it render be...
|
691
|
//NT.push_back(S.node[0].type); // set the neuronal_type value to the first vertex in the network
|
1599ae65
Jiaming Guo
add swc.h and fun...
|
692
693
|
std::vector<unsigned> id2vert; // this list stores the SWC vertex ID associated with each network vertex
unsigned i[2]; // temporary, IDs associated with the first and last points
|
2b28db3e
Jiaming Guo
better render
|
694
695
|
for (unsigned int l = 0; l < S.numE(); l++) { // for every edge
|
30b20d4f
Jiaming Guo
make it render be...
|
696
|
//NT.push_back(S.node[l].type);
|
2b28db3e
Jiaming Guo
better render
|
697
698
699
700
701
702
703
704
|
std::vector< stim::vec3<T> > c;
S.get_points(l, c);
stim::centerline<T> c3(c.size()); // new fiber
for (unsigned j = 0; j < c.size(); j++)
c3[j] = c[j]; // copy the points
|
1599ae65
Jiaming Guo
add swc.h and fun...
|
705
706
|
c3.update(); // update the L information
|
58674036
Jiaming Guo
fix minor error f...
|
707
708
709
710
|
stim::cylinder<T> C3(c3); // create a new cylinder in order to copy the origin radius information
// upadate the R information
std::vector<T> radius;
|
2b28db3e
Jiaming Guo
better render
|
711
712
|
S.get_radius(l, radius);
|
58674036
Jiaming Guo
fix minor error f...
|
713
714
|
C3.copy_r(radius);
|
2b28db3e
Jiaming Guo
better render
|
715
716
717
718
|
edge new_edge(C3); // new edge
//create an edge from the given centerline
unsigned int I = new_edge.size(); //calculate the number of points on the centerline
|
58674036
Jiaming Guo
fix minor error f...
|
719
|
|
1599ae65
Jiaming Guo
add swc.h and fun...
|
720
|
//get the first and last vertex IDs for the line
|
2b28db3e
Jiaming Guo
better render
|
721
722
|
i[0] = S.E[l].front();
i[1] = S.E[l].back();
|
1599ae65
Jiaming Guo
add swc.h and fun...
|
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
std::vector<unsigned>::iterator it; //create an iterator for searching the id2vert array
unsigned it_idx; //create an integer for the id2vert entry index
//find out if the nodes for this fiber have already been created
it = find(id2vert.begin(), id2vert.end(), i[0]); //look for the first node
if (it == id2vert.end()) { //if i[0] hasn't already been used
vertex new_vertex = new_edge[0]; //create a new vertex, assign it a position
new_vertex.e[0].push_back(E.size()); //add the current edge as outgoing
new_edge.v[0] = V.size(); //add the new edge to the edge
V.push_back(new_vertex); //add the new vertex to the vertex list
id2vert.push_back(i[0]); //add the ID to the ID->vertex conversion list
}
else { //if the vertex already exists
it_idx = std::distance(id2vert.begin(), it);
V[it_idx].e[0].push_back(E.size()); //add the current edge as outgoing
new_edge.v[0] = it_idx;
}
it = find(id2vert.begin(), id2vert.end(), i[1]); //look for the second ID
if (it == id2vert.end()) { //if i[1] hasn't already been used
|
2b28db3e
Jiaming Guo
better render
|
744
|
vertex new_vertex = new_edge[I - 1]; //create a new vertex, assign it a position
|
1599ae65
Jiaming Guo
add swc.h and fun...
|
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
|
new_vertex.e[1].push_back(E.size()); //add the current edge as incoming
new_edge.v[1] = V.size(); //add the new vertex to the edge
V.push_back(new_vertex); //add the new vertex to the vertex list
id2vert.push_back(i[1]); //add the ID to the ID->vertex conversion list
}
else { //if the vertex already exists
it_idx = std::distance(id2vert.begin(), it);
V[it_idx].e[1].push_back(E.size()); //add the current edge as incoming
new_edge.v[1] = it_idx;
}
E.push_back(new_edge); //push the edge to the list
}
}
|
26e84a59
Jiaming Guo
add adjacency_mat...
|
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
|
/// Get adjacency matrix of the network
std::vector< typename std::vector<int> > get_adj_mat() {
unsigned n = V.size(); // get the number of vertices in the networks
std::vector< typename std::vector<int> > result(n, std::vector<int>(n, 0)); // initialize every entry in the matrix to be 0
result.resize(n); // resize rows
for (unsigned i = 0; i < n; i++)
result[i].resize(n); // resize columns
for (unsigned i = 0; i < n; i++) { // for every vertex
unsigned num_out = V[i].e[0].size(); // number of outgoing edges of current vertex
if (num_out != 0) {
for (unsigned j = 0; j < num_out; j++) {
int edge_idx = V[i].e[0][j]; // get the jth out-going edge index of current vertex
int vertex_idx = E[edge_idx].v[1]; // get the ending vertex of specific out-going edge
result[i][vertex_idx] = 1; // can simply set to 1 if it is simple-graph
result[vertex_idx][i] = 1; // symmetric
}
}
}
return result;
}
|
7f27eafa
David Mayerich
simplified the st...
|
785
786
|
/// Output the network as a string
std::string str(){
|
e376212f
David Mayerich
added support for...
|
787
|
|
7f27eafa
David Mayerich
simplified the st...
|
788
789
790
791
|
std::stringstream ss;
ss<<"Nodes ("<<V.size()<<")--------"<<std::endl;
for(unsigned int v = 0; v < V.size(); v++){
ss<<"\t"<<v<<V[v].str()<<std::endl;
|
e376212f
David Mayerich
added support for...
|
792
793
|
}
|
7f27eafa
David Mayerich
simplified the st...
|
794
795
796
|
ss<<"Edges ("<<E.size()<<")--------"<<std::endl;
for(unsigned e = 0; e < E.size(); e++){
ss<<"\t"<<e<<E[e].str()<<std::endl;
|
df036f4c
David Mayerich
added the network...
|
797
798
|
}
|
7f27eafa
David Mayerich
simplified the st...
|
799
|
return ss.str();
|
df036f4c
David Mayerich
added the network...
|
800
|
}
|
3800c27f
David Mayerich
added code to sub...
|
801
802
|
/// This function resamples all fibers in a network given a desired minimum spacing
|
9c97e126
David Mayerich
added an axis-ali...
|
803
|
/// @param spacing is the minimum distance between two points on the network
|
3800c27f
David Mayerich
added code to sub...
|
804
|
stim::network<T> resample(T spacing){
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
805
806
|
stim::network<T> n; //create a new network that will be an exact copy, with resampled fibers
n.V = V; //copy all vertices
|
30b20d4f
Jiaming Guo
make it render be...
|
807
|
//n.NT = NT; //copy all the neuronal type information
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
808
|
n.E.resize(edges()); //allocate space for the edge list
|
3800c27f
David Mayerich
added code to sub...
|
809
810
|
//copy all fibers, resampling them in the process
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
811
812
|
for(unsigned e = 0; e < edges(); e++){ //for each edge in the edge list
n.E[e] = E[e].resample(spacing); //resample the edge and copy it to the new network
|
3800c27f
David Mayerich
added code to sub...
|
813
814
|
}
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
815
|
return n; //return the resampled network
|
4df9ec0e
pranathivemuri
compare networks ...
|
816
817
|
}
|
9c97e126
David Mayerich
added an axis-ali...
|
818
|
/// Calculate the total number of points on all edges.
|
797f8ab0
David Mayerich
cleaned up the st...
|
819
820
821
822
|
unsigned total_points(){
unsigned n = 0;
for(unsigned e = 0; e < E.size(); e++)
n += E[e].size();
|
4df9ec0e
pranathivemuri
compare networks ...
|
823
|
return n;
|
797f8ab0
David Mayerich
cleaned up the st...
|
824
|
}
|
4df9ec0e
pranathivemuri
compare networks ...
|
825
|
|
fcd2eb7c
David Mayerich
added support for...
|
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
|
//Copy the point cloud representing the centerline for the network into an array
void centerline_cloud(T* dst) {
size_t p; //stores the current edge point
size_t P; //stores the number of points in an edge
size_t i = 0; //index into the output array of points
for (size_t e = 0; e < E.size(); e++) { //for each edge in the network
P = E[e].size(); //get the number of points in this edge
for (p = 0; p < P; p++) {
dst[i * 3 + 0] = E[e][p][0];
dst[i * 3 + 1] = E[e][p][1];
dst[i * 3 + 2] = E[e][p][2];
i++;
}
}
}
|
fbbc07be
Jiaming Guo
replace ANN by kd...
|
842
843
|
// convert vec3 to array
void stim2array(float *a, stim::vec3<T> b){
|
797f8ab0
David Mayerich
cleaned up the st...
|
844
845
846
847
848
|
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
}
|
c72184d1
Jiaming Guo
add many function...
|
849
|
// convert vec3 to array in bunch
|
c9712fb4
Jiaming Guo
fix minor error w...
|
850
|
void edge2array(T* a, edge b){
|
c72184d1
Jiaming Guo
add many function...
|
851
852
853
854
855
856
857
858
|
size_t n = b.size();
for(size_t i = 0; i < n; i++){
a[i * 3 + 0] = b[i][0];
a[i * 3 + 1] = b[i][1];
a[i * 3 + 2] = b[i][2];
}
}
|
5068402b
Jiaming Guo
add function to a...
|
859
860
861
862
863
864
865
866
867
868
869
870
871
|
// get list of metric
std::vector<T> metric() {
std::vector<T> result;
T m;
for (size_t e = 0; e < E.size(); e++) {
for (size_t p = 0; p < E[e].size(); p++) {
m = E[e].r(p);
result.push_back(m);
}
}
return result;
}
|
9c97e126
David Mayerich
added an axis-ali...
|
872
873
874
875
|
/// Calculate the average magnitude across the entire network.
/// @param m is the magnitude value to use. The default is 0 (usually radius).
T average(unsigned m = 0){
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
876
877
878
|
T M, L; //allocate space for the total magnitude and length
M = L = 0; //initialize both the initial magnitude and length to zero
for(unsigned e = 0; e < E.size(); e++){ //for each edge in the network
|
c72184d1
Jiaming Guo
add many function...
|
879
|
M += E[e].integrate(); //get the integrated magnitude
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
880
|
L += E[e].length(); //get the edge length
|
9c97e126
David Mayerich
added an axis-ali...
|
881
|
}
|
4df9ec0e
pranathivemuri
compare networks ...
|
882
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
883
|
return M / L; //return the average magnitude
|
9c97e126
David Mayerich
added an axis-ali...
|
884
885
886
|
}
/// This function compares two networks and returns the percentage of the current network that is missing from A.
|
9c97e126
David Mayerich
added an axis-ali...
|
887
888
|
/// @param A is the network to compare to - the field is generated for A
/// @param sigma is the user-defined tolerance value - smaller values provide a stricter comparison
|
eb554b48
David Mayerich
bug fixes for Net...
|
889
|
stim::network<T> compare(stim::network<T> A, float sigma, int device = -1){
|
9c97e126
David Mayerich
added an axis-ali...
|
890
|
|
fcd2eb7c
David Mayerich
added support for...
|
891
892
|
stim::network<T> R; //generate a network storing the result of the comparison
R = (*this); //initialize the result with the current network
|
9c97e126
David Mayerich
added an axis-ali...
|
893
|
|
fcd2eb7c
David Mayerich
added support for...
|
894
|
T *c; // centerline (array of double pointers) - points on kdtree must be double
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
895
|
size_t n_data = A.total_points(); // set the number of points
|
c9712fb4
Jiaming Guo
fix minor error w...
|
896
|
c = (T*) malloc(sizeof(T) * n_data * 3); // allocate an array to store all points in the data set
|
9c97e126
David Mayerich
added an axis-ali...
|
897
|
|
797f8ab0
David Mayerich
cleaned up the st...
|
898
|
unsigned t = 0;
|
fcd2eb7c
David Mayerich
added support for...
|
899
900
|
for(unsigned e = 0; e < A.E.size(); e++){ //for each edge in the network
for(unsigned p = 0; p < A.E[e].size(); p++){ //for each point in the edge
|
797f8ab0
David Mayerich
cleaned up the st...
|
901
902
|
for(unsigned d = 0; d < 3; d++){ //for each coordinate
|
fcd2eb7c
David Mayerich
added support for...
|
903
|
c[t * 3 + d] = A.E[e][p][d]; //copy the point into the array c
|
4df9ec0e
pranathivemuri
compare networks ...
|
904
|
}
|
797f8ab0
David Mayerich
cleaned up the st...
|
905
|
t++;
|
4df9ec0e
pranathivemuri
compare networks ...
|
906
|
}
|
797f8ab0
David Mayerich
cleaned up the st...
|
907
908
|
}
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
909
|
//generate a KD-tree for network A
|
fcd2eb7c
David Mayerich
added support for...
|
910
|
size_t MaxTreeLevels = 3; // max tree level
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
911
912
913
|
#ifdef __CUDACC__
cudaSetDevice(device);
|
c72184d1
Jiaming Guo
add many function...
|
914
915
|
stim::kdtree<T, 3> kdt; // initialize a pointer to a kd tree
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
916
|
kdt.create(c, n_data, MaxTreeLevels); // build a KD tree
|
797f8ab0
David Mayerich
cleaned up the st...
|
917
|
|
9c97e126
David Mayerich
added an axis-ali...
|
918
|
for(unsigned e = 0; e < R.E.size(); e++){ //for each edge in A
|
0d0ef1a9
David Mayerich
Adapted classes t...
|
919
920
|
//R.E[e].add_mag(0); //add a new magnitude for the metric
//size_t errormag_id = R.E[e].nmags() - 1; //get the id for the new magnitude
|
c72184d1
Jiaming Guo
add many function...
|
921
922
|
size_t n = R.E[e].size(); // the number of points in current edge
|
c9712fb4
Jiaming Guo
fix minor error w...
|
923
|
T* queryPt = new T[3 * n];
|
c72184d1
Jiaming Guo
add many function...
|
924
925
926
|
T* m1 = new T[n];
T* dists = new T[n];
size_t* nnIdx = new size_t[n];
|
797f8ab0
David Mayerich
cleaned up the st...
|
927
|
|
c72184d1
Jiaming Guo
add many function...
|
928
929
930
931
|
T* d_dists;
T* d_m1;
cudaMalloc((void**)&d_dists, n * sizeof(T));
cudaMalloc((void**)&d_m1, n * sizeof(T));
|
797f8ab0
David Mayerich
cleaned up the st...
|
932
|
|
c72184d1
Jiaming Guo
add many function...
|
933
934
935
936
937
938
939
940
941
|
edge2array(queryPt, R.E[e]);
kdt.search(queryPt, n, nnIdx, dists);
cudaMemcpy(d_dists, dists, n * sizeof(T), cudaMemcpyHostToDevice); // copy dists from host to device
// configuration parameters
size_t threads = (1024>n)?n:1024;
size_t blocks = n/threads + (n%threads)?1:0;
|
5068402b
Jiaming Guo
add function to a...
|
942
|
find_metric_parallel<<<blocks, threads>>>(d_m1, n, d_dists, sigma); //calculate the metric value based on the distance
|
c72184d1
Jiaming Guo
add many function...
|
943
944
945
946
|
cudaMemcpy(m1, d_m1, n * sizeof(T), cudaMemcpyDeviceToHost);
for(unsigned p = 0; p < n; p++){
|
0d0ef1a9
David Mayerich
Adapted classes t...
|
947
|
R.E[e].set_r(p, m1[p]);
|
c72184d1
Jiaming Guo
add many function...
|
948
949
950
951
952
953
954
955
956
957
|
}
//d_set_mag<<<blocks, threads>>>(R.E[e].M, errormag_id, n, m1);
}
#else
stim::kdtree<T, 3> kdt;
kdt.create(c, n_data, MaxTreeLevels);
for(unsigned e = 0; e < R.E.size(); e++){ //for each edge in A
|
c72184d1
Jiaming Guo
add many function...
|
958
959
|
size_t n = R.E[e].size(); // the number of points in current edge
|
c9712fb4
Jiaming Guo
fix minor error w...
|
960
|
T* query = new T[3 * n];
|
c72184d1
Jiaming Guo
add many function...
|
961
962
|
T* m1 = new T[n];
T* dists = new T[n];
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
963
|
size_t* nnIdx = new size_t[n];
|
c72184d1
Jiaming Guo
add many function...
|
964
|
|
5068402b
Jiaming Guo
add function to a...
|
965
|
edge2array(query, R.E[e]);
|
c72184d1
Jiaming Guo
add many function...
|
966
|
|
5068402b
Jiaming Guo
add function to a...
|
967
|
kdt.cpu_search(query, n, nnIdx, dists); //find the distance between A and the current network
|
c72184d1
Jiaming Guo
add many function...
|
968
969
|
for(unsigned p = 0; p < R.E[e].size(); p++){
|
2b28db3e
Jiaming Guo
better render
|
970
971
|
m1[p] = 1.0f - gaussianFunction((T)dists[p], sigma); //calculate the metric value based on the distance
R.E[e].set_r(p, m1[p]); //set the error for the second point in the segment
|
c72184d1
Jiaming Guo
add many function...
|
972
973
974
975
976
977
978
979
980
981
982
|
}
}
#endif
return R; //return the resulting network
}
/// This function compares two networks and split the current one according to the nearest neighbor of each point in each edge
/// @param A is the network to split
/// @param B is the corresponding mapping network
/// @param sigma is the user-defined tolerance value - smaller values provide a stricter comparison
/// @param device is the device that user want to use
|
6b317c71
Jiaming Guo
add function to r...
|
983
|
void split(stim::network<T> A, stim::network<T> B, float sigma, int device, float threshold){
|
c72184d1
Jiaming Guo
add many function...
|
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
|
T *c;
size_t n_data = B.total_points();
c = (T*) malloc(sizeof(T) * n_data * 3);
size_t NB = B.E.size(); // the number of edges in B
unsigned t = 0;
for(unsigned e = 0; e < NB; e++){ // for every edge in B
for(unsigned p = 0; p < B.E[e].size(); p++){ // for every points in B.E[e]
for(unsigned d = 0; d < 3; d++){
c[t * 3 + d] = B.E[e][p][d]; // convert to array
}
t++;
}
}
size_t MaxTreeLevels = 3; // max tree level
#ifdef __CUDACC__
cudaSetDevice(device);
stim::kdtree<T, 3> kdt; // initialize a pointer to a kd tree
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
1005
|
|
9c97e126
David Mayerich
added an axis-ali...
|
1006
|
//compare each point in the current network to the field produced by A
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
1007
|
kdt.create(c, n_data, MaxTreeLevels); // build a KD tree
|
797f8ab0
David Mayerich
cleaned up the st...
|
1008
|
|
ee17b90b
Jiaming Guo
make it compatibl...
|
1009
|
std::vector<std::vector<unsigned> > relation; // the relationship between GT and T corresponding to NN
|
c72184d1
Jiaming Guo
add many function...
|
1010
1011
1012
|
relation.resize(A.E.size());
for(unsigned e = 0; e < A.E.size(); e++){ //for each edge in A
|
0d0ef1a9
David Mayerich
Adapted classes t...
|
1013
1014
|
//A.E[e].add_mag(0); //add a new magnitude for the metric
//size_t errormag_id = A.E[e].nmags() - 1;
|
c72184d1
Jiaming Guo
add many function...
|
1015
1016
1017
|
size_t n = A.E[e].size(); // the number of edges in A
|
c9712fb4
Jiaming Guo
fix minor error w...
|
1018
|
T* queryPt = new T[3 * n]; // set of all the points in current edge
|
c72184d1
Jiaming Guo
add many function...
|
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
|
T* m1 = new T[n]; // array of metrics for every point in current edge
T* dists = new T[n]; // store the distances for every point in current edge
size_t* nnIdx = new size_t[n]; // store the indices for every point in current edge
// define pointers in device
T* d_dists;
T* d_m1;
size_t* d_nnIdx;
// allocate memory for defined pointers
cudaMalloc((void**)&d_dists, n * sizeof(T));
cudaMalloc((void**)&d_m1, n * sizeof(T));
cudaMalloc((void**)&d_nnIdx, n * sizeof(size_t));
edge2array(queryPt, A.E[e]); // convert edge to array
kdt.search(queryPt, n, nnIdx, dists); // search the tree to find the NN for every point in current edge
cudaMemcpy(d_dists, dists, n * sizeof(T), cudaMemcpyHostToDevice); // copy dists from host to device
cudaMemcpy(d_nnIdx, nnIdx, n * sizeof(size_t), cudaMemcpyHostToDevice); // copy Idx from host to device
// configuration parameters
size_t threads = (1024>n)?n:1024; // test to see whether the number of point in current edge is more than 1024
size_t blocks = n/threads + (n%threads)?1:0;
|
5068402b
Jiaming Guo
add function to a...
|
1043
|
find_metric_parallel<<<blocks, threads>>>(d_m1, n, d_dists, sigma); // calculate the metrics in parallel
|
c72184d1
Jiaming Guo
add many function...
|
1044
1045
1046
1047
|
cudaMemcpy(m1, d_m1, n * sizeof(T), cudaMemcpyDeviceToHost);
for(unsigned p = 0; p < n; p++){
|
0d0ef1a9
David Mayerich
Adapted classes t...
|
1048
|
A.E[e].set_r(p, m1[p]); // set the error(radius) value to every point in current edge
|
c72184d1
Jiaming Guo
add many function...
|
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
|
}
relation[e].resize(n); // resize every edge relation size
unsigned* d_relation;
cudaMalloc((void**)&d_relation, n * sizeof(unsigned)); // allocate memory
std::vector<size_t> edge_point_num(NB); // %th element is the number of points that %th edge has
for(unsigned ee = 0; ee < NB; ee++)
edge_point_num[ee] = B.E[ee].size();
size_t* d_edge_point_num;
cudaMalloc((void**)&d_edge_point_num, NB * sizeof(size_t));
cudaMemcpy(d_edge_point_num, &edge_point_num[0], NB * sizeof(size_t), cudaMemcpyHostToDevice);
|
5068402b
Jiaming Guo
add function to a...
|
1064
|
find_edge_index_parallel<<<blocks, threads>>>(d_nnIdx, n, d_relation, d_edge_point_num, NB); // find the edge corresponding to the array index in parallel
|
c72184d1
Jiaming Guo
add many function...
|
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
|
cudaMemcpy(&relation[e][0], d_relation, n * sizeof(unsigned), cudaMemcpyDeviceToHost); //copy relationship from device to host
}
#else
stim::kdtree<T, 3> kdt;
kdt.create(c, n_data, MaxTreeLevels);
std::vector<std::vector<unsigned>> relation; // the mapping relationship between two networks
relation.resize(A.E.size());
for(unsigned i = 0; i < A.E.size(); i++)
relation[i].resize(A.E[i].size());
std::vector<size_t> edge_point_num(NB); //%th element is the number of points that %th edge has
for(unsigned ee = 0; ee < NB; ee++)
edge_point_num[ee] = B.E[ee].size();
|
c72184d1
Jiaming Guo
add many function...
|
1081
|
for(unsigned e = 0; e < A.E.size(); e++){ //for each edge in A
|
2b28db3e
Jiaming Guo
better render
|
1082
1083
|
size_t n = A.E[e].size(); //the number of edges in A
|
c72184d1
Jiaming Guo
add many function...
|
1084
|
|
c9712fb4
Jiaming Guo
fix minor error w...
|
1085
|
T* queryPt = new T[3 * n];
|
c72184d1
Jiaming Guo
add many function...
|
1086
1087
1088
1089
1090
1091
1092
1093
1094
|
T* m1 = new T[n];
T* dists = new T[n]; //store the distances
size_t* nnIdx = new size_t[n]; //store the indices
edge2array(queryPt, A.E[e]);
kdt.search(queryPt, n, nnIdx, dists);
for(unsigned p = 0; p < A.E[e].size(); p++){
m1[p] = 1.0f - gaussianFunction((T)dists[p], sigma); //calculate the metric value based on the distance
|
2b28db3e
Jiaming Guo
better render
|
1095
|
A.E[e].set_r(p, m1[p]); //set the error for the second point in the segment
|
c72184d1
Jiaming Guo
add many function...
|
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
|
unsigned id = 0; //mapping edge's idx
size_t num = 0; //total number of points before #th edge
for(unsigned i = 0; i < NB; i++){
num += B.E[i].size();
if(nnIdx[p] < num){ //find the edge it belongs to
relation[e][p] = id;
break;
}
id++; //current edge won't be the one, move to next edge
}
}
}
#endif
E = A.E;
V = A.V;
unsigned int id = 0; // split value
for(unsigned e = 0; e < E.size(); e++){ // for every edge
for(unsigned p = 0; p < E[e].size() - 1; p++){ // for every point in each edge
|
470ab611
David Mayerich
started fixing wa...
|
1116
|
int t = (int)(E[e].length() / sigma * 2);
|
62d95dcd
Jiaming Guo
fixed resample bugs
|
1117
1118
1119
1120
|
if (t <= 20)
threshold_fac = E[e].size();
else
threshold_fac = (E[e].length() / sigma * 2)/10;
|
c72184d1
Jiaming Guo
add many function...
|
1121
1122
|
if(relation[e][p] != relation[e][p + 1]){ // find the nearest edge changing point
id = p + 1; // if there is no change in NN
|
62d95dcd
Jiaming Guo
fixed resample bugs
|
1123
|
if(id < threshold_fac || (E[e].size() - id) < threshold_fac)
|
c72184d1
Jiaming Guo
add many function...
|
1124
1125
1126
1127
1128
|
id = E[e].size() - 1; // extreme situation is not acceptable
else
break;
}
if(p == E[e].size() - 2) // if there is no splitting index, set the id to the last point index of current edge
|
62d95dcd
Jiaming Guo
fixed resample bugs
|
1129
|
id = E[e].size() - 1;
|
c72184d1
Jiaming Guo
add many function...
|
1130
|
}
|
0d0ef1a9
David Mayerich
Adapted classes t...
|
1131
|
//unsigned errormag_id = E[e].nmags() - 1;
|
c72184d1
Jiaming Guo
add many function...
|
1132
1133
|
T G = 0; // test to see whether it has its nearest neighbor
for(unsigned i = 0; i < E[e].size(); i++)
|
0d0ef1a9
David Mayerich
Adapted classes t...
|
1134
|
G += E[e].r(i); // won't split special edges
|
6b317c71
Jiaming Guo
add function to r...
|
1135
|
if(G / E[e].size() > threshold) // should based on the color map
|
c72184d1
Jiaming Guo
add many function...
|
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
|
id = E[e].size() - 1; // set split idx to outgoing direction vertex
std::vector<edge> tmpe;
tmpe.resize(2);
tmpe = E[e].split(id);
vertex tmpv = stim::vec3<T>(-1, -1, 0); // store the split point as vertex
if(tmpe.size() == 2){
relation.resize(relation.size() + 1);
for(unsigned d = id; d < E[e].size(); d++)
relation[relation.size() - 1].push_back(relation[e][d]);
tmpe[0].v[0] = E[e].v[0]; // begining vertex of first half edge -> original begining vertex
tmpe[1].v[1] = E[e].v[1]; // ending vertex of second half edge -> original ending vertex
tmpv = E[e][id];
V.push_back(tmpv);
tmpe[0].v[1] = (unsigned)V.size() - 1; // ending vertex of first half edge -> new vertex
tmpe[1].v[0] = (unsigned)V.size() - 1; // begining vertex of second half edge -> new vertex
edge tmp(E[e]);
E[e] = tmpe[0]; // replace original edge by first half edge
E.push_back(tmpe[1]); // push second half edge to the last
V[V.size() - 1].e[1].push_back(e); // push first half edge to the incoming of new vertex
V[V.size() - 1].e[0].push_back((unsigned)E.size() - 1); // push second half edge to the outgoing of new vertex
for(unsigned i = 0; i < V[tmp.v[1]].e[1].size(); i++) // find the incoming edge of original ending vertex
if(V[tmp.v[1]].e[1][i] == e)
|
30b20d4f
Jiaming Guo
make it render be...
|
1159
|
V[tmp.v[1]].e[1][i] = (unsigned)E.size() - 1; // set to new edge
|
c72184d1
Jiaming Guo
add many function...
|
1160
1161
1162
|
}
}
}
|
797f8ab0
David Mayerich
cleaned up the st...
|
1163
|
|
c72184d1
Jiaming Guo
add many function...
|
1164
1165
1166
1167
|
/// This function compares two splitted networks and yields a mapping relationship between them according to NN
/// @param B is the network that the current network is going to map to
/// @param C is the mapping relationship: C[e1] = _e1 means e1 edge in current network is mapping the _e1 edge in B
/// @param device is the device that user want to use
|
6b317c71
Jiaming Guo
add function to r...
|
1168
|
void mapping(stim::network<T> B, std::vector<unsigned> &C, int device, float threshold){
|
c72184d1
Jiaming Guo
add many function...
|
1169
1170
|
stim::network<T> A; //generate a network storing the result of the comparison
A = (*this);
|
797f8ab0
David Mayerich
cleaned up the st...
|
1171
|
|
c72184d1
Jiaming Guo
add many function...
|
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
|
size_t n = A.E.size(); // the number of edges in A
size_t NB = B.E.size(); // the number of edges in B
C.resize(A.E.size());
T *c; // centerline (array of double pointers) - points on kdtree must be double
size_t n_data = B.total_points(); // set the number of points
c = (T*) malloc(sizeof(T) * n_data * 3);
unsigned t = 0;
for(unsigned e = 0; e < NB; e++){ // for each edge in the network
for(unsigned p = 0; p < B.E[e].size(); p++){ // for each point in the edge
for(unsigned d = 0; d < 3; d++){ // for each coordinate
c[t * 3 + d] = B.E[e][p][d];
}
t++;
}
}
|
fbbc07be
Jiaming Guo
replace ANN by kd...
|
1191
|
|
c72184d1
Jiaming Guo
add many function...
|
1192
1193
1194
1195
1196
1197
1198
1199
1200
|
//generate a KD-tree for network A
//float metric = 0.0; // initialize metric to be returned after comparing the network
size_t MaxTreeLevels = 3; // max tree level
#ifdef __CUDACC__
cudaSetDevice(device);
stim::kdtree<T, 3> kdt; // initialize a pointer to a kd tree
kdt.create(c, n_data, MaxTreeLevels); // build a KD tree
|
797f8ab0
David Mayerich
cleaned up the st...
|
1201
|
|
c72184d1
Jiaming Guo
add many function...
|
1202
|
for(unsigned e = 0; e < n; e++){ //for each edge in A
|
0d0ef1a9
David Mayerich
Adapted classes t...
|
1203
|
//size_t errormag_id = A.E[e].nmags() - 1; //get the id for the new magnitude
|
c72184d1
Jiaming Guo
add many function...
|
1204
1205
1206
1207
|
//pre-judge to get rid of impossibly mapping edges
T M = 0;
for(unsigned p = 0; p < A.E[e].size(); p++)
|
0d0ef1a9
David Mayerich
Adapted classes t...
|
1208
|
M += A.E[e].r(p);
|
c72184d1
Jiaming Guo
add many function...
|
1209
|
M = M / A.E[e].size();
|
6b317c71
Jiaming Guo
add function to r...
|
1210
|
if(M > threshold)
|
c72184d1
Jiaming Guo
add many function...
|
1211
1212
|
C[e] = (unsigned)-1; //set the nearest edge of impossibly mapping edges to maximum of unsigned
else{
|
c9712fb4
Jiaming Guo
fix minor error w...
|
1213
|
T* queryPt = new T[3];
|
c72184d1
Jiaming Guo
add many function...
|
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
|
T* dists = new T[1];
size_t* nnIdx = new size_t[1];
stim2array(queryPt, A.E[e][A.E[e].size()/2]);
kdt.search(queryPt, 1, nnIdx, dists);
unsigned id = 0; //mapping edge's idx
size_t num = 0; //total number of points before #th edge
for(unsigned i = 0; i < NB; i++){
num += B.E[i].size();
if(nnIdx[0] < num){
C[e] = id;
break;
}
id++;
}
|
797f8ab0
David Mayerich
cleaned up the st...
|
1230
1231
|
}
}
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
1232
|
#else
|
c72184d1
Jiaming Guo
add many function...
|
1233
|
stim::kdtree<T, 3> kdt;
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
1234
1235
|
kdt.create(c, n_data, MaxTreeLevels);
T *dists = new T[1]; // near neighbor distances
|
fcd2eb7c
David Mayerich
added support for...
|
1236
|
size_t *nnIdx = new size_t[1]; // near neighbor indices // allocate near neigh indices
|
797f8ab0
David Mayerich
cleaned up the st...
|
1237
|
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
1238
|
stim::vec3<T> p0, p1;
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
1239
|
T* queryPt = new T[3];
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
1240
|
|
66d1c0ff
Pavel Govyadinov
fixed some cuda c...
|
1241
|
for(unsigned int e = 0; e < R.E.size(); e++){ // for each edge in A
|
c72184d1
Jiaming Guo
add many function...
|
1242
|
T M; // the sum of metrics of current edge
|
c72184d1
Jiaming Guo
add many function...
|
1243
|
for(unsigned p = 0; p < R.E[e].size(); p++)
|
2b28db3e
Jiaming Guo
better render
|
1244
|
M += A.E[e].r(p);
|
c72184d1
Jiaming Guo
add many function...
|
1245
|
M = M / A.E[e].size();
|
6b317c71
Jiaming Guo
add function to r...
|
1246
|
if(M > threshold)
|
c72184d1
Jiaming Guo
add many function...
|
1247
1248
1249
|
C[e] = (unsigned)-1;
else{ // if it should have corresponding edge in B, then...
p1 = R.E[e][R.E[e].size()/2];
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
1250
|
stim2array(queryPt, p1);
|
c72184d1
Jiaming Guo
add many function...
|
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
|
kdt.cpu_search(queryPt, 1, nnIdx, dists); // search the tree
unsigned id = 0; //mapping edge's idx
size_t num = 0; //total number of points before #th edge
for(unsigned i = 0; i < NB; i++){
num += B.E[i].size();
if(nnIdx[0] < num){
C[e] = id;
break;
}
id++;
}
|
41100e51
Jiaming Guo
add cpu kdtree ve...
|
1263
1264
1265
|
}
}
#endif
|
3800c27f
David Mayerich
added code to sub...
|
1266
|
}
|
9c97e126
David Mayerich
added an axis-ali...
|
1267
|
|
b3a38641
David Mayerich
added the ability...
|
1268
|
/// Returns the number of magnitude values stored in each edge. This should be uniform across the network.
|
0d0ef1a9
David Mayerich
Adapted classes t...
|
1269
1270
1271
|
//unsigned nmags(){
// return E[0].nmags();
//}
|
10b95d6e
pranathivemuri
load text file to...
|
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
|
// split a string in text by the character sep
stim::vec<T> split(std::string &text, char sep)
{
stim::vec<T> tokens;
std::size_t start = 0, end = 0;
while ((end = text.find(sep, start)) != std::string::npos) {
tokens.push_back(atof(text.substr(start, end - start).c_str()));
start = end + 1;
}
tokens.push_back(atof(text.substr(start).c_str()));
return tokens;
}
// load a network in text file to a network class
void load_txt(std::string filename)
{
std::vector <std::string> file_contents;
|
e8c9a82b
David Mayerich
fixed GCC errors ...
|
1288
|
std::ifstream file(filename.c_str());
|
10b95d6e
pranathivemuri
load text file to...
|
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
|
std::string line;
std::vector<unsigned> id2vert; //this list stores the vertex ID associated with each network vertex
//for each line in the text file, store them as strings in file_contents
while (std::getline(file, line))
{
std::stringstream ss(line);
file_contents.push_back(ss.str());
}
unsigned int numEdges = atoi(file_contents[0].c_str()); //number of edges in the network
unsigned int I = atoi(file_contents[1].c_str()) ; //calculate the number of points3d on the first edge
unsigned int count = 1; unsigned int k = 2; // count is global counter through the file contents, k is for the vertices on the edges
// for each edge in the network.
for (unsigned int i = 0; i < numEdges; i ++ )
{
// pre allocate a position vector p with number of points3d on the edge p
std::vector< stim::vec<T> > p(0, I);
// for each point on the nth edge
for (unsigned int j = k; j < I + k; j++)
{
// split the points3d of floats with separator space and form a float3 position vector out of them
p.push_back(split(file_contents[j], ' '));
}
count += p.size() + 1; // increment count to point at the next edge in the network
I = atoi(file_contents[count].c_str()); // read in the points3d at the next edge and convert it to an integer
k = count + 1;
edge new_edge = p; // create an edge with a vector of points3d on the edge
E.push_back(new_edge); // push the edge into the network
}
unsigned int numVertices = atoi(file_contents[count].c_str()); // this line in the text file gives the number of distinct vertices
count = count + 1; // this line of text file gives the first verrtex
// push each vertex into V
for (unsigned int i = 0; i < numVertices; i ++)
{
vertex new_vertex = split(file_contents[count], ' ');
V.push_back(new_vertex);
count += atoi(file_contents[count + 1].c_str()) + 2; // Skip number of edge ids + 2 to point to the next vertex
}
} // end load_txt function
|
b3a38641
David Mayerich
added the ability...
|
1327
|
|
abce2443
pranathivemuri
average branching...
|
1328
1329
1330
1331
1332
1333
1334
1335
1336
|
// strTxt returns a string of edges
std::string
strTxt(std::vector< stim::vec<T> > p)
{
std::stringstream ss;
std::stringstream oss;
for(unsigned int i = 0; i < p.size(); i++){
ss.str(std::string());
for(unsigned int d = 0; d < 3; d++){
|
9f5c0d4a
Pavel Govyadinov
edits to make net...
|
1337
|
ss<<p[i][d];
|
abce2443
pranathivemuri
average branching...
|
1338
|
}
|
8b7d6f9a
David Mayerich
light modificatio...
|
1339
|
ss << "\n";
|
abce2443
pranathivemuri
average branching...
|
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
|
}
return ss.str();
}
// removes specified character from string
void removeCharsFromString(std::string &str, char* charsToRemove ) {
for ( unsigned int i = 0; i < strlen(charsToRemove); ++i ) {
str.erase( remove(str.begin(), str.end(), charsToRemove[i]), str.end() );
}
}
//exports network to txt file
void
to_txt(std::string filename)
{
|
e8c9a82b
David Mayerich
fixed GCC errors ...
|
1353
1354
|
std::ofstream ofs(filename.c_str(), std::ofstream::out | std::ofstream::app);
//int num;
|
abce2443
pranathivemuri
average branching...
|
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
|
ofs << (E.size()).str() << "\n";
for(unsigned int i = 0; i < E.size(); i++)
{
std::string str;
ofs << (E[i].size()).str() << "\n";
str = E[i].strTxt();
ofs << str << "\n";
}
for(int i = 0; i < V.size(); i++)
{
std::string str;
str = V[i].str();
|
e8c9a82b
David Mayerich
fixed GCC errors ...
|
1367
1368
|
char temp[4] = "[],";
removeCharsFromString(str, temp);
|
abce2443
pranathivemuri
average branching...
|
1369
1370
1371
1372
|
ofs << str << "\n";
}
ofs.close();
}
|
7f27eafa
David Mayerich
simplified the st...
|
1373
1374
|
}; //end stim::network class
}; //end stim namespace
|
3b14b0f5
Pavel Govyadinov
added operators f...
|
1375
|
#endif
|