Initial commit

This commit is contained in:
torsten committed 2026-08-13 18:33:10 +02:00
commit b7d90b9afe
16245 files changed
+3396666

No files matched your search

@@ -0,0 +1,341 @@
/**
* Handler for player data
* $Id$
* $HeadURL$
* $Author$
* Created by hjl, SLSE, 2017-02-20
*/
#include "handler_building_data.h"
#include "settings.h"
#include "handler_participant.h"
#include <LtsUtilsCore/dds_bitarray.h>
#include <LtsCoordTrans/lts_coord_trans.h>
#include <QHash>
#include <QHashIterator>
namespace CentralEffectSimulation
{
HandlerBuildingData::HandlerBuildingData(const std::shared_ptr<CentralEffectSimulation::DdsHandler> &ddsHandler, QObject *parent)
: QObject(parent)
, m_ddsHandler(ddsHandler)
, m_logger(CentralEffectSimulation::getLogger("HandlerBuildingData"))
{
INFO_LOG(m_logger) << "HandlerBuildingData::HandlerBuildingData()";
} // end of ctor()
//-----------------------------------------------------------------------------
HandlerBuildingData::~HandlerBuildingData()
{
} // end of dtor()
//-----------------------------------------------------------------------------
bool HandlerBuildingData::init()
{
INFO_LOG(m_logger) << "HandlerBuildingData::init()";
auto dtcIsm = m_ddsHandler->dtcIsm();
dtcIsm->waitForHistoricalDataMainStructureElement(1000);
connect(dtcIsm.get(), &InfrastructureManagement::Interface::Dtc::mainStructureElementChanged,
this, &HandlerBuildingData::onMainStructureElementChanged);
dtcIsm->enableMountingPointAsset();
connect(dtcIsm.get(), &InfrastructureManagement::Interface::Dtc::mountingPointAssetChanged,
this, &HandlerBuildingData::onMountingPointAssetChanged);
dtcIsm->enableInstrumentationAsset();
connect(dtcIsm.get(), &InfrastructureManagement::Interface::Dtc::instrumentationAssetChanged,
this, &HandlerBuildingData::onInstrumentationAssetChanged);
// Get 'static' data for all MainStructureElements
{
auto cb = [this](const InfrastructureManagement::MainStructureElement & mse)
{
onMainStructureElementChanged(mse, DataModification::eAdded);
};
if (!dtcIsm->mainStructureElements(cb))
return false;
}
return true;
} // end of init()
//-----------------------------------------------------------------------------
// ///////////////////////////////////
// I S M (InfrastructureManagement)
// ///////////////////////////////////
void HandlerBuildingData::onMainStructureElementChanged(const InfrastructureManagement::MainStructureElement &data, DataModification modType)
{
INFO_LOG(m_logger) << "HandlerBuildingData::onMainStructureElementChanged() id=" << data.id();
if (modType == DataModification::eRemoved
|| !m_ddsHandler->dtcIsm()->isElementAssignedToExercise(data, m_ddsHandler->getExerciseId()))
{
INFO_LOG(m_logger) << "HandlerBuildingData::onMainStructureElementChanged() id=" << data.id() << " remove";
m_mainStructureElements.remove(data.id());
m_footprints.remove(data.id());
return;
}
m_mainStructureElements.insert(data.id(), data);
DEBUG_LOG(m_logger) << "Got MainStructureElement: " << data;
for (const InfrastructureManagement::StructureElement &ele : qAsConst(data.elements()))
{
if (ele.structureElementTypeId() == InfrastructureManagement::StructureElementType_def::Room)
{
const std::vector<int32_t> &path = ele.path();
if (path.size() < 3)
continue;
const int structureElementId = path.at(0);
const int floorId = path.at(1);
const int roomId = path.at(2);
DEBUG_LOG(m_logger) << "ele.id=" << ele.id() << " name =" << ele.name()
<< " ele.StructureElementType=" << ele.structureElementTypeId()
<< " buildingId = " << structureElementId
<< " floorId =" << floorId
<< " roomId = " << roomId;
m_floorRoom.insert(roomId, floorId);
m_structureElementToFloor.insert(floorId, structureElementId);
m_buildingFloor.insert(floorId, data.id());
}
// get assetPositions
{
if (ele.mountingPointTypeId() == InfrastructureManagement::MountingPointType::EffectGenerationControlUnit)
{
DEBUG_LOG(m_logger) << "EGCU id=" << ele.id()
<< " displayName=" << ele.displayName()
<< " name=" << ele.name()
<< " type=" << ele.mountingPointTypeId();
AssetPosition assetPosition;
assetPosition.structureElementId = ele.id();
assetPosition.mountingPointType = ele.mountingPointTypeId();
assetPosition.relPosition = ele.relPosition();
assetPosition.geoPosition = ele.geoPosition();
const QPointF &posUtm = CentralEffectSimulation::geographic2Utm(assetPosition.geoPosition, m_zone);
qreal x = posUtm.x() + assetPosition.relPosition.offsetX();
qreal y = posUtm.y() + assetPosition.relPosition.offsetY();
const QPointF posAssetUtm(x, y);
assetPosition.positionUtm = posAssetUtm;
assetPosition.zone = m_zone;
//QString hostname = QString::fromStdString(ele.hostname());
//assetPosition.hostname = hostname;
m_assetPositions.insert(assetPosition.structureElementId, assetPosition);
DEBUG_LOG(m_logger) << "assetPosition.insert StructureElementId= " << assetPosition.structureElementId
<< " geoPosition=" << assetPosition.geoPosition
<< " relPosition=" << assetPosition.relPosition
<< " hostname=" << assetPosition.hostname
<< " Type=" << assetPosition.mountingPointType
<< " positionUtm=" << assetPosition.positionUtm.x()
<< "/" << assetPosition.positionUtm.y()
<< " zone=" << assetPosition.zone
<< " posUtm=" << posUtm.x() << "/" << posUtm.y();
}
}
}
getFootprintsByExercise();
}
void HandlerBuildingData::onMountingPointAssetChanged(const InfrastructureManagement::MountingPointAsset &data, DataModification modType)
{
if (modType == DataModification::eRemoved)
return;
DEBUG_LOG(m_logger) << "onMountingPointAssetChanged id=" << data.id()
<< " assetId=" << data.assetId()
<< " structureElementId=" << data.structureElementId();
m_mountingPointAsset.insert(data.structureElementId(), m_assetMountingPoint.value(data.assetId()));
m_assetMountingPoint.insert(data.assetId(), data.structureElementId());
}
void HandlerBuildingData::onInstrumentationAssetChanged(const InfrastructureManagement::InstrumentationAsset &data, DataModification modType)
{
if (modType == DataModification::eRemoved)
return;
if (data.mountingPointTypeId() == InfrastructureManagement::MountingPointType::EffectGenerationControlUnit)
{
DEBUG_LOG(m_logger) << "onInstrumentationAssetChanged id=" << data.id()
<< " hostname=" << data.hostname();
QString hostname = QString::fromStdString(data.hostname());
m_instrumentationAssets.insert(data.id(), hostname);
if (m_assetMountingPoint.contains(data.id()))
{
int assetId = data.id();
int mountpointId = m_assetMountingPoint.value(assetId);
DEBUG_LOG(m_logger) << "m_instrumentationAsset.contains(" << assetId << ")"
<< " assetId=" << assetId
<< " mountpointId=" << mountpointId;
if (m_assetPositions.contains(mountpointId))
{
AssetPosition assetPosition = m_assetPositions.value(mountpointId);
assetPosition.hostname = hostname;
assetPosition.mountingPointType = data.mountingPointTypeId();
m_assetPositions.insert(assetPosition.structureElementId, assetPosition);
DEBUG_LOG(m_logger) << "m_assetPositions.insert assetPosition structureElementId= " << assetPosition.structureElementId
<< " type=" << assetPosition.mountingPointType
<< " geoPosition=" << assetPosition.geoPosition
<< " relPosition=" << assetPosition.relPosition
<< " hostname=" << assetPosition.hostname
<< " positionUtm=" << assetPosition.positionUtm.x()
<< "/" << assetPosition.positionUtm.y();
}
}
}
}
QVector<int> HandlerBuildingData::getFloorsFromStructureElementId(int structureElementId) const
{
DEBUG_LOG(m_logger) << "getFloorsFromStructureElementId structureElementId=" << structureElementId;
QVector<int> result;
result.reserve(m_structureElementToFloor.size());
for (auto i = m_structureElementToFloor.begin(); i != m_structureElementToFloor.end(); ++i)
{
if (i.value() == structureElementId)
result.append(i.key());
}
DEBUG_LOG(m_logger) << "result.size()" << result.size();
return result;
}
QVector<int> HandlerBuildingData::getFloorsFromBuilding(int buildingId) const
{
DEBUG_LOG(m_logger) << "getFloorsFromBuilding buildingId=" << buildingId;
QVector<int> result;
result.reserve(m_buildingFloor.size());
for (auto i = m_buildingFloor.begin(); i != m_buildingFloor.end(); ++i)
{
if (i.value() == buildingId)
result.append(i.key());
}
DEBUG_LOG(m_logger) << "result.size()" << result.size();
return result;
}
void HandlerBuildingData::printAssetPositions()
{
DEBUG_LOG(m_logger) << "printAssetPositions: ";
QHashIterator<int, AssetPosition> i(m_assetPositions);
while (i.hasNext())
{
i.next();
DEBUG_LOG(m_logger) << "printAssetPositions: "
<< i.key() << ": " << i.value().structureElementId
<< " type = " << i.value().mountingPointType
<< " hostname =" << i.value().hostname
<< " pos=" << i.value().geoPosition
<< " posUtm=" << i.value().positionUtm.x()
<< "/" << i.value().positionUtm.y() ;
}
}
int HandlerBuildingData::getBuildingIdFromRoomId(const int roomId)
{
int buildingId = 0;
int floorId = getFloorIdFromRoomId(roomId);
if (floorId < 0)
buildingId = -1;
else
buildingId = getBuildingIdFromFloorId(floorId);
DEBUG_LOG(m_logger) << "getBuildingIdFromRoomId roomId=" << roomId
<< " buildingId=" << buildingId;
return buildingId;
}
QVector<int> HandlerBuildingData::polygonEncloseBuildings(const QPolygonF &polygon)
{
DEBUG_LOG(m_logger) << "polygonEncloseBuildings";
int exerciseId = m_ddsHandler->getExerciseId();
bool completeEnclosed = true;
DEBUG_LOG(m_logger) << "Vor getBuildingByPolygon";
bool isGroundLevel = false;
QVector<int> buildingsInside = m_ddsHandler->dtcIsm()->getBuildingByPolygon(exerciseId, polygon, completeEnclosed, isGroundLevel);
DEBUG_LOG(m_logger) << "Nach getBuildingByPolygon";
//int buildingId = m_ddsHandler->dtcIsm()->getSurroundingBuildingByPolygon(exerciseId, polygon);
//DEBUG_LOG(m_logger) << "getSurroundingBuildingByPolygon buildingId= " << buildingId;
return buildingsInside;
}
void HandlerBuildingData::getFootprintsByExercise()
{
auto dtcIsm = m_ddsHandler->dtcIsm();
int exerciseId = m_ddsHandler->getExerciseId();
bool isGroundLevel = false;
QMap<int, QPolygonF> footprintsUtm = dtcIsm->getUtmFootprintsByExerciseId(exerciseId, m_zone, isGroundLevel);
QMapIterator<int, QPolygonF> i(footprintsUtm);
while (i.hasNext())
{
i.next();
DEBUG_LOG(m_logger) << "Key=" << i.key() << ": " << i.value().size();
Footprint footprint;
footprint.mseId = i.key();
footprint.polyUtm = i.value();
footprint.zone = m_zone;
footprint.boundingBoxPolyUtm = footprint.polyUtm.boundingRect();
// for concealment calculation (Verdeckung)
if (!footprint.boundingBoxPolyUtm.isValid())
{
WARNING_LOG(m_logger) << "mseId=" << footprint.mseId
<< " boundingBoxPolyUtm not valid";
continue;
}
footprint.centroid = footprint.boundingBoxPolyUtm.center(); //compute2DPolygonCentroid_deprecated(footprint.polyUtm);
m_footprints.insert(i.key(), footprint);
}
}
QPointF compute2DPolygonCentroid_deprecated(const QPolygonF &metricVertices)
{
// taken from https://stackoverflow.com/questions/2792443/finding-the-centroid-of-a-polygon
// https://en.wikipedia.org/wiki/Centroid#Of_a_polygon
const int vertexCount = metricVertices.size();
if (metricVertices.size() < 3)
return {};
QPointF centroid;
double signedArea = 0.0;
double x0 = 0.0; // Current vertex X
double y0 = 0.0; // Current vertex Y
double x1 = 0.0; // Next vertex X
double y1 = 0.0; // Next vertex Y
double a = 0.0; // Partial signed area
// For all vertices except last
int i=0;
for (i=0; i<vertexCount-1; ++i)
{
x0 = metricVertices[i].x();
y0 = metricVertices[i].y();
x1 = metricVertices[i+1].x();
y1 = metricVertices[i+1].y();
a = x0*y1 - x1*y0;
signedArea += a;
centroid.rx() += (x0 + x1)*a;
centroid.ry() += (y0 + y1)*a;
} // for
// Do last vertex separately to avoid performing an expensive
// modulus operation in each iteration.
x0 = metricVertices[i].x();
y0 = metricVertices[i].y();
x1 = metricVertices[0].x();
y1 = metricVertices[0].y();
a = x0*y1 - x1*y0;
signedArea += a;
centroid.rx() += (x0 + x1)*a;
centroid.ry() += (y0 + y1)*a;
signedArea *= 0.5;
centroid.rx() /= (6.0*signedArea);
centroid.ry() /= (6.0*signedArea);
return centroid;
}
} // namespace CentralEffectSimulation2D