Compare View
Commits (3)
Showing
2 changed files
Show diff stats
stim/biomodels/network.h
... | ... | @@ -12,10 +12,9 @@ |
12 | 12 | #include <stim/visualization/obj.h> |
13 | 13 | #include <stim/visualization/swc.h> |
14 | 14 | #include <stim/visualization/cylinder.h> |
15 | -#include <stim/structures/kdtree.cuh> | |
16 | 15 | #include <stim/cuda/cudatools/timer.h> |
17 | 16 | #include <stim/cuda/cudatools/callable.h> |
18 | - | |
17 | +#include <stim/structures/kdtree.cuh> | |
19 | 18 | //********************help function******************** |
20 | 19 | // gaussian_function |
21 | 20 | CUDA_CALLABLE float gaussianFunction(float x, float std = 25) { return exp(-x / (2 * std*std)); } // std default sigma value is 25 |
... | ... | @@ -64,13 +63,21 @@ class network{ |
64 | 63 | class edge : public cylinder<T> |
65 | 64 | { |
66 | 65 | public: |
67 | - unsigned v[2]; //unique id's designating the starting and ending | |
66 | + unsigned int v[2]; //unique id's designating the starting and ending | |
68 | 67 | // default constructor |
69 | 68 | edge() : cylinder<T>() { |
70 | 69 | v[1] = (unsigned)(-1); v[0] = (unsigned)(-1); |
71 | 70 | } |
72 | 71 | /// Constructor - creates an edge from a list of points by calling the stim::fiber constructor |
72 | +/* | |
73 | + ///@param v0, the starting index. | |
74 | + ///@param v1, the ending index. | |
75 | + ///@param sz, the number of point in the fiber. | |
76 | + edge(unsigned int v0, unsigned int v1, unsigned int sz) : cylinder<T>( | |
77 | + { | |
73 | 78 | |
79 | + } | |
80 | +*/ | |
74 | 81 | ///@param p is an array of positions in space |
75 | 82 | edge(stim::centerline<T> p) : cylinder<T>(p){} |
76 | 83 | |
... | ... | @@ -106,6 +113,51 @@ class network{ |
106 | 113 | return E; |
107 | 114 | } |
108 | 115 | |
116 | + /// operator for writing the edge information into a binary .nwt file. | |
117 | + friend std::ofstream& operator<<(std::ofstream& out, const edge& e) | |
118 | + { | |
119 | + out.write(reinterpret_cast<const char*>(&e.v[0]), sizeof(unsigned int)); ///write the starting point. | |
120 | + out.write(reinterpret_cast<const char*>(&e.v[1]), sizeof(unsigned int)); ///write the ending point. | |
121 | + unsigned int sz = e.size(); ///write the number of point in the edge. | |
122 | + out.write(reinterpret_cast<const char*>(&sz), sizeof(unsigned int)); | |
123 | + for(int i = 0; i < sz; i++) ///write each point | |
124 | + { | |
125 | + stim::vec3<T> point = e[i]; | |
126 | + out.write(reinterpret_cast<const char*>(&point[0]), 3*sizeof(T)); | |
127 | + // for(int j = 0; j < nmags(); j++) //future code for multiple mags | |
128 | + // { | |
129 | + out.write(reinterpret_cast<const char*>(&e.R[i]), sizeof(T)); ///write the radius | |
130 | + std::cout << point.str() << " " << e.R[i] << std::endl; | |
131 | + // } | |
132 | + } | |
133 | + return out; //return stream | |
134 | + } | |
135 | + | |
136 | + /// operator for reading an edge from a binary .nwt file. | |
137 | + friend std::ifstream& operator>>(std::ifstream& in, edge& e) | |
138 | + { | |
139 | + unsigned int v0, v1, sz; | |
140 | + in.read(reinterpret_cast<char*>(&v0), sizeof(unsigned int)); //read the staring point. | |
141 | + in.read(reinterpret_cast<char*>(&v1), sizeof(unsigned int)); //read the ending point | |
142 | + in.read(reinterpret_cast<char*>(&sz), sizeof(unsigned int)); //read the number of points in the edge | |
143 | + stim::centerline<T> temp = stim::centerline<T>(sz); //allocate the new edge | |
144 | + e = edge(temp); | |
145 | + e.v[0] = v0; e.v[1] = v1; | |
146 | + for(int i = 0; i < sz; i++) //set the points and radii to the newly read values | |
147 | + { | |
148 | + stim::vec3<T> point; | |
149 | + in.read(reinterpret_cast<char*>(&point[0]), 3*sizeof(T)); | |
150 | + e[i] = point; | |
151 | + T mag; | |
152 | + // for(int j = 0; j < nmags(); j++) ///future code for mags | |
153 | + // { | |
154 | + in.read(reinterpret_cast<char*>(&mag), sizeof(T)); | |
155 | + e.set_r(0, mag); | |
156 | + std::cout << point.str() << " " << mag << std::endl; | |
157 | + // } | |
158 | + } | |
159 | + return in; | |
160 | + } | |
109 | 161 | }; |
110 | 162 | |
111 | 163 | ///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. |
... | ... | @@ -115,12 +167,16 @@ class network{ |
115 | 167 | //std::vector<unsigned int> edges; //indices of edges connected to this node. |
116 | 168 | std::vector<unsigned int> e[2]; //indices of edges going out (e[0]) and coming in (e[1]) |
117 | 169 | //stim::vec3<T> p; //position of this node in physical space. |
118 | - | |
170 | + //default constructor | |
171 | + vertex() : stim::vec3<T>() | |
172 | + { | |
173 | + } | |
119 | 174 | //constructor takes a stim::vec |
120 | 175 | vertex(stim::vec3<T> p) : stim::vec3<T>(p){} |
121 | 176 | |
122 | 177 | /// Output the vertex information as a string |
123 | - std::string str(){ | |
178 | + std::string | |
179 | + str(){ | |
124 | 180 | std::stringstream ss; |
125 | 181 | ss<<"\t(x, y, z) = "<<stim::vec3<T>::str(); |
126 | 182 | |
... | ... | @@ -137,6 +193,36 @@ class network{ |
137 | 193 | |
138 | 194 | return ss.str(); |
139 | 195 | } |
196 | + ///operator for writing the edge into the stream; | |
197 | + friend std::ofstream& operator<<(std::ofstream& out, const vertex& v) | |
198 | + { | |
199 | + unsigned int s0, s1; | |
200 | + s0 = v.e[0].size(); | |
201 | + s1 = v.e[1].size(); | |
202 | + out.write(reinterpret_cast<const char*>(&v.ptr[0]), 3*sizeof(T)); ///write physical vertex location | |
203 | + out.write(reinterpret_cast<const char*>(&s0), sizeof(unsigned int)); ///write the number of "incoming edges" | |
204 | + out.write(reinterpret_cast<const char*>(&s1), sizeof(unsigned int)); ///write the number of "outgoing edges" | |
205 | + out.write(reinterpret_cast<const char*>(&v.e[0][0]), sizeof(unsigned int)*v.e[0].size()); ///write the "incoming edges" | |
206 | + out.write(reinterpret_cast<const char*>(&v.e[1][0]), sizeof(unsigned int)*v.e[1].size()); ///write the "outgoing edges" | |
207 | + return out; | |
208 | + } | |
209 | + | |
210 | + ///operator for reading the edge out of the stream; | |
211 | + friend std::ifstream& operator>>(std::ifstream& in, vertex& v) | |
212 | + { | |
213 | + in.read(reinterpret_cast<char*>(&v[0]), 3*sizeof(T)); ///read the physical position | |
214 | + unsigned int s[2]; | |
215 | + in.read(reinterpret_cast<char*>(&s[0]), 2*sizeof(unsigned int)); ///read the sizes of incoming and outgoing edge arrays | |
216 | + | |
217 | + std::vector<unsigned int> one(s[0]); | |
218 | + std::vector<unsigned int> two(s[1]); | |
219 | + v.e[0] = one; | |
220 | + v.e[1] = two; | |
221 | + in.read(reinterpret_cast<char*>(&v.e[0][0]),s[0]*sizeof(unsigned int)); ///read the arrays of "incoming edges" | |
222 | + in.read(reinterpret_cast<char*>(&v.e[1][0]),s[1]*sizeof(unsigned int)); ///read the arrays of "outgoing edges" | |
223 | + return in; | |
224 | + } | |
225 | + | |
140 | 226 | }; |
141 | 227 | |
142 | 228 | protected: |
... | ... | @@ -445,6 +531,95 @@ public: |
445 | 531 | } |
446 | 532 | } |
447 | 533 | |
534 | + ///loads a .nwt file. Reads the header and loads the data into the network according to the header. | |
535 | + void | |
536 | + loadNwt(std::string filename) | |
537 | + { | |
538 | + int dims[2]; ///number of vertex, number of edges | |
539 | + readHeader(filename, &dims[0]); //read header | |
540 | + std::ifstream file; | |
541 | + file.open(filename, std::ios::in | std::ios::binary); ///skip header information. | |
542 | + file.seekg(14+58+4+4, file.beg); | |
543 | + vertex v; | |
544 | + for(int i = 0; i < dims[0]; i++) ///for every vertex, read vertex, add to network. | |
545 | + { | |
546 | + file >> v; | |
547 | + V.push_back(v); | |
548 | +// std::cout << i << " " << v.str() << std::endl; | |
549 | + } | |
550 | + | |
551 | + std::cout << std::endl; | |
552 | + for(int i = 0; i < dims[1]; i++) ///for every edge, read edge, add to network. | |
553 | + { | |
554 | + edge e; | |
555 | + file >> e; | |
556 | + E.push_back(e); | |
557 | + std::cout << i << " " << E[i].str() << std::endl; | |
558 | + } | |
559 | + file.close(); | |
560 | + } | |
561 | + | |
562 | + ///saves a .nwt file. Writes the header in raw text format, then saves the network as a binary file. | |
563 | + void | |
564 | + saveNwt(std::string filename) | |
565 | + { | |
566 | + writeHeader(filename); | |
567 | + std::ofstream file; | |
568 | + file.open(filename, std::ios::out | std::ios::binary | std::ios::app); ///since we have written the header we are not appending. | |
569 | + for(int i = 0; i < V.size(); i++) ///look through the Vertices and write each one. | |
570 | + { | |
571 | +// std::cout << i << " " << V[i].str() << std::endl; | |
572 | + file << V[i]; | |
573 | + } | |
574 | + for(int i = 0; i < E.size(); i++) ///loop through the Edges and write each one. | |
575 | + { | |
576 | + std::cout << i << " " << E[i].str() << std::endl; | |
577 | + file << E[i]; | |
578 | + } | |
579 | + file.close(); | |
580 | + } | |
581 | + | |
582 | + | |
583 | + ///Writes the header information to a .nwt file. | |
584 | + void | |
585 | + writeHeader(std::string filename) | |
586 | + { | |
587 | + std::string magicString = "nwtFileFormat "; ///identifier for the file. | |
588 | + std::string desc = "fileid(14B), desc(58B), #vertices(4B), #edges(4B): bindata"; | |
589 | + int hNumVertices = V.size(); ///int byte header storing the number of vertices in the file | |
590 | + int hNumEdges = E.size(); ///int byte header storing the number of edges. | |
591 | + std::ofstream file; | |
592 | + file.open(filename, std::ios::out | std::ios::binary); | |
593 | + std::cout << hNumVertices << " " << hNumEdges << std::endl; | |
594 | + file.write(reinterpret_cast<const char*>(&magicString.c_str()[0]), 14); //write the file id | |
595 | + file.write(reinterpret_cast<const char*>(&desc.c_str()[0]), 58); //write the description | |
596 | + file.write(reinterpret_cast<const char*>(&hNumVertices), sizeof(int)); //write #vert. | |
597 | + file.write(reinterpret_cast<const char*>(&hNumEdges), sizeof(int)); //write #edges | |
598 | +// file << magicString.c_str() << desc.c_str() << hNumVertices << hNumEdges; | |
599 | + file.close(); | |
600 | + | |
601 | + } | |
602 | + | |
603 | + ///Reads the header information from a .nwt file. | |
604 | + void | |
605 | + readHeader(std::string filename, int *dims) | |
606 | + { | |
607 | + char magicString[14]; ///id | |
608 | + char desc[58]; ///description | |
609 | + int hNumVertices; ///#vert | |
610 | + int hNumEdges; ///#edges | |
611 | + std::ifstream file; ////create stream | |
612 | + file.open(filename, std::ios::in | std::ios::binary); | |
613 | + file.read(reinterpret_cast<char*>(&magicString[0]), 14); ///read the file id. | |
614 | + file.read(reinterpret_cast<char*>(&desc[0]), 58); ///read the description | |
615 | + file.read(reinterpret_cast<char*>(&hNumVertices), sizeof(int)); ///read the number of vertices | |
616 | + file.read(reinterpret_cast<char*>(&hNumEdges), sizeof(int)); ///read the number of edges | |
617 | +// std::cout << magicString << desc << hNumVertices << " " << hNumEdges << std::endl; | |
618 | + file.close(); ///close the file. | |
619 | + dims[0] = hNumVertices; ///fill the returned reference. | |
620 | + dims[1] = hNumEdges; | |
621 | + } | |
622 | + | |
448 | 623 | //load a network from an SWC file |
449 | 624 | void load_swc(std::string filename) { |
450 | 625 | stim::swc<T> S; // create swc variable |
... | ... | @@ -724,7 +899,7 @@ public: |
724 | 899 | T* query = new T[3 * n]; |
725 | 900 | T* m1 = new T[n]; |
726 | 901 | T* dists = new T[n]; |
727 | - size* nnIdx = new size_t[n]; | |
902 | + size_t* nnIdx = new size_t[n]; | |
728 | 903 | |
729 | 904 | edge2array(query, R.E[e]); |
730 | 905 | |
... | ... | @@ -1002,7 +1177,7 @@ public: |
1002 | 1177 | stim::vec3<T> p0, p1; |
1003 | 1178 | T* queryPt = new T[3]; |
1004 | 1179 | |
1005 | - for(unsigned e = 0; e < R.E.size(); e++){ // for each edge in A | |
1180 | + for(unsigned int e = 0; e < R.E.size(); e++){ // for each edge in A | |
1006 | 1181 | T M; // the sum of metrics of current edge |
1007 | 1182 | for(unsigned p = 0; p < R.E[e].size(); p++) |
1008 | 1183 | M += A.E[e].r(p); |
... | ... | @@ -1136,4 +1311,4 @@ public: |
1136 | 1311 | } |
1137 | 1312 | }; //end stim::network class |
1138 | 1313 | }; //end stim namespace |
1139 | -#endif | |
1140 | 1314 | \ No newline at end of file |
1315 | +#endif | ... | ... |
stim/visualization/cylinder.h
... | ... | @@ -46,7 +46,7 @@ public: |
46 | 46 | cylinder(centerline<T>c) : centerline<T>(c) { |
47 | 47 | init(); |
48 | 48 | } |
49 | - | |
49 | + | |
50 | 50 | //cylinder(centerline<T>c, T r) : centerline(c) { |
51 | 51 | // init(); |
52 | 52 | // //add_mag(r); |
... | ... | @@ -711,4 +711,4 @@ public: |
711 | 711 | }; |
712 | 712 | |
713 | 713 | } |
714 | -#endif | |
715 | 714 | \ No newline at end of file |
715 | +#endif | ... | ... |