Stroke improvements

* return const& of vector instead of begin and end iterator
presentation
Andreas Stallinger 7 years ago
parent 81e09ab0d6
commit 2d83b7eb41
  1. 31
      src/control/shaperecognizer/CircleRecognizer.cpp
  2. 2
      src/control/tools/StrokeHandler.cpp
  3. 9
      src/model/Stroke.cpp
  4. 5
      src/model/Stroke.h
  5. 25
      src/util/LoopUtil.h
  6. 89
      src/view/StrokeView.cpp

@ -5,6 +5,7 @@
#include "model/Stroke.h"
#include <cmath>
#include "util/LoopUtil.h"
/**
* Create circle stroke for inertia
@ -45,27 +46,19 @@ double CircleRecognizer::scoreCircle(Stroke* s, Inertia& inertia)
double y0 = inertia.centerY();
double r0 = inertia.rad();
auto it = s->iteratorBegin();
Point p1;
for_first_then_each(
s->getPointVector(),
[&p1, &x0, &y0, &r0](auto first) { p1 = first; },
[&p1, &x0, &y0, &r0, &sum](auto other) {
Point p2 = other;
if (it != s->iteratorEnd())
{
return 0;
}
Point p1 = *it;
it++;
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);
while (it != s->iteratorEnd())
{
Point p2 = *it;
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;
it++;
}
p2 = p1;
});
return sum / (inertia.getMass() * r0);
}

@ -186,7 +186,7 @@ void StrokeHandler::onButtonReleaseEvent(const PositionInputData& pos)
// twice the same Point is also OK
if (stroke->getPointCount() == 1)
{
stroke->addPoint(*stroke->iteratorBegin());
stroke->addPoint(stroke->getPointVector()[0]);
// No pressure sensitivity
stroke->clearPressure();
}

@ -170,14 +170,9 @@ int Stroke::getPointCount() const
return this->points.size();
}
std::vector<Point>::const_iterator Stroke::iteratorBegin() const
std::vector<Point> const& Stroke::getPointVector() const
{
return std::begin(points);
}
std::vector<Point>::const_iterator Stroke::iteratorEnd() const
{
return std::end(points);
return points;
}
void Stroke::deletePointsFrom(int index)

@ -70,8 +70,7 @@ public:
void setLastPoint(const Point& p);
int getPointCount() const;
void freeUnusedPointItems();
std::vector<Point>::const_iterator iteratorBegin() const;
std::vector<Point>::const_iterator iteratorEnd() const;
std::vector<Point> const& getPointVector() const;
Point getPoint(int index) const;
const Point* getPoints() const;
@ -91,7 +90,7 @@ public:
void setLastPressure(double pressure);
void clearPressure();
void scalePressure(double factor);
bool hasPressure() const;
double getAvgPressure() const;

@ -0,0 +1,25 @@
/*
* Xournal++
*
* Utiltiy for loops
*
* @author Xournal++ Team
* https://github.com/xournalpp/xournalpp
*
* @license GNU GPLv2 or later
*/
#pragma once
#include <iterator>
template <typename Container, typename Function_first, typename Function_others>
void for_first_then_each(Container container, Function_first function_first, Function_others function_others)
{
if (cbegin(container) == cend(container)) return;
function_first(*cbegin(container));
for (auto it = std::next(cbegin(container)); it != cend(container); it++)
{
function_others(*it);
};
};

@ -3,6 +3,7 @@
#include "model/eraser/EraseableStroke.h"
#include "model/Stroke.h"
#include "util/LoopUtil.h"
StrokeView::StrokeView(cairo_t* cr, Stroke* s, int startPoint, double scaleFactor, bool noAlpha)
: cr(cr),
@ -19,25 +20,10 @@ StrokeView::~StrokeView()
void StrokeView::drawFillStroke()
{
auto points = s->iteratorBegin();
if (points != s->iteratorEnd())
{
Point p = *points;
points++;
cairo_move_to(cr, p.x, p.y);
}
else
{
return;
}
while (points != s->iteratorEnd())
{
Point p = *points;
points++;
cairo_line_to(cr, p.x, p.y);
}
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); });
cairo_fill(cr);
}
@ -111,7 +97,6 @@ void StrokeView::drawNoPressure()
{
int count = 1;
double width = s->getWidth();
std::vector<Point>::const_iterator points = s->iteratorBegin();
bool group = false;
if (s->getFill() != -1 && s->getToolType() == STROKE_TOOL_HIGHLIGHTER)
@ -128,24 +113,17 @@ void StrokeView::drawNoPressure()
cairo_set_line_width(cr, width * scaleFactor);
applyDashed(0);
Point lastPoint = *points;
while (points != s->iteratorEnd())
for (auto&& point: s->getPointVector())
{
Point p = *points;
if (startPoint <= count)
{
cairo_line_to(cr, p.x, p.y);
cairo_line_to(cr, point.x, point.y);
}
else
{
cairo_move_to(cr, p.x, p.y);
cairo_move_to(cr, point.x, point.y);
}
count++;
lastPoint = p;
points++;
}
cairo_stroke(cr);
@ -174,35 +152,33 @@ void StrokeView::drawWithPressuire()
{
int count = 1;
double width = s->getWidth();
std::vector<Point>::const_iterator points = s->iteratorBegin();
Point lastPoint1 = *points;
points++;
Point lastPoint1;
double dashOffset = 0;
while (points != s->iteratorEnd())
{
Point p = *points;
points++;
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, p.x, p.y);
cairo_stroke(cr);
}
count++;
dashOffset += lastPoint1.lineLengthTo(p);
lastPoint1 = p;
}
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);
}
@ -229,4 +205,3 @@ void StrokeView::paint(bool dontRenderEditingStroke)
drawWithPressuire();
}
}

Loading…
Cancel
Save