RuntimeMeshManager.h
Go to the documentation of this file.
1 // Copyright 2018-2019 Gabriel Zerbib (Moddingear). All Rights Reserved.
2 
3 #pragma once
4 
5 #include "CoreMinimal.h"
6 
16 template<typename VertexType, typename TriangleType>
17 struct /*RUNTIMEMESHCOMPONENT_API*/ FRuntimeMeshDataStruct //Do not uncomment the API, otherwise you'll get unresolved externals
18 {
19  TArray<VertexType> Vertices;
20  TArray<TriangleType> Triangles;
21 
23 
24  FRuntimeMeshDataStruct(TArray<VertexType> InVertices, TArray<TriangleType> InTriangles) {
25  Vertices = InVertices;
26  Triangles = InTriangles;
27  }
28 
31  for (int32 i = 0; i < Triangles.Num(); i += 3) {
32  newmesh.Triangles[i] = Triangles[i + 1];
33  newmesh.Triangles[i + 1] = Triangles[i];
34  }
35  return newmesh;
36  }
37 };
38 
39 template<typename Datastruct, typename VertexType, typename TriangleType>
40 class /*RUNTIMEMESHCOMPONENT_API*/ FRuntimeMeshManager
41 {
42 private:
43 
45 
47 
48  TArray<Datastruct> DataMap;
49 
50  TArray<int32> VertLengths;
51 
52  TArray<int32> TrisLengths;
53 
54 public:
55 
56  /* Used to know if a mesh is already present in the mesh manager */
57  FORCEINLINE bool HasMesh(Datastruct id)
58  {
59  return DataMap.contains(id);
60  }
61 
62  /* Returns the full mesh that combines all provided meshes */
63  FORCEINLINE MeshType GetMesh()
64  {
65  return Mesh;
66  }
67 
68  /* Returns the internal array of the saved meshes' data, in correct order
69  Only useful if you want to iterate on the meshes */
70  FORCEINLINE TArray<Datastruct> GetMeshData()
71  {
72  return DataMap;
73  }
74 
75  /* Returns the mesh that was saved at the ID */
76  FORCEINLINE MeshType GetMesh(Datastruct id)
77  {
78  int32 index = DataMap.find(id);
79  return GetMeshAtIndex(index);
80  }
81 
82  /* Returns the mesh that was saved at the index
83  Only useful when iterating */
84  FORCEINLINE MeshType GetMeshAtIndex(int32 index)
85  {
86  MeshType submesh;
87  submesh.Vertices.SetNum(VertLengths[index]);
88  submesh.Triangles.SetNum(TrisLengths[index]);
89  int32 vertstart, trisstart;
90  getStart(index, vertstart, trisstart);
91  for (int32 i = 0; i < VertLengths[index]; i++)
92  {
93  submesh.Vertices[i] = Mesh.Vertices[i + vertstart];
94  }
95  for (int32 i = 0; i < TrisLengths[index]; i++)
96  {
97  submesh.Triangles[i] = Mesh.Triangles[i + trisstart];
98  }
99  }
100 
101  /* Adds or updates the mesh for the specified ID
102  Giving a mesh with an ID that is already registered will cause it to update
103  Use the same vert and tris length to avoid recreating the array */
104  FORCEINLINE void AddMesh(Datastruct id, MeshType mesh)
105  {
106  if (DataMap.Contains(id)) {
107  /*int32 index = DataMap.Find(id);
108  if (mesh.Vertices.Num() == VertLengths[index] && mesh.Triangles.Num() == TrisLengths[index]) {
109  //If the lengths match, update the arrays only
110  int32 VertStart, TrisStart;
111  getStart(index, VertStart, TrisStart);
112  for (int i = 0; i < mesh.Vertices.Num(); i++)
113  {
114  Mesh.Vertices[i + VertStart] = mesh.Vertices[i];
115  }
116  for (int i = 0; i < mesh.Triangles.Num(); i++)
117  {
118  Mesh.Triangles[i + TrisStart] = mesh.Triangles[i] + VertStart;
119  }
120  return; //stop the execution
121  }
122  else {*/
123  RemoveMesh(id);
124  //}
125  }
126 
127 
128  int32 vertstart = Mesh.Vertices.Num(), vertlen = mesh.Vertices.Num(), trisstart = Mesh.Triangles.Num(), trislen = mesh.Triangles.Num();
129 
130  DataMap.Add(id); //Add panel and mesh properties to the map
131  VertLengths.Add(vertlen);
132  TrisLengths.Add(trislen);
133 
134  Mesh.Vertices.Append(mesh.Vertices); //Modify the mesh
135  for (int i = 0; i < trislen; i++)
136  {
137  Mesh.Triangles.Add(vertstart + mesh.Triangles[i]);
138  }
139  };
140 
141  /* Removes the submesh that was linked to the supplied id */
142  FORCEINLINE void RemoveMesh(Datastruct id)
143  {
144  if (!DataMap.Contains(id)) {
145  return;
146  }
147  int index = DataMap.Find(id);
148  int32 vertstart = 0, vertlen = VertLengths[index], trisstart = 0, trislen = TrisLengths[index];
149 
150  getStart(index, vertstart, trisstart); // Find the start of the saved data
151 
152  //Remove tris first to avoid updating the indices that will be removed
153  removeTris(trisstart, trislen); //Delete tris
154  removeVerts(vertstart, vertlen, trisstart); //Delete vertices
155 
156  //Data
157  DataMap.RemoveAt(index);
158  VertLengths.RemoveAt(index);
159  TrisLengths.RemoveAt(index);
160  };
161 
162 private:
163 
164  FORCEINLINE void getStart(int32 index, int32& VertStart, int32& TrisStart)
165  {
166  VertStart = 0; TrisStart = 0;
167 
168  for (int i = 0; i < index; i++)
169  {
170  VertStart += VertLengths[i];
171  TrisStart += TrisLengths[i];
172  }
173  }
174 
175  /* Removes the verts and shifts the triangles references */
176  FORCEINLINE void removeVerts(int32 start, int32 len, int32 trisStart)
177  {
178  // Since meshes don't refer to other meshes, we update only the last since we are sure that only the last have indices bigger than those removed
179  for (int i = trisStart; i < Mesh.Triangles.Num(); i++)
180  {
181  Mesh.Triangles[i] -= len;
182  }
183  Mesh.Vertices.RemoveAt(start, len, true);
184  }
185 
186  FORCEINLINE void removeTris(int32 start, int32 len)
187  {
188  Mesh.Triangles.RemoveAt(start, len, true);
189  }
190 
191 public:
192  /* Retrieves the ID of the submesh that was hit
193  Takes the tris number, not the index of the tris (it doesn't need to be multiplied by 3, just feed the collision data)*/
194  FORCEINLINE Datastruct GetMeshHit(TriangleType tris)
195  {
196  int32 trisnum = 0;
197  for (int i = 0; i < DataMap.Num(); i++)
198  {
199  trisnum += TrisLengths[i];
200  if (tris * 3 < trisnum) {
201  return DataMap[i];
202  }
203  }
204  return Datastruct();
205  };
206 
207  /* Returns the internal mesh index of the mesh that was hit */
208  FORCEINLINE int32 GetMeshIndexHit(TriangleType tris)
209  {
210  int32 trisnum = 0;
211  for (int i = 0; i < DataMap.Num(); i++)
212  {
213  trisnum += TrisLengths[i];
214  if (tris * 3 < trisnum) {
215  return i;
216  }
217  }
218  return INDEX_NONE;
219  };
220 };
FRuntimeMeshDataStruct< VertexType, TriangleType > FlipNormals()
FORCEINLINE void RemoveMesh(Datastruct id)
FRuntimeMeshDataStruct< VertexType, TriangleType > Mesh
FORCEINLINE void removeTris(int32 start, int32 len)
FRuntimeMeshDataStruct(TArray< VertexType > InVertices, TArray< TriangleType > InTriangles)
FORCEINLINE MeshType GetMesh(Datastruct id)
FORCEINLINE int32 GetMeshIndexHit(TriangleType tris)
FORCEINLINE bool HasMesh(Datastruct id)
TArray< VertexType > Vertices
GLuint index
GLenum GLsizei len
Definition: glext.h:3285
FORCEINLINE void getStart(int32 index, int32 &VertStart, int32 &TrisStart)
TArray< TriangleType > Triangles
TArray< int32 > VertLengths
FORCEINLINE void AddMesh(Datastruct id, MeshType mesh)
GLuint start
TArray< Datastruct > DataMap
FORCEINLINE TArray< Datastruct > GetMeshData()
TArray< int32 > TrisLengths
FORCEINLINE void removeVerts(int32 start, int32 len, int32 trisStart)
FRuntimeMeshDataStruct< VertexType, TriangleType > MeshType
static GLuint mesh
Definition: heightmap.c:113
FORCEINLINE Datastruct GetMeshHit(TriangleType tris)
int i
FORCEINLINE MeshType GetMeshAtIndex(int32 index)
FORCEINLINE MeshType GetMesh()


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