VR-Engage  2.2
Loading...
Searching...
No Matches
fsUtil.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2024 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file fsUtil.h
7//! \brief Unit conversion utilities for flight simulation cockpit displays
8//!
9//! This file provides constants and inline functions for common unit conversions
10//! used in fixed-wing and rotary-wing GL Studio cockpit projects. Includes
11//! conversions between metric and imperial units for altitude, speed, and distance.
12
13#pragma once
14
16
17#include <vrvCore/DtDisplayUnitsSettingsManager.h>
18
19#include <string>
20#include <vector>
21#include <iostream> // std::fixed
22#include <iomanip> // std::setprecision
23#include <matrix/vlVector.h>
24#include <matrix/topoCoord.h>
25
26//! Contains constants and utility functions used in Fixed Wing and Rotary Wing
27//! GLS projects.
28
29static const float radToDegValue = 57.2958f;
30static const float degToRadValue = 1.0f / radToDegValue;
31
32static const float metersToFeetValue = 3.28084f;
33static const float feetToMetersValue = 1.0f / metersToFeetValue;
34
35static const float nauticalMilesToMetersValue = 1852.0f;
37
39
40//format is "azimuth;range;elevation;markingText;trackId;screenX;screenY;isDesignated"
41static const int radarContactElements = 8;
42
43inline float radToDeg(float value) { return value * radToDegValue; };
44inline float degToRad(float value) { return value * degToRadValue; };
45
46inline float metersToFeet(float value) { return value * metersToFeetValue; };
47inline float feetToMeters(float value) { return value * feetToMetersValue; };
48
49inline float nauticalMilesToMeters(float value) { return value * nauticalMilesToMetersValue; };
50inline float metersToNauticalMiles(float value) { return value * metersToNauticalMilesValue; };
51
52inline float metersPerSecondToKnots(float value) { return value * metersPerSecondToKnotsValue; };
53
54#pragma warning (push)
55#pragma warning (disable:4505) //These functions are not used by all modules. Disable this warning.
56
57static std::string metersTo10kFeetText(float value)
58{
59 int altitudeIn10kFeet = metersToFeet(value) / 1000;
60 std::stringstream ss;
61 ss << altitudeIn10kFeet;
62 return ss.str();
63}
64
65static void split(const std::string& str, char delimeter, std::vector<std::string>& tokens)
66{
67 std::stringstream ss(str);
68 std::string item;
69 while (std::getline(ss, item, delimeter))
70 {
71 tokens.push_back(item);
72 }
73}
74
75static std::string numberToString(double num, int decimalPrecision)
76{
77 std::stringstream ss;
78 ss << std::fixed << std::setprecision(decimalPrecision) << num;
79 return ss.str();
80}
81
82//! Calculate the closing speed of two objects by their position and velocity vectors.
83//! C = -((v1-v2)•(p1-p2)/|p1-p2|)
84static double closingSpeed(const DtVector& p1, const DtVector& p2, const DtVector32& v1, const DtVector32& v2)
85{
86 DtVector positionDifference = p1 - p2;
87 DtVector velocityDifference = DtVector32To64(v1 - v2);
88 return -((velocityDifference).dotProduct(positionDifference)/sqrt(positionDifference.magnitudeSquared()));
89}
90
91static double headingAngleTo(const DtVector& geocentricObserverPosition, const DtVector& geocentricTargetPosition)
92{
93 DtGeodeticCoord geodeticObserverPosition;
94 geodeticObserverPosition.setGeocentric(geocentricObserverPosition);
95
96 DtDcm geocToTopo;
97 DtLatLon_to_GeocToTopo(geodeticObserverPosition, geocToTopo);
98
99 // Compute vector from observer to target (in geocentric coordinates)
100 DtVector observerToTarget = geocentricTargetPosition -
101 geocentricObserverPosition;
102
103 // Transform to topographic coordinates
104 DtDcmVecMul(geocToTopo, observerToTarget, observerToTarget);
105
106 // Compute heading from topographic observer to target vector
107 // Heading is measured as positive rotation about topo-centric Z axis
108 // constrain to 0->2pi
109 double heading = atan2(observerToTarget[DtY], observerToTarget[DtX]);
110
111 // Convert to current North units (magnetic or true North).
112 makVrv::DtDisplayUnitsConverter converter = makVrv::DtDisplayUnitsSettingsManager::instance(makVre::DtPlayerStationApp::instance()->de()).converter();
113 makArchives::DtDisplayUnitsSettingsRecord::NorthUnits northUnits = converter.currentNorthUnit(
114 makVrv::DtSharedDisplayUnitsSettings::theApplicationDisplayUnitCollectionName,
115 makVrv::DtSharedDisplayUnitsSettings::theHeadingDisplayUnitName);
116
117 return DtModPerLo(converter.convertHeadingUnitNativeToDisplay(geocentricObserverPosition, heading, northUnits), 0, 360);
118}
119
120//! Returns the heading of an entity according to its world position and orientation.
121static float entityHeading(const DtVector& worldPosition, const DtTaitBryan& worldOrientnation)
122{
123 return DtModPerLo(radToDeg(DtGetHeadingFromGeocentric(worldPosition, worldOrientnation)), 0, 360);
124}
125
126#pragma warning (pop)
127
128//! \brief Returns what ever is greater, x or y of type T
129template<typename T>
130T maximum(T x,T y)
131{
132 return (x>y ? x:y);
133}
134
135//! \brief Returns the which ever is lesser, x or y of type T
136template<typename T>
137T minimum(T x,T y)
138{
139 return (x<y ? x:y);
140}
static DtPlayerStationApp * instance()
Gets the singleton instance of this class.
static double headingAngleTo(const DtVector &geocentricObserverPosition, const DtVector &geocentricTargetPosition)
Definition fsUtil.h:91
static float entityHeading(const DtVector &worldPosition, const DtTaitBryan &worldOrientnation)
Returns the heading of an entity according to its world position and orientation.
Definition fsUtil.h:121
T maximum(T x, T y)
Returns what ever is greater, x or y of type T.
Definition fsUtil.h:130
float metersPerSecondToKnots(float value)
Definition fsUtil.h:52
static const float degToRadValue
Definition fsUtil.h:30
static const int radarContactElements
Definition fsUtil.h:41
static const float radToDegValue
Contains constants and utility functions used in Fixed Wing and Rotary Wing GLS projects.
Definition fsUtil.h:29
static std::string numberToString(double num, int decimalPrecision)
Definition fsUtil.h:75
static std::string metersTo10kFeetText(float value)
Definition fsUtil.h:57
static const float metersToNauticalMilesValue
Definition fsUtil.h:36
float feetToMeters(float value)
Definition fsUtil.h:47
T minimum(T x, T y)
Returns the which ever is lesser, x or y of type T.
Definition fsUtil.h:137
float radToDeg(float value)
Definition fsUtil.h:43
float metersToNauticalMiles(float value)
Definition fsUtil.h:50
float nauticalMilesToMeters(float value)
Definition fsUtil.h:49
float degToRad(float value)
Definition fsUtil.h:44
static void split(const std::string &str, char delimeter, std::vector< std::string > &tokens)
Definition fsUtil.h:65
static double closingSpeed(const DtVector &p1, const DtVector &p2, const DtVector32 &v1, const DtVector32 &v2)
Calculate the closing speed of two objects by their position and velocity vectors....
Definition fsUtil.h:84
static const float metersPerSecondToKnotsValue
Definition fsUtil.h:38
static const float feetToMetersValue
Definition fsUtil.h:33
static const float metersToFeetValue
Definition fsUtil.h:32
static const float nauticalMilesToMetersValue
Definition fsUtil.h:35
float metersToFeet(float value)
Definition fsUtil.h:46
Defines the DtPlayerStationApp class for the VR-Engage application.
makVrv::DtDe * de()
Gets the display element pointer used by the plugin.