RuntimeMeshTessellationUtilities.h
Go to the documentation of this file.
1 // Copyright 2016-2018 Chris Conway (Koderz). All Rights Reserved.
2 
3 #pragma once
4 
5 #include "CoreMinimal.h"
6 #include "RuntimeMeshBuilder.h"
7 
8 
13 {
14 public:
15  static void CalculateTessellationIndices(int32 NumVertices, int32 NumIndices,
16  TFunction<FVector(int32)> PositionAccessor, TFunction<FVector2D(int32)> UVAccessor, TFunction<int32(int32)> IndexAccessor,
17  TFunction<void(int32)> OutIndicesSizeSetter, TFunction<int32()> OutIndicesSizeGetter, TFunction<void(int32, int32)> OutIndicesWriter, TFunction<int32(int32)> OutIndicesReader);
18 
19 
20 
21 
22 private:
23  struct Vertex
24  {
25  FVector Position;
26  FVector2D TexCoord;
27 
28  Vertex() { }
29  Vertex(const FVector& InPosition, const FVector2D& InTexCoord)
30  : Position(InPosition), TexCoord(InTexCoord)
31  { }
32 
33  FORCEINLINE bool operator==(const Vertex& Other) const
34  {
35  return Position == Other.Position;
36  }
37  FORCEINLINE bool operator<(const Vertex& Other) const
38  {
39  return Position.X < Other.Position.X
40  || Position.Y < Other.Position.Y
41  || Position.Z < Other.Position.Z;
42  }
43  };
44 
45  static FORCEINLINE uint32 HashValue(const FVector& Vec)
46  {
47  return 31337 * GetTypeHash(Vec.X) + 13 * GetTypeHash(Vec.Y) + 3 * GetTypeHash(Vec.Z);
48  }
49 
50  static FORCEINLINE uint32 HashValue(const Vertex& Vert)
51  {
52  return HashValue(Vert.Position);
53  }
54 
55  struct Edge
56  {
57  private:
58  uint32 IndexFrom;
59  uint32 IndexTo;
60 
63 
64  uint32 CachedHash;
65 
66  public:
67  Edge() : CachedHash(0) { }
68  Edge(uint32 InIndexFrom, uint32 InIndexTo, const Vertex& InVertexFrom, const Vertex& InVertexTo)
69  : IndexFrom(InIndexFrom), IndexTo(InIndexTo), VertexFrom(InVertexFrom), VertexTo(InVertexTo)
70  {
71  // Hash should only consider position, not index.
72  // We want values with different indices to compare true.
73  CachedHash = 7 * HashValue(VertexFrom) + 2 * HashValue(VertexTo);
74  }
75 
76  Vertex GetVertex(uint32 I) const
77  {
78  switch (I)
79  {
80  case 0:
81  return VertexFrom;
82  case 1:
83  return VertexTo;
84  default:
85  checkNoEntry();
86  return Vertex();
87  }
88  }
89 
90  uint32 GetIndex(uint32 I) const
91  {
92  switch (I)
93  {
94  case 0:
95  return IndexFrom;
96  case 1:
97  return IndexTo;
98  default:
99  checkNoEntry();
100  return 0;
101  }
102  }
103 
104  Edge GetReverse() const
105  {
106  return Edge(IndexTo, IndexFrom, VertexTo, VertexFrom);
107  }
108 
109  FORCEINLINE bool operator<(const Edge& Other) const
110  {
111  // Quick out, otherwise we have to compare vertices
112  if (IndexFrom == Other.IndexFrom && IndexTo == Other.IndexTo)
113  {
114  return false;
115  }
116 
117  return VertexFrom < Other.VertexFrom || VertexTo < Other.VertexTo;
118  }
119 
120  FORCEINLINE bool operator==(const Edge& Other) const
121  {
122  return (IndexFrom == Other.IndexFrom && IndexTo == Other.IndexTo) ||
123  (VertexFrom == Other.VertexFrom && VertexTo == Other.VertexTo);
124  }
125 
126  friend FORCEINLINE uint32 GetTypeHash(const Edge& E)
127  {
128  return E.CachedHash;
129  }
130  };
131 
132  struct Corner
133  {
134  uint32 Index;
135  FVector2D TexCoord;
136 
137  Corner() : Index(0) { }
138  Corner(uint32 InIndex, FVector2D InTexCoord)
139  : Index(InIndex), TexCoord(InTexCoord)
140  { }
141  };
142 
143  class Triangle
144  {
148 
149  public:
150  Triangle(uint32 Index0, uint32 Index1, uint32 Index2, const Vertex& Vertex0, const Vertex& Vertex1, const Vertex& Vertex2)
151  : Edge0(Index0, Index1, Vertex0, Vertex1)
152  , Edge1(Index1, Index2, Vertex1, Vertex2)
153  , Edge2(Index2, Index0, Vertex2, Vertex0)
154  { }
155 
156  FORCEINLINE bool operator<(const Triangle& Other) const
157  {
158  return Edge0 < Other.Edge0 || Edge1 < Other.Edge1 || Edge2 < Other.Edge2;
159  }
160 
161  FORCEINLINE const Edge& GetEdge(uint32 I)
162  {
163  return ((Edge*)&Edge0)[I];
164  }
165  FORCEINLINE uint32 GetIndex(uint32 I)
166  {
167  return GetEdge(I).GetIndex(0);
168  }
169 
170  };
171  using EdgeDictionary = TMap<Edge, Edge>;
172  using PositionDictionary = TMap<FVector, Corner>;
173 
174  static void AddIfLeastUV(PositionDictionary& PosDict, const Vertex& Vert, uint32 Index);
175 
176  static void ReplacePlaceholderIndices(int32 NumVertices, int32 NumIndices,
177  TFunction<FVector(int32)> PositionAccessor, TFunction<FVector2D(int32)> UVAccessor, TFunction<int32(int32)> IndexAccessor,
178  TFunction<void(int32)> OutIndicesSizeSetter, TFunction<int32()> OutIndicesSizeGetter, TFunction<void(int32, int32)> OutIndicesWriter, TFunction<int32(int32)> OutIndicesReader,
179  EdgeDictionary& EdgeDict, PositionDictionary& PosDict);
180 
181  static void ExpandIB(int32 NumVertices, int32 NumIndices,
182  TFunction<FVector(int32)> PositionAccessor, TFunction<FVector2D(int32)> UVAccessor, TFunction<int32(int32)> IndexAccessor,
183  TFunction<void(int32)> OutIndicesSizeSetter, TFunction<int32()> OutIndicesSizeGetter, TFunction<void(int32, int32)> OutIndicesWriter, TFunction<int32(int32)> OutIndicesReader,
184  EdgeDictionary& OutEdgeDict, PositionDictionary& OutPosDict);
185 };
static void CalculateTessellationIndices(int32 NumVertices, int32 NumIndices, TFunction< FVector(int32)> PositionAccessor, TFunction< FVector2D(int32)> UVAccessor, TFunction< int32(int32)> IndexAccessor, TFunction< void(int32)> OutIndicesSizeSetter, TFunction< int32()> OutIndicesSizeGetter, TFunction< void(int32, int32)> OutIndicesWriter, TFunction< int32(int32)> OutIndicesReader)
static void ReplacePlaceholderIndices(int32 NumVertices, int32 NumIndices, TFunction< FVector(int32)> PositionAccessor, TFunction< FVector2D(int32)> UVAccessor, TFunction< int32(int32)> IndexAccessor, TFunction< void(int32)> OutIndicesSizeSetter, TFunction< int32()> OutIndicesSizeGetter, TFunction< void(int32, int32)> OutIndicesWriter, TFunction< int32(int32)> OutIndicesReader, EdgeDictionary &EdgeDict, PositionDictionary &PosDict)
Vertex(const FVector &InPosition, const FVector2D &InTexCoord)
static FORCEINLINE uint32 HashValue(const FVector &Vec)
FORCEINLINE bool operator==(const Vertex &Other) const
friend FORCEINLINE uint32 GetTypeHash(const Edge &E)
FORCEINLINE bool operator<(const Vertex &Other) const
static FORCEINLINE uint32 HashValue(const Vertex &Vert)
Triangle(uint32 Index0, uint32 Index1, uint32 Index2, const Vertex &Vertex0, const Vertex &Vertex1, const Vertex &Vertex2)
TMap< FVector, Corner > PositionDictionary
Corner(uint32 InIndex, FVector2D InTexCoord)
FORCEINLINE bool operator<(const Triangle &Other) const
Edge(uint32 InIndexFrom, uint32 InIndexTo, const Vertex &InVertexFrom, const Vertex &InVertexTo)
FORCEINLINE bool operator<(const Edge &Other) const
FORCEINLINE bool operator==(const Edge &Other) const
static void AddIfLeastUV(PositionDictionary &PosDict, const Vertex &Vert, uint32 Index)
static void ExpandIB(int32 NumVertices, int32 NumIndices, TFunction< FVector(int32)> PositionAccessor, TFunction< FVector2D(int32)> UVAccessor, TFunction< int32(int32)> IndexAccessor, TFunction< void(int32)> OutIndicesSizeSetter, TFunction< int32()> OutIndicesSizeGetter, TFunction< void(int32, int32)> OutIndicesWriter, TFunction< int32(int32)> OutIndicesReader, EdgeDictionary &OutEdgeDict, PositionDictionary &OutPosDict)
FORCEINLINE const Edge & GetEdge(uint32 I)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:41