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 FString& RefActorName, const AActor* RefActor, FTransform& OutTransf)
366 
367  {
368 
369  if (RefActorName.IsEmpty()) // refrence is world origin
370 
371  {
372 
373  OutTransf = FTransform::Identity;
374 
375  }
376 
377  else
378 
379  {
380 
381  if (RefActor == nullptr)
382 
383  {
384 
385  return false;
386 
387  }
388 
389  OutTransf = RefActor->GetTransform();
390 
391  }
392 
393  return true;
394 
395  }
396 
397 
398 
426  static bool GetRefTransform(const FString& RefActorName,
427 
428  const AActor* RefActor,
429 
430  const UObject* WorldContextObject,
431 
432  FTransform& OutTransf,
433 
434  const bool Verbose = false)
435 
436  {
437 
438  bool res = GetRefTransformByActor(RefActor, OutTransf, Verbose);
439 
440  if (!res)
441 
442  {
443 
444  res = GetRefTransformByName(RefActorName, WorldContextObject, OutTransf, Verbose);
445 
446  }
447 
448  return res;
449 
450  }
451 
452 
453 
477  static bool GetRefTransformByActor(const AActor* RefActor, FTransform& OutTransf, const bool Verbose = false)
478 
479  {
480 
481  if (RefActor == nullptr)
482 
483  {
484 
485  OutTransf = FTransform::Identity;
486 
487  if (Verbose)
488 
489  {
490 
491  UE_LOG(LogTemp, Error, TEXT("RefActor is not valid."));
492 
493  }
494 
495  return false;
496 
497  }
498 
499  OutTransf = RefActor->GetTransform();
500 
501  return true;
502 
503  }
504 
505 
506 
532  static bool GetRefTransformByName(const FString& RefActorName,
533 
534  const UObject* WorldContextObject,
535 
536  FTransform& OutTransf,
537 
538  const bool Verbose = false)
539 
540  {
541 
542  if (RefActorName.IsEmpty()) // refrence is world origin
543 
544  {
545 
546  OutTransf = FTransform::Identity;
547 
548  return true;
549 
550  }
551 
552 
553 
554  if (WorldContextObject == nullptr)
555 
556  {
557 
558  OutTransf = FTransform::Identity;
559 
560  if (Verbose)
561 
562  {
563 
564  UE_LOG(LogTemp, Error, TEXT("World is not given. Return Idnetity Transform"));
565 
566  }
567 
568  return false;
569 
570  }
571 
572 
573 
574  AActor* refActor = URRGeneralUtils::FindActorByName(WorldContextObject, RefActorName);
575 
576  if (refActor == nullptr)
577 
578  {
579 
580  OutTransf = FTransform::Identity;
581 
582  if (Verbose)
583 
584  {
585 
586  UE_LOG(LogTemp, Warning, TEXT("Reference Actor %s is not valid."), *RefActorName);
587 
588  }
589 
590  return false;
591 
592  }
593 
594 
595 
596  OutTransf = refActor->GetTransform();
597 
598  return true;
599 
600  }
601 
602 
603 
623  static FTransform GetRelativeTransform(const FTransform& RefTransf, const FTransform& WorldTransf)
624 
625  {
626 
627  FTransform refTransfNormalized = RefTransf;
628 
629  refTransfNormalized.NormalizeRotation();
630 
631 
632 
633  FTransform relativeTransf = WorldTransf.GetRelativeTransform(refTransfNormalized);
634 
635  relativeTransf.NormalizeRotation();
636 
637 
638 
639  return relativeTransf;
640 
641  }
642 
643 
644 
659  static FTransform GetRelativeTransform(const AActor* RefActor, const FTransform& WorldTransf, const bool Verbose = false)
660 
661  {
662 
663  FTransform outTransf;
664 
665  GetRefTransformByActor(RefActor, outTransf, Verbose);
666 
667  return GetRelativeTransform(outTransf, WorldTransf);
668 
669  }
670 
671 
672 
689  static FTransform GetRelativeTransform(const FString& RefActorName,
690 
691  const UObject* WorldContextObject,
692 
693  const FTransform& WorldTransf,
694 
695  const bool Verbose = false)
696 
697  {
698 
699  FTransform outTransf;
700 
701  GetRefTransformByName(RefActorName, WorldContextObject, outTransf, Verbose);
702 
703  return GetRelativeTransform(outTransf, WorldTransf);
704 
705  }
706 
707 
708 
728  static FTransform GetRelativeTransformFromActor(const AActor* RefActor,
729 
730  const FTransform& WorldTransf,
731 
732  const bool Verbose = false)
733 
734  {
735 
736  return GetRelativeTransform(RefActor, WorldTransf, Verbose);
737 
738  }
739 
740 
741 
761  static FTransform GetRelativeTransformFromName(const FString& RefActorName,
762 
763  const UObject* WorldContextObject,
764 
765  const FTransform& WorldTransf,
766 
767  const bool Verbose = false)
768 
769  {
770 
771  return GetRelativeTransform(RefActorName, WorldContextObject, WorldTransf, Verbose);
772 
773  }
774 
775 
776 
797  static bool GetRelativeTransform(const FString& RefActorName,
798 
799  const AActor* RefActor,
800 
801  const FTransform& InTransf,
802 
803  FTransform& OutTransf)
804 
805  {
806 
807  FTransform refTransf;
808 
809  bool result = GetRefTransform(RefActorName, RefActor, refTransf);
810 
811  if (result)
812 
813  {
814 
815  OutTransf = URRGeneralUtils::GetRelativeTransform(refTransf, InTransf);
816 
817  }
818 
819  return result;
820 
821  }
822 
823 
824 
845  static bool GetRelativeTransform(const FString& RefActorName,
846 
847  const AActor* RefActor,
848 
849  const FTransform& InTransf,
850 
851  const UObject* WorldContextObject,
852 
853  FTransform& OutTransf)
854 
855  {
856 
857  FTransform refTransf;
858 
859  bool result = GetRefTransform(RefActorName, RefActor, WorldContextObject, refTransf);
860 
861  if (result)
862 
863  {
864 
865  OutTransf = URRGeneralUtils::GetRelativeTransform(refTransf, InTransf);
866 
867  }
868 
869  return result;
870 
871  }
872 
873 
874 
894  static FTransform GetWorldTransform(const FTransform& RefTransf, const FTransform& RelativeTransf)
895 
896  {
897 
898  FTransform worldTransf;
899 
900 
901 
902  FTransform::Multiply(&worldTransf, &RelativeTransf, &RefTransf);
903 
904 
905 
906  worldTransf.NormalizeRotation();
907 
908 
909 
910  return worldTransf;
911 
912  }
913 
914 
915 
930  static FTransform GetWorldTransform(const AActor* RefActor, const FTransform& RelativeTransf, const bool Verbose = false)
931 
932  {
933 
934  FTransform outTransf;
935 
936  GetRefTransformByActor(RefActor, outTransf, Verbose);
937 
938  return GetWorldTransform(outTransf, RelativeTransf);
939 
940  }
941 
942 
943 
964  static FTransform GetWorldTransform(const FString& RefActorName,
965 
966  const UObject* WorldContextObject,
967 
968  const FTransform& RelativeTransf,
969 
970  const bool Verbose = false)
971 
972  {
973 
974  FTransform outTransf;
975 
976  GetRefTransformByName(RefActorName, WorldContextObject, outTransf, Verbose);
977 
978  return GetWorldTransform(outTransf, RelativeTransf);
979 
980  }
981 
982 
983 
1003  static FTransform GetWorldTransformFromActor(const AActor* RefActor,
1004 
1005  const FTransform& RelativeTransf,
1006 
1007  const bool Verbose = false)
1008 
1009  {
1010 
1011  return GetWorldTransform(RefActor, RelativeTransf, Verbose);
1012 
1013  }
1014 
1015 
1016 
1036  static FTransform GetWorldTransformFromName(const FString& RefActorName,
1037 
1038  const UObject* WorldContextObject,
1039 
1040  const FTransform& RelativeTransf,
1041 
1042  const bool Verbose = false)
1043 
1044  {
1045 
1046  return GetWorldTransform(RefActorName, WorldContextObject, RelativeTransf, Verbose);
1047 
1048  }
1049 
1050 
1051 
1074  static bool GetWorldTransform(const FString& RefActorName,
1075 
1076  const AActor* RefActor,
1077 
1078  const FTransform& InTransf,
1079 
1080  FTransform& OutTransf)
1081 
1082  {
1083 
1084  FTransform refTransf;
1085 
1086  bool result = GetRefTransform(RefActorName, RefActor, refTransf);
1087 
1088  if (result)
1089 
1090  {
1091 
1092  OutTransf = URRGeneralUtils::GetWorldTransform(refTransf, InTransf);
1093 
1094  }
1095 
1096  return result;
1097 
1098  }
1099 
1100 
1101 
1124  static bool GetWorldTransform(const FString& RefActorName,
1125 
1126  const AActor* RefActor,
1127 
1128  const FTransform& InTransf,
1129 
1130  const UObject* WorldContextObject,
1131 
1132  FTransform& OutTransf)
1133 
1134  {
1135 
1136  FTransform refTransf;
1137 
1138  bool result = GetRefTransform(RefActorName, RefActor, WorldContextObject, refTransf);
1139 
1140  if (result)
1141 
1142  {
1143 
1144  OutTransf = URRGeneralUtils::GetWorldTransform(refTransf, InTransf);
1145 
1146  }
1147 
1148  return result;
1149 
1150  }
1151 
1152 
1153 
1166  FORCEINLINE static FString GetNewROS2NodeName(const FString& InAffix = FString())
1167 
1168  {
1169 
1170  return FString::Printf(TEXT("UE%s_%s"), *InAffix, *FGuid::NewGuid().ToString());
1171 
1172  }
1173 
1174 
1175 
1190  FORCEINLINE static FString ComposeROSFullFrameId(const FString& InPrefix, const TCHAR* InFrameId)
1191 
1192  {
1193 
1194  return InPrefix.IsEmpty() ? InFrameId : FString::Printf(TEXT("%s/%s"), *InPrefix, InFrameId);
1195 
1196  }
1197 
1198 
1199 
1216  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, FString& OutValue)
1217 
1218  {
1219 
1220  return InJsonObj.Get()->TryGetStringField(InFieldName, OutValue);
1221 
1222  }
1223 
1242  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj,
1243 
1244  const FString& InFieldName,
1245 
1246  float& OutValue,
1247 
1248  float InMultiplier = 1.f)
1249 
1250  {
1251 
1252  double resultValue;
1253 
1254  bool bFieldFound = InJsonObj.Get()->TryGetNumberField(InFieldName, resultValue);
1255 
1256  if (!bFieldFound)
1257 
1258  {
1259 
1260  return false;
1261 
1262  }
1263 
1264  OutValue = static_cast<float>(resultValue) * InMultiplier;
1265 
1266  return true;
1267 
1268  }
1269 
1288  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj,
1289 
1290  const FString& InFieldName,
1291 
1292  double& OutValue,
1293 
1294  double InMultiplier = 1.)
1295 
1296  {
1297 
1298  bool bFieldFound = InJsonObj.Get()->TryGetNumberField(InFieldName, OutValue);
1299 
1300  if (!bFieldFound)
1301 
1302  {
1303 
1304  return false;
1305 
1306  }
1307 
1308  OutValue *= InMultiplier;
1309 
1310  return true;
1311 
1312  }
1313 
1330  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, int& OutValue)
1331 
1332  {
1333 
1334  return InJsonObj.Get()->TryGetNumberField(InFieldName, OutValue);
1335 
1336  }
1337 
1354  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, bool& OutValue)
1355 
1356  {
1357 
1358  return InJsonObj.Get()->TryGetBoolField(InFieldName, OutValue);
1359 
1360  }
1361 
1362 
1363 
1380  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, FVector& OutValue)
1381 
1382  {
1383 
1384  bool res = true;
1385 
1386  auto const tempJsonObj = InJsonObj->GetObjectField(InFieldName);
1387 
1388  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("x"), OutValue.X);
1389 
1390  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("y"), OutValue.Y);
1391 
1392  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("z"), OutValue.Z);
1393 
1394 
1395 
1396  return res;
1397 
1398  }
1399 
1400 
1401 
1418  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, FRotator& OutValue)
1419 
1420  {
1421 
1422  bool res = true;
1423 
1424  auto const tempJsonObj = InJsonObj->GetObjectField(InFieldName);
1425 
1426  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("roll"), OutValue.Roll);
1427 
1428  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("pitch"), OutValue.Pitch);
1429 
1430  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("yaw"), OutValue.Yaw);
1431 
1432 
1433 
1434  return res;
1435 
1436  }
1437 
1438 
1439 
1456  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, FQuat& OutValue)
1457 
1458  {
1459 
1460  bool res = true;
1461 
1462  auto const tempJsonObj = InJsonObj->GetObjectField(InFieldName);
1463 
1464  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("x"), OutValue.X);
1465 
1466  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("y"), OutValue.Y);
1467 
1468  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("z"), OutValue.Z);
1469 
1470  res &= tempJsonObj.Get()->TryGetNumberField(TEXT("w"), OutValue.W);
1471 
1472 
1473 
1474  return res;
1475 
1476  }
1477 
1478 
1479 
1496  FORCEINLINE static bool GetJsonField(const TSharedPtr<FJsonObject>& InJsonObj, const FString& InFieldName, FTransform& OutValue)
1497 
1498  {
1499 
1500  bool res = true;
1501 
1502  FVector vectorParam = FVector::ZeroVector;
1503 
1504  FRotator rotatorParam = FRotator::ZeroRotator;
1505 
1506  auto const tempJsonObj = InJsonObj->GetObjectField(InFieldName);
1507 
1508  res &= GetJsonField(tempJsonObj, TEXT("position"), vectorParam);
1509 
1510  res &= GetJsonField(tempJsonObj, TEXT("orientation"), rotatorParam);
1511 
1512  OutValue = FTransform(rotatorParam, vectorParam, FVector::OneVector);
1513 
1514 
1515 
1516  return res;
1517 
1518  }
1519 
1520 
1521 
1542  template<typename T>
1543 
1544  FORCEINLINE static bool GetJsonFieldOrDefault(const TSharedPtr<FJsonObject>& InJsonObj,
1545 
1546  const FString& InFieldName,
1547 
1548  const T& InDefaultValue,
1549 
1550  T& OutValue)
1551 
1552  {
1553 
1554  if (GetJsonField(InJsonObj, InFieldName, OutValue))
1555 
1556  {
1557 
1558  return true;
1559 
1560  }
1561 
1562  OutValue = InDefaultValue;
1563 
1564  return false;
1565 
1566  }
1567 
1568 
1569 
1589  static UPrimitiveComponent* GetComponentOfActorFromName(const AActor* Actor, FName ComponentName)
1590 
1591  {
1592 
1593  UPrimitiveComponent* PrimComp = NULL;
1594 
1595 
1596 
1597  if (Actor != NULL)
1598 
1599  {
1600 
1601  // No name specified, use the root component
1602 
1603  if (ComponentName == NAME_None)
1604 
1605  {
1606 
1607  PrimComp = Cast<UPrimitiveComponent>(Actor->GetRootComponent());
1608 
1609  }
1610 
1611  // Name specified, see if we can find that component..
1612 
1613  else
1614 
1615  {
1616 
1617  for (UActorComponent* Comp : Actor->GetComponents())
1618 
1619  {
1620 
1621  if (Comp->GetFName() == ComponentName)
1622 
1623  {
1624 
1625  if (UChildActorComponent* ChildActorComp = Cast<UChildActorComponent>(Comp))
1626 
1627  {
1628 
1629  if (AActor* ChildActor = ChildActorComp->GetChildActor())
1630 
1631  {
1632 
1633  PrimComp = Cast<UPrimitiveComponent>(ChildActor->GetRootComponent());
1634 
1635  }
1636 
1637  }
1638 
1639  else
1640 
1641  {
1642 
1643  PrimComp = Cast<UPrimitiveComponent>(Comp);
1644 
1645  }
1646 
1647  break;
1648 
1649  }
1650 
1651  }
1652 
1653  }
1654 
1655  }
1656 
1657 
1658 
1659  return PrimComp;
1660 
1661  }
1662 
1663 
1664 
1686  static UPrimitiveComponent* GetPhysicsConstraintComponent(const UPhysicsConstraintComponent* InConstraint,
1687 
1688  EConstraintFrame::Type Frame)
1689 
1690  {
1691 
1692  if (InConstraint != nullptr)
1693 
1694  {
1695 
1696  UPrimitiveComponent* PrimComp = NULL;
1697 
1698 
1699 
1700  FName ComponentName = NAME_None;
1701 
1702  AActor* Actor = NULL;
1703 
1704 
1705 
1706  // Frame 1
1707 
1708  if (Frame == EConstraintFrame::Frame1)
1709 
1710  {
1711 
1712  // Use override component if specified
1713 
1714  if (InConstraint->OverrideComponent1.IsValid())
1715 
1716  {
1717 
1718  return InConstraint->OverrideComponent1.Get();
1719 
1720  }
1721 
1722 
1723 
1724  ComponentName = InConstraint->ComponentName1.ComponentName;
1725 
1726  Actor = InConstraint->ConstraintActor1;
1727 
1728  }
1729 
1730  // Frame 2
1731 
1732  else
1733 
1734  {
1735 
1736  // Use override component if specified
1737 
1738  if (InConstraint->OverrideComponent2.IsValid())
1739 
1740  {
1741 
1742  return InConstraint->OverrideComponent2.Get();
1743 
1744  }
1745 
1746 
1747 
1748  ComponentName = InConstraint->ComponentName2.ComponentName;
1749 
1750  Actor = InConstraint->ConstraintActor2;
1751 
1752  }
1753 
1754 
1755 
1756  return GetComponentOfActorFromName(Actor, ComponentName);
1757 
1758  }
1759 
1760  else
1761 
1762  {
1763 
1764  UE_LOG(LogTemp, Error, TEXT("[GetPhysicsConstraintComponent]Physics Constraint is not valid."));
1765 
1766  return nullptr;
1767 
1768  }
1769 
1770  }
1771 
1772 
1773 
1795  static FTransform GetPhysicsConstraintTransform(const UPhysicsConstraintComponent* InConstraint,
1796 
1797  const FTransform InitialJointToChildLink,
1798 
1799  UPrimitiveComponent* InChildLink = nullptr)
1800 
1801  {
1802 
1803  FTransform outTF = FTransform::Identity;
1804 
1805  if (InConstraint != nullptr)
1806 
1807  {
1808 
1809  UPrimitiveComponent* ChildLink = InChildLink;
1810 
1811  if (ChildLink == nullptr)
1812 
1813  {
1814 
1815  ChildLink = GetPhysicsConstraintComponent(InConstraint, EConstraintFrame::Frame2);
1816 
1817  }
1818 
1819 
1820 
1821  if (ChildLink != nullptr)
1822 
1823  {
1824 
1825  FTransform relativeTrans = URRGeneralUtils::GetRelativeTransform(InConstraint->GetComponentTransform(),
1826 
1827  ChildLink->GetComponentTransform());
1828 
1829 
1830 
1831  FVector position = relativeTrans.GetLocation() - InitialJointToChildLink.GetLocation();
1832 
1833  FRotator orientation = (relativeTrans.GetRotation() * InitialJointToChildLink.GetRotation().Inverse()).Rotator();
1834 
1835 
1836 
1837  outTF.SetLocation(position);
1838 
1839  outTF.SetRotation(orientation.Quaternion());
1840 
1841  }
1842 
1843  else
1844 
1845  {
1846 
1847  outTF = FTransform::Identity;
1848 
1849  }
1850 
1851  }
1852 
1853  else
1854 
1855  {
1856 
1857  UE_LOG(LogTemp, Error, TEXT("[GetPhysicsConstraintTransform]Physics Constraint is not valid."));
1858 
1859  outTF = FTransform::Identity;
1860 
1861  }
1862 
1863 
1864 
1865  return outTF;
1866 
1867  }
1868 
1869 
1870 
1889  static void GetPhysicsConstraintTransform(const UPhysicsConstraintComponent* InConstraint,
1890 
1891  const FTransform InitialJointToChildLink,
1892 
1893  FVector& OutPosition,
1894 
1895  FRotator& OutOrientation,
1896 
1897  UPrimitiveComponent* InChildLink = nullptr)
1898 
1899  {
1900 
1901  FTransform tf = GetPhysicsConstraintTransform(InConstraint, InitialJointToChildLink, InChildLink);
1902 
1903  OutPosition = tf.GetLocation();
1904 
1905  OutOrientation = tf.GetRotation().Rotator();
1906 
1907  }
1908 
1909 
1910 
1911 
1916  static FString PascalToSnake(const FString& InPascalString, const bool InCheckNum = false)
1917 
1918  {
1919 
1920  FString output = TEXT("");
1921 
1922  for (int32 i = 0; i < InPascalString.Len(); i++)
1923 
1924  {
1925 
1926  FString currStr = InPascalString.Mid(i, 1);
1927 
1928  FString newStr = currStr;
1929 
1930  if (i > 0 && (isupper(*TCHAR_TO_ANSI(*currStr)) || (InCheckNum && currStr.IsNumeric())))
1931 
1932  {
1933 
1934  newStr = TEXT("_") + newStr.ToLower();
1935 
1936  }
1937 
1938  else
1939 
1940  {
1941 
1942  newStr = newStr.ToLower();
1943 
1944  }
1945 
1946  output.Append(newStr);
1947 
1948  }
1949 
1950  return output;
1951 
1952  }
1953 
1954 
1955 
1956 
1961  static USceneComponent* FindChildComponentByClass(const USceneComponent* InTarget,
1962 
1963  const TSubclassOf<UActorComponent> InComponentClass,
1964 
1965  bool bIncludeAllDescendants = false)
1966 
1967  {
1968 
1969  TArray<USceneComponent*> children;
1970 
1971  InTarget->GetChildrenComponents(bIncludeAllDescendants, children);
1972 
1973  for (const auto& child : children)
1974 
1975  {
1976 
1977  if (child->IsA(InComponentClass))
1978 
1979  {
1980 
1981  return child;
1982 
1983  }
1984 
1985  }
1986 
1987  return nullptr;
1988 
1989  }
1990 
1991 };
1992 
URRGeneralUtils::GetNewROS2NodeName
static FORCEINLINE FString GetNewROS2NodeName(const FString &InAffix=FString())
Create Unique name start with UE + InAffix_ + Guid.
Definition: RRGeneralUtils.h:1166
URRGeneralUtils::GetWorldTransformFromActor
static FTransform GetWorldTransformFromActor(const AActor *RefActor, const FTransform &RelativeTransf, const bool Verbose=false)
Blueprint wrapper for GetRelativeTransform.
Definition: RRGeneralUtils.h:1003
URRGeneralUtils::GetRelativeTransformFromActor
static FTransform GetRelativeTransformFromActor(const AActor *RefActor, const FTransform &WorldTransf, const bool Verbose=false)
Blueprint wrapper for GetRelativeTransform.
Definition: RRGeneralUtils.h:728
URRGeneralUtils::GetPhysicsConstraintComponent
static UPrimitiveComponent * GetPhysicsConstraintComponent(const UPhysicsConstraintComponent *InConstraint, EConstraintFrame::Type Frame)
Get the Physics Constraint Component.
Definition: RRGeneralUtils.h:1686
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::GetWorldTransform
static bool GetWorldTransform(const FString &RefActorName, const AActor *RefActor, const FTransform &InTransf, const UObject *WorldContextObject, FTransform &OutTransf)
Get the transform in world frame.
Definition: RRGeneralUtils.h:1124
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:1354
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:1242
URRGeneralUtils::GetRelativeTransform
static bool GetRelativeTransform(const FString &RefActorName, const AActor *RefActor, const FTransform &InTransf, FTransform &OutTransf)
Get the transform in reference frame.
Definition: RRGeneralUtils.h:797
URRGeneralUtils::GetWorldTransform
static FTransform GetWorldTransform(const AActor *RefActor, const FTransform &RelativeTransf, const bool Verbose=false)
Get the transform in world frame. If RefActor==nullptr, return RelativeTransf.
Definition: RRGeneralUtils.h:930
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:477
URRGeneralUtils::GetRelativeTransform
static bool GetRelativeTransform(const FString &RefActorName, const AActor *RefActor, const FTransform &InTransf, const UObject *WorldContextObject, FTransform &OutTransf)
Get the transform in reference frame.
Definition: RRGeneralUtils.h:845
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:1496
URRGeneralUtils::GetWorldTransform
static bool GetWorldTransform(const FString &RefActorName, const AActor *RefActor, const FTransform &InTransf, FTransform &OutTransf)
Get the transform in world frame.
Definition: RRGeneralUtils.h:1074
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:532
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 name. If no actor is found,...
Definition: RRGeneralUtils.h:426
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::GetWorldTransform
static FTransform GetWorldTransform(const FTransform &RefTransf, const FTransform &RelativeTransf)
Get the transform in world frame.
Definition: RRGeneralUtils.h:894
URRGeneralUtils::GetRelativeTransform
static FTransform GetRelativeTransform(const AActor *RefActor, const FTransform &WorldTransf, const bool Verbose=false)
Get the transform in reference frame. If RefActor==nullptr, return WorldTransf.
Definition: RRGeneralUtils.h:659
URRGeneralUtils::FindActorListBySubname
static TArray< AActor * > FindActorListBySubname(const UObject *WorldContextObject, const FString &InSubname, const ESearchCase::Type InCaseType=ESearchCase::IgnoreCase)
Definition: RRGeneralUtils.h:331
URRGeneralUtils::GetRelativeTransform
static FTransform GetRelativeTransform(const FTransform &RefTransf, const FTransform &WorldTransf)
Get the transform in reference frame.
Definition: RRGeneralUtils.h:623
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:1795
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:1288
URRGeneralUtils::GetWorldTransform
static FTransform GetWorldTransform(const FString &RefActorName, const UObject *WorldContextObject, const FTransform &RelativeTransf, const bool Verbose=false)
Get the transform in world frame. If RefActor==nullptr, return RelativeTransf.
Definition: RRGeneralUtils.h:964
URRGeneralUtils::GetComponentOfActorFromName
static UPrimitiveComponent * GetComponentOfActorFromName(const AActor *Actor, FName ComponentName)
Get the component of actor from component name.
Definition: RRGeneralUtils.h:1589
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:1544
URRGeneralUtils::GetWorldTransformFromName
static FTransform GetWorldTransformFromName(const FString &RefActorName, const UObject *WorldContextObject, const FTransform &RelativeTransf, const bool Verbose=false)
Blueprint wrapper for GetRelativeTransform.
Definition: RRGeneralUtils.h:1036
URRGeneralUtils::GetRefTransform
static bool GetRefTransform(const FString &RefActorName, const AActor *RefActor, FTransform &OutTransf)
Get the Ref Transform.
Definition: RRGeneralUtils.h:365
URRGeneralUtils::PascalToSnake
static FString PascalToSnake(const FString &InPascalString, const bool InCheckNum=false)
Definition: RRGeneralUtils.h:1916
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:1330
URRGeneralUtils::FindChildComponentByClass
static USceneComponent * FindChildComponentByClass(const USceneComponent *InTarget, const TSubclassOf< UActorComponent > InComponentClass, bool bIncludeAllDescendants=false)
Definition: RRGeneralUtils.h:1961
URRGeneralUtils::GetRelativeTransformFromName
static FTransform GetRelativeTransformFromName(const FString &RefActorName, const UObject *WorldContextObject, const FTransform &WorldTransf, const bool Verbose=false)
Blueprint wrapper for GetRelativeTransform.
Definition: RRGeneralUtils.h:761
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:1889
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:1418
URRGeneralUtils::ComposeROSFullFrameId
static FORCEINLINE FString ComposeROSFullFrameId(const FString &InPrefix, const TCHAR *InFrameId)
Create prefixed frame_id.
Definition: RRGeneralUtils.h:1190
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:1380
URRGeneralUtils::FindActorListBySubname
static TArray< T * > FindActorListBySubname(UWorld *InWorld, const FString &InSubname, const ESearchCase::Type InCaseType=ESearchCase::IgnoreCase)
Definition: RRGeneralUtils.h:280
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:1456
URRGeneralUtils::GetRelativeTransform
static FTransform GetRelativeTransform(const FString &RefActorName, const UObject *WorldContextObject, const FTransform &WorldTransf, const bool Verbose=false)
Get the transform in reference frame. If Actor with given name is not exists, return WorldTransf.
Definition: RRGeneralUtils.h:689
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:1216