diff --git a/src/core/UBSettings.cpp b/src/core/UBSettings.cpp index cfde5119..d759eec6 100644 --- a/src/core/UBSettings.cpp +++ b/src/core/UBSettings.cpp @@ -272,6 +272,7 @@ void UBSettings::init() boardInterpolatePenStrokes = new UBSetting(this, "Board", "InterpolatePenStrokes", true); boardSimplifyPenStrokes = new UBSetting(this, "Board", "SimplifyPenStrokes", true); boardSimplifyPenStrokesThresholdAngle = new UBSetting(this, "Board", "SimplifyPenStrokesThresholdAngle", 2); + boardSimplifyPenStrokesThresholdWidthDifference = new UBSetting(this, "Board", "SimplifyPenStrokesThresholdWidthDifference", 2.0); boardInterpolateMarkerStrokes = new UBSetting(this, "Board", "InterpolateMarkerStrokes", true); boardSimplifyMarkerStrokes = new UBSetting(this, "Board", "SimplifyMarkerStrokes", true); diff --git a/src/core/UBSettings.h b/src/core/UBSettings.h index 0fb8bf68..21e1e9c9 100644 --- a/src/core/UBSettings.h +++ b/src/core/UBSettings.h @@ -276,6 +276,7 @@ class UBSettings : public QObject UBSetting* boardInterpolatePenStrokes; UBSetting* boardSimplifyPenStrokes; UBSetting* boardSimplifyPenStrokesThresholdAngle; + UBSetting* boardSimplifyPenStrokesThresholdWidthDifference; UBSetting* boardInterpolateMarkerStrokes; UBSetting* boardSimplifyMarkerStrokes; diff --git a/src/domain/UBGraphicsStroke.cpp b/src/domain/UBGraphicsStroke.cpp index a640d0a0..7dddf6e9 100644 --- a/src/domain/UBGraphicsStroke.cpp +++ b/src/domain/UBGraphicsStroke.cpp @@ -223,23 +223,32 @@ UBGraphicsStroke* UBGraphicsStroke::simplify() * test these three points. As long as they are aligned, B is erased and we start over. * If not, the current B becomes the new A, and so on. * + * In case the stroke thickness varies a lot between A and B, then B is not removed even if it + * should be according to the previous criteria. * * TODO: more advanced algorithm that could also simplify curved sections of the stroke */ // angle difference in degrees between AB and BC below which the segments are considered colinear - qreal threshold = UBSettings::settings()->boardSimplifyPenStrokesThresholdAngle->get().toReal(); + qreal thresholdAngle = UBSettings::settings()->boardSimplifyPenStrokesThresholdAngle->get().toReal(); + + // Relative difference in thickness between two consecutive points (A and B) below which they are considered equal + qreal thresholdWidthDifference = UBSettings::settings()->boardSimplifyPenStrokesThresholdWidthDifference->get().toReal(); QList::iterator it = points.begin(); QList::iterator> toDelete; while (it+2 != points.end()) { + // it, b_it and (b_it+1) correspond to A, B and C respectively QList::iterator b_it(it+1); while (b_it+1 != points.end()) { qreal angle = qFabs(QLineF(it->first, b_it->first).angle() - QLineF(b_it->first, (b_it+1)->first).angle()); + qreal widthRatio = qMax(it->second, b_it->second)/qMin(it->second, b_it->second); + if (widthRatio > thresholdWidthDifference) + qDebug() << "Width ratio: " << widthRatio; - if (angle < threshold) + if (angle < thresholdAngle && widthRatio < thresholdWidthDifference) b_it = points.erase(b_it); else break; @@ -267,16 +276,18 @@ UBGraphicsStroke* UBGraphicsStroke::simplify() // certain threshold helps mitigate this issue. // TODO: fix fill issue - if (newStrokePoints.size() > 1 && i < points.size() - 1) { - qreal angle = qFabs(UBGeometryUtils::angle(points[i-1].first, points[i].first, points[i+1].first)); - qDebug() << "Angle: " << angle; - if (angle > 40 && angle < 320) + if (hasAlpha()) { + if (newStrokePoints.size() > 1 && i < points.size() - 1) { + qreal angle = qFabs(UBGeometryUtils::angle(points[i-1].first, points[i].first, points[i+1].first)); + qDebug() << "Angle: " << angle; + if (angle > 40 && angle < 320) + drawCurve = true; + } + + if (newStrokePoints.size() % 20 == 0) drawCurve = true; } - if (newStrokePoints.size() % 20 == 0) - drawCurve = true; - if (drawCurve) { UBGraphicsPolygonItem* poly = mScene->polygonToPolygonItem(UBGeometryUtils::curveToPolygon(newStrokePoints, true, true));