CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
Unreal
CarlaUnreal
Plugins
Carla
Source
Carla
Util
DebugShapeDrawer.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/Util/DebugShapeDrawer.h
"
8
#include "
Carla.h
"
9
#include "
Carla/Game/CarlaHUD.h
"
10
#include "
Carla/Game/CarlaStatics.h
"
11
#include "
Carla/MapGen/LargeMapManager.h
"
12
#include "DrawDebugHelpers.h"
13
#include "Components/LineBatchComponent.h"
14
15
#include <
util/disable-ue4-macros.h
>
16
#include <
carla/rpc/DebugShape.h
>
17
#include <
carla/rpc/String.h
>
18
#include <
util/enable-ue4-macros.h
>
19
20
21
22
23
struct
FShapeVisitor
24
{
25
using
Shape
=
carla::rpc::DebugShape
;
26
27
FShapeVisitor
(UWorld &InWorld, FColor InColor,
float
InLifeTime,
bool
bInPersistentLines)
28
:
World
(&InWorld),
29
Color
(InColor.ReinterpretAsLinear() *
BrightMultiplier
),
30
LifeTime
(InLifeTime),
31
bPersistentLines
(bInPersistentLines)
32
{
33
World
->GetLineBatcher(UWorld::ELineBatcherType::WorldPersistent)->SetCollisionEnabled(ECollisionEnabled::NoCollision);
34
}
35
36
void
operator()
(
const
Shape::Point
&Point)
const
37
{
38
FVector
Location
= FVector(Point.location);
39
ALargeMapManager
* LargeMap =
UCarlaStatics::GetLargeMapManager
(
World
);
40
if
(LargeMap)
41
{
42
Location
= LargeMap->
GlobalToLocalLocation
(
Location
);
43
}
44
World
->GetLineBatcher(UWorld::ELineBatcherType::WorldPersistent)->DrawPoint(
45
Location
,
46
Color
,
47
1e2f * Point.size,
48
DepthPriority
,
49
LifeTime
);
50
}
51
52
void
operator()
(
const
Shape::Line
&Line)
const
53
{
54
FVector Begin = FVector(Line.
begin
);
55
FVector End = FVector(Line.
end
);
56
ALargeMapManager
* LargeMap =
UCarlaStatics::GetLargeMapManager
(
World
);
57
if
(LargeMap)
58
{
59
Begin = LargeMap->
GlobalToLocalLocation
(Begin);
60
End = LargeMap->
GlobalToLocalLocation
(End);
61
}
62
World
->GetLineBatcher(UWorld::ELineBatcherType::WorldPersistent)->DrawLine(
63
Begin,
64
End,
65
Color
,
66
DepthPriority
,
67
1e2f * Line.
thickness
,
68
LifeTime
);
69
}
70
71
void
operator()
(
const
Shape::Arrow
&Arrow)
const
72
{
73
FVector Begin = FVector(Arrow.
line
.
begin
);
74
FVector End = FVector(Arrow.
line
.
end
);
75
ALargeMapManager
* LargeMap =
UCarlaStatics::GetLargeMapManager
(
World
);
76
if
(LargeMap)
77
{
78
Begin = LargeMap->
GlobalToLocalLocation
(Begin);
79
End = LargeMap->
GlobalToLocalLocation
(End);
80
}
81
const
auto
Diff = End - Begin;
82
const
FRotator LookAt = FRotationMatrix::MakeFromX(Diff).Rotator();
83
const
FTransform
Transform
= {LookAt, Begin};
84
85
// Everything in centimeters
86
const
auto
Dist = Diff.Size();
87
const
auto
ArrowSize = 1e2f * Arrow.
arrow_size
;
88
const
auto
ArrowTipDist = Dist - ArrowSize;
89
const
auto
Thickness = 1e2f * Arrow.
line
.
thickness
;
90
91
FBatchedLine BatchedLines[] =
92
{
93
FBatchedLine(
94
Begin,
95
End,
96
Color
,
97
LifeTime
,
98
Thickness,
99
DepthPriority
),
100
FBatchedLine(
101
Transform
.TransformPosition(FVector(ArrowTipDist, +ArrowSize, +ArrowSize)),
102
End,
103
Color
,
104
LifeTime
,
105
Thickness,
106
DepthPriority
),
107
FBatchedLine(
108
Transform
.TransformPosition(FVector(ArrowTipDist, +ArrowSize, -ArrowSize)),
109
End,
110
Color
,
111
LifeTime
,
112
Thickness,
113
DepthPriority
),
114
FBatchedLine(
115
Transform
.TransformPosition(FVector(ArrowTipDist, -ArrowSize, +ArrowSize)),
116
End,
117
Color
,
118
LifeTime
,
119
Thickness,
120
DepthPriority
),
121
FBatchedLine(
122
Transform
.TransformPosition(FVector(ArrowTipDist, -ArrowSize, -ArrowSize)),
123
End,
124
Color
,
125
LifeTime
,
126
Thickness,
127
DepthPriority
)
128
};
129
130
World
->GetLineBatcher(UWorld::ELineBatcherType::WorldPersistent)->DrawLines(TArrayView<FBatchedLine>(BatchedLines, 5));
131
}
132
133
void
operator()
(
const
Shape::Box
&Box)
const
134
{
135
const
FVector Extent = 1e2f * FVector{Box.box.extent.x, Box.box.extent.y, Box.box.extent.z};
136
FTransform
Transform
= {FRotator(Box.rotation), Box.box.location};
137
const
auto
Thickness = 1e2f * Box.thickness;
138
139
ALargeMapManager
* LargeMap =
UCarlaStatics::GetLargeMapManager
(
World
);
140
if
(LargeMap)
141
{
142
Transform
= LargeMap->
GlobalToLocalTransform
(
Transform
);
143
}
144
145
FVector B[2], P, Q;
146
B[0] = -Extent;
147
B[1] = Extent;
148
149
for
(int32 i = 0; i < 2; ++i)
150
{
151
for
(int32 j = 0; j < 2; ++j)
152
{
153
P.X=B[i].X;
154
Q.X=B[i].X;
155
P.Y=B[j].Y;
156
Q.Y=B[j].Y;
157
P.Z=B[0].Z;
158
Q.Z=B[1].Z;
159
World
->GetLineBatcher(UWorld::ELineBatcherType::WorldPersistent)->DrawLine(
160
Transform
.TransformPosition(P),
161
Transform
.TransformPosition(Q),
162
Color
,
163
DepthPriority
,
164
Thickness,
165
LifeTime
);
166
167
P.Y=B[i].Y;
168
Q.Y=B[i].Y;
169
P.Z=B[j].Z;
170
Q.Z=B[j].Z;
171
P.X=B[0].X;
172
Q.X=B[1].X;
173
World
->GetLineBatcher(UWorld::ELineBatcherType::WorldPersistent)->DrawLine(
174
Transform
.TransformPosition(P),
175
Transform
.TransformPosition(Q),
176
Color
,
177
DepthPriority
,
178
Thickness,
179
LifeTime
);
180
181
P.Z=B[i].Z;
182
Q.Z=B[i].Z;
183
P.X=B[j].X;
184
Q.X=B[j].X;
185
P.Y=B[0].Y;
186
Q.Y=B[1].Y;
187
World
->GetLineBatcher(UWorld::ELineBatcherType::WorldPersistent)->DrawLine(
188
Transform
.TransformPosition(P),
189
Transform
.TransformPosition(Q),
190
Color
,
191
DepthPriority
,
192
Thickness,
193
LifeTime
);
194
}
195
}
196
}
197
198
void
operator()
(
const
Shape::String
&Str)
const
199
{
200
auto
PlayerController = UGameplayStatics::GetPlayerController(
World
, 0);
201
if
(PlayerController ==
nullptr
)
202
{
203
UE_LOG(LogCarla, Error, TEXT(
"Can't find player controller!"
));
204
return
;
205
}
206
FVector
Location
= FVector(Str.
location
);
207
ALargeMapManager
* LargeMap =
UCarlaStatics::GetLargeMapManager
(
World
);
208
if
(LargeMap)
209
{
210
Location
= LargeMap->
GlobalToLocalLocation
(
Location
);
211
}
212
ACarlaHUD
*Hud = Cast<ACarlaHUD>(PlayerController->GetHUD());
213
Hud->
AddHUDString
(carla::rpc::ToFString(Str.
text
),
Location
,
Color
.QuantizeRound(),
LifeTime
);
214
}
215
216
private
:
217
218
UWorld *
World
;
219
220
FLinearColor
Color
;
221
222
float
LifeTime
;
223
224
bool
bPersistentLines
;
225
226
uint8
DepthPriority
= SDPG_World;
227
228
// Debug lines are way more dark in the package, that's why this
229
// multiplier is needed.
230
#if UE_BUILD_SHIPPING
231
static
constexpr
double
BrightMultiplier
= 1000.0;
232
#else
233
// @TODO: Use UKismetSystemLibrary::IsStandalone to support colors
234
// in Editor's standalone mode.
235
static
constexpr
double
BrightMultiplier
= 1.0;
236
#endif
237
238
};
239
240
void
FDebugShapeDrawer::Draw
(
const
carla::rpc::DebugShape
&Shape)
241
{
242
auto
Visitor =
FShapeVisitor
(
World
, Shape.
color
, Shape.
life_time
, Shape.
persistent_lines
);
243
std::visit(Visitor, Shape.
primitive
);
244
}
245
246
void
FDebugShapeDrawer::Clear
()
247
{
248
ULineBatchComponent* LineBatcher =
World
.GetLineBatcher(UWorld::ELineBatcherType::WorldPersistent);
249
if
(LineBatcher !=
nullptr
)
250
{
251
LineBatcher->Flush();
252
}
253
}
CarlaHUD.h
CarlaStatics.h
Carla.h
DebugShapeDrawer.h
DebugShape.h
LargeMapManager.h
ACarlaHUD
Class to draw on HUD.
Definition
CarlaHUD.h:45
ACarlaHUD::AddHUDString
void AddHUDString(const FString Str, const FVector Location, const FColor Color, double LifeTime)
Definition
CarlaHUD.cpp:56
ALargeMapManager
Definition
LargeMapManager.h:62
ALargeMapManager::GlobalToLocalTransform
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
Definition
LargeMapManager.cpp:348
ALargeMapManager::GlobalToLocalLocation
FVector GlobalToLocalLocation(const FVector &InLocation) const
Definition
LargeMapManager.cpp:356
FDebugShapeDrawer::Clear
void Clear()
Definition
DebugShapeDrawer.cpp:246
FDebugShapeDrawer::World
UWorld & World
Definition
DebugShapeDrawer.h:25
FDebugShapeDrawer::Draw
void Draw(const carla::rpc::DebugShape &Shape)
Definition
DebugShapeDrawer.cpp:240
Transform
A rigid transform in CARLA's / Unreal's left-handed frame: x forward, y right, z up,...
Definition
geom/Transform.h:32
UCarlaStatics::GetLargeMapManager
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
Definition
CarlaStatics.h:111
carla::rpc::DebugShape
Definition
DebugShape.h:29
carla::rpc::DebugShape::primitive
std::variant< Point, Line, Arrow, Box, String > primitive
Definition
DebugShape.h:65
carla::rpc::DebugShape::life_time
float life_time
Definition
DebugShape.h:69
carla::rpc::DebugShape::persistent_lines
bool persistent_lines
Definition
DebugShape.h:71
carla::rpc::DebugShape::color
Color color
Definition
DebugShape.h:67
disable-ue4-macros.h
enable-ue4-macros.h
String.h
FShapeVisitor::Shape
carla::rpc::DebugShape Shape
Definition
DebugShapeDrawer.cpp:25
FShapeVisitor::Color
FLinearColor Color
Definition
DebugShapeDrawer.cpp:220
FShapeVisitor::DepthPriority
uint8 DepthPriority
Definition
DebugShapeDrawer.cpp:226
FShapeVisitor::operator()
void operator()(const Shape::Box &Box) const
Definition
DebugShapeDrawer.cpp:133
FShapeVisitor::operator()
void operator()(const Shape::Line &Line) const
Definition
DebugShapeDrawer.cpp:52
FShapeVisitor::bPersistentLines
bool bPersistentLines
Definition
DebugShapeDrawer.cpp:224
FShapeVisitor::operator()
void operator()(const Shape::Arrow &Arrow) const
Definition
DebugShapeDrawer.cpp:71
FShapeVisitor::operator()
void operator()(const Shape::String &Str) const
Definition
DebugShapeDrawer.cpp:198
FShapeVisitor::operator()
void operator()(const Shape::Point &Point) const
Definition
DebugShapeDrawer.cpp:36
FShapeVisitor::World
UWorld * World
Definition
DebugShapeDrawer.cpp:218
FShapeVisitor::LifeTime
float LifeTime
Definition
DebugShapeDrawer.cpp:222
FShapeVisitor::BrightMultiplier
static constexpr double BrightMultiplier
Definition
DebugShapeDrawer.cpp:235
FShapeVisitor::FShapeVisitor
FShapeVisitor(UWorld &InWorld, FColor InColor, float InLifeTime, bool bInPersistentLines)
Definition
DebugShapeDrawer.cpp:27
Location
Definition
geom/Location.h:23
carla::rpc::DebugShape::Arrow
Definition
DebugShape.h:45
carla::rpc::DebugShape::Arrow::line
Line line
Definition
DebugShape.h:46
carla::rpc::DebugShape::Arrow::arrow_size
float arrow_size
Definition
DebugShape.h:47
carla::rpc::DebugShape::Box
Definition
DebugShape.h:51
carla::rpc::DebugShape::Line
Definition
DebugShape.h:38
carla::rpc::DebugShape::Line::end
geom::Location end
Definition
DebugShape.h:40
carla::rpc::DebugShape::Line::thickness
float thickness
Definition
DebugShape.h:41
carla::rpc::DebugShape::Line::begin
geom::Location begin
Definition
DebugShape.h:39
carla::rpc::DebugShape::Point
Definition
DebugShape.h:32
carla::rpc::DebugShape::String
Definition
DebugShape.h:58
carla::rpc::DebugShape::String::text
std::string text
Definition
DebugShape.h:60
carla::rpc::DebugShape::String::location
geom::Location location
Definition
DebugShape.h:59
Generated by
1.18.0