Skip to content

Commit

Permalink
Performance and stile fixes
Browse files Browse the repository at this point in the history
quick fix 4 fast merge
  • Loading branch information
Febbe committed Nov 5, 2019
1 parent 8e8d971 commit ab1af77
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/control/ToolHandler.h
Expand Up @@ -126,7 +126,7 @@ class ToolHandler

private:
std::array<std::unique_ptr<Tool>, TOOL_COUNT> tools;
Tool* current = NULL;
Tool* current = nullptr;

/**
* Last selected tool, reference with color values etc.
Expand Down
30 changes: 14 additions & 16 deletions src/control/shaperecognizer/CircleRecognizer.cpp
Expand Up @@ -36,31 +36,29 @@ 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<double>::epsilon()
if (divisor == 0)
{
return 0;
}

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)
Expand Down
7 changes: 4 additions & 3 deletions src/control/tools/StrokeHandler.cpp
Expand Up @@ -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();
}

Expand Down
3 changes: 1 addition & 2 deletions src/gui/XournalView.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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)));
Expand Down
16 changes: 8 additions & 8 deletions src/util/LoopUtil.h
Expand Up @@ -11,20 +11,20 @@

#pragma once

#include <algorithm>
#include <iterator>

/**
* for_first_then_each
* loop over the container from begin to end but handles the first element
* differently.
*/
template <typename Container, typename Function_first, typename Function_others>
void for_first_then_each(Container container, Function_first function_first, Function_others function_others)
template <typename Container, typename Fun1, typename Fun2>
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<Fun2&&>(f2));
};
65 changes: 19 additions & 46 deletions src/view/StrokeView.cpp
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -202,6 +175,6 @@ void StrokeView::paint(bool dontRenderEditingStroke)
}
else
{
drawWithPressuire();
drawWithPressure();
}
}
2 changes: 1 addition & 1 deletion src/view/StrokeView.h
Expand Up @@ -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:
Expand Down

0 comments on commit ab1af77

Please sign in to comment.