UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ConvexFlattenedArrayStructureData.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Chaos/Core.h"
6#include "ChaosArchive.h"
7#include "ChaosCheck.h"
8#include "ChaosLog.h"
11
12//
13//
14// LEGACY CODE
15// Kept around for serialization of old data. Most of the API has been removed,
16// except what is needed to convert to the new format.
17// See ConvexHalfEdgeStructureData.h for the replacement.
18//
19//
20
21namespace Chaos
22{
23 namespace Legacy
24 {
26
27 // Base class for TConvexFlattenedArrayStructureData.
28 // NOTE: Deliberately no-virtual destructor so it should never be deleted from a
29 // base class pointer.
36
37 // A container for the convex structure data arrays.
38 // This is templated on the index type required, which needs to
39 // be large enough to hold the max of the number of vertices or
40 // planes on the convex.
41 //
42 // T_INDEX: the type used to index into the convex vertices
43 // and planes array (in the outer convex). Must be
44 // able to contain max(NumPlanes, NumVerts).
45 //
46 // T_OFFSETINDEX: the type used to index the flattened array of
47 // indices. Must be able to contain
48 // Max(NumPlanes*AverageVertsPerPlane)
49 //
50 template<typename T_INDEX, typename T_OFFSETINDEX>
52 {
53 public:
54 // Everything public - not used outside of legacy loader and unit tests
55
56 using FIndex = T_INDEX;
58
60 {
61 return VertexPlanesOffsetCount.Num();
62 }
63
65 {
66 return PlaneVerticesOffsetCount.Num();
67 }
68
69 // The number of planes that use the specified vertex
70 int32 NumVertexPlanes(int32 VertexIndex) const
71 {
72 return VertexPlanesOffsetCount[VertexIndex].Value;
73 }
74
75 // Get the plane index (in the outer convex container) of one of the planes that uses the specified vertex
83
84 // The number of vertices that make up the corners of the specified face
85 int32 NumPlaneVertices(int32 PlaneIndex) const
86 {
87 return PlaneVerticesOffsetCount[PlaneIndex].Value;
88 }
89
90 // Get the vertex index (in the outer convex container) of one of the vertices making up the corners of the specified face
98
100 {
101 // We flatten the ragged arrays into a single array, and store a seperate
102 // arrray of [index,count] tuples to reproduce the ragged arrays.
103 Reset();
104
105 // Generate the ragged array [offset,count] tuples
106 PlaneVerticesOffsetCount.SetNumZeroed(InPlaneVertices.Num());
107 VertexPlanesOffsetCount.SetNumZeroed(NumVerts);
108
109 // Count the number of planes for each vertex (store it in the tuple)
110 int FlatArrayIndexCount = 0;
111 for (int32 PlaneIndex = 0; PlaneIndex < InPlaneVertices.Num(); ++PlaneIndex)
112 {
113 FlatArrayIndexCount += InPlaneVertices[PlaneIndex].Num();
114
116 {
117 const int32 VertexIndex = InPlaneVertices[PlaneIndex][PlaneVertexIndex];
118 VertexPlanesOffsetCount[VertexIndex].Value++;
119 }
120 }
121
122 // Initialize the flattened arrary offsets and reset the count (we re-increment it when copying the data below)
124 for (int32 VertexIndex = 0; VertexIndex < NumVerts; ++VertexIndex)
125 {
128 VertexPlanesOffsetCount[VertexIndex].Value = 0;
129 }
130
131 // Allocate space for the flattened arrays
134
135 // Copy the indices into the flattened arrays
137 for (int32 PlaneIndex = 0; PlaneIndex < InPlaneVertices.Num(); ++PlaneIndex)
138 {
140 PlaneVerticesOffsetCount[PlaneIndex].Value = InPlaneVertices[PlaneIndex].Num();
141 PlaneVerticesArrayStart += InPlaneVertices[PlaneIndex].Num();
142
144 {
145 const int32 VertexIndex = InPlaneVertices[PlaneIndex][PlaneVertexIndex];
146
149
150 const int32 VertexPlaneFlatArrayIndex = VertexPlanesOffsetCount[VertexIndex].Key + VertexPlanesOffsetCount[VertexIndex].Value;
151 VertexPlanesOffsetCount[VertexIndex].Value++;
153 }
154 }
155 }
156
157 void Reset()
158 {
163 }
164
166 {
169 Ar << PlaneVertices;
170 Ar << VertexPlanes;
171 }
172
178
179
180 // Array of [offset, count] for each plane that gives the set of indices in the PlaneVertices flattened array
182
183 // Array of [offset, count] for each vertex that gives the set of indices in the VertexPlanes flattened array
185
186 // A flattened ragged array. For each plane: the set of vertex indices that form the corners of the face in counter-clockwise order
188
189 // A flattened ragged array. For each vertex: the set of plane indices that use the vertex
191 };
192
195
196 //
197 // A utility for loading old convex structure data
198 //
200 {
201 public:
202 // Load data from an asset saved before we had a proper half-edge data structure
204 {
206 bool bUseVariableSizeStructureDataFN = Ar.CustomVer(FFortniteMainBranchObjectVersion::GUID) >= FFortniteMainBranchObjectVersion::ChaosConvexVariableStructureDataAndVerticesArray;
208
210 {
211 LoadFixedSizeRaggedArrays(Ar, OutPlaneVertices, OutNumVertices);
212 }
213 else
214 {
215 LoadVariableSizePackedArrays(Ar, OutPlaneVertices, OutNumVertices);
216 }
217 }
218
219 private:
220 enum class EIndexType : int8
221 {
222 None,
223 S32,
224 U8,
225 };
226
227 // Orginal structure used 32bit indices regardless of max index, and they were store in ragged TArray<TArray<int32>> of vertex indices per plane
228 static void LoadFixedSizeRaggedArrays(FArchive& Ar, TArray<TArray<int32>>& OutPlaneVertices, int32& OutNumVertices)
229 {
232 Ar << OldPlaneVertices;
233 Ar << OldVertexPlanes;
234
237 }
238
239 // The second data structure packed the indices into flat arrays, and used an 8bit index if possible (and 32bit otherwise)
240 static void LoadVariableSizePackedArrays(FArchive& Ar, TArray<TArray<int32>>& OutPlaneVertices, int32& OutNumVertices)
241 {
242 EIndexType OldIndexType;
245
246 Ar << OldIndexType;
247
248 // Read the old structure and pull out the data we need to generate the new one (the output params)
249 if (OldIndexType == EIndexType::S32)
250 {
252 Ar << OldData;
254 }
255 else if (OldIndexType == EIndexType::U8)
256 {
258 Ar << OldData;
260 }
261
264 }
265
266 // Extract ragged arrays of vertices per plane from the flattened array structure
267 // For use by LoadVariableSizePackedArrays to avoid code dupe based on index size
268 template<typename T_OLDCONTAINER>
270 {
271 OldPlaneVertices.SetNum(OldData.NumPlanes());
272 for (int32 PlaneIndex = 0; PlaneIndex < OldData.NumPlanes(); ++PlaneIndex)
273 {
274 OldPlaneVertices[PlaneIndex].SetNum(OldData.NumPlaneVertices(PlaneIndex));
275 for (int32 PlaneVertexIndex = 0; PlaneVertexIndex < OldData.NumPlaneVertices(PlaneIndex); ++PlaneVertexIndex)
276 {
277 OldPlaneVertices[PlaneIndex][PlaneVertexIndex] = (int32)OldData.GetPlaneVertex(PlaneIndex, PlaneVertexIndex);
278 }
279 }
280 OldNumVertices = OldData.NumVertices();
281 }
282 };
283 }
284
285}
#define FORCEINLINE
Definition AndroidPlatform.h:140
#define check(expr)
Definition AssertionMacros.h:314
RAD_U8 U8
Definition egttypes.h:481
RAD_S32 S32
Definition egttypes.h:496
FPlatformTypes::int8 int8
An 8-bit signed integer.
Definition Platform.h:1121
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
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition ConvexFlattenedArrayStructureData.h:31
FConvexFlattenedArrayStructureData()
Definition ConvexFlattenedArrayStructureData.h:33
~FConvexFlattenedArrayStructureData()
Definition ConvexFlattenedArrayStructureData.h:34
Definition ConvexFlattenedArrayStructureData.h:200
static void Load(FArchive &Ar, TArray< TArray< int32 > > &OutPlaneVertices, int32 &OutNumVertices)
Definition ConvexFlattenedArrayStructureData.h:203
Definition ConvexFlattenedArrayStructureData.h:52
int32 NumPlaneVertices(int32 PlaneIndex) const
Definition ConvexFlattenedArrayStructureData.h:85
T_OFFSETINDEX FOffsetIndex
Definition ConvexFlattenedArrayStructureData.h:57
FORCEINLINE int32 NumVertices() const
Definition ConvexFlattenedArrayStructureData.h:59
void Reset()
Definition ConvexFlattenedArrayStructureData.h:157
TArray< TPair< FOffsetIndex, FIndex > > VertexPlanesOffsetCount
Definition ConvexFlattenedArrayStructureData.h:184
T_INDEX FIndex
Definition ConvexFlattenedArrayStructureData.h:56
TArray< FIndex > VertexPlanes
Definition ConvexFlattenedArrayStructureData.h:190
TArray< TPair< FOffsetIndex, FIndex > > PlaneVerticesOffsetCount
Definition ConvexFlattenedArrayStructureData.h:181
void SetPlaneVertices(const TArray< TArray< int32 > > &InPlaneVertices, int32 NumVerts)
Definition ConvexFlattenedArrayStructureData.h:99
int32 GetPlaneVertex(int32 PlaneIndex, int32 PlaneVertexIndex) const
Definition ConvexFlattenedArrayStructureData.h:91
int32 NumPlanes() const
Definition ConvexFlattenedArrayStructureData.h:64
int32 NumVertexPlanes(int32 VertexIndex) const
Definition ConvexFlattenedArrayStructureData.h:70
friend FArchive & operator<<(FArchive &Ar, TConvexFlattenedArrayStructureData< T_INDEX, T_OFFSETINDEX > &Value)
Definition ConvexFlattenedArrayStructureData.h:173
void Serialize(FArchive &Ar)
Definition ConvexFlattenedArrayStructureData.h:165
int32 GetVertexPlane(int32 VertexIndex, int32 VertexPlaneIndex) const
Definition ConvexFlattenedArrayStructureData.h:76
TArray< FIndex > PlaneVertices
Definition ConvexFlattenedArrayStructureData.h:187
Definition Archive.h:1208
virtual void Serialize(void *V, int64 Length)
Definition Archive.h:1689
CORE_API int32 CustomVer(const struct FGuid &Key) const
Definition Archive.cpp:602
Definition Array.h:670
void SetNumZeroed(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2340
void Reset(SizeType NewSize=0)
Definition Array.h:2246
TConvexFlattenedArrayStructureData< int32, int32 > FConvexFlattenedArrayStructureDataS32
Definition ConvexFlattenedArrayStructureData.h:193
TConvexFlattenedArrayStructureData< uint8, uint16 > FConvexFlattenedArrayStructureDataU8
Definition ConvexFlattenedArrayStructureData.h:194
Definition SkeletalMeshComponent.h:307
CORE_API static const FGuid GUID
Definition FortniteMainBranchObjectVersion.h:21
@ VariableConvexStructureData
Definition PhysicsObjectVersion.h:28
CORE_API static const FGuid GUID
Definition PhysicsObjectVersion.h:78