UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ColoringGraph.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "CoreMinimal.h"
5
6namespace Chaos
7{
8
17 {
18 public:
19
21 {
22 return Islands.Num();
23 }
24
25 int32 GetVertexIsland(int32 VertexIndex) const
26 {
27 if (VertexIndex != INDEX_NONE)
28 {
29 return Vertices[VertexIndex].Island;
30 }
31 return INDEX_NONE;
32 }
33
34 int32 GetVertexIslandSize(int32 VertexIndex) const
35 {
36 if (VertexIndex != INDEX_NONE)
37 {
38 int32 IslandIndex = Vertices[VertexIndex].Island;
39 return Islands[IslandIndex].VertexIndices.Num();
40 }
41 return INDEX_NONE;
42 }
43
44 int32 GetVertexLevel(int32 VertexIndex) const
45 {
46 if (VertexIndex != INDEX_NONE)
47 {
48 return Vertices[VertexIndex].Level;
49 }
50 return INDEX_NONE;
51 }
52
53 void SetVertexLevel(int32 VertexIndex, int32 Level)
54 {
55 if (VertexIndex != INDEX_NONE)
56 {
57 Vertices[VertexIndex].Level = Level;
58 }
59 }
60
61 int32 GetVertexColor(int32 VertexIndex) const
62 {
63 if (VertexIndex != INDEX_NONE)
64 {
65 return Vertices[VertexIndex].Color;
66 }
67 return INDEX_NONE;
68 }
69
74
76 {
77 Edges.Reserve(InNumEdges);
78 }
79
81 {
82 return Vertices.AddDefaulted();
83 }
84
86 {
87 Vertices.SetNum(Vertices.Num() + InNumVertices, EAllowShrinking::No);
88 return Vertices.Num() - InNumVertices;
89 }
90
91 int32 AddEdge(int32 VertexIndex0, int32 VertexIndex1)
92 {
93 int32 EdgeIndex = Edges.Emplace(FEdge(VertexIndex0, VertexIndex1));
94 Vertices[VertexIndex0].EdgeIndices.Add(EdgeIndex);
95 Vertices[VertexIndex1].EdgeIndices.Add(EdgeIndex);
96 return EdgeIndex;
97 }
98
103 void Islandize();
104
109 void Levelize();
110
114 void Colorize();
115
116 private:
117 struct FVertex
118 {
119 FVertex() : Island(INDEX_NONE), Level(INDEX_NONE), Color(INDEX_NONE) {}
120
121 // @todo(ccaulfield): eliminate allocations
122 TArray<int32> EdgeIndices;
123 int32 Island;
124 int32 Level;
125 int32 Color;
126 };
127
128 struct FEdge
129 {
130 FEdge(int32 VertexIndex0, int32 VertexIndex1)
131 {
132 VertexIndices[0] = VertexIndex0;
133 VertexIndices[1] = VertexIndex1;
134 }
135
136 int32 VertexIndices[2];
137 };
138
139 struct FIsland
140 {
141 FIsland() : NextColorIndex(0) {}
142
143 // @todo(ccaulfield): eliminate allocations
144 TArray<int32> VertexIndices;
145 int32 NextColorIndex;
146 };
147
148 // Move vertices from one island into the other
149 void MergeIslands(int32 IslandIndex0, int32 IslandIndex1);
150
151 // Move all non-empty islands to lowest island indices and clip array
152 void PackIslands();
153
154 // Set the color on the vertex so it is not the same as any adjacent vertices
155 void AssignColor(int32 VertexIndex);
156
157 TArray<FVertex> Vertices;
158 TArray<FEdge> Edges;
159 TArray<FIsland> Islands;
160 };
161
162}
@ INDEX_NONE
Definition CoreMiscDefines.h:150
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
Definition ColoringGraph.h:17
int32 GetVertexLevel(int32 VertexIndex) const
Definition ColoringGraph.h:44
int32 GetVertexIsland(int32 VertexIndex) const
Definition ColoringGraph.h:25
void ReserveEdges(int32 InNumEdges)
Definition ColoringGraph.h:75
void Islandize()
Definition ColoringGraph.cpp:8
int32 GetVertexIslandSize(int32 VertexIndex) const
Definition ColoringGraph.h:34
int32 GetNumIslands() const
Definition ColoringGraph.h:20
void SetVertexLevel(int32 VertexIndex, int32 Level)
Definition ColoringGraph.h:53
int32 AddVertices(int32 InNumVertices)
Definition ColoringGraph.h:85
void Colorize()
Definition ColoringGraph.cpp:69
int32 GetVertexColor(int32 VertexIndex) const
Definition ColoringGraph.h:61
int32 AddEdge(int32 VertexIndex0, int32 VertexIndex1)
Definition ColoringGraph.h:91
int32 AddVertex()
Definition ColoringGraph.h:80
void Levelize()
Definition ColoringGraph.cpp:34
void ReserveVertices(int32 InNumVertices)
Definition ColoringGraph.h:70
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
SizeType AddDefaulted()
Definition Array.h:2795
UE_FORCEINLINE_HINT SizeType Emplace(ArgsType &&... Args)
Definition Array.h:2561
void SetNum(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2308
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
UE_FORCEINLINE_HINT void Reserve(SizeType Number)
Definition Array.h:3016
Definition SkeletalMeshComponent.h:307
Definition Edge.h:12
Definition BlendSpaceHelpers.h:20