FileIO.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // Created by wrede on 19.04.16.
  3. //
  4. #include "FileIO.h"
  5. #include "Logger.h"
  6. namespace util
  7. {
  8. void FileIO::ReadCSV(core::Vector3d& values, const std::string& filename,
  9. const char& delimiter)
  10. {
  11. Logger::LogInfo("Reading CSV file");
  12. std::ifstream in(filename, std::ifstream::in);
  13. std::string line;
  14. // Read lines while the reader is in good condition
  15. while (in.good() && !in.eof())
  16. {
  17. getline(in, line);
  18. // Ignore empty lines
  19. if (line.size() == 0) continue;
  20. // Get frame index
  21. size_t dIndex = line.find(delimiter);
  22. size_t frameIndex = std::stoul(line.substr(0, dIndex).c_str());
  23. // Extract point values
  24. std::vector<double> pointValues;
  25. while (dIndex != std::string::npos)
  26. {
  27. line = line.substr(dIndex + 1);
  28. dIndex = line.find(delimiter);
  29. pointValues.push_back(std::stof(line.substr(0, dIndex).c_str()));
  30. }
  31. // Add point data to detection data
  32. while (frameIndex >= values.size())
  33. {
  34. values.push_back(std::vector<std::vector<double>>());
  35. }
  36. values[frameIndex].push_back(pointValues);
  37. }
  38. in.close();
  39. Logger::LogDebug("frame count " + std::to_string(values.size()));
  40. }
  41. void FileIO::ReadCSV(core::Vector2d& values, const std::string& filename,
  42. const char& delimiter)
  43. {
  44. Logger::LogInfo("Reading CSV file");
  45. std::ifstream in(filename, std::ifstream::in);
  46. std::string line;
  47. // Read lines while the reader is in good condition and the
  48. // end of file is not reached
  49. while (in.good() && !in.eof())
  50. {
  51. getline(in, line);
  52. // Ignore empty lines
  53. if (line.size() == 0) continue;
  54. // Extract point values
  55. size_t dIndex;
  56. std::vector<double> pointValues;
  57. do
  58. {
  59. dIndex = line.find(delimiter);
  60. pointValues.push_back(
  61. std::stof(line.substr(0, dIndex).c_str()));
  62. line = line.substr(dIndex + 1);
  63. }
  64. while (dIndex != std::string::npos);
  65. // Add point data to detection data
  66. values.push_back(pointValues);
  67. }
  68. in.close();
  69. Logger::LogDebug("line count " + std::to_string(values.size()));
  70. }
  71. }