From ab1af778a3cf2ada5646b8384f5b29cd84902301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ke=C3=9Fler?= Date: Sat, 2 Nov 2019 00:46:44 +0100 Subject: [PATCH] Performance and stile fixes quick fix 4 fast merge --- src/control/ToolHandler.h | 2 +- .../shaperecognizer/CircleRecognizer.cpp | 30 ++++----- src/control/tools/StrokeHandler.cpp | 7 +- src/gui/XournalView.cpp | 3 +- src/util/LoopUtil.h | 16 ++--- src/view/StrokeView.cpp | 65 ++++++------------- src/view/StrokeView.h | 2 +- 7 files changed, 48 insertions(+), 77 deletions(-) diff --git a/src/control/ToolHandler.h b/src/control/ToolHandler.h index d500f4b252e6..2db1891e0eae 100644 --- a/src/control/ToolHandler.h +++ b/src/control/ToolHandler.h @@ -126,7 +126,7 @@ class ToolHandler private: std::array, TOOL_COUNT> tools; - Tool* current = NULL; + Tool* current = nullptr; /** * Last selected tool, reference with color values etc. diff --git a/src/control/shaperecognizer/CircleRecognizer.cpp b/src/control/shaperecognizer/CircleRecognizer.cpp index 80bd898bea3e..02fed2fb7d80 100644 --- a/src/control/shaperecognizer/CircleRecognizer.cpp +++ b/src/control/shaperecognizer/CircleRecognizer.cpp @@ -36,7 +36,11 @@ Stroke* CircleRecognizer::makeCircleShape(Stroke* originalStroke, Inertia& inert */ double CircleRecognizer::scoreCircle(Stroke* s, Inertia& inertia) { - if (inertia.getMass() == 0.0) + double r0 = inertia.rad(); + double divisor = inertia.getMass() * r0; + + //Todo: test: std::abs(divisor) <= std::numeric_limits::epsilon() + if (divisor == 0) { return 0; } @@ -44,23 +48,17 @@ double CircleRecognizer::scoreCircle(Stroke* s, Inertia& inertia) double sum = 0.0; double x0 = inertia.centerX(); double y0 = inertia.centerY(); - double r0 = inertia.rad(); - - Point p1; - for_first_then_each( - s->getPointVector(), - [&p1](auto first) { p1 = first; }, - [&p1, &x0, &y0, &r0, &sum](auto other) { - Point p2 = other; - double dm = hypot(p2.x - p1.x, p2.y - p1.y); - double deltar = hypot(p1.x - x0, p1.y - y0) - r0; - sum += dm * fabs(deltar); - - p1 = p2; - }); + auto const& pv = s->getPointVector(); + for (auto pt_1st = begin(pv), pt_2nd = std::next(pt_1st), p_end_i = end(pv); pt_1st != p_end_i && pt_2nd != p_end_i; + ++pt_2nd, ++pt_1st) + { + double dm = hypot(pt_2nd->x - pt_1st->x, pt_2nd->y - pt_1st->y); + double deltar = hypot(pt_1st->x - x0, pt_1st->y - y0) - r0; + sum += dm * fabs(deltar); + } - return sum / (inertia.getMass() * r0); + return sum / (divisor); } Stroke* CircleRecognizer::recognize(Stroke* stroke) diff --git a/src/control/tools/StrokeHandler.cpp b/src/control/tools/StrokeHandler.cpp index f44162d2ce75..1c94bf544ef1 100644 --- a/src/control/tools/StrokeHandler.cpp +++ b/src/control/tools/StrokeHandler.cpp @@ -184,10 +184,11 @@ void StrokeHandler::onButtonReleaseEvent(const PositionInputData& pos) // Backward compatibility and also easier to handle for me;-) // I cannot draw a line with one point, to draw a visible line I need two points, // twice the same Point is also OK - if (stroke->getPointCount() == 1) + if (auto const& pv = stroke->getPointVector(); pv.size() == 1) { - stroke->addPoint(stroke->getPointVector()[0]); - // No pressure sensitivity + stroke->addPoint(pv.front()); + // Todo: check if the following is the reason for a bug, that single points have no pressure: + // No pressure sensitivity, stroke->clearPressure(); } diff --git a/src/gui/XournalView.cpp b/src/gui/XournalView.cpp index df6d6940ba9d..413b385ab643 100644 --- a/src/gui/XournalView.cpp +++ b/src/gui/XournalView.cpp @@ -714,8 +714,6 @@ void XournalView::pageInserted(size_t page) viewPages.insert(begin(viewPages) + page, pageView); - this->viewPages[page] = pageView; - Layout* layout = gtk_xournal_get_layout(this->widget); layout->recalculate(); layout->updateVisibility(); @@ -885,6 +883,7 @@ void XournalView::documentChanged(DocumentChangeType type) doc->lock(); size_t pagecount = doc->getPageCount(); + viewPages.reserve(pagecount); for (size_t i = 0; i < pagecount; i++) { viewPages.push_back(new XojPageView(this, doc->getPage(i))); diff --git a/src/util/LoopUtil.h b/src/util/LoopUtil.h index 0c2502e4d692..a0c25ca85ede 100644 --- a/src/util/LoopUtil.h +++ b/src/util/LoopUtil.h @@ -11,6 +11,7 @@ #pragma once +#include #include /** @@ -18,13 +19,12 @@ * loop over the container from begin to end but handles the first element * differently. */ -template -void for_first_then_each(Container container, Function_first function_first, Function_others function_others) +template +void for_first_then_each(Container&& c, Fun1 f1, Fun2&& f2) { - if (cbegin(container) == cend(container)) return; - function_first(*cbegin(container)); - for (auto it = std::next(cbegin(container)); it != cend(container); it++) - { - function_others(*it); - }; + auto begi = begin(c); + auto endi = end(c); + if (begi == endi) return; + f1(*begi); + std::for_each(std::next(begi), endi, std::forward(f2)); }; diff --git a/src/view/StrokeView.cpp b/src/view/StrokeView.cpp index 161af0b7d8d0..f4adaf087a9e 100644 --- a/src/view/StrokeView.cpp +++ b/src/view/StrokeView.cpp @@ -22,8 +22,8 @@ void StrokeView::drawFillStroke() { for_first_then_each( s->getPointVector(), - [this](auto first) { cairo_move_to(this->cr, first.x, first.y); }, - [this](auto other) { cairo_line_to(this->cr, other.x, other.y); }); + [this](auto const& first) { cairo_move_to(this->cr, first.x, first.y); }, + [this](auto const& other) { cairo_line_to(this->cr, other.x, other.y); }); cairo_fill(cr); } @@ -113,19 +113,10 @@ void StrokeView::drawNoPressure() cairo_set_line_width(cr, width * scaleFactor); applyDashed(0); - for (auto&& point: s->getPointVector()) - { - if (startPoint <= count) - { - cairo_line_to(cr, point.x, point.y); - } - else - { - cairo_move_to(cr, point.x, point.y); - } - count++; - } - + for_first_then_each( + s->getPointVector(), + [this](auto const& first) { cairo_move_to(cr, first.x, first.y); }, + [this](auto const& other) { cairo_line_to(cr, other.x, other.y); }); cairo_stroke(cr); if (group) @@ -148,39 +139,21 @@ void StrokeView::drawNoPressure() * Draw a stroke with pressure, for this multiple * lines with different widths needs to be drawn */ -void StrokeView::drawWithPressuire() +void StrokeView::drawWithPressure() { - int count = 1; - double width = s->getWidth(); - - Point lastPoint1; double dashOffset = 0; - for_first_then_each( - s->getPointVector(), - [&lastPoint1](auto first) { lastPoint1 = first; }, - [this, &lastPoint1, &dashOffset, &count, &width](auto other) { - if (startPoint <= count) - { - if (lastPoint1.z != Point::NO_PRESSURE) - { - width = lastPoint1.z; - } - - // Set width - cairo_set_line_width(cr, width * scaleFactor); - applyDashed(dashOffset); - - cairo_move_to(cr, lastPoint1.x, lastPoint1.y); - cairo_line_to(cr, other.x, other.y); - cairo_stroke(cr); - } - count++; - dashOffset += lastPoint1.lineLengthTo(other); - lastPoint1 = other; - }); - - cairo_stroke(cr); + for (auto p1i = begin(s->getPointVector()), p2i = std::next(p1i), endi = end(s->getPointVector()); + p1i != endi && p2i != endi; ++p1i, ++p2i) + { + auto width = p1i->z != Point::NO_PRESSURE ? p1i->z : s->getWidth(); + cairo_set_line_width(cr, width * scaleFactor); + applyDashed(dashOffset); + cairo_move_to(cr, p1i->x, p1i->y); + cairo_line_to(cr, p2i->x, p2i->y); + cairo_stroke(cr); + dashOffset += p1i->lineLengthTo(*p2i); + } } void StrokeView::paint(bool dontRenderEditingStroke) @@ -202,6 +175,6 @@ void StrokeView::paint(bool dontRenderEditingStroke) } else { - drawWithPressuire(); + drawWithPressure(); } } diff --git a/src/view/StrokeView.h b/src/view/StrokeView.h index e462337e2ab2..388520a7c356 100644 --- a/src/view/StrokeView.h +++ b/src/view/StrokeView.h @@ -44,7 +44,7 @@ class StrokeView * Draw a stroke with pressure, for this multiple * lines with different widths needs to be drawn */ - void drawWithPressuire(); + void drawWithPressure(); private: