RapyutaSimulationPlugins
RRGeneralUtils.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 
16 
17 // UE
18 
19 #include "Engine/World.h"
20 
21 #include "EngineUtils.h"
22 
23 #include "Json.h"
24 
25 #include "Kismet/KismetSystemLibrary.h"
26 
27 #include "PhysicsEngine/PhysicsConstraintComponent.h"
28 
29 #include "TimerManager.h"
30 
31 
32 
33 // #include "Core/RRUObjectUtils.h"
34 
35 
36 
37 #include "RRGeneralUtils.generated.h"
38 
39 
40 
41 // NOTE: Using TCHAR* = TEXT("") -> could cause linking error in some case!
42 
43 #define EMPTY_STR (TEXT(""))
44 
45 
46 
60 class RAPYUTASIMULATIONPLUGINS_API URRGeneralUtils : public UBlueprintFunctionLibrary
61 
62 {
63 
64 public:
65 
86  template<typename T>
87 
88  static T* FindActorByName(UWorld* InWorld, const FString& InName, const ESearchCase::Type InCaseType = ESearchCase::IgnoreCase)
89 
90  {
91 
92  for (TActorIterator<T> actorItr(InWorld); actorItr; ++actorItr)
93 
94  {
95 
96  if (actorItr->GetName().Equals(InName, InCaseType))
97 
98  {
99 
100  return *actorItr;
101 
102  }
103 
104  }
105 
106 #if WITH_EDITOR
107 
108  // check Display Name if actor not found by ID Name
109 
110  for (TActorIterator<T> actorItr(InWorld); actorItr; ++actorItr)
111 
112  {
113 
114  if (UKismetSystemLibrary::GetDisplayName(*actorItr).Equals(InName, InCaseType))
115 
116  {
117 
118  return *actorItr;
119 
120  }
121 
122  }
123 
124 #endif
125 
126  UE_LOG(LogTemp, Log, TEXT("Actor named [%s] is unavailable."), *InName);
127 
128  return nullptr;
129 
130  }
131 
132 
133 
155  static AActor* FindActorByName(const UObject* WorldContextObject,
156 
157  const FString& InName,
158 
159  const ESearchCase::Type InCaseType = ESearchCase::IgnoreCase)
160 
161  {
162 
163  UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
164 
165  return FindActorByName<AActor>(World, InName, InCaseType);
166 
167  }
168 
169 
170 
189  template<typename T>
190 
191  static T* FindActorBySubname(UWorld* InWorld,
192 
193  const FString& InSubname,
194 
195  const ESearchCase::Type InCaseType = ESearchCase::IgnoreCase)
196 
197  {
198 
199  for (TActorIterator<T> actorItr(InWorld); actorItr; ++actorItr)
200 
201  {
202 
203  if (actorItr->GetName().Contains(InSubname, InCaseType))
204 
205  {
206 
207  return *actorItr;
208 
209  }
210 
211  }
212 
213 #if WITH_EDITOR
214 
215  // check Display Name if actor not found by ID Name
216 
217  for (TActorIterator<T> actorItr(InWorld); actorItr; ++actorItr)
218 
219  {
220 
221  if (UKismetSystemLibrary::GetDisplayName(*actorItr).Contains(InSubname, InCaseType))
222 
223  {
224 
225  return *actorItr;
226 
227  }
228 
229  }
230 
231 #endif
232 
233  UE_LOG(LogTemp, Log, TEXT("Actor name containing [%s] is unavailable."), *InSubname);
234 
235  return nullptr;
236 
237  }
238 
239 
240 
262  static AActor* FindActorBySubname(const UObject* WorldContextObject,
263 
264  const FString& InSubname,
265 
266  const ESearchCase::Type InCaseType = ESearchCase::IgnoreCase)
267 
268  {
269 
270  UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
271 
272  return FindActorBySubname<AActor>(World, InSubname, InCaseType);
273 
274  }
275 
276 
277 
278  template<typename T>
279 
280  static TArray<T*> FindActorListBySubname(UWorld* InWorld,
281 
282  const FString& InSubname,
283 
284  const ESearchCase::Type InCaseType = ESearchCase::IgnoreCase)
285 
286  {
287 
288  TArray<T*> actors;
289 
290  for (TActorIterator<T> actorItr(InWorld); actorItr; ++actorItr)
291 
292  {
293 
294  if (actorItr->GetName().Contains(InSubname, InCaseType))
295 
296  {
297 
298  actors.Add(*actorItr);
299 
300  }
301 
302 #if WITH_EDITOR
303 
304  // check Display Name if actor not found by ID Name
305 
306  else if (UKismetSystemLibrary::GetDisplayName(*actorItr).Contains(InSubname, InCaseType))
307 
308  {
309 
310  actors.Add(*actorItr);
311 
312  }
313 
314 #endif
315 
316  }
317 
318 
319 
320  return actors;
321 
322  }
323 
324 
325 
326 
331  static TArray<AActor*> FindActorListBySubname(const UObject* WorldContextObject,
332 
333  const FString& InSubname,
334 
335  const ESearchCase::Type InCaseType = ESearchCase::IgnoreCase)
336 
337  {
338 
339  UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull);
340 
341  return FindActorListBySubname<AActor>(World, InSubname, InCaseType);
342 
343  }
344 
345 
346 
365  static bool GetRefTransform(const AActor* RefActor,
366 
367  FTransform& OutTransf,
368 
369  const bool ReturnIdentityWithNullptr = true,
370 
371  const bool Verbose = false)
372 
373  {
374 
375  bool res = false;
376 
377  if (RefActor == nullptr)
378 
379  {
380 
381  if (ReturnIdentityWithNullptr)
382 
383  {
384 
385  OutTransf = FTransform::Identity;
386 
387  res = true;
388 
389  }
390 
391  else
392 
393  {
394 
395  if (Verbose)
396 
397  {
398 
399  UE_LOG(LogTemp, Error, TEXT("RefActor is not valid."));
400 
401  }
402 
403  res = false;
404 
405  }
406 
407  }
408 
409  else
410 
411  {
412 
413  OutTransf = RefActor->GetTransform();
414 
415  res = true;
416 
417  }
418 
419  return res;
420 
421  }
422 
423 
424 
452  static bool GetRefTransform(const FString& RefActorName,
453 
454  const AActor* RefActor,
455 
456  const UObject* WorldContextObject,
457 
458  FTransform& OutTransf,
459 
460  const bool Verbose = false)
461 
462  {
463 
464  bool res = GetRefTransformByActor(RefActor, OutTransf, Verbose);
465 
466  if (!res)
467 
468  {
469 
470  res = GetRefTransformByName(RefActorName, WorldContextObject, OutTransf, Verbose);
471 
472  }
473 
474  return res;
475 
476  }
477 
478 
479 
503  static bool GetRefTransformByActor(const AActor* RefActor, FTransform& OutTransf, const bool Verbose = false)
504 
505  {
506 
507  if (RefActor == nullptr)
508 
509  {
510 
511  OutTransf = FTransform::Identity;
512 
513  if (Verbose)
514 
515  {
516 
517  UE_LOG(LogTemp, Error, TEXT("RefActor is not valid."));
518 
519  }
520 
521  return false;
522 
523  }
524 
525  OutTransf = RefActor->GetTransform();
526 
527  return true;
528 
529  }
530 
531 
532 
558  static bool GetRefTransformByName(const FString& RefActorName,
559 
560  const UObject* WorldContextObject,
561 
562  FTransform& OutTransf,
563 
564  const bool Verbose = false)
565 
566  {
567 
568  if (RefActorName.IsEmpty()) // refrence is world origin
569 
570  {
571 
572  OutTransf = FTransform::Identity;
573 
574  return true;
575 
576  }
577 
578 
579 
580  if (WorldContextObject == nullptr)
581 
582  {
583 
584  OutTransf = FTransform::Identity;
585 
586  if (Verbose)
587 
588  {
589 
590  UE_LOG(LogTemp, Error, TEXT("World is not given. Return Idnetity Transform"));
591 
592  }
593 
594  return false;
595 
596  }
597 
598 
599 
600  AActor* refActor = URRGeneralUtils::FindActorByName(WorldContextObject, RefActorName);
601 
602  if (refActor == nullptr)
603 
604  {
605 
606  OutTransf = FTransform::Identity;
607 
608  if (Verbose)
609 
610  {
611 
612  UE_LOG(LogTemp, Warning, TEXT("Reference Actor %s is not valid."), *RefActorName);
613 
614  }
615 
616  return false;
617 
618  }
619 
620 
621 
622  OutTransf = refActor->GetTransform();
623 
624  return true;
625 
626  }
627 
628 
629 
651  static FTransform GetRelativeTransform(const FTransform& RefTransf,
652 
653  const FTransform& WorldTransf,
654 
655  const bool IgnoreScale = false)
656 
657  {
658 
659  FTransform worldTransf = WorldTransf;
660 
661  FTransform refTransfNormalized = RefTransf;
662 
663  refTransfNormalized.NormalizeRotation();
664 
665  if (IgnoreScale)
666 
667  {
668 
669  worldTransf.SetScale3D(FVector::OneVector);
670 
671  refTransfNormalized.SetScale3D(FVector::OneVector);
672 
673  }
674 
675 
676 
677  FTransform relativeTransf = worldTransf.GetRelativeTransform(refTransfNormalized);
678 
679  relativeTransf.NormalizeRotation();
680 
681 
682 
683  return relativeTransf;
684 
685  }
686 
687 
688 
707  static FTransform GetRelativeTransform(const AActor* RefActor,
708 
709  const FTransform& WorldTransf,
710 
711  const bool IgnoreScale = false,
712 
713  const bool Verbose = false)
714 
715  {
716 
717  FTransform outTransf;
718 
719  GetRefTransformByActor(RefActor, outTransf, Verbose);
720 
721  return GetRelativeTransform(outTransf, WorldTransf, IgnoreScale);
722 
723  }
724 
725 
726 
747  static FTransform GetRelativeTransform(const FString& RefActorName,
748 
749  const UObject* WorldContextObject,
750 
751  const FTransform& WorldTransf,
752 
753  const bool IgnoreScale = false,
754 
755  const bool Verbose = false)
756 
757  {
758 
759  FTransform outTransf;
760 
761  GetRefTransformByName(RefActorName, WorldContextObject, outTransf, Verbose);
762 
763  return GetRelativeTransform(outTransf, WorldTransf, IgnoreScale);
764 
765  }
766 
767 
768 
792  static FTransform GetRelativeTransformFromActor(const AActor* RefActor,
793 
794  const FTransform& WorldTransf,
795 
796  const bool IgnoreScale = false,
797 
798  const bool Verbose = false)
799 
800  {
801 
802  return GetRelativeTransform(RefActor, WorldTransf, IgnoreScale, Verbose);
803 
804  }
805 
806 
807 
833  static FTransform GetRelativeTransformFromName(const FString& RefActorName,
834 
835  const UObject* WorldContextObject,
836 
837  const FTransform& WorldTransf,
838 
839  const bool IgnoreScale = false,
840 
841  const bool Verbose = false)
842 
843  {
844 
845  return GetRelativeTransform(RefActorName, WorldContextObject, WorldTransf, IgnoreScale, Verbose);
846 
847  }
848 
849 
850 
873  static bool GetRelativeTransform(const AActor* RefActor,
874 
875  const FTransform& InTransf,
876 
877  FTransform& OutTransf,
878 
879  const bool IgnoreScale = false,
880 
881  const bool ReturnIdentityWithNullptr = true,
882 
883  const bool Verbose = false)
884 
885  {
886 
887  FTransform refTransf;
888 
889  bool result = GetRefTransform(RefActor, refTransf, ReturnIdentityWithNullptr, Verbose);
890 
891  if (result)
892 
893  {
894 
895  OutTransf = GetRelativeTransform(refTransf, InTransf, IgnoreScale);
896 
897  }
898 
899  return result;
900 
901  }
902 
903 
904 
925  static bool GetRelativeTransform(const FString& RefActorName,
926 
927  const AActor* RefActor,
928 
929  const FTransform& InTransf,
930 
931  const UObject* WorldContextObject,
932 
933  FTransform& OutTransf,
934 
935  const bool IgnoreScale = false,
936 
937  const bool Verbose = false)
938 
939  {
940 
941  FTransform refTransf;
942 
943  bool result = GetRefTransform(RefActorName, RefActor, WorldContextObject, refTransf, Verbose);
944 
945  if (result)
946 
947  {
948 
949  OutTransf = GetRelativeTransform(refTransf, InTransf, IgnoreScale);
950 
951  }
952 
953  return result;
954 
955  }
956 
957 
958 
980  static FTransform GetWorldTransform(const FTransform& RefTransf,
981 
982  const FTransform& RelativeTransf,
983 
984  const bool IgnoreScale = false)
985 
986  {
987 
988  FTransform worldTransf;
989 
990  FTransform refTransf = RefTransf;
991 
992  FTransform relativeTransf = RelativeTransf;
993 
994  if (IgnoreScale)
995 
996  {
997 
998  refTransf.SetScale3D(FVector::OneVector);
999 
1000  relativeTransf.SetScale3D(FVector::OneVector);
1001 
1002  }
1003 
1004 
1005 
1006  FTransform::Multiply(&worldTransf, &relativeTransf, &refTransf);
1007 
1008 
1009 
1010  worldTransf.NormalizeRotation();
1011 
1012 
1013 
1014  return worldTransf;
1015 
1016  }
1017 
1018 
1019 
1038  static FTransform GetWorldTransform(const AActor* RefActor,
1039 
1040  const FTransform& RelativeTransf,
1041 
1042  const bool IgnoreScale = false,
1043 
1044  const bool Verbose = false)
1045 
1046  {
1047 
1048  FTransform outTransf;
1049 
1050  GetRefTransformByActor(RefActor, outTransf, Verbose);
1051 
1052  return GetWorldTransform(outTransf, RelativeTransf, IgnoreScale);
1053 
1054  }
1055 
1056 
1057 
1080  static FTransform GetWorldTransform(const FString& RefActorName,
1081 
1082  const UObject* WorldContextObject,
1083 
1084  const FTransform& RelativeTransf,
1085 
1086  const bool IgnoreScale = false,
1087 
1088  const bool Verbose = false)
1089 
1090  {
1091 
1092  FTransform outTransf;
1093 
1094  GetRefTransformByName(RefActorName, WorldContextObject, outTransf, Verbose);
1095 
1096  return GetWorldTransform(outTransf, RelativeTransf, IgnoreScale);
1097 
1098  }
1099 
1100 
1101 
1125  static FTransform GetWorldTransformFromActor(const AActor* RefActor,
1126 
1127  const FTransform& RelativeTransf,
1128 
1129  const bool IgnoreScale = false,
1130 
1131  const bool Verbose = false)
1132 
1133  {
1134 
1135  return GetWorldTransform(RefActor, RelativeTransf, IgnoreScale, Verbose);
1136 
1137  }
1138 
1139 
1140 
1166  static FTransform GetWorldTransformFromName(const FString& RefActorName,
1167 
1168  const UObject* WorldContextObject,
1169 
1170  const FTransform& RelativeTransf,
1171 
1172  const bool IgnoreScale = false,
1173 
1174  const bool Verbose = false)
1175 
1176  {
1177 
1178  return GetWorldTransform(RefActorName, WorldContextObject, RelativeTransf, IgnoreScale, Verbose);
1179 
1180  }
1181 
1182 
1183 
1210  static bool GetWorldTransform(const AActor* RefActor,
1211 
1212  const FTransform& InTransf,
1213 
1214  FTransform& OutTransf,
1215 
1216  const bool ReturnIdentityWithNullptr = true,
1217 
1218  const bool IgnoreScale = false,
1219 
1220  const bool Verbose = false)
1221 
1222  {
1223 
1224  FTransform refTransf;
1225 
1226  bool result = GetRefTransform(RefActor, refTransf, ReturnIdentityWithNullptr, Verbose);
1227 
1228  if (result)
1229 
1230  {
1231 
1232  OutTransf = GetWorldTransform(refTransf, InTransf, IgnoreScale);
1233 
1234  }
1235 
1236  return result;
1237 
1238  }
1239 
1240 
1241 
1264  static bool GetWorldTransform(const FString& RefActorName,
1265 
1266  const AActor* RefActor,
1267 
1268  const FTransform& InTransf,
1269 
1270  const UObject* WorldContextObject,
1271 
1272  FTransform& OutTransf,
1273 
1274  const bool IgnoreScale = false)
1275 
1276  {
1277 
1278  FTransform refTransf;
1279 
1280  bool result = GetRefTransform(RefActorName, RefActor, WorldContextObject, refTransf);
1281 
1282  if (result)
1283 
1284  {
1285 
1286  OutTransf = GetWorldTransform(refTransf, InTransf, IgnoreScale);
1287 
1288  }
1289 
1290  return result;
1291 
1292  }
1293 
1294 
1295 
1308  FORCEINLINE static FString GetNewROS2NodeName(const FString& InAffix = FString())
1309 
1310  {
1311 
1312  return FString::Printf(TEXT("UE%s_%s"), *InAffix, *FGuid::NewGuid().ToString());
1313 
1314  }
1315 
1316 
1317 
1332  FORCEINLINE static FString ComposeROSFullFrameId(const FString& InPrefix, const TCHAR* InFrameId)
1333 
1334  {
1335 
1336  return InPrefix.IsEmpty() ? InFrameId : FString::Printf(TEXT("%s/%s"), *InPrefix, InFrameId);
1337 
1338  }
1339 
1340 
1341 
1358  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, FString& OutValue)
1359 
1360  {
1361 
1362  return InJsonObj.Get()->TryGetStringField(InFieldName, OutValue);
1363 
1364  }
1365 
1384  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj,
1385 
1386  const FString& InFieldName,
1387 
1388  float& OutValue,
1389 
1390  float InMultiplier = 1.f)
1391 
1392  {
1393 
1394  double resultValue;
1395 
1396  bool bFieldFound = InJsonObj.Get()->TryGetNumberField(InFieldName, resultValue);
1397 
1398  if (!bFieldFound)
1399 
1400  {
1401 
1402  return false;
1403 
1404  }
1405 
1406  OutValue = static_cast<float>(resultValue) * InMultiplier;
1407 
1408  return true;
1409 
1410  }
1411 
1430  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj,
1431 
1432  const FString& InFieldName,
1433 
1434  double& OutValue,
1435 
1436  double InMultiplier = 1.)
1437 
1438  {
1439 
1440  bool bFieldFound = InJsonObj.Get()->TryGetNumberField(InFieldName, OutValue);
1441 
1442  if (!bFieldFound)
1443 
1444  {
1445 
1446  return false;
1447 
1448  }
1449 
1450  OutValue *= InMultiplier;
1451 
1452  return true;
1453 
1454  }
1455 
1472  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, int& OutValue)
1473 
1474  {
1475 
1476  return InJsonObj.Get()->TryGetNumberField(InFieldName, OutValue);
1477 
1478  }
1479 
1496  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, bool& OutValue)
1497 
1498  {
1499 
1500  return InJsonObj.Get()->TryGetBoolField(InFieldName, OutValue);
1501 
1502  }
1503 
1504 
1505 
1522  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, FVector& OutValue)
1523 
1524  {
1525 
1526  bool res = true;
1527 
1528  auto const tempJsonObj = InJsonObj->GetObjectField(InFieldName);
1529 
1530  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("x"), OutValue.X);
1531 
1532  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("y"), OutValue.Y);
1533 
1534  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("z"), OutValue.Z);
1535 
1536 
1537 
1538  return res;
1539 
1540  }
1541 
1542 
1543 
1560  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, FRotator& OutValue)
1561 
1562  {
1563 
1564  bool res = true;
1565 
1566  auto const tempJsonObj = InJsonObj->GetObjectField(InFieldName);
1567 
1568  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("roll"), OutValue.Roll);
1569 
1570  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("pitch"), OutValue.Pitch);
1571 
1572  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("yaw"), OutValue.Yaw);
1573 
1574 
1575 
1576  return res;
1577 
1578  }
1579 
1580 
1581 
1598  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, FQuat& OutValue)
1599 
1600  {
1601 
1602  bool res = true;
1603 
1604  auto const tempJsonObj = InJsonObj->GetObjectField(InFieldName);
1605 
1606  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("x"), OutValue.X);
1607 
1608  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("y"), OutValue.Y);
1609 
1610  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("z"), OutValue.Z);
1611 
1612  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("w"), OutValue.W);
1613 
1614 
1615 
1616  return res;
1617 
1618  }
1619 
1620 
1621 
1638  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, FTransform& OutValue)
1639 
1640  {
1641 
1642  bool res = true;
1643 
1644  FVector vectorParam = FVector::ZeroVector;
1645 
1646  FRotator rotatorParam = FRotator::ZeroRotator;
1647 
1648  auto const tempJsonObj = InJsonObj->GetObjectField(InFieldName);
1649 
1650  res &= GetJsonField(tempJsonObj, TEXT("position"), vectorParam);
1651 
1652  res &= GetJsonField(tempJsonObj, TEXT("orientation"), rotatorParam);
1653 
1654  OutValue = FTransform(rotatorParam, vectorParam, FVector::OneVector);
1655 
1656 
1657 
1658  return res;
1659 
1660  }
1661 
1662 
1663 
1682  template<typename T>
1683 
1684  static bool GetJsonField(const FString& InJsonString, const FString& InFieldName, T& OutValue)
1685 
1686  {
1687 
1688  TSharedRef<TJsonReader<TCHAR>> jsonReader = TJsonReaderFactory<TCHAR>::Create(InJsonString);
1689 
1690  TSharedPtr<FJsonObject> jsonObj = MakeShareable(new FJsonObject());
1691 
1692  if (!FJsonSerializer::Deserialize(jsonReader, jsonObj) && jsonObj.IsValid())
1693 
1694  {
1695 
1696  UE_LOG(LogTemp, Error, TEXT("Failed to deserialize json to object"));
1697 
1698  return false;
1699 
1700  }
1701 
1702  return GetJsonField(jsonObj, InFieldName, OutValue);
1703 
1704  }
1705 
1706 
1707 
1729  static bool GetJsonFieldVector(const FString& InJsonString, const FString& InFieldName, FVector& OutValue)
1730 
1731  {
1732 
1733  return GetJsonField<FVector>(InJsonString, InFieldName, OutValue);
1734 
1735  }
1736 
1737 
1738 
1760  static bool GetJsonFieldRotator(const FString& InJsonString, const FString& InFieldName, FRotator& OutValue)
1761 
1762  {
1763 
1764  return GetJsonField<FRotator>(InJsonString, InFieldName, OutValue);
1765 
1766  }
1767 
1768 
1769 
1791  static bool GetJsonFieldQuat(const FString& InJsonString, const FString& InFieldName, FQuat& OutValue)
1792 
1793  {
1794 
1795  return GetJsonField<FQuat>(InJsonString, InFieldName, OutValue);
1796 
1797  }
1798 
1799 
1800 
1822  static bool GetJsonFieldTransform(const FString& InJsonString, const FString& InFieldName, FTransform& OutValue)
1823 
1824  {
1825 
1826  return GetJsonField<FTransform>(InJsonString, InFieldName, OutValue);
1827 
1828  }
1829 
1830 
1831 
1852  template<typename T>
1853 
1854  FORCEINLINE static bool GetJsonFieldOrDefault(const TSharedPtr<FJsonObject>& InJsonObj,
1855 
1856  const FString& InFieldName,
1857 
1858  const T& InDefaultValue,
1859 
1860  T& OutValue)
1861 
1862  {
1863 
1864  if (GetJsonField(InJsonObj, InFieldName, OutValue))
1865 
1866  {
1867 
1868  return true;
1869 
1870  }
1871 
1872  OutValue = InDefaultValue;
1873 
1874  return false;
1875 
1876  }
1877 
1878 
1879 
1899  static UPrimitiveComponent* GetComponentOfActorFromName(const AActor* Actor, FName ComponentName)
1900 
1901  {
1902 
1903  UPrimitiveComponent* PrimComp = NULL;
1904 
1905 
1906 
1907  if (Actor != NULL)
1908 
1909  {
1910 
1911  // No name specified, use the root component
1912 
1913  if (ComponentName == NAME_None)
1914 
1915  {
1916 
1917  PrimComp = Cast<UPrimitiveComponent>(Actor->GetRootComponent());
1918 
1919  }
1920 
1921  // Name specified, see if we can find that component..
1922 
1923  else
1924 
1925  {
1926 
1927  for (UActorComponent* Comp : Actor->GetComponents())
1928 
1929  {
1930 
1931  if (Comp->GetFName() == ComponentName)
1932 
1933  {
1934 
1935  if (UChildActorComponent* ChildActorComp = Cast<UChildActorComponent>(Comp))
1936 
1937  {
1938 
1939  if (AActor* ChildActor = ChildActorComp->GetChildActor())
1940 
1941  {
1942 
1943  PrimComp = Cast<UPrimitiveComponent>(ChildActor->GetRootComponent());
1944 
1945  }
1946 
1947  }
1948 
1949  else
1950 
1951  {
1952 
1953  PrimComp = Cast<UPrimitiveComponent>(Comp);
1954 
1955  }
1956 
1957  break;
1958 
1959  }
1960 
1961  }
1962 
1963  }
1964 
1965  }
1966 
1967 
1968 
1969  return PrimComp;
1970 
1971  }
1972 
1973 
1974 
1996  static UPrimitiveComponent* GetPhysicsConstraintComponent(const UPhysicsConstraintComponent* InConstraint,
1997 
1998  EConstraintFrame::Type Frame)
1999 
2000  {
2001 
2002  if (InConstraint != nullptr)
2003 
2004  {
2005 
2006  UPrimitiveComponent* PrimComp = NULL;
2007 
2008 
2009 
2010  FName ComponentName = NAME_None;
2011 
2012  AActor* Actor = NULL;
2013 
2014 
2015 
2016  // Frame 1
2017 
2018  if (Frame == EConstraintFrame::Frame1)
2019 
2020  {
2021 
2022  // Use override component if specified
2023 
2024  if (InConstraint->OverrideComponent1.IsValid())
2025 
2026  {
2027 
2028  return InConstraint->OverrideComponent1.Get();
2029 
2030  }
2031 
2032 
2033 
2034  ComponentName = InConstraint->ComponentName1.ComponentName;
2035 
2036  Actor = InConstraint->ConstraintActor1;
2037 
2038  }
2039 
2040  // Frame 2
2041 
2042  else
2043 
2044  {
2045 
2046  // Use override component if specified
2047 
2048  if (InConstraint->OverrideComponent2.IsValid())
2049 
2050  {
2051 
2052  return InConstraint->OverrideComponent2.Get();
2053 
2054  }
2055 
2056 
2057 
2058  ComponentName = InConstraint->ComponentName2.ComponentName;
2059 
2060  Actor = InConstraint->ConstraintActor2;
2061 
2062  }
2063 
2064 
2065 
2066  return GetComponentOfActorFromName(Actor, ComponentName);
2067 
2068  }
2069 
2070  else
2071 
2072  {
2073 
2074  UE_LOG(LogTemp, Error, TEXT("[GetPhysicsConstraintComponent]Physics Constraint is not valid."));
2075 
2076  return nullptr;
2077 
2078  }
2079 
2080  }
2081 
2082 
2083 
2105  static FTransform GetPhysicsConstraintTransform(const UPhysicsConstraintComponent* InConstraint,
2106 
2107  const FTransform InitialJointToChildLink,
2108 
2109  UPrimitiveComponent* InChildLink = nullptr)
2110 
2111  {
2112 
2113  FTransform outTF = FTransform::Identity;
2114 
2115  if (InConstraint != nullptr)
2116 
2117  {
2118 
2119  UPrimitiveComponent* ChildLink = InChildLink;
2120 
2121  if (ChildLink == nullptr)
2122 
2123  {
2124 
2125  ChildLink = GetPhysicsConstraintComponent(InConstraint, EConstraintFrame::Frame2);
2126 
2127  }
2128 
2129 
2130 
2131  if (ChildLink != nullptr)
2132 
2133  {
2134 
2135  FTransform relativeTrans = URRGeneralUtils::GetRelativeTransform(InConstraint->GetComponentTransform(),
2136 
2137  ChildLink->GetComponentTransform());
2138 
2139 
2140 
2141  FVector position = relativeTrans.GetLocation() - InitialJointToChildLink.GetLocation();
2142 
2143  FRotator orientation = (relativeTrans.GetRotation() * InitialJointToChildLink.GetRotation().Inverse()).Rotator();
2144 
2145 
2146 
2147  outTF.SetLocation(position);
2148 
2149  outTF.SetRotation(orientation.Quaternion());
2150 
2151  }
2152 
2153  else
2154 
2155  {
2156 
2157  outTF = FTransform::Identity;
2158 
2159  }
2160 
2161  }
2162 
2163  else
2164 
2165  {
2166 
2167  UE_LOG(LogTemp, Error, TEXT("[GetPhysicsConstraintTransform]Physics Constraint is not valid."));
2168 
2169  outTF = FTransform::Identity;
2170 
2171  }
2172 
2173 
2174 
2175  return outTF;
2176 
2177  }
2178 
2179 
2180 
2199  static void GetPhysicsConstraintTransform(const UPhysicsConstraintComponent* InConstraint,
2200 
2201  const FTransform InitialJointToChildLink,
2202 
2203  FVector& OutPosition,
2204 
2205  FRotator& OutOrientation,
2206 
2207  UPrimitiveComponent* InChildLink = nullptr)
2208 
2209  {
2210 
2211  FTransform tf = GetPhysicsConstraintTransform(InConstraint, InitialJointToChildLink, InChildLink);
2212 
2213  OutPosition = tf.GetLocation();
2214 
2215  OutOrientation = tf.GetRotation().Rotator();
2216 
2217  }
2218 
2219 
2220 
2221 
2226  static FString PascalToSnake(const FString& InPascalString, const bool InCheckNum = false)
2227 
2228  {
2229 
2230  FString output = TEXT("");
2231 
2232  for (int32 i = 0; i < InPascalString.Len(); i++)
2233 
2234  {
2235 
2236  FString currStr = InPascalString.Mid(i, 1);
2237 
2238  FString newStr = currStr;
2239 
2240  if (i > 0 && (isupper(*TCHAR_TO_ANSI(*currStr)) || (InCheckNum && currStr.IsNumeric())))
2241 
2242  {
2243 
2244  newStr = TEXT("_") + newStr.ToLower();
2245 
2246  }
2247 
2248  else
2249 
2250  {
2251 
2252  newStr = newStr.ToLower();
2253 
2254  }
2255 
2256  output.Append(newStr);
2257 
2258  }
2259 
2260  return output;
2261 
2262  }
2263 
2264 
2265 
2266 
2271  static USceneComponent* FindChildComponentByClass(const USceneComponent* InTarget,
2272 
2273  const TSubclassOf<UActorComponent> InComponentClass,
2274 
2275  bool bIncludeAllDescendants = false)
2276 
2277  {
2278 
2279  TArray<USceneComponent*> children;
2280 
2281  InTarget->GetChildrenComponents(bIncludeAllDescendants, children);
2282 
2283  for (const auto& child : children)
2284 
2285  {
2286 
2287  if (child->IsA(InComponentClass))
2288 
2289  {
2290 
2291  return child;
2292 
2293  }
2294 
2295  }
2296 
2297  return nullptr;
2298 
2299  }
2300 
2301 };
2302 
URRGeneralUtils::GetRelativeTransform
static bool GetRelativeTransform(const FString &RefActorName, const AActor *RefActor, const FTransform &InTransf, const UObject *WorldContextObject, FTransform &OutTransf, const bool IgnoreScale=false, const bool Verbose=false)
Get the transform in reference frame.
Definition: RRGeneralUtils.h:925
URRGeneralUtils::GetNewROS2NodeName
static FORCEINLINE FString GetNewROS2NodeName(const FString &InAffix=FString())
Create Unique name start with UE + InAffix_ + Guid.
Definition: RRGeneralUtils.h:1308
URRGeneralUtils::GetPhysicsConstraintComponent
static UPrimitiveComponent * GetPhysicsConstraintComponent(const UPhysicsConstraintComponent *InConstraint, EConstraintFrame::Type Frame)
Get the Physics Constraint Component.
Definition: RRGeneralUtils.h:1996
URRGeneralUtils::GetWorldTransform
static FTransform GetWorldTransform(const FTransform &RefTransf, const FTransform &RelativeTransf, const bool IgnoreScale=false)
Get the transform in world frame.
Definition: RRGeneralUtils.h:980
URRGeneralUtils::GetRelativeTransform
static FTransform GetRelativeTransform(const AActor *RefActor, const FTransform &WorldTransf, const bool IgnoreScale=false, const bool Verbose=false)
Get the transform in reference frame. If RefActor==nullptr, return WorldTransf.
Definition: RRGeneralUtils.h:707
URRGeneralUtils::FindActorByName
static AActor * FindActorByName(const UObject *WorldContextObject, const FString &InName, const ESearchCase::Type InCaseType=ESearchCase::IgnoreCase)
Blueprint Callable, non template version of FindActorByName.
Definition: RRGeneralUtils.h:155
URRGeneralUtils::GetRelativeTransformFromName
static FTransform GetRelativeTransformFromName(const FString &RefActorName, const UObject *WorldContextObject, const FTransform &WorldTransf, const bool IgnoreScale=false, const bool Verbose=false)
Blueprint wrapper for GetRelativeTransform.
Definition: RRGeneralUtils.h:833
URRGeneralUtils::GetJsonField
static FORCEINLINE bool GetJsonField(const TSharedPtr< FJsonObject > &InJsonObj, const FString &InFieldName, bool &OutValue)
Initialize OutValue with the value of the requested field in a FJsonObject.
Definition: RRGeneralUtils.h:1496
URRGeneralUtils::GetJsonFieldQuat
static bool GetJsonFieldQuat(const FString &InJsonString, const FString &InFieldName, FQuat &OutValue)
Blueprint wrapper of GetJsonField.
Definition: RRGeneralUtils.h:1791
URRGeneralUtils::GetJsonField
static FORCEINLINE bool GetJsonField(const TSharedPtr< FJsonObject > &InJsonObj, const FString &InFieldName, float &OutValue, float InMultiplier=1.f)
Initialize OutValue with the value of the requested field in a FJsonObject.
Definition: RRGeneralUtils.h:1384
URRGeneralUtils::GetWorldTransform
static bool GetWorldTransform(const FString &RefActorName, const AActor *RefActor, const FTransform &InTransf, const UObject *WorldContextObject, FTransform &OutTransf, const bool IgnoreScale=false)
Get the transform in world frame.
Definition: RRGeneralUtils.h:1264
URRGeneralUtils::GetRefTransformByActor
static bool GetRefTransformByActor(const AActor *RefActor, FTransform &OutTransf, const bool Verbose=false)
Get the Ref Transform. If RefActor==nullptr, OutTransf = FTransform::Identity.
Definition: RRGeneralUtils.h:503
URRGeneralUtils::GetJsonField
static FORCEINLINE bool GetJsonField(const TSharedPtr< FJsonObject > &InJsonObj, const FString &InFieldName, FTransform &OutValue)
Initialize OutValue with the value of the requested field in a FJsonObject.
Definition: RRGeneralUtils.h:1638
URRGeneralUtils::GetJsonFieldTransform
static bool GetJsonFieldTransform(const FString &InJsonString, const FString &InFieldName, FTransform &OutValue)
Blueprint wrapper of GetJsonField.
Definition: RRGeneralUtils.h:1822
URRGeneralUtils::GetWorldTransformFromActor
static FTransform GetWorldTransformFromActor(const AActor *RefActor, const FTransform &RelativeTransf, const bool IgnoreScale=false, const bool Verbose=false)
Blueprint wrapper for GetRelativeTransform.
Definition: RRGeneralUtils.h:1125
URRGeneralUtils::GetRefTransformByName
static bool GetRefTransformByName(const FString &RefActorName, const UObject *WorldContextObject, FTransform &OutTransf, const bool Verbose=false)
Get the Ref Transform. Search actor from name. If no actor is found, return false and OutTransf = FTr...
Definition: RRGeneralUtils.h:558
URRGeneralUtils::GetRelativeTransformFromActor
static FTransform GetRelativeTransformFromActor(const AActor *RefActor, const FTransform &WorldTransf, const bool IgnoreScale=false, const bool Verbose=false)
Blueprint wrapper for GetRelativeTransform.
Definition: RRGeneralUtils.h:792
URRGeneralUtils::GetRefTransform
static bool GetRefTransform(const FString &RefActorName, const AActor *RefActor, const UObject *WorldContextObject, FTransform &OutTransf, const bool Verbose=false)
Get the Ref Transform. If RefActor==nullptr, search actor from RefActorName. If no actor is found,...
Definition: RRGeneralUtils.h:452
URRGeneralUtils::FindActorByName
static T * FindActorByName(UWorld *InWorld, const FString &InName, const ESearchCase::Type InCaseType=ESearchCase::IgnoreCase)
Find actor by name. GetAllActors() is expensive.
Definition: RRGeneralUtils.h:88
URRGeneralUtils::GetRelativeTransform
static FTransform GetRelativeTransform(const FTransform &RefTransf, const FTransform &WorldTransf, const bool IgnoreScale=false)
Get the transform in reference frame.
Definition: RRGeneralUtils.h:651
URRGeneralUtils::GetRelativeTransform
static bool GetRelativeTransform(const AActor *RefActor, const FTransform &InTransf, FTransform &OutTransf, const bool IgnoreScale=false, const bool ReturnIdentityWithNullptr=true, const bool Verbose=false)
Get the transform in reference frame.
Definition: RRGeneralUtils.h:873
URRGeneralUtils::FindActorListBySubname
static TArray< AActor * > FindActorListBySubname(const UObject *WorldContextObject, const FString &InSubname, const ESearchCase::Type InCaseType=ESearchCase::IgnoreCase)
Definition: RRGeneralUtils.h:331
URRGeneralUtils::GetWorldTransform
static FTransform GetWorldTransform(const AActor *RefActor, const FTransform &RelativeTransf, const bool IgnoreScale=false, const bool Verbose=false)
Get the transform in world frame. If RefActor==nullptr, return RelativeTransf.
Definition: RRGeneralUtils.h:1038
URRGeneralUtils::GetJsonField
static bool GetJsonField(const FString &InJsonString, const FString &InFieldName, T &OutValue)
template wrapper of GetJsonField.
Definition: RRGeneralUtils.h:1684
URRGeneralUtils::GetPhysicsConstraintTransform
static FTransform GetPhysicsConstraintTransform(const UPhysicsConstraintComponent *InConstraint, const FTransform InitialJointToChildLink, UPrimitiveComponent *InChildLink=nullptr)
Get the Physics Constraint Transform changes from initial joint transform, i.e. child link transfrom ...
Definition: RRGeneralUtils.h:2105
URRGeneralUtils::GetJsonField
static FORCEINLINE bool GetJsonField(const TSharedPtr< FJsonObject > &InJsonObj, const FString &InFieldName, double &OutValue, double InMultiplier=1.)
Initialize OutValue with the value of the requested field in a FJsonObject.
Definition: RRGeneralUtils.h:1430
URRGeneralUtils::GetRelativeTransform
static FTransform GetRelativeTransform(const FString &RefActorName, const UObject *WorldContextObject, const FTransform &WorldTransf, const bool IgnoreScale=false, const bool Verbose=false)
Get the transform in reference frame. If Actor with given name is not exists, return WorldTransf.
Definition: RRGeneralUtils.h:747
URRGeneralUtils::GetComponentOfActorFromName
static UPrimitiveComponent * GetComponentOfActorFromName(const AActor *Actor, FName ComponentName)
Get the component of actor from component name.
Definition: RRGeneralUtils.h:1899
URRGeneralUtils::GetRefTransform
static bool GetRefTransform(const AActor *RefActor, FTransform &OutTransf, const bool ReturnIdentityWithNullptr=true, const bool Verbose=false)
Get the Ref Transform.
Definition: RRGeneralUtils.h:365
URRGeneralUtils::GetJsonFieldOrDefault
static FORCEINLINE bool GetJsonFieldOrDefault(const TSharedPtr< FJsonObject > &InJsonObj, const FString &InFieldName, const T &InDefaultValue, T &OutValue)
Initialize OutValue with the value of the requested field in a FJsonObject.
Definition: RRGeneralUtils.h:1854
URRGeneralUtils::GetWorldTransformFromName
static FTransform GetWorldTransformFromName(const FString &RefActorName, const UObject *WorldContextObject, const FTransform &RelativeTransf, const bool IgnoreScale=false, const bool Verbose=false)
Blueprint wrapper for GetRelativeTransform.
Definition: RRGeneralUtils.h:1166
URRGeneralUtils::GetWorldTransform
static FTransform GetWorldTransform(const FString &RefActorName, const UObject *WorldContextObject, const FTransform &RelativeTransf, const bool IgnoreScale=false, const bool Verbose=false)
Get the transform in world frame. If RefActor==nullptr, return RelativeTransf.
Definition: RRGeneralUtils.h:1080
URRGeneralUtils::PascalToSnake
static FString PascalToSnake(const FString &InPascalString, const bool InCheckNum=false)
Definition: RRGeneralUtils.h:2226
URRGeneralUtils::GetWorldTransform
static bool GetWorldTransform(const AActor *RefActor, const FTransform &InTransf, FTransform &OutTransf, const bool ReturnIdentityWithNullptr=true, const bool IgnoreScale=false, const bool Verbose=false)
Get the transform in world frame.
Definition: RRGeneralUtils.h:1210
URRGeneralUtils::GetJsonField
static FORCEINLINE bool GetJsonField(const TSharedPtr< FJsonObject > &InJsonObj, const FString &InFieldName, int &OutValue)
Initialize OutValue with the value of the requested field in a FJsonObject.
Definition: RRGeneralUtils.h:1472
URRGeneralUtils::FindChildComponentByClass
static USceneComponent * FindChildComponentByClass(const USceneComponent *InTarget, const TSubclassOf< UActorComponent > InComponentClass, bool bIncludeAllDescendants=false)
Definition: RRGeneralUtils.h:2271
URRGeneralUtils::FindActorBySubname
static AActor * FindActorBySubname(const UObject *WorldContextObject, const FString &InSubname, const ESearchCase::Type InCaseType=ESearchCase::IgnoreCase)
Blueprint Callable, non template version of FindActorBySubname.
Definition: RRGeneralUtils.h:262
URRGeneralUtils
General utils.
Definition: RRGeneralUtils.h:60
URRGeneralUtils::GetPhysicsConstraintTransform
static void GetPhysicsConstraintTransform(const UPhysicsConstraintComponent *InConstraint, const FTransform InitialJointToChildLink, FVector &OutPosition, FRotator &OutOrientation, UPrimitiveComponent *InChildLink=nullptr)
Get the Physics Constraint Transform.
Definition: RRGeneralUtils.h:2199
URRGeneralUtils::GetJsonField
static FORCEINLINE bool GetJsonField(const TSharedPtr< FJsonObject > &InJsonObj, const FString &InFieldName, FRotator &OutValue)
Initialize OutValue with the value of the requested field in a FJsonObject.
Definition: RRGeneralUtils.h:1560
URRGeneralUtils::ComposeROSFullFrameId
static FORCEINLINE FString ComposeROSFullFrameId(const FString &InPrefix, const TCHAR *InFrameId)
Create prefixed frame_id.
Definition: RRGeneralUtils.h:1332
URRGeneralUtils::FindActorBySubname
static T * FindActorBySubname(UWorld *InWorld, const FString &InSubname, const ESearchCase::Type InCaseType=ESearchCase::IgnoreCase)
Find actor by subname. search actor whose name contains InSubname.
Definition: RRGeneralUtils.h:191
URRGeneralUtils::GetJsonField
static FORCEINLINE bool GetJsonField(const TSharedPtr< FJsonObject > &InJsonObj, const FString &InFieldName, FVector &OutValue)
Initialize OutValue with the value of the requested field in a FJsonObject.
Definition: RRGeneralUtils.h:1522
URRGeneralUtils::GetJsonFieldVector
static bool GetJsonFieldVector(const FString &InJsonString, const FString &InFieldName, FVector &OutValue)
Blueprint wrapper of GetJsonField.
Definition: RRGeneralUtils.h:1729
URRGeneralUtils::FindActorListBySubname
static TArray< T * > FindActorListBySubname(UWorld *InWorld, const FString &InSubname, const ESearchCase::Type InCaseType=ESearchCase::IgnoreCase)
Definition: RRGeneralUtils.h:280
URRGeneralUtils::GetJsonFieldRotator
static bool GetJsonFieldRotator(const FString &InJsonString, const FString &InFieldName, FRotator &OutValue)
Blueprint wrapper of GetJsonField.
Definition: RRGeneralUtils.h:1760
URRGeneralUtils::GetJsonField
static FORCEINLINE bool GetJsonField(const TSharedPtr< FJsonObject > &InJsonObj, const FString &InFieldName, FQuat &OutValue)
Initialize OutValue with the value of the requested field in a FJsonObject.
Definition: RRGeneralUtils.h:1598
URRGeneralUtils::GetJsonField
static FORCEINLINE bool GetJsonField(const TSharedPtr< FJsonObject > &InJsonObj, const FString &InFieldName, FString &OutValue)
Initialize OutValue with the value of the requested field in a FJsonObject.
Definition: RRGeneralUtils.h:1358