Files
qt5-legatus-legacy/Lts/Tools/WlsRandomPositions/position_helper.cpp
T
2026-08-13 18:33:10 +02:00

75 lines
2.5 KiB
C++
Executable File

/*
* @RDE_HEADER@
* Generates a postion for a player
* $Id$
* $HeadURL$
* $Author$
* Created by Hanna E. Rathje, SLTE, 2022-03-29
*/
#include "position_helper.h"
#include <LtsCoordTrans/lts_coord_trans.h>
#include <NGLLogger/NGLLoggerBootstrap.h>
#include <QLineF>
static QRectF s_area;
static double s_heightMin;
static double s_heightMax;
namespace WlsRandomPositions
{
PositionHelper::PositionHelper(const QPointF &topLeft, const QPointF &bottomRight, double heightMin, double heightMax)
: m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("PositionHelper"))
{
s_area = QRectF(topLeft, bottomRight).normalized();
s_heightMin = heightMin;
s_heightMax = heightMax;
INFO_LOG(m_logger) << "Area initialized: " << LtsCoordTrans::UtmCoordinate::fromGeographic(s_area.topLeft()).mgrs() << "//"
<< LtsCoordTrans::UtmCoordinate::fromGeographic(s_area.bottomRight()).mgrs();
}
void PositionHelper::set(const QPointF &topLeft, const QPointF &bottomRight, double heightMin, double heightMax)
{
s_area = QRectF(topLeft, bottomRight).normalized();
s_heightMin = heightMin;
s_heightMax = heightMax;
INFO_LOG(m_logger) << "New Area initialized: " << LtsCoordTrans::UtmCoordinate::fromGeographic(s_area.topLeft()).mgrs() << "//"
<< LtsCoordTrans::UtmCoordinate::fromGeographic(s_area.bottomRight()).mgrs();
}
QPair<QPointF,double> PositionHelper::randPos()
{
LtsCoordTrans::UtmCoordinate bl = LtsCoordTrans::UtmCoordinate::fromGeographic(s_area.bottomLeft());
LtsCoordTrans::UtmCoordinate tr = LtsCoordTrans::UtmCoordinate::fromGeographic(s_area.topRight());
QPointF distance = bl.relativeTo(tr);
// Multiplying with -1 works for all Degree Squares (northern/southern hemisphere, west/east of Greenwich)
distance.setX((rand() % 100) / 100.0 * distance.x() * -1.0);
distance.setY((rand() % 100) / 100.0 * distance.y() * -1.0);
double z = (rand() % 100) / 100.0 * (s_heightMax - s_heightMin) + s_heightMin;
return {bl.geographic(distance),z};
}
QPair<QPointF,double> PositionHelper::randPos(const QPointF &oldPos, double maxDistance)
{
double z = (rand() % 100) / 100.0 * (s_heightMax - s_heightMin) + s_heightMin;
LtsCoordTrans::UtmCoordinate utm = LtsCoordTrans::UtmCoordinate::fromGeographic(oldPos);
QLineF line;
line.setP1({0,0});
line.setP2({1,0});
do
{
line.setAngle((rand() % 36000) / 100.0);
line.setLength(((rand() % 100) / 100.0) * maxDistance);
} while (!s_area.contains(utm.geographic(line.p2())));
return {utm.geographic(line.p2()),z};
}
} // Namespace WlsRandomPositions