Take into account stroke thickness when simplifying it

preferencesAboutTextFull
Craig Watson 8 years ago
parent b6f41165a6
commit 5bf52b8b3f
  1. 1
      src/core/UBSettings.cpp
  2. 1
      src/core/UBSettings.h
  3. 29
      src/domain/UBGraphicsStroke.cpp

@ -272,6 +272,7 @@ void UBSettings::init()
boardInterpolatePenStrokes = new UBSetting(this, "Board", "InterpolatePenStrokes", true); boardInterpolatePenStrokes = new UBSetting(this, "Board", "InterpolatePenStrokes", true);
boardSimplifyPenStrokes = new UBSetting(this, "Board", "SimplifyPenStrokes", true); boardSimplifyPenStrokes = new UBSetting(this, "Board", "SimplifyPenStrokes", true);
boardSimplifyPenStrokesThresholdAngle = new UBSetting(this, "Board", "SimplifyPenStrokesThresholdAngle", 2); boardSimplifyPenStrokesThresholdAngle = new UBSetting(this, "Board", "SimplifyPenStrokesThresholdAngle", 2);
boardSimplifyPenStrokesThresholdWidthDifference = new UBSetting(this, "Board", "SimplifyPenStrokesThresholdWidthDifference", 2.0);
boardInterpolateMarkerStrokes = new UBSetting(this, "Board", "InterpolateMarkerStrokes", true); boardInterpolateMarkerStrokes = new UBSetting(this, "Board", "InterpolateMarkerStrokes", true);
boardSimplifyMarkerStrokes = new UBSetting(this, "Board", "SimplifyMarkerStrokes", true); boardSimplifyMarkerStrokes = new UBSetting(this, "Board", "SimplifyMarkerStrokes", true);

@ -276,6 +276,7 @@ class UBSettings : public QObject
UBSetting* boardInterpolatePenStrokes; UBSetting* boardInterpolatePenStrokes;
UBSetting* boardSimplifyPenStrokes; UBSetting* boardSimplifyPenStrokes;
UBSetting* boardSimplifyPenStrokesThresholdAngle; UBSetting* boardSimplifyPenStrokesThresholdAngle;
UBSetting* boardSimplifyPenStrokesThresholdWidthDifference;
UBSetting* boardInterpolateMarkerStrokes; UBSetting* boardInterpolateMarkerStrokes;
UBSetting* boardSimplifyMarkerStrokes; UBSetting* boardSimplifyMarkerStrokes;

@ -223,23 +223,32 @@ UBGraphicsStroke* UBGraphicsStroke::simplify()
* test these three points. As long as they are aligned, B is erased and we start over. * 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. * 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 * 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 // 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<strokePoint>::iterator it = points.begin(); QList<strokePoint>::iterator it = points.begin();
QList<QList<strokePoint>::iterator> toDelete; QList<QList<strokePoint>::iterator> toDelete;
while (it+2 != points.end()) { while (it+2 != points.end()) {
// it, b_it and (b_it+1) correspond to A, B and C respectively
QList<strokePoint>::iterator b_it(it+1); QList<strokePoint>::iterator b_it(it+1);
while (b_it+1 != points.end()) { 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 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); b_it = points.erase(b_it);
else else
break; break;
@ -267,16 +276,18 @@ UBGraphicsStroke* UBGraphicsStroke::simplify()
// certain threshold helps mitigate this issue. // certain threshold helps mitigate this issue.
// TODO: fix fill issue // TODO: fix fill issue
if (newStrokePoints.size() > 1 && i < points.size() - 1) { if (hasAlpha()) {
qreal angle = qFabs(UBGeometryUtils::angle(points[i-1].first, points[i].first, points[i+1].first)); if (newStrokePoints.size() > 1 && i < points.size() - 1) {
qDebug() << "Angle: " << angle; qreal angle = qFabs(UBGeometryUtils::angle(points[i-1].first, points[i].first, points[i+1].first));
if (angle > 40 && angle < 320) qDebug() << "Angle: " << angle;
if (angle > 40 && angle < 320)
drawCurve = true;
}
if (newStrokePoints.size() % 20 == 0)
drawCurve = true; drawCurve = true;
} }
if (newStrokePoints.size() % 20 == 0)
drawCurve = true;
if (drawCurve) { if (drawCurve) {
UBGraphicsPolygonItem* poly = mScene->polygonToPolygonItem(UBGeometryUtils::curveToPolygon(newStrokePoints, true, true)); UBGraphicsPolygonItem* poly = mScene->polygonToPolygonItem(UBGeometryUtils::curveToPolygon(newStrokePoints, true, true));

Loading…
Cancel
Save