Skip to content

Commit

Permalink
fix(map_loader): use std::filesystem to load pcd files in pointcloud_…
Browse files Browse the repository at this point in the history
…map_loader (tier4#942)

* fix(map_loader): use std::filesystem to load pcd files in pointcloud_map_loader

Signed-off-by: RyuYamamoto <ryu.yamamoto@tier4.jp>

* fix(map_loader): remove c_str

Signed-off-by: RyuYamamoto <ryu.yamamoto@tier4.jp>

* fix(map_loader): replace c_str to string

Signed-off-by: RyuYamamoto <ryu.yamamoto@tier4.jp>
  • Loading branch information
RyuYamamoto authored and boyali committed Oct 19, 2022
1 parent dbd13f0 commit abfa230
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@
#include <glob.h>
#include <pcl/io/pcd_io.h>
#include <pcl_conversions/pcl_conversions.h>
#include <rcutils/filesystem.h> // To be replaced by std::filesystem in C++17

#include <filesystem>
#include <string>
#include <vector>

namespace fs = std::filesystem;

namespace
{
bool isPcdFile(const std::string & p)
{
if (!rcutils_is_file(p.c_str())) {
if (fs::is_directory(p)) {
return false;
}

const auto ext = p.substr(p.find_last_of(".") + 1);
const std::string ext = fs::path(p).extension();

if (ext != "pcd" && ext != "PCD") {
if (ext != ".pcd" && ext != ".PCD") {
return false;
}

Expand All @@ -70,21 +72,21 @@ PointCloudMapLoaderNode::PointCloudMapLoaderNode(const rclcpp::NodeOptions & opt
std::vector<std::string> pcd_paths{};

for (const auto & p : pcd_paths_or_directory) {
if (!rcutils_exists(p.c_str())) {
if (!fs::exists(p)) {
RCLCPP_ERROR_STREAM(get_logger(), "invalid path: " << p);
}

if (isPcdFile(p)) {
pcd_paths.push_back(p);
}

if (rcutils_is_directory(p.c_str())) {
glob_t glob_buf;
glob((p + "/*.pcd").c_str(), 0, NULL, &glob_buf);
for (size_t i = 0; i < glob_buf.gl_pathc; ++i) {
pcd_paths.push_back(glob_buf.gl_pathv[i]);
if (fs::is_directory(p)) {
for (const auto & file : fs::directory_iterator(p)) {
const auto filename = file.path().string();
if (isPcdFile(filename)) {
pcd_paths.push_back(filename);
}
}
globfree(&glob_buf);
}
}

Expand Down

0 comments on commit abfa230

Please sign in to comment.