Commit f5d463e5381d8b55193217d0c404f5d901baf030
1 parent
7b20b4e5
added the stim::cellset class
Showing
1 changed file
with
124 additions
and
0 deletions
Show diff stats
1 | +#ifndef STIM_CELLSET_H | ||
2 | +#define STIM_CELLSET_H | ||
3 | + | ||
4 | +#include <stim/math/vec3.h> | ||
5 | +#include <vector> | ||
6 | +#include <unordered_map> | ||
7 | +#include <fstream> | ||
8 | + | ||
9 | +namespace stim{ | ||
10 | + | ||
11 | +class cellset{ | ||
12 | +private: | ||
13 | + static const char delim = ' '; | ||
14 | +protected: | ||
15 | + std::vector<double*> cells; //vector storing field data for each cell | ||
16 | + std::unordered_map<std::string, size_t> fields; //unordered map storing field->index information for each field | ||
17 | + size_t ip[3]; //hard code to position indices (for speed) | ||
18 | + void init(){ | ||
19 | + | ||
20 | + } | ||
21 | +public: | ||
22 | + /// Constructor - create an empty cell set | ||
23 | + cellset(){ | ||
24 | + init(); | ||
25 | + } | ||
26 | + | ||
27 | + /// Constructor - load a cellset from a file | ||
28 | + cellset(std::string filename){ | ||
29 | + init(); //initialize an empty cellset | ||
30 | + load(filename); //load the cellset from an existing file | ||
31 | + } | ||
32 | + | ||
33 | + /// Loads a cellset from a file | ||
34 | + void load(std::string filename){ | ||
35 | + std::ifstream infile(filename); | ||
36 | + std::string header; //allocate space for the file header | ||
37 | + std::getline(infile, header); //get the file header | ||
38 | + | ||
39 | + // break the header into fields | ||
40 | + std::stringstream ss(header); //create a string stream | ||
41 | + std::string field; //store a single field name | ||
42 | + size_t i = 0; //current field index | ||
43 | + while (std::getline(ss, field, delim)) { //split the header into individual fields | ||
44 | + std::pair<std::string, size_t> p(field, i); //create a pair associating the header name with the index | ||
45 | + fields.insert(p); //insert the pair into the fields map | ||
46 | + i++; //increment the data index | ||
47 | + } | ||
48 | + size_t nfields = fields.size(); //store the number of fields for each cell | ||
49 | + | ||
50 | + //load each cell and all associated fields | ||
51 | + std::string cell_line; //string holds all information for a cell | ||
52 | + std::list<std::string> cell_list; //list will be temporary storage for the cell fields | ||
53 | + while(std::getline(infile, cell_line)){ //for each cell entry | ||
54 | + cell_list.push_back(cell_line); //push the cell entry into the list | ||
55 | + } | ||
56 | + | ||
57 | + //convert the list into actual data | ||
58 | + size_t ncells = cell_list.size(); //count the number of cells | ||
59 | + cells.resize(ncells); //allocate enough space in the array to store all cells | ||
60 | + for(size_t c = 0; c < ncells; c++){ //for each cell entry in the list | ||
61 | + cells[c] = (double*) malloc(sizeof(double) * nfields); //allocate enough space for each field | ||
62 | + std::stringstream fss(cell_list.front()); //turn the string representing the cell list into a stringstream | ||
63 | + for(size_t f = 0; f < nfields; f++){ //for each field | ||
64 | + fss>>cells[c][f]; //load the field | ||
65 | + } | ||
66 | + cell_list.pop_front(); //pop the read string off of the front of the list | ||
67 | + } | ||
68 | + infile.close(); //close the input file | ||
69 | + | ||
70 | + ip[0] = fields["x"]; //hard code the position indices for speed | ||
71 | + ip[1] = fields["y"]; // this assumes all cells have positions | ||
72 | + ip[2] = fields["z"]; | ||
73 | + } | ||
74 | + | ||
75 | + /// Return the value a specified field for a cell | ||
76 | + /// @param c is the cell index | ||
77 | + /// @param f is the field | ||
78 | + double value(size_t c, std::string f){ | ||
79 | + size_t idx = fields[f]; | ||
80 | + return cells[c][idx]; | ||
81 | + } | ||
82 | + | ||
83 | + /// Return the position of cell [i] | ||
84 | + stim::vec3<double> p(size_t i){ | ||
85 | + stim::vec3<double> pos(cells[i][ip[0]], cells[i][ip[1]], cells[i][ip[2]]); | ||
86 | + return pos; | ||
87 | + } | ||
88 | + | ||
89 | + /// Return the number of cells in the set | ||
90 | + size_t size(){ | ||
91 | + return cells.size(); | ||
92 | + } | ||
93 | + | ||
94 | + /// Return the maximum value of a field in this cell set | ||
95 | + double max(std::string field){ | ||
96 | + size_t idx = fields[field]; //get the field index | ||
97 | + size_t ncells = cells.size(); //get the total number of cells | ||
98 | + double maxval, val; //stores the current and maximum values | ||
99 | + for(size_t c = 0; c < ncells; c++){ //for each cell | ||
100 | + val = cells[c][idx]; //get the field value for this cell | ||
101 | + if(c == 0) maxval = val; //if this is the first cell, just assign the maximum | ||
102 | + else if(val > maxval) maxval = val; // otherwise text for the size of val and assign it as appropriate | ||
103 | + } | ||
104 | + return maxval; | ||
105 | + } | ||
106 | + | ||
107 | + /// Return the maximum value of a field in this cell set | ||
108 | + double min(std::string field){ | ||
109 | + size_t idx = fields[field]; //get the field index | ||
110 | + size_t ncells = cells.size(); //get the total number of cells | ||
111 | + double minval, val; //stores the current and maximum values | ||
112 | + for(size_t c = 0; c < ncells; c++){ //for each cell | ||
113 | + val = cells[c][idx]; //get the field value for this cell | ||
114 | + if(c == 0) minval = val; //if this is the first cell, just assign the maximum | ||
115 | + else if(val < minval) minval = val; // otherwise text for the size of val and assign it as appropriate | ||
116 | + } | ||
117 | + return minval; | ||
118 | + } | ||
119 | + | ||
120 | + | ||
121 | +}; //end class cellset | ||
122 | +}; //end namespace stim | ||
123 | + | ||
124 | +#endif | ||
0 | \ No newline at end of file | 125 | \ No newline at end of file |