UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PBDVolumeConstraintBase.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4// HEADER_UNIT_SKIP - Internal
5
7#include "Chaos/Array.h"
10
11namespace Chaos::Softs
12{
13
15{
16 public:
19 {
21 for (uint32 i = 0; i < InParticles.Size(); ++i)
22 {
23 Com += InParticles.GetX(i);
24 }
25 Com /= (FSolverReal)InParticles.Size();
26 RefVolume = (FSolverReal)0.;
27 for (const TVec3<int32>& Constraint : Constraints)
28 {
29 const FSolverVec3& P1 = InParticles.GetX(Constraint[0]);
30 const FSolverVec3& P2 = InParticles.GetX(Constraint[1]);
31 const FSolverVec3& P3 = InParticles.GetX(Constraint[2]);
32 RefVolume += GetVolume(P1, P2, P3, Com);
33 }
34 RefVolume /= (FSolverReal)9.;
35 }
37
39 {
41 W.SetNum(InParticles.Size());
43 const FSolverReal Wg = (FSolverReal)1. / (FSolverReal)InParticles.Size();
45 for (uint32 i = 0; i < InParticles.Size(); ++i)
46 {
47 WlDenom += (InParticles.P(i) - InParticles.GetX(i)).Size();
48 }
49 for (uint32 i = 0; i < InParticles.Size(); ++i)
50 {
51 const FSolverReal Wl = (InParticles.P(i) - InParticles.GetX(i)).Size() / WlDenom;
52 W[i] = OneMinusAlpha * Wl + Alpha * Wg;
53 }
54 return W;
55 }
56
58 {
60 for (uint32 i = 0; i < InParticles.Size(); ++i)
61 {
62 Com += InParticles.P(i);
63 }
64 Com /= (FSolverReal)InParticles.Size();
66 Grads.SetNum(InParticles.Size());
67 for (FSolverVec3& Elem : Grads)
68 {
69 Elem = FSolverVec3(0.);
70 }
71 for (int32 i = 0; i < Constraints.Num(); ++i)
72 {
73 const TVec3<int32>& Constraint = Constraints[i];
74 const int32 i1 = Constraint[0];
75 const int32 i2 = Constraint[1];
76 const int32 i3 = Constraint[2];
77 const FSolverVec3& P1 = InParticles.P(i1);
78 const FSolverVec3& P2 = InParticles.P(i2);
79 const FSolverVec3& P3 = InParticles.P(i3);
80 const FSolverReal Area = GetArea(P1, P2, P3);
81 const FSolverVec3 Normal = GetNormal(P1, P2, P3, Com);
82 Grads[i1] += Area * Normal;
83 Grads[i2] += Area * Normal;
84 Grads[i3] += Area * Normal;
85 }
86 for (FSolverVec3& Elem : Grads)
87 {
88 Elem *= (FSolverReal)1. / (FSolverReal)3.;
89 }
90 return Grads;
91 }
92
94 {
96 for (uint32 i = 0; i < InParticles.Size(); ++i)
97 {
98 Com += InParticles.P(i);
99 }
100 Com /= (FSolverReal)InParticles.Size();
102 for (const TVec3<int32>& Constraint : Constraints)
103 {
104 const FSolverVec3& P1 = InParticles.P(Constraint[0]);
105 const FSolverVec3& P2 = InParticles.P(Constraint[1]);
106 const FSolverVec3& P3 = InParticles.P(Constraint[2]);
107 Volume += GetVolume(P1, P2, P3, Com);
108 }
109 Volume /= (FSolverReal)9.;
111 for (uint32 i = 0; i < InParticles.Size(); ++i)
112 {
113 Denom += W[i] * Grads[i].SizeSquared();
114 }
115 const FSolverReal S = (Volume - RefVolume) / Denom;
116 return Stiffness * S;
117 }
118
120
121protected:
123
124private:
125 // Utility functions for the triangle concept
126 FSolverVec3 GetNormal(const FSolverVec3 P1, const FSolverVec3& P2, const FSolverVec3& P3, const FSolverVec3& Com) const
127 {
128 const FSolverVec3 Normal = FSolverVec3::CrossProduct(P2 - P1, P3 - P1).GetSafeNormal();
129 if (FSolverVec3::DotProduct((P1 + P2 + P3) / (FSolverReal)3. - Com, Normal) < (FSolverReal)0.)
130 {
131 return -Normal;
132 }
133 return Normal;
134 }
135
136 FSolverReal GetArea(const FSolverVec3& P1, const FSolverVec3& P2, const FSolverVec3& P3) const
137 {
138 const FSolverVec3 B = (P2 - P1).GetSafeNormal();
139 const FSolverVec3 H = FSolverVec3::DotProduct(B, P3 - P1) * B + P1;
140 return (FSolverReal)0.5 * (P2 - P1).Size() * (P3 - H).Size();
141 }
142
143 FSolverReal GetVolume(const FSolverVec3& P1, const FSolverVec3& P2, const FSolverVec3& P3, const FSolverVec3& Com) const
144 {
145 return GetArea(P1, P2, P3) * FSolverVec3::DotProduct(P1 + P2 + P3, GetNormal(P1, P2, P3, Com));
146 }
147
148 FSolverReal RefVolume;
149 FSolverReal Stiffness;
150};
151
152} // End namespace Chaos::Softs
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
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition PBDVolumeConstraintBase.h:15
TArray< FSolverReal > GetWeights(const FSolverParticles &InParticles, const FSolverReal Alpha) const
Definition PBDVolumeConstraintBase.h:38
TArray< TVec3< int32 > > Constraints
Definition PBDVolumeConstraintBase.h:122
virtual ~FPBDVolumeConstraintBase()
Definition PBDVolumeConstraintBase.h:36
FPBDVolumeConstraintBase(const FSolverParticles &InParticles, TArray< TVec3< int32 > > &&InConstraints, const FSolverReal InStiffness=(FSolverReal) 1.)
Definition PBDVolumeConstraintBase.h:17
void SetStiffness(FSolverReal InStiffness)
Definition PBDVolumeConstraintBase.h:119
TArray< FSolverVec3 > GetGradients(const FSolverParticles &InParticles) const
Definition PBDVolumeConstraintBase.h:57
FSolverReal GetScalingFactor(const FSolverParticles &InParticles, const TArray< FSolverVec3 > &Grads, const TArray< FSolverReal > &W) const
Definition PBDVolumeConstraintBase.h:93
Definition PBDSoftsSolverParticles.h:20
Definition Vector.h:1000
Definition Constraints.Build.cs:6
Definition Array.h:670
void SetNum(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2308
Definition CollectionEmbeddedSpringConstraintFacade.cpp:6
FRealSingle FSolverReal
Definition PBDSoftsEvolutionFwd.h:31
TVector< FSolverReal, 3 > FSolverVec3
Definition PBDSoftsEvolutionFwd.h:33
FRealSingle FSolverReal
Definition SolverBody.h:38
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592