From 029c021a64c23612b891b81d630834a9b7801dba Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 18 Jun 2017 22:25:10 -0500 Subject: [PATCH] filled in the rest of the code necessary for reading the writing .nwt files. fully functional --- stim/biomodels/network.h | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 80 insertions(+), 23 deletions(-) diff --git a/stim/biomodels/network.h b/stim/biomodels/network.h index 98a4f77..1325c6c 100644 --- a/stim/biomodels/network.h +++ b/stim/biomodels/network.h @@ -112,6 +112,7 @@ class network{ } return E; } + /// operator for writing the edge information into a binary .nwt file. friend std::ofstream& operator<<(std::ofstream& out, const edge& e) { @@ -122,10 +123,11 @@ class network{ for(int i = 0; i < sz; i++) ///write each point { stim::vec3 point = e[i]; - out.write(reinterpret_cast(point[0]), 3*sizeof(T)); + out.write(reinterpret_cast(&point[0]), 3*sizeof(T)); // for(int j = 0; j < nmags(); j++) //future code for multiple mags // { - out.write(reinterpret_cast(e[i].R[0]), sizeof(T)); ///write the radius + out.write(reinterpret_cast(&e.R[i]), sizeof(T)); ///write the radius + std::cout << point.str() << " " << e.R[i] << std::endl; // } } return out; //return stream @@ -134,13 +136,13 @@ class network{ /// operator for reading an edge from a binary .nwt file. friend std::ifstream& operator>>(std::ifstream& in, edge& e) { -// unsigned int v0, v1, sz; - unsigned int sz; - in.read(reinterpret_cast(&e.v[0]), sizeof(unsigned int)); //read the staring point. - in.read(reinterpret_cast(&e.v[1]), sizeof(unsigned int)); //read the ending point + unsigned int v0, v1, sz; + in.read(reinterpret_cast(&v0), sizeof(unsigned int)); //read the staring point. + in.read(reinterpret_cast(&v1), sizeof(unsigned int)); //read the ending point in.read(reinterpret_cast(&sz), sizeof(unsigned int)); //read the number of points in the edge stim::centerline temp = stim::centerline(sz); //allocate the new edge e = edge(temp); + e.v[0] = v0; e.v[1] = v1; for(int i = 0; i < sz; i++) //set the points and radii to the newly read values { stim::vec3 point; @@ -150,7 +152,8 @@ class network{ // for(int j = 0; j < nmags(); j++) ///future code for mags // { in.read(reinterpret_cast(&mag), sizeof(T)); - set_r(0, mag); + e.set_r(0, mag); + std::cout << point.str() << " " << mag << std::endl; // } } return in; @@ -164,7 +167,10 @@ class network{ //std::vector edges; //indices of edges connected to this node. std::vector e[2]; //indices of edges going out (e[0]) and coming in (e[1]) //stim::vec3 p; //position of this node in physical space. - + //default constructor + vertex() : stim::vec3() + { + } //constructor takes a stim::vec vertex(stim::vec3 p) : stim::vec3(p){} @@ -193,27 +199,27 @@ class network{ unsigned int s0, s1; s0 = v.e[0].size(); s1 = v.e[1].size(); - out.write(reinterpret_cast(&v[0]), 3*sizeof(T)); - out.write(reinterpret_cast(&s0), sizeof(unsigned int)); - out.write(reinterpret_cast(&s1), sizeof(unsigned int)); - out.write(reinterpret_cast(&v.e[0][0]), sizeof(unsigned int)*v.e[0].size()); - out.write(reinterpret_cast(&v.e[1][0]), sizeof(unsigned int)*v.e[1].size()); + out.write(reinterpret_cast(&v.ptr[0]), 3*sizeof(T)); ///write physical vertex location + out.write(reinterpret_cast(&s0), sizeof(unsigned int)); ///write the number of "incoming edges" + out.write(reinterpret_cast(&s1), sizeof(unsigned int)); ///write the number of "outgoing edges" + out.write(reinterpret_cast(&v.e[0][0]), sizeof(unsigned int)*v.e[0].size()); ///write the "incoming edges" + out.write(reinterpret_cast(&v.e[1][0]), sizeof(unsigned int)*v.e[1].size()); ///write the "outgoing edges" return out; } ///operator for reading the edge out of the stream; friend std::ifstream& operator>>(std::ifstream& in, vertex& v) { - in.read(reinterpret_cast(&v[0]), 3*sizeof(T)); - unsigned int s[2]; - in.read(reinterpret_cast(&s[0]), 2*sizeof(unsigned int)); + in.read(reinterpret_cast(&v[0]), 3*sizeof(T)); ///read the physical position + unsigned int s[2]; + in.read(reinterpret_cast(&s[0]), 2*sizeof(unsigned int)); ///read the sizes of incoming and outgoing edge arrays std::vector one(s[0]); std::vector two(s[1]); v.e[0] = one; v.e[1] = two; - in.read(reinterpret_cast(&v.e[0][0]),s[0]*sizeof(unsigned int)); - in.read(reinterpret_cast(&v.e[1][0]),s[1]*sizeof(unsigned int)); + in.read(reinterpret_cast(&v.e[0][0]),s[0]*sizeof(unsigned int)); ///read the arrays of "incoming edges" + in.read(reinterpret_cast(&v.e[1][0]),s[1]*sizeof(unsigned int)); ///read the arrays of "outgoing edges" return in; } @@ -529,23 +535,48 @@ public: void loadNwt(std::string filename) { + int dims[2]; ///number of vertex, number of edges + readHeader(filename, &dims[0]); //read header + std::ifstream file; + file.open(filename, std::ios::in | std::ios::binary); ///skip header information. + file.seekg(14+58+4+4, file.beg); + vertex v; + for(int i = 0; i < dims[0]; i++) ///for every vertex, read vertex, add to network. + { + file >> v; + V.push_back(v); +// std::cout << i << " " << v.str() << std::endl; + } + std::cout << std::endl; + for(int i = 0; i < dims[1]; i++) ///for every edge, read edge, add to network. + { + edge e; + file >> e; + E.push_back(e); + std::cout << i << " " << E[i].str() << std::endl; + } + file.close(); } ///saves a .nwt file. Writes the header in raw text format, then saves the network as a binary file. void saveNwt(std::string filename) { + writeHeader(filename); std::ofstream file; - file.open(filename, std::ios::out | std::ios::binary); - for(int i = 0; i < V.size(); i++) + file.open(filename, std::ios::out | std::ios::binary | std::ios::app); ///since we have written the header we are not appending. + for(int i = 0; i < V.size(); i++) ///look through the Vertices and write each one. { +// std::cout << i << " " << V[i].str() << std::endl; file << V[i]; } - for(int i = 0; i < E.size(); i++) + for(int i = 0; i < E.size(); i++) ///loop through the Edges and write each one. { + std::cout << i << " " << E[i].str() << std::endl; file << E[i]; } + file.close(); } @@ -553,14 +584,40 @@ public: void writeHeader(std::string filename) { + std::string magicString = "nwtFileFormat "; ///identifier for the file. + std::string desc = "fileid(14B), desc(58B), #vertices(4B), #edges(4B): bindata"; + int hNumVertices = V.size(); ///int byte header storing the number of vertices in the file + int hNumEdges = E.size(); ///int byte header storing the number of edges. + std::ofstream file; + file.open(filename, std::ios::out | std::ios::binary); + std::cout << hNumVertices << " " << hNumEdges << std::endl; + file.write(reinterpret_cast(&magicString.c_str()[0]), 14); //write the file id + file.write(reinterpret_cast(&desc.c_str()[0]), 58); //write the description + file.write(reinterpret_cast(&hNumVertices), sizeof(int)); //write #vert. + file.write(reinterpret_cast(&hNumEdges), sizeof(int)); //write #edges +// file << magicString.c_str() << desc.c_str() << hNumVertices << hNumEdges; + file.close(); } ///Reads the header information from a .nwt file. void - readHeader(std::string filename, unsigned int &dims) + readHeader(std::string filename, int *dims) { - + char magicString[14]; ///id + char desc[58]; ///description + int hNumVertices; ///#vert + int hNumEdges; ///#edges + std::ifstream file; ////create stream + file.open(filename, std::ios::in | std::ios::binary); + file.read(reinterpret_cast(&magicString[0]), 14); ///read the file id. + file.read(reinterpret_cast(&desc[0]), 58); ///read the description + file.read(reinterpret_cast(&hNumVertices), sizeof(int)); ///read the number of vertices + file.read(reinterpret_cast(&hNumEdges), sizeof(int)); ///read the number of edges +// std::cout << magicString << desc << hNumVertices << " " << hNumEdges << std::endl; + file.close(); ///close the file. + dims[0] = hNumVertices; ///fill the returned reference. + dims[1] = hNumEdges; } //load a network from an SWC file -- libgit2 0.21.4