n-Dimensional Fast Methods  0.7
 All Classes Functions Variables Typedefs Pages
gridwriter.hpp
1 
20 #ifndef GRIDWRITER_H_
21 #define GRIDWRITER_H_
22 
23 #include <fstream>
24 
25 #include <fast_methods/ndgridmap/ndgridmap.hpp>
26 
27 // TODO: include checks which ensure that the grids are adecuate for the functions used.
28 // TODO: there should be a check when writing grid: it is written already? erase and write. Something like that.
29 class GridWriter {
30  public:
46  template <class T, size_t ndims>
47  static void saveGridValues
48  (const char * filename, nDGridMap<T, ndims> & grid) {
49  std::ofstream ofs;
50  ofs.open (filename, std::ofstream::out | std::ofstream::trunc);
51 
52  ofs << grid.getCell(0).type() << '\n';
53  ofs << grid.getLeafSize() << '\n' << ndims;
54 
55  std::array<unsigned int, ndims> dimsize = grid.getDimSizes();
56  for (unsigned int i = 0; i < ndims; ++i)
57  ofs << '\n' << dimsize[i] << "\t";
58 
59  for (unsigned int i = 0; i < grid.size(); ++i)
60  ofs << '\n' << grid.getCell(i).getValue();
61 
62  ofs.close();
63  }
64 
80  template <class T, size_t ndims>
81  static void saveVelocities
82  (const char * filename, nDGridMap<T, ndims> & grid) {
83  std::ofstream ofs;
84  ofs.open (filename, std::ofstream::out | std::ofstream::trunc);
85 
86  ofs << grid.getCell(0).type() << '\n';
87  ofs << grid.getLeafSize() << '\n' << ndims;
88 
89  std::array<unsigned int, ndims> dimsize = grid.getDimSizes();
90  for (unsigned int i = 0; i < ndims; ++i)
91  ofs << '\n' << dimsize[i] << "\t";
92 
93  for (unsigned int i = 0; i < grid.size(); ++i)
94  ofs << '\n' << grid.getCell(i).getVelocity();
95 
96  ofs.close();
97  }
98 
110  template <class T, size_t ndims>
111  static void savePath
112  (const char * filename, nDGridMap<T, ndims> & grid, std::vector< std::array<double,ndims> > & path) {
113  std::ofstream ofs;
114  ofs.open (filename, std::ofstream::out | std::ofstream::trunc);
115 
116  ofs << grid.getLeafSize() << '\n' << ndims;
117 
118  std::array<unsigned int, ndims> dimsize = grid.getDimSizes();
119  for (unsigned int i = 0; i < ndims; ++i)
120  ofs << '\n'<< dimsize[i] << "\t";
121 
122  for (unsigned int i = 0; i < path.size(); ++i) {
123  ofs << '\n';
124  for (unsigned int j = 0; j < ndims; ++j)
125  ofs << path[i][j] << "\t" ;
126  }
127 
128  ofs.close();
129  }
130 
142  template <class T, size_t ndims>
143  static void savePathVelocity
144  (const char * filename, nDGridMap<T, ndims> & grid, std::vector< std::array<double,ndims> > & path, std::vector <double> path_velocity) {
145  std::ofstream ofs;
146  ofs.open (filename, std::ofstream::out | std::ofstream::trunc);
147 
148  ofs << grid.getLeafSize() << '\n' << ndims;
149 
150  std::array<unsigned int, ndims> dimsize = grid.getDimSizes();
151  for (unsigned int i = 0; i < ndims; ++i)
152  ofs << '\n' << dimsize[i];
153 
154  for (unsigned int i = 0; i < path.size(); ++i) {
155  ofs <<'\n';
156  for (unsigned int j = 0; j < ndims; ++j)
157  ofs << path[i][j] << "\t" ;
158  ofs << path_velocity[i];
159  }
160 
161  ofs.close();
162  }
163 };
164 
165 #endif /* GRIDWRITER_H_ */
static void savePathVelocity(const char *filename, nDGridMap< T, ndims > &grid, std::vector< std::array< double, ndims > > &path, std::vector< double > path_velocity)
Saves the 2D path with velocity values in an ASCII file with the following format: ...
Definition: gridwriter.hpp:144
static void saveGridValues(const char *filename, nDGridMap< T, ndims > &grid)
Saves grid values in ASCII format into the specified file.
Definition: gridwriter.hpp:48
Auxiliar class which helps to save nDGridMaps into text files.
Definition: gridwriter.hpp:29
Templated class which represents a n-dimensional grid map. Its cells are assumed to be cubic...
Definition: ndgridmap.hpp:47
T & getCell(unsigned int idx)
Returns the cell with index idx.
Definition: ndgridmap.hpp:110
std::array< unsigned int, ndims > getDimSizes() const
Returns the size of each dimension.
Definition: ndgridmap.hpp:115
static void saveVelocities(const char *filename, nDGridMap< T, ndims > &grid)
Saves grid velocities in ASCII format into the specified file.
Definition: gridwriter.hpp:82
unsigned int size() const
Returns number of cells in the grid.
Definition: ndgridmap.hpp:252
static void savePath(const char *filename, nDGridMap< T, ndims > &grid, std::vector< std::array< double, ndims > > &path)
Saves the 2D path in an ASCII file with the following format:
Definition: gridwriter.hpp:112
double getLeafSize() const
Returns the leaf size of the grid.
Definition: ndgridmap.hpp:104