FileIO.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include "../core/Tracklet.h"
  12. namespace util
  13. {
  14. typedef std::vector<std::vector<std::vector<double>>> Vector3d;
  15. typedef std::vector<std::vector<double>> Vector2d;
  16. typedef std::unordered_map<std::string, double> ValueMap;
  17. typedef std::vector<ValueMap> ValueMapVector;
  18. /**
  19. * Utility class for file in- and output.
  20. */
  21. class FileIO
  22. {
  23. public:
  24. /**
  25. * Reads a CSV file and stores the values in a 3D array.
  26. * The first dimension is the first value of each row, used as a
  27. * index to bundle multiple rows with the same first value into a
  28. * single vector.
  29. * The second dimension is the row in the row bundle.
  30. * The third dimension is the value in that row.
  31. *
  32. * @param values The 3D array of values to store the read values in
  33. * @param file_name The name of the file to read
  34. * @param delimiter The value delimiter of the file
  35. */
  36. static void ReadCSV(Vector3d& values, const std::string& file_name, 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. *
  41. * @param values The 2D array of values to store the read values in
  42. * @param file_name The name of the file to read
  43. * @param delimiter The value delimiter of the file
  44. */
  45. static void ReadCSV(Vector2d& values, const std::string& file_name, char delimiter);
  46. /**
  47. * Lists all file names in the given folder.
  48. *
  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, std::vector<std::string>& file_names,
  54. bool sort = true);
  55. /**
  56. * Writes the specified graph into a CSV file with an format readable by
  57. * Matlab.
  58. *
  59. * @param graph The graph to write
  60. * @param file_name The name of the file to write
  61. */
  62. static void WriteCSVMatlab(DirectedGraph& graph, const std::string& file_name);
  63. /**
  64. * Writes the specified multi predecessor map into a CSV format
  65. * readable by Matlab to display all paths in the corresponding graph.
  66. *
  67. * @param map The multi predecessor map to extract the paths from
  68. * @param source The sink, this is the vertex where all paths begin
  69. * @param sink The sink, this is the vertex where all paths end
  70. * @param file_name The name of the file to write
  71. */
  72. static void WriteCSVMatlab(MultiPredecessorMap& map, Vertex& source, Vertex& sink,
  73. const std::string& file_name);
  74. /**
  75. * Reads a CSV file.
  76. * The first line of the CSV file is a header specifying the keys.
  77. * The values are stored with their specified key into one map per line.
  78. *
  79. * @param values A vector of maps to store the key-value pairs into
  80. * @param file_name The name of the file to read
  81. * @param delimiter The value delimiter of the file
  82. */
  83. static void ReadCSV(ValueMapVector& values, const std::string& file_name, char delimiter);
  84. /**
  85. * Reads a CSV file.
  86. * The header specifies the keys.
  87. * The values are stored with their specified key into one map per line.
  88. *
  89. * @param values A vector of maps to store the key-value pairs into
  90. * @param header A string containing the keys separated by the delimiter
  91. * @param file_name The name of the file to read
  92. * @param delimiter The value delimiter of the file
  93. */
  94. static void ReadCSV(ValueMapVector& values, const std::string& header,
  95. const std::string& file_name, char delimiter);
  96. /**
  97. * Writes the given tracks to the given file.
  98. *
  99. * @param tracks The tracks to store
  100. * @param file_name The path to the file to store the tracks in
  101. * @param delimiter The delimiter used to separate values
  102. */
  103. static void WriteTracks(std::vector<core::TrackletPtr>& tracks, const std::string& file_name,
  104. char delimiter);
  105. /**
  106. * Reads the tracks from the specified file.
  107. *
  108. * @param tracks A vector for storing the read tracks
  109. * @param file_name The path of the file to read from
  110. * @param delimiter The delimiter used to separate values
  111. */
  112. static void ReadTracks(std::vector<core::TrackletPtr>& tracks, const std::string& file_name,
  113. char delimiter);
  114. /**
  115. * Splits the input string at the delimiter and store each part into the return vector.
  116. * If there is nothing between two delimiters the part will be an empty string.
  117. *
  118. * @param input The string to split
  119. * @param delimiter The delimiter used to split, the delimiter will not be stored
  120. * @return A vector of parts like (for string of length n: [0,d)...(d,d)...(d,n))
  121. */
  122. static std::vector<std::string> split(const std::string& input, char delimiter);
  123. };
  124. }
  125. #endif //GBMOT_FILEIO_H