Commit 3b14b0f52d92c10e0d1826fbdaaacadb9a1cfe80

Authored by Pavel Govyadinov
1 parent 4b9199d3

added operators for reading and writing binary .nwt files::need to fill in the methods

Showing 2 changed files with 98 additions and 3 deletions   Show diff stats
stim/biomodels/network.h
... ... @@ -71,6 +71,14 @@ class network{
71 71 }
72 72 /// Constructor - creates an edge from a list of points by calling the stim::fiber constructor
73 73  
  74 + ///@param v0, the starting index.
  75 + ///@param v1, the ending index.
  76 + ///@param sz, the number of point in the fiber.
  77 + edge(unsigned int v0, unsigned int v1, unsigned int sz) : cylinder<T>(
  78 + {
  79 +
  80 + }
  81 +
74 82 ///@param p is an array of positions in space
75 83 edge(stim::centerline<T> p) : cylinder<T>(p){}
76 84  
... ... @@ -105,7 +113,48 @@ class network{
105 113 }
106 114 return E;
107 115 }
  116 + /// operator for writing the edge information into a binary .nwt file.
  117 + std::ofstream& operator<<(std::ofstream& out, const edge<T>& 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 = 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[i].R[0]), sizeof(T)); ///write the radius
  130 + // }
  131 + }
  132 + return out; //return stream
  133 + }
108 134  
  135 + /// operator for reading an edge from a binary .nwt file.
  136 + std::ofstream& operator>>(std::ofstream& in, edge<T>& e)
  137 + {
  138 + unsigned int v0, v1, sz;
  139 + in.read((reinterpret_cast<const char*>(&v[0]), sizeof(unsigned int)); //read the staring point.
  140 + in.read((reinterpret_cast<const char*>(&v[1]), sizeof(unsigned int)); //read the ending point
  141 + in.read((reinterpret_cast<const char*>(&sz), sizeof(unsigned int)); //read the number of points in the edge
  142 + stim::centerline temp = stim::centerline(sz) //allocate the new edge
  143 + e = edge(temp);
  144 + for(int i = 0; i < sz; i++) //set the points and radii to the newly read values
  145 + {
  146 + stim::vec3<T> point;
  147 + in.read((reinterpret_cast<const char*>(&point[0]) 3*sizeof(T));
  148 + e[i] = point;
  149 + T mag;
  150 +// for(int j = 0; j < nmags(); j++) ///future code for mags
  151 +// {
  152 + in.read(reinterpret_cast<const char*>(&mag), sizeof(T));
  153 + set_r(0, mag);
  154 +// }
  155 + }
  156 + return in;
  157 + }
109 158 };
110 159  
111 160 ///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.
... ... @@ -137,6 +186,32 @@ class network{
137 186  
138 187 return ss.str();
139 188 }
  189 + ///operator for writing the edge into the stream;
  190 + std::ofstream& operator<<(std::ofstream& out, const vertex& v)
  191 + {
  192 + out.write(reinterpret_cast<const char*>(&v[0]), 3*sizeof(T));
  193 + out.write(reinterpret_cast<const char*>(&v.e[0].size()), sizeof(unsigned int));
  194 + out.write(reinterpret_cast<const char*>(&v.e[1].size()), sizeof(unsigned int));
  195 + out.write(reinterpret_cast<const char*>(&v.e[0][0]), sizeof(unsigned int)*v.e[0].size());
  196 + out.write(reinterpret_cast<const char*>(&v.e[1][0]), sizeof(unsigned int)*v.e[1].size());
  197 + return out;
  198 + }
  199 +
  200 + ///operator for reading the edge out of the stream;
  201 + std::ofstream& operator<<(std::ofstream& in, vertex& v)
  202 + {
  203 + in.read(reinterpret_cast<const char*>(&v[0]), 3*sizeof(T))
  204 + unsigned int s[2];
  205 + in.read(reinterpret_cast<const char*>(&s[0]), 2*sizeof(unsigned int));
  206 +
  207 + std::vector<unsigned int> one(s[0]);
  208 + std::vector<unsigned int> two(s[1]);
  209 + v.e[0] = one;
  210 + v.e[1] = two;
  211 + in.read(reinterpret_cast<const char*>(&v.e[0][0]),s[0]*sizeof(unsigned int));
  212 + in.read(reinterpret_cast<const char*>(&v.e[1][0]),s[1]*sizeof(unsigned int));
  213 + return in;
  214 + }
140 215 };
141 216  
142 217 protected:
... ... @@ -444,7 +519,27 @@ public:
444 519  
445 520 }
446 521 }
  522 + ///loads a .nwt file. Reads the header and loads the data into the network according to the header.
  523 +/* void
  524 + loadNwt(std::string filename)
  525 + {
447 526  
  527 + }
  528 +
  529 + ///saves a .nwt file. Writes the header in raw text format, then saves the network as a binary file.
  530 + ///
  531 + void
  532 + saveNwt(std::string filename)
  533 + {
  534 +
  535 + }
  536 +
  537 + void
  538 + writeHeader(std::string filename)
  539 + {
  540 +
  541 + }
  542 +*/
448 543 //load a network from an SWC file
449 544 void load_swc(std::string filename) {
450 545 stim::swc<T> S; // create swc variable
... ... @@ -1136,4 +1231,4 @@ public:
1136 1231 }
1137 1232 }; //end stim::network class
1138 1233 }; //end stim namespace
1139   -#endif
1140 1234 \ No newline at end of file
  1235 +#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
... ...