RuntimeMeshTessellationUtilities.cpp
Go to the documentation of this file.
1 // Copyright 2016-2018 Chris Conway (Koderz). All Rights Reserved.
2 
5 
6 const uint32 EdgesPerTriangle = 3;
7 const uint32 IndicesPerTriangle = 3;
8 const uint32 VerticesPerTriangle = 3;
9 const uint32 DuplicateIndexCount = 3;
10 
12 
13 DECLARE_CYCLE_STAT(TEXT("RML - Calculate Tessellation Indices"), STAT_RuntimeMeshLibrary_CalculateTessellationIndices, STATGROUP_RuntimeMesh);
14 
16 {
17  auto* Pos = PosDict.Find(Vert.Position);
18  if (Pos == nullptr)
19  {
20  PosDict.Add(Vert.Position, Corner(Index, Vert.TexCoord));
21  }
22  else if (Vert.TexCoord < Pos->TexCoord)
23  {
24  PosDict[Vert.Position] = Corner(Index, Vert.TexCoord);
25  }
26 }
27 
28 
29 void FTessellationUtilities::CalculateTessellationIndices(int32 NumVertices, int32 NumIndices,
30  TFunction<FVector(int32)> PositionAccessor, TFunction<FVector2D(int32)> UVAccessor, TFunction<int32(int32)> IndexAccessor,
31  TFunction<void(int32)> OutIndicesSizeSetter, TFunction<int32()> OutIndicesSizeGetter, TFunction<void(int32, int32)> OutIndicesWriter, TFunction<int32(int32)> OutIndicesReader)
32 {
33  SCOPE_CYCLE_COUNTER(STAT_RuntimeMeshLibrary_CalculateTessellationIndices);
34 
35  EdgeDictionary EdgeDict;
36  EdgeDict.Reserve(NumIndices);
37  PositionDictionary PosDict;
38  PosDict.Reserve(NumIndices);
39 
40  OutIndicesSizeSetter(PnAenDomCorner_IndicesPerPatch * (NumIndices / IndicesPerTriangle));
41 
42  ExpandIB(NumVertices, NumIndices, PositionAccessor, UVAccessor, IndexAccessor, OutIndicesSizeSetter, OutIndicesSizeGetter, OutIndicesWriter, OutIndicesReader, EdgeDict, PosDict);
43 
44  ReplacePlaceholderIndices(NumVertices, NumIndices, PositionAccessor, UVAccessor, IndexAccessor, OutIndicesSizeSetter, OutIndicesSizeGetter, OutIndicesWriter, OutIndicesReader, EdgeDict, PosDict);
45 }
46 
47 
48 void FTessellationUtilities::ExpandIB(int32 NumVertices, int32 NumIndices,
49  TFunction<FVector(int32)> PositionAccessor, TFunction<FVector2D(int32)> UVAccessor, TFunction<int32(int32)> IndexAccessor,
50  TFunction<void(int32)> OutIndicesSizeSetter, TFunction<int32()> OutIndicesSizeGetter, TFunction<void(int32, int32)> OutIndicesWriter, TFunction<int32(int32)> OutIndicesReader,
51  EdgeDictionary& OutEdgeDict, PositionDictionary& OutPosDict)
52 {
53  const uint32 TriangleCount = NumIndices / IndicesPerTriangle;
54 
55  for (uint32 U = 0; U < TriangleCount; U++)
56  {
57  const uint32 StartInIndex = U * IndicesPerTriangle;
58  const uint32 StartOutIndex = U * PnAenDomCorner_IndicesPerPatch;
59 
60  const uint32 Index0 = IndexAccessor(U * 3 + 0);
61  const uint32 Index1 = IndexAccessor(U * 3 + 1);
62  const uint32 Index2 = IndexAccessor(U * 3 + 2);
63 
64  const Vertex Vertex0(PositionAccessor(Index0), UVAccessor(Index0));
65  const Vertex Vertex1(PositionAccessor(Index1), UVAccessor(Index1));
66  const Vertex Vertex2(PositionAccessor(Index2), UVAccessor(Index2));
67 
68  Triangle Tri(Index0, Index1, Index2, Vertex0, Vertex1, Vertex2);
69 
70  if ((uint32)OutIndicesSizeGetter() <= (StartOutIndex + PnAenDomCorner_IndicesPerPatch))
71  {
72  OutIndicesSizeSetter((StartOutIndex + PnAenDomCorner_IndicesPerPatch) + 1);
73  }
74 
75  OutIndicesWriter(StartOutIndex + 0, Tri.GetIndex(0));
76  OutIndicesWriter(StartOutIndex + 1, Tri.GetIndex(1));
77  OutIndicesWriter(StartOutIndex + 2, Tri.GetIndex(2));
78 
79  OutIndicesWriter(StartOutIndex + 3, Tri.GetIndex(0));
80  OutIndicesWriter(StartOutIndex + 4, Tri.GetIndex(1));
81 
82  OutIndicesWriter(StartOutIndex + 5, Tri.GetIndex(1));
83  OutIndicesWriter(StartOutIndex + 6, Tri.GetIndex(2));
84 
85  OutIndicesWriter(StartOutIndex + 7, Tri.GetIndex(2));
86  OutIndicesWriter(StartOutIndex + 8, Tri.GetIndex(0));
87 
88  OutIndicesWriter(StartOutIndex + 9, Tri.GetIndex(0));
89  OutIndicesWriter(StartOutIndex + 10, Tri.GetIndex(1));
90  OutIndicesWriter(StartOutIndex + 11, Tri.GetIndex(2));
91 
92 
93  Edge Rev0 = Tri.GetEdge(0).GetReverse();
94  Edge Rev1 = Tri.GetEdge(1).GetReverse();
95  Edge Rev2 = Tri.GetEdge(2).GetReverse();
96 
97  OutEdgeDict.Add(Rev0, Rev0);
98  OutEdgeDict.Add(Rev1, Rev1);
99  OutEdgeDict.Add(Rev2, Rev2);
100 
101  AddIfLeastUV(OutPosDict, Vertex0, Index0);
102  AddIfLeastUV(OutPosDict, Vertex1, Index1);
103  AddIfLeastUV(OutPosDict, Vertex2, Index2);
104  }
105 }
106 
107 
108 
109 void FTessellationUtilities::ReplacePlaceholderIndices(int32 NumVertices, int32 NumIndices,
110  TFunction<FVector(int32)> PositionAccessor, TFunction<FVector2D(int32)> UVAccessor, TFunction<int32(int32)> IndexAccessor,
111  TFunction<void(int32)> OutIndicesSizeSetter, TFunction<int32()> OutIndicesSizeGetter, TFunction<void(int32, int32)> OutIndicesWriter, TFunction<int32(int32)> OutIndicesReader,
112  EdgeDictionary& EdgeDict, PositionDictionary& PosDict)
113 {
114  const uint32 TriangleCount = NumIndices / PnAenDomCorner_IndicesPerPatch;
115 
116  for (uint32 U = 0; U < TriangleCount; U++)
117  {
118  const uint32 StartOutIndex = U * PnAenDomCorner_IndicesPerPatch;
119 
120  const uint32 Index0 = OutIndicesReader(StartOutIndex + 0);
121  const uint32 Index1 = OutIndicesReader(StartOutIndex + 1);
122  const uint32 Index2 = OutIndicesReader(StartOutIndex + 2);
123 
124  const Vertex Vertex0(PositionAccessor(Index0), UVAccessor(Index0));
125  const Vertex Vertex1(PositionAccessor(Index1), UVAccessor(Index1));
126  const Vertex Vertex2(PositionAccessor(Index2), UVAccessor(Index2));
127 
128  Triangle Tri(Index0, Index1, Index2, Vertex0, Vertex1, Vertex2);
129 
130  Edge* Ed = EdgeDict.Find(Tri.GetEdge(0));
131  if (Ed != nullptr)
132  {
133  OutIndicesWriter(StartOutIndex + 3, Ed->GetIndex(0));
134  OutIndicesWriter(StartOutIndex + 4, Ed->GetIndex(1));
135  }
136 
137  Ed = EdgeDict.Find(Tri.GetEdge(1));
138  if (Ed != nullptr)
139  {
140  OutIndicesWriter(StartOutIndex + 5, Ed->GetIndex(0));
141  OutIndicesWriter(StartOutIndex + 6, Ed->GetIndex(1));
142  }
143 
144  Ed = EdgeDict.Find(Tri.GetEdge(2));
145  if (Ed != nullptr)
146  {
147  OutIndicesWriter(StartOutIndex + 7, Ed->GetIndex(0));
148  OutIndicesWriter(StartOutIndex + 8, Ed->GetIndex(1));
149  }
150 
151  // Deal with dominant positions.
152  for (uint32 V = 0; V < VerticesPerTriangle; V++)
153  {
154  Corner* Corn = PosDict.Find(Tri.GetEdge(V).GetVertex(0).Position);
155  if (Corn != nullptr)
156  {
157  OutIndicesWriter(StartOutIndex + 9 + V, Corn->Index);
158  }
159  }
160  }
161 }
const uint32 DuplicateIndexCount
const uint32 EdgesPerTriangle
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)
DECLARE_CYCLE_STAT(TEXT("RML - Calculate Tessellation Indices"), STAT_RuntimeMeshLibrary_CalculateTessellationIndices, STATGROUP_RuntimeMesh)
const uint32 IndicesPerTriangle
TMap< FVector, Corner > PositionDictionary
const uint32 VerticesPerTriangle
const uint32 PnAenDomCorner_IndicesPerPatch
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