Blame view

stim/biomodels/network.h 5.99 KB
df036f4c   David Mayerich   added the network...
1
2
3
  #ifndef STIM_NETWORK_H
  #define STIM_NETWORK_H
  
7f27eafa   David Mayerich   simplified the st...
4
5
6
7
8
9
10
  #include <list>
  #include <stdlib.h>
  #include <sstream>
  #include <fstream>
  #include <algorithm>
  #include <string.h>
  #include <math.h>
c61fd046   David Mayerich   fixed vessel mode...
11
  #include <stim/math/vector.h>
5687cf1b   Cherub P. Harder   Added #include <s...
12
  #include <stim/visualization/obj.h>
7f27eafa   David Mayerich   simplified the st...
13
  #include <stim/biomodels/fiber.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
31
32
33
34
  	///Each edge is a fiber with two nodes.
  	///Each node is an in index to the endpoint of the fiber in the nodes array.
  	class edge : public fiber<T>
  	{
  		public:
  		unsigned v[2];		//unique id's designating the starting and ending
df036f4c   David Mayerich   added the network...
35
  
7f27eafa   David Mayerich   simplified the st...
36
  		/// Constructor - creates an edge from a list of points by calling the stim::fiber constructor
e376212f   David Mayerich   added support for...
37
  
7f27eafa   David Mayerich   simplified the st...
38
39
40
41
  		///@param p is a position in space
  		edge(std::vector< stim::vec<T> > p) : fiber<T>(p){}
  			
  		/// Output the edge information as a string
df036f4c   David Mayerich   added the network...
42
43
  		std::string str(){
  			std::stringstream ss;
7f27eafa   David Mayerich   simplified the st...
44
  			ss<<"("<<fiber<T>::N<<")\tl = "<<length()<<"\t"<<v[0]<<"----"<<v[1];
df036f4c   David Mayerich   added the network...
45
46
  			return ss.str();
  		}
df036f4c   David Mayerich   added the network...
47
  
7f27eafa   David Mayerich   simplified the st...
48
49
50
51
52
53
54
55
  	};	
  	
  	///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.
  	class vertex : public stim::vec<T>
  	{
  		public:
  			//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])
df480014   pranathivemuri   added in comments...
56
  			//stim::vec<T> p;						//position of this node in physical space.
7f27eafa   David Mayerich   simplified the st...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  
  			//constructor takes a stim::vec
  			vertex(stim::vec<T> p) : stim::vec<T>(p){}
  
  			/// Output the vertex information as a string
  			std::string	str(){
  				std::stringstream ss;
  				ss<<"\t(x, y, z) = "<<stim::vec<T>::str();
  
  				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...
76
  
7f27eafa   David Mayerich   simplified the st...
77
  				return ss.str();
df036f4c   David Mayerich   added the network...
78
  			}
7f27eafa   David Mayerich   simplified the st...
79
  			
df036f4c   David Mayerich   added the network...
80
81
  	};
  
7f27eafa   David Mayerich   simplified the st...
82
  	private:
df036f4c   David Mayerich   added the network...
83
  
7f27eafa   David Mayerich   simplified the st...
84
85
86
87
  	std::vector<edge> E;       //list of edges
  	std::vector<vertex> V;	    //list of vertices.
  	
  	public:
df036f4c   David Mayerich   added the network...
88
  
7f27eafa   David Mayerich   simplified the st...
89
90
91
  	///Returns the number of edges in the network.
  	unsigned int edges(){
  		return E.size();
df036f4c   David Mayerich   added the network...
92
93
  	}
  
7f27eafa   David Mayerich   simplified the st...
94
95
96
  	///Returns the number of nodes in the network.
  	unsigned int vertices(){
  		return V.size();
df036f4c   David Mayerich   added the network...
97
98
  	}
  
7f27eafa   David Mayerich   simplified the st...
99
100
  	//load a network from an OBJ file
  	void load_obj(std::string filename){
e376212f   David Mayerich   added support for...
101
  
7f27eafa   David Mayerich   simplified the st...
102
103
  		stim::obj<T> O;			//create an OBJ object
  		O.load(filename);		//load the OBJ file as an object
df036f4c   David Mayerich   added the network...
104
  
df480014   pranathivemuri   added in comments...
105
  		//prints each line in the obj file - vertex positions and fibers
7f27eafa   David Mayerich   simplified the st...
106
  		std::cout<<O.str()<<std::endl;
df036f4c   David Mayerich   added the network...
107
  
7f27eafa   David Mayerich   simplified the st...
108
  		std::vector<unsigned> id2vert;	//this list stores the OBJ vertex ID associated with each network vertex
df036f4c   David Mayerich   added the network...
109
  
7f27eafa   David Mayerich   simplified the st...
110
  		unsigned i[2];						//temporary, IDs associated with the first and last points in an OBJ line
df036f4c   David Mayerich   added the network...
111
  
7f27eafa   David Mayerich   simplified the st...
112
113
  		//for each line in the OBJ object
  		for(unsigned int l = 1; l <= O.numL(); l++){
df036f4c   David Mayerich   added the network...
114
  
7f27eafa   David Mayerich   simplified the st...
115
116
  			std::vector< stim::vec<T> > c;				//allocate an array of points for the vessel centerline
  			O.getLine(l, c);							//get the fiber centerline
df036f4c   David Mayerich   added the network...
117
  
df480014   pranathivemuri   added in comments...
118
  			edge e = c;									//create an edge from the given centerline
df036f4c   David Mayerich   added the network...
119
  
7f27eafa   David Mayerich   simplified the st...
120
121
122
123
124
  			//get the first and last vertex IDs for the line
  			std::vector< unsigned > id;					//create an array to store the centerline point IDs
  			O.getLinei(l, id);							//get the list of point IDs for the line			
  			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...
125
  
7f27eafa   David Mayerich   simplified the st...
126
127
  			std::vector<unsigned>::iterator it;			//create an iterator for searching the id2vert array
  			unsigned it_idx;							//create an integer for the id2vert entry index
df036f4c   David Mayerich   added the network...
128
  
7f27eafa   David Mayerich   simplified the st...
129
130
131
132
133
134
135
136
137
  			//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
  			it_idx = std::distance(id2vert.begin(), it);
  			if(it == id2vert.end()){							//if i[0] hasn't already been used
  				vertex v = e[0];								//create a new vertex, assign it a position
  				v.e[0].push_back(E.size());						//add the current edge as outgoing
  				e.v[0] = V.size();								//add the new vertex to the edge
  				V.push_back(v);									//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...
138
  			}
7f27eafa   David Mayerich   simplified the st...
139
  			else{												//if the vertex already exists
df480014   pranathivemuri   added in comments...
140
  				V[it_idx].e[0].push_back(E.size());				//add the current edge as outgoing
7f27eafa   David Mayerich   simplified the st...
141
  				e.v[0] = it_idx;
df036f4c   David Mayerich   added the network...
142
143
  			}
  
7f27eafa   David Mayerich   simplified the st...
144
145
146
147
148
149
150
151
  			it = find(id2vert.begin(), id2vert.end(), i[1]);	//look for the second ID
  			it_idx = std::distance(id2vert.begin(), it);
  			if(it == id2vert.end()){							//if i[1] hasn't already been used
  				vertex v = e.back();							//create a new vertex, assign it a position
  				v.e[1].push_back(E.size());						//add the current edge as incoming
  				e.v[1] = V.size();
  				V.push_back(v);									//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...
152
  			}
7f27eafa   David Mayerich   simplified the st...
153
  			else{												//if the vertex already exists
df480014   pranathivemuri   added in comments...
154
  				V[it_idx].e[1].push_back(E.size());				//add the current edge as incoming
7f27eafa   David Mayerich   simplified the st...
155
  				e.v[1] = it_idx;
df036f4c   David Mayerich   added the network...
156
  			}
df480014   pranathivemuri   added in comments...
157
158
159
160
161
162
163
164
  			/*lines to be added for resampling each fiber in the network class
  			//std::vector< stim::vec<T> > newFiberPos, fiberPos;
  			//fiberPos = e.centerline();
  			//newFiberPos = e.Resample(fiberPos);
  			//edge newEdge = newFiberPos;
  			//E.push_back(newEdge);	
  			*/
  			
7f27eafa   David Mayerich   simplified the st...
165
  			E.push_back(e);										//push the edge to the list
df036f4c   David Mayerich   added the network...
166
  
df036f4c   David Mayerich   added the network...
167
  		}
e376212f   David Mayerich   added support for...
168
169
  	}
  
7f27eafa   David Mayerich   simplified the st...
170
171
  	/// Output the network as a string
  	std::string str(){
e376212f   David Mayerich   added support for...
172
  
7f27eafa   David Mayerich   simplified the st...
173
174
175
176
  		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...
177
178
  		}
  
7f27eafa   David Mayerich   simplified the st...
179
180
181
  		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...
182
183
  		}
  
7f27eafa   David Mayerich   simplified the st...
184
  		return ss.str();
df036f4c   David Mayerich   added the network...
185
  	}
7f27eafa   David Mayerich   simplified the st...
186
187
  };		//end stim::network class
  };		//end stim namespace
df036f4c   David Mayerich   added the network...
188
  #endif