Commit 029c021a64c23612b891b81d630834a9b7801dba

Authored by Pavel Govyadinov
1 parent 66d1c0ff

filled in the rest of the code necessary for reading the writing .nwt files. fully functional

Showing 1 changed file with 80 additions and 23 deletions   Show diff stats
stim/biomodels/network.h
... ... @@ -112,6 +112,7 @@ class network{
112 112 }
113 113 return E;
114 114 }
  115 +
115 116 /// operator for writing the edge information into a binary .nwt file.
116 117 friend std::ofstream& operator<<(std::ofstream& out, const edge& e)
117 118 {
... ... @@ -122,10 +123,11 @@ class network{
122 123 for(int i = 0; i < sz; i++) ///write each point
123 124 {
124 125 stim::vec3<T> point = e[i];
125   - out.write(reinterpret_cast<const char*>(point[0]), 3*sizeof(T));
  126 + out.write(reinterpret_cast<const char*>(&point[0]), 3*sizeof(T));
126 127 // for(int j = 0; j < nmags(); j++) //future code for multiple mags
127 128 // {
128   - out.write(reinterpret_cast<const char*>(e[i].R[0]), sizeof(T)); ///write the radius
  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;
129 131 // }
130 132 }
131 133 return out; //return stream
... ... @@ -134,13 +136,13 @@ class network{
134 136 /// operator for reading an edge from a binary .nwt file.
135 137 friend std::ifstream& operator>>(std::ifstream& in, edge& e)
136 138 {
137   -// unsigned int v0, v1, sz;
138   - unsigned int sz;
139   - in.read(reinterpret_cast<char*>(&e.v[0]), sizeof(unsigned int)); //read the staring point.
140   - in.read(reinterpret_cast<char*>(&e.v[1]), sizeof(unsigned int)); //read the ending point
  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
141 142 in.read(reinterpret_cast<char*>(&sz), sizeof(unsigned int)); //read the number of points in the edge
142 143 stim::centerline<T> temp = stim::centerline<T>(sz); //allocate the new edge
143 144 e = edge(temp);
  145 + e.v[0] = v0; e.v[1] = v1;
144 146 for(int i = 0; i < sz; i++) //set the points and radii to the newly read values
145 147 {
146 148 stim::vec3<T> point;
... ... @@ -150,7 +152,8 @@ class network{
150 152 // for(int j = 0; j < nmags(); j++) ///future code for mags
151 153 // {
152 154 in.read(reinterpret_cast<char*>(&mag), sizeof(T));
153   - set_r(0, mag);
  155 + e.set_r(0, mag);
  156 + std::cout << point.str() << " " << mag << std::endl;
154 157 // }
155 158 }
156 159 return in;
... ... @@ -164,7 +167,10 @@ class network{
164 167 //std::vector<unsigned int> edges; //indices of edges connected to this node.
165 168 std::vector<unsigned int> e[2]; //indices of edges going out (e[0]) and coming in (e[1])
166 169 //stim::vec3<T> p; //position of this node in physical space.
167   -
  170 + //default constructor
  171 + vertex() : stim::vec3<T>()
  172 + {
  173 + }
168 174 //constructor takes a stim::vec
169 175 vertex(stim::vec3<T> p) : stim::vec3<T>(p){}
170 176  
... ... @@ -193,27 +199,27 @@ class network{
193 199 unsigned int s0, s1;
194 200 s0 = v.e[0].size();
195 201 s1 = v.e[1].size();
196   - out.write(reinterpret_cast<const char*>(&v[0]), 3*sizeof(T));
197   - out.write(reinterpret_cast<const char*>(&s0), sizeof(unsigned int));
198   - out.write(reinterpret_cast<const char*>(&s1), sizeof(unsigned int));
199   - out.write(reinterpret_cast<const char*>(&v.e[0][0]), sizeof(unsigned int)*v.e[0].size());
200   - out.write(reinterpret_cast<const char*>(&v.e[1][0]), sizeof(unsigned int)*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"
201 207 return out;
202 208 }
203 209  
204 210 ///operator for reading the edge out of the stream;
205 211 friend std::ifstream& operator>>(std::ifstream& in, vertex& v)
206 212 {
207   - in.read(reinterpret_cast<char*>(&v[0]), 3*sizeof(T));
208   - unsigned int s[2];
209   - in.read(reinterpret_cast<char*>(&s[0]), 2*sizeof(unsigned int));
  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
210 216  
211 217 std::vector<unsigned int> one(s[0]);
212 218 std::vector<unsigned int> two(s[1]);
213 219 v.e[0] = one;
214 220 v.e[1] = two;
215   - in.read(reinterpret_cast<char*>(&v.e[0][0]),s[0]*sizeof(unsigned int));
216   - in.read(reinterpret_cast<char*>(&v.e[1][0]),s[1]*sizeof(unsigned int));
  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"
217 223 return in;
218 224 }
219 225  
... ... @@ -529,23 +535,48 @@ public:
529 535 void
530 536 loadNwt(std::string filename)
531 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 + }
532 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();
533 560 }
534 561  
535 562 ///saves a .nwt file. Writes the header in raw text format, then saves the network as a binary file.
536 563 void
537 564 saveNwt(std::string filename)
538 565 {
  566 + writeHeader(filename);
539 567 std::ofstream file;
540   - file.open(filename, std::ios::out | std::ios::binary);
541   - for(int i = 0; i < V.size(); i++)
  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.
542 570 {
  571 +// std::cout << i << " " << V[i].str() << std::endl;
543 572 file << V[i];
544 573 }
545   - for(int i = 0; i < E.size(); i++)
  574 + for(int i = 0; i < E.size(); i++) ///loop through the Edges and write each one.
546 575 {
  576 + std::cout << i << " " << E[i].str() << std::endl;
547 577 file << E[i];
548 578 }
  579 + file.close();
549 580 }
550 581  
551 582  
... ... @@ -553,14 +584,40 @@ public:
553 584 void
554 585 writeHeader(std::string filename)
555 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();
556 600  
557 601 }
558 602  
559 603 ///Reads the header information from a .nwt file.
560 604 void
561   - readHeader(std::string filename, unsigned int &dims)
  605 + readHeader(std::string filename, int *dims)
562 606 {
563   -
  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;
564 621 }
565 622  
566 623 //load a network from an SWC file
... ...