From 0a06dc7db6ac2a09e63d8fd91e9cb4af6d08c765 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Wed, 14 Nov 2018 19:50:19 +0100 Subject: [PATCH] Return waypoints and spawn points as pure Python lists --- PythonAPI/source/libcarla/Map.cpp | 28 +++----------------------- PythonAPI/source/libcarla/libcarla.cpp | 26 +++++++++++++++++++++--- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/PythonAPI/source/libcarla/Map.cpp b/PythonAPI/source/libcarla/Map.cpp index eda1abc1d..3e9f7fa37 100644 --- a/PythonAPI/source/libcarla/Map.cpp +++ b/PythonAPI/source/libcarla/Map.cpp @@ -9,8 +9,6 @@ #include #include -#include - #include namespace carla { @@ -49,37 +47,17 @@ static auto GetTopology(const carla::client::Map &self) { return result; } -static auto GenerateWaypoints(const carla::client::Map &self, double distance) { - namespace py = boost::python; - auto waypoints = self.GenerateWaypoints(distance); - py::list result; - for (auto &&waypoint : waypoints) { - result.append(waypoint); - } - return result; -} - void export_map() { using namespace boost::python; namespace cc = carla::client; namespace cg = carla::geom; - class_>("vector_of_transforms") - .def(vector_indexing_suite>()) - .def(self_ns::str(self_ns::self)) - ; - - class_>>("vector_of_waypoints") - .def(vector_indexing_suite>, true>()) - .def(self_ns::str(self_ns::self)) - ; - class_>("Map", no_init) .add_property("name", CALL_RETURNING_COPY(cc::Map, GetName)) - .def("get_spawn_points", CALL_RETURNING_COPY(cc::Map, GetRecommendedSpawnPoints)) + .def("get_spawn_points", CALL_RETURNING_LIST(cc::Map, GetRecommendedSpawnPoints)) .def("get_waypoint", &cc::Map::GetWaypoint, (arg("location"), arg("project_to_road")=true)) .def("get_topology", &GetTopology) - .def("generate_waypoints", &GenerateWaypoints) + .def("generate_waypoints", CALL_RETURNING_LIST_1(cc::Map, GenerateWaypoints, double), (args("distance"))) .def("to_opendrive", CALL_RETURNING_COPY(cc::Map, GetOpenDrive)) .def("save_to_disk", &SaveOpenDriveToDisk, (arg("path")="")) .def(self_ns::str(self_ns::self)) @@ -89,7 +67,7 @@ void export_map() { .add_property("transform", CALL_RETURNING_COPY(cc::Waypoint, GetTransform)) .add_property("road_id", &cc::Waypoint::GetRoadId) .add_property("lane_id", &cc::Waypoint::GetLaneId) - .def("next", &cc::Waypoint::Next, (args("distance"))) + .def("next", CALL_RETURNING_LIST_1(cc::Waypoint, Next, double), (args("distance"))) .def(self_ns::str(self_ns::self)) ; } diff --git a/PythonAPI/source/libcarla/libcarla.cpp b/PythonAPI/source/libcarla/libcarla.cpp index 0cb84fec6..223104835 100644 --- a/PythonAPI/source/libcarla/libcarla.cpp +++ b/PythonAPI/source/libcarla/libcarla.cpp @@ -28,9 +28,29 @@ } // Convenient for const requests that need to make a copy of the returned value. -#define CALL_RETURNING_COPY_1(cls, fn, _T1) +[](const cls &self, _T1 t1) \ - -> std::decay_t> { \ - return self.fn(std::forward<_T1>(t1)); \ +#define CALL_RETURNING_COPY_1(cls, fn, T1_) +[](const cls &self, T1_ t1) \ + -> std::decay_t> { \ + return self.fn(std::forward(t1)); \ + } + +// Convenient for const requests that needs to convert the return value to a +// Python list. +#define CALL_RETURNING_LIST(cls, fn) +[](const cls &self) { \ + boost::python::list result; \ + for (auto &&item : self.fn()) { \ + result.append(item); \ + } \ + return result; \ + } + +// Convenient for const requests that needs to convert the return value to a +// Python list. +#define CALL_RETURNING_LIST_1(cls, fn, T1_) +[](const cls &self, T1_ t1) { \ + boost::python::list result; \ + for (auto &&item : self.fn(std::forward(t1))) { \ + result.append(item); \ + } \ + return result; \ } template