Commit 10b95d6e6b22663e7caaf21afba1414e41f3bf54
1 parent
fe0c7539
load text file to a network load_txt function written
Showing
1 changed file
with
55 additions
and
2 deletions
Show diff stats
stim/biomodels/network.h
... | ... | @@ -372,8 +372,61 @@ public: |
372 | 372 | unsigned nmags(){ |
373 | 373 | return E[0].nmags(); |
374 | 374 | } |
375 | - | |
376 | - | |
375 | + // split a string in text by the character sep | |
376 | + stim::vec<T> split(std::string &text, char sep) | |
377 | + { | |
378 | + stim::vec<T> tokens; | |
379 | + std::size_t start = 0, end = 0; | |
380 | + while ((end = text.find(sep, start)) != std::string::npos) { | |
381 | + tokens.push_back(atof(text.substr(start, end - start).c_str())); | |
382 | + start = end + 1; | |
383 | + } | |
384 | + tokens.push_back(atof(text.substr(start).c_str())); | |
385 | + return tokens; | |
386 | + } | |
387 | + // load a network in text file to a network class | |
388 | + void load_txt(std::string filename) | |
389 | + { | |
390 | + std::vector <std::string> file_contents; | |
391 | + std::ifstream file(filename); | |
392 | + std::string line; | |
393 | + std::vector<unsigned> id2vert; //this list stores the vertex ID associated with each network vertex | |
394 | + //for each line in the text file, store them as strings in file_contents | |
395 | + while (std::getline(file, line)) | |
396 | + { | |
397 | + std::stringstream ss(line); | |
398 | + file_contents.push_back(ss.str()); | |
399 | + } | |
400 | + unsigned int numEdges = atoi(file_contents[0].c_str()); //number of edges in the network | |
401 | + unsigned int I = atoi(file_contents[1].c_str()) ; //calculate the number of points3d on the first edge | |
402 | + unsigned int count = 1; unsigned int k = 2; // count is global counter through the file contents, k is for the vertices on the edges | |
403 | + // for each edge in the network. | |
404 | + for (unsigned int i = 0; i < numEdges; i ++ ) | |
405 | + { | |
406 | + // pre allocate a position vector p with number of points3d on the edge p | |
407 | + std::vector< stim::vec<T> > p(0, I); | |
408 | + // for each point on the nth edge | |
409 | + for (unsigned int j = k; j < I + k; j++) | |
410 | + { | |
411 | + // split the points3d of floats with separator space and form a float3 position vector out of them | |
412 | + p.push_back(split(file_contents[j], ' ')); | |
413 | + } | |
414 | + count += p.size() + 1; // increment count to point at the next edge in the network | |
415 | + I = atoi(file_contents[count].c_str()); // read in the points3d at the next edge and convert it to an integer | |
416 | + k = count + 1; | |
417 | + edge new_edge = p; // create an edge with a vector of points3d on the edge | |
418 | + E.push_back(new_edge); // push the edge into the network | |
419 | + } | |
420 | + unsigned int numVertices = atoi(file_contents[count].c_str()); // this line in the text file gives the number of distinct vertices | |
421 | + count = count + 1; // this line of text file gives the first verrtex | |
422 | + // push each vertex into V | |
423 | + for (unsigned int i = 0; i < numVertices; i ++) | |
424 | + { | |
425 | + vertex new_vertex = split(file_contents[count], ' '); | |
426 | + V.push_back(new_vertex); | |
427 | + count += atoi(file_contents[count + 1].c_str()) + 2; // Skip number of edge ids + 2 to point to the next vertex | |
428 | + } | |
429 | + } // end load_txt function | |
377 | 430 | }; //end stim::network class |
378 | 431 | }; //end stim namespace |
379 | 432 | #endif | ... | ... |