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>
|
58ee2b21
David Mayerich
incorporated stim...
|
13
|
#include <stim/visualization/cylinder.h>
|
df036f4c
David Mayerich
added the network...
|
14
|
#include <ANN/ANN.h>
|
7f27eafa
David Mayerich
simplified the st...
|
15
16
|
#include <boost/tuple/tuple.hpp>
|
df036f4c
David Mayerich
added the network...
|
17
18
|
namespace stim{
|
7f27eafa
David Mayerich
simplified the st...
|
19
20
21
22
23
|
/** 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.
* 2)Network connectivity (a graph of nodes and edges), reconstructed using ANN library.
*/
|
df036f4c
David Mayerich
added the network...
|
24
|
|
df036f4c
David Mayerich
added the network...
|
25
26
27
28
|
template<typename T>
class network{
|
7f27eafa
David Mayerich
simplified the st...
|
29
30
|
///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...
|
31
|
class edge : public cylinder<T>
|
7f27eafa
David Mayerich
simplified the st...
|
32
33
34
|
{
public:
unsigned v[2]; //unique id's designating the starting and ending
|
7c43b7f9
pranathivemuri
return resampled ...
|
35
|
// default constructor
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
36
37
38
39
|
edge() : cylinder<T>()
{
v[1] = -1; v[0] = -1;
}
|
7f27eafa
David Mayerich
simplified the st...
|
40
|
/// Constructor - creates an edge from a list of points by calling the stim::fiber constructor
|
e376212f
David Mayerich
added support for...
|
41
|
|
3800c27f
David Mayerich
added code to sub...
|
42
|
///@param p is an array of positions in space
|
308a743c
David Mayerich
fixed class compa...
|
43
|
edge(std::vector< stim::vec3<T> > p) : cylinder<T>(p){}
|
3800c27f
David Mayerich
added code to sub...
|
44
45
|
/// Copy constructor creates an edge from a fiber
|
58ee2b21
David Mayerich
incorporated stim...
|
46
|
edge(stim::cylinder<T> f) : cylinder<T>(f) {}
|
3800c27f
David Mayerich
added code to sub...
|
47
48
49
|
/// Resamples an edge by calling the fiber resampling function
edge resample(T spacing){
|
58ee2b21
David Mayerich
incorporated stim...
|
50
|
edge e(cylinder<T>::resample(spacing)); //call the fiber->edge constructor
|
3800c27f
David Mayerich
added code to sub...
|
51
52
53
54
55
|
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...
|
56
|
|
7f27eafa
David Mayerich
simplified the st...
|
57
|
/// Output the edge information as a string
|
df036f4c
David Mayerich
added the network...
|
58
59
|
std::string str(){
std::stringstream ss;
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
60
|
ss<<"("<<cylinder<T>::size()<<")\tl = "<<this.length()<<"\t"<<v[0]<<"----"<<v[1];
|
df036f4c
David Mayerich
added the network...
|
61
62
|
return ss.str();
}
|
df036f4c
David Mayerich
added the network...
|
63
|
|
9c97e126
David Mayerich
added an axis-ali...
|
64
65
|
};
|
7f27eafa
David Mayerich
simplified the st...
|
66
|
///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...
|
67
|
class vertex : public stim::vec3<T>
|
7f27eafa
David Mayerich
simplified the st...
|
68
69
|
{
public:
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
70
71
72
|
//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.
|
7f27eafa
David Mayerich
simplified the st...
|
73
74
|
//constructor takes a stim::vec
|
308a743c
David Mayerich
fixed class compa...
|
75
|
vertex(stim::vec3<T> p) : stim::vec3<T>(p){}
|
7f27eafa
David Mayerich
simplified the st...
|
76
77
78
79
|
/// Output the vertex information as a string
std::string str(){
std::stringstream ss;
|
308a743c
David Mayerich
fixed class compa...
|
80
|
ss<<"\t(x, y, z) = "<<stim::vec3<T>::str();
|
7f27eafa
David Mayerich
simplified the st...
|
81
82
83
84
85
86
87
88
89
90
91
|
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...
|
92
|
|
7f27eafa
David Mayerich
simplified the st...
|
93
|
return ss.str();
|
df036f4c
David Mayerich
added the network...
|
94
|
}
|
9c97e126
David Mayerich
added an axis-ali...
|
95
|
|
fe0c7539
pranathivemuri
added functions t...
|
96
|
|
df036f4c
David Mayerich
added the network...
|
97
98
|
};
|
9c97e126
David Mayerich
added an axis-ali...
|
99
|
protected:
|
df036f4c
David Mayerich
added the network...
|
100
|
|
7f27eafa
David Mayerich
simplified the st...
|
101
102
|
std::vector<edge> E; //list of edges
std::vector<vertex> V; //list of vertices.
|
9c97e126
David Mayerich
added an axis-ali...
|
103
104
|
public:
|
df036f4c
David Mayerich
added the network...
|
105
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
106
107
108
109
110
111
112
113
114
115
116
117
|
///default constructor
network()
{
}
///constructor with a file to load.
network(std::string fileLocation)
{
load_obj(fileLocation);
}
|
7f27eafa
David Mayerich
simplified the st...
|
118
119
120
|
///Returns the number of edges in the network.
unsigned int edges(){
return E.size();
|
df036f4c
David Mayerich
added the network...
|
121
122
|
}
|
7f27eafa
David Mayerich
simplified the st...
|
123
124
125
|
///Returns the number of nodes in the network.
unsigned int vertices(){
return V.size();
|
df036f4c
David Mayerich
added the network...
|
126
127
|
}
|
fe0c7539
pranathivemuri
added functions t...
|
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
std::vector<vertex> operator*(T s){
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;
}
|
abce2443
pranathivemuri
average branching...
|
144
145
146
147
148
149
150
151
152
153
154
155
|
// Returns an average of branching index in the network
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...
|
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
|
// Returns number of branch points in thenetwork
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
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;
//}
|
fe0c7539
pranathivemuri
added functions t...
|
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
// Returns an average of fiber/edge lengths in the network
double Lengths(){
stim::vec<T> L;double sumLength = 0;
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...
|
222
223
224
225
226
|
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...
|
227
228
229
230
231
232
233
234
235
|
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...
|
236
237
238
|
return avg;
}
|
15a53122
pranathivemuri
Added other simpl...
|
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
// 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...
|
255
|
|
15a53122
pranathivemuri
Added other simpl...
|
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
// 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;
}
|
58ee2b21
David Mayerich
incorporated stim...
|
272
|
stim::cylinder<T> get_cylinder(unsigned f){
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
273
|
return E[f]; //return the specified edge (casting it to a fiber)
|
3800c27f
David Mayerich
added code to sub...
|
274
275
|
}
|
7f27eafa
David Mayerich
simplified the st...
|
276
277
|
//load a network from an OBJ file
void load_obj(std::string filename){
|
e376212f
David Mayerich
added support for...
|
278
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
279
280
|
stim::obj<T> O; //create an OBJ object
O.load(filename); //load the OBJ file as an object
|
df036f4c
David Mayerich
added the network...
|
281
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
282
|
std::vector<unsigned> id2vert; //this list stores the OBJ vertex ID associated with each network vertex
|
df036f4c
David Mayerich
added the network...
|
283
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
284
|
unsigned i[2]; //temporary, IDs associated with the first and last points in an OBJ line
|
df036f4c
David Mayerich
added the network...
|
285
|
|
7f27eafa
David Mayerich
simplified the st...
|
286
287
|
//for each line in the OBJ object
for(unsigned int l = 1; l <= O.numL(); l++){
|
df036f4c
David Mayerich
added the network...
|
288
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
289
|
std::vector< stim::vec<T> > c; //allocate an array of points for the vessel centerline
|
7f27eafa
David Mayerich
simplified the st...
|
290
|
O.getLine(l, c); //get the fiber centerline
|
df036f4c
David Mayerich
added the network...
|
291
|
|
308a743c
David Mayerich
fixed class compa...
|
292
293
294
295
|
std::vector< stim::vec3<T> > c3(c.size());
for(size_t j = 0; j < c.size(); j++)
c3[j] = c[j];
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
296
297
298
299
300
|
// 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...
|
301
|
|
7f27eafa
David Mayerich
simplified the st...
|
302
|
//get the first and last vertex IDs for the line
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
303
|
std::vector< unsigned > id; //create an array to store the centerline point IDs
|
9c97e126
David Mayerich
added an axis-ali...
|
304
|
O.getLinei(l, id); //get the list of point IDs for the line
|
7f27eafa
David Mayerich
simplified the st...
|
305
306
|
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...
|
307
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
308
|
std::vector<unsigned>::iterator it; //create an iterator for searching the id2vert array
|
7f27eafa
David Mayerich
simplified the st...
|
309
|
unsigned it_idx; //create an integer for the id2vert entry index
|
df036f4c
David Mayerich
added the network...
|
310
|
|
7f27eafa
David Mayerich
simplified the st...
|
311
|
//find out if the nodes for this fiber have already been created
|
7f27eafa
David Mayerich
simplified the st...
|
312
|
it = find(id2vert.begin(), id2vert.end(), i[0]); //look for the first node
|
7f27eafa
David Mayerich
simplified the st...
|
313
|
if(it == id2vert.end()){ //if i[0] hasn't already been used
|
4df9ec0e
pranathivemuri
compare networks ...
|
314
|
vertex new_vertex = new_edge[0]; //create a new vertex, assign it a position
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
315
316
317
318
|
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
|
df036f4c
David Mayerich
added the network...
|
319
|
}
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
320
|
else{ //if the vertex already exists
|
15a53122
pranathivemuri
Added other simpl...
|
321
|
it_idx = std::distance(id2vert.begin(), it);
|
df480014
pranathivemuri
added in comments...
|
322
|
V[it_idx].e[0].push_back(E.size()); //add the current edge as outgoing
|
3800c27f
David Mayerich
added code to sub...
|
323
|
new_edge.v[0] = it_idx;
|
df036f4c
David Mayerich
added the network...
|
324
325
|
}
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
326
|
it = find(id2vert.begin(), id2vert.end(), i[1]); //look for the second ID
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
327
328
329
|
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
new_vertex.e[1].push_back(E.size()); //add the current edge as incoming
|
b2f48bca
Pavel Govyadinov
fixed merge confl...
|
330
|
new_edge.v[1] = V.size(); //add the new vertex to the edge
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
331
332
|
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
|
df036f4c
David Mayerich
added the network...
|
333
|
}
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
334
|
else{ //if the vertex already exists
|
15a53122
pranathivemuri
Added other simpl...
|
335
|
it_idx = std::distance(id2vert.begin(), it);
|
df480014
pranathivemuri
added in comments...
|
336
|
V[it_idx].e[1].push_back(E.size()); //add the current edge as incoming
|
3800c27f
David Mayerich
added code to sub...
|
337
|
new_edge.v[1] = it_idx;
|
df036f4c
David Mayerich
added the network...
|
338
|
}
|
9c97e126
David Mayerich
added an axis-ali...
|
339
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
340
|
E.push_back(new_edge); //push the edge to the list
|
df036f4c
David Mayerich
added the network...
|
341
|
|
df036f4c
David Mayerich
added the network...
|
342
|
}
|
e376212f
David Mayerich
added support for...
|
343
344
|
}
|
7f27eafa
David Mayerich
simplified the st...
|
345
346
|
/// Output the network as a string
std::string str(){
|
e376212f
David Mayerich
added support for...
|
347
|
|
7f27eafa
David Mayerich
simplified the st...
|
348
349
350
351
|
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...
|
352
353
|
}
|
7f27eafa
David Mayerich
simplified the st...
|
354
355
356
|
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...
|
357
358
|
}
|
7f27eafa
David Mayerich
simplified the st...
|
359
|
return ss.str();
|
df036f4c
David Mayerich
added the network...
|
360
|
}
|
3800c27f
David Mayerich
added code to sub...
|
361
362
|
/// This function resamples all fibers in a network given a desired minimum spacing
|
9c97e126
David Mayerich
added an axis-ali...
|
363
|
/// @param spacing is the minimum distance between two points on the network
|
3800c27f
David Mayerich
added code to sub...
|
364
|
stim::network<T> resample(T spacing){
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
365
366
|
stim::network<T> n; //create a new network that will be an exact copy, with resampled fibers
n.V = V; //copy all vertices
|
3800c27f
David Mayerich
added code to sub...
|
367
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
368
|
n.E.resize(edges()); //allocate space for the edge list
|
3800c27f
David Mayerich
added code to sub...
|
369
370
|
//copy all fibers, resampling them in the process
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
371
372
|
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...
|
373
374
|
}
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
375
|
return n; //return the resampled network
|
4df9ec0e
pranathivemuri
compare networks ...
|
376
377
378
|
}
|
4df9ec0e
pranathivemuri
compare networks ...
|
379
|
|
9c97e126
David Mayerich
added an axis-ali...
|
380
|
/// Calculate the total number of points on all edges.
|
797f8ab0
David Mayerich
cleaned up the st...
|
381
382
383
384
|
unsigned total_points(){
unsigned n = 0;
for(unsigned e = 0; e < E.size(); e++)
n += E[e].size();
|
4df9ec0e
pranathivemuri
compare networks ...
|
385
|
return n;
|
797f8ab0
David Mayerich
cleaned up the st...
|
386
|
}
|
4df9ec0e
pranathivemuri
compare networks ...
|
387
388
389
390
|
// gaussian function
float gaussianFunction(float x, float std=25){ return exp(-x/(2*std*std));} // by default std = 25
|
e06b2e0b
pranathivemuri
removed functions...
|
391
|
// stim 3d vector to annpoint of 3 dimensions
|
308a743c
David Mayerich
fixed class compa...
|
392
|
void stim2ann(ANNpoint &a, stim::vec3<T> b){
|
797f8ab0
David Mayerich
cleaned up the st...
|
393
394
395
396
397
|
a[0] = b[0];
a[1] = b[1];
a[2] = b[2];
}
|
9c97e126
David Mayerich
added an axis-ali...
|
398
399
400
401
|
/// 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 ...
|
402
403
404
|
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
|
9c97e126
David Mayerich
added an axis-ali...
|
405
|
M += E[e].integrate(m); //get the integrated magnitude
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
406
|
L += E[e].length(); //get the edge length
|
9c97e126
David Mayerich
added an axis-ali...
|
407
|
}
|
4df9ec0e
pranathivemuri
compare networks ...
|
408
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
409
|
return M / L; //return the average magnitude
|
9c97e126
David Mayerich
added an axis-ali...
|
410
411
412
413
414
415
416
417
418
419
420
421
|
}
/// This function compares two networks and returns the percentage of the current network that is missing from A.
/// @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
stim::network<T> compare(stim::network<T> A, float sigma){
stim::network<T> R; //generate a network storing the result of the comparison
R = (*this); //initialize the result with the current network
//generate a KD-tree for network A
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
422
423
424
425
426
427
|
float metric = 0.0; // initialize metric to be returned after comparing the networks
ANNkd_tree* kdt; // initialize a pointer to a kd tree
double **c; // centerline (array of double pointers) - points on kdtree must be double
unsigned int n_data = A.total_points(); // set the number of points
c = (double**) malloc(sizeof(double*) * n_data); // allocate the array pointer
for(unsigned int i = 0; i < n_data; i++) // allocate space for each point of 3 dimensions
|
797f8ab0
David Mayerich
cleaned up the st...
|
428
|
c[i] = (double*) malloc(sizeof(double) * 3);
|
9c97e126
David Mayerich
added an axis-ali...
|
429
|
|
797f8ab0
David Mayerich
cleaned up the st...
|
430
|
unsigned t = 0;
|
9c97e126
David Mayerich
added an axis-ali...
|
431
|
for(unsigned e = 0; e < A.E.size(); e++){ //for each edge in the network
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
432
|
for(unsigned p = 0; p < A.E[e].size(); p++){ //for each point in the edge
|
797f8ab0
David Mayerich
cleaned up the st...
|
433
434
|
for(unsigned d = 0; d < 3; d++){ //for each coordinate
|
9c97e126
David Mayerich
added an axis-ali...
|
435
|
c[t][d] = A.E[e][p][d];
|
4df9ec0e
pranathivemuri
compare networks ...
|
436
|
}
|
797f8ab0
David Mayerich
cleaned up the st...
|
437
|
t++;
|
4df9ec0e
pranathivemuri
compare networks ...
|
438
|
}
|
797f8ab0
David Mayerich
cleaned up the st...
|
439
440
|
}
|
9c97e126
David Mayerich
added an axis-ali...
|
441
|
//compare each point in the current network to the field produced by A
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
442
443
|
ANNpointArray pts = (ANNpointArray)c; // create an array of data points of type double
kdt = new ANNkd_tree(pts, n_data, 3); // build a KD tree using the annpointarray
|
4df9ec0e
pranathivemuri
compare networks ...
|
444
|
double eps = 0; // error bound
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
445
446
|
ANNdistArray dists = new ANNdist[1]; // near neighbor distances
ANNidxArray nnIdx = new ANNidx[1]; // near neighbor indices // allocate near neigh indices
|
797f8ab0
David Mayerich
cleaned up the st...
|
447
|
|
308a743c
David Mayerich
fixed class compa...
|
448
449
|
stim::vec3<T> p0, p1;
float m1;
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
450
451
|
float M = 0; //stores the total metric value
float L = 0; //stores the total network length
|
797f8ab0
David Mayerich
cleaned up the st...
|
452
|
ANNpoint queryPt = annAllocPt(3);
|
9c97e126
David Mayerich
added an axis-ali...
|
453
|
for(unsigned e = 0; e < R.E.size(); e++){ //for each edge in A
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
454
|
R.E[e].add_mag(0); //add a new magnitude for the metric
|
797f8ab0
David Mayerich
cleaned up the st...
|
455
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
456
|
for(unsigned p = 0; p < R.E[e].size(); p++){ //for each point in the edge
|
797f8ab0
David Mayerich
cleaned up the st...
|
457
|
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
458
|
p1 = R.E[e][p]; //get the next point in the edge
|
797f8ab0
David Mayerich
cleaned up the st...
|
459
|
stim2ann(queryPt, p1);
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
460
|
kdt->annkSearch( queryPt, 1, nnIdx, dists, eps); //find the distance between A and the current network
|
308a743c
David Mayerich
fixed class compa...
|
461
|
m1 = 1.0f - gaussianFunction((float)dists[0], sigma); //calculate the metric value based on the distance
|
2e0f052a
Pavel Govyadinov
minor changes to ...
|
462
|
R.E[e].set_mag(m1, p, 1); //set the error for the second point in the segment
|
797f8ab0
David Mayerich
cleaned up the st...
|
463
|
|
797f8ab0
David Mayerich
cleaned up the st...
|
464
465
466
|
}
}
|
9c97e126
David Mayerich
added an axis-ali...
|
467
|
return R; //return the resulting network
|
3800c27f
David Mayerich
added code to sub...
|
468
|
}
|
9c97e126
David Mayerich
added an axis-ali...
|
469
|
|
b3a38641
David Mayerich
added the ability...
|
470
471
472
473
|
/// Returns the number of magnitude values stored in each edge. This should be uniform across the network.
unsigned nmags(){
return E[0].nmags();
}
|
10b95d6e
pranathivemuri
load text file to...
|
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
|
// 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;
std::ifstream file(filename);
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...
|
529
|
|
abce2443
pranathivemuri
average branching...
|
530
531
532
533
534
535
536
537
538
|
// 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...
|
539
|
ss<<p[i][d];
|
abce2443
pranathivemuri
average branching...
|
540
|
}
|
9f5c0d4a
Pavel Govyadinov
edits to make net...
|
541
|
ss < "\n";
|
abce2443
pranathivemuri
average branching...
|
542
543
544
545
546
547
548
549
550
551
552
553
554
|
}
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)
{
|
9f5c0d4a
Pavel Govyadinov
edits to make net...
|
555
|
std::ofstream ofs(filename, std::ofstream::out | std::ofstream::app);
|
abce2443
pranathivemuri
average branching...
|
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
int num;
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();
removeCharsFromString(str, "[],");
ofs << str << "\n";
}
ofs.close();
}
|
7f27eafa
David Mayerich
simplified the st...
|
574
575
|
}; //end stim::network class
}; //end stim namespace
|
df036f4c
David Mayerich
added the network...
|
576
|
#endif
|