CARLA
 
载入中...
搜索中...
未找到
SpringBasedVegetationComponent.h
浏览该文件的文档.
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// 禁用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
31USTRUCT(BlueprintType)// 定义一个可在蓝图中使用的结构体
33{
34// GENERATED_BODY()宏用于自动生成Unreal Engine需要的反射代码
35// 这使得结构体可以在Unreal Engine的序列化系统中被正确处理
36 GENERATED_BODY()
37// 使用UPROPERTY宏定义一个属性
38// EditAnywhere表示该属性可以在属性窗口中编辑,无论是在C++代码中还是在蓝图编辑器中
39// BlueprintReadWrite表示该属性在蓝图编辑器中可读可写
40// Category参数为该属性指定了一个分类,便于在属性窗口中查找
41 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
42 float Mass = 1.f;
43 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
44 float Length = 0.5f;
45 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
46 FVector CenterOfMass = FVector(0,0,0);
47};
48
49USTRUCT(BlueprintType) // 定义一个可在蓝图中使用的结构体
50struct FSkeletonJoint// 骨骼关节的结构体
51{
52 GENERATED_BODY()
53
54 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
55 int JointId;
56 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
57 int ParentId;
58 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
59 FString JointName;
60 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
61 bool bIsStatic = false;
62 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
63 TArray<int> ChildrenIds;
64 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
65 FTransform Transform; // relative to parent
66 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
67 FRotator RestingAngles; // resting angle position of springs
68 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
69 float Mass = 10.f;
70 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
71 float SpringStrength = 1000.f;
72 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
73 FRotator AngularVelocity = FRotator(0,0,0);
74 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
75 FRotator AngularAcceleration = FRotator(0,0,0);
76 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
77 FTransform GlobalTransform;
78 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
79 FTransform GolbalInverseTransform;
80 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
81 TArray<FSkeletonBone> Bones;
82 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
83 FVector ExternalForces = FVector(0,0,0);
84 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
85 float CollisionForceProportionalFactor = 1.0f;
86};
87
88struct FJointCollision// 关节碰撞结构体
89{
90 bool CanRest = true;
91 int Iteration = 1;
92};
93
95{
96 // 关节的质量,默认为0.0
97 // 质量是影响物体惯性和重力效应的物理量
98 float Mass = 0.0;
99// 关节的惯性张量,初始化为零矩阵
100// 惯性张量描述了物体在旋转时的惯性,是质量分布和物体形状的函数
101 Eigen::Matrix3d InertiaTensor = Eigen::Matrix3d::Zero();
102// 作用在关节上的力,初始化为零向量
103// 力是改变物体运动状态的原因,包括线性和旋转运动
104 Eigen::Vector3d Force = Eigen::Vector3d::Zero();
105// 作用在关节上的力矩,初始化为零向量
106// 力矩是改变物体旋转状态的原因,与力和力臂的叉积有关
107 Eigen::Vector3d Torque = Eigen::Vector3d::Zero();
108// 虚力矩,初始化为零向量
109// 虚力矩可能用于计算或校正,通常与刚体动力学中的某些特定问题相关
110 Eigen::Vector3d FictitiousTorque = Eigen::Vector3d::Zero();
111// 关节的质心位置,初始化为零向量
112// 质心是物体质量分布的平均位置,对于计算物体的整体运动很重要
113 Eigen::Vector3d CenterOfMass = Eigen::Vector3d::Zero();
114// 关节到全局坐标系的变换矩阵,初始化为零矩阵
115// 这个矩阵用于将关节的局部坐标转换为全局坐标
116 Eigen::Matrix3d JointToGlobalMatrix = Eigen::Matrix3d::Zero();
117// 关节的角速度,初始化为零向量
118 // 角速度描述了物体旋转的快慢和方向
119 Eigen::Vector3d AngularVelocity = Eigen::Vector3d::Zero();
120 Eigen::Vector3d LinearVelocity = Eigen::Vector3d::Zero();
121 Eigen::Vector3d AngularAcceleration = Eigen::Vector3d::Zero();
122 Eigen::Vector3d LinearAcceleration = Eigen::Vector3d::Zero();
123 Eigen::Vector3d LocalAngularAcceleration = Eigen::Vector3d::Zero();
124};
125
126USTRUCT(BlueprintType)
128{
129 GENERATED_BODY()
130
131 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
132 TArray<FSkeletonJoint> Joints;
133 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
134 TArray<int> EndJoints;
135 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
136 TArray<int> EndToRootOrder;
137 UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Skeleton Bone")
138 TArray<int> RootToEndOrder;
139
140 void Clear();
141 void ComputeChildrenJointsAndBones();
142 void ComputeEndJoints();
143 void AddForce(const FString& BoneName, const FVector& Force);
144 void ClearExternalForces();
145 FSkeletonJoint& GetRootJoint() { return Joints[0]; }
146};
147
148UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
149class CARLA_API USpringBasedVegetationComponent : public UActorComponent
150{
151 GENERATED_BODY()
152
153public:
154
155 USpringBasedVegetationComponent(const FObjectInitializer& ObjectInitializer);
156
157 void BeginPlay() override;
158
159 void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
160
161 void TickComponent(float DeltaTime,
162 enum ELevelTick TickType,
163 FActorComponentTickFunction * ThisTickFunction) override;
164
165 void UpdateSkeletalMesh();
166
167 void UpdateGlobalTransform();
168
169 void GenerateCollisionCapsules();
170
171 void ResetComponent();
172
173 UFUNCTION(CallInEditor, Category = "Spring Based Vegetation Component")
174 void ComputeSpringStrengthForBranches();
175
176 UFUNCTION()
177 void OnCollisionEvent(
178 UPrimitiveComponent* HitComponent,
179 AActor* OtherActor,
180 UPrimitiveComponent* OtherComponent,
181 FVector NormalImpulse,
182 const FHitResult& Hit);
183
184 UFUNCTION()
185 void OnBeginOverlapEvent(
186 UPrimitiveComponent* OverlapComponent,
187 AActor* OtherActor,
188 UPrimitiveComponent* OtherComponent,
189 int32 OtherBodyIndex,
190 bool bFromSweep,
191 const FHitResult& SweepResult);
192
193 UFUNCTION()
194 void OnEndOverlapEvent(
195 UPrimitiveComponent* OverlapComponent,
196 AActor* OtherActor,
197 UPrimitiveComponent* OtherComponent,
198 int32 OtherBodyIndex);
199
200 UFUNCTION(CallInEditor, Category = "Spring Based Vegetation Component")
201 void GenerateSkeletonHierarchy();
202 // UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Spring Based Vegetation Component")
203 // TArray<AActor*> OverlappingActors;
204 // UPROPERTY(EditAnywhere, Category = "Spring Based Vegetation Component")
205 TMap<AActor*, TArray<UPrimitiveComponent*>> OverlappingActors;
206
207 UFUNCTION(BlueprintCallable)
208 void AddForce(const FString& BoneName, const FVector& Force)
209 {
210 Skeleton.AddForce(BoneName, Force);
211 }
212 UPROPERTY(EditAnywhere, Category = "Spring Based Vegetation Component")
213 TArray<FString> FixedJointsList = {"joint1"};
214
215 UPROPERTY(EditAnywhere, Category = "Spring Based Vegetation Component")
216 TArray<FString> DebugJointsToSimulate = {};
217
218private:
219
220 void ComputePerJointProperties(
221 std::vector<FJointProperties>& JointLocalPropertiesList,
222 std::vector<FJointProperties>& JointPropertiesList);
223 void ComputeCompositeBodyContribution(
224 std::vector<FJointProperties>& JointLocalPropertiesList,
225 std::vector<FJointProperties>& JointPropertiesList);
226 void ComputeFictitiousForces(
227 std::vector<FJointProperties>& JointLocalPropertiesList,
228 std::vector<FJointProperties>& JointPropertiesList);
229 void ResolveContactsAndCollisions(
230 std::vector<FJointProperties>& JointLocalPropertiesList,
231 std::vector<FJointProperties>& JointPropertiesList);
232 void SolveEquationOfMotion(std::vector<FJointProperties>& JointPropertiesList, float DeltaTime);
233
234 std::vector<FJointCollision> JointCollisionList;
235
236public:
237 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
238 USkeletalMeshComponent* SkeletalMesh;
239
240 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
241 TArray<UPrimitiveComponent*> BoneCapsules;
242
243 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
244 TMap<UPrimitiveComponent*, int> CapsuleToJointId;
245
246 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
247 float Beta = 0.5f;
248
249 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
250 float Alpha = 0.f;
251
252 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
253 FVector Gravity = FVector(0,0,-1);
254
255 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
256 float BaseSpringStrength = 10000.f;
257
258 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
259 float MinSpringStrength = 2000.f;
260
261 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
262 float HorizontalFallof = 0.1f;
263
264 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
265 float VerticalFallof = 0.1f;
266
267 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
268 float RestFactor = 0.5f;
269
270 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
271 float DeltaTimeOverride = -1.f;
272
273 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
274 float CollisionForceParameter = 10.f;
275
276 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
277 float CollisionForceMinVel = 1.f;
278
279 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
280 float ForceDistanceFalloffExponent = 1.f;
281 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
282 float ForceMaxDistance = 180.f;
283 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
284 float MinForceFactor = 0.01;
285 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
286 float LineTraceMaxDistance = 180.f;
287 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
288 float CapsuleRadius = 6.0f;
289 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
290 float MinBoneLength = 10.f;
291 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
292 FVector SpringStrengthMulFactor = FVector(1,1,1);
293 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
294 float VehicleCenterZOffset = 120.f;
295
296
297
298public:
299 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
300 FSkeletonHierarchy Skeleton;
301
302 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
303 bool DebugEnableVisualization { false };
304
305 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
306 bool DebugEnableAllCollisions { false };
307
308 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Spring Based Vegetation Component")
309 bool bAutoComputeStrength = true;
310};
sode override
Definition ActorData.h:280
return true