Krita Source Code Documentation
Loading...
Searching...
No Matches
KisSpeedSmoother Class Reference

#include <kis_speed_smoother.h>

Classes

struct  Private
 

Public Member Functions

void clear ()
 
qreal getNextSpeed (const QPointF &pt, ulong timestamp)
 
 KisSpeedSmoother ()
 
qreal lastSpeed () const
 
void updateSettings ()
 
 ~KisSpeedSmoother ()
 

Private Member Functions

qreal getNextSpeedImpl (const QPointF &pt, qreal time)
 

Private Attributes

const QScopedPointer< Privatem_d
 

Detailed Description

Definition at line 15 of file kis_speed_smoother.h.

Constructor & Destructor Documentation

◆ KisSpeedSmoother()

KisSpeedSmoother::KisSpeedSmoother ( )

Definition at line 66 of file kis_speed_smoother.cpp.

68{
70}
const QScopedPointer< Private > m_d
#define MAX_SMOOTH_HISTORY

References updateSettings().

◆ ~KisSpeedSmoother()

KisSpeedSmoother::~KisSpeedSmoother ( )

Definition at line 72 of file kis_speed_smoother.cpp.

73{
74}

Member Function Documentation

◆ clear()

void KisSpeedSmoother::clear ( )

Definition at line 90 of file kis_speed_smoother.cpp.

91{
92 m_d->timer.restart();
93 m_d->distances.clear();
94 m_d->distances.push_back(Private::DistancePoint(0.0, 0.0));
95 m_d->lastPoint = QPointF();
96 m_d->lastSpeed = 0.0;
97}

References m_d.

◆ getNextSpeed()

qreal KisSpeedSmoother::getNextSpeed ( const QPointF & pt,
ulong timestamp )

Definition at line 81 of file kis_speed_smoother.cpp.

82{
83 const qreal time = m_d->useTimestamps ?
84 qreal(timestamp) :
85 qreal(m_d->timer.nsecsElapsed()) / 1000000;
86
87 return getNextSpeedImpl(pt, time);
88}
qreal getNextSpeedImpl(const QPointF &pt, qreal time)

References getNextSpeedImpl(), and m_d.

◆ getNextSpeedImpl()

qreal KisSpeedSmoother::getNextSpeedImpl ( const QPointF & pt,
qreal time )
private

Mind you, we don't care about the specific timestamps of the tablet events! They are not reliable. Instead, we are trying to estimate the sample rate of the tablet itself using the filtered mean accumulator. It works in an assumption that all the tablets generate events at a fixed sample rate.

Definition at line 107 of file kis_speed_smoother.cpp.

108{
109 if (m_d->lastPoint.isNull()) {
110 m_d->lastPoint = pt;
111 m_d->lastTime = time;
112 m_d->lastSpeed = 0.0;
113 return 0.0;
114 }
115
116 const qreal dist = kisDistance(pt, m_d->lastPoint);
117 // Getting the exact same position is bogus, it is probably just a previous
118 // point reported again. On Android, this happens all the time. Skip them.
119 if (qFuzzyIsNull(dist)) {
120 return m_d->lastSpeed;
121 }
122
123 const qreal timeDiff = time - m_d->lastTime;
124
125 m_d->timeDiffsMean.addValue(timeDiff);
126 const qreal avgTimeDiff = m_d->timeDiffsMean.filteredMean();
127
128 m_d->lastPoint = pt;
129 m_d->lastTime = time;
130
131 m_d->distances.push_back(Private::DistancePoint(dist, time));
132
133 Private::DistanceBuffer::const_reverse_iterator it = m_d->distances.rbegin();
134 Private::DistanceBuffer::const_reverse_iterator end = m_d->distances.rend();
135
136 qreal totalDistance = 0;
137 qreal totalTime = 0.0;
138 int itemsSearched = 0;
139
140 for (; it != end; ++it) {
141 itemsSearched++;
142 totalDistance += it->distance;
143
152 totalTime += avgTimeDiff;
153
154 if (itemsSearched > m_d->numSmoothingSamples &&
155 totalDistance > MIN_TRACKING_DISTANCE) {
156
157 break;
158 }
159 }
160
161 if (totalTime > 0 && totalDistance > MIN_TRACKING_DISTANCE) {
162 m_d->lastSpeed = totalDistance / totalTime;
163 }
164
165 return m_d->lastSpeed;
166}
static bool qFuzzyIsNull(half h)
qreal kisDistance(const QPointF &pt1, const QPointF &pt2)
Definition kis_global.h:190
#define MIN_TRACKING_DISTANCE

References kisDistance(), m_d, MIN_TRACKING_DISTANCE, and qFuzzyIsNull().

◆ lastSpeed()

qreal KisSpeedSmoother::lastSpeed ( ) const

Definition at line 76 of file kis_speed_smoother.cpp.

77{
78 return m_d->lastSpeed;
79}

References m_d.

◆ updateSettings()

void KisSpeedSmoother::updateSettings ( )

Definition at line 99 of file kis_speed_smoother.cpp.

100{
101
102 KisConfig cfg(true);
103 m_d->useTimestamps = cfg.readEntry("useTimestampsForBrushSpeed", false);
104 m_d->numSmoothingSamples = cfg.readEntry("speedValueSmoothing", 3);
105}

References m_d, and KisConfig::readEntry().

Member Data Documentation

◆ m_d

const QScopedPointer<Private> KisSpeedSmoother::m_d
private

Definition at line 31 of file kis_speed_smoother.h.


The documentation for this class was generated from the following files: