Expose road maps to Python

This commit is contained in:
nsubiron 2018-10-24 22:42:50 +02:00
parent 6b7ad14665
commit bf0685d115
27 changed files with 246 additions and 179 deletions

View File

@ -18,6 +18,7 @@
- `id`
- `map_name`
- `get_blueprint_library()`
- `get_map()`
- `get_spectator()`
- `get_weather()`
- `set_weather(weather_parameters)`
@ -152,6 +153,18 @@
- `__eq__(other)`
- `__ne__(other)`
## `carla.Map`
- `name`
- `get_spawn_points()`
- `get_waypoint(location, project_to_road=True)`
- `to_opendrive()`
## `carla.Waypoint`
- `transform`
- `next(distance)`
## `carla.WeatherParameters`
- `cloudyness`

View File

@ -4,27 +4,38 @@
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
#include "carla/road/Map.h"
#include "carla/client/Map.h"
#include "carla/client/Waypoint.h"
#include "carla/opendrive/OpenDrive.h"
#include "carla/road/Map.h"
namespace carla {
namespace client {
Map::Map(std::string name, std::string open_drive)
: _name(std::move(name)),
_open_drive(std::move(open_drive)) {
_map = std::make_shared<road::Map>(opendrive::OpenDrive::Load(open_drive));
static auto MakeMap(const std::string &opendrive_contents) {
return opendrive::OpenDrive::Load(opendrive_contents);
}
const std::string &Map::GetOpenDrive() const {
return _open_drive;
Map::Map(rpc::MapInfo description)
: _description(std::move(description)),
_map(MakeMap(_description.open_drive_file)) {}
Map::~Map() = default;
SharedPtr<Waypoint> Map::GetWaypoint(
const geom::Location &location,
bool project_to_road) const {
Optional<road::element::Waypoint> waypoint;
if (project_to_road) {
waypoint = _map->GetClosestWaypointOnRoad(location);
} else {
waypoint = _map->GetWaypoint(location);
}
return waypoint.has_value() ?
SharedPtr<Waypoint>(new Waypoint{shared_from_this(), *waypoint}) :
nullptr;
}
// @todo
/*detail::Waypoint Map::GetWaypoint(const geom::Location &loc) const {
}*/
} // namespace client
} // namespace carla

View File

@ -8,41 +8,46 @@
#include "carla/Memory.h"
#include "carla/NonCopyable.h"
#include "carla/client/detail/Waypoint.h"
#include "carla/rpc/MapInfo.h"
#include <string>
namespace carla {
namespace road {
class Map;
}
namespace road { class Map; }
namespace client {
class Waypoint;
class Map
: public EnableSharedFromThis<Map>,
private NonCopyable {
private:
public:
Map(Map &&) = default;
Map &operator=(Map &&) = default;
explicit Map(rpc::MapInfo description);
const std::string &GetOpenDrive() const;
~Map();
detail::Waypoint GetWaypoint(const geom::Location &) const;
const std::string &GetName() const {
return _description.name;
}
const std::string &GetOpenDrive() const {
return _description.open_drive_file;
}
const std::vector<geom::Transform> &GetRecommendedSpawnPoints() const {
return _description.recommended_spawn_points;
}
SharedPtr<Waypoint> GetWaypoint(
const geom::Location &location,
bool project_to_road = true) const;
private:
explicit Map(std::string name, std::string open_drive);
rpc::MapInfo _description;
explicit Map(std::shared_ptr<road::Map> map) : _map(map) {}
std::string _name;
std::string _open_drive;
std::shared_ptr<road::Map> _map;
SharedPtr<road::Map> _map;
};
} // namespace client

View File

@ -0,0 +1,31 @@
// 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/client/Waypoint.h"
#include "carla/client/Map.h"
namespace carla {
namespace client {
Waypoint::Waypoint(SharedPtr<const Map> parent, road::element::Waypoint waypoint)
: _parent(std::move(parent)),
_waypoint(std::move(waypoint)) {}
Waypoint::~Waypoint() = default;
std::vector<SharedPtr<Waypoint>> Waypoint::Next(double distance) const {
auto waypoints = _waypoint.Next(distance);
std::vector<SharedPtr<Waypoint>> result;
result.reserve(waypoints.size());
for (auto &waypoint : waypoints) {
result.emplace_back(SharedPtr<Waypoint>(new Waypoint(_parent, std::move(waypoint))));
}
return result;
}
} // namespace client
} // namespace carla

View File

@ -0,0 +1,43 @@
// 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/Memory.h"
#include "carla/NonCopyable.h"
#include "carla/road/element/Waypoint.h"
namespace carla {
namespace client {
class Map;
class Waypoint
: public EnableSharedFromThis<Waypoint>,
private NonCopyable {
public:
~Waypoint();
const geom::Transform &GetTransform() const {
return _waypoint.GetTransform();
}
std::vector<SharedPtr<Waypoint>> Next(double distance) const;
private:
friend class Map;
Waypoint(SharedPtr<const Map> parent, road::element::Waypoint waypoint);
SharedPtr<const Map> _parent;
road::element::Waypoint _waypoint;
};
} // namespace client
} // namespace carla

View File

@ -25,6 +25,10 @@ namespace client {
return _episode.Lock()->GetCurrentMapName();
}
SharedPtr<Map> World::GetMap() const {
return _episode.Lock()->GetCurrentMap();
}
SharedPtr<BlueprintLibrary> World::GetBlueprintLibrary() const {
return _episode.Lock()->GetBlueprintLibrary();
}

View File

@ -20,6 +20,7 @@ namespace client {
class ActorBlueprint;
class ActorList;
class BlueprintLibrary;
class Map;
class World {
public:
@ -41,6 +42,9 @@ namespace client {
/// usual map name of the town.
const std::string &GetMapName() const;
/// Return the map that describes this world.
SharedPtr<Map> GetMap() const;
/// Return the list of blueprints available in this world. This blueprints
/// can be used to spawning actor into the world.
SharedPtr<BlueprintLibrary> GetBlueprintLibrary() const;

View File

@ -8,6 +8,7 @@
#include "carla/Logging.h"
#include "carla/client/BlueprintLibrary.h"
#include "carla/client/Map.h"
#include "carla/client/Sensor.h"
#include "carla/client/detail/ActorFactory.h"
#include "carla/sensor/Deserializer.h"
@ -63,6 +64,10 @@ namespace detail {
return EpisodeProxy{shared_from_this()};
}
SharedPtr<Map> Simulator::GetCurrentMap() {
return MakeShared<Map>(_client.GetMapInfo());
}
// ===========================================================================
// -- Access to global objects in the episode --------------------------------
// ===========================================================================

View File

@ -25,6 +25,7 @@ namespace client {
class ActorBlueprint;
class BlueprintLibrary;
class Map;
class Sensor;
namespace detail {
@ -60,13 +61,15 @@ namespace detail {
return _episode->GetId();
}
const std::string &GetCurrentMapName() const {
const std::string &GetCurrentMapName() {
DEBUG_ASSERT(_episode != nullptr);
return _episode->GetMapName();
}
EpisodeProxy GetCurrentEpisode();
SharedPtr<Map> GetCurrentMap();
/// @}
// =========================================================================
/// @name Garbage collection policy

View File

@ -1,24 +0,0 @@
// 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/client/detail/Waypoint.h"
namespace carla {
namespace client {
namespace detail {
Waypoint::~Waypoint() = default; // to ensure we delete _map
// @todo
/*std::vector<Waypoint> Waypoint::NextWaypoints(double dist) {
std::vector<Waypoint> v;
return v;
}*/
} // namespace detail
} // namespace client
} // namespace carla

View File

@ -1,61 +0,0 @@
// 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/Memory.h"
#include "carla/geom/Location.h"
namespace carla {
namespace client {
class Map;
namespace detail {
struct Info {
size_t _id;
double _speed_limit;
double _width;
double _heading;
};
class Waypoint {
public:
Waypoint()
: _pos(),
_heading(0.0) {}
Waypoint(const geom::Location &p)
: _pos(p),
_heading(0.0) {}
Waypoint(const geom::Location &p, double heading)
: _pos(p),
_heading(heading) {}
~Waypoint();
std::vector<Waypoint> NextWaypoints(double dist);
const Info &GetInfo() {
return _info;
}
private:
geom::Location _pos;
double _heading;
Info _info;
SharedPtr<Map> _map;
};
} // namespace detail
} // namespace client
} // namespace carla

View File

@ -46,7 +46,7 @@ namespace opendrive {
}
}
road::Map OpenDrive::Load(const std::string &file) {
SharedPtr<road::Map> OpenDrive::Load(const std::string &file) {
carla::opendrive::types::OpenDriveData open_drive_road;
OpenDriveParser::Parse(file.c_str(), open_drive_road);
carla::road::MapBuilder mapBuilder;
@ -168,7 +168,7 @@ namespace opendrive {
return mapBuilder.Build();
}
road::Map OpenDrive::Load(std::istream &input) {
SharedPtr<road::Map> OpenDrive::Load(std::istream &input) {
UNUSED(input);
carla::road::MapBuilder mapBuilder;

View File

@ -6,6 +6,7 @@
#pragma once
#include "carla/Memory.h"
#include "carla/road/Map.h"
#include <istream>
@ -17,8 +18,8 @@ namespace opendrive {
class OpenDrive {
public:
static road::Map Load(std::istream &input);
static road::Map Load(const std::string &file);
static SharedPtr<road::Map> Load(std::istream &input);
static SharedPtr<road::Map> Load(const std::string &file);
static void Dump(const road::Map &map, std::ostream &output);
};

View File

@ -1,38 +0,0 @@
// 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/road/Map.h"
using namespace carla::road::element;
namespace carla {
namespace road {
bool Map::ExistId(id_type id) const {
return _elements.count(id);
}
const RoadSegment *Map::GetRoad(id_type id) const {
if (ExistId(id)) {
return _elements.find(id)->second.get();
}
return nullptr;
}
std::vector<id_type> Map::GetAllIds() const {
std::vector<id_type> result;
for (auto &&e : _elements) {
result.emplace_back(e.first);
}
return result;
}
uint32_t Map::GetRoadCount() const {
return _elements.size();
}
} // namespace road
} // namespace carla

View File

@ -7,17 +7,16 @@
#pragma once
#include "carla/Memory.h"
#include "carla/Optional.h"
#include "carla/NonCopyable.h"
#include "carla/Optional.h"
#include "carla/road/MapData.h"
#include "carla/road/element/Waypoint.h"
namespace carla {
namespace road {
class Map
: EnableSharedFromThis<Map>,
: public EnableSharedFromThis<Map>,
private MovableNonCopyable {
public:

View File

@ -16,7 +16,7 @@ namespace road {
return true;
}
Map MapBuilder::Build() {
SharedPtr<Map> MapBuilder::Build() {
// Move the RoadSegmentDefinitions needed information to a RoadSegments
for (auto &&id_seg : _temp_sections) {
MakeElement<RoadSegment>(id_seg.first, std::move(id_seg.second));
@ -44,7 +44,7 @@ namespace road {
// _map_data is a memeber of MapBuilder so you must especify if
// you want to keep it (will return copy -> Map(const Map &))
// or move it (will return move -> Map(Map &&))
return Map(std::move(_map_data));
return SharedPtr<Map>(new Map{std::move(_map_data)});
}
bool MapBuilder::InterpretRoadFlow() {

View File

@ -19,7 +19,7 @@ namespace road {
bool AddRoadSegmentDefinition(element::RoadSegmentDefinition &seg);
Map Build();
SharedPtr<Map> Build();
private:

View File

@ -6,7 +6,6 @@
#pragma once
#include "carla/road/element/RoadSegment.h"
#include "carla/road/element/RoadInfoVisitor.h"
#include <string>

View File

@ -6,9 +6,6 @@
#pragma once
#include "carla/road/element/RoadSegment.h"
#include "carla/road/element/RoadInfo.h"
#include <iterator>
namespace carla {

View File

@ -6,6 +6,8 @@
#include "carla/road/element/Waypoint.h"
#include "carla/road/Map.h"
namespace carla {
namespace road {
namespace element {
@ -14,6 +16,8 @@ namespace element {
return RoadInfoList(_map->GetData().GetRoad(_road_id)->GetInfos(_dist));
}
Waypoint::~Waypoint() = default;
} // namespace element
} // namespace road
} // namespace carla

View File

@ -6,9 +6,10 @@
#pragma once
#include "carla/road/Map.h"
#include "carla/Memory.h"
#include "carla/geom/Transform.h"
#include "carla/road/element/RoadInfoList.h"
#include "carla/road/element/Types.h"
namespace carla {
namespace road {
@ -22,6 +23,8 @@ namespace element {
Waypoint() = default;
~Waypoint();
const geom::Transform &GetTransform() const {
return _transform;
}

View File

@ -22,9 +22,9 @@ namespace rpc {
std::string open_drive_file;
std::vector<geom::Transform> recommended_start_transforms;
std::vector<geom::Transform> recommended_spawn_points;
MSGPACK_DEFINE_ARRAY(name, open_drive_file, recommended_start_transforms);
MSGPACK_DEFINE_ARRAY(name, open_drive_file, recommended_spawn_points);
};
} // namespace rpc

View File

@ -39,7 +39,8 @@ TEST(road, add_information) {
def.MakeInfo<element::RoadInfoLane>();
builder.AddRoadSegmentDefinition(def);
Map m = builder.Build();
auto map_ptr = builder.Build();
Map &m = *map_ptr;
const RoadInfoVelocity *r = m.GetData().GetRoad(0)->GetInfo<RoadInfoVelocity>(0.0);
ASSERT_EQ(r->velocity, 50.0);
@ -71,7 +72,8 @@ TEST(road, set_and_get_connections_for) {
}
builder.AddRoadSegmentDefinition(def);
}
Map m = builder.Build();
auto map_ptr = builder.Build();
Map &m = *map_ptr;
for (auto &&r : m.GetData().GetAllIds()) {
for (size_t i = 0; i < 10; ++i) {
ASSERT_EQ(m.GetData().GetRoad(r)->GetPredecessorsIds().at(i), i);
@ -104,7 +106,8 @@ TEST(road, geom_line) {
builder.AddRoadSegmentDefinition(def1);
Map m = builder.Build();
auto map_ptr = builder.Build();
Map &m = *map_ptr;
ASSERT_EQ(m.GetData().GetRoad(1)->GetLength(), 20.0);
AssertNear(m.GetData().GetRoad(1)->GetDirectedPointIn(0),
@ -143,7 +146,8 @@ TEST(road, geom_arc) {
builder.AddRoadSegmentDefinition(def1);
Map m = builder.Build();
auto map_ptr = builder.Build();
Map &m = *map_ptr;
ASSERT_EQ(m.GetData().GetRoad(1)->GetLength(), 10.0);
}
@ -163,7 +167,8 @@ TEST(road, geom_spiral) {
builder.AddRoadSegmentDefinition(def1);
Map m = builder.Build();
auto map_ptr = builder.Build();
Map &m = *map_ptr;
ASSERT_EQ(m.GetData().GetRoad(1)->GetLength(), 10.0);
// not implemented yet
@ -189,8 +194,10 @@ TEST(road, get_information) {
def.MakeInfo<element::RoadInfoLane>();
builder.AddRoadSegmentDefinition(def);
Map m = builder.Build();
auto map_ptr = builder.Build();
Map &m = *map_ptr;
const RoadInfoVelocity *r = m.GetData().GetRoad(0)->GetInfo<RoadInfoVelocity>(0.0);
(void)r;
}
}

View File

@ -0,0 +1,57 @@
// 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/PythonUtil.h>
#include <carla/client/Map.h>
#include <carla/client/Waypoint.h>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
namespace carla {
namespace client {
std::ostream &operator<<(std::ostream &out, const Map &map) {
out << "Map(name=" << map.GetName() << ')';
return out;
}
std::ostream &operator<<(std::ostream &out, const Waypoint &waypoint) {
out << "Waypoint(" << waypoint.GetTransform() << ')';
return out;
}
} // namespace client
} // namespace carla
void export_map() {
using namespace boost::python;
namespace cc = carla::client;
namespace cg = carla::geom;
class_<std::vector<cg::Transform>>("vector_of_transforms")
.def(vector_indexing_suite<std::vector<cg::Transform>>())
.def(self_ns::str(self_ns::self))
;
class_<std::vector<carla::SharedPtr<cc::Waypoint>>>("vector_of_waypoints")
.def(vector_indexing_suite<std::vector<carla::SharedPtr<cc::Waypoint>>>())
.def(self_ns::str(self_ns::self))
;
class_<cc::Map, boost::noncopyable, boost::shared_ptr<cc::Map>>("Map", no_init)
.add_property("name", CALL_RETURNING_COPY(cc::Map, GetName))
.def("get_spawn_points", CALL_RETURNING_COPY(cc::Map, GetRecommendedSpawnPoints))
.def("get_waypoint", &cc::Map::GetWaypoint, (arg("location"), arg("project_to_road")=true))
.def("to_opendrive", CALL_RETURNING_COPY(cc::Map, GetOpenDrive))
.def(self_ns::str(self_ns::self))
;
class_<cc::Waypoint, boost::noncopyable, boost::shared_ptr<cc::Waypoint>>("Waypoint", no_init)
.add_property("transform", CALL_RETURNING_COPY(cc::Waypoint, GetTransform))
.def("next", &cc::Waypoint::Next, (args("distance")))
.def(self_ns::str(self_ns::self))
;
}

View File

@ -85,6 +85,7 @@ void export_world() {
.add_property("id", &cc::World::GetId)
.add_property("map_name", CALL_RETURNING_COPY(cc::World, GetMapName))
.def("get_blueprint_library", CONST_CALL_WITHOUT_GIL(cc::World, GetBlueprintLibrary))
.def("get_map", CONST_CALL_WITHOUT_GIL(cc::World, GetMap))
.def("get_spectator", CONST_CALL_WITHOUT_GIL(cc::World, GetSpectator))
.def("get_weather", CONST_CALL_WITHOUT_GIL(cc::World, GetWeather))
.def("set_weather", &cc::World::SetWeather)

View File

@ -105,6 +105,7 @@ static auto MakeCallback(boost::python::object callback) {
#include "Control.cpp"
#include "Exception.cpp"
#include "Geom.cpp"
#include "Map.cpp"
#include "Sensor.cpp"
#include "SensorData.cpp"
#include "Weather.cpp"
@ -122,6 +123,7 @@ BOOST_PYTHON_MODULE(libcarla) {
export_sensor_data();
export_weather();
export_world();
export_map();
export_client();
export_exception();
}

View File

@ -41,7 +41,8 @@ AOpenDriveActor::AOpenDriveActor()
void AOpenDriveActor::BeginPlay()
{
Super::BeginPlay();
carla::road::Map map = carla::opendrive::OpenDrive::Load("C:\\Users\\ajianu\\Desktop\\xodr\\test_03.xodr");
auto MapPtr = carla::opendrive::OpenDrive::Load("C:\\Users\\ajianu\\Desktop\\xodr\\test_03.xodr");
auto &map = MapPtr->GetData();
std::vector<carla::road::element::id_type> roadIDs = map.GetAllIds();
TArray<carla::road::element::id_type> processed;