You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nmWTAI-Platform/Include/nmNum/nmSubWxs/nmDfnGenerator.h

142 lines
7.3 KiB
C

#ifndef NMDFNGENERATOR_H
#define NMDFNGENERATOR_H
#include "nmDfnData.h"
#include <QElapsedTimer>
#include <cmath>
// 生成参数全部采用基准单位;窗口只负责读取输入,算法不访问控件或工程数据。
struct nmDfnGenerationOptions
{
// 保留原来常用的数量、长度、角度和二十米最小间距。
nmDfnGenerationOptions() : m_nCount(5), m_dMinLength(100.0), m_dMaxLength(250.0),
m_dMinAngle(0.0), m_dMaxAngle(3.14159265358979323846), m_dSpacing(20.0), m_nDistribution(0) {}
int m_nCount; // 目标裂缝数量。
double m_dMinLength; // 最小整段长度,m。
double m_dMaxLength; // 最大整段长度,m。
double m_dMinAngle; // 最小方向角,rad,从 X 正向逆时针。
double m_dMaxAngle; // 最大方向角,rad。
double m_dSpacing; // 裂缝间最小距离,m;为零时仍避免线段相交。
int m_nDistribution; // 长度与方向的分布:0 均匀、1 截断指数、2 截断正态。
};
// 独立随机生成器,使用局部随机状态,避免修改应用全局 qrand 种子。
class nmDfnGenerator
{
public:
// 调用方提供种子,固定种子可复现同一组采样。
explicit nmDfnGenerator(quint32 nSeed) : m_nState(nSeed ? nSeed : 1u) {}
// 全部生成成功才写入输出;失败保留调用方旧数据,并返回实际放置数量供说明。
bool generate(const nmDfnBoundary& oBoundary, const nmDfnGenerationOptions& oOptions,
QVector<nmDfnRecord>& vecResult, int& nPlaced)
{
nPlaced = 0;
if (!oBoundary.isValid() || oOptions.m_nCount < 1 || oOptions.m_nCount > 1000
|| !qIsFinite(oOptions.m_dMinLength) || !qIsFinite(oOptions.m_dMaxLength)
|| oOptions.m_dMinLength <= 0.0 || oOptions.m_dMaxLength < oOptions.m_dMinLength
|| !qIsFinite(oOptions.m_dMinAngle) || !qIsFinite(oOptions.m_dMaxAngle)
|| oOptions.m_dMaxAngle < oOptions.m_dMinAngle || !qIsFinite(oOptions.m_dSpacing)
|| oOptions.m_dSpacing < 0.0 || oOptions.m_nDistribution < 0 || oOptions.m_nDistribution > 2) return false;
const QRectF oBounds = oBoundary.bounds();
QVector<nmDfnRecord> vecGenerated;
QElapsedTimer oTimer;
oTimer.start();
for (int i = 0; i < oOptions.m_nCount; ++i) {
bool bPlaced = false;
// 同时限制每条裂缝的尝试次数和整次运算时间,凹多边形也不会无限采样。
for (int nAttempt = 0; nAttempt < 200; ++nAttempt) {
if (oTimer.elapsed() > 3000) return false;
const double dLength = sample(oOptions.m_dMinLength, oOptions.m_dMaxLength, oOptions.m_nDistribution);
const double dAngle = sample(oOptions.m_dMinAngle, oOptions.m_dMaxAngle, oOptions.m_nDistribution);
const QPointF oCenter(oBounds.left() + uniform() * oBounds.width(),
oBounds.top() + uniform() * oBounds.height());
const QPointF oHalf(dLength * 0.5 * std::cos(dAngle), dLength * 0.5 * std::sin(dAngle));
const QLineF oLine(oCenter - oHalf, oCenter + oHalf);
if (!inside(oBoundary, oLine)) continue;
bool bTooClose = false;
for (int j = 0; j < vecGenerated.size(); ++j) {
if (tooClose(oLine, vecGenerated[j].m_oLine, oOptions.m_dSpacing)) { bTooClose = true; break; }
}
if (bTooClose) continue;
nmDfnRecord oRecord;
oRecord.m_sName = QString("DFN_%1").arg(i + 1);
oRecord.m_oLine = oLine;
if (!oRecord.isValid()) continue;
vecGenerated.append(oRecord);
nPlaced = vecGenerated.size();
bPlaced = true;
break;
}
if (!bPlaced) return false;
}
vecResult = vecGenerated;
return true;
}
private:
// 返回严格位于 (0,1) 的随机数,避免指数和正态采样出现 log(0)。
double uniform()
{
m_nState ^= m_nState << 13;
m_nState ^= m_nState >> 17;
m_nState ^= m_nState << 5;
return (static_cast<double>(m_nState) + 0.5) / 4294967296.0;
}
// 在给定范围内采样;正态标准差仅乘一次,避免旧实现复用样本时重复缩放。
double sample(double dMin, double dMax, int nDistribution)
{
if (dMin == dMax) return dMin;
if (nDistribution == 1) {
const double dScale = (dMin + dMax) * 0.5;
if (dScale > 0.0)
return dMin - dScale * std::log(1.0 - uniform() * (1.0 - std::exp(-(dMax - dMin) / dScale)));
} else if (nDistribution == 2) {
for (int i = 0; i < 64; ++i) {
const double dNormal = std::sqrt(-2.0 * std::log(uniform()))
* std::cos(6.28318530717958647692 * uniform());
const double dValue = (dMin + dMax) * 0.5 + dNormal * (dMax - dMin) / 6.0;
if (dValue >= dMin && dValue <= dMax) return dValue;
}
return (dMin + dMax) * 0.5;
}
return dMin + (dMax - dMin) * uniform();
}
// 凹多边形必须检查整条线段与边界交叉,不能只检查两个端点。
static bool inside(const nmDfnBoundary& oBoundary, const QLineF& oLine)
{
if (oBoundary.m_bCircle)
return QLineF(oBoundary.m_oCenter, oLine.p1()).length() <= oBoundary.m_dRadius
&& QLineF(oBoundary.m_oCenter, oLine.p2()).length() <= oBoundary.m_dRadius;
const QPolygonF& oPolygon = oBoundary.m_oPolygon;
if (!oPolygon.containsPoint(oLine.p1(), Qt::OddEvenFill)
|| !oPolygon.containsPoint(oLine.p2(), Qt::OddEvenFill)) return false;
for (int i = 0; i < oPolygon.size(); ++i) {
QPointF oIntersection;
if (oLine.intersect(QLineF(oPolygon[i], oPolygon[(i + 1) % oPolygon.size()]), &oIntersection)
== QLineF::BoundedIntersection) return false;
}
return true;
}
// 点到有限线段的最短距离,投影限制在线段内。
static double distance(const QPointF& oPoint, const QLineF& oLine)
{
const double dLength = oLine.length();
if (dLength <= 0.0) return QLineF(oPoint, oLine.p1()).length();
const QPointF oDirection((oLine.x2() - oLine.x1()) / dLength, (oLine.y2() - oLine.y1()) / dLength);
const QPointF oOffset = oPoint - oLine.p1();
const double dAlong = qBound(0.0, oOffset.x() * oDirection.x() + oOffset.y() * oDirection.y(), dLength);
return QLineF(oPoint, oLine.p1() + oDirection * dAlong).length();
}
// 相交、重合或距离不足时拒绝采样,保留原有不交叉的生成约束。
static bool tooClose(const QLineF& oFirst, const QLineF& oSecond, double dSpacing)
{
QPointF oIntersection;
if (oFirst.intersect(oSecond, &oIntersection) == QLineF::BoundedIntersection) return true;
const double dDistance = qMin(qMin(distance(oFirst.p1(), oSecond), distance(oFirst.p2(), oSecond)),
qMin(distance(oSecond.p1(), oFirst), distance(oSecond.p2(), oFirst)));
return dDistance <= dSpacing;
}
quint32 m_nState; // 本次生成独占的伪随机状态。
};
#endif // NMDFNGENERATOR_H