Ocean
TextureGenerator.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_TRACKING_MAPTEXTURING_TEXTURE_GENERATOR_H
9 #define META_OCEAN_TRACKING_MAPTEXTURING_TEXTURE_GENERATOR_H
10 
22 
23 #include "ocean/base/Frame.h"
24 
26 
27 #include "ocean/math/AnyCamera.h"
29 
31 
32 #include "ocean/rendering/Engine.h"
35 
36 namespace Ocean
37 {
38 
39 namespace Tracking
40 {
41 
42 namespace MapTexturing
43 {
44 
45 /**
46  * This class implements a texture generator for scene elements.
47  * The generator separates the scene elements into individual meshes sharing the same texture.
48  * @ingroup trackingmaptexturing
49  */
50 class OCEAN_TRACKING_MAPTEXTURING_EXPORT TextureGenerator
51 {
52  public:
53 
54  /**
55  * Definition of individual texturing modes.
56  */
57  enum TexturingMode : uint32_t
58  {
59  /// An invalid texturing mode.
60  TM_INVALID = 0u,
61  /// Keeping retired triangles.
63  /// Redraw retired triangles.
64  TM_REDRAW_RETIRED
65  };
66 
67  /**
68  * This class stores the relevant information of a mesh textures with the same texture.
69  */
70  class Mesh
71  {
72  public:
73 
74  /// The vertices of the mesh.
76 
77  /// The triangle faces of the mesh.
79 
80  /// The texture coordinates, one for each vertex.
82  };
83 
84  /**
85  * Definition of an unordered map mapping Mesh ids to mesh objects.
86  */
87  typedef std::unordered_map<Index32, Mesh> MeshMap;
88 
89  protected:
90 
91  /**
92  * This class stores the relevant information for a triangle.
93  */
95  {
96  public:
97 
98  /**
99  * Creates a new triangle object.
100  * @param id The id of the triangle
101  * @param frameIndex The index of the frame in which the triangle has been created
102  */
103  inline TriangleObject(const Index32 id, const Index32 frameIndex);
104 
105  public:
106 
107  /// The unique ids of the triangle.
108  Index32 id_ = Index32(-1);
109 
110  /// The index of the frame in which the triangle existed the last time.
111  Index32 frameIndex_ = Index32(-1);
112  };
113 
114  /**
115  * Definition of an unordered map mapping hash-able triangles to a TriangleObject objects.
116  */
117  typedef std::unordered_map<HashableTriangle, TriangleObject, HashableTriangle> TriangleMap;
118 
119  /**
120  * Definition of a vector holding hash-able triangle objects.
121  */
122  typedef std::vector<HashableTriangle> Triangles;
123 
124  /**
125  * Definition of a map mapping unique scene mesh ids to vertex counts.
126  */
127  typedef std::unordered_map<Index32, size_t> SceneMeshVertexCounterMap;
128 
129  /**
130  * Definition of an unordered map mapping mesh ids to texture framebuffers.
131  */
132  typedef std::unordered_map<Index32, Rendering::TextureFramebufferRef> TextureFramebufferMap;
133 
134  public:
135 
136  /**
137  * Creates a new texture generator.
138  * @param texturingMode The texturing mode to be used
139  */
140  explicit inline TextureGenerator(const TexturingMode texturingMode = TM_REDRAW_RETIRED);
141 
142  /**
143  * Updates the generator's meshes with new meshes given as scene elements.
144  * The mesh can be updated before each new frame is processed or with a lower frequency (e.g., only for every second frame or only once before the very first frame).
145  * @param sceneElement The new scene element to be used to update the generator
146  * @param skipIfVertexNumberHasNotChanged True, to skip all meshes when their vertex number has not changed; False, to handle meshes even when their vertex number have not changed
147  * @return True, if succeeded
148  */
149  bool updateMesh(const Devices::SceneTracker6DOF::SharedSceneElement& sceneElement, const bool skipIfVertexNumberHasNotChanged);
150 
151  /**
152  * Processes a new frame and updates or sets the texture of the texture atlases whenever necessary.
153  * @param rgbFrame The new frame to be used, with pixel format FORMAT_RGB24, must be valid
154  * @param anyCamera The camera profile defining the projection of the given frame, must be valid
155  * @param world_T_camera The transformation between camera and world, with the camera pointing into the negative z-space with y-axis up, must be valid
156  * @param engine The rendering engine to be used, must be valid
157  * @return True, if succeeded
158  */
159  bool processFrame(Frame&& rgbFrame, const AnyCamera& anyCamera, const HomogenousMatrix4& world_T_camera, const Rendering::EngineRef& engine);
160 
161  /**
162  * Renders the current textured mesh for a given camera pose.
163  * @param anyCamera The camera profile used when rendering the image, must be valid
164  * @param world_T_camera The transformation between camera and world, must be valid
165  * @param engine The rendering engine to be used, must be valid
166  * @param renderTexturedTrianglesOnly True, to render only textured triangles; False, to render also non-textured triangles
167  * @param frame The resulting rendered frame
168  * @return True, if succeeded
169  */
170  bool render(const AnyCamera& anyCamera, const HomogenousMatrix4& world_T_camera, const Rendering::EngineRef& engine, const bool renderTexturedTrianglesOnly, Frame& frame);
171 
172  /**
173  * Returns the ids of the generated textured meshes.
174  * @return The meshes' ids
175  */
177 
178  /**
179  * Returns a current mesh.
180  * @param meshId The id of the mesh to return
181  * @param mesh The resulting mesh
182  * @param meshTexture The resulting texture of the mesh, invalid if the mesh is not textured
183  * @return True, if succeeded
184  */
185  bool exportMesh(const Index32 meshId, Mesh& mesh, Frame& meshTexture);
186 
187  /**
188  * Exports all currently textured meshes and creates an X3D scene file.
189  * @param filename The filename of the resulting scene file
190  * @param exportTexturedTrianglesOnly True, to export only textured triangles; False, to export also non-textured triangles
191  * @return True, if succeeded
192  */
193  bool exportMeshs(const std::string& filename, const bool exportTexturedTrianglesOnly);
194 
195  /**
196  * Returns whether this generator holds at least one triangle.
197  * @return True, if succeeded
198  */
199  inline bool isValid() const;
200 
201  protected:
202 
203  /**
204  * Processes a new frame and updates or sets the texture of the texture atlases whenever necessary with texturing mode TM_KEEP_RETIRED.
205  * @param rgbFrame The new frame to be used, with pixel format FORMAT_RGB24, must be valid
206  * @param anyCamera The camera profile defining the projection of the given frame, must be valid
207  * @param world_T_camera The transformation between camera and world, with the camera pointing into the negative z-space with y-axis up, must be valid
208  * @param engine The rendering engine to be used, must be valid
209  * @return True, if succeeded
210  */
211  bool processFrameKeepRetired(Frame&& rgbFrame, const AnyCamera& anyCamera, const HomogenousMatrix4& world_T_camera, const Rendering::EngineRef& engine);
212 
213  /**
214  * Processes a new frame and updates or sets the texture of the texture atlases whenever necessary with texturing mode TM_REDRAW_RETIRED.
215  * @param rgbFrame The new frame to be used, with pixel format FORMAT_RGB24, must be valid
216  * @param anyCamera The camera profile defining the projection of the given frame, must be valid
217  * @param world_T_camera The transformation between camera and world, with the camera pointing into the negative z-space with y-axis up, must be valid
218  * @param engine The rendering engine to be used, must be valid
219  * @return True, if succeeded
220  */
221  bool processFrameRedrawRetired(Frame&& rgbFrame, const AnyCamera& anyCamera, const HomogenousMatrix4& world_T_camera, const Rendering::EngineRef& engine);
222 
223  /**
224  * Extracts a mesh of this texture generator with texture coordinates associated with the texture atlas.
225  * @param meshId The id of the mesh to extract
226  * @param mesh The resulting mesh
227  * @return True, if at least one triangle was extracted
228  */
230 
231  /**
232  * Extracts all meshes of this texture generator with texture coordinates associated with the texture atlas.
233  * @param meshMap The resulting map with the individual meshes
234  * @param exportTexturedTrianglesOnly True, to extract only textured triangles; False, to extract also non-textured triangles
235  * @return True, if at least one triangle was extracted
236  */
237  bool extractMeshesWithAtlasTextureCoordinates(MeshMap& meshMap, const bool exportTexturedTrianglesOnly);
238 
239  /**
240  * Returns an invalid triangle id.
241  * @return The invalid triangle id
242  */
243  static constexpr Index32 invalidTriangleId();
244 
245  protected:
246 
247  /// The texturing mode to be used.
248  TexturingMode texturingMode_ = TM_INVALID;
249 
250  /// The counter for triangle ids.
251  Index32 triangleIdCounter_ = invalidTriangleId();
252 
253  /// The map mapping hash-able triangles to triangle objects.
255 
256  /// The vector of all triangles, with ids identical to indices.
257  Triangles triangles_ = Triangles(1024 * 1024);
258 
259  /// The ids of triangles which can be reused as they have been deleted before.
261 
262  /// The current frame index.
263  unsigned int frameIndex_ = 0u;
264 
265  /// The dummy framebuffer for platforms not having their own main framebuffer.
267 
268  /// The renderer able to render triangles with individual color ids of all active triangles (not including retired triangles).
270 
271  /// The renderer able to render triangles with individual color ids of all existing triangles (active and retired triangles).
273 
274 #ifndef OCEAN_PLATFORM_BUILD_APPLE_IOS_ANY
275 
276  /// The renderer able to render textured triangles to the texture atlas.
278 
279 #endif
280 
281  /// The renderer able to render textured triangles to the texture atlas.
283 
284  /// The renderer able to render all retired triangles.
286 
287  /// The renderer able to down-sample and to filter a depth buffer.
289 
290  /// The renderer able to determine which triangles are visible.
292 
293  /// The renderer able to manage the states of all textures.
295 
296  /// The renderer for the entire mesh.
298 
299  /// The profile of the texture atlas to be used for all textures.
301 
302  /// Reusable frame for triangle ids.
304 
305  /// Reusable frame for the z-buffer.
307 
308  /// The ids of all new triangles since the last update call.
310 
311  /// The ids of all retired triangles since the last update call.
313 
314  /// The reusable vector holding ids of deleted triangle.
316 
317  /// The vertex set holding the vertices to be rendered.
319 
320  /// The triangle object which will be used to render the triangles.
322 
323  /// True, if any mesh scene mesh has changed.
324  bool anySceneMeshHasChanged_ = false;
325 
326  /// The latest vertex counts of all scene meshes.
328 
329  /// The generator's lock.
330  mutable Lock lock_;
331 };
332 
334  texturingMode_(texturingMode)
335 {
336  // nothing to do here
337 }
338 
340  id_(id),
341  frameIndex_(frameIndex)
342 {
343  // nothing to do here
344 }
345 
346 inline bool TextureGenerator::isValid() const
347 {
348  return !triangleMap_.empty();
349 }
350 
352 {
353  return 0u;
354 }
355 
356 }
357 
358 }
359 
360 }
361 
362 #endif // META_OCEAN_TRACKING_MAPTEXTURING_TEXTURE_GENERATOR_H
This class implements the abstract base class for all AnyCamera objects.
Definition: AnyCamera.h:130
std::shared_ptr< SceneElement > SharedSceneElement
Definition of a shared pointer holding a scene element.
Definition: SceneTracker6DOF.h:896
This class implements Ocean's image class.
Definition: Frame.h:1760
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a renderer for a depth buffer.
Definition: DepthBufferRenderer.h:33
This class implements a renderer for the entire mesh.
Definition: MeshRenderer.h:41
This class implements a renderer for retired triangle allowing to determine whether they can be delet...
Definition: RetiredTrianglesRenderer.h:37
This class implements a texture atlas for triangles with regular shape.
Definition: TextureAtlas.h:31
This class stores the relevant information of a mesh textures with the same texture.
Definition: TextureGenerator.h:71
Vectors2 textureCoordinates_
The texture coordinates, one for each vertex.
Definition: TextureGenerator.h:81
Vectors3 vertices_
The vertices of the mesh.
Definition: TextureGenerator.h:75
Rendering::TriangleFaces triangleFaces_
The triangle faces of the mesh.
Definition: TextureGenerator.h:78
This class stores the relevant information for a triangle.
Definition: TextureGenerator.h:95
TriangleObject(const Index32 id, const Index32 frameIndex)
Creates a new triangle object.
Definition: TextureGenerator.h:339
This class implements a texture generator for scene elements.
Definition: TextureGenerator.h:51
TrianglesIdRenderer activeTrianglesIdRenderer_
The renderer able to render triangles with individual color ids of all active triangles (not includin...
Definition: TextureGenerator.h:269
Lock lock_
The generator's lock.
Definition: TextureGenerator.h:330
TextureAtlas textureAtlas_
The profile of the texture atlas to be used for all textures.
Definition: TextureGenerator.h:300
TexturingMode
Definition of individual texturing modes.
Definition: TextureGenerator.h:58
@ TM_KEEP_RETIRED
Keeping retired triangles.
Definition: TextureGenerator.h:62
TexturedTrianglesRenderer texturedTrianglesRenderer_
The renderer able to render textured triangles to the texture atlas.
Definition: TextureGenerator.h:277
SceneMeshVertexCounterMap latestSceneMeshVertexCounterMap_
The latest vertex counts of all scene meshes.
Definition: TextureGenerator.h:327
Rendering::FramebufferRef dummyFramebuffer_
The dummy framebuffer for platforms not having their own main framebuffer.
Definition: TextureGenerator.h:266
Frame reusableDepthFrame_
Reusable frame for the z-buffer.
Definition: TextureGenerator.h:306
Indices32 retiredTriangleIds_
The ids of all retired triangles since the last update call.
Definition: TextureGenerator.h:312
std::vector< HashableTriangle > Triangles
Definition of a vector holding hash-able triangle objects.
Definition: TextureGenerator.h:122
std::unordered_map< HashableTriangle, TriangleObject, HashableTriangle > TriangleMap
Definition of an unordered map mapping hash-able triangles to a TriangleObject objects.
Definition: TextureGenerator.h:117
TriangleMap triangleMap_
The map mapping hash-able triangles to triangle objects.
Definition: TextureGenerator.h:254
bool extractMeshesWithAtlasTextureCoordinates(MeshMap &meshMap, const bool exportTexturedTrianglesOnly)
Extracts all meshes of this texture generator with texture coordinates associated with the texture at...
Indices32 freeTriangleIds_
The ids of triangles which can be reused as they have been deleted before.
Definition: TextureGenerator.h:260
std::unordered_map< Index32, Mesh > MeshMap
Definition of an unordered map mapping Mesh ids to mesh objects.
Definition: TextureGenerator.h:87
TrianglesIdRenderer anyTrianglesIdRenderer_
The renderer able to render triangles with individual color ids of all existing triangles (active and...
Definition: TextureGenerator.h:272
bool render(const AnyCamera &anyCamera, const HomogenousMatrix4 &world_T_camera, const Rendering::EngineRef &engine, const bool renderTexturedTrianglesOnly, Frame &frame)
Renders the current textured mesh for a given camera pose.
bool exportMeshs(const std::string &filename, const bool exportTexturedTrianglesOnly)
Exports all currently textured meshes and creates an X3D scene file.
RetiredTrianglesRenderer retiredTrianglesRenderer_
The renderer able to render all retired triangles.
Definition: TextureGenerator.h:285
TexturedTrianglesRendererBackup texturedTrianglesRendererBackup_
The renderer able to render textured triangles to the texture atlas.
Definition: TextureGenerator.h:282
std::unordered_map< Index32, size_t > SceneMeshVertexCounterMap
Definition of a map mapping unique scene mesh ids to vertex counts.
Definition: TextureGenerator.h:127
bool processFrame(Frame &&rgbFrame, const AnyCamera &anyCamera, const HomogenousMatrix4 &world_T_camera, const Rendering::EngineRef &engine)
Processes a new frame and updates or sets the texture of the texture atlases whenever necessary.
VisibleTrianglesRenderer visibleTrianglesRenderer_
The renderer able to determine which triangles are visible.
Definition: TextureGenerator.h:291
std::unordered_map< Index32, Rendering::TextureFramebufferRef > TextureFramebufferMap
Definition of an unordered map mapping mesh ids to texture framebuffers.
Definition: TextureGenerator.h:132
Frame reusableIdFrame_
Reusable frame for triangle ids.
Definition: TextureGenerator.h:303
TrianglesManagerRenderer trianglesManagerRenderer_
The renderer able to manage the states of all textures.
Definition: TextureGenerator.h:294
Rendering::VertexSetRef renderingVertexSet_
The vertex set holding the vertices to be rendered.
Definition: TextureGenerator.h:318
MeshRenderer meshRenderer_
The renderer for the entire mesh.
Definition: TextureGenerator.h:297
bool processFrameRedrawRetired(Frame &&rgbFrame, const AnyCamera &anyCamera, const HomogenousMatrix4 &world_T_camera, const Rendering::EngineRef &engine)
Processes a new frame and updates or sets the texture of the texture atlases whenever necessary with ...
Indices32 deletedTriangleIds_
The reusable vector holding ids of deleted triangle.
Definition: TextureGenerator.h:315
bool processFrameKeepRetired(Frame &&rgbFrame, const AnyCamera &anyCamera, const HomogenousMatrix4 &world_T_camera, const Rendering::EngineRef &engine)
Processes a new frame and updates or sets the texture of the texture atlases whenever necessary with ...
bool exportMesh(const Index32 meshId, Mesh &mesh, Frame &meshTexture)
Returns a current mesh.
TextureGenerator(const TexturingMode texturingMode=TM_REDRAW_RETIRED)
Creates a new texture generator.
Definition: TextureGenerator.h:333
bool updateMesh(const Devices::SceneTracker6DOF::SharedSceneElement &sceneElement, const bool skipIfVertexNumberHasNotChanged)
Updates the generator's meshes with new meshes given as scene elements.
Rendering::TrianglesRef renderingTriangles_
The triangle object which will be used to render the triangles.
Definition: TextureGenerator.h:321
DepthBufferRenderer depthBufferRenderer_
The renderer able to down-sample and to filter a depth buffer.
Definition: TextureGenerator.h:288
Indices32 newTriangleIds_
The ids of all new triangles since the last update call.
Definition: TextureGenerator.h:309
bool extractMeshesWithAtlasTextureCoordinates(const Index32 meshId, Mesh &mesh)
Extracts a mesh of this texture generator with texture coordinates associated with the texture atlas.
Indices32 meshIds() const
Returns the ids of the generated textured meshes.
bool isValid() const
Returns whether this generator holds at least one triangle.
Definition: TextureGenerator.h:346
static constexpr Index32 invalidTriangleId()
Returns an invalid triangle id.
Definition: TextureGenerator.h:351
This class implements a renderer rendering textured triangles into a texture atlas using a backup app...
Definition: TexturedTrianglesRendererBackup.h:41
This class implements a renderer rendering textured triangles into a texture atlas using a geometry s...
Definition: TexturedTrianglesRenderer.h:41
This class implements a renderer for triangles with individual color ids.
Definition: TrianglesIdRenderer.h:38
This class implements a manager for active, textured, retired, and deleted triangles.
Definition: TrianglesManagerRenderer.h:41
This class implements a renderer for all visible triangles.
Definition: VisibleTrianglesRenderer.h:38
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition: Base.h:96
uint32_t Index32
Definition of a 32 bit index value.
Definition: Base.h:84
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition: Vector2.h:64
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition: Vector3.h:65
std::vector< TriangleFace > TriangleFaces
Definition of a vector holding triangle faces.
Definition: TriangleFace.h:23
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15