Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • tool
strokeinterpolator.cpp
1/***************************************************************************
2 * This code is heavily influenced by the instrument proxy from QAquarelle *
3 * QAquarelle - Copyright (C) 2009 by Anton R. <commanderkyle@gmail.com> *
4 * *
5 * QAquarelle is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#include "strokeinterpolator.h"
22
23#include <QLineF>
24#include <QPainterPath>
25#include "object.h"
26#include "pointerevent.h"
27
28StrokeInterpolator::StrokeInterpolator()
29{
30 mTabletInUse = false;
31 mTabletPressure = 0;
32
33 reset();
34 connect(&timer, &QTimer::timeout, this, &StrokeInterpolator::interpolatePollAndPaint);
35}
36
37void StrokeInterpolator::reset()
38{
39 mStrokeStarted = false;
40 pressureQueue.clear();
41 strokeQueue.clear();
42 pressure = 0.0f;
43 mHasTangent = false;
44 timer.stop();
45 mStabilizerLevel = -1;
46}
47
48void StrokeInterpolator::setPressure(float pressure)
49{
50 mTabletPressure = pressure;
51}
52
53void StrokeInterpolator::pointerPressEvent(PointerEvent* event)
54{
55 reset();
56 if (!(event->button() == Qt::NoButton)) // if the user is pressing the left/right button
57 {
58 mCurrentPressPixel = event->viewportPos();
59 }
60
61 mLastPixel = mCurrentPixel = event->viewportPos();
62
63 mStrokeStarted = true;
64 setPressure(event->pressure());
65
66 mTabletInUse = mTabletInUse || event->isTabletEvent();
67}
68
69void StrokeInterpolator::pointerMoveEvent(PointerEvent* event)
70{
71 // only applied to drawing tools.
72 if (mStabilizerLevel != -1)
73 {
74 smoothMousePos(event->viewportPos());
75 }
76 else
77 {
78 // No smoothing
79 mLastPixel = mCurrentPixel;
80 mCurrentPixel = event->viewportPos();
81 mLastInterpolated = mCurrentPixel;
82 }
83 if(event->isTabletEvent())
84 {
85 setPressure(event->pressure());
86 }
87}
88
89void StrokeInterpolator::pointerReleaseEvent(PointerEvent* event)
90{
91 // flush out stroke
92 if (mStrokeStarted)
93 {
94 pointerMoveEvent(event);
95 }
96
97 mStrokeStarted = false;
98 mTabletInUse = mTabletInUse && !event->isTabletEvent();
99}
100
101void StrokeInterpolator::setStabilizerLevel(int level)
102{
103 mStabilizerLevel = level;
104}
105
106void StrokeInterpolator::smoothMousePos(QPointF pos)
107{
108 // Smooth mouse position before drawing
109 QPointF smoothPos;
110
111 if (mStabilizerLevel == StabilizationLevel::NONE)
112 {
113 mLastPixel = mCurrentPixel;
114 mCurrentPixel = pos;
115 mLastInterpolated = mCurrentPixel;
116 }
117 else if (mStabilizerLevel == StabilizationLevel::SIMPLE)
118 {
119 // simple interpolation
120 smoothPos = QPointF((pos.x() + mCurrentPixel.x()) / 2.0, (pos.y() + mCurrentPixel.y()) / 2.0);
121 mLastPixel = mCurrentPixel;
122 mCurrentPixel = smoothPos;
123 mLastInterpolated = mCurrentPixel;
124
125 // shift queue
126 while (strokeQueue.size() >= STROKE_QUEUE_LENGTH)
127 {
128 strokeQueue.pop_front();
129 }
130
131 strokeQueue.push_back(smoothPos);
132 }
133 else if (mStabilizerLevel == StabilizationLevel::STRONG)
134 {
135 smoothPos = QPointF((pos.x() + mLastInterpolated.x()) / 2.0, (pos.y() + mLastInterpolated.y()) / 2.0);
136
137 mLastInterpolated = mCurrentPixel;
138 mCurrentPixel = smoothPos;
139 mLastPixel = mLastInterpolated;
140 }
141
142 if (!mStrokeStarted)
143 {
144 return;
145 }
146
147 if (!mTabletInUse) // a mouse is used instead of a tablet
148 {
149 setPressure(1.0);
150 }
151}
152
153
154QPointF StrokeInterpolator::interpolateStart(QPointF firstPoint)
155{
156 if (mStabilizerLevel == StabilizationLevel::SIMPLE)
157 {
158 // Clear queue
159 strokeQueue.clear();
160 pressureQueue.clear();
161
162 mLastPixel = firstPoint;
163 }
164 else if (mStabilizerLevel == StabilizationLevel::STRONG)
165 {
166 // Clear queue
167 strokeQueue.clear();
168 pressureQueue.clear();
169
170 const int sampleSize = 5;
171 Q_ASSERT(sampleSize > 0);
172
173 // fill strokeQueue with firstPoint x times
174 for (int i = sampleSize; i > 0; i--)
175 {
176 strokeQueue.enqueue(firstPoint);
177 }
178
179 // last interpolated stroke should always be firstPoint
180 mLastInterpolated = firstPoint;
181
182 // draw and poll each millisecond
183 timer.setInterval(sampleSize);
184 timer.start();
185 }
186 else if (mStabilizerLevel == StabilizationLevel::NONE)
187 {
188 // Clear queue
189 strokeQueue.clear();
190 pressureQueue.clear();
191
192 mLastPixel = firstPoint;
193 }
194 return firstPoint;
195}
196
197void StrokeInterpolator::interpolatePoll()
198{
199 // remove oldest stroke
200 strokeQueue.dequeue();
201
202 // add new stroke with the last interpolated pixel position
203 strokeQueue.enqueue(mLastInterpolated);
204}
205
206void StrokeInterpolator::interpolatePollAndPaint()
207{
208 //qDebug() <<"inpol:" << mStabilizerLevel << "strokes"<< strokeQueue;
209 if (!strokeQueue.isEmpty())
210 {
211 interpolatePoll();
212 interpolateStroke();
213 }
214}
215
216QList<QPointF> StrokeInterpolator::interpolateStroke()
217{
218 // is nan initially
219 QList<QPointF> result;
220
221 if (mStabilizerLevel == StabilizationLevel::SIMPLE)
222 {
223 result = tangentInpolOp(result);
224
225 }
226 else if (mStabilizerLevel == StabilizationLevel::STRONG)
227 {
228 qreal x = 0;
229 qreal y = 0;
230 qreal pressure = 0;
231 result = meanInpolOp(result, x, y, pressure);
232
233 }
234 else if (mStabilizerLevel == StabilizationLevel::NONE)
235 {
236 result = noInpolOp(result);
237 }
238 return result;
239}
240
241QList<QPointF> StrokeInterpolator::noInpolOp(QList<QPointF> points)
242{
243 setPressure(getPressure());
244
245 points << mLastPixel << mLastPixel << mCurrentPixel << mCurrentPixel;
246
247 // Set lastPixel to CurrentPixel
248 // new interpolated pixel
249 mLastPixel = mCurrentPixel;
250
251 return points;
252}
253
254QList<QPointF> StrokeInterpolator::tangentInpolOp(QList<QPointF> points)
255{
256 static const qreal smoothness = 1.f;
257 QLineF line(mLastPixel, mCurrentPixel);
258
259 qreal scaleFactor = line.length() * 3.f;
260
261 if (!mHasTangent && scaleFactor > 0.01f)
262 {
263 mHasTangent = true;
264 /*
265 qDebug() << "scaleFactor" << scaleFactor
266 << "current pixel " << mCurrentPixel
267 << "last pixel" << mLastPixel;
268 */
269 m_previousTangent = (mCurrentPixel - mLastPixel) * smoothness / (3.0 * scaleFactor);
270 //qDebug() << "previous tangent" << m_previousTangent;
271 QLineF _line(QPointF(0, 0), m_previousTangent);
272 // don't bother for small tangents, as they can induce single pixel wobbliness
273 if (_line.length() < 2)
274 {
275 m_previousTangent = QPointF(0, 0);
276 }
277 }
278 else
279 {
280 QPointF c1 = mLastPixel + m_previousTangent * scaleFactor;
281 QPointF newTangent = (mCurrentPixel - c1) * smoothness / (3.0 * scaleFactor);
282 //qDebug() << "scalefactor1=" << scaleFactor << m_previousTangent << newTangent;
283 if (scaleFactor == 0)
284 {
285 newTangent = QPointF(0, 0);
286 }
287 else
288 {
289 //QLineF _line(QPointF(0,0), newTangent);
290 //if (_line.length() < 2)
291 //{
292 // newTangent = QPointF(0,0);
293 //}
294 }
295 QPointF c2 = mCurrentPixel - newTangent * scaleFactor;
296 //c1 = mLastPixel;
297 //c2 = mCurrentPixel;
298 points << mLastPixel << c1 << c2 << mCurrentPixel;
299 //qDebug() << mLastPixel << c1 << c2 << mCurrentPixel;
300 m_previousTangent = newTangent;
301 }
302
303 return points;
304}
305
306// Mean sampling interpolation operation
307QList<QPointF> StrokeInterpolator::meanInpolOp(QList<QPointF> points, qreal x, qreal y, qreal pressure)
308{
309 for (int i = 0; i < strokeQueue.size(); i++)
310 {
311 x += strokeQueue[i].x();
312 y += strokeQueue[i].y();
313 pressure += getPressure();
314 }
315
316 // get arithmetic mean of x, y and pressure
317 x /= strokeQueue.size();
318 y /= strokeQueue.size();
319 pressure /= strokeQueue.size();
320
321 // Use our interpolated points
322 QPointF mNewInterpolated(x, y);
323
324 points << mLastPixel << mLastInterpolated << mNewInterpolated << mCurrentPixel;
325
326 // Set lastPixel non interpolated pixel to our
327 // new interpolated pixel
328 mLastPixel = mNewInterpolated;
329
330 return points;
331}
332
333void StrokeInterpolator::interpolateEnd()
334{
335 // Stop timer
336 timer.stop();
337 if (mStabilizerLevel == StabilizationLevel::STRONG)
338 {
339 if (!strokeQueue.isEmpty())
340 {
341 // How many samples should we get point from?
342 // TODO: Qt slider.
343 int sampleSize = 5;
344
345 Q_ASSERT(sampleSize > 0);
346 for (int i = sampleSize; i > 0; i--)
347 {
348 interpolatePoll();
349 interpolateStroke();
350 }
351 }
352 }
353}
PointerEvent
Definition: pointerevent.h:8
QLineF
QList
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::event
virtual bool event(QEvent *e)
QPointF
QPointF::x
qreal x() const const
QPointF::y
qreal y() const const
QQueue::dequeue
T dequeue()
QQueue::enqueue
void enqueue(const T &t)
Qt::NoButton
NoButton
QTimer::setInterval
void setInterval(int msec)
QTimer::start
void start(int msec)
QTimer::stop
void stop()
QTimer::timeout
void timeout()
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39