Implementation:CARLA simulator Carla InMemoryMap
| Knowledge Sources | |
|---|---|
| Domains | Traffic_Management, Autonomous_Driving |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
The InMemoryMap class builds and maintains a discretized local map cache of the CARLA world, providing fast spatial queries for waypoint lookup used by the Traffic Manager pipeline.
Description
InMemoryMap is the spatial backbone of the Traffic Manager. It converts the CARLA cc::Map (OpenDRIVE road network) into a densely sampled graph of SimpleWaypoint objects interconnected by successor and predecessor links. Key features include:
- Dense Topology Construction: The SetUp() method retrieves the sparse road topology from the CARLA map, then interpolates waypoints at a fine resolution (typically 1 meter) along each road segment. Waypoints are stored in dense_topology (a NodeList / std::vector<SimpleWaypointPtr>).
- Segment-based Graph Building: Roads are decomposed into SegmentId tuples of (RoadId, LaneId, SectionId). The class builds a SegmentTopology mapping each segment to its predecessors and successors, and a SegmentMap mapping each segment to its ordered list of waypoints.
- Successor/Predecessor Resolution: The GetSuccessors and GetPredecessors methods recursively resolve segment connectivity, handling cases where intermediate segments may have been pruned during topology construction.
- Spatial R-tree Index: An Rtree (Boost.Geometry R* tree with 16-entry pages) indexes all waypoints by 3D location, enabling efficient nearest-neighbor queries via GetWaypoint(location) and area queries via GetWaypointsInDelta(location, n_points, random_sample).
- Binary Serialization: The Save(path) and Load(content) methods support caching the computed map to a binary file. On subsequent loads, the map can be deserialized from a std::vector<uint8_t> buffer, avoiding expensive recomputation. CachedSimpleWaypoint objects handle the serialization format.
- Static Cook Method: The Cook(world_map, path) static method provides a convenience interface to construct and immediately serialize a map to disk.
Usage
InMemoryMap is created by TrafficManagerLocal::SetupLocalMap(). It first attempts to load a pre-cached binary file from the server; if unavailable, it falls back to SetUp() which constructs the map from scratch. The resulting LocalMapPtr (shared pointer) is passed to LocalizationStage, MotionPlanStage, and ALSM for waypoint lookups.
Code Reference
Source Location
- Repository: CARLA
- File:
LibCarla/source/carla/trafficmanager/InMemoryMap.cpp - Header:
LibCarla/source/carla/trafficmanager/InMemoryMap.h
Signature
class InMemoryMap {
public:
InMemoryMap(WorldMap world_map);
~InMemoryMap();
static void Cook(WorldMap world_map, const std::string& path);
bool Load(const std::vector<uint8_t>& content);
void SetUp();
void Save(const std::string& path);
SimpleWaypointPtr GetWaypoint(const cg::Location loc) const;
NodeList GetWaypointsInDelta(const cg::Location loc,
const uint16_t n_points,
const float random_sample) const;
NodeList GetDenseTopology() const;
std::string GetMapName();
const cc::Map& GetMap() const;
private:
WorldMap _world_map;
NodeList dense_topology;
Rtree rtree;
SegmentId GetSegmentId(const WaypointPtr &wp) const;
SegmentId GetSegmentId(const SimpleWaypointPtr &swp) const;
static NodeList GetSuccessors(const SegmentId segment_id,
const SegmentTopology &segment_topology,
const SegmentMap &segment_map);
static NodeList GetPredecessors(const SegmentId segment_id,
const SegmentTopology &segment_topology,
const SegmentMap &segment_map);
};
Import
#include "carla/trafficmanager/InMemoryMap.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| world_map | WorldMap (carla::SharedPtr<const cc::Map>) | Yes | The CARLA world map providing road topology and waypoint generation |
| content | const std::vector<uint8_t>& | No | Binary content for deserializing a pre-cached map (used by Load) |
| path | const std::string& | No | File path for saving/loading the binary map cache |
| loc | cg::Location | No | Query location for nearest waypoint lookup |
| n_points | uint16_t | No | Number of waypoints to return in delta area queries |
| random_sample | float | No | Distance parameter for delta area waypoint queries |
Outputs
| Name | Type | Description |
|---|---|---|
| SimpleWaypointPtr | std::shared_ptr<SimpleWaypoint> | Nearest waypoint to a queried location |
| NodeList | std::vector<SimpleWaypointPtr> | List of waypoints matching spatial queries or full dense topology |
| bool | bool | Success/failure indicator for Load operations |
Usage Examples
// Creating and setting up the in-memory map:
const carla::SharedPtr<const cc::Map> world_map = world.GetMap();
auto local_map = std::make_shared<InMemoryMap>(world_map);
// Try to load from cache first:
auto content = episode_proxy.Lock()->GetCacheFile("map.bin", true);
if (content.size() != 0) {
local_map->Load(content);
} else {
local_map->SetUp(); // Build from scratch
}
// Query nearest waypoint to a vehicle location:
SimpleWaypointPtr closest = local_map->GetWaypoint(vehicle_location);
// Query waypoints in an area around the hero for teleportation:
NodeList candidates = local_map->GetWaypointsInDelta(hero_location, 10, 200.0f);