From 58ccb873f14bd0d329d5fec5645004083bc0c127 Mon Sep 17 00:00:00 2001 From: yuntang <123@qq.com> Date: Tue, 22 Apr 2025 11:35:39 +0800 Subject: [PATCH] 2025-04-22T11:35:38 --- src/data/pointcalculate.cpp | 47 +++++++++++++++++++++++++++++++++---- src/data/pointcalculate.h | 3 ++- src/ui/centralwidget.cpp | 40 +++++++++++++++++++++++++++---- 3 files changed, 80 insertions(+), 10 deletions(-) diff --git a/src/data/pointcalculate.cpp b/src/data/pointcalculate.cpp index 62d30ca..6d3aa73 100644 --- a/src/data/pointcalculate.cpp +++ b/src/data/pointcalculate.cpp @@ -477,9 +477,9 @@ QString PointCalculate::textFormatEndPoint(const QPointF point) } -QPointF PointCalculate::getClosestPointByY(const float left,const float right,const float valueY) +QPointF PointCalculate::getClosestPointByY(const double left,const double right,const double valueY) { - float minValue = std::numeric_limits::infinity(); // 初始化为正无穷 + double minValue = std::numeric_limits::infinity(); // 初始化为正无穷 QPointF closestPoint; for (const Global::ExperimentData& ed : _dataVtr) { @@ -541,7 +541,44 @@ QVector PointCalculate::getDataInXRange(const float x1, return targetVtr; } - +QVector PointCalculate::movingAveragePoint(const QVector &points, int windowSize) +{ +#if 0 + QVector result; + int n = points.size(); + for (int i = 0; i < n; ++i) { + double sumY = 0.0; + double sumX = 0.0; + int count = 0; + for (int j = std::max(0, i - windowSize / 2); j <= std::min(n - 1, i + windowSize / 2); ++j) { + sumY += points[j].y(); + sumX += points[j].x(); + count++; + } + if (count > 0) { + double avgY = sumY / count; + double avgX = sumX / count; + result.append(QPointF(avgX, avgY)); + } + } + return result; +#endif + QVector result; + int n = points.size(); + for (int i = 0; i < n; ++i) { + double sumY = 0.0; + int count = 0; + for (int j = std::max(0, i - windowSize / 2); j <= std::min(n - 1, i + windowSize / 2); ++j) { + sumY += points[j].y(); + count++; + } + if (count > 0) { + double avgY = sumY / count; + result.append(QPointF(points[i].x(), avgY)); // 使用原始的 x 值 + } + } + return result; +} QVector PointCalculate::getNearbyPointGroupByX(const float targetX) { const int conCount = 10; @@ -572,7 +609,7 @@ double PointCalculate::calculateSlope(double x1, double y1, double x2, double y2 return (y2 - y1) / (x2 - x1); } -// 寻找第一个拐点 +// 寻找拐点 QVector PointCalculate::findInflectionPoints( const QVector& x, const QVector& y) { QVector inflectionPointsX; @@ -619,3 +656,5 @@ QVector PointCalculate::getPointVtrInXRange(const float x1, const float return targetVtr; } + + diff --git a/src/data/pointcalculate.h b/src/data/pointcalculate.h index 7a08b0e..be7274d 100644 --- a/src/data/pointcalculate.h +++ b/src/data/pointcalculate.h @@ -17,7 +17,7 @@ void setRegionPointX(const float,const float); QPointF getClosestPointByX(const float); QVector getNearbyPointGroupByX(const float); -QPointF getClosestPointByY(const float left,const float right,const float valueY); +QPointF getClosestPointByY(const double left,const double right,const double valueY); QPointF getPeakPoint(); QPair getMaxMinValue(); @@ -59,6 +59,7 @@ QPointF calculateIntersection(const QPointF p1,const QPointF p2, const QPointF p3, const QPointF p4); QVector getPeakPointGroup(); std::vector movingAverage(const std::vector& data, int windowSize); +QVector movingAveragePoint(const QVector& data, int windowSize); extern QVector _dataVtr; extern QPointF _peakPoint; diff --git a/src/ui/centralwidget.cpp b/src/ui/centralwidget.cpp index 32694c3..eef72c9 100644 --- a/src/ui/centralwidget.cpp +++ b/src/ui/centralwidget.cpp @@ -612,7 +612,9 @@ void CentralWidget::glassTransitionHandle2() #endif // inflection point. - QVector pointVtr = PointCalculate::getPointVtrInXRange(point1.x(),point2.x()); + QVector originalPointVtr = PointCalculate::getPointVtrInXRange(point1.x(),point2.x()); + // QVector pointVtr = PointCalculate::movingAveragePoint(originalPointVtr,3); + QVector pointVtr = originalPointVtr; xVtr.clear(); yVtr.clear(); @@ -626,6 +628,7 @@ void CentralWidget::glassTransitionHandle2() double targetX = 0.0; PointCalculate::Line targetLine; double maxSlopeAbs = 0.0; // 初始化最大斜率绝对值为0 +#if 0 for (auto it = tangentLines.begin(); it != tangentLines.end(); ++it) { if (std::fabs(it.value().slope) > maxSlopeAbs) { maxSlopeAbs = std::fabs(it.value().slope); // 更新最大斜率绝对值 @@ -634,7 +637,35 @@ void CentralWidget::glassTransitionHandle2() targetLine = it.value(); } } +#endif + // new method + for (auto it = tangentLines.begin(); it != tangentLines.end(); ++it) { + if (std::fabs(it.value().slope) > maxSlopeAbs) { + maxSlopeAbs = std::fabs(it.value().slope); // 更新最大斜率绝对值 + targetX = it.key(); + targetLine = it.value(); + + } + } + + int index = 0; + QPointF prePoint,currentPoint,lastPoint; + for (int i = 0;i < pointVtr.size();i++) { + currentPoint = pointVtr[i]; + if(currentPoint.x() == targetX){ + index = i; + break; + } + } + + prePoint = pointVtr[index - 1]; + lastPoint = pointVtr[index + 1]; + + + targetLine.slope = PointCalculate::calculateSlope(lastPoint.x(),lastPoint.y(),prePoint.x(),prePoint.y()); + targetLine.intercept = currentPoint.y() - targetLine.slope * currentPoint.x(); + // #if 1 QPointF intersection1 = PointCalculate::getIntersection(targetLine,line1); QPointF intersection2 = PointCalculate::getIntersection(targetLine,line2); @@ -670,12 +701,11 @@ void CentralWidget::glassTransitionHandle2() QPointF averagePoint = PointCalculate::getClosestPointByY(point1.x(),point2.x(),averageY); - - QString str = PointCalculate::textFormatGlassTranstion(intersection2.x(), + QString str = PointCalculate::textFormatGlassTranstion(intersection1.x(), averagePoint.x(), - intersection1.x()); + intersection2.x()); - drawText(averagePoint,str); + drawText(averagePoint,str); } // 使用最小二乘法计算线性回归