CARLA
Loading...
Searching...
No Matches
TrafficManagerGeometry.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include <cstdint>
10#include <deque>
11#include <memory>
12#include <utility>
13#include <vector>
14
15#include "carla/geom/Location.h"
16
18
19namespace carla {
20namespace traffic_manager {
21
22namespace cg = carla::geom;
23using SimpleWaypointPtr = std::shared_ptr<SimpleWaypoint>;
24using Buffer = std::deque<SimpleWaypointPtr>;
25
26// Pure geometry helpers shared between the LocalizationStage (junction radius
27// gating) and the MotionPlanStage (target waypoint selection). Free functions
28// so the unit-test suite can exercise them directly without a class instance.
29
30/// Returns the radius of the circle defined by three points in the XY plane.
31/// Returns std::numeric_limits<float>::max() when the three points are
32/// collinear (no circumscribing circle exists).
33[[nodiscard]] float GetThreePointCircleRadius(
34 cg::Location first_location,
35 cg::Location middle_location,
36 cg::Location last_location);
37
38/// Locations-only variant of GetTargetData. Takes a flat list of waypoint
39/// locations and walks them to find the bracketing pair around
40/// `target_distance` from `vehicle_location`, then linearly interpolates.
41/// Exposed as a free function so the unit-test suite can exercise it
42/// without constructing SimpleWaypoint instances (which require a real
43/// carla::client::Waypoint and therefore a road map).
44///
45/// Defensive: an empty list returns {vehicle_location, 0u}; a single-element
46/// list returns that element.
47[[nodiscard]] std::pair<cg::Location, uint64_t> InterpolateBufferAt(
48 const std::vector<cg::Location> &waypoint_locations,
49 float target_distance,
50 cg::Location vehicle_location);
51
52/// Returns the location reached by walking `target_distance` along the
53/// waypoint buffer from `vehicle_location`, plus the index of the bracketing
54/// waypoint. Thin adapter that extracts each waypoint's location and
55/// delegates to InterpolateBufferAt.
56[[nodiscard]] std::pair<cg::Location, uint64_t> GetTargetData(
57 const Buffer &waypoint_buffer,
58 float target_distance,
59 cg::Location vehicle_location);
60
61/// Lowest sqrt(R * lateral_acceleration + 2 * braking_deceleration * distance)
62/// over every three consecutive samples taken at `sample_spacing` along `path`.
63/// `path_start_offset` is where `path.front()` sits relative to the vehicle,
64/// negative when behind it. Fewer than three points returns `max_speed`.
65[[nodiscard]] float GetPathSpeedLimit(
66 const std::vector<cg::Location> &path,
67 float path_start_offset,
68 float sample_spacing,
69 float lateral_acceleration,
70 float braking_deceleration,
71 float max_speed);
72
73/// Lateral offset profile for the large-vehicle wide turn, as a function of
74/// `t`, the fraction of the junction still ahead of the target waypoint
75/// (1 at the entry, 0 at the exit). Returns the *unsigned* offset in the
76/// right-turn convention: positive values are inboard (toward the inside of
77/// the turn, the exit "cut-in"), negative values are outboard (the entry
78/// swing that opens the turn up). The caller flips the sign for left turns.
79///
80/// The raw profile is a cosine blend that is 0 at both entry and exit. The
81/// inboard (positive) lobe is then scaled by `inboard_scale` in [0, 1] so the
82/// cut-in that drives a long vehicle's rear into the inside shoulder can be
83/// attenuated (0) or kept (1) independently of the outboard swing.
84[[nodiscard]] float LargeVehicleJunctionOffsetProfile(
85 float t,
86 float max_offset,
87 float max_offset_point,
88 float inboard_scale);
89
90/// Wide-turn offset magnitude scaled by vehicle length:
91/// clamp(gain * (vehicle_length - reference_length), 0, offset_cap).
92/// Short vehicles (at or below the reference length) get no offset; longer
93/// vehicles ramp up to the cap.
94[[nodiscard]] float LargeVehicleOffsetMagnitude(
95 float vehicle_length,
96 float reference_length,
97 float gain,
98 float offset_cap);
99
100/// Returns true when a neighbouring vehicle sits in the lateral band that the
101/// wide-turn offset would swing the ego vehicle into. `offset_direction` is a
102/// unit vector pointing to the swing side; `ego_forward` is the ego heading
103/// unit vector. A neighbour counts as blocking when it is on the offset side
104/// within `offset_magnitude + lateral_clearance` (plus its own radius) and
105/// roughly alongside the ego within `longitudinal_window` (plus its radius).
106/// Each neighbour is a {location, radius} pair.
107[[nodiscard]] bool IsOffsetSideOccupied(
108 cg::Location ego_location,
109 cg::Vector3D ego_forward,
110 cg::Vector3D offset_direction,
111 float offset_magnitude,
112 float lateral_clearance,
113 float longitudinal_window,
114 const std::vector<std::pair<cg::Location, float>> &neighbours);
115
116} // namespace traffic_manager
117} // namespace carla
float LargeVehicleJunctionOffsetProfile(float t, float max_offset, float max_offset_point, float inboard_scale)
Lateral offset profile for the large-vehicle wide turn, as a function of t, the fraction of the junct...
float GetPathSpeedLimit(const std::vector< cg::Location > &path, const float path_start_offset, const float sample_spacing, const float lateral_acceleration, const float braking_deceleration, const float max_speed)
Lowest sqrt(R * lateral_acceleration + 2 * braking_deceleration * distance) over every three consecut...
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
std::pair< cg::Location, uint64_t > InterpolateBufferAt(const std::vector< cg::Location > &waypoint_locations, float target_distance, cg::Location vehicle_location)
Locations-only variant of GetTargetData.
std::pair< cg::Location, uint64_t > GetTargetData(const Buffer &waypoint_buffer, float target_distance, cg::Location vehicle_location)
Returns the location reached by walking target_distance along the waypoint buffer from vehicle_locati...
float GetThreePointCircleRadius(cg::Location first_location, cg::Location middle_location, cg::Location last_location)
Returns the radius of the circle defined by three points in the XY plane.
bool IsOffsetSideOccupied(cg::Location ego_location, cg::Vector3D ego_forward, cg::Vector3D offset_direction, float offset_magnitude, float lateral_clearance, float longitudinal_window, const std::vector< std::pair< cg::Location, float > > &neighbours)
Returns true when a neighbouring vehicle sits in the lateral band that the wide-turn offset would swi...
float LargeVehicleOffsetMagnitude(float vehicle_length, float reference_length, float gain, float offset_cap)
Wide-turn offset magnitude scaled by vehicle length: clamp(gain * (vehicle_length - reference_length)...
std::shared_ptr< SimpleWaypoint > SimpleWaypointPtr
This file contains definitions of common data structures used in traffic manager.