Commit 58ee2b2161d82073123935023a6026ec7e79e7f3

Authored by David Mayerich
1 parent a42c92ae

incorporated stim::cylinder into stim::network, replacing stim::fiber

Showing 2 changed files with 76 additions and 12 deletions   Show diff stats
stim/biomodels/network.h
... ... @@ -10,7 +10,7 @@
10 10 #include <math.h>
11 11 #include <stim/math/vector.h>
12 12 #include <stim/visualization/obj.h>
13   -#include <stim/biomodels/fiber.h>
  13 +#include <stim/visualization/cylinder.h>
14 14 #include <ANN/ANN.h>
15 15 #include <boost/tuple/tuple.hpp>
16 16  
... ... @@ -28,23 +28,23 @@ class network{
28 28  
29 29 ///Each edge is a fiber with two nodes.
30 30 ///Each node is an in index to the endpoint of the fiber in the nodes array.
31   - class edge : public fiber<T>
  31 + class edge : public cylinder<T>
32 32 {
33 33 public:
34 34 unsigned v[2]; //unique id's designating the starting and ending
35 35 // default constructor
36   - edge() : fiber<T>(){v[1] = -1; v[0] = -1;}
  36 + edge() : cylinder<T>(){v[1] = -1; v[0] = -1;}
37 37 /// Constructor - creates an edge from a list of points by calling the stim::fiber constructor
38 38  
39 39 ///@param p is an array of positions in space
40   - edge(std::vector< stim::vec<T> > p) : fiber<T>(p){}
  40 + edge(std::vector< stim::vec<T> > p) : cylinder<T>(p){}
41 41  
42 42 /// Copy constructor creates an edge from a fiber
43   - edge(stim::fiber<T> f) : fiber<T>(f) {}
  43 + edge(stim::cylinder<T> f) : cylinder<T>(f) {}
44 44  
45 45 /// Resamples an edge by calling the fiber resampling function
46 46 edge resample(T spacing){
47   - edge e(fiber<T>::resample(spacing)); //call the fiber->edge constructor
  47 + edge e(cylinder<T>::resample(spacing)); //call the fiber->edge constructor
48 48 e.v[0] = v[0]; //copy the vertex data
49 49 e.v[1] = v[1];
50 50  
... ... @@ -54,7 +54,7 @@ class network{
54 54 /// Output the edge information as a string
55 55 std::string str(){
56 56 std::stringstream ss;
57   - ss<<"("<<fiber<T>::N<<")\tl = "<<length()<<"\t"<<v[0]<<"----"<<v[1];
  57 + ss<<"("<<cylinder<T>::size()<<")\tl = "<<length()<<"\t"<<v[0]<<"----"<<v[1];
58 58 return ss.str();
59 59 }
60 60  
... ... @@ -109,7 +109,7 @@ class network{
109 109 return V.size();
110 110 }
111 111  
112   - stim::fiber<T> get_fiber(unsigned f){
  112 + stim::cylinder<T> get_cylinder(unsigned f){
113 113 return E[f]; //return the specified edge (casting it to a fiber)
114 114 }
115 115  
... ... @@ -130,6 +130,7 @@ class network{
130 130 O.getLine(l, c); //get the fiber centerline
131 131  
132 132 edge new_edge = c; //create an edge from the given centerline
  133 + unsigned int I = new_edge.size(); //calculate the number of points on the centerline
133 134  
134 135 //get the first and last vertex IDs for the line
135 136 std::vector< unsigned > id; //create an array to store the centerline point IDs
... ... @@ -158,7 +159,7 @@ class network{
158 159 it = find(id2vert.begin(), id2vert.end(), i[1]); //look for the second ID
159 160 it_idx = std::distance(id2vert.begin(), it);
160 161 if(it == id2vert.end()){ //if i[1] hasn't already been used
161   - vertex new_vertex = new_edge.back(); //create a new vertex, assign it a position
  162 + vertex new_vertex = new_edge[I-1]; //create a new vertex, assign it a position
162 163 new_vertex.e[1].push_back(E.size()); //add the current edge as incoming
163 164 new_edge.v[1] = V.size();
164 165 V.push_back(new_vertex); //add the new vertex to the vertex list
... ... @@ -193,7 +194,6 @@ class network{
193 194  
194 195 /// This function resamples all fibers in a network given a desired minimum spacing
195 196 stim::network<T> resample(T spacing){
196   - std::cout<<"network::resample()"<<std::endl;
197 197 stim::network<T> n; //create a new network that will be an exact copy, with resampled fibers
198 198 n.V = V; //copy all vertices
199 199  
... ... @@ -201,9 +201,7 @@ class network{
201 201  
202 202 //copy all fibers, resampling them in the process
203 203 for(unsigned e = 0; e < edges(); e++){ //for each edge in the edge list
204   - std::cout<<"edge: "<<e<<std::endl;
205 204 n.E[e] = E[e].resample(spacing); //resample the edge and copy it to the new network
206   - std::cout<<"resampled"<<std::endl;
207 205 }
208 206  
209 207 return n; //return the resampled network
... ...
stim/visualization/cylinder.h
... ... @@ -48,6 +48,8 @@ class cylinder
48 48  
49 49 }
50 50  
  51 +
  52 +
51 53 ///returns the total length of the line at index j.
52 54 T
53 55 getl(int j)
... ... @@ -98,6 +100,21 @@ class cylinder
98 100 init(inP, inM);
99 101 }
100 102  
  103 + ///Constructor defines a cylinder with centerline inP and magnitudes of zero
  104 + ///@param inP: Vector of stim vecs composing the points of the centerline
  105 + cylinder(std::vector< stim::vec<T> > inP){
  106 + std::vector< stim::vec<T> > inM; //create an array of arbitrary magnitudes
  107 + inM.resize(inP.size(), 0); //initialize the magnitude values to zero
  108 + init(inP, inM);
  109 + }
  110 +
  111 +
  112 + ///Returns the number of points on the cylinder centerline
  113 +
  114 + unsigned int size(){
  115 + return pos.size();
  116 + }
  117 +
101 118  
102 119 ///Returns a position vector at the given p-value (p value ranges from 0 to 1).
103 120 ///interpolates the position along the line.
... ... @@ -189,6 +206,55 @@ class cylinder
189 206 return points;
190 207 }
191 208 }
  209 +
  210 + /// Allows a point on the centerline to be accessed using bracket notation
  211 +
  212 + vec<T> operator[](unsigned int i){
  213 +
  214 + return pos[i];
  215 +
  216 + }
  217 +
  218 + /// Returns the total length of the cylinder centerline
  219 + T length(){
  220 + return L.back();
  221 + }
  222 +
  223 + /// Resamples the cylinder to provide a maximum distance of "spacing" between centerline points. All current
  224 + /// centerline points are guaranteed to exist in the new cylinder
  225 + /// @param spacing is the maximum spacing allowed between sample points
  226 + cylinder<T> resample(T spacing){
  227 +
  228 + std::vector< vec<T> > result;
  229 +
  230 + vec<T> p0 = pos[0]; //initialize p0 to the first point on the centerline
  231 + vec<T> p1;
  232 + unsigned N = size(); //number of points in the current centerline
  233 +
  234 + //for each line segment on the centerline
  235 + for(unsigned int i = 1; i < N; i++){
  236 + p1 = pos[i]; //get the second point in the line segment
  237 +
  238 + vec<T> v = p1 - p0; //calculate the vector between these two points
  239 + T d = v.len(); //calculate the distance between these two points (length of the line segment)
  240 +
  241 + unsigned nsteps = d / spacing; //calculate the number of steps to take along the segment to meet the spacing criteria
  242 + T stepsize = 1.0 / nsteps; //calculate the parametric step size between new centerline points
  243 +
  244 + //for each step along the line segment
  245 + for(unsigned s = 0; s < nsteps; s++){
  246 + T alpha = stepsize * s; //calculate the fraction of the distance along the line segment covered
  247 + result.push_back(p0 + alpha * v); //push the point at alpha position along the line segment
  248 + }
  249 +
  250 + p0 = p1; //shift the points to move to the next line segment
  251 + }
  252 +
  253 + result.push_back(pos[size() - 1]); //push the last point in the centerline
  254 +
  255 + return cylinder<T>(result);
  256 +
  257 + }
192 258  
193 259 };
194 260  
... ...