UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
DynamicGraph2.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6
7#include "BoxTypes.h"
8#include "DynamicGraph.h"
9#include "SegmentTypes.h"
10#include "Util/DynamicVector.h"
11#include "Util/IndexUtil.h"
12#include "Util/IteratorUtil.h"
13#include "Util/RefCountVector.h"
14#include "Util/SmallListSet.h"
15#include "VectorTypes.h"
16#include "VectorUtil.h"
17
18namespace UE
19{
20namespace Geometry
21{
22
23using namespace UE::Math;
24
25template <typename T>
27{
28 TAxisAlignedBox2<T> cached_bounds;
29 int cached_bounds_timestamp = -1;
30
31 TDynamicVectorN<T, 2> vertices;
32
33public:
35 {
37 }
38
39 TVector2<T> GetVertex(int VID) const
40 {
41 return vertices_refcount.IsValid(VID) ? vertices.AsVector2(VID) : InvalidVertex();
42 }
43
45 {
46 check(VectorUtil::IsFinite(VNewPos)); // this will really catch a lot of bugs...
48 {
49 vertices.SetVector2(VID, VNewPos);
50 updateTimeStamp(true);
51 }
52 }
53
56 {
58 {
59 A = vertices.AsVector2(edges[EID].A);
60 B = vertices.AsVector2(edges[EID].B);
61 return true;
62 }
63 return false;
64 }
65
67 {
68 checkf(edges_refcount.IsValid(EID), TEXT("FDynamicGraph2.GetEdgeSegment: invalid segment with id %d"), EID);
69 const FEdge& e = edges[EID];
70 return TSegment2<T>(
71 vertices.AsVector2(e.A),
72 vertices.AsVector2(e.B));
73 }
74
76 {
77 checkf(edges_refcount.IsValid(EID), TEXT("FDynamicGraph2.GetEdgeCenter: invalid segment with id %d"), EID);
78 const FEdge& e = edges[EID];
79 return 0.5 * (vertices.AsVector2(e.A) + vertices.AsVector2(e.B));
80 }
81
83 {
84 int vid = append_vertex_internal();
85 vertices.InsertAt({{V.X, V.Y}}, vid);
86 return vid;
87 }
88
89 //void AppendPolygon(Polygon2d poly, int gid = -1) {
90 // int first = -1;
91 // int prev = -1;
92 // int N = poly.VertexCount;
93 // for (int i = 0; i < N; ++i) {
94 // int cur = AppendVertex(poly[i]);
95 // if (prev == -1)
96 // first = cur;
97 // else
98 // AppendEdge(prev, cur, gid);
99 // prev = cur;
100 // }
101 // AppendEdge(prev, first, gid);
102 //}
103 //void AppendPolygon(GeneralPolygon2d poly, int gid = -1)
104 //{
105 // AppendPolygon(poly.Outer, gid);
106 // foreach(var hole in poly.Holes)
107 // AppendPolygon(hole, gid);
108 //}
109
110 //void AppendPolyline(PolyLine2d poly, int gid = -1)
111 //{
112 // int prev = -1;
113 // int N = poly.VertexCount;
114 // for (int i = 0; i < N; ++i) {
115 // int cur = AppendVertex(poly[i]);
116 // if (i > 0)
117 // AppendEdge(prev, cur, gid);
118 // prev = cur;
119 // }
120 //}
121
122 /*
123 void AppendGraph(FDynamicGraph2 graph, int gid = -1)
124 {
125 int[] mapV = new int[graph.MaxVertexID];
126 foreach(int vid in graph.VertexIndices()) {
127 mapV[vid] = this.AppendVertex(graph.GetVertex(vid));
128 }
129 foreach(int eid in graph.EdgeIndices()) {
130 FIndex2i ev = graph.GetEdgeV(eid);
131 int use_gid = (gid == -1) ? graph.GetEdgeGroup(eid) : gid;
132 this.AppendEdge(mapV[ev.A], mapV[ev.B], use_gid);
133 }
134 }
135
136*/
137
140 {
142 [&, this](int vid) {
143 return vertices.template AsVector2<T>(vid);
144 });
145 }
146
150 bool SortedVtxEdges(int VID, TArray<int>& Sorted) const
151 {
152 if (vertices_refcount.IsValid(VID) == false)
153 return false;
154 Sorted.Reserve(vertex_edges.GetCount(VID));
155 for (int EID : vertex_edges.Values(VID))
156 {
157 Sorted.Add(EID);
158 }
159 TVector2<T> V = vertices.AsVector2(VID);
160 Algo::SortBy(Sorted, [&](int EID) {
161 int NbrVID = edge_other_v(EID, VID);
162 TVector2<T> D = vertices.AsVector2(NbrVID) - V;
163 return TMathUtil<T>::Atan2Positive(D.Y, D.X);
164 });
165
166 return true;
167 }
168
169 // compute vertex bounding box
171 {
173 for (const TVector2<T>& V : Vertices())
174 {
175 AABB.Contain(V);
176 }
177 return AABB;
178 }
179
182 {
183 if (cached_bounds_timestamp != Timestamp())
184 {
185 cached_bounds = GetBounds();
186 cached_bounds_timestamp = Timestamp();
187 }
188 return cached_bounds;
189 }
190
196 double OpeningAngle(int VID, double InvalidValue = TNumericLimits<T>::Max()) const
197 {
198 if (vertices_refcount.IsValid(VID) == false)
199 {
200 return InvalidValue;
201 }
202 if (vertex_edges.GetCount(VID) != 2)
203 {
204 return InvalidValue;
205 }
207
208 int nbra = edge_other_v(*ValueIterate, VID);
209 int nbrb = edge_other_v(*++ValueIterate, VID);
210
211 TVector2<T> V = vertices.AsVector2(VID);
212 TVector2<T> A = vertices.AsVector2(nbra);
213 TVector2<T> B = vertices.AsVector2(nbrb);
214 A -= V;
215 if (Normalize(A) == 0)
216 {
217 return InvalidValue;
218 }
219 B -= V;
220 if (Normalize(B) == 0)
221 {
222 return InvalidValue;
223 }
224 return AngleD(A, B);
225 }
226
227protected:
228 // internal used in SplitEdge
229 virtual int append_new_split_vertex(int A, int B) override
230 {
231 TVector2<T> vNew = 0.5 * (GetVertex(A) + GetVertex(B));
232 int f = AppendVertex(vNew);
233 return f;
234 }
235
236 virtual void subclass_validity_checks(TFunction<void(bool)> CheckOrFailF) const override
237 {
238 for (int VID : VertexIndices())
239 {
240 TVector2<T> V = GetVertex(VID);
242 }
243 }
244};
245
247
248
249} // end namespace UE::Geometry
250} // end namespace UE
#define check(expr)
Definition AssertionMacros.h:314
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition Array.h:670
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 AndroidPlatformMisc.h:14
static RealType Atan2Positive(const RealType Y, const RealType X)
Definition MathUtil.h:429
Definition DynamicGraph2.h:27
value_iteration< TVector2< T > > Vertices() const
Definition DynamicGraph2.h:139
double OpeningAngle(int VID, double InvalidValue=TNumericLimits< T >::Max()) const
Definition DynamicGraph2.h:196
bool GetEdgeV(int EID, UE::Math::TVector2< T > &A, UE::Math::TVector2< T > &B) const
Definition DynamicGraph2.h:55
virtual void subclass_validity_checks(TFunction< void(bool)> CheckOrFailF) const override
Definition DynamicGraph2.h:236
TSegment2< T > GetEdgeSegment(int EID) const
Definition DynamicGraph2.h:66
TVector2< T > GetEdgeCenter(int EID) const
Definition DynamicGraph2.h:75
TAxisAlignedBox2< T > CachedBounds()
cached bounding box, lazily re-computed on access if mesh has changed
Definition DynamicGraph2.h:181
bool SortedVtxEdges(int VID, TArray< int > &Sorted) const
Definition DynamicGraph2.h:150
int AppendVertex(TVector2< T > V)
Definition DynamicGraph2.h:82
void SetVertex(int VID, TVector2< T > VNewPos)
Definition DynamicGraph2.h:44
virtual int append_new_split_vertex(int A, int B) override
Definition DynamicGraph2.h:229
TVector2< T > GetVertex(int VID) const
Definition DynamicGraph2.h:39
FAxisAlignedBox2d GetBounds() const
Definition DynamicGraph2.h:170
static TVector2< T > InvalidVertex()
Definition DynamicGraph2.h:34
Definition DynamicGraph.h:25
void updateTimeStamp(bool bShapeChange)
Definition DynamicGraph.h:68
int edge_other_v(int EID, int VID) const
Definition DynamicGraph.h:559
FRefCountVector edges_refcount
Definition DynamicGraph.h:49
FIndex2i GetEdgeV(int EID) const
Definition DynamicGraph.h:152
int append_vertex_internal()
Definition DynamicGraph.h:184
TDynamicVector< FEdge > edges
Definition DynamicGraph.h:50
int Timestamp() const
Definition DynamicGraph.h:78
vertex_iterator VertexIndices() const
Definition DynamicGraph.h:275
FSmallListSet vertex_edges
Definition DynamicGraph.h:47
FRefCountVector vertices_refcount
Definition DynamicGraph.h:45
MappedEnumerable< ToType > MappedIndices(TFunction< ToType(int)> MapFunc) const
Definition RefCountVector.h:497
bool IsValid(int Index) const
Definition RefCountVector.h:66
FSmallListSet::ValueIterator begin() const
Definition SmallListSet.h:564
Definition SmallListSet.h:512
ValueEnumerable Values(int32 ListIndex) const
Definition SmallListSet.h:571
int32 GetCount(int32 ListIndex) const
Definition SmallListSet.h:155
Definition DynamicVector.h:427
TVector2< Type > AsVector2(unsigned int TopIndex) const
Definition DynamicVector.h:522
void SetVector2(unsigned int TopIndex, const TVector2< Type > &V)
Definition DynamicVector.h:507
void InsertAt(const ElementVectorN &AddData, unsigned int Index)
Definition DynamicVector.h:491
UE_REWRITE void SortBy(RangeType &&Range, ProjectionType Proj)
Definition Sort.h:40
bool IsFinite(const TVector2< RealType > &V)
Definition VectorUtil.h:42
T AngleD(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:92
FDynamicGraph2< double > FDynamicGraph2d
Definition DynamicGraph2.h:246
T Normalize(UE::Math::TVector2< T > &Vector, const T Epsilon=0)
Definition VectorTypes.h:46
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
Definition NumericLimits.h:41
Definition DynamicGraph.h:31
int A
Definition DynamicGraph.h:32
int B
Definition DynamicGraph.h:32
Definition BoxTypes.h:637
void Contain(const TVector2< RealType > &V)
Definition BoxTypes.h:738
Definition SegmentTypes.h:23
Definition Vector2D.h:38
T Y
Definition Vector2D.h:52
T X
Definition Vector2D.h:49