UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MLLevelset.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Chaos/ArrayND.h"
5#include "Chaos/Box.h"
10
11struct FKMLLevelSetElem;
12
13namespace Chaos
14{
15
23
36
37class FMLLevelSet final : public FImplicitObject
38{
39 public:
41
42 FMLLevelSet(const FMLLevelSet& Other) = delete;
44 CHAOS_API virtual ~FMLLevelSet();
45
46 CHAOS_API virtual Chaos::FImplicitObjectPtr CopyGeometry() const override;
48
49 CHAOS_API virtual FReal PhiWithNormal(const FVec3& x, FVec3& Normal) const override;
52 const Chaos::Softs::FSolverReal CollisionThickness, const int32 MLLevelsetThread, const int32 BatchBegin, const int32 BatchEnd) const;
53
54 CHAOS_API FReal SignedDistance(const FVec3& x) const;
58
59 const int32 GetNumberOfActiveBones() const { return ActiveBoneNames.Num(); }
60
61 const TArray<FName>& GetActiveBoneNames() const { return ActiveBoneNames; }
62 virtual const FAABB3 BoundingBox() const override { return LocalBoundingBox; }
63 const FVector3f GetTrainingGridMin() const { return TrainingGridMin; }
64 const FVector3f GetTrainingGridVector(int32 Index) const { return TrainingGridUnitAxesXYZ[Index] * TrainingGridAxesLengthsXYZ[Index]; }
65
66 // Returns a const ref to the underlying grid structure
67 const TUniformGrid<FReal, 3>& GetGrid() const { return DebugGrid; }
68
69 // Returns a const ref to the underlying phi grid
70 const TArrayND<FReal, 3>& GetPhiArray() const { return DebugPhi; }
71
76
78 {
80 TBox<FReal, 3>::SerializeAsAABB(Ar, LocalBoundingBox);
81 Ar << ActiveBoneNames;
82 Ar << ActiveBonesRelativeTransforms;
83 Ar << SignedDistanceModelWeights;
84 Ar << SignedDistanceModelWeightsShapes;
85 Ar << IncorrectZoneModelWeights;
86 Ar << IncorrectZoneModelWeightsShapes;
87 Ar << bUseIncorrectZoneModel;
88 Ar << SignedDistanceScaling;
89 Ar << TrainingGridMin;
90 Ar << TrainingGridUnitAxesXYZ;
91 Ar << TrainingGridAxesLengthsXYZ;
92 Ar << TotalNumberOfRotationComponents;
93 Ar << ActiveBonesRotationComponents;
94 Ar << ActiveBonesReferenceRotations;
95 Ar << ActiveBonesReferenceTranslations;
96 Ar << DebugGrid;
97 Ar << DebugPhi;
98
99 if (Ar.IsLoading())
100 {
101 SignedDistanceNeuralInferences.Empty();
102 SignedDistanceNeuralInferences.Add(FMLLevelSetNeuralInference(NNESignedDistanceModel, SignedDistanceModelWeightsShapes));
103 if (bUseIncorrectZoneModel)
104 {
105 IncorrectZoneNeuralInferences.Empty();
106 IncorrectZoneNeuralInferences.Add(FMLLevelSetNeuralInference(NNEIncorrectZoneModel, IncorrectZoneModelWeightsShapes));
107 }
108 }
109 }
110
111 virtual void Serialize(FChaosArchive& Ar) override
112 {
113 SerializeImp(Ar);
114 }
115
116 virtual void Serialize(FArchive& Ar) override
117 {
118 SerializeImp(Ar);
119 }
120
121 // Used to generate a simple debug surface
125
126 virtual uint32 GetTypeHash() const override
127 {
128 uint32 Result = 0;
129
130 for(int32 LayerIdx = 0; LayerIdx < SignedDistanceModelWeights.Num(); ++LayerIdx)
131 {
132 for (int32 i = 0; i < SignedDistanceModelWeights[LayerIdx].Num(); ++i)
133 {
134 Result = HashCombine(Result, ::GetTypeHash(SignedDistanceModelWeights[LayerIdx][i]));
135 }
136 }
137
138 for (int32 LayerIdx = 0; LayerIdx < IncorrectZoneModelWeights.Num(); ++LayerIdx)
139 {
140 for (int32 i = 0; i < IncorrectZoneModelWeights[LayerIdx].Num(); ++i)
141 {
142 Result = HashCombine(Result, ::GetTypeHash(IncorrectZoneModelWeights[LayerIdx][i]));
143 }
144 }
145
146 return Result;
147 }
148
149 private:
150 void ProcessTrainingGridAxesVectors();
151 void ComputeSignedDistanceNetworkWeightsInput(TArray<TArray<float, TAlignedHeapAllocator<64>>>& NetworkWeightsInput) const;
152 void ComputeIncorrectZoneNetworkWeightsInput(TArray<TArray<float, TAlignedHeapAllocator<64>>>& NetworkWeightsInput) const;
154 void LoadMLModelWeightsFromString(const FString& MLModelWeightsString, TArray<TArray<float>>& ModelWeightArray);
155 void BuildNNEModel(const TArray<int32>& ModelArchitectureActivationNodeSizes, TObjectPtr<UNNEModelData> NNEModelData, const FString& ModelWeightsString,
158 const bool IsFTransformArraysDifferent(TArray<FTransform>& FTransformArr1, TArray<FTransform>& FTransformArr2, FReal Tol = UE_KINDA_SMALL_NUMBER) const;
159
160 // We assume the closest active bone joint is always the first active bone.
161 const int32 GetClosestActiveBoneIndex(const FVec3 ParticlePositionMS) const { return 0; }
162
163 private:
164 //Relative Bone Transform Related Data
165 TArray<FName> ActiveBoneNames;
166 TArray<FTransform> ActiveBonesRelativeTransforms;
167
168 // NNE ML Model Elements
169 // ToDo: Move these elements inside a new struct or FMLLevelSetNeuralInference
170 TSharedPtr<UE::NNE::IModelCPU> NNESignedDistanceModel;
171 TArray<FMLLevelSetNeuralInference> SignedDistanceNeuralInferences;
172 TArray<TArray<float>> SignedDistanceModelWeights;
173 TArray<TArray<int32>> SignedDistanceModelWeightsShapes;
174
175 // NNE Incorrect Zone Model Elements
176 // ToDo: Move these elements inside a new struct or FMLLevelSetNeuralInference
177 TSharedPtr<UE::NNE::IModelCPU> NNEIncorrectZoneModel;
178 TArray<FMLLevelSetNeuralInference> IncorrectZoneNeuralInferences;
179 TArray<TArray<float>> IncorrectZoneModelWeights;
180 TArray<TArray<int32>> IncorrectZoneModelWeightsShapes;
181 bool bUseIncorrectZoneModel;
182
183 // Inference Input Transformation.
184 FAABB3 LocalBoundingBox;
185 float SignedDistanceScaling;
186 FVector3f TrainingGridMin;
187 TArray<FVector3f> TrainingGridUnitAxesXYZ;
188 TArray<float> TrainingGridAxesLengthsXYZ;
189 int32 TotalNumberOfRotationComponents;
190 TArray<TArray<int32>> ActiveBonesRotationComponents;
191 TArray<FVector3f> ActiveBonesReferenceRotations;
192 TArray<FVector3f>ActiveBonesReferenceTranslations;
193
194 //Debug Drawing
195 TUniformGrid<FReal, 3> DebugGrid;
196 TArrayND<FReal, 3> DebugPhi;
197
198private:
199 FMLLevelSet() : FImplicitObject(EImplicitObject::HasBoundingBox, ImplicitObjectType::MLLevelSet) {} //needed for copy()
201
202 friend FImplicitObject; //needed for serialization
203 friend ::FKMLLevelSetElem; //needed for serialization
204};
205}
#define FORCEINLINE
Definition AndroidPlatform.h:140
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
constexpr uint32 HashCombine(uint32 A, uint32 C)
Definition TypeHash.h:36
#define UE_KINDA_SMALL_NUMBER
Definition UnrealMathUtility.h:131
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ChaosArchive.h:167
Definition ImplicitObject.h:111
bool HasBoundingBox() const
Definition ImplicitObject.h:275
CHAOS_API void SerializeImp(FArchive &Ar)
Definition ImplicitObject.cpp:337
Definition MLLevelSetNeuralInference.h:14
Definition MLLevelset.h:38
const FVector3f GetTrainingGridMin() const
Definition MLLevelset.h:63
const int32 GetNumberOfActiveBones() const
Definition MLLevelset.h:59
virtual void Serialize(FArchive &Ar) override
Definition MLLevelset.h:116
virtual uint32 GetTypeHash() const override
Definition MLLevelset.h:126
const TUniformGrid< FReal, 3 > & GetGrid() const
Definition MLLevelset.h:67
FORCEINLINE void SerializeImp(FArchive &Ar)
Definition MLLevelset.h:77
const TArrayND< FReal, 3 > & GetPhiArray() const
Definition MLLevelset.h:70
virtual CHAOS_API Chaos::FImplicitObjectPtr CopyGeometry() const override
Definition MLLevelset.cpp:98
virtual CHAOS_API ~FMLLevelSet()
Definition MLLevelset.cpp:94
CHAOS_API FReal SignedDistance(const FVec3 &x) const
Definition MLLevelset.cpp:271
static FORCEINLINE constexpr EImplicitObjectType StaticType()
Definition MLLevelset.h:72
CHAOS_API void GetZeroIsosurfaceGridCellFaces(TArray< FVector3f > &Vertices, TArray< FIntVector > &Tris) const
Definition MLLevelset.cpp:167
CHAOS_API void GetInteriorCells(TArray< TVec3< int32 > > &InteriorCells, const FReal InteriorThreshold) const
Definition MLLevelset.cpp:248
virtual CHAOS_API Chaos::FImplicitObjectPtr CopyGeometryWithScale(const FVec3 &Scale) const override
Definition MLLevelset.cpp:132
const FVector3f GetTrainingGridVector(int32 Index) const
Definition MLLevelset.h:64
virtual CHAOS_API FReal PhiWithNormal(const FVec3 &x, FVec3 &Normal) const override
Definition MLLevelset.cpp:495
CHAOS_API void CreatePhiFromMLModel()
Definition MLLevelset.cpp:277
CHAOS_API void UpdateActiveBonesRelativeTransforms(TArray< FTransform > &InActiveBonesRelativeTransforms)
Definition MLLevelset.cpp:339
FMLLevelSet(const FMLLevelSet &Other)=delete
virtual const FAABB3 BoundingBox() const override
Definition MLLevelset.h:62
CHAOS_API void BatchPhiWithNormal(const TConstArrayView< Softs::FPAndInvM > PAndInvMArray, const Softs::FSolverRigidTransform3 &SolverToThis, TArray< Softs::FSolverReal > &OutBatchPhis, TArray< Softs::FSolverVec3 > &OutBatchNormals, const Chaos::Softs::FSolverReal CollisionThickness, const int32 MLLevelsetThread, const int32 BatchBegin, const int32 BatchEnd) const
Definition MLLevelset.cpp:569
virtual void Serialize(FChaosArchive &Ar) override
Definition MLLevelset.h:111
CHAOS_API void UpdateNeuralInferencesNumber(const int32 InNeuralInferencesNumber)
Definition MLLevelset.cpp:368
const TArray< FName > & GetActiveBoneNames() const
Definition MLLevelset.h:61
CHAOS_API void UpdateActiveBonesRelativeTransformsAndUpdateDebugPhi(TArray< FTransform > &InActiveBonesRelativeTransforms)
Definition MLLevelset.cpp:355
Definition ArrayND.h:194
static void SerializeAsAABB(FArchive &Ar, TAABB< T, d > &AABB)
Definition Box.h:467
Definition Transform.h:115
Definition UniformGrid.h:267
Definition Vector.h:1000
Definition Archive.h:1208
UE_FORCEINLINE_HINT bool IsLoading() const
Definition Archive.h:236
Definition ContainerAllocationPolicies.h:447
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
Definition SharedPointer.h:692
@ MLLevelSet
Definition ImplicitObjectType.h:30
FRealSingle FSolverReal
Definition PBDSoftsEvolutionFwd.h:31
Definition SkeletalMeshComponent.h:307
uint8 EImplicitObjectType
Definition ImplicitObjectType.h:41
FRealDouble FReal
Definition Real.h:22
TAABB< FReal, 3 > FAABB3
Definition ImplicitObject.h:34
U16 Index
Definition radfft.cpp:71
Definition MLLevelset.h:25
TArray< FName > ActiveBoneNames
Definition MLLevelset.h:26
TArray< FVector3f > TrainingGridAxesXYZ
Definition MLLevelset.h:33
const FVector3f TrainingGridMin
Definition MLLevelset.h:32
FIntVector DebugGridResolution
Definition MLLevelset.h:34
TArray< TArray< int32 > > ActiveBonesRotationComponents
Definition MLLevelset.h:31
const float SignedDistanceScaling
Definition MLLevelset.h:30
TArray< FMLLevelSetNNEModelData > NNEModelDataArr
Definition MLLevelset.h:27
TArray< FVector3f > ActiveBonesReferenceTranslations
Definition MLLevelset.h:29
TArray< FVector3f > ActiveBonesReferenceRotations
Definition MLLevelset.h:28
Definition MLLevelset.h:17
const FString NNEModelPath
Definition MLLevelset.h:20
const FString MLModelWeightsString
Definition MLLevelset.h:19
TArray< int32 > ModelArchitectureActivationNodeSizes
Definition MLLevelset.h:18
TObjectPtr< UNNEModelData > NNEModelData
Definition MLLevelset.h:21
Definition MLLevelSetElem.h:17
Definition ObjectPtr.h:488