UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
XPBDLongRangeConstraints.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
7#include "ChaosStats.h"
8
9DECLARE_CYCLE_STAT(TEXT("Chaos XPBD Long Range Constraint"), STAT_XPBD_LongRange, STATGROUP_Chaos);
10
11namespace Chaos::Softs
12{
13
15{
16public:
18 typedef typename Base::FTether FTether;
19
20 static constexpr FSolverReal MinStiffness = (FSolverReal)1e-1;
21 static constexpr FSolverReal MaxStiffness = (FSolverReal)1e7;
22
52
54 const FSolverParticles& Particles,
60 const FSolverVec2& InStiffness = FSolverVec2::UnitVector,
61 const FSolverVec2& InScale = FSolverVec2::UnitVector)
63 Particles,
70 InScale, // Scale clamping done in constructor
74 {
75 NumTethers = 0;
76 for (const TConstArrayView<FTether>& TetherBatch : Tethers)
77 {
78 NumTethers += TetherBatch.Num();
79 }
80 Lambdas.Reserve(NumTethers);
81 }
82
83 virtual ~FXPBDLongRangeConstraints() override {}
84
87 const TMap<FString, TConstArrayView<FRealSingle>>& WeightMaps,
88 FSolverReal MeshScale)
89 {
91 {
94 {
96 Stiffness = FPBDStiffness(
97 WeightedValue,
98 WeightMaps.FindRef(WeightMapName),
99 ParticleCount,
103 }
104 else
105 {
106 Stiffness.SetWeightedValue(WeightedValue, MaxStiffness);
107 }
108 }
110 {
111 const FSolverVec2 WeightedValue = FSolverVec2(GetWeightedFloatXPBDTetherScale(PropertyCollection)).ClampAxes(MinTetherScale, MaxTetherScale) * MeshScale;
113 {
115 TetherScale = FPBDWeightMap(WeightedValue, WeightMaps.FindRef(WeightMapName), ParticleCount);
116 }
117 else
118 {
119 TetherScale.SetWeightedValue(WeightedValue);
120 }
121 }
122 }
123
124 // Set the stiffness and scale values used by the constraint
126 {
128 TetherScale.SetWeightedValue(InTetherScale.ClampAxes(MinTetherScale, MaxTetherScale) * MeshScale);
129 }
130
131 // Set stiffness offset and range, as well as the simulation stiffness exponent
132 void ApplyProperties(const FSolverReal /*Dt*/, const int32 /*NumIterations*/)
133 {
134 Stiffness.ApplyXPBDValues(MaxStiffness);
135 TetherScale.ApplyValues();
136 }
137
138 void Init() const
139 {
140 Lambdas.Reset();
141 Lambdas.AddZeroed(NumTethers);
142 }
143
144 void Apply(FSolverParticles& Particles, const FSolverReal Dt) const
145 {
147 // Run particles in parallel, and ranges in sequence to avoid a race condition when updating the same particle from different tethers
149
150 if (Stiffness.HasWeightMap())
151 {
152 if (TetherScale.HasWeightMap())
153 {
155 for (const TConstArrayView<FTether>& TetherBatch : Tethers)
156 {
157 PhysicsParallelFor(TetherBatch.Num(), [this, &Particles, Dt, &TetherBatch, ConstraintOffset](int32 Index)
158 {
159 const FTether& Tether = TetherBatch[Index];
160 const int32 LocalParticleIndex = GetEndIndex(Tether);
161 const FSolverReal Scale = TetherScale[LocalParticleIndex];
162 const FSolverReal ExpStiffnessValue = Stiffness[LocalParticleIndex];
163 Apply(Particles, Dt, Tether, ConstraintOffset + Index, ExpStiffnessValue, Scale);
164 }, TetherBatch.Num() < MinParallelSize);
166 }
167 }
168 else
169 {
170 const FSolverReal ScaleValue = (FSolverReal)TetherScale;
172 for (const TConstArrayView<FTether>& TetherBatch : Tethers)
173 {
175 {
176 const FTether& Tether = TetherBatch[Index];
177 const int32 LocalParticleIndex = GetEndIndex(Tether);
178 const FSolverReal ExpStiffnessValue = Stiffness[LocalParticleIndex];
179 Apply(Particles, Dt, Tether, ConstraintOffset + Index, ExpStiffnessValue, ScaleValue);
180 }, TetherBatch.Num() < MinParallelSize);
182 }
183 }
184 }
185 else
186 {
187 const FSolverReal ExpStiffnessValue = (FSolverReal)Stiffness;
188
189 if (TetherScale.HasWeightMap())
190 {
192 for (const TConstArrayView<FTether>& TetherBatch : Tethers)
193 {
195 {
196 const FTether& Tether = TetherBatch[Index];
197 const int32 LocalParticleIndex = GetEndIndex(Tether);
198 const FSolverReal Scale = TetherScale[LocalParticleIndex];
199 Apply(Particles, Dt, Tether, ConstraintOffset + Index, ExpStiffnessValue, Scale);
200 }, TetherBatch.Num() < MinParallelSize);
202 }
203 }
204 else
205 {
206 const FSolverReal ScaleValue = (FSolverReal)TetherScale;
208 for (const TConstArrayView<FTether>& TetherBatch : Tethers)
209 {
211 {
212 const FTether& Tether = TetherBatch[Index];
213 Apply(Particles, Dt, Tether, ConstraintOffset + Index, ExpStiffnessValue, ScaleValue);
214 }, TetherBatch.Num() < MinParallelSize);
216 }
217 }
218 }
219 }
220
221private:
222 void Apply(FSolverParticles& Particles, const FSolverReal Dt, const FTether& Tether, int32 ConstraintIndex, const FSolverReal InStiffness, const FSolverReal InScale) const
223 {
225 {
226 return;
227 }
228 FSolverVec3 Direction;
230 GetDelta(Particles, Tether, InScale, Direction, Offset);
231
232 FSolverReal& Lambda = Lambdas[ConstraintIndex];
233 const FSolverReal Alpha = (FSolverReal)1 / (InStiffness * Dt * Dt);
234
235 const FSolverReal DLambda = (Offset - Alpha * Lambda) / ((FSolverReal)1. + Alpha);
236 Particles.P(GetEndParticle(Tether)) += DLambda * Direction;
237 Lambda += DLambda;
238 }
239
242 using Base::Tethers;
245 using Base::Stiffness;
246 using Base::TetherScale;
247
248 mutable TArray<FSolverReal> Lambdas;
249 int32 NumTethers;
250
253};
254
255} // End namespace Chaos::Softs
#define UE_CHAOS_DECLARE_PROPERTYCOLLECTION_NAME(PropertyName, Type)
Definition CollectionPropertyFacade.h:969
@ ForceInit
Definition CoreMiscDefines.h:155
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define DECLARE_CYCLE_STAT(CounterName, StatId, GroupId)
Definition Stats.h:669
#define SCOPE_CYCLE_COUNTER(Stat)
Definition Stats.h:650
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
uint32 Offset
Definition VulkanMemory.cpp:4033
Definition CollectionPropertyFacade.h:49
Definition PBDLongRangeConstraintsBase.h:17
const int32 ParticleOffset
Definition PBDLongRangeConstraintsBase.h:129
static constexpr FSolverReal MaxTetherScale
Definition PBDLongRangeConstraintsBase.h:22
int32 GetEndParticle(const FTether &Tether) const
Definition PBDLongRangeConstraintsBase.h:82
FPBDWeightMap TetherScale
Definition PBDLongRangeConstraintsBase.h:132
const TArray< TConstArrayView< FTether > > & Tethers
Definition PBDLongRangeConstraintsBase.h:128
static constexpr FSolverReal MinTetherScale
Definition PBDLongRangeConstraintsBase.h:21
FSolverVec3 GetDelta(const SolverParticlesOrRange &Particles, const FTether &Tether, const FSolverReal InScale) const
Definition PBDLongRangeConstraintsBase.h:99
static CHAOS_API int32 GetMinParallelBatchSize()
Definition PBDLongRangeConstraintsBase.cpp:22
const int32 ParticleCount
Definition PBDLongRangeConstraintsBase.h:130
FPBDStiffness Stiffness
Definition PBDLongRangeConstraintsBase.h:131
Definition PBDStiffness.h:21
void SetWeightedValue(const FSolverVec2 &InWeightedValue, FSolverReal MaxStiffness=DefaultPBDMaxStiffness)
Definition PBDStiffness.h:65
static constexpr int32 DefaultTableSize
Definition PBDStiffness.h:26
void ApplyXPBDValues(const FSolverReal MaxStiffnesss)
Definition PBDStiffness.h:164
static constexpr FSolverReal DefaultParameterFitBase
Definition PBDStiffness.h:25
Definition PBDWeightMap.h:19
void ApplyValues(bool *bOutUpdated=nullptr)
Definition PBDWeightMap.h:71
void SetWeightedValue(const FSolverVec2 &InWeightedValue)
Definition PBDWeightMap.h:62
bool HasWeightMap() const
Definition PBDWeightMap.h:55
Definition PBDSoftsSolverParticles.h:20
const FSolverVec3 & P(const int32 index) const
Definition PBDSoftsSolverParticles.h:36
Definition XPBDLongRangeConstraints.h:15
FPBDLongRangeConstraintsBase Base
Definition XPBDLongRangeConstraints.h:17
void Init() const
Definition XPBDLongRangeConstraints.h:138
FXPBDLongRangeConstraints(const FSolverParticles &Particles, const int32 InParticleOffset, const int32 InParticleCount, const TArray< TConstArrayView< TTuple< int32, int32, FRealSingle > > > &InTethers, const TConstArrayView< FRealSingle > &StiffnessMultipliers, const TConstArrayView< FRealSingle > &ScaleMultipliers, const FSolverVec2 &InStiffness=FSolverVec2::UnitVector, const FSolverVec2 &InScale=FSolverVec2::UnitVector)
Definition XPBDLongRangeConstraints.h:53
void ApplyProperties(const FSolverReal, const int32)
Definition XPBDLongRangeConstraints.h:132
static constexpr FSolverReal MaxStiffness
Definition XPBDLongRangeConstraints.h:21
FXPBDLongRangeConstraints(const FSolverParticles &Particles, const int32 InParticleOffset, const int32 InParticleCount, const TArray< TConstArrayView< TTuple< int32, int32, FRealSingle > > > &InTethers, const TMap< FString, TConstArrayView< FRealSingle > > &WeightMaps, const FCollectionPropertyConstFacade &PropertyCollection, FSolverReal MeshScale)
Definition XPBDLongRangeConstraints.h:23
void SetProperties(const FSolverVec2 &InStiffness, const FSolverVec2 &InTetherScale, FSolverReal MeshScale=(FSolverReal) 1)
Definition XPBDLongRangeConstraints.h:125
virtual ~FXPBDLongRangeConstraints() override
Definition XPBDLongRangeConstraints.h:83
static constexpr FSolverReal MinStiffness
Definition XPBDLongRangeConstraints.h:20
void SetProperties(const FCollectionPropertyConstFacade &PropertyCollection, const TMap< FString, TConstArrayView< FRealSingle > > &WeightMaps, FSolverReal MeshScale)
Definition XPBDLongRangeConstraints.h:85
Base::FTether FTether
Definition XPBDLongRangeConstraints.h:18
void Apply(FSolverParticles &Particles, const FSolverReal Dt) const
Definition XPBDLongRangeConstraints.h:144
Definition Vector.h:1000
Definition Array.h:670
void Reset(SizeType NewSize=0)
Definition Array.h:2246
SizeType AddZeroed()
Definition Array.h:2755
UE_FORCEINLINE_HINT void Reserve(SizeType Number)
Definition Array.h:3016
Definition UnrealString.h.inl:34
Definition CollectionEmbeddedSpringConstraintFacade.cpp:6
TVector< FSolverReal, 2 > FSolverVec2
Definition PBDSoftsEvolutionFwd.h:32
FRealSingle FSolverReal
Definition PBDSoftsEvolutionFwd.h:31
void CHAOS_API PhysicsParallelFor(int32 InNum, TFunctionRef< void(int32)> InCallable, bool bForceSingleThreaded=false)
Definition Parallel.cpp:55
FRealSingle FSolverReal
Definition SolverBody.h:38
U16 Index
Definition radfft.cpp:71
Definition Tuple.h:652