VR-Engage  2.2
Loading...
Searching...
No Matches
vreRtdUtil.h
Go to the documentation of this file.
1/*******************************************************************************
2** Copyright (c) 2025 MAK Technologies
3** All rights reserved.
4*******************************************************************************/
5
6//! \file vreRtdUtil.h
7//! \ingroup vreVrfRtd
8//! \brief Utility functions for VREngage RTD components
9//!
10//! This file provides common utility functions and macros for coordinate
11//! transformations, terrain queries, and other operations used by the VREngage
12//! Real-Time Dynamics (RTD) components. It includes conversions between
13//! VRF (Virtual Reality Forces) and RTD coordinate systems, orientation
14//! transformations, terrain height calculation, and vehicle property modifiers
15//! such as wind, speed limits, and altitude limits.
16
17#pragma once
18
19#include <Physics/smVec3r.h>
20
22#include <matrix/vltaitbryan.h>
23
24#include <vrfobjcore/localObject.h>
25
26#pragma warning(pop)
27
28
29 class DtVrfObjectStateRepository;
30class DtSimulationServices;
31class DtPhysicalWorld;
32class DtSetDataRequest;
33
34//! \brief Macro to convert geodetic coordinates to local database coordinates
35//! \param geo The geodetic coordinates to convert
36//! \param local The resulting local database coordinates
37//!
38//! Converts geodetic coordinates (latitude, longitude, altitude) to local
39//! database coordinates using the simulation's physical world.
40#define GEODETIC_TO_LOCAL(geo, local) simulationServices()->physicalWorld()->geodeticToDatabase((geo), (local))
41
42//! \brief Macro to convert local database coordinates to geodetic coordinates
43//! \param local The local database coordinates to convert
44//! \param geo The resulting geodetic coordinates
45//!
46//! Converts local database coordinates to geodetic coordinates (latitude, longitude, altitude)
47//! using the simulation's physical world.
48#define LOCAL_TO_GEODETIC(local, geo) simulationServices()->physicalWorld()->databaseToGeodetic((local), (geo));
49
50//! \brief Version-dependent macro for accessing object name properties
51//!
52//! This macro provides version compatibility between different VRF plugin versions.
53//! In version 4.4.0 and later, we use markingText, while in earlier versions we use objectName.
54#if RTD_VRF_PLUGIN_VER >= 440
55 #define VRF_OBJECT_GET_NAME markingText
56#else
57 #define VRF_OBJECT_GET_NAME objectName
58#endif
59
60//! \brief Macro to get a formatted name for an external entity
61//! \param entity Pointer to the entity object
62//! \return A string containing the marking text and name of the entity
63//!
64//! Creates a formatted display name for an external entity that includes both
65//! the marking text (in single quotes) and the entity name (in parentheses).
66#define GET_EXT_ENTITY_NAME(entity) ("'" + (entity)->getMarkingText() + "'" + " (" + (entity)->getName() + ')')
67
68//! \brief Macro to get a formatted name for a VRF object
69//! \param object Pointer to the VRF object
70//! \return A string containing the marking text and UUID of the object
71//!
72//! Creates a formatted display name for a VRF object that includes both
73//! the marking text (in single quotes) and the object's UUID (in parentheses).
74#define GET_EXT_VRF_OBJ_NAME(object) \
75 ("'" + (object)->markingText().stdString() + "'" + " (" + (object)->uuid().uuidString().stdString() + ')')
76
77
78namespace vreRtd
79{
80//! \brief Utility functions for VREngage Real-Time Dynamics (RTD) components
81//!
82//! This namespace contains various utility functions for coordinate transformations,
83//! terrain queries, and other operations used by the VREngage RTD components.
84
85//! \brief Sets dead reckoning thresholds for an entity
86//! \param entity The entity to set thresholds for
87//! \param transThreshold Translational threshold in meters
88//! \param rotationThreshold Rotational threshold in radians
89//!
90//! Sets the translational and rotational thresholds used for dead reckoning
91//! calculations for the specified entity. These thresholds control when
92//! position updates are sent across the network.
93void setDeadreckoningThresholds(DtLocalObject* entity, double transThreshold, double rotationThreshold);
94
95//! \brief Converts from a VRF/DtTaitBryan orientation to RTD/HPR
96//! \param ori The VRF orientation to convert
97//! \return The equivalent RTD HPR orientation
98//!
99//! Converts from a VRF/DtTaitBryan orientation to RTD/HPR orientation.
100//! DtTaitBryan orientation is expected to be a rotation in the coordinate system with x straight, y right, z down.
101//! HPR is expected to be a rotation in the coordinate system with y straight, x right, z up.
102//! A DtTaitBryan(0,0,0) is hpr(0,0,0). A DtTaitBryan(pi/2,0,0) is hpr(-90,0,0) for example.
103smVec3r VRFOriToRTDHPR(const DtTaitBryan& ori);
104//! \brief Converts from an RTD/HPR orientation to VRF/DtTaitBryan
105//! \param hpr The RTD HPR orientation to convert
106//! \return The equivalent VRF/DtTaitBryan orientation
107//!
108//! Inverse transform of VRFOriToRTDHPR, converting from RTD HPR to VRF orientation.
109DtTaitBryan HprToVRFOri(const smVec3r& hpr);
110
111//! \brief Converts airplane world orientation from VRF to RTD HPR
112//! \param ori The VRF world orientation to convert
113//! \return The equivalent RTD world HPR orientation
114//!
115//! Converts airplane world orientation from VRF to RTD HPR. Even if VRF world coordinate
116//! system and RTD world coordinate system are both WGS84, airplanes are differently oriented
117//! in their local coordinate systems:
118//! - In RTD, airplanes point along y-axis with z-axis up
119//! - In VRF, airplanes point along x-axis with z-axis down
120//!
121//! An RTD airplane with zero rotation (in world coordinates) points to WGS84 y-axis with
122//! 'up' pointing along WGS84 z-axis. A VRF airplane with zero rotation (in world coordinates)
123//! points to WGS84 x-axis with 'up' pointing along WGS84 -z-axis (upside down).
124smVec3r VRFWorldOriToRTDWorldHPR(const DtTaitBryan& ori);
125
126//! \brief Converts airplane world orientation from RTD HPR to VRF
127//! \param hpr The RTD world HPR orientation to convert
128//! \return The equivalent VRF world orientation
129//!
130//! Inverse of VRFWorldOriToRTDWorldHPR, converting from RTD world HPR to VRF world orientation.
131DtTaitBryan RTDWorldHprToVRFWorldOri(const smVec3r& hpr);
132
133//! \brief Converts rotational velocity from VRF to RTD world coordinate system
134//! \param entityWorldOrientation The entity's world orientation
135//! \param rotationalVelocity The rotational velocity in VRF coordinate system
136//! \return The equivalent rotational velocity in RTD world coordinate system
137//!
138//! Converts a rotational velocity vector from VRF to RTD world coordinate system.
139smVec3r VRFRotVelToRTDRotVelWCS(const DtTaitBryan& entityWorldOrientation, const DtVector& rotationalVelocity);
140
141//! \brief Converts rotational velocity from RTD to VRF coordinate system
142//! \param entityWorldOrientation The entity's world orientation
143//! \param rotationalVelocity The rotational velocity in RTD world coordinate system
144//! \return The equivalent rotational velocity in VRF coordinate system
145//!
146//! Converts a rotational velocity vector from RTD world coordinate system to VRF.
147DtVector RTDRotVelWCSToVRFRotVel(const DtTaitBryan& entityWorldOrientation, const smVec3r& rotationalVelocity);
148
149//! \brief Converts topographic orientation to local orientation
150//! \param physicalWorld Pointer to the physical world
151//! \param localPosition The local position to convert at
152//! \param topographicOrientation The topographic orientation to convert
153//! \return The equivalent local orientation with respect to the database
154//!
155//! Converts a topographic orientation to a local orientation with respect to the database.
157 DtPhysicalWorld* physicalWorld, const DtVector& localPosition, const DtTaitBryan& topographicOrientation);
158
159//! \brief Converts topographic vector to local vector
160//! \param physicalWorld Pointer to the physical world
161//! \param localPosition The local position to convert at
162//! \param vec The topographic vector to convert
163//! \return The equivalent local vector with respect to the database
164//!
165//! Converts a topographic vector to a local vector with respect to the database.
166DtVector TopoToLocalWrtDatabase(DtPhysicalWorld* physicalWorld, const DtVector& localPosition, const DtVector& vec);
167
168//! \brief Converts local orientation to topographic orientation
169//! \param physicalWorld Pointer to the physical world
170//! \param localPosition The local position to convert at
171//! \param localOrientation The local orientation to convert
172//! \return The equivalent topographic orientation
173//!
174//! Converts a local orientation with respect to the database to a topographic orientation.
176 DtPhysicalWorld* physicalWorld, const DtVector& localPosition, const DtTaitBryan& localOrientation);
177
178//! \brief Computes the 2D distance between two points
179//! \param pnt1 The first point
180//! \param pnt2 The second point
181//! \return The 2D distance between the two points
182//!
183//! Computes the 2D distance between two points.
184smReal compute2DDist(const smVec3r& pnt1, const DtVector& pnt2);
185
186//! \brief Gets the heading-aligned speed vector
187//! \param hpr The heading, pitch, and roll of the entity
188//! \param speed The speed of the entity
189//! \param xyOnly Whether to only consider the x and y components
190//! \return The heading-aligned speed vector
191//!
192//! Gets the heading-aligned speed vector for an entity.
193smVec3r getHeadingAlignedSpeedVector(const smVec3r& hpr, smReal speed, bool xyOnly = false);
194
195//! \brief Gets the heading-aligned speed vector for an airplane
196//! \param bot The airplane entity
197//! \param speed The speed of the airplane
198//! \param xyOnly Whether to only consider the x and y components
199//! \return The heading-aligned speed vector
200//!
201//! Gets the heading-aligned speed vector for an airplane entity.
202smVec3r getHeadingAlignedSpeedVector(FixedWingLibCGF::AirplaneBot* bot, smReal speed, bool xyOnly = false);
203
204//! \brief Gets a vector matching the direction of an entity
205//! \param physicalWorld Pointer to the physical world
206//! \param sr Pointer to the object state repository
207//! \param vecMagnitude The magnitude of the vector
208//! \return The vector matching the direction of the entity
209//!
210//! Gets a vector matching the direction of an entity.
211DtVector getVecMatchingDirection(DtPhysicalWorld* physicalWorld, DtVrfObjectStateRepository* sr, DtReal vecMagnitude);
212
213//! \brief Applies an altitude limit to a vehicle
214//! \param vehicle The vehicle to apply the limit to
215//! \param maxAltitude The maximum altitude
216//!
217//! Applies an altitude limit to a vehicle.
218void applyAltitudeLimit(RTD::VehicleBase* vehicle, const smReal& maxAltitude);
219
220//! \brief Applies a speed limit to a vehicle
221//! \param vehicle The vehicle to apply the limit to
222//! \param maxSpeed The maximum speed
223//!
224//! Applies a speed limit to a vehicle.
225void applySpeedLimit(RTD::VehicleBase* vehicle, const smReal& maxSpeed);
226
227//! \brief Applies wind to a vehicle
228//! \param physicalWorld Pointer to the physical world
229//! \param vehicle The vehicle to apply wind to
230//!
231//! Applies wind to a vehicle.
232void applyWind(DtPhysicalWorld* physicalWorld, RTD::VehicleBase* vehicle);
233
234//! \brief Applies wind to a vehicle
235//! \param vehicle The vehicle to apply wind to
236//! \param vrfWindDir The wind direction in VRF coordinates
237//! \param vrfWindSpeed The wind speed
238//!
239//! Applies wind to a vehicle.
240void applyWind(RTD::VehicleBase* vehicle, double vrfWindDir, double vrfWindSpeed);
241
242//! \brief Macro to convert an RTD heading to a VRF heading
243//! \param hdgInDeg RTD heading in degrees
244//! \return VRF heading in degrees
245//!
246//! Converts a heading from RTD coordinate system to VRF coordinate system.
247//! In VRF, heading increases clockwise from north, while in RTD, heading increases counterclockwise from north.
248#define RTDHeadingToVrfHeading(hdgInDeg) (-hdgInDeg)
249
250//! \brief Macro to convert a VRF heading to an RTD heading
251//! \param hdgInDeg VRF heading in degrees
252//! \return RTD heading in degrees
253//!
254//! Converts a heading from VRF coordinate system to RTD coordinate system.
255//! In VRF, heading increases clockwise from north, while in RTD, heading increases counterclockwise from north.
256#define VrfHeadingToRTDHeading(hdgInDeg) (-hdgInDeg)
257
258//! \brief Macro to convert a vector to DtVector
259//! \param v Vector to convert
260//! \return Equivalent DtVector
261#define toDtVector(v) DtVector((v)[0], (v)[1], (v)[2])
262
263//! \brief Macro to convert a vector to smVec3r (single precision 3D vector)
264//! \param v Vector to convert
265//! \return Equivalent smVec3r
266#define toSmVec3r(v) smVec3r((v)[0], (v)[1], (v)[2])
267
268//! \brief Macro to convert a vector to smVec2r (single precision 2D vector)
269//! \param v Vector to convert
270//! \return Equivalent smVec2r
271#define toSmVec2r(v) smVec2r((v)[0], (v)[1])
272
273//! \brief Macro to convert a vector to smVec3d (double precision 3D vector)
274//! \param v Vector to convert
275//! \return Equivalent smVec3d
276#define toSmVec3d(v) smVec3d((v)[0], (v)[1], (v)[2])
277
278//! \brief Macro to convert a vector to smVec2d (double precision 2D vector)
279//! \param v Vector to convert
280//! \return Equivalent smVec2d
281#define toSmVec2d(v) smVec2d((v)[0], (v)[1])
282
283//! \brief Macro to convert a geodetic coordinate to RTD LLA (Latitude, Longitude, Altitude)
284//! \param geodeticCoord Geodetic coordinate in radians with altitude in meters
285//! \return RTD LLA with latitude and longitude in degrees, altitude in meters
286#define toRTDLLA(geodeticCoord) \
287 smVec3r(smMath::radToDeg(geodeticCoord[0]), smMath::radToDeg(geodeticCoord[1]), geodeticCoord[2])
288
289//! \brief Macro to convert a geodetic coordinate to RTD LL (Latitude, Longitude)
290//! \param geodeticCoord Geodetic coordinate in radians
291//! \return RTD LL with latitude and longitude in degrees
292#define toRTDLL(geodeticCoord) smVec2r(smMath::radToDeg(geodeticCoord[0]), smMath::radToDeg(geodeticCoord[1]))
293
294//! \brief Macro to convert an RTD LLA to geodetic coordinate
295//! \param lla RTD LLA with latitude and longitude in degrees, altitude in meters
296//! \return Geodetic coordinate in radians with altitude in meters
297#define toGeodetic(lla) DtGeodeticCoord(smMath::degToRad(lla[0]), smMath::degToRad(lla[1]), lla[2])
298
299//! \brief Computes the terrain height at a specified local position
300//! \param simMan Pointer to the simulation services
301//! \param localPosition Local position to query terrain height at
302//! \param result Reference where the computed terrain height will be stored
303//! \return True if terrain height computation was successful, false otherwise
304//!
305//! Uses the terrain provider to compute the height of terrain at the specified local position.
306//! Returns false if the terrain query fails or the position is outside the terrain bounds.
307bool computeTerrainHeight(const DtSimulationServices* simMan, const DtVector& localPosition, double& result);
308
309//! \brief Gets the terrain height at a specified local position
310//! \param simMan Pointer to the simulation services
311//! \param localPosition Local position to query terrain height at
312//! \return Terrain height in meters, or 0.0 if computation fails
313//!
314//! Convenience function that calls computeTerrainHeight and returns the result directly.
315//! Returns 0.0 if the terrain height could not be determined.
316double getTerrainHeight(const DtSimulationServices* simMan, const DtVector& localPosition);
317
318//! \brief Gets the height above terrain (HAT) for a geodetic position
319//! \param simMan Pointer to the simulation services
320//! \param pos Geodetic position to query HAT for
321//! \return Height above terrain in meters
322//!
323//! Calculates the height above terrain by converting the geodetic position to local coordinates,
324//! determining the terrain height at that location, and subtracting it from the position's altitude.
325double getHAT(const DtSimulationServices* simMan, const DtGeodeticCoord& pos);
326
327} // namespace vreRtd
328
329//! \brief RTD namespace forward declarations
330namespace RTD
331{
332//! \brief World class for RTD simulation environment
333class World;
334} // namespace RTD
335
336//! \brief Forward declarations for the FixedWingLibCGF namespace
337namespace FixedWingLibCGF
338{
339//! \brief Class representing a fixed-wing aircraft in CGF
340class CGFAirplane;
341} // namespace FixedWingLibCGF
342
343//! \brief Forward declarations for the RotorLibCGF namespace
344namespace RotorLibCGF
345{
346//! \brief Class representing a helicopter in CGF
347class CGFHelicopter;
348} // namespace RotorLibCGF
349
350//! \brief Macro to check an expression and return if false, logging a console warning
351//! \param expression The expression to check
352//! \param complaint The message to log if the expression is false
353//!
354//! Evaluates the given expression and, if false, logs a warning to the object console
355//! and returns from the current function (with no return value).
356#define RTD_CHECK_CONSWARN_RETURN(expression, complaint) \
357 if (!(expression)) \
358 { \
359 objectConsoleWarn() << (complaint) << std::endl; \
360 return; \
361 }
362
363//! \brief Macro to check an expression and return if false, logging a DtWarn
364//! \param expression The expression to check
365//! \param complaint The message to log if the expression is false
366//!
367//! Evaluates the given expression and, if false, logs a warning using DtWarn
368//! and returns from the current function (with no return value).
369#define RTD_CHECK_DTWARN_RETURN(expression, complaint) \
370 if (!(expression)) \
371 { \
372 DtWarn(complaint "\n"); \
373 return; \
374 }
375
376//! \brief Macro to check an expression and return a value if false, logging a console warning
377//! \param expression The expression to check
378//! \param complaint The message to log if the expression is false
379//! \param retVal The value to return if the expression is false
380//!
381//! Evaluates the given expression and, if false, logs a warning to the object console
382//! and returns the specified value from the current function.
383#define RTD_CHECK_CONSWARN_RETURN2(expression, complaint, retVal) \
384 if (!(expression)) \
385 { \
386 objectConsoleWarn() << (complaint) << std::endl; \
387 return retVal; \
388 }
389
390//! \brief Macro to check an expression and return a value if false, logging a DtWarn
391//! \param expression The expression to check
392//! \param complaint The message to log if the expression is false
393//! \param retVal The value to return if the expression is false
394//!
395//! Evaluates the given expression and, if false, logs a warning using DtWarn
396//! and returns the specified value from the current function.
397#define RTD_CHECK_DTWARN_RETURN2(expression, complaint, retVal) \
398 if (!(expression)) \
399 { \
400 DtWarn(complaint "\n"); \
401 return retVal; \
402 }
Forward declarations for the FixedWingLibCGF namespace.
Definition vreFwlActuator.h:30
RTD namespace forward declarations.
Definition vreRtdUtil.h:331
Forward declarations for the RotorLibCGF namespace.
Definition vreRlActuator.h:35
Definition vreRtdConfigFileReader.h:18
DtTaitBryan TopoToLocalWrtDatabase(DtPhysicalWorld *physicalWorld, const DtVector &localPosition, const DtTaitBryan &topographicOrientation)
Converts topographic orientation to local orientation.
bool computeTerrainHeight(const DtSimulationServices *simMan, const DtVector &localPosition, double &result)
Computes the terrain height at a specified local position.
void applyAltitudeLimit(RTD::VehicleBase *vehicle, const smReal &maxAltitude)
Applies an altitude limit to a vehicle.
smVec3r VRFWorldOriToRTDWorldHPR(const DtTaitBryan &ori)
Converts airplane world orientation from VRF to RTD HPR.
void applySpeedLimit(RTD::VehicleBase *vehicle, const smReal &maxSpeed)
Applies a speed limit to a vehicle.
DtTaitBryan LocalWrtDatabaseToTopo(DtPhysicalWorld *physicalWorld, const DtVector &localPosition, const DtTaitBryan &localOrientation)
Converts local orientation to topographic orientation.
DtTaitBryan RTDWorldHprToVRFWorldOri(const smVec3r &hpr)
Converts airplane world orientation from RTD HPR to VRF.
smVec3r getHeadingAlignedSpeedVector(const smVec3r &hpr, smReal speed, bool xyOnly=false)
Gets the heading-aligned speed vector.
smReal compute2DDist(const smVec3r &pnt1, const DtVector &pnt2)
Computes the 2D distance between two points.
smVec3r VRFOriToRTDHPR(const DtTaitBryan &ori)
Converts from a VRF/DtTaitBryan orientation to RTD/HPR.
void applyWind(DtPhysicalWorld *physicalWorld, RTD::VehicleBase *vehicle)
Applies wind to a vehicle.
double getTerrainHeight(const DtSimulationServices *simMan, const DtVector &localPosition)
Gets the terrain height at a specified local position.
DtVector getVecMatchingDirection(DtPhysicalWorld *physicalWorld, DtVrfObjectStateRepository *sr, DtReal vecMagnitude)
Gets a vector matching the direction of an entity.
DtTaitBryan HprToVRFOri(const smVec3r &hpr)
Converts from an RTD/HPR orientation to VRF/DtTaitBryan.
smVec3r VRFRotVelToRTDRotVelWCS(const DtTaitBryan &entityWorldOrientation, const DtVector &rotationalVelocity)
Converts rotational velocity from VRF to RTD world coordinate system.
void setDeadreckoningThresholds(DtLocalObject *entity, double transThreshold, double rotationThreshold)
Utility functions for VREngage Real-Time Dynamics (RTD) components.
double getHAT(const DtSimulationServices *simMan, const DtGeodeticCoord &pos)
Gets the height above terrain (HAT) for a geodetic position.
DtVector RTDRotVelWCSToVRFRotVel(const DtTaitBryan &entityWorldOrientation, const smVec3r &rotationalVelocity)
Converts rotational velocity from RTD to VRF coordinate system.
RTDVRFConfigWarnsForExternalIncludes() class DtVrfObjectStateRepository