UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
XPBDVolumeConstraints.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4// HEADER_UNIT_SKIP - Not included directly
5
7#include "ChaosStats.h"
11
12
13namespace Chaos::Softs
14{
15
17 {
18
19 public:
20 //this one only accepts tetmesh input and mesh
23 int32 ParticleOffset,
24 int32 ParticleCount,
27 )
29 {
30
33 for (int32 ElementIndex = 0; ElementIndex < InMesh.Num(); ElementIndex++)
34 {
35 TVec4<int32> Constraint = InMesh[ElementIndex];
36 const FSolverVec3& P1 = InParticles.GetX(Constraint[0]);
37 const FSolverVec3& P2 = InParticles.GetX(Constraint[1]);
38 const FSolverVec3& P3 = InParticles.GetX(Constraint[2]);
39 const FSolverVec3& P4 = InParticles.GetX(Constraint[3]);
40 Volumes[ElementIndex] = FSolverVec3::DotProduct(FSolverVec3::CrossProduct(P2 - P1, P3 - P1), P4 - P1) / (FSolverReal)6.;
41 }
42
43 InitColor(InParticles);
44 }
45
47
48
49 void Init() const
50 {
51 for (FSolverReal& Lambdas : LambdaArray) { Lambdas = (FSolverReal)0.; }
52 }
53
54 virtual void ApplyOneElement(FSolverParticles& Particles, const FSolverReal Dt, const int32 ElementIndex) const
55 {
56 //TRACE_CPUPROFILER_EVENT_SCOPE(STAT_ChaosXPBDCorotatedApplySingle);
57
58 TVec4<FSolverVec3> VolumeDelta = GetVolumeDelta(Particles, Dt, ElementIndex);
59 for (int i = 0; i < 4; i++)
60 {
61 Particles.P(MeshConstraints[ElementIndex][i]) += VolumeDelta[i];
62 }
63
64
65 }
66
67 void ApplyInSerial(FSolverParticles& Particles, const FSolverReal Dt) const
68 {
69 //SCOPE_CYCLE_COUNTER(STAT_ChaosXPBDCorotated);
71 for (int32 ElementIndex = 0; ElementIndex < MeshConstraints.Num(); ++ElementIndex)
72 {
73 ApplyOneElement(Particles, Dt, ElementIndex);
74 }
75
76
77 }
78
79 void ApplyInParallel(FSolverParticles& Particles, const FSolverReal Dt) const
80 {
81 {
83 if ((ConstraintsPerColorStartIndex.Num() > 1))//&& (MeshConstraints.Num() > Chaos_Spring_ParallelConstraintCount))
84 {
86
88 {
92 {
93 const int32 ConstraintIndex = ColorStart + Index;
94 ApplyOneElement(Particles, Dt, ConstraintIndex);
95 });
96 }
97 }
98 }
99 }
100
101 private:
102
103 void InitColor(const FSolverParticles& Particles)
104 {
105
106 const TArray<TArray<int32>> ConstraintsPerColor = FGraphColoring::ComputeGraphColoring(MeshConstraints, Particles);
107
108 // Reorder constraints based on color so each array in ConstraintsPerColor contains contiguous elements.
112 TArray<int32> OrigToReorderedIndices; // used to reorder stiffness indices
114 ReorderedVolumes.SetNumUninitialized(Volumes.Num());
115 OrigToReorderedIndices.SetNumUninitialized(MeshConstraints.Num());
116
117 ConstraintsPerColorStartIndex.Reset(ConstraintsPerColor.Num() + 1);
118
120 for (const TArray<int32>& ConstraintsBatch : ConstraintsPerColor)
121 {
124 {
127 //ReorderedMeasure[ReorderedIndex] = Measure[OrigIndex];
130
132 }
133 }
135
138 }
139
140 protected:
141
142
143 TVec4<FSolverVec3> GetVolumeDelta(const FSolverParticles& Particles, const FSolverReal Dt, const int32 ElementIndex) const
144 {
146 const TVec4<int32>& Constraint = MeshConstraints[ElementIndex];
147 const FSolverVec3& P1 = Particles.P(Constraint[0]);
148 const FSolverVec3& P2 = Particles.P(Constraint[1]);
149 const FSolverVec3& P3 = Particles.P(Constraint[2]);
150 const FSolverVec3& P4 = Particles.P(Constraint[3]);
151 const FSolverVec3 P2P1 = P2 - P1;
152 const FSolverVec3 P4P1 = P4 - P1;
153 const FSolverVec3 P3P1 = P3 - P1;
154 Grads[1] = FSolverVec3::CrossProduct(P3P1, P4P1) / (FSolverReal)6.;
155 Grads[2] = FSolverVec3::CrossProduct(P4P1, P2P1) / (FSolverReal)6.;
156 Grads[3] = FSolverVec3::CrossProduct(P2P1, P3P1) / (FSolverReal)6.;
157 Grads[0] = -(Grads[1] + Grads[2] + Grads[3]);
158 const int32 i1 = Constraint[0];
159 const int32 i2 = Constraint[1];
160 const int32 i3 = Constraint[2];
161 const int32 i4 = Constraint[3];
162
163 const FSolverReal Volume = FSolverVec3::DotProduct(FSolverVec3::CrossProduct(P2 - P1, P3 - P1), P4 - P1) / (FSolverReal)6.;
164
165 FSolverReal AlphaTilde = (FSolverReal)1. / (Stiffness * Dt * Dt);
166
167 FSolverReal DLambda = Volumes[ElementIndex] - Volume - AlphaTilde * LambdaArray[ElementIndex];
168
170 for (int i = 0; i < 4; i++)
171 {
172 for (int j = 0; j < 3; j++)
173 {
174 Denom += Grads[i][j] * Particles.InvM(MeshConstraints[ElementIndex][i]) * Grads[i][j];
175 }
176 }
177 DLambda /= Denom;
178 LambdaArray[ElementIndex] += DLambda;
180 for (int i = 0; i < 4; i++)
181 {
182 for (int j = 0; j < 3; j++)
183 {
184 Delta[i][j] = Particles.InvM(MeshConstraints[ElementIndex][i]) * Grads[i][j] * DLambda;
185 }
186 }
187 return Delta;
188 }
189
190
191
192 protected:
194
195 //material constants calculated from E:
196
200 TArray<int32> ConstraintsPerColorStartIndex; // Constraints are ordered so each batch is contiguous. This is ColorNum + 1 length so it can be used as start and end.
201 };
202
203} // End namespace Chaos::Softs
204
205#pragma once
206#pragma once
int Volume
Definition AndroidPlatformMisc.cpp:380
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define TRACE_CPUPROFILER_EVENT_SCOPE(Name)
Definition CpuProfilerTrace.h:528
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
static TArray< TArray< int32 > > ComputeGraphColoring(const TArray< TVector< int32, N > > &Graph, const TDynamicParticles< T, 3 > &InParticles, const int32 GraphParticlesStart, const int32 GraphParticlesEnd)
Definition GraphColoring.h:24
Definition PBDSoftsSolverParticles.h:20
const FSolverVec3 & P(const int32 index) const
Definition PBDSoftsSolverParticles.h:36
Definition XPBDVolumeConstraints.h:17
TArray< FSolverReal > LambdaArray
Definition XPBDVolumeConstraints.h:193
TVec4< FSolverVec3 > GetVolumeDelta(const FSolverParticles &Particles, const FSolverReal Dt, const int32 ElementIndex) const
Definition XPBDVolumeConstraints.h:143
TArray< int32 > ConstraintsPerColorStartIndex
Definition XPBDVolumeConstraints.h:200
virtual void ApplyOneElement(FSolverParticles &Particles, const FSolverReal Dt, const int32 ElementIndex) const
Definition XPBDVolumeConstraints.h:54
void ApplyInSerial(FSolverParticles &Particles, const FSolverReal Dt) const
Definition XPBDVolumeConstraints.h:67
FXPBDVolumeConstraints(const Chaos::Softs::FSolverParticles &InParticles, int32 ParticleOffset, int32 ParticleCount, const TArray< TVector< int32, 4 > > &InMesh, const FSolverReal InStiffness)
Definition XPBDVolumeConstraints.h:21
FSolverReal Stiffness
Definition XPBDVolumeConstraints.h:199
TArray< FSolverReal > Volumes
Definition XPBDVolumeConstraints.h:198
virtual ~FXPBDVolumeConstraints()
Definition XPBDVolumeConstraints.h:46
TArray< TVector< int32, 4 > > MeshConstraints
Definition XPBDVolumeConstraints.h:197
void Init() const
Definition XPBDVolumeConstraints.h:49
void ApplyInParallel(FSolverParticles &Particles, const FSolverReal Dt) const
Definition XPBDVolumeConstraints.h:79
const T InvM(const int32 Index) const
Definition DynamicParticles.h:48
Definition Vector.h:41
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
void Reset(SizeType NewSize=0)
Definition Array.h:2246
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
void Init(const ElementType &Element, SizeType Number)
Definition Array.h:3043
void SetNumUninitialized(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2369
Definition CollectionEmbeddedSpringConstraintFacade.cpp:6
FRealSingle FSolverReal
Definition PBDSoftsEvolutionFwd.h:31
TVector< FSolverReal, 3 > FSolverVec3
Definition PBDSoftsEvolutionFwd.h:33
void CHAOS_API PhysicsParallelFor(int32 InNum, TFunctionRef< void(int32)> InCallable, bool bForceSingleThreaded=false)
Definition Parallel.cpp:55
U16 Index
Definition radfft.cpp:71