CARLA
 
载入中...
搜索中...
未找到
CarlaRecorderQuery.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
8
10
11#include "CarlaRecorder.h"
12
13#include <ctime>
14#include <sstream>
15#include <string>
16
21
23
25{
26 if (File.eof())
27 {
28 return false;
29 }
30
31 ReadValue<char>(File, Header.Id);
32 ReadValue<uint32_t>(File, Header.Size);
33
34 return true;
35}
36
38{
39 File.seekg(Header.Size, std::ios::cur);
40}
41
42inline bool CarlaRecorderQuery::CheckFileInfo(std::stringstream &Info)
43{
44 // read Info
46
47 // check magic string
48 if (RecInfo.Magic != "CARLA_RECORDER")
49 {
50 Info << "File is not a CARLA recorder" << std::endl;
51 return false;
52 }
53
54 // show general Info
55 Info << "Version: " << RecInfo.Version << std::endl;
56 Info << "Map: " << TCHAR_TO_UTF8(*RecInfo.Mapfile) << std::endl;
57 tm *TimeInfo = localtime(&RecInfo.Date);
58 char DateStr[100];
59 strftime(DateStr, sizeof(DateStr), "%x %X", TimeInfo);
60 Info << "Date: " << DateStr << std::endl << std::endl;
61
62 return true;
63}
64
65std::string CarlaRecorderQuery::QueryInfo(std::string Filename, bool bShowAll)
66{
67 std::stringstream Info;
68
69 // get the final path + filename
70 std::string Filename2 = GetRecorderFilename(Filename);
71
72 // try to open
73 File.open(Filename2, std::ios::binary);
74 if (!File.is_open())
75 {
76 Info << "File " << Filename2 << " not found on server\n";
77 return Info.str();
78 }
79
80 uint16_t i, Total;
81 bool bFramePrinted = false;
82
83 // lambda for repeating task
84 auto PrintFrame = [this](std::stringstream &Info)
85 {
86 Info << "Frame " << Frame.Id << " at " << Frame.Elapsed << " seconds\n";
87 };
88
89 if (!CheckFileInfo(Info))
90 return Info.str();
91
92 // parse only frames
93 while (File)
94 {
95 // get header
96 if (!ReadHeader())
97 {
98 break;
99 }
100
101 // check for a frame packet
102 switch (Header.Id)
103 {
104 // frame
105 case static_cast<char>(CarlaRecorderPacketId::FrameStart):
106 Frame.Read(File);
107 if (bShowAll)
108 {
109 PrintFrame(Info);
110 bFramePrinted = true;
111 }
112 else
113 bFramePrinted = false;
114 break;
115
116 // events add
117 case static_cast<char>(CarlaRecorderPacketId::EventAdd):
118 ReadValue<uint16_t>(File, Total);
119 if (Total > 0 && !bFramePrinted)
120 {
121 PrintFrame(Info);
122 bFramePrinted = true;
123 }
124 for (i = 0; i < Total; ++i)
125 {
126 // add
128 Info << " Create " << EventAdd.DatabaseId << ": " << TCHAR_TO_UTF8(*EventAdd.Description.Id) <<
129 " (" <<
130 static_cast<int>(EventAdd.Type) << ") at (" << EventAdd.Location.X << ", " <<
131 EventAdd.Location.Y << ", " << EventAdd.Location.Z << ")" << std::endl;
132 for (auto &Att : EventAdd.Description.Attributes)
133 {
134 Info << " " << TCHAR_TO_UTF8(*Att.Id) << " = " << TCHAR_TO_UTF8(*Att.Value) << std::endl;
135 }
136 }
137 break;
138
139 // events del
140 case static_cast<char>(CarlaRecorderPacketId::EventDel):
141 ReadValue<uint16_t>(File, Total);
142 if (Total > 0 && !bFramePrinted)
143 {
144 PrintFrame(Info);
145 bFramePrinted = true;
146 }
147 for (i = 0; i < Total; ++i)
148 {
150 Info << " Destroy " << EventDel.DatabaseId << "\n";
151 }
152 break;
153
154 // events parenting
155 case static_cast<char>(CarlaRecorderPacketId::EventParent):
156 ReadValue<uint16_t>(File, Total);
157 if (Total > 0 && !bFramePrinted)
158 {
159 PrintFrame(Info);
160 bFramePrinted = true;
161 }
162 for (i = 0; i < Total; ++i)
163 {
165 Info << " Parenting " << EventParent.DatabaseId << " with " << EventParent.DatabaseIdParent <<
166 " (parent)\n";
167 }
168 break;
169
170 // collisions
171 case static_cast<char>(CarlaRecorderPacketId::Collision):
172 ReadValue<uint16_t>(File, Total);
173 if (Total > 0 && !bFramePrinted)
174 {
175 PrintFrame(Info);
176 bFramePrinted = true;
177 }
178 for (i = 0; i < Total; ++i)
179 {
181 Info << " Collision id " << Collision.Id << " between " << Collision.DatabaseId1;
183 Info << " (hero) ";
184 Info << " with " << Collision.DatabaseId2;
186 Info << " (hero) ";
187 Info << std::endl;
188 }
189 break;
190
191 // positions
192 case static_cast<char>(CarlaRecorderPacketId::Position):
193 if (bShowAll)
194 {
195 ReadValue<uint16_t>(File, Total);
196 if (Total > 0 && !bFramePrinted)
197 {
198 PrintFrame(Info);
199 bFramePrinted = true;
200 }
201 Info << " Positions: " << Total << std::endl;
202 for (i = 0; i < Total; ++i)
203 {
205 Info << " Id: " << Position.DatabaseId << " Location: (" << Position.Location.X << ", " << Position.Location.Y << ", " << Position.Location.Z << ") Rotation: (" << Position.Rotation.X << ", " << Position.Rotation.Y << ", " << Position.Rotation.Z << ")" << std::endl;
206 }
207 }
208 else
209 SkipPacket();
210 break;
211
212 // traffic light
213 case static_cast<char>(CarlaRecorderPacketId::State):
214 if (bShowAll)
215 {
216 ReadValue<uint16_t>(File, Total);
217 if (Total > 0 && !bFramePrinted)
218 {
219 PrintFrame(Info);
220 bFramePrinted = true;
221 }
222 Info << " State traffic lights: " << Total << std::endl;
223 for (i = 0; i < Total; ++i)
224 {
226 Info << " Id: " << StateTraffic.DatabaseId << " state: " << static_cast<char>(0x30 + StateTraffic.State) << " frozen: " <<
227 StateTraffic.IsFrozen << " elapsedTime: " << StateTraffic.ElapsedTime << std::endl;
228 }
229 }
230 else
231 SkipPacket();
232 break;
233
234 // vehicle animations
235 case static_cast<char>(CarlaRecorderPacketId::AnimVehicle):
236 if (bShowAll)
237 {
238 ReadValue<uint16_t>(File, Total);
239 if (Total > 0 && !bFramePrinted)
240 {
241 PrintFrame(Info);
242 bFramePrinted = true;
243 }
244 Info << " Vehicle animations: " << Total << std::endl;
245 for (i = 0; i < Total; ++i)
246 {
248 Info << " Id: " << Vehicle.DatabaseId << " Steering: " << Vehicle.Steering << " Throttle: " << Vehicle.Throttle << " Brake: " << Vehicle.Brake << " Handbrake: " << Vehicle.bHandbrake << " Gear: " << Vehicle.Gear << std::endl;
249 }
250 }
251 else
252 SkipPacket();
253 break;
254
255 // walker animations
256 case static_cast<char>(CarlaRecorderPacketId::AnimWalker):
257 if (bShowAll)
258 {
259 ReadValue<uint16_t>(File, Total);
260 if (Total > 0 && !bFramePrinted)
261 {
262 PrintFrame(Info);
263 bFramePrinted = true;
264 }
265 Info << " Walker animations: " << Total << std::endl;
266 for (i = 0; i < Total; ++i)
267 {
269 Info << " Id: " << Walker.DatabaseId << " speed: " << Walker.Speed << std::endl;
270 }
271 }
272 else
273 SkipPacket();
274 break;
275
276 // vehicle door animations
277 case static_cast<char>(CarlaRecorderPacketId::VehicleDoor):
278 if (bShowAll)
279 {
280 ReadValue<uint16_t>(File, Total);
281 if (Total > 0 && !bFramePrinted)
282 {
283 PrintFrame(Info);
284 bFramePrinted = true;
285 }
286 Info << " Vehicle door animations: " << Total << std::endl;
287 for (i = 0; i < Total; ++i)
288 {
290
292 doorVehicle = DoorVehicle.Doors;
293 EVehicleDoor eDoorVehicle = static_cast<EVehicleDoor>(doorVehicle);
294 std::string opened_doors_list;
295
296 Info << " Id: " << DoorVehicle.DatabaseId << std::endl;
297 Info << " Doors opened: ";
298 if (eDoorVehicle == EVehicleDoor::FL)
299 Info << " Front Left " << std::endl;
300 if (eDoorVehicle == EVehicleDoor::FR)
301 Info << " Front Right " << std::endl;
302 if (eDoorVehicle == EVehicleDoor::RL)
303 Info << " Rear Left " << std::endl;
304 if (eDoorVehicle == EVehicleDoor::RR)
305 Info << " Rear Right " << std::endl;
306 if (eDoorVehicle == EVehicleDoor::Hood)
307 Info << " Hood " << std::endl;
308 if (eDoorVehicle == EVehicleDoor::Trunk)
309 Info << " Trunk " << std::endl;
310 if (eDoorVehicle == EVehicleDoor::All)
311 Info << " All " << std::endl;
312 }
313 }
314 else
315 SkipPacket();
316 break;
317
318
319 // vehicle light animations
320 case static_cast<char>(CarlaRecorderPacketId::VehicleLight):
321 if (bShowAll)
322 {
323 ReadValue<uint16_t>(File, Total);
324 if (Total > 0 && !bFramePrinted)
325 {
326 PrintFrame(Info);
327 bFramePrinted = true;
328 }
329 Info << " Vehicle light animations: " << Total << std::endl;
330 for (i = 0; i < Total; ++i)
331 {
333
335 FVehicleLightState State(LightState);
336 std::string enabled_lights_list;
337 if (State.Position)
338 enabled_lights_list += "Position ";
339 if (State.LowBeam)
340 enabled_lights_list += "LowBeam ";
341 if (State.HighBeam)
342 enabled_lights_list += "HighBeam ";
343 if (State.Brake)
344 enabled_lights_list += "Brake ";
345 if (State.RightBlinker)
346 enabled_lights_list += "RightBlinker ";
347 if (State.LeftBlinker)
348 enabled_lights_list += "LeftBlinker ";
349 if (State.Reverse)
350 enabled_lights_list += "Reverse ";
351 if (State.Interior)
352 enabled_lights_list += "Interior ";
353 if (State.Fog)
354 enabled_lights_list += "Fog ";
355 if (State.Special1)
356 enabled_lights_list += "Special1 ";
357 if (State.Special2)
358 enabled_lights_list += "Special2 ";
359
360 if (enabled_lights_list.size())
361 {
362 Info << " Id: " << LightVehicle.DatabaseId << " " <<
363 enabled_lights_list.substr(0, enabled_lights_list.size() - 1) << std::endl;
364 }
365 else
366 {
367 Info << " Id: " << LightVehicle.DatabaseId << " None" << std::endl;
368 }
369 }
370 }
371 else
372 SkipPacket();
373 break;
374
375 // scene light animations
376 case static_cast<char>(CarlaRecorderPacketId::SceneLight):
377 if (bShowAll)
378 {
379 ReadValue<uint16_t>(File, Total);
380 if (Total > 0 && !bFramePrinted)
381 {
382 PrintFrame(Info);
383 bFramePrinted = true;
384 }
385 Info << " Scene light changes: " << Total << std::endl;
386 for (i = 0; i < Total; ++i)
387 {
389 Info << " Id: " << LightScene.LightId << " enabled: " << (LightScene.bOn ? "True" : "False")
390 << " intensity: " << LightScene.Intensity
391 << " RGB_color: (" << LightScene.Color.R << ", " << LightScene.Color.G << ", " << LightScene.Color.B << ")"
392 << std::endl;
393 }
394 }
395 else
396 SkipPacket();
397 break;
398
399 // dynamic actor kinematics
400 case static_cast<char>(CarlaRecorderPacketId::Kinematics):
401 if (bShowAll)
402 {
403 ReadValue<uint16_t>(File, Total);
404 if (Total > 0 && !bFramePrinted)
405 {
406 PrintFrame(Info);
407 bFramePrinted = true;
408 }
409 Info << " Dynamic actors: " << Total << std::endl;
410 for (i = 0; i < Total; ++i)
411 {
413 Info << " Id: " << Kinematics.DatabaseId << " linear_velocity: ("
414 << Kinematics.LinearVelocity.X << ", " << Kinematics.LinearVelocity.Y << ", " << Kinematics.LinearVelocity.Z << ")"
415 << " angular_velocity: ("
417 << std::endl;
418 }
419 }
420 else
421 SkipPacket();
422 break;
423
424 // actors bounding boxes
425 case static_cast<char>(CarlaRecorderPacketId::BoundingBox):
426 if (bShowAll)
427 {
428 ReadValue<uint16_t>(File, Total);
429 if (Total > 0 && !bFramePrinted)
430 {
431 PrintFrame(Info);
432 bFramePrinted = true;
433 }
434 Info << " Actor bounding boxes: " << Total << std::endl;
435 for (i = 0; i < Total; ++i)
436 {
438 Info << " Id: " << ActorBoundingBox.DatabaseId << " origin: ("
442 << " extension: ("
446 << std::endl;
447 }
448 }
449 else
450 SkipPacket();
451 break;
452
453 // actors trigger volumes
454 case static_cast<char>(CarlaRecorderPacketId::TriggerVolume):
455 if (bShowAll)
456 {
457 ReadValue<uint16_t>(File, Total);
458 if (Total > 0 && !bFramePrinted)
459 {
460 PrintFrame(Info);
461 bFramePrinted = true;
462 }
463 Info << " Actor trigger volumes: " << Total << std::endl;
464 for (i = 0; i < Total; ++i)
465 {
467 Info << " Id: " << ActorBoundingBox.DatabaseId << " origin: ("
471 << " extension: ("
475 << std::endl;
476 }
477 }
478 else
479 SkipPacket();
480 break;
481
482 // Platform time
483 case static_cast<char>(CarlaRecorderPacketId::PlatformTime):
484 if (bShowAll)
485 {
486 if (!bFramePrinted)
487 {
488 PrintFrame(Info);
489 bFramePrinted = true;
490 }
491
493 Info << " Current platform time: " << PlatformTime.Time << std::endl;
494 }
495 else
496 SkipPacket();
497 break;
498
499 case static_cast<char>(CarlaRecorderPacketId::PhysicsControl):
500 if (bShowAll)
501 {
502 ReadValue<uint16_t>(File, Total);
503 if (Total > 0 && !bFramePrinted)
504 {
505 PrintFrame(Info);
506 bFramePrinted = true;
507 }
508
509 Info << " Physics Control events: " << Total << std::endl;
510 for (i = 0; i < Total; ++i)
511 {
514 Info << " Id: " << PhysicsControl.DatabaseId << std::endl
515 << " max_rpm = " << Control.max_rpm << std::endl
516 << " MOI = " << Control.moi << std::endl
517 << " damping_rate_full_throttle = " << Control.damping_rate_full_throttle << std::endl
518 << " damping_rate_zero_throttle_clutch_engaged = " << Control.damping_rate_zero_throttle_clutch_engaged << std::endl
519 << " damping_rate_zero_throttle_clutch_disengaged = " << Control.damping_rate_zero_throttle_clutch_disengaged << std::endl
520 << " use_gear_auto_box = " << (Control.use_gear_autobox ? "true" : "false") << std::endl
521 << " gear_switch_time = " << Control.gear_switch_time << std::endl
522 << " clutch_strength = " << Control.clutch_strength << std::endl
523 << " final_ratio = " << Control.final_ratio << std::endl
524 << " mass = " << Control.mass << std::endl
525 << " drag_coefficient = " << Control.drag_coefficient << std::endl
526 << " center_of_mass = " << "(" << Control.center_of_mass.x << ", " << Control.center_of_mass.y << ", " << Control.center_of_mass.z << ")" << std::endl;
527 Info << " torque_curve =";
528 for (auto& vec : Control.torque_curve)
529 {
530 Info << " (" << vec.x << ", " << vec.y << ")";
531 }
532 Info << std::endl;
533 Info << " steering_curve =";
534 for (auto& vec : Control.steering_curve)
535 {
536 Info << " (" << vec.x << ", " << vec.y << ")";
537 }
538 Info << std::endl;
539 Info << " forward_gears:" << std::endl;
540 uint32_t count = 0;
541 for (auto& Gear : Control.forward_gears)
542 {
543 Info << " gear " << count << ": ratio " << Gear.ratio
544 << " down_ratio " << Gear.down_ratio
545 << " up_ratio " << Gear.up_ratio << std::endl;
546 ++count;
547 }
548 Info << " wheels:" << std::endl;
549 count = 0;
550 for (auto& Wheel : Control.wheels)
551 {
552 Info << " wheel " << count << ": tire_friction " << Wheel.tire_friction
553 << " damping_rate " << Wheel.damping_rate
554 << " max_steer_angle " << Wheel.max_steer_angle
555 << " radius " << Wheel.radius
556 << " max_brake_torque " << Wheel.max_brake_torque
557 << " max_handbrake_torque " << Wheel.max_handbrake_torque
558 << " position " << "(" << Wheel.position.x << ", " << Wheel.position.y << ", " << Wheel.position.z << ")"
559 << std::endl;
560 ++count;
561 }
562 }
563 }
564 else
565 SkipPacket();
566 break;
567
568 case static_cast<char>(CarlaRecorderPacketId::TrafficLightTime):
569 if (bShowAll)
570 {
571 ReadValue<uint16_t>(File, Total);
572 if (Total > 0 && !bFramePrinted)
573 {
574 PrintFrame(Info);
575 bFramePrinted = true;
576 }
577
578 Info << " Traffic Light time events: " << Total << std::endl;
579 for (i = 0; i < Total; ++i)
580 {
582 Info << " Id: " << TrafficLightTime.DatabaseId
583 << " green_time: " << TrafficLightTime.GreenTime
584 << " yellow_time: " << TrafficLightTime.YellowTime
585 << " red_time: " << TrafficLightTime.RedTime
586 << std::endl;
587 }
588 }
589 else
590 SkipPacket();
591 break;
592
593 case static_cast<char>(CarlaRecorderPacketId::WalkerBones):
594 if (bShowAll)
595 {
596 ReadValue<uint16_t>(File, Total);
597 if (Total > 0 && !bFramePrinted)
598 {
599 PrintFrame(Info);
600 bFramePrinted = true;
601 }
602
603 Info << " Walkers Bones: " << Total << std::endl;
604 for (i = 0; i < Total; ++i)
605 {
608 Info << " Id: " << WalkerBones.DatabaseId << "\n";
609 for (const auto &Bone : WalkerBones.Bones)
610 {
611 Info << " Bone: \"" << TCHAR_TO_UTF8(*Bone.Name) << "\" relative: " << "Loc("
612 << Bone.Location.X << ", " << Bone.Location.Y << ", " << Bone.Location.Z << ") Rot("
613 << Bone.Rotation.X << ", " << Bone.Rotation.Y << ", " << Bone.Rotation.Z << ")\n";
614 }
615 }
616 Info << std::endl;
617 }
618 else
619 SkipPacket();
620 break;
621
622 // frame end
623 case static_cast<char>(CarlaRecorderPacketId::FrameEnd):
624 // do nothing, it is empty
625 break;
626
627 default:
628 SkipPacket();
629 break;
630 }
631 }
632
633 Info << "\nFrames: " << Frame.Id << "\n";
634 Info << "Duration: " << Frame.Elapsed << " seconds\n";
635
636 File.close();
637
638 return Info.str();
639}
640
641std::string CarlaRecorderQuery::QueryCollisions(std::string Filename, char Category1, char Category2)
642{
643 std::stringstream Info;
644
645 // get the final path + filename
646 std::string Filename2 = GetRecorderFilename(Filename);
647
648 // try to open
649 File.open(Filename2, std::ios::binary);
650 if (!File.is_open())
651 {
652 Info << "File " << Filename2 << " not found on server\n";
653 return Info.str();
654 }
655
656 if (!CheckFileInfo(Info))
657 return Info.str();
658
659 // other, vehicle, walkers, trafficLight, hero, any
660 char Categories[] = { 'o', 'v', 'w', 't', 'h', 'a' };
661 uint16_t i, Total;
662 struct ReplayerActorInfo
663 {
664 uint8_t Type;
665 FString Id;
666 };
667 std::unordered_map<uint32_t, ReplayerActorInfo> Actors;
668 struct PairHash
669 {
670 std::size_t operator()(const std::pair<uint32_t, uint32_t>& P) const
671 {
672 std::size_t hash = P.first;
673 hash <<= 32;
674 hash += P.second;
675 return hash;
676 }
677 };
678 std::unordered_set<std::pair<uint32_t, uint32_t>, PairHash > oldCollisions, newCollisions;
679
680 // header
681 Info << std::setw(8) << "Time";
682 Info << " " << std::setw(6) << "Types";
683 Info << " " << std::setw(6) << std::right << "Id";
684 Info << " " << std::setw(35) << std::left << "Actor 1";
685 Info << " " << std::setw(6) << std::right << "Id";
686 Info << " " << std::setw(35) << std::left << "Actor 2";
687 Info << std::endl;
688
689 // parse only frames
690 while (File)
691 {
692
693 // get header
694 if (!ReadHeader())
695 {
696 break;
697 }
698
699 // check for a frame packet
700 switch (Header.Id)
701 {
702 // frame
703 case static_cast<char>(CarlaRecorderPacketId::FrameStart):
704 Frame.Read(File);
705 // exchange sets of collisions (to know when a collision is new or continue from previous frame)
706 oldCollisions = std::move(newCollisions);
707 newCollisions.clear();
708 break;
709
710 // events add
711 case static_cast<char>(CarlaRecorderPacketId::EventAdd):
712 ReadValue<uint16_t>(File, Total);
713 for (i = 0; i < Total; ++i)
714 {
715 // add
717 Actors[EventAdd.DatabaseId] = ReplayerActorInfo { EventAdd.Type, EventAdd.Description.Id };
718 }
719 break;
720
721 // events del
722 case static_cast<char>(CarlaRecorderPacketId::EventDel):
723 ReadValue<uint16_t>(File, Total);
724 for (i = 0; i < Total; ++i)
725 {
727 Actors.erase(EventAdd.DatabaseId);
728 }
729 break;
730
731 // events parenting
732 case static_cast<char>(CarlaRecorderPacketId::EventParent):
733 SkipPacket();
734 break;
735
736 // collisions
737 case static_cast<char>(CarlaRecorderPacketId::Collision):
738 ReadValue<uint16_t>(File, Total);
739 for (i = 0; i < Total; ++i)
740 {
742
743 int Valid = 0;
744
745 // get categories for both actors
746 uint8_t Type1, Type2;
747 if (Collision.DatabaseId1 != uint32_t(-1))
748 Type1 = Categories[Actors[Collision.DatabaseId1].Type];
749 else
750 Type1 = 'o'; // other non-actor object
751
752 if (Collision.DatabaseId2 != uint32_t(-1))
753 Type2 = Categories[Actors[Collision.DatabaseId2].Type];
754 else
755 Type2 = 'o'; // other non-actor object
756
757 // filter actor 1
758 if (Category1 == 'a')
759 ++Valid;
760 else if (Category1 == Type1)
761 ++Valid;
762 else if (Category1 == 'h' && Collision.IsActor1Hero)
763 ++Valid;
764
765 // filter actor 2
766 if (Category2 == 'a')
767 ++Valid;
768 else if (Category2 == Type2)
769 ++Valid;
770 else if (Category2 == 'h' && Collision.IsActor2Hero)
771 ++Valid;
772
773 // only show if both actors has passed the filter
774 if (Valid == 2)
775 {
776 // check if we need to show as a starting collision or it is a continuation one
777 auto collisionPair = std::make_pair(Collision.DatabaseId1, Collision.DatabaseId2);
778 if (oldCollisions.count(collisionPair) == 0)
779 {
780 Info << std::setw(8) << std::setprecision(0) << std::right << std::fixed << Frame.Elapsed;
781 Info << " " << " " << Type1 << " " << Type2 << " ";
782 Info << " " << std::setw(6) << std::right << Collision.DatabaseId1;
783 Info << " " << std::setw(35) << std::left << TCHAR_TO_UTF8(*Actors[Collision.DatabaseId1].Id);
784 Info << " " << std::setw(6) << std::right << Collision.DatabaseId2;
785 Info << " " << std::setw(35) << std::left << TCHAR_TO_UTF8(*Actors[Collision.DatabaseId2].Id);
786 Info << std::endl;
787 }
788 // save current collision
789 newCollisions.insert(collisionPair);
790 }
791 }
792 break;
793
794 case static_cast<char>(CarlaRecorderPacketId::Position):
795 SkipPacket();
796 break;
797
798 case static_cast<char>(CarlaRecorderPacketId::State):
799 SkipPacket();
800 break;
801
802 // frame end
803 case static_cast<char>(CarlaRecorderPacketId::FrameEnd):
804 // do nothing, it is empty
805 break;
806
807 default:
808 SkipPacket();
809 break;
810 }
811 }
812
813 Info << "\nFrames: " << Frame.Id << "\n";
814 Info << "Duration: " << Frame.Elapsed << " seconds\n";
815
816 File.close();
817
818 return Info.str();
819}
820
821std::string CarlaRecorderQuery::QueryBlocked(std::string Filename, double MinTime, double MinDistance)
822{
823 std::stringstream Info;
824
825 // get the final path + filename
826 std::string Filename2 = GetRecorderFilename(Filename);
827
828 // try to open
829 File.open(Filename2, std::ios::binary);
830 if (!File.is_open())
831 {
832 Info << "File " << Filename2 << " not found on server\n";
833 return Info.str();
834 }
835
836 if (!CheckFileInfo(Info))
837 return Info.str();
838
839 // other, vehicle, walkers, trafficLight, hero, any
840 uint16_t i, Total;
841 struct ReplayerActorInfo
842 {
843 uint8_t Type;
844 FString Id;
845 FVector LastPosition;
846 double Time;
847 double Duration;
848 };
849 std::unordered_map<uint32_t, ReplayerActorInfo> Actors;
850 // to be able to sort the results by the duration of each actor (decreasing order)
851 std::multimap<double, std::string, std::greater<double>> Results;
852
853 // header
854 Info << std::setw(8) << "Time";
855 Info << " " << std::setw(6) << "Id";
856 Info << " " << std::setw(35) << std::left << "Actor";
857 Info << " " << std::setw(10) << std::right << "Duration";
858 Info << std::endl;
859
860 // parse only frames
861 while (File)
862 {
863
864 // get header
865 if (!ReadHeader())
866 {
867 break;
868 }
869
870 // check for a frame packet
871 switch (Header.Id)
872 {
873 // frame
874 case static_cast<char>(CarlaRecorderPacketId::FrameStart):
875 Frame.Read(File);
876 break;
877
878 // events add
879 case static_cast<char>(CarlaRecorderPacketId::EventAdd):
880 ReadValue<uint16_t>(File, Total);
881 for (i = 0; i < Total; ++i)
882 {
883 // add
885 Actors[EventAdd.DatabaseId] = ReplayerActorInfo { EventAdd.Type, EventAdd.Description.Id, FVector(0, 0, 0), 0.0, 0.0 };
886 }
887 break;
888
889 // events del
890 case static_cast<char>(CarlaRecorderPacketId::EventDel):
891 ReadValue<uint16_t>(File, Total);
892 for (i = 0; i < Total; ++i)
893 {
895 Actors.erase(EventAdd.DatabaseId);
896 }
897 break;
898
899 // events parenting
900 case static_cast<char>(CarlaRecorderPacketId::EventParent):
901 SkipPacket();
902 break;
903
904 // collisions
905 case static_cast<char>(CarlaRecorderPacketId::Collision):
906 SkipPacket();
907 break;
908
909 // positions
910 case static_cast<char>(CarlaRecorderPacketId::Position):
911 // read all positions
912 ReadValue<uint16_t>(File, Total);
913 for (i=0; i<Total; ++i)
914 {
916 // check if actor moved less than a distance
917 if (FVector::Distance(Actors[Position.DatabaseId].LastPosition, Position.Location) < MinDistance)
918 {
919 // actor stopped
920 if (Actors[Position.DatabaseId].Duration == 0)
921 Actors[Position.DatabaseId].Time = Frame.Elapsed;
922 Actors[Position.DatabaseId].Duration += Frame.DurationThis;
923 }
924 else
925 {
926 // check to show info
927 if (Actors[Position.DatabaseId].Duration >= MinTime)
928 {
929 std::stringstream Result;
930 Result << std::setw(8) << std::setprecision(0) << std::fixed << Actors[Position.DatabaseId].Time;
931 Result << " " << std::setw(6) << Position.DatabaseId;
932 Result << " " << std::setw(35) << std::left << TCHAR_TO_UTF8(*Actors[Position.DatabaseId].Id);
933 Result << " " << std::setw(10) << std::setprecision(0) << std::fixed << std::right << Actors[Position.DatabaseId].Duration;
934 Result << std::endl;
935 Results.insert(std::make_pair(Actors[Position.DatabaseId].Duration, Result.str()));
936 }
937 // actor moving
938 Actors[Position.DatabaseId].Duration = 0;
939 Actors[Position.DatabaseId].LastPosition = Position.Location;
940 }
941 }
942 break;
943
944 // traffic light
945 case static_cast<char>(CarlaRecorderPacketId::State):
946 SkipPacket();
947 break;
948
949 // frame end
950 case static_cast<char>(CarlaRecorderPacketId::FrameEnd):
951 // do nothing, it is empty
952 break;
953
954 default:
955 SkipPacket();
956 break;
957 }
958 }
959
960 // show actors stopped that were not moving again
961 for (auto &Actor : Actors)
962 {
963 // check to show info
964 if (Actor.second.Duration >= MinTime)
965 {
966 std::stringstream Result;
967 Result << std::setw(8) << std::setprecision(0) << std::fixed << Actor.second.Time;
968 Result << " " << std::setw(6) << Actor.first;
969 Result << " " << std::setw(35) << std::left << TCHAR_TO_UTF8(*Actor.second.Id);
970 Result << " " << std::setw(10) << std::setprecision(0) << std::fixed << std::right << Actor.second.Duration;
971 Result << std::endl;
972 Results.insert(std::make_pair(Actor.second.Duration, Result.str()));
973 }
974 }
975
976 // show the result
977 for (auto &Result : Results)
978 {
979 Info << Result.second;
980 }
981
982 Info << "\nFrames: " << Frame.Id << "\n";
983 Info << "Duration: " << Frame.Elapsed << " seconds\n";
984
985 File.close();
986
987 return Info.str();
988}
std::string GetRecorderFilename(std::string Filename)
EVehicleDoor
Type of door to open/close
CarlaRecorderCollision Collision
CarlaRecorderAnimWalker Walker
CarlaRecorderEventParent EventParent
CarlaRecorderActorBoundingBox ActorBoundingBox
CarlaRecorderFrame Frame
CarlaRecorderEventDel EventDel
CarlaRecorderDoorVehicle DoorVehicle
CarlaRecorderLightVehicle LightVehicle
CarlaRecorderStateTrafficLight StateTraffic
CarlaRecorderPhysicsControl PhysicsControl
bool CheckFileInfo(std::stringstream &Info)
CarlaRecorderLightScene LightScene
std::string QueryBlocked(std::string Filename, double MinTime=30, double MinDistance=10)
std::string QueryCollisions(std::string Filename, char Category1='a', char Category2='a')
CarlaRecorderWalkerBones WalkerBones
CarlaRecorderKinematics Kinematics
CarlaRecorderAnimVehicle Vehicle
CarlaRecorderEventAdd EventAdd
CarlaRecorderPlatformTime PlatformTime
CarlaRecorderTrafficLightTime TrafficLightTime
CarlaRecorderPosition Position
CarlaRecorderInfo RecInfo
std::string QueryInfo(std::string Filename, bool bShowAll=false)
Defines the physical appearance of a vehicle whitch is obtained by the sensors.
std::vector< CarlaRecorderActorAttribute > Attributes
void Read(std::istream &InFile)
void Read(std::istream &InFile)
void Read(std::istream &InFile)
void Read(std::istream &InFile)
void Read(std::istream &InFile)
CarlaRecorderActorDescription Description
void Read(std::istream &InFile)
void Read(std::istream &InFile)
void Read(std::istream &InFile)
void Read(std::istream &File)
void Read(std::istream &InFile)
void Read(std::istream &InFile)
void Read(std::istream &InFile)
FVehiclePhysicsControl VehiclePhysicsControl
void Read(std::istream &InFile)
void Read(std::istream &InFile)
void Read(std::istream &InFile)
void Read(std::ifstream &InFile)
std::vector< CarlaRecorderWalkerBone > Bones