mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-14 16:55:36 +08:00
Ref T709, improved normalization and space pressed handling, unit test
This commit is contained in:
committed by
Mat Sutcliffe
parent
9cb9556d87
commit
4830bbc646
@@ -92,6 +92,16 @@ namespace BlackMisc
|
|||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! Normalize value to range start -> end (like for +-180degrees)
|
||||||
|
inline double normalizeValue(const double value, const double start, const double end)
|
||||||
|
{
|
||||||
|
// https://stackoverflow.com/questions/1628386/normalise-orientation-between-0-and-360
|
||||||
|
if (value >= start && value <= end) { return value; }
|
||||||
|
const double width = end - start ;
|
||||||
|
const double offsetValue = value - start; // value relative to 0
|
||||||
|
return (offsetValue - (floor(offsetValue / width) * width)) + start ;
|
||||||
|
}
|
||||||
|
|
||||||
//! ACF properties
|
//! ACF properties
|
||||||
struct AcfProperties
|
struct AcfProperties
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
#include <XPLM/XPLMUtilities.h>
|
#include <XPLM/XPLMUtilities.h>
|
||||||
#include <XPLM/XPLMPlanes.h>
|
#include <XPLM/XPLMPlanes.h>
|
||||||
#include <XPLM/XPLMPlugin.h>
|
#include <XPLM/XPLMPlugin.h>
|
||||||
|
#include "blackmisc/simulation/xplane/qtfreeutils.h"
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
@@ -30,6 +31,8 @@
|
|||||||
|
|
||||||
// clazy:excludeall=reserve-candidates
|
// clazy:excludeall=reserve-candidates
|
||||||
|
|
||||||
|
using namespace BlackMisc::Simulation::XPlane::QtFreeUtils;
|
||||||
|
|
||||||
namespace XSwiftBus
|
namespace XSwiftBus
|
||||||
{
|
{
|
||||||
CTraffic::Plane::Plane(void *id_, const std::string &callsign_, const std::string &aircraftIcao_, const std::string &airlineIcao_, const std::string &livery_, const std::string &modelName_)
|
CTraffic::Plane::Plane(void *id_, const std::string &callsign_, const std::string &aircraftIcao_, const std::string &airlineIcao_, const std::string &livery_, const std::string &modelName_)
|
||||||
@@ -918,7 +921,7 @@ namespace XSwiftBus
|
|||||||
|
|
||||||
// Ideally we would like to test against right mouse button, but X-Plane SDK does not
|
// Ideally we would like to test against right mouse button, but X-Plane SDK does not
|
||||||
// allow that.
|
// allow that.
|
||||||
if (!traffic->m_deltaCameraPosition.isInitialized || traffic->m_isSpacePressed)
|
if (!traffic->m_deltaCameraPosition.isInitialized)
|
||||||
{
|
{
|
||||||
int w = 0, h = 0, x = 0, y = 0;
|
int w = 0, h = 0, x = 0, y = 0;
|
||||||
// First get the screen size and mouse location. We will use this to decide
|
// First get the screen size and mouse location. We will use this to decide
|
||||||
@@ -936,11 +939,17 @@ namespace XSwiftBus
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
traffic->m_deltaCameraPosition.headingDeg = 360.0 * static_cast<double>(x) / static_cast<double>(w);
|
if (x < 1 || y < 1 || x >= w || y >= h)
|
||||||
double usedCameraPitchDeg = 20.0 * ((static_cast<double>(y) / static_cast<double>(h)) * 2.0 - 1.0);
|
{
|
||||||
|
WARNING_LOG("Screen w/h, and x/y" + std::to_string(w) + "/" + std::to_string(h) + " | " + std::to_string(x) + "/" + std::to_string(y));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
// make sure we can use it with tan in range +-90 degrees
|
traffic->m_deltaCameraPosition.headingDeg = normalizeToZero360DegD(360.0 * static_cast<double>(x) / static_cast<double>(w)); // range 0-360
|
||||||
// we limit to +-85deg
|
double usedCameraPitchDeg = 60.0 - (60.0 * 2.0 * static_cast<double>(y) / static_cast<double>(h)); // range +-
|
||||||
|
|
||||||
|
// make sure we can use it with tan in range +-90 degrees and the result of tan not getting too high
|
||||||
|
// we limit to +-85deg, tan 45deg: 1 | tan 60deg: 1.73 | tan 85deg: 11.4
|
||||||
if (usedCameraPitchDeg >= 85.0) { usedCameraPitchDeg = 85.0; }
|
if (usedCameraPitchDeg >= 85.0) { usedCameraPitchDeg = 85.0; }
|
||||||
else if (usedCameraPitchDeg <= -85.0) { usedCameraPitchDeg = -85.0; }
|
else if (usedCameraPitchDeg <= -85.0) { usedCameraPitchDeg = -85.0; }
|
||||||
traffic->m_deltaCameraPosition.pitchDeg = usedCameraPitchDeg;
|
traffic->m_deltaCameraPosition.pitchDeg = usedCameraPitchDeg;
|
||||||
@@ -1071,8 +1080,10 @@ namespace XSwiftBus
|
|||||||
// We are only interested in Space key
|
// We are only interested in Space key
|
||||||
if (virtualKey == XPLM_VK_SPACE)
|
if (virtualKey == XPLM_VK_SPACE)
|
||||||
{
|
{
|
||||||
if (flags & xplm_DownFlag) { traffic->m_isSpacePressed = true; }
|
// if XPlane looses focus it can happen that key down is NOT reset
|
||||||
if (flags & xplm_UpFlag) { traffic->m_isSpacePressed = false; }
|
// for the camera we use the init flag instead, so it is only run once
|
||||||
|
if (flags & xplm_DownFlag) { traffic->m_isSpacePressed = true; traffic->m_deltaCameraPosition.isInitialized = false; }
|
||||||
|
if (flags & xplm_UpFlag) { traffic->m_isSpacePressed = false; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Return 1 to pass the keystroke to plugin windows and X-Plane.
|
/* Return 1 to pass the keystroke to plugin windows and X-Plane.
|
||||||
@@ -1111,17 +1122,32 @@ namespace XSwiftBus
|
|||||||
float CTraffic::normalizeToPlusMinus180DegF(float v)
|
float CTraffic::normalizeToPlusMinus180DegF(float v)
|
||||||
{
|
{
|
||||||
if (std::isnan(v)) { return 0.0f; }
|
if (std::isnan(v)) { return 0.0f; }
|
||||||
if (v > 180.0f) { return v - 360.0f;}
|
return static_cast<float>(normalizeToPlusMinus180DegD(static_cast<double>(v)));
|
||||||
if (v <= -180.0f) { return v + 360.0f;}
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double CTraffic::normalizeToPlusMinus180DegD(double v)
|
double CTraffic::normalizeToPlusMinus180DegD(double v)
|
||||||
{
|
{
|
||||||
if (std::isnan(v)) { return 0.0; }
|
if (std::isnan(v)) { return 0.0; }
|
||||||
if (v > 180.0) { return v - 360.0;}
|
const double n = normalizeValue(v, -180.0, 180.0);
|
||||||
if (v <= -180.0) { return v + 360.0;}
|
if (n <= -180.0) { return 180.0; }
|
||||||
return v;
|
if (n > 180.0) { return 180.0; }
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
float CTraffic::normalizeToZero360DegF(float v)
|
||||||
|
{
|
||||||
|
if (std::isnan(v)) { return 0.0f; }
|
||||||
|
return static_cast<float>(normalizeToZero360DegD(static_cast<double>(v)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
double CTraffic::normalizeToZero360DegD(double v)
|
||||||
|
{
|
||||||
|
if (std::isnan(v)) { return 0.0; }
|
||||||
|
const double n = normalizeValue(v, 0, 360.0);
|
||||||
|
if (n >= 360.0) { return 0.0;}
|
||||||
|
if (n < 0.0) { return 0.0;}
|
||||||
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CTraffic::isValidPosition(const XPMPPlanePosition_t &position)
|
bool CTraffic::isValidPosition(const XPMPPlanePosition_t &position)
|
||||||
|
|||||||
@@ -196,9 +196,11 @@ namespace XSwiftBus
|
|||||||
static bool isZeroTo360(double v);
|
static bool isZeroTo360(double v);
|
||||||
//! @}
|
//! @}
|
||||||
|
|
||||||
//! Normalize to (-180, 180] degress @{
|
//! Normalize to (-180, 180] or [0, 360) degrees @{
|
||||||
static float normalizeToPlusMinus180DegF(float v);
|
static float normalizeToPlusMinus180DegF(float v);
|
||||||
static double normalizeToPlusMinus180DegD(double v);
|
static double normalizeToPlusMinus180DegD(double v);
|
||||||
|
static float normalizeToZero360DegF(float v);
|
||||||
|
static double normalizeToZero360DegD(double v);
|
||||||
//! @}
|
//! @}
|
||||||
|
|
||||||
//! Check the position if values are valid @{
|
//! Check the position if values are valid @{
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ namespace BlackMiscTest
|
|||||||
void splitTest();
|
void splitTest();
|
||||||
void acfPropertiesTest();
|
void acfPropertiesTest();
|
||||||
void xSwiftBusSettingsTest();
|
void xSwiftBusSettingsTest();
|
||||||
|
void qtFreeUtils();
|
||||||
};
|
};
|
||||||
|
|
||||||
void CTestXPlane::getFileNameTest()
|
void CTestXPlane::getFileNameTest()
|
||||||
@@ -141,6 +142,40 @@ namespace BlackMiscTest
|
|||||||
QCOMPARE(s.getDBusServerAddressQt(), s2.getDBusServerAddressQt());
|
QCOMPARE(s.getDBusServerAddressQt(), s2.getDBusServerAddressQt());
|
||||||
QVERIFY2(s2.getNightTextureModeQt() == "foo", "Expect lower case foo");
|
QVERIFY2(s2.getNightTextureModeQt() == "foo", "Expect lower case foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CTestXPlane::qtFreeUtils()
|
||||||
|
{
|
||||||
|
double vOut;
|
||||||
|
vOut = normalizeValue(77.0, 0.0, 360.0);
|
||||||
|
QVERIFY2(qFuzzyCompare(77.0, vOut), "Wrong normalize 0-360");
|
||||||
|
|
||||||
|
vOut = normalizeValue(361.0, 0.0, 360.0);
|
||||||
|
QVERIFY2(qFuzzyCompare(1.0, vOut), "Wrong normalize 0-360");
|
||||||
|
|
||||||
|
vOut = normalizeValue(-10.0, 0.0, 360.0);
|
||||||
|
QVERIFY2(qFuzzyCompare(350.0, vOut), "Wrong normalize 0-360");
|
||||||
|
|
||||||
|
vOut = normalizeValue(-370.0, 0.0, 360.0);
|
||||||
|
QVERIFY2(qFuzzyCompare(350.0, vOut), "Wrong normalize 0-360");
|
||||||
|
|
||||||
|
vOut = normalizeValue(-180.0, 0.0, 360.0);
|
||||||
|
QVERIFY2(qFuzzyCompare(180.0, vOut), "Wrong normalize 0-360");
|
||||||
|
|
||||||
|
vOut = normalizeValue(-10.0, -180.0, 180.0);
|
||||||
|
QVERIFY2(qFuzzyCompare(-10.0, vOut), "Wrong normalize +-180");
|
||||||
|
|
||||||
|
vOut = normalizeValue(100.0, -180.0, 180.0);
|
||||||
|
QVERIFY2(qFuzzyCompare(100.0, vOut), "Wrong normalize +-180");
|
||||||
|
|
||||||
|
vOut = normalizeValue(190.0, -180.0, 180.0);
|
||||||
|
QVERIFY2(qFuzzyCompare(-170.0, vOut), "Wrong normalize +-180");
|
||||||
|
|
||||||
|
vOut = normalizeValue(360.0, -180.0, 180.0);
|
||||||
|
QVERIFY2(qFuzzyCompare(0, vOut), "Wrong normalize +-180");
|
||||||
|
|
||||||
|
vOut = normalizeValue(-190, -180.0, 180.0);
|
||||||
|
QVERIFY2(qFuzzyCompare(170, vOut), "Wrong normalize +-180");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//! main
|
//! main
|
||||||
|
|||||||
Reference in New Issue
Block a user