Implementation:CARLA simulator Carla Python Geometry Bindings
| Knowledge Sources | |
|---|---|
| Domains | Python Bindings, Geometry |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
Boost.Python binding file that exposes CARLA's geometry types (Vector2D, Vector3D, Location, Rotation, Transform, BoundingBox, GeoLocation) to the Python API.
Description
This file defines the export_geom() function that registers the core geometry classes. Vector2D and Vector3D provide standard vector operations (length, squared_length, make_unit_vector) and arithmetic operators (+, -, *, /). Vector3D additionally offers cross, dot, distance, distance_2d, distance_squared, distance_squared_2d, and get_vector_angle methods. Location extends Vector3D with a distance method. Rotation holds pitch, yaw, roll and provides directional vectors (get_forward_vector, get_right_vector, get_up_vector). Transform combines Location and Rotation, supports point/vector transformation (transform, transform_vector), and matrix access (get_matrix, get_inverse_matrix). BoundingBox has location, extent, rotation and provides contains and vertex queries. GeoLocation holds latitude, longitude, and altitude. Implicit conversions between Vector3D and Location are registered.
Usage
These geometry types are fundamental to virtually every CARLA operation -- positioning actors, computing distances, defining spawn points, and performing coordinate transformations.
Code Reference
Source Location
- Repository: CARLA
- File: PythonAPI/carla/src/Geometry.cpp
Signature
void export_geom();
// Key classes exposed:
class_<cg::Vector2D>("Vector2D")
class_<cg::Vector3D>("Vector3D")
class_<cg::Location, bases<cg::Vector3D>>("Location")
class_<cg::Rotation>("Rotation")
class_<cg::Transform>("Transform")
class_<cg::BoundingBox>("BoundingBox")
class_<cg::GeoLocation>("GeoLocation")
Import
import carla
loc = carla.Location(x=10.0, y=20.0, z=0.5)
rot = carla.Rotation(pitch=0.0, yaw=90.0, roll=0.0)
transform = carla.Transform(loc, rot)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| x, y, z | float | No | Coordinate components (default: 0.0) |
| pitch, yaw, roll | float | No | Rotation angles in degrees (default: 0.0) |
| location | carla.Location | No | Location component of Transform |
| rotation | carla.Rotation | No | Rotation component of Transform |
| extent | carla.Vector3D | No | Half-size extents for BoundingBox |
Outputs
| Name | Type | Description |
|---|---|---|
| length() | float | Vector magnitude |
| distance() | float | Euclidean distance to another vector/location |
| get_forward_vector() | carla.Vector3D | Unit forward direction from rotation |
| get_matrix() | list[list[float]] | 4x4 transformation matrix |
Usage Examples
import carla
# Create geometry objects
v1 = carla.Vector3D(1.0, 0.0, 0.0)
v2 = carla.Vector3D(0.0, 1.0, 0.0)
cross = v1.cross(v2)
print(f"Cross product: {cross}")
# Transform operations
t = carla.Transform(carla.Location(x=10, y=5, z=0), carla.Rotation(yaw=45))
fwd = t.get_forward_vector()
matrix = t.get_matrix()
# Distance
loc1 = carla.Location(0, 0, 0)
loc2 = carla.Location(3, 4, 0)
print(f"Distance: {loc1.distance(loc2)}")