Add methods for saving point clouds in ply format

This commit is contained in:
nsubiron 2018-09-30 19:03:09 +02:00
parent d02d9ced3c
commit 4ad2095c92
5 changed files with 77 additions and 0 deletions

View File

@ -100,6 +100,7 @@
- `channels`
- `raw_data`
- `get_point_count(channel)`
- `save_to_disk(path)`
- `__len__()`
- `__iter__()`

View File

@ -39,6 +39,12 @@ file(GLOB libcarla_carla_opendrive_sources
set(libcarla_sources "${libcarla_sources};${libcarla_carla_opendrive_sources}")
install(FILES ${libcarla_carla_opendrive_sources} DESTINATION include/carla/opendrive)
file(GLOB libcarla_carla_pointcloud_sources
"${libcarla_source_path}/carla/pointcloud/*.cpp"
"${libcarla_source_path}/carla/pointcloud/*.h")
set(libcarla_sources "${libcarla_sources};${libcarla_carla_pointcloud_sources}")
install(FILES ${libcarla_carla_pointcloud_sources} DESTINATION include/carla/pointcloud)
file(GLOB libcarla_carla_road_sources
"${libcarla_source_path}/carla/road/*.cpp"
"${libcarla_source_path}/carla/road/*.h")

View File

@ -0,0 +1,29 @@
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#include "carla/pointcloud/PLY.h"
#include <iomanip>
namespace carla {
namespace pointcloud {
void PLY::WriteHeader(OutputFile &out, size_t number_of_points) {
out << "ply\n"
"format ascii 1.0\n"
"element vertex " << number_of_points << "\n"
"property float32 x\n"
"property float32 y\n"
"property float32 z\n"
// "property uchar diffuse_red\n"
// "property uchar diffuse_green\n"
// "property uchar diffuse_blue\n"
"end_header\n";
out << std::fixed << std::setprecision(4u);
}
} // namespace pointcloud
} // namespace carla

View File

@ -0,0 +1,34 @@
// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
// de Barcelona (UAB).
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#pragma once
#include "carla/OutputFile.h"
#include <iterator>
namespace carla {
namespace pointcloud {
class PLY {
public:
template <typename PointIt>
static void SaveToDisk(const std::string &path, PointIt begin, PointIt end) {
OutputFile out(path, ".ply");
WriteHeader(out, std::distance(begin, end));
for (; begin != end; ++begin) {
out << begin->x << ' ' << begin->y << ' ' << begin->z << '\n';
}
}
private:
static void WriteHeader(OutputFile &out, size_t number_of_points);
};
} // namespace pointcloud
} // namespace carla

View File

@ -5,6 +5,7 @@
// For a copy, see <https://opensource.org/licenses/MIT>.
#include <carla/client/Sensor.h>
#include <carla/pointcloud/PLY.h>
#include <carla/sensor/SensorData.h>
#include <carla/sensor/data/Image.h>
#include <carla/sensor/data/LidarMeasurement.h>
@ -77,6 +78,11 @@ static auto GetRawDataAsBuffer(T &self) {
return boost::python::object(boost::python::handle<>(ptr));
}
template <typename T>
static void SavePointCloudToDisk(T &self, const std::string &path) {
carla::pointcloud::PLY::SaveToDisk(path, self.begin(), self.end());
}
void export_sensor() {
using namespace boost::python;
namespace cc = carla::client;
@ -105,6 +111,7 @@ void export_sensor() {
.add_property("channels", &csd::LidarMeasurement::GetChannelCount)
.add_property("raw_data", &GetRawDataAsBuffer<csd::LidarMeasurement>)
.def("get_point_count", &csd::LidarMeasurement::GetPointCount, (arg("channel")))
.def("save_to_disk", &SavePointCloudToDisk<csd::LidarMeasurement>, (arg("path")))
.def("__len__", &csd::LidarMeasurement::size)
.def("__iter__", iterator<csd::LidarMeasurement>())
.def(self_ns::str(self_ns::self))