CARLA
 
载入中...
搜索中...
未找到
DebugShapeDrawer.cpp
浏览该文件的文档.
1// Copyright (c) 2017 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.h"
12
13#include "DrawDebugHelpers.h"
14#include "Components/LineBatchComponent.h"
15
18#include <carla/rpc/String.h>
20
21
22
24{
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->PersistentLineBatcher->SetCollisionEnabled(ECollisionEnabled::NoCollision);
34 }
35
36 ACarlaHUD * GetHUD() const {
37 auto PlayerController = UGameplayStatics::GetPlayerController(World, 0);
38 if (PlayerController == nullptr)
39 {
40 UE_LOG(LogCarla, Error, TEXT("Can't find player controller!"));
41 return nullptr;
42 }
43 ACarlaHUD *Hud = Cast<ACarlaHUD>(PlayerController->GetHUD());
44 return Hud;
45 }
46
47 void operator()(const Shape::Point &Point) const
48 {
49 FVector Location = FVector(Point.location);
51 if (LargeMap)
52 {
53 Location = LargeMap->GlobalToLocalLocation(Location);
54 }
55 World->PersistentLineBatcher->DrawPoint(
56 Location,
57 Color,
58 1e2f * Point.size,
60 LifeTime);
61 }
62
63 void operator()(const Shape::HUDPoint &Point) const
64 {
65 auto Hud = GetHUD();
66 if (!Hud) return; // Don't draw if HUD is not available.
67
68 FVector Location = FVector(Point.location);
70 if (LargeMap)
71 {
72 Location = LargeMap->GlobalToLocalLocation(Location);
73 }
74 Hud->AddHUDLine(Location, Location, Point.size, Color.Quantize(), LifeTime);
75 }
76
77 void operator()(const Shape::Line &Line) const
78 {
79 FVector Begin = FVector(Line.begin);
80 FVector End = FVector(Line.end);
82 if (LargeMap)
83 {
84 Begin = LargeMap->GlobalToLocalLocation(Begin);
85 End = LargeMap->GlobalToLocalLocation(End);
86 }
87 World->PersistentLineBatcher->DrawLine(
88 Begin,
89 End,
90 Color,
92 1e2f * Line.thickness,
93 LifeTime);
94 }
95
96 void operator()(const Shape::HUDLine &Line) const {
97 auto Hud = GetHUD();
98 if (!Hud) return; // Don't draw if HUD is not available.
99 FVector Begin = FVector(Line.begin);
100 FVector End = FVector(Line.end);
102 if (LargeMap)
103 {
104 Begin = LargeMap->GlobalToLocalLocation(Begin);
105 End = LargeMap->GlobalToLocalLocation(End);
106 }
107 Hud->AddHUDLine(Begin, End, Line.thickness, Color.Quantize(), LifeTime);
108 }
109
110 void operator()(const Shape::Arrow &Arrow) const
111 {
112 FVector Begin = FVector(Arrow.line.begin);
113 FVector End = FVector(Arrow.line.end);
115 if (LargeMap)
116 {
117 Begin = LargeMap->GlobalToLocalLocation(Begin);
118 End = LargeMap->GlobalToLocalLocation(End);
119 }
120 const auto Diff = End - Begin;
121 const FRotator LookAt = FRotationMatrix::MakeFromX(Diff).Rotator();
122 const FTransform Transform = {LookAt, Begin};
123
124 // Everything in centimeters
125 const auto Dist = Diff.Size();
126 const auto ArrowSize = 1e2f * Arrow.arrow_size;
127 const auto ArrowTipDist = Dist - ArrowSize;
128 const auto Thickness = 1e2f * Arrow.line.thickness;
129
130 World->PersistentLineBatcher->DrawLines(TArray<FBatchedLine>({
131 FBatchedLine(
132 Begin, End, Color, LifeTime, Thickness, DepthPriority),
133 FBatchedLine(
134 Transform.TransformPosition(FVector(ArrowTipDist, +ArrowSize, +ArrowSize)),
135 End, Color, LifeTime, Thickness, DepthPriority),
136 FBatchedLine(
137 Transform.TransformPosition(FVector(ArrowTipDist, +ArrowSize, -ArrowSize)),
138 End, Color, LifeTime, Thickness, DepthPriority),
139 FBatchedLine(
140 Transform.TransformPosition(FVector(ArrowTipDist, -ArrowSize, +ArrowSize)),
141 End, Color, LifeTime, Thickness, DepthPriority),
142 FBatchedLine(
143 Transform.TransformPosition(FVector(ArrowTipDist, -ArrowSize, -ArrowSize)),
144 End, Color, LifeTime, Thickness, DepthPriority)}));
145 }
146
147 void operator()(const Shape::HUDArrow &Arrow) const {
148 auto Hud = GetHUD();
149 if (!Hud) return; // Don't draw if HUD is not available.
150 FVector Begin = FVector(Arrow.line.begin);
151 FVector End = FVector(Arrow.line.end);
153 if (LargeMap)
154 {
155 Begin = LargeMap->GlobalToLocalLocation(Begin);
156 End = LargeMap->GlobalToLocalLocation(End);
157 }
158 const auto Diff = End - Begin;
159 const FRotator LookAt = FRotationMatrix::MakeFromX(Diff).Rotator();
160 const FTransform Transform = {LookAt, Begin};
161
162 // Everything in centimeters
163 const auto Dist = Diff.Size();
164 const auto ArrowSize = Arrow.arrow_size;
165 const auto ArrowTipDist = Dist - ArrowSize;
166 const auto Thickness = Arrow.line.thickness;
167
168 Hud->AddHUDLine(Begin, End, Thickness, Color.Quantize(), LifeTime);
169 Hud->AddHUDLine(
170 Transform.TransformPosition(FVector(ArrowTipDist, +ArrowSize, +ArrowSize)),
171 End, Thickness, Color.Quantize(), LifeTime);
172 Hud->AddHUDLine(
173 Transform.TransformPosition(FVector(ArrowTipDist, +ArrowSize, -ArrowSize)),
174 End, Thickness, Color.Quantize(), LifeTime);
175 Hud->AddHUDLine(
176 Transform.TransformPosition(FVector(ArrowTipDist, -ArrowSize, +ArrowSize)),
177 End, Thickness, Color.Quantize(), LifeTime);
178 Hud->AddHUDLine(
179 Transform.TransformPosition(FVector(ArrowTipDist, -ArrowSize, -ArrowSize)),
180 End, Thickness, Color.Quantize(), LifeTime);
181 }
182
183 void operator()(const Shape::Box &Box) const
184 {
185 const FVector Extent = 1e2f * FVector{Box.box.extent.x, Box.box.extent.y, Box.box.extent.z};
186 FTransform Transform = {FRotator(Box.rotation), Box.box.location};
187 const auto Thickness = 1e2f * Box.thickness;
188
190 if (LargeMap)
191 {
192 Transform = LargeMap->GlobalToLocalTransform(Transform);
193 }
194
195 FVector B[2], P, Q;
196 B[0] = -Extent;
197 B[1] = Extent;
198
199 for(int32 i = 0; i < 2; ++i)
200 {
201 for(int32 j = 0; j < 2; ++j)
202 {
203 P.X=B[i].X; Q.X=B[i].X; P.Y=B[j].Y;
204 Q.Y=B[j].Y; P.Z=B[0].Z; Q.Z=B[1].Z;
205 World->PersistentLineBatcher->DrawLine(
206 Transform.TransformPosition(P), Transform.TransformPosition(Q),
207 Color, DepthPriority, Thickness, LifeTime);
208
209 P.Y=B[i].Y; Q.Y=B[i].Y; P.Z=B[j].Z;
210 Q.Z=B[j].Z; P.X=B[0].X; Q.X=B[1].X;
211 World->PersistentLineBatcher->DrawLine(
212 Transform.TransformPosition(P), Transform.TransformPosition(Q),
213 Color, DepthPriority, Thickness, LifeTime);
214
215 P.Z=B[i].Z; Q.Z=B[i].Z; P.X=B[j].X;
216 Q.X=B[j].X; P.Y=B[0].Y; Q.Y=B[1].Y;
217 World->PersistentLineBatcher->DrawLine(
218 Transform.TransformPosition(P), Transform.TransformPosition(Q),
219 Color, DepthPriority, Thickness, LifeTime);
220 }
221 }
222 }
223
224 void operator()(const Shape::HUDBox &Box) const {
225 auto Hud = GetHUD();
226 if (!Hud) return; // Don't draw if HUD is not available.
227 const FVector Extent = 1e2f * FVector{Box.box.extent.x, Box.box.extent.y, Box.box.extent.z};
228 FTransform Transform = {FRotator(Box.rotation), Box.box.location};
229 const auto Thickness = Box.thickness;
230
232 if (LargeMap)
233 {
234 Transform = LargeMap->GlobalToLocalTransform(Transform);
235 }
236
237 FVector B[2], P, Q;
238 B[0] = -Extent;
239 B[1] = Extent;
240
241 for(int32 i = 0; i < 2; ++i)
242 {
243 for(int32 j = 0; j < 2; ++j)
244 {
245 P.X=B[i].X; Q.X=B[i].X; P.Y=B[j].Y;
246 Q.Y=B[j].Y; P.Z=B[0].Z; Q.Z=B[1].Z;
247 Hud->AddHUDLine(
248 Transform.TransformPosition(P), Transform.TransformPosition(Q),
249 Thickness, Color.Quantize(), LifeTime);
250
251 P.Y=B[i].Y; Q.Y=B[i].Y; P.Z=B[j].Z;
252 Q.Z=B[j].Z; P.X=B[0].X; Q.X=B[1].X;
253 Hud->AddHUDLine(
254 Transform.TransformPosition(P), Transform.TransformPosition(Q),
255 Thickness, Color.Quantize(), LifeTime);
256
257 P.Z=B[i].Z; Q.Z=B[i].Z; P.X=B[j].X;
258 Q.X=B[j].X; P.Y=B[0].Y; Q.Y=B[1].Y;
259 Hud->AddHUDLine(
260 Transform.TransformPosition(P), Transform.TransformPosition(Q),
261 Thickness, Color.Quantize(), LifeTime);
262 }
263 }
264 }
265
266 void operator()(const Shape::String &Str) const
267 {
268 auto Hud = GetHUD();
269 if (!Hud) return;
270 FVector Location = FVector(Str.location);
272 if (LargeMap)
273 {
274 Location = LargeMap->GlobalToLocalLocation(Location);
275 }
276 Hud->AddHUDString(carla::rpc::ToFString(Str.text), Location, Color.Quantize(), LifeTime);
277 }
278
279private:
280
281 UWorld *World;
282
283 FLinearColor Color;
284
285 float LifeTime;
286
288
289 uint8 DepthPriority = SDPG_World;
290
291 // Debug lines are way more dark in the package, that's why this
292 // multiplier is needed.
293#if UE_BUILD_SHIPPING
294 static constexpr double BrightMultiplier = 1000.0;
295#else
296 // @TODO: Use UKismetSystemLibrary::IsStandalone to support colors
297 // in Editor's standalone mode.
298 static constexpr double BrightMultiplier = 1.0;
299#endif
300
301};
302
304{
305 auto Visitor = FShapeVisitor(World, Shape.color, Shape.life_time, Shape.persistent_lines);
306 boost::variant2::visit(Visitor, Shape.primitive);
307}
Class to draw on HUD
Definition CarlaHUD.h:50
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
FVector GlobalToLocalLocation(const FVector &InLocation) const
void Draw(const carla::rpc::DebugShape &Shape)
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
boost::variant2::variant< Point, Line, Arrow, Box, String, HUDPoint, HUDLine, HUDArrow, HUDBox > primitive
Definition DebugShape.h:91
void operator()(const Shape::HUDArrow &Arrow) const
void operator()(const Shape::HUDPoint &Point) const
void operator()(const Shape::Box &Box) const
void operator()(const Shape::Line &Line) const
void operator()(const Shape::HUDLine &Line) const
void operator()(const Shape::Arrow &Arrow) const
void operator()(const Shape::String &Str) const
void operator()(const Shape::Point &Point) const
static constexpr double BrightMultiplier
FShapeVisitor(UWorld &InWorld, FColor InColor, float InLifeTime, bool bInPersistentLines)
ACarlaHUD * GetHUD() const
void operator()(const Shape::HUDBox &Box) const