FileIO.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // Created by wrede on 19.04.16.
  3. //
  4. #include "FileIO.h"
  5. namespace util
  6. {
  7. void FileIO::ReadCSV(
  8. const std::string &filename,
  9. const char &delimiter,
  10. core::Vector3d &values)
  11. {
  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()) - 1;
  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. if (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. }
  40. void FileIO::ReadCSV(
  41. const std::string &filename,
  42. const char &delimiter,
  43. core::Vector2d &values)
  44. {
  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. }
  70. }