VR-Engage  2.2
Loading...
Searching...
No Matches
vortexPagedTerrain.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2024 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file vortexPagedTerrain.h
7//! \brief Class that manages all the translation from the VR-Forces terrain to the Vortex Terrain system.
8//! This class also manages dynamic terrain events that come from DtVortexDynamicTerrainHandler.
9//! This class acts as an interface between the paged terrain producer and the terrain database in regions
10//! where parts and collision geometries are required for collision with other entities.
11//! The responsibility of the paged terrain interface is to answer these queries and to create the requested
12//! parts and collision geometries from the terrain information in the terrain database.
13//!
14//! \ingroup vreVrfVortex
15
16#pragma once
17
18#include <vreVrfVortex/export.h>
21
22#include <vrfobjcore/simObjectReference.h>
23
25
26#include <VxTerrain/VxPagedTerrainInterface.h>
27
28#include <VxMath/VxMath.h>
29#include <VxMath/BoundingBox.h>
30
31#include <tbb/concurrent_vector.h>
32#include <tbb/task_arena.h>
33#include <tbb/spin_mutex.h>
34
35#include <future>
36
37// Forward declares
38class DtTerrainImplementation;
39
40namespace VxTerrain
41{
42class VxPagedTerrainProducer;
43}
44
45namespace Vx
46{
47class VxCollisionGeometry;
48class VxPart;
49class VxTriangleMeshUVGrid;
50class VxTriangleMeshBVTree;
51} // namespace Vx
52
53namespace makVre
54{
59
60//! \brief Main class that manages terrain translation between VR-Forces and Vortex
61//!
62//! This class facilitates the communication between VR-Forces terrain system and the Vortex physics engine.
63//! It handles the conversion of terrain data, processes terrain generation requests, creates collision
64//! geometries, and manages dynamic terrain modifications. The class implements the VxPagedTerrainInterface
65//! to interact with the Vortex paged terrain system.
66class VREVRFVORTEX_DLL DtVortexPagedTerrainInterface : public VxTerrain::VxPagedTerrainInterface
67{
68public:
69 //! \brief Data structure for managing new terrain requests from the Vortex Forecaster
70 //!
71 //! Holds information about asynchronous terrain generation requests including priority,
72 //! unique identifier, and future object for tracking completion.
74 {
75 std::shared_future<void> future; //!< Future object for asynchronous processing
76 int priority; //!< Priority level of the request
77 int requestId; //!< Unique identifier for the request
78 };
79 using NewTerrainRequestQueue = std::list<VortexNewTerrainRequest>;
80
81 //! \brief Structure for queueing vortex requests to be inserted on the next update()
82 //!
83 //! Contains all necessary data for creating and inserting a new terrain cell,
84 //! including the bounding box, triangle mesh, and triangle interface.
86 {
87 //! \brief Default constructor
88 //!
89 //! Initializes the structure with null pointers for the triangle interface and mesh.
91
92 //! \brief Destructor
93 //!
94 //! Cleans up by setting pointers to nullptr to prevent dangling references.
96
97 VxMath::BoundingBox requestBounds; //!< The bounding box of this request
98 std::shared_ptr<DtVortexTriangleOutputInterface> newTriangleInterface; //!< Triangle Interface
99 Vx::VxTriangleMeshBVTree* newTriangleMesh; //!< The triangle mesh
100 };
101
102 //! \brief Structure for queueing vortex requests to be modified on the next step()
103 //!
104 //! Contains the data needed to modify an existing terrain cell, including
105 //! source ID, collision geometry, and new triangle data.
107 {
108 //! \brief Default constructor
109 //!
110 //! Initializes the structure with null pointers for collision geometry and triangle data.
112
113 int sourceId; //!< The source ID this cell collision geometry links to
114 Vx::VxCollisionGeometry* collisionGeometry; //!< Collision Geometry
115 Vx::VxTriangleMeshBVTree* newTriangleData; //!< This is the new triangleData
116 std::vector<DtSimObjectReference>
117 objects; //!< List of objects that will need to be dropped if this is a dynamic terrain event
118 };
119
120public:
121 //! \brief Default constructor
122 //!
123 //! Initializes the paged terrain interface with default settings, sets up the material mapper,
124 //! and retrieves configuration parameters from the Vortex physical world. Also initializes
125 //! the terrain implementation and coordinate conversion system.
127
128 //! \brief Destructor
129 //!
130 //! Performs cleanup of resources owned by this interface.
132
133 //! \brief Called every frame from within the vortex application while running
134 //!
135 //! Reorganizes requests based on priority, processes queued collision geometries,
136 //! and manages the terrain regeneration queue. Also handles the completion of
137 //! asynchronous terrain generation tasks.
138 //!
139 //! \param producer The current terrain producer
140 virtual void step(VxTerrain::VxPagedTerrainProducer* producer) override;
141
142 //! \brief Called when new requests for terrain arise
143 //!
144 //! Processes and queues new terrain generation requests by calling queueNewTerrainRequests.
145 //! These requests are then processed asynchronously by the terrain system.
146 //!
147 //! \param request A list of requests to queue up for terrain generation
148 //! \param producer The current terrain producer
149 virtual void produce(
150 const std::vector<VxTerrain::VxTerrainRequest>& request, VxTerrain::VxPagedTerrainProducer* producer) override;
151
152 //! \brief Called when pending requests for terrain get higher priority
153 //!
154 //! Updates the priority of existing terrain requests to ensure that urgent
155 //! requests are processed first during the next update cycle.
156 //!
157 //! \param request A list of requests to update priority for
158 //! \param producer The current terrain producer
159 virtual void update(
160 const std::vector<VxTerrain::VxTerrainRequest>& request, VxTerrain::VxPagedTerrainProducer* producer) override;
161
162 //! \brief Gets the material mapper
163 //!
164 //! Returns the current material mapper used for translating terrain surface types
165 //! to Vortex material properties.
166 //!
167 //! \return The current material mapper
168 virtual std::shared_ptr<DtSurfaceToVxMaterialMapper> materialMapper() const;
169
170 //! \brief Sets the material mapper
171 //!
172 //! Updates the material mapper used for translating terrain surface types
173 //! to Vortex material properties.
174 //!
175 //! \param val The new material mapper to use
176 virtual void setMaterialMapper(std::shared_ptr<DtSurfaceToVxMaterialMapper> val);
177
178 //! \brief Sort the incoming requests to have the highest priority ones first
179 //!
180 //! Prioritizes terrain requests based on their priority level and submits them
181 //! as asynchronous jobs to the thread pool. This ensures that more important
182 //! terrain cells are generated first.
183 //!
184 //! \param requests List of requests to queue up
185 virtual void queueNewTerrainRequests(const std::vector<VxTerrain::VxTerrainRequest>& requests);
186
187 //! \brief Queues up and manages any new dynamic terrain or terrain regeneration requests
188 //!
189 //! Processes terrain modification requests from the dynamic terrain handler.
190 //! This includes creating new triangle meshes for modified terrain areas
191 //! and queuing them for integration in the next update cycle.
193
194 //! \brief Gets the maximum terrain height tolerance check value
195 //!
196 //! Returns the tolerance value used to determine how far above/below
197 //! the terrain to check for collisions.
198 //!
199 //! \return The current terrain height tolerance check value in meters
200 virtual double terrainHeightToleranceCheck() const;
201
202 //! \brief Sets the maximum terrain height tolerance check value
203 //!
204 //! Updates the tolerance value used to determine how far above/below
205 //! the terrain to check for collisions. This value can be configured
206 //! through vortexConfig.lua.
207 //!
208 //! \param val The new terrain height tolerance check value in meters
209 virtual void setTerrainHeightToleranceCheck(double val);
210
211protected:
212 //! \brief Generates a source ID given the minimum x and y coordinate of a request
213 //!
214 //! Creates a unique identifier for a terrain cell based on its position.
215 //! The ID is calculated using the cell's row and column in the terrain grid,
216 //! and is guaranteed to be positive and within a valid range.
217 //!
218 //! \param minXCoordinate The minimum X coordinate of the request
219 //! \param minYCoordinate The minimum Y coordinate of the request
220 //! \return A unique source ID for the terrain cell
221 virtual int generateSourceID(double minXCoordinate, double minYCoordinate);
222
223 //! \brief Helper function for processing queued collision geometries
224 //!
225 //! Processes collision geometries contained in myCellsToInsert and myCellsToModify.
226 //! This includes creating new collision geometries, integrating them into the
227 //! Vortex system, and handling dynamic terrain modifications.
229
230 //! \brief Processes all dynamic terrain events that have been queued up for regeneration
231 //!
232 //! Handles the regeneration of terrain cells that have been modified by dynamic terrain events.
233 //! This includes modifying existing triangle meshes, updating collision geometries,
234 //! and managing affected simulation objects.
235 //!
236 //! \param regenerationData Pair containing the source ID and new triangle mesh
237 //! \return True if the regeneration was successful, false otherwise
238 virtual bool processTerrainRegenerationRequest(const std::pair<int, Vx::VxTriangleMeshBVTree*>& regenerationData);
239
240 //! \brief Processes a terrain request and builds a collision geometry for Vortex
241 //!
242 //! Creates a new terrain cell from a terrain request, extracting triangles from the
243 //! terrain implementation and building a collision geometry for use in Vortex.
244 //! The resulting cell is then queued for insertion in the next update cycle.
245 //!
246 //! \param req The VxTerrainRequest that has been queued for generation
247 //! \param triMesh Optional pre-created triangle mesh to use
248 virtual void processNewTerrainRequest(VxTerrain::VxTerrainRequest req, Vx::VxTriangleMeshBVTree* triMesh = nullptr);
249
250 //! \brief Creates a collision geometry based on the vertices and the surface
251 //!
252 //! Generates a Vortex collision geometry from a triangle output interface,
253 //! either using a provided triangle mesh or creating a new one. The collision
254 //! geometry is configured with appropriate material properties and settings.
255 //!
256 //! \param outputInterface The information of the terrain cell that has been generated
257 //! \param triMeshParam Optional triangle mesh to use for the collision geometry
258 //! \return The created collision geometry
259 virtual Vx::VxCollisionGeometry* createVortexCollisionGeometry(
260 DtVortexTriangleOutputInterface& outputInterface, Vx::VxTriangleMeshBVTree* triMeshParam = nullptr);
261
262 //! \brief Fills out a triangle mesh based on all the triangles from the output interface
263 //!
264 //! Populates a Vortex triangle mesh with vertices, triangles, and material properties
265 //! from a triangle output interface. Handles coordinate conversion between VR-Forces
266 //! and Vortex coordinate systems.
267 //!
268 //! \param outputInterface The information of the terrain cell that has been generated
269 //! \param triMesh The triangle mesh to input all the triangles in
270 virtual void buildTriangleMesh(
271 const std::shared_ptr<DtVortexTriangleOutputInterface> outputInterface, Vx::VxTriangleMeshBVTree* triMesh);
272
273 //! \brief Creates a triangle mesh based on the output interface
274 //!
275 //! Creates and initializes a new Vortex triangle mesh from a triangle output interface.
276 //! The mesh is configured with appropriate settings and then populated with triangles
277 //! by calling buildTriangleMesh.
278 //!
279 //! \param outputInterface The information of the terrain cell that has been generated
280 //! \return A triangle mesh created based on the information
281 virtual Vx::VxTriangleMeshBVTree* createVortexTriangleMesh(DtVortexTriangleOutputInterface& outputInterface);
282
283 //! \brief Gets the terrain process box in terms of geocentric/local terrain
284 //!
285 //! Converts a Vortex bounding box to a terrain process box that can be used
286 //! with the terrain implementation to extract triangles. Handles the conversion
287 //! between Vortex and VR-Forces coordinate systems.
288 //!
289 //! \param bb Vortex request bounds
290 //! \return A terrain process box translated for use in getting triangles
291 virtual DtTerrainProcessPolygonsBox getTerrainProcessBox(const Vx::VxBoundingBox& bb);
292
293 //! \brief Modifies a triangle mesh with z values from a new mesh
294 //!
295 //! Updates the heights (z-values) of vertices in an existing triangle mesh
296 //! based on a new mesh. Only applies changes if the difference exceeds the
297 //! specified tolerance.
298 //!
299 //! \param currentMesh The existing mesh to be modified with updated height values
300 //! \param newMesh The new mesh to get the updated triangles
301 //! \param upAxis The axis representing the up direction
302 //! \param tolerance The tolerance for z-value differences
303 //! \return True if there were deformations, false otherwise
304 virtual bool modifyTriangleMesh(Vx::VxTriangleMeshBVTree* currentMesh, const Vx::VxTriangleMeshBVTree* newMesh,
305 const int upAxis, double tolerance = 0.1);
306
307 //! \brief Compares each triangle from both meshes and checks if they are equal
308 //!
309 //! Determines if two triangle meshes are different by comparing their vertices.
310 //! Vertices are considered equal if their positions differ by less than the
311 //! specified tolerance.
312 //!
313 //! \param meshOne First triangle mesh to compare
314 //! \param meshTwo Second triangle mesh to compare
315 //! \param tolerance The tolerance for considering vertices equal
316 //! \return True if the meshes are different, false if they are the same
318 Vx::VxTriangleMeshBVTree* meshOne, Vx::VxTriangleMeshBVTree* meshTwo, double tolerance = 0.1);
319
320 //! \brief Sets the orphan edges of the triangle mesh to not have an invalid angle between meshes
321 //!
322 //! Fixes orphan edges in a triangle mesh to ensure valid angles between connected triangles.
323 //! This addresses an issue in the Vortex code that can cause incorrect edge handling.
324 //!
325 //! \param triMesh The triangle mesh to fix orphan edges for
326 void fixTriangleMeshOrphanEdges(Vx::VxTriangleMeshBVTree* triMesh);
327
328protected:
329 DtVortexPhysicalWorld* myPhysicalWorld; //!< Pointer to the physical world
330 DtTerrainImplementation* myTerrainImplementation; //!< Pointer to the terrain implementation
331 DtVortexPagedTerrainProducer* myTerrainProducer; //!< Pointer to the terrain producer
332 std::weak_ptr<DtVortexDynamicTerrainHandler> myDynamicTerrainHandler; //!< Pointer to the dynamic terrain handler
333
334 std::shared_ptr<DtSurfaceToVxMaterialMapper>
335 myMaterialMapper; //!< Material mapper for surface to Vortex material conversion
336
337 std::unique_ptr<tbb::task_arena> myDynamicTerrainTaskArena; //!< Task arena for dynamic terrain processing
338
339 double myTerrainHeightToleranceCheck; //!< Height tolerance for terrain checks
340 double myDynamicTerrainRegenerationRetryInterval; //!< Interval for retrying dynamic terrain regeneration
341 int myDynamicTerrainRegenerationFailureCount; //!< Maximum number of failures before giving up on regeneration
342 unsigned int myInitialTriangleVectorSize; //!< Initial size for triangle vectors
343
344 bool myWaitForFutureRequests; //!< Whether to wait for future requests to complete
345
346 // Containers for terrain requests and cells
347 NewTerrainRequestQueue myNewTerrainRequests; //!< Queue of new terrain requests
348 tbb::concurrent_vector<VortexCellToInsert> myCellsToInsert; //!< Cells to insert on the next update
349 tbb::concurrent_vector<VortexCellToModify> myCellsToModify; //!< Cells to modify on the next step
350
351 tbb::spin_mutex myCellsToInsertSwapMutex; //!< Protects swap operation on myCellsToInsert (push_back is thread-safe)
352 tbb::spin_mutex myCellsToModifySwapMutex; //!< Protects swap operation on myCellsToModify (push_back is thread-safe)
353};
354} // namespace makVre
Manages dynamic terrain events and updates in the Vortex physics engine.
Definition vortexDynamicTerrainHandler.h:52
void fixTriangleMeshOrphanEdges(Vx::VxTriangleMeshBVTree *triMesh)
Sets the orphan edges of the triangle mesh to not have an invalid angle between meshes.
virtual void queueNewTerrainRequests(const std::vector< VxTerrain::VxTerrainRequest > &requests)
Sort the incoming requests to have the highest priority ones first.
tbb::concurrent_vector< VortexCellToInsert > myCellsToInsert
Cells to insert on the next update.
Definition vortexPagedTerrain.h:348
virtual std::shared_ptr< DtSurfaceToVxMaterialMapper > materialMapper() const
Gets the material mapper.
virtual void processQueuedCollisionGeometries()
Helper function for processing queued collision geometries.
std::list< VortexNewTerrainRequest > NewTerrainRequestQueue
Definition vortexPagedTerrain.h:79
virtual void processNewTerrainRequest(VxTerrain::VxTerrainRequest req, Vx::VxTriangleMeshBVTree *triMesh=nullptr)
Processes a terrain request and builds a collision geometry for Vortex.
virtual void queueTerrainRegenerationRequests()
Queues up and manages any new dynamic terrain or terrain regeneration requests.
DtVortexPhysicalWorld * myPhysicalWorld
Pointer to the physical world.
Definition vortexPagedTerrain.h:329
virtual void step(VxTerrain::VxPagedTerrainProducer *producer) override
Called every frame from within the vortex application while running.
virtual void buildTriangleMesh(const std::shared_ptr< DtVortexTriangleOutputInterface > outputInterface, Vx::VxTriangleMeshBVTree *triMesh)
Fills out a triangle mesh based on all the triangles from the output interface.
double myDynamicTerrainRegenerationRetryInterval
Interval for retrying dynamic terrain regeneration.
Definition vortexPagedTerrain.h:340
std::shared_ptr< DtSurfaceToVxMaterialMapper > myMaterialMapper
Material mapper for surface to Vortex material conversion.
Definition vortexPagedTerrain.h:335
virtual void setTerrainHeightToleranceCheck(double val)
Sets the maximum terrain height tolerance check value.
virtual Vx::VxTriangleMeshBVTree * createVortexTriangleMesh(DtVortexTriangleOutputInterface &outputInterface)
Creates a triangle mesh based on the output interface.
virtual bool areTriangleMeshesDifferent(Vx::VxTriangleMeshBVTree *meshOne, Vx::VxTriangleMeshBVTree *meshTwo, double tolerance=0.1)
Compares each triangle from both meshes and checks if they are equal.
virtual double terrainHeightToleranceCheck() const
Gets the maximum terrain height tolerance check value.
unsigned int myInitialTriangleVectorSize
Initial size for triangle vectors.
Definition vortexPagedTerrain.h:342
virtual DtTerrainProcessPolygonsBox getTerrainProcessBox(const Vx::VxBoundingBox &bb)
Gets the terrain process box in terms of geocentric/local terrain.
virtual void setMaterialMapper(std::shared_ptr< DtSurfaceToVxMaterialMapper > val)
Sets the material mapper.
DtVortexPagedTerrainProducer * myTerrainProducer
Pointer to the terrain producer.
Definition vortexPagedTerrain.h:331
NewTerrainRequestQueue myNewTerrainRequests
Queue of new terrain requests.
Definition vortexPagedTerrain.h:347
virtual void produce(const std::vector< VxTerrain::VxTerrainRequest > &request, VxTerrain::VxPagedTerrainProducer *producer) override
Called when new requests for terrain arise.
tbb::spin_mutex myCellsToModifySwapMutex
Protects swap operation on myCellsToModify (push_back is thread-safe)
Definition vortexPagedTerrain.h:352
virtual Vx::VxCollisionGeometry * createVortexCollisionGeometry(DtVortexTriangleOutputInterface &outputInterface, Vx::VxTriangleMeshBVTree *triMeshParam=nullptr)
Creates a collision geometry based on the vertices and the surface.
double myTerrainHeightToleranceCheck
Height tolerance for terrain checks.
Definition vortexPagedTerrain.h:339
bool myWaitForFutureRequests
Whether to wait for future requests to complete.
Definition vortexPagedTerrain.h:344
virtual void update(const std::vector< VxTerrain::VxTerrainRequest > &request, VxTerrain::VxPagedTerrainProducer *producer) override
Called when pending requests for terrain get higher priority.
tbb::spin_mutex myCellsToInsertSwapMutex
Protects swap operation on myCellsToInsert (push_back is thread-safe)
Definition vortexPagedTerrain.h:351
virtual bool modifyTriangleMesh(Vx::VxTriangleMeshBVTree *currentMesh, const Vx::VxTriangleMeshBVTree *newMesh, const int upAxis, double tolerance=0.1)
Modifies a triangle mesh with z values from a new mesh.
DtVortexPagedTerrainInterface()
Default constructor.
std::weak_ptr< DtVortexDynamicTerrainHandler > myDynamicTerrainHandler
Pointer to the dynamic terrain handler.
Definition vortexPagedTerrain.h:332
virtual int generateSourceID(double minXCoordinate, double minYCoordinate)
Generates a source ID given the minimum x and y coordinate of a request.
virtual bool processTerrainRegenerationRequest(const std::pair< int, Vx::VxTriangleMeshBVTree * > &regenerationData)
Processes all dynamic terrain events that have been queued up for regeneration.
tbb::concurrent_vector< VortexCellToModify > myCellsToModify
Cells to modify on the next step.
Definition vortexPagedTerrain.h:349
DtTerrainImplementation * myTerrainImplementation
Pointer to the terrain implementation.
Definition vortexPagedTerrain.h:330
int myDynamicTerrainRegenerationFailureCount
Maximum number of failures before giving up on regeneration.
Definition vortexPagedTerrain.h:341
std::unique_ptr< tbb::task_arena > myDynamicTerrainTaskArena
Task arena for dynamic terrain processing.
Definition vortexPagedTerrain.h:337
virtual ~DtVortexPagedTerrainInterface() override
Destructor.
Producer that manages terrain in the Vortex physics universe.
Definition vortexPagedTerrainProducer.h:31
Main class responsible for managing the Vortex physics simulation environment.
Definition vortexPhysicalWorld.h:55
Interface for collecting and processing terrain triangles for Vortex.
Definition pagedTerrainRequest.h:83
Export macros for the VR-Forces Vortex extension library.
#define VREVRFVORTEX_DLL
Export macro for non-Windows platforms.
Definition export.h:30
Wheel articulated part IDs used in the system.
Definition surfaceToVxMaterialMapper.h:17
Definition vortexDynamicTerrainHandler.h:36
Include export definitions for this library.
Definition glsVreMessageUtil.h:49
Classes for handling terrain triangle data and requests for the Vortex physics engine.
std::shared_ptr< DtVortexTriangleOutputInterface > newTriangleInterface
Triangle Interface.
Definition vortexPagedTerrain.h:98
VxMath::BoundingBox requestBounds
The bounding box of this request.
Definition vortexPagedTerrain.h:97
Vx::VxTriangleMeshBVTree * newTriangleMesh
The triangle mesh.
Definition vortexPagedTerrain.h:99
int sourceId
The source ID this cell collision geometry links to.
Definition vortexPagedTerrain.h:113
Vx::VxCollisionGeometry * collisionGeometry
Collision Geometry.
Definition vortexPagedTerrain.h:114
std::vector< DtSimObjectReference > objects
List of objects that will need to be dropped if this is a dynamic terrain event.
Definition vortexPagedTerrain.h:117
Vx::VxTriangleMeshBVTree * newTriangleData
This is the new triangleData.
Definition vortexPagedTerrain.h:115
Data structure for managing new terrain requests from the Vortex Forecaster.
Definition vortexPagedTerrain.h:74
int priority
Priority level of the request.
Definition vortexPagedTerrain.h:76
int requestId
Unique identifier for the request.
Definition vortexPagedTerrain.h:77
std::shared_future< void > future
Future object for asynchronous processing.
Definition vortexPagedTerrain.h:75
Provides mapping functionality between VR-Forces terrain surface types and Vortex physics materials.
Defines the base class for all VREngage messages.