|
| 1 | +#include "dataloader.hpp" |
| 2 | + |
| 3 | +#include <open3d/Open3D.h> |
| 4 | + |
| 5 | +#include <Eigen/Core> |
| 6 | +#include <filesystem> |
| 7 | +#include <fstream> |
| 8 | +#include <string> |
| 9 | +#include <utility> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +namespace fs = std::filesystem; |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +std::vector<std::string> ReadPointCloudFiles(const fs::path& pointcloud_path) { |
| 17 | + std::vector<std::string> filenames; |
| 18 | + for (const auto& file : fs::directory_iterator(pointcloud_path)) { |
| 19 | + if (file.path().extension() == ".ply") { |
| 20 | + filenames.emplace_back(file.path().string()); |
| 21 | + } |
| 22 | + } |
| 23 | + if (filenames.empty()) { |
| 24 | + std::cerr << pointcloud_path << "contains no files with .ply extension" |
| 25 | + << std::endl; |
| 26 | + exit(1); |
| 27 | + } |
| 28 | + std::sort(filenames.begin(), filenames.end()); |
| 29 | + |
| 30 | + return filenames; |
| 31 | +} |
| 32 | + |
| 33 | +Vector3dVector ExtractPointCloud(const std::string& filename) { |
| 34 | + open3d::geometry::PointCloud pointcloud; |
| 35 | + open3d::io::ReadPointCloudFromPLY(filename, pointcloud, |
| 36 | + open3d::io::ReadPointCloudOption()); |
| 37 | + return pointcloud.points_; |
| 38 | +} |
| 39 | + |
| 40 | +std::vector<Eigen::Matrix4d> ReadPoses(const std::string& filename) { |
| 41 | + std::ifstream file(filename); |
| 42 | + std::vector<Eigen::Matrix4d> poses; |
| 43 | + Eigen::Matrix4d pose = Eigen::Matrix4d::Identity(); |
| 44 | + while (file >> pose(0, 0) >> pose(0, 1) >> pose(0, 2) >> pose(0, 3) >> |
| 45 | + pose(1, 0) >> pose(1, 1) >> pose(1, 2) >> pose(1, 3) >> pose(2, 0) >> |
| 46 | + pose(2, 1) >> pose(2, 2) >> pose(2, 3)) { |
| 47 | + poses.emplace_back(pose); |
| 48 | + } |
| 49 | + return poses; |
| 50 | +} |
| 51 | + |
| 52 | +} // namespace |
| 53 | + |
| 54 | +namespace dataloader { |
| 55 | +Dataset::Dataset(const std::string& data_dir) { |
| 56 | + pointcloud_files_ = ReadPointCloudFiles(fs::path(data_dir) / "PLY/"); |
| 57 | + |
| 58 | + poses_.reserve(pointcloud_files_.size()); |
| 59 | + poses_ = ReadPoses(fs::path(data_dir) / "gt_poses.txt"); |
| 60 | +} |
| 61 | + |
| 62 | +PoseAndCloud Dataset::operator[]( |
| 63 | + const int idx) const { |
| 64 | + return std::make_pair(poses_[idx], ExtractPointCloud(pointcloud_files_[idx])); |
| 65 | +} |
| 66 | +} // namespace dataloader |
0 commit comments