FileIO.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // Created by wrede on 19.04.16.
  3. //
  4. #include <algorithm>
  5. #include "FileIO.h"
  6. #include "Logger.h"
  7. #include "dirent.h"
  8. namespace util
  9. {
  10. void FileIO::ReadCSV(Vector3d& values,
  11. const std::string& filename,
  12. char delimiter)
  13. {
  14. Logger::LogInfo("Reading CSV file");
  15. std::ifstream in(filename, std::ifstream::in);
  16. std::string line;
  17. // Read lines while the reader is in good condition
  18. while (in.good() && !in.eof())
  19. {
  20. getline(in, line);
  21. // Ignore empty lines
  22. if (line.size() == 0) continue;
  23. // Get frame index
  24. size_t dIndex = line.find(delimiter);
  25. size_t frameIndex = std::stoul(line.substr(0, dIndex).c_str());
  26. // Extract point values
  27. std::vector<double> pointValues;
  28. while (dIndex != std::string::npos)
  29. {
  30. line = line.substr(dIndex + 1);
  31. dIndex = line.find(delimiter);
  32. pointValues.push_back(std::stof(line.substr(0, dIndex).c_str()));
  33. }
  34. // Add point data to detection data
  35. while (frameIndex >= values.size())
  36. {
  37. values.push_back(std::vector<std::vector<double>>());
  38. }
  39. values[frameIndex].push_back(pointValues);
  40. }
  41. in.close();
  42. Logger::LogDebug("frame count " + std::to_string(values.size()));
  43. }
  44. void FileIO::ReadCSV(Vector2d& values,
  45. const std::string& filename,
  46. char delimiter)
  47. {
  48. Logger::LogInfo("Reading CSV file");
  49. std::ifstream in(filename, std::ifstream::in);
  50. std::string line;
  51. // Read lines while the reader is in good condition and the
  52. // end of file is not reached
  53. while (in.good() && !in.eof())
  54. {
  55. getline(in, line);
  56. // Ignore empty lines
  57. if (line.size() == 0) continue;
  58. // Extract point values
  59. size_t dIndex;
  60. std::vector<double> pointValues;
  61. do
  62. {
  63. dIndex = line.find(delimiter);
  64. pointValues.push_back(
  65. std::stof(line.substr(0, dIndex).c_str()));
  66. line = line.substr(dIndex + 1);
  67. }
  68. while (dIndex != std::string::npos);
  69. // Add point data to detection data
  70. values.push_back(pointValues);
  71. }
  72. in.close();
  73. Logger::LogDebug("line count " + std::to_string(values.size()));
  74. }
  75. void FileIO::ListFiles(const std::string& folder,
  76. std::vector<std::string>& file_names,
  77. bool sort)
  78. {
  79. Logger::LogInfo("Listing files in folder");
  80. DIR* dir;
  81. struct dirent *ent;
  82. if ((dir = opendir(folder.c_str())) != NULL)
  83. {
  84. int offset = 2;
  85. while ((ent = readdir(dir)) != NULL)
  86. {
  87. if (offset <= 0)
  88. {
  89. file_names.push_back(ent->d_name);
  90. }
  91. offset--;
  92. }
  93. closedir(dir);
  94. if (sort)
  95. {
  96. std::sort(file_names.begin(), file_names.end());
  97. }
  98. Logger::LogDebug("file count " + std::to_string(file_names.size()));
  99. }
  100. else
  101. {
  102. Logger::LogError("Could not open folder");
  103. }
  104. }
  105. void FileIO::WriteCSVMatlab(DirectedGraph& graph,
  106. const std::string& file_name,
  107. char delimiter)
  108. {
  109. std::ofstream out(file_name, std::ofstream::out);
  110. // Iterate all outgoing edges of every vertex
  111. EdgeWeightMap weights = boost::get(boost::edge_weight, graph);
  112. VertexIndexMap indices = boost::get(boost::vertex_index, graph);
  113. boost::graph_traits<DirectedGraph>::vertex_iterator vi, vi_end;
  114. boost::graph_traits<DirectedGraph>::out_edge_iterator oei, oei_end;
  115. for (boost::tie(vi, vi_end) = boost::vertices(graph); vi != vi_end; ++vi)
  116. {
  117. for (boost::tie(oei, oei_end) = boost::out_edges(*vi, graph);
  118. oei != oei_end; ++oei)
  119. {
  120. // Write the edge to file
  121. out << indices[boost::source(*oei, graph)] << delimiter
  122. << indices[boost::target(*oei, graph)] << delimiter
  123. << weights[*oei] << std::endl;
  124. }
  125. }
  126. out.close();
  127. }
  128. }