CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
client
LibCarla/source/carla/client/LaneInvasionSensor.cpp
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
#include "
carla/client/LaneInvasionSensor.h
"
8
9
#include "
carla/Logging.h
"
10
#include "
carla/client/Map.h
"
11
#include "
carla/client/Vehicle.h
"
12
#include "
carla/client/detail/Simulator.h
"
13
#include "
carla/geom/Location.h
"
14
#include "
carla/geom/Math.h
"
15
#include "
carla/sensor/data/LaneInvasionEvent.h
"
16
17
#include <exception>
18
19
namespace
carla
{
20
namespace
client
{
21
22
// ===========================================================================
23
// -- Static local methods ---------------------------------------------------
24
// ===========================================================================
25
26
static
geom::Location
Rotate
(
float
yaw,
const
geom::Location
&location) {
27
yaw *=
geom::Math::Pi<float>
() / 180.0f;
28
const
float
c = std::cos(yaw);
29
const
float
s = std::sin(yaw);
30
return
{
31
c * location.
x
- s * location.
y
,
32
s * location.
x
+ c * location.
y
,
33
location.
z
};
34
}
35
36
// ===========================================================================
37
// -- LaneInvasionCallback ---------------------------------------------------
38
// ===========================================================================
39
40
class
LaneInvasionCallback
{
41
public
:
42
43
LaneInvasionCallback
(
44
const
Vehicle
&vehicle,
45
SharedPtr<Map>
&&map,
46
Sensor::CallbackFunctionType
&&user_callback)
47
:
_parent
(vehicle.GetId()),
48
_parent_bounding_box
(vehicle.GetBoundingBox()),
49
_map
(
std
::move(map)),
50
_callback
(
std
::move(user_callback)) {
51
DEBUG_ASSERT
(
_map
!=
nullptr
);
52
}
53
54
void
Tick
(
const
WorldSnapshot
&snapshot)
const
;
55
56
private
:
57
58
struct
Bounds
{
59
size_t
frame
;
60
std::array<geom::Location, 4u>
corners
;
61
};
62
63
std::shared_ptr<const Bounds>
MakeBounds
(
64
size_t
frame,
65
const
geom::Transform
&vehicle_transform)
const
;
66
67
ActorId
_parent
;
68
69
geom::BoundingBox
_parent_bounding_box
;
70
71
SharedPtr<const Map>
_map
;
72
73
Sensor::CallbackFunctionType
_callback
;
74
75
mutable
AtomicSharedPtr<const Bounds>
_bounds
;
76
};
77
78
void
LaneInvasionCallback::Tick
(
const
WorldSnapshot
&snapshot)
const
{
79
// Make sure the parent is alive.
80
auto
parent = snapshot.
Find
(
_parent
);
81
if
(!parent) {
82
return
;
83
}
84
85
auto
next =
MakeBounds
(snapshot.
GetFrame
(), parent->transform);
86
auto
prev =
_bounds
.load();
87
88
// First frame it'll be null.
89
if
((prev ==
nullptr
) &&
_bounds
.compare_exchange(&prev, next)) {
90
return
;
91
}
92
93
// Make sure the distance is long enough.
94
constexpr
float
distance_threshold = 10.0f * std::numeric_limits<float>::epsilon();
95
for
(
auto
i = 0u; i < 4u; ++i) {
96
if
((next->corners[i] - prev->corners[i]).Length() < distance_threshold) {
97
return
;
98
}
99
}
100
101
// Make sure the current frame is up-to-date.
102
do
{
103
if
(prev->frame >= next->frame) {
104
return
;
105
}
106
}
while
(!
_bounds
.compare_exchange(&prev, next));
107
108
// Finally it's safe to compute the crossed lanes.
109
std::vector<road::element::LaneMarking> crossed_lanes;
110
for
(
auto
i = 0u; i < 4u; ++i) {
111
const
auto
lanes =
_map
->CalculateCrossedLanes(prev->corners[i], next->corners[i]);
112
crossed_lanes.insert(crossed_lanes.end(), lanes.begin(), lanes.end());
113
}
114
115
if
(!crossed_lanes.empty()) {
116
_callback
(MakeShared<sensor::data::LaneInvasionEvent>(
117
snapshot.
GetTimestamp
().
frame
,
118
snapshot.
GetTimestamp
().
elapsed_seconds
,
119
parent->transform,
120
_parent
,
121
std::move(crossed_lanes)));
122
}
123
}
124
125
std::shared_ptr<const LaneInvasionCallback::Bounds>
LaneInvasionCallback::MakeBounds
(
126
const
size_t
frame,
127
const
geom::Transform
&transform)
const
{
128
const
auto
&box =
_parent_bounding_box
;
129
const
auto
location = transform.
location
+ box.location;
130
const
auto
yaw = transform.
rotation
.
yaw
;
131
return
std::make_shared<Bounds>(
Bounds
{frame, {
132
location +
Rotate
(yaw,
geom::Location
( box.extent.x, box.extent.y, 0.0f)),
133
location +
Rotate
(yaw,
geom::Location
(-box.extent.x, box.extent.y, 0.0f)),
134
location +
Rotate
(yaw,
geom::Location
( box.extent.x, -box.extent.y, 0.0f)),
135
location +
Rotate
(yaw,
geom::Location
(-box.extent.x, -box.extent.y, 0.0f))}});
136
}
137
138
// ===========================================================================
139
// -- LaneInvasionSensor -----------------------------------------------------
140
// ===========================================================================
141
142
LaneInvasionSensor::~LaneInvasionSensor
() {
143
Stop
();
144
}
145
146
void
LaneInvasionSensor::Listen
(
CallbackFunctionType
callback) {
147
auto
vehicle = std::dynamic_pointer_cast<Vehicle>(
GetParent
());
148
if
(vehicle ==
nullptr
) {
149
log_error(
GetDisplayId
(),
": not attached to a vehicle"
);
150
return
;
151
}
152
153
auto
episode =
GetEpisode
().
Lock
();
154
155
auto
cb = std::make_shared<LaneInvasionCallback>(
156
*vehicle,
157
episode->GetCurrentMap(),
158
std::move(callback));
159
160
const
size_t
callback_id = episode->RegisterOnTickEvent([cb=std::move(cb)](
const
auto
&snapshot) {
161
try
{
162
cb->Tick(snapshot);
163
}
catch
(
const
std::exception &e) {
164
log_error(
"LaneInvasionSensor:"
, e.what());
165
}
166
});
167
168
const
size_t
previous =
_callback_id
.exchange(callback_id);
169
if
(previous != 0u) {
170
episode->RemoveOnTickEvent(previous);
171
}
172
}
173
174
void
LaneInvasionSensor::Stop
() {
175
const
size_t
previous =
_callback_id
.exchange(0u);
176
auto
episode =
GetEpisode
().
TryLock
();
177
if
((previous != 0u) && (episode !=
nullptr
)) {
178
episode->RemoveOnTickEvent(previous);
179
}
180
}
181
182
}
// namespace client
183
}
// namespace carla
DEBUG_ASSERT
#define DEBUG_ASSERT(predicate)
Definition
Debug.h:66
LaneInvasionEvent.h
LaneInvasionSensor.h
Logging.h
Math.h
Simulator.h
Vehicle.h
carla::AtomicSharedPtr
A very simple atomic shared ptr with release-acquire memory order.
Definition
AtomicSharedPtr.h:15
carla::client::LaneInvasionCallback::_parent
ActorId _parent
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:67
carla::client::LaneInvasionCallback::_parent_bounding_box
geom::BoundingBox _parent_bounding_box
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:69
carla::client::LaneInvasionCallback::_map
SharedPtr< const Map > _map
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:71
carla::client::LaneInvasionCallback::Tick
void Tick(const WorldSnapshot &snapshot) const
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:78
carla::client::LaneInvasionCallback::_callback
Sensor::CallbackFunctionType _callback
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:73
carla::client::LaneInvasionCallback::LaneInvasionCallback
LaneInvasionCallback(const Vehicle &vehicle, SharedPtr< Map > &&map, Sensor::CallbackFunctionType &&user_callback)
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:43
carla::client::LaneInvasionCallback::MakeBounds
std::shared_ptr< const Bounds > MakeBounds(size_t frame, const geom::Transform &vehicle_transform) const
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:125
carla::client::LaneInvasionCallback::_bounds
AtomicSharedPtr< const Bounds > _bounds
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:75
carla::client::LaneInvasionSensor::_callback_id
std::atomic_size_t _callback_id
Definition
LibCarla/source/carla/client/LaneInvasionSensor.h:46
carla::client::LaneInvasionSensor::~LaneInvasionSensor
~LaneInvasionSensor()
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:142
carla::client::LaneInvasionSensor::Stop
void Stop() override
Stop listening for new measurements.
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:174
carla::client::LaneInvasionSensor::Listen
void Listen(CallbackFunctionType callback) override
Register a callback to be executed each time a new measurement is received.
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:146
carla::client::Sensor::CallbackFunctionType
std::function< void(SharedPtr< sensor::SensorData >)> CallbackFunctionType
Definition
LibCarla/source/carla/client/Sensor.h:20
carla::client::Timestamp::frame
std::size_t frame
Number of frames elapsed since the simulator was launched.
Definition
Timestamp.h:30
carla::client::Timestamp::elapsed_seconds
double elapsed_seconds
Simulated seconds elapsed since the beginning of the current episode.
Definition
Timestamp.h:33
carla::client::Vehicle
Definition
Vehicle.h:33
carla::client::WorldSnapshot
Definition
WorldSnapshot.h:18
carla::client::WorldSnapshot::GetTimestamp
const Timestamp & GetTimestamp() const
Get timestamp of this snapshot.
Definition
WorldSnapshot.h:34
carla::client::WorldSnapshot::GetFrame
size_t GetFrame() const
Definition
WorldSnapshot.h:29
carla::client::WorldSnapshot::Find
std::optional< ActorSnapshot > Find(ActorId actor_id) const
Find an ActorSnapshot by id.
Definition
WorldSnapshot.h:44
carla::client::detail::ActorState::GetEpisode
EpisodeProxy & GetEpisode()
Definition
client/detail/ActorState.h:85
carla::client::detail::ActorState::GetParent
SharedPtr< Actor > GetParent() const
Definition
ActorState.cpp:31
carla::client::detail::ActorState::GetDisplayId
const std::string & GetDisplayId() const
Definition
client/detail/ActorState.h:35
carla::client::detail::EpisodeProxyImpl::TryLock
SharedPtrType TryLock() const noexcept
Definition
EpisodeProxy.cpp:32
carla::client::detail::EpisodeProxyImpl::Lock
SharedPtrType Lock() const
Same as TryLock but never return nullptr.
Definition
EpisodeProxy.cpp:39
carla::geom::BoundingBox
Definition
LibCarla/source/carla/geom/BoundingBox.h:26
carla::geom::Math::Pi
static constexpr T Pi()
Definition
Math.h:25
carla::geom::Rotation::yaw
float yaw
Definition
Rotation.h:54
carla::geom::Transform
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
Definition
geom/Transform.h:32
carla::geom::Transform::location
Location location
Definition
geom/Transform.h:39
carla::geom::Transform::rotation
Rotation rotation
Definition
geom/Transform.h:41
Map.h
Location.h
carla::client
Definition
BasicAgent.h:24
carla::client::Rotate
static geom::Location Rotate(float yaw, const geom::Location &location)
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:26
carla::traffic_manager::ActorId
carla::ActorId ActorId
Definition
AtomicActorSet.h:21
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::SharedPtr
std::shared_ptr< T > SharedPtr
Definition
Memory.h:15
std
Definition
CarlaRecorderCollision.h:31
carla::client::LaneInvasionCallback::Bounds
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:58
carla::client::LaneInvasionCallback::Bounds::frame
size_t frame
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:59
carla::client::LaneInvasionCallback::Bounds::corners
std::array< geom::Location, 4u > corners
Definition
LibCarla/source/carla/client/LaneInvasionSensor.cpp:60
carla::geom::Location
Definition
geom/Location.h:23
carla::geom::Vector3D::y
float y
Definition
geom/Vector3D.h:27
carla::geom::Vector3D::x
float x
Definition
geom/Vector3D.h:25
carla::geom::Vector3D::z
float z
Definition
geom/Vector3D.h:29
Generated by
1.18.0