#include "detectiondata.h" #include #include #include /** * 检测数据模型实现 * 管理实时检测信息,包括目标检测、状态更新等 */ DetectionData::DetectionData(QObject* parent) : QObject(parent) , status_(IDLE) , accuracy_(0.0f) , lastUpdate_(QDateTime::currentDateTime()) , simulationTimer_(new QTimer(this)) , simulationStep_(0) { // 初始化目标类型和ID列表 targetTypes_ << "人员" << "车辆" << "建筑物" << "障碍物" << "动物"; targetIds_ << "T001" << "T002" << "T003" << "T004" << "T005" << "T006" << "T007" << "T008"; // 设置模拟定时器 simulationTimer_->setInterval(2000); // 每2秒更新一次 connect(simulationTimer_, &QTimer::timeout, this, &DetectionData::onSimulationTimer); } DetectionData::~DetectionData() { stopSimulation(); } QString DetectionData::getStatusText() const { switch (status_) { case IDLE: return "● 空闲"; case DETECTING: return "● 检测中"; case PROCESSING: return "● 处理中"; case ERROR: return "● 错误"; default: return "● 未知"; } } void DetectionData::setStatus(DetectionStatus status) { if (status_ != status) { status_ = status; lastUpdate_ = QDateTime::currentDateTime(); emit statusChanged(status); emit detectionUpdated(); qDebug() << "检测状态变更为:" << getStatusText(); } } void DetectionData::setAccuracy(float accuracy) { if (qAbs(accuracy_ - accuracy) > 0.01f) { accuracy_ = qBound(0.0f, accuracy, 100.0f); lastUpdate_ = QDateTime::currentDateTime(); emit detectionUpdated(); } } void DetectionData::addTarget(const DetectionTarget& target) { targets_.append(target); lastUpdate_ = QDateTime::currentDateTime(); emit targetAdded(target); emit detectionUpdated(); qDebug() << "添加检测目标:" << target.type << target.id << "置信度:" << target.confidence; } void DetectionData::removeTarget(const QString& targetId) { for (int i = 0; i < targets_.size(); ++i) { if (targets_[i].id == targetId) { targets_.removeAt(i); lastUpdate_ = QDateTime::currentDateTime(); emit targetRemoved(targetId); emit detectionUpdated(); qDebug() << "移除检测目标:" << targetId; return; } } } void DetectionData::clearTargets() { if (!targets_.isEmpty()) { targets_.clear(); lastUpdate_ = QDateTime::currentDateTime(); emit detectionUpdated(); qDebug() << "清空所有检测目标"; } } void DetectionData::startSimulation() { if (!simulationTimer_->isActive()) { simulationTimer_->start(); setStatus(DETECTING); qDebug() << "开始检测模拟"; } } void DetectionData::stopSimulation() { if (simulationTimer_->isActive()) { simulationTimer_->stop(); setStatus(IDLE); qDebug() << "停止检测模拟"; } } void DetectionData::updateSimulation() { onSimulationTimer(); } void DetectionData::onSimulationTimer() { simulationStep_++; // 随机更新检测状态 if (simulationStep_ % 10 == 0) { DetectionStatus newStatus = static_cast( QRandomGenerator::global()->bounded(0, 4)); setStatus(newStatus); } // 随机添加或移除目标 if (simulationStep_ % 3 == 0) { if (targets_.size() < 5 && QRandomGenerator::global()->bounded(0, 2)) { generateRandomTarget(); } else if (targets_.size() > 0 && QRandomGenerator::global()->bounded(0, 3) == 0) { int index = QRandomGenerator::global()->bounded(0, targets_.size()); removeTarget(targets_[index].id); } } // 更新现有目标位置 updateTargetPositions(); // 更新检测精度 float newAccuracy = 85.0f + QRandomGenerator::global()->generateDouble() * 15.0f; setAccuracy(newAccuracy); emit detectionUpdated(); } void DetectionData::generateRandomTarget() { DetectionTarget target; target.type = getRandomTargetType(); target.id = getRandomTargetId(); target.confidence = 70.0f + QRandomGenerator::global()->generateDouble() * 30.0f; target.position = QVector3D( QRandomGenerator::global()->generateDouble() * 100.0f, QRandomGenerator::global()->generateDouble() * 100.0f, QRandomGenerator::global()->generateDouble() * 20.0f ); target.size = QVector3D( 1.0f + QRandomGenerator::global()->generateDouble() * 3.0f, 1.0f + QRandomGenerator::global()->generateDouble() * 3.0f, 1.0f + QRandomGenerator::global()->generateDouble() * 2.0f ); target.timestamp = QDateTime::currentDateTime(); addTarget(target); } void DetectionData::updateTargetPositions() { for (auto& target : targets_) { // 模拟目标移动 float dx = (QRandomGenerator::global()->generateDouble() - 0.5) * 2.0f; float dy = (QRandomGenerator::global()->generateDouble() - 0.5) * 2.0f; float dz = (QRandomGenerator::global()->generateDouble() - 0.5) * 0.5f; target.position += QVector3D(dx, dy, dz); // 限制在合理范围内 target.position.setX(qBound(0.0f, target.position.x(), 100.0f)); target.position.setY(qBound(0.0f, target.position.y(), 100.0f)); target.position.setZ(qBound(0.0f, target.position.z(), 20.0f)); } } QString DetectionData::getRandomTargetType() { return targetTypes_[QRandomGenerator::global()->bounded(0, targetTypes_.size())]; } QString DetectionData::getRandomTargetId() { return targetIds_[QRandomGenerator::global()->bounded(0, targetIds_.size())]; }