Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_cubic_curve.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2005 C. Boemann <cbo@boemann.dk>
3 * SPDX-FileCopyrightText: 2009 Dmitry Kazakov <dimula73@gmail.com>
4 * SPDX-FileCopyrightText: 2010 Cyrille Berger <cberger@cberger.net>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "kis_cubic_curve.h"
10
11#include <QPointF>
12#include <QList>
13#include <QSharedData>
14#include <QStringList>
15#include "kis_dom_utils.h"
16#include "kis_algebra_2d.h"
17
18KisCubicCurvePoint::KisCubicCurvePoint(const QPointF &position, bool setAsCorner)
19 : m_position(position), m_isCorner(setAsCorner)
20{}
21
22KisCubicCurvePoint::KisCubicCurvePoint(qreal x, qreal y, bool setAsCorner)
23 : m_position(x, y), m_isCorner(setAsCorner)
24{}
25
27{
28 return m_position == other.m_position && m_isCorner == other.m_isCorner;
29}
30
32{
33 return m_position.x();
34}
35
37{
38 return m_position.y();
39}
40
41const QPointF& KisCubicCurvePoint::position() const
42{
43 return m_position;
44}
45
47{
48 return m_isCorner;
49}
50
52{
53 m_position.setX(newX);
54}
55
57{
58 m_position.setY(newY);
59}
60
61void KisCubicCurvePoint::setPosition(const QPointF &newPosition)
62{
63 m_position = newPosition;
64}
65
66void KisCubicCurvePoint::setAsCorner(bool newIsSetAsCorner)
67{
68 m_isCorner = newIsSetAsCorner;
69}
70
72{
73 return a.x() < b.x();
74}
75
76struct Q_DECL_HIDDEN KisCubicCurve::Data : public QSharedData {
77 Data() {
78 }
79 Data(const Data& data) : QSharedData() {
80 points = data.points;
81 name = data.name;
82 }
84 }
85
86 mutable QString name;
89 mutable bool validSpline {false};
91 mutable bool validU8Transfer {false};
93 mutable bool validU16Transfer {false};
95 mutable bool validFTransfer {false};
96
98 void keepSorted();
99 qreal value(qreal x);
101
102 template<typename _T_, typename _T2_>
103 void updateTransfer(QVector<_T_>* transfer, bool& valid, _T2_ min, _T2_ max, int size);
104};
105
106void KisCubicCurve::Data::updateSpline()
107{
108 if (validSpline) return;
109 validSpline = true;
110 spline.createSpline(points);
111}
112
113void KisCubicCurve::Data::invalidate()
114{
115 validSpline = false;
116 validFTransfer = false;
117 validU16Transfer = false;
118}
119
120void KisCubicCurve::Data::keepSorted()
121{
122 std::sort(points.begin(), points.end(), pointLessThan);
123}
124
125qreal KisCubicCurve::Data::value(qreal x)
126{
127 updateSpline();
128 /* Automatically extend non-existing parts of the curve
129 * (e.g. before the first point) and cut off big y-values
130 */
131 x = qBound(points.first().x(), x, points.last().x());
132 qreal y = spline.getValue(x);
133 return qBound(qreal(0.0), y, qreal(1.0));
134}
135
136template<typename _T_, typename _T2_>
137void KisCubicCurve::Data::updateTransfer(QVector<_T_>* transfer, bool& valid, _T2_ min, _T2_ max, int size)
138{
139 if (!valid || transfer->size() != size) {
140 if (transfer->size() != size) {
141 transfer->resize(size);
142 }
143 qreal end = 1.0 / (size - 1);
144 for (int i = 0; i < size; ++i) {
145 /* Direct uncached version */
146 _T2_ val = value(i * end ) * max;
147 val = qBound(min, val, max);
148 (*transfer)[i] = val;
149 }
150 valid = true;
151 }
152}
153
154struct Q_DECL_HIDDEN KisCubicCurve::Private {
155 QSharedDataPointer<Data> data;
156};
157
159 : d(new Private)
160{
161 d->data = new Data;
162 d->data->points.append({ 0.0, 0.0, false });
163 d->data->points.append({ 1.0, 1.0, false });
164}
165
167 : KisCubicCurve(points, false)
168{
169}
170
171KisCubicCurve::KisCubicCurve(const QList<QPointF>& points, bool setAsCorner)
172 : d(new Private)
173{
174 d->data = new Data;
175 d->data->points.reserve(points.size());
176 Q_FOREACH(const QPointF p, points) {
177 d->data->points.append({ p, setAsCorner });
178 }
179 d->data->keepSorted();
180}
181
183 : d(new Private)
184{
185 d->data = new Data;
186 d->data->points = points;
187 d->data->keepSorted();
188}
189
191 : d(new Private(*curve.d))
192{
193}
194
195KisCubicCurve::KisCubicCurve(const QString &curveString)
196 : d(new Private)
197{
198 // Curve string format: a semi-colon separated list of point entries.
199 // Previous point entry format: a pair of numbers, separated by a comma,
200 // that represent the x and y coordinates of the point, in that order.
201 // Examples: identity "0.0,0.0;1.0,1.0;"
202 // U-shaped curve "0.0,1.0;0.5,0.0;1.0,1.0"
203 // New point entry format: a list of comma separated point properties. The
204 // first and second properties are required and should be numbers
205 // that represent the x and y coordinates of the point, in that order.
206 // After those, some optional properties/flags may be present or not.
207 // If there are no optional properties for any point entry, then the
208 // format of the string is identical to the old one.
209 // Currently, only the "is_corner" flag is supported. All other
210 // present optional properties are ignored.
211 // Examples: identity "0.0,0.0;1.0,1.0;"
212 // V-shaped curve "0.0,1.0;0.5,0.0,is_corner;1.0,1.0"
213
214 d->data = new Data;
215
216 KIS_SAFE_ASSERT_RECOVER(!curveString.isEmpty()) {
217 *this = KisCubicCurve();
218 return;
219 }
220
221 const QStringList data = curveString.split(';', Qt::SkipEmptyParts);
222
224 Q_FOREACH (const QString &entry, data) {
225 const QStringList entryData = entry.split(',', Qt::SkipEmptyParts);
226 KIS_SAFE_ASSERT_RECOVER(entryData.size() > 1) {
227 *this = KisCubicCurve();
228 return;
229 }
230 bool ok;
231 const qreal x = KisDomUtils::toDouble(entryData[0], &ok);
233 *this = KisCubicCurve();
234 return;
235 }
236 const qreal y = KisDomUtils::toDouble(entryData[1], &ok);
238 *this = KisCubicCurve();
239 return;
240 }
241 bool isCorner = false;
242 for (int i = 2; i < entryData.size(); ++i) {
243 if (entryData[i] == "is_corner") {
244 isCorner = true;
245 }
246 }
247 points.append({ x, y, isCorner });
248 }
249
251}
252
254{
255 delete d;
256}
257
259{
260 if (&curve != this) {
261 *d = *curve.d;
262 }
263 return *this;
264}
265
267{
268 if (d->data == curve.d->data) return true;
269 return d->data->points == curve.d->data->points;
270}
271
272qreal KisCubicCurve::value(qreal x) const
273{
274 qreal value = d->data->value(x);
275 return value;
276}
277
279{
280 QList<QPointF> pointPositions;
281 Q_FOREACH(const KisCubicCurvePoint &point, d->data->points) {
282 pointPositions.append(point.position());
283 }
284 return pointPositions;
285}
286
288{
289 return d->data->points;
290}
291
293{
294 d->data.detach();
295 d->data->points.clear();
296 Q_FOREACH(const QPointF &p, points) {
297 d->data->points.append({ p, false });
298 }
299 d->data->invalidate();
300}
301
303{
304 d->data.detach();
305 d->data->points = points;
306 d->data->invalidate();
307}
308
310{
311 d->data.detach();
312 d->data->points[idx] = point;
313 d->data->keepSorted();
314 d->data->invalidate();
315}
316
317void KisCubicCurve::setPoint(int idx, const QPointF& position, bool setAsCorner)
318{
319 setPoint(idx, { position, setAsCorner });
320}
321
322void KisCubicCurve::setPoint(int idx, const QPointF& position)
323{
324 setPointPosition(idx, position);
325}
326
327void KisCubicCurve::setPointPosition(int idx, const QPointF& position)
328{
329 d->data.detach();
330 d->data->points[idx].setPosition(position);
331 d->data->keepSorted();
332 d->data->invalidate();
333}
334
335void KisCubicCurve::setPointAsCorner(int idx, bool setAsCorner)
336{
337 d->data.detach();
338 d->data->points[idx].setAsCorner(setAsCorner);
339 d->data->invalidate();
340}
341
343{
344 d->data.detach();
345 d->data->points.append(point);
346 d->data->keepSorted();
347 d->data->invalidate();
348
349 return d->data->points.indexOf(point);
350}
351
352int KisCubicCurve::addPoint(const QPointF& position, bool setAsCorner)
353{
354 return addPoint({ position, setAsCorner });
355}
356
357int KisCubicCurve::addPoint(const QPointF& position)
358{
359 return addPoint({ position, false });
360}
361
363{
364 d->data.detach();
365 d->data->points.removeAt(idx);
366 d->data->invalidate();
367}
368
370{
371 const QList<KisCubicCurvePoint> &points = d->data->points;
372
373 if (points.first().x() != 0.0 || points.first().y() != 0.0 ||
374 points.last().x() != 1.0 || points.last().y() != 1.0) {
375 return false;
376 }
377
378 for (int i = 1; i < points.size() - 1; i++) {
379 if (!qFuzzyCompare(points[i].x(), points[i].y())) {
380 return false;
381 }
382 }
383
384 return true;
385}
386
387bool KisCubicCurve::isConstant(qreal c) const
388{
389 const QList<KisCubicCurvePoint> &points = d->data->points;
390
391 Q_FOREACH (const KisCubicCurvePoint &pt, points) {
392 if (!qFuzzyCompare(c, pt.y())) {
393 return false;
394 }
395 }
396
397 return true;
398}
399
400const QString& KisCubicCurve::name() const
401{
402 return d->data->name;
403}
404
405qreal KisCubicCurve::interpolateLinear(qreal normalizedValue, const QVector<qreal> &transfer)
406{
407 const qreal maxValue = transfer.size() - 1;
408
409 const qreal bilinearX = qBound(0.0, maxValue * normalizedValue, maxValue);
410 const qreal xFloored = std::floor(bilinearX);
411 const qreal xCeiled = std::ceil(bilinearX);
412
413 const qreal t = bilinearX - xFloored;
414
415 constexpr qreal eps = 1e-6;
416
417 qreal newValue = normalizedValue;
418
419 if (t < eps) {
420 newValue = transfer[int(xFloored)];
421 } else if (t > (1.0 - eps)) {
422 newValue = transfer[int(xCeiled)];
423 } else {
424 qreal a = transfer[int(xFloored)];
425 qreal b = transfer[int(xCeiled)];
426
427 newValue = a + t * (b - a);
428 }
429
430 return KisAlgebra2D::copysign(newValue, normalizedValue);
431}
432
433void KisCubicCurve::setName(const QString& name)
434{
435 d->data->name = name;
436}
437
439{
440 // See comments in KisCubicCurve(const QString &curveString) for the
441 // specification of the string format
442
443 QString sCurve;
444
445 if(d->data->points.count() < 1)
446 return sCurve;
447
448 Q_FOREACH (const KisCubicCurvePoint &point, d->data->points) {
449 sCurve += QString::number(point.x());
450 sCurve += ',';
451 sCurve += QString::number(point.y());
452 if (point.isSetAsCorner()) {
453 sCurve += ',';
454 sCurve += "is_corner";
455 }
456 sCurve += ';';
457 }
458
459 return sCurve;
460}
461
462void KisCubicCurve::fromString(const QString& string)
463{
464 *this = KisCubicCurve(string);
465}
466
468{
469 d->data->updateTransfer<quint16, int>(&d->data->u16Transfer, d->data->validU16Transfer, 0x0, 0xFFFF, size);
470 return d->data->u16Transfer;
471}
472
474{
475 d->data->updateTransfer<qreal, qreal>(&d->data->fTransfer, d->data->validFTransfer, 0.0, 1.0, size);
476 return d->data->fTransfer;
477}
float value(const T *src, size_t ch)
const Params2D p
bool operator==(const KisCubicCurvePoint &other) const
KisCubicCurvePoint()=default
void setX(qreal newX)
void setY(qreal newY)
void setAsCorner(bool newIsSetAsCorner)
const QPointF & position() const
bool isSetAsCorner() const
void setPosition(const QPointF &newPosition)
static bool qFuzzyCompare(half p1, half p2)
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126
static bool pointLessThan(const KisCubicCurvePoint &a, const KisCubicCurvePoint &b)
const qreal eps
T copysign(T x, T y)
double toDouble(const QString &str, bool *ok=nullptr)
int size(const Forest< T > &forest)
Definition KisForest.h:1232
constexpr std::enable_if< sizeof...(values)==0, size_t >::type max()
bool operator==(const KisCubicCurve &curve) const
QString toString() const
QVector< quint16 > u16Transfer
const QVector< quint16 > uint16Transfer(int size=256) const
void setPoint(int idx, const KisCubicCurvePoint &point)
const QVector< qreal > floatTransfer(int size=256) const
Data(const Data &data)
void setPointPosition(int idx, const QPointF &position)
QVector< quint8 > u8Transfer
KisCubicCurve & operator=(const KisCubicCurve &curve)
QList< KisCubicCurvePoint > points
Q_DECL_DEPRECATED void fromString(const QString &)
void setPointAsCorner(int idx, bool setAsCorner)
void setPoints(const QList< QPointF > &points)
void setName(const QString &name)
void updateTransfer(QVector< _T_ > *transfer, bool &valid, _T2_ min, _T2_ max, int size)
void removePoint(int idx)
void keepSorted()
bool isConstant(qreal c) const
KisCubicSpline< KisCubicCurvePoint, qreal > spline
QSharedDataPointer< Data > data
static qreal interpolateLinear(qreal normalizedValue, const QVector< qreal > &transfer)
void updateSpline()
QVector< qreal > fTransfer
qreal value(qreal x)
void invalidate()
const QList< KisCubicCurvePoint > & curvePoints() const
int addPoint(const KisCubicCurvePoint &point)
bool isIdentity() const
Private *const d