FileIO.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Created by wrede on 19.04.16.
  3. //
  4. #ifndef GBMOT_FILEIO_H
  5. #define GBMOT_FILEIO_H
  6. #include <string>
  7. #include <fstream>
  8. #include <vector>
  9. #include "../core/ObjectData.h"
  10. #include "../graph/Definitions.h"
  11. namespace util
  12. {
  13. typedef std::vector<std::vector<std::vector<double>>> Vector3d;
  14. typedef std::vector<std::vector<double>> Vector2d;
  15. typedef std::unordered_map<std::string, double> ValueMap;
  16. typedef std::vector<ValueMap> ValueMapVector;
  17. /**
  18. * Utility class for file in- and output.
  19. */
  20. class FileIO
  21. {
  22. public:
  23. /**
  24. * Reads a CSV file and stores the values in a 3D array.
  25. * The first dimension is the first value of each row, used as a
  26. * index to bundle multiple rows with the same first value into a
  27. * single vector.
  28. * The second dimension is the row in the row bundle.
  29. * The third dimension is the value in that row.
  30. * @param values The 3D array of values to store the read values in
  31. * @param file_name The name of the file to read
  32. * @param delimiter The value delimiter of the file
  33. */
  34. static void ReadCSV(Vector3d& values,
  35. const std::string& file_name,
  36. char delimiter);
  37. /**
  38. * Reads a CSV file and stores the values in a 2D array.
  39. * The first dimension is the row and the second the value in that row.
  40. * @param values The 2D array of values to store the read values in
  41. * @param file_name The name of the file to read
  42. * @param delimiter The value delimiter of the file
  43. */
  44. static void ReadCSV(Vector2d& values,
  45. const std::string& file_name,
  46. char delimiter);
  47. /**
  48. * Lists all file names in the given folder.
  49. * @param folder The folder to look into
  50. * @param file_names The name of the files in the folder
  51. * @param sort True, if the files should be sorted alphabetically
  52. */
  53. static void ListFiles(const std::string& folder,
  54. std::vector<std::string>& file_names,
  55. bool sort = true);
  56. /**
  57. * Writes the specified graph into a CSV file with an format readable by
  58. * Matlab.
  59. *
  60. * @param graph The graph to write
  61. * @param file_name The name of the file to write
  62. */
  63. static void WriteCSVMatlab(DirectedGraph& graph,
  64. const std::string& file_name);
  65. /**
  66. * Writes the specified multi predecessor map into a CSV format
  67. * readable by Matlab to display all paths in the corresponding graph.
  68. *
  69. * @param map The multi predecessor map to extract the paths from
  70. * @param source The sink, this is the vertex where all paths begin
  71. * @param sink The sink, this is the vertex where all paths end
  72. * @param file_name The name of the file to write
  73. */
  74. static void WriteCSVMatlab(MultiPredecessorMap& map,
  75. Vertex& source, Vertex& sink,
  76. const std::string& file_name);
  77. /**
  78. * Reads a CSV file.
  79. * The first line of the CSV file is a header specifying the keys.
  80. * The values are stored with their specified key into one map per line.
  81. *
  82. * @param values A vector of maps to store the key-value pairs into
  83. * @param file_name The name of the file to read
  84. * @param delimiter The value delimiter of the file
  85. */
  86. static void ReadCSV(ValueMapVector& values,
  87. const std::string& file_name,
  88. char delimiter);
  89. /**
  90. * Reads a CSV file.
  91. * The header specifies the keys.
  92. * The values are stored with their specified key into one map per line.
  93. *
  94. * @param values A vector of maps to store the key-value pairs into
  95. * @param header A string containing the keys separated by the delimiter
  96. * @param file_name The name of the file to read
  97. * @param delimiter The value delimiter of the file
  98. */
  99. static void ReadCSV(ValueMapVector& values,
  100. const std::string& header,
  101. const std::string& file_name,
  102. char delimiter);
  103. };
  104. }
  105. #endif //GBMOT_FILEIO_H