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. 15
      src/domain/UBGraphicsStroke.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);

@ -276,6 +276,7 @@ class UBSettings : public QObject
UBSetting* boardInterpolatePenStrokes;
UBSetting* boardSimplifyPenStrokes;
UBSetting* boardSimplifyPenStrokesThresholdAngle;
UBSetting* boardSimplifyPenStrokesThresholdWidthDifference;
UBSetting* boardInterpolateMarkerStrokes;
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.
* 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<strokePoint>::iterator it = points.begin();
QList<QList<strokePoint>::iterator> toDelete;
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);
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,6 +276,7 @@ UBGraphicsStroke* UBGraphicsStroke::simplify()
// certain threshold helps mitigate this issue.
// TODO: fix fill issue
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;
@ -276,6 +286,7 @@ UBGraphicsStroke* UBGraphicsStroke::simplify()
if (newStrokePoints.size() % 20 == 0)
drawCurve = true;
}
if (drawCurve) {

Loading…
Cancel
Save