CARLA
SpringBasedVegetationComponent.h
Go to the documentation of this file.
1 // Copyright (c) 2022 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include "Components/ActorComponent.h"
10 #include "Components/SkeletalMeshComponent.h"
11 #include <vector>
12 
13 // disable warnings for eigen 3.1.0
14 #if defined(__clang__)
15 # pragma clang diagnostic push
16 # pragma clang diagnostic ignored "-Wdeprecated-register"
17 # pragma clang diagnostic ignored "-Wmisleading-indentation"
18 # pragma clang diagnostic ignored "-Wint-in-bool-context"
19 # pragma clang diagnostic ignored "-Wdeprecated-declarations"
20 # pragma clang diagnostic ignored "-Wshadow"
21 #endif
22 #include <Eigen/Dense>
23 #include <Eigen/Cholesky>
24 #include <Eigen/Eigenvalues>
25 #if defined(__clang__)
26 # pragma clang diagnostic pop
27 #endif
28 
29 #include "SpringBasedVegetationComponent.generated.h"
30 
31 USTRUCT(BlueprintType)
33 {
34  GENERATED_BODY()
35 
36  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
37  float Mass = 1.f;
38  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
39  float Length = 0.5f;
40  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
41  FVector CenterOfMass = FVector(0,0,0);
42 };
43 
44 USTRUCT(BlueprintType)
46 {
47  GENERATED_BODY()
48 
49  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
50  int JointId;
51  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
52  int ParentId;
53  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
54  FString JointName;
55  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
56  bool bIsStatic = false;
57  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
58  TArray<int> ChildrenIds;
59  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
60  FTransform Transform; // relative to parent
61  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
62  FRotator RestingAngles; // resting angle position of springs
63  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
64  float Mass = 10.f;
65  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
66  float SpringStrength = 1000.f;
67  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
68  FRotator AngularVelocity = FRotator(0,0,0);
69  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
70  FRotator AngularAcceleration = FRotator(0,0,0);
71  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
72  FTransform GlobalTransform;
73  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
74  FTransform GolbalInverseTransform;
75  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
76  TArray<FSkeletonBone> Bones;
77  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
78  FVector ExternalForces = FVector(0,0,0);
79  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
80  float CollisionForceProportionalFactor = 1.0f;
81 };
82 
84 {
85  bool CanRest = true;
86  int Iteration = 1;
87 };
88 
90 {
91  float Mass = 0.0;
92  Eigen::Matrix3d InertiaTensor = Eigen::Matrix3d::Zero();
93  Eigen::Vector3d Force = Eigen::Vector3d::Zero();
94  Eigen::Vector3d Torque = Eigen::Vector3d::Zero();
95  Eigen::Vector3d FictitiousTorque = Eigen::Vector3d::Zero();
96  Eigen::Vector3d CenterOfMass = Eigen::Vector3d::Zero();
97  Eigen::Matrix3d JointToGlobalMatrix = Eigen::Matrix3d::Zero();
98  Eigen::Vector3d AngularVelocity = Eigen::Vector3d::Zero();
99  Eigen::Vector3d LinearVelocity = Eigen::Vector3d::Zero();
100  Eigen::Vector3d AngularAcceleration = Eigen::Vector3d::Zero();
101  Eigen::Vector3d LinearAcceleration = Eigen::Vector3d::Zero();
102  Eigen::Vector3d LocalAngularAcceleration = Eigen::Vector3d::Zero();
103 };
104 
105 USTRUCT(BlueprintType)
107 {
108  GENERATED_BODY()
109 
110  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
111  TArray<FSkeletonJoint> Joints;
112  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
113  TArray<int> EndJoints;
114  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
115  TArray<int> EndToRootOrder;
116  UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
117  TArray<int> RootToEndOrder;
118 
119  void Clear();
120  void ComputeChildrenJointsAndBones();
121  void ComputeEndJoints();
122  void AddForce(const FString& BoneName, const FVector& Force);
123  void ClearExternalForces();
124  FSkeletonJoint& GetRootJoint() { return Joints[0]; }
125 };
126 
127 UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
128 class CARLA_API USpringBasedVegetationComponent : public UActorComponent
129 {
130  GENERATED_BODY()
131 
132 public:
133 
134  USpringBasedVegetationComponent(const FObjectInitializer& ObjectInitializer);
135 
136  void BeginPlay() override;
137 
138  void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
139 
140  void TickComponent(float DeltaTime,
141  enum ELevelTick TickType,
142  FActorComponentTickFunction * ThisTickFunction) override;
143 
144  void UpdateSkeletalMesh();
145 
146  void UpdateGlobalTransform();
147 
148  void GenerateCollisionCapsules();
149 
150  void ResetComponent();
151 
152  UFUNCTION(CallInEditor, Category = "Spring Based Vegetation Component")
153  void ComputeSpringStrengthForBranches();
154 
155  UFUNCTION()
156  void OnCollisionEvent(
157  UPrimitiveComponent* HitComponent,
158  AActor* OtherActor,
159  UPrimitiveComponent* OtherComponent,
160  FVector NormalImpulse,
161  const FHitResult& Hit);
162 
163  UFUNCTION()
164  void OnBeginOverlapEvent(
165  UPrimitiveComponent* OverlapComponent,
166  AActor* OtherActor,
167  UPrimitiveComponent* OtherComponent,
168  int32 OtherBodyIndex,
169  bool bFromSweep,
170  const FHitResult& SweepResult);
171 
172  UFUNCTION()
173  void OnEndOverlapEvent(
174  UPrimitiveComponent* OverlapComponent,
175  AActor* OtherActor,
176  UPrimitiveComponent* OtherComponent,
177  int32 OtherBodyIndex);
178 
179  UFUNCTION(CallInEditor, Category = "Spring Based Vegetation Component")
180  void GenerateSkeletonHierarchy();
181  // UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Spring Based Vegetation Component")
182  // TArray<AActor*> OverlappingActors;
183  // UPROPERTY(EditAnywhere, Category = "Spring Based Vegetation Component")
184  TMap<AActor*, TArray<UPrimitiveComponent*>> OverlappingActors;
185 
186  UFUNCTION(BlueprintCallable)
187  void AddForce(const FString& BoneName, const FVector& Force)
188  {
189  Skeleton.AddForce(BoneName, Force);
190  }
191  UPROPERTY(EditAnywhere, Category = "Spring Based Vegetation Component")
192  TArray<FString> FixedJointsList = {"joint1"};
193 
194  UPROPERTY(EditAnywhere, Category = "Spring Based Vegetation Component")
195  TArray<FString> DebugJointsToSimulate = {};
196 
197 private:
198 
199  void ComputePerJointProperties(
200  std::vector<FJointProperties>& JointLocalPropertiesList,
201  std::vector<FJointProperties>& JointPropertiesList);
202  void ComputeCompositeBodyContribution(
203  std::vector<FJointProperties>& JointLocalPropertiesList,
204  std::vector<FJointProperties>& JointPropertiesList);
205  void ComputeFictitiousForces(
206  std::vector<FJointProperties>& JointLocalPropertiesList,
207  std::vector<FJointProperties>& JointPropertiesList);
208  void ResolveContactsAndCollisions(
209  std::vector<FJointProperties>& JointLocalPropertiesList,
210  std::vector<FJointProperties>& JointPropertiesList);
211  void SolveEquationOfMotion(std::vector<FJointProperties>& JointPropertiesList, float DeltaTime);
212 
213  std::vector<FJointCollision> JointCollisionList;
214 
215 public:
216  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
217  USkeletalMeshComponent* SkeletalMesh;
218 
219  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
220  TArray<UPrimitiveComponent*> BoneCapsules;
221 
222  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
223  TMap<UPrimitiveComponent*, int> CapsuleToJointId;
224 
225  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
226  float Beta = 0.5f;
227 
228  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
229  float Alpha = 0.f;
230 
231  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
232  FVector Gravity = FVector(0,0,-1);
233 
234  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
235  float BaseSpringStrength = 10000.f;
236 
237  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
238  float MinSpringStrength = 2000.f;
239 
240  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
241  float HorizontalFallof = 0.1f;
242 
243  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
244  float VerticalFallof = 0.1f;
245 
246  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
247  float RestFactor = 0.5f;
248 
249  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
250  float DeltaTimeOverride = -1.f;
251 
252  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
253  float CollisionForceParameter = 10.f;
254 
255  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
256  float CollisionForceMinVel = 1.f;
257 
258  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
259  float ForceDistanceFalloffExponent = 1.f;
260  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
261  float ForceMaxDistance = 180.f;
262  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
263  float MinForceFactor = 0.01;
264  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
265  float LineTraceMaxDistance = 180.f;
266  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
267  float CapsuleRadius = 6.0f;
268  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
269  float MinBoneLength = 10.f;
270  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
271  FVector SpringStrengthMulFactor = FVector(1,1,1);
272  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
273  float VehicleCenterZOffset = 120.f;
274 
275 
276 
277 public:
278  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
279  FSkeletonHierarchy Skeleton;
280 
281  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
282  bool DebugEnableVisualization { false };
283 
284  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
285  bool DebugEnableAllCollisions { false };
286 
287  UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
288  bool bAutoComputeStrength = true;
289 };
Eigen::Vector3d LocalAngularAcceleration
geom::Transform Transform
Definition: rpc/Transform.h:16