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

#include <kis_four_point_interpolator_backward.h>

Public Member Functions

QPointF fallbackSourcePoint () const
 
QPointF getValue () const
 
bool isValid (const qreal tolerance=0.1) const
 
 KisFourPointInterpolatorBackward (const QPolygonF &srcPolygon, const QPolygonF &dstPolygon)
 
QPointF map (const QPointF &pt)
 
void setX (qreal x)
 
void setY (qreal y)
 

Private Member Functions

QPointF getResult (qreal nu, qreal mu) const
 
bool inGoodRange (qreal value) const
 
qreal xBasedDenom (qreal nu) const
 
qreal xBasedMu (qreal nu, qreal xBasedDenominator) const
 
qreal yBasedDenom (qreal nu) const
 
qreal yBasedMu (qreal nu, qreal yBasedDenominator) const
 

Private Attributes

QPointF m_a
 
QPointF m_b
 
QPointF m_c
 
QPointF m_d
 
QPointF m_dstBase
 
qreal m_px {0.0}
 
qreal m_py {0.0}
 
qreal m_qA {0.0}
 
qreal m_qB_const {0.0}
 
qreal m_qB_varX {0.0}
 
qreal m_qB_varY {0.0}
 
qreal m_qC_varX {0.0}
 
qreal m_qC_varY {0.0}
 
qreal m_qD_div {0.0}
 
QPointF m_srcBase
 
qreal m_xCoeff {0.0}
 
qreal m_yCoeff {0.0}
 

Detailed Description

A--—B The polygons must be initialized in this order: | | | | polygon << A << B << D << C; C--—D

Definition at line 27 of file kis_four_point_interpolator_backward.h.

Constructor & Destructor Documentation

◆ KisFourPointInterpolatorBackward()

KisFourPointInterpolatorBackward::KisFourPointInterpolatorBackward ( const QPolygonF & srcPolygon,
const QPolygonF & dstPolygon )
inline

Definition at line 30 of file kis_four_point_interpolator_backward.h.

30 {
31#ifdef FPIB_DEBUG
32 m_dbgSrcPolygon = srcPolygon;
33 m_dbgDstPolygon = dstPolygon;
34#endif
35
36 m_a = dstPolygon[1] - dstPolygon[0]; // AB
37 m_b = dstPolygon[2] - dstPolygon[1]; // BD
38 m_c = dstPolygon[3] - dstPolygon[0]; // AC
39 m_d = m_b - m_c; // BD - AC
40
41 m_qA = m_c.x() * m_d.y() - m_c.y() * m_d.x();
42
43 m_srcBase = srcPolygon[0];
44 m_dstBase = dstPolygon[0];
45 m_xCoeff = srcPolygon[1].x() - srcPolygon[0].x(); // AB_src
46 m_yCoeff = srcPolygon[3].y() - srcPolygon[0].y(); // AC_src
47
48 m_qB_const = m_c.x() * m_a.y() - m_c.y() * m_a.x();
49
50 m_qD_div = 1.0 / (2 * m_qA);
51 }

References m_a, m_b, m_c, m_d, m_dstBase, m_qA, m_qB_const, m_qD_div, m_srcBase, m_xCoeff, and m_yCoeff.

Member Function Documentation

◆ fallbackSourcePoint()

QPointF KisFourPointInterpolatorBackward::fallbackSourcePoint ( ) const
inline

Definition at line 66 of file kis_four_point_interpolator_backward.h.

66 {
67 return m_srcBase + QPointF(0.5 * m_xCoeff, 0.5 * m_yCoeff);
68 }

References m_srcBase, m_xCoeff, and m_yCoeff.

◆ getResult()

QPointF KisFourPointInterpolatorBackward::getResult ( qreal nu,
qreal mu ) const
inlineprivate

Definition at line 234 of file kis_four_point_interpolator_backward.h.

234 {
235 return m_srcBase + QPointF(mu * m_xCoeff, nu * m_yCoeff);
236 }

References m_srcBase, m_xCoeff, and m_yCoeff.

◆ getValue()

QPointF KisFourPointInterpolatorBackward::getValue ( ) const
inline

Definition at line 99 of file kis_four_point_interpolator_backward.h.

99 {
100 static const qreal eps = 1e-6; // pixels in Krita only get to 32-33k in every direction
101
102
103 qreal qB = m_qB_const + m_qB_varX + m_qB_varY;
104 qreal qC = m_qC_varX + m_qC_varY;
105
106 qreal nu = 0.0;
107 qreal D = 0.0;
108 qreal sqrtD = 0.0;
109
110 bool dontCheckOtherNu = false;
111
112 if (qAbs(m_qA) < eps) {
113 nu = -qC / qB;
114 dontCheckOtherNu = true;
115 } else {
116 D = pow2(qB) - 4 * m_qA * qC;
117 if (D > 0.0) {
118 sqrtD = std::sqrt(D);
119 nu = (-qB - sqrtD) * m_qD_div;
120 } else {
121 nu = 0.0;
122 dontCheckOtherNu = true;
123 }
124 }
125
126 qreal nu1 = nu;
127
128 qreal xDenomNu1 = xBasedDenom(nu1);
129 qreal xMu1 = xBasedMu(nu1, xDenomNu1);
130
131 bool goodNu1 = inGoodRange(nu1);
132
133 if (goodNu1 && inGoodRange(xMu1)) {
134 return getResult(nu1, xMu1);
135 }
136
137 qreal yDenomNu1 = yBasedDenom(nu1);
138 qreal yMu1 = yBasedMu(nu1, yDenomNu1);
139
140
141 if (goodNu1 && inGoodRange(yMu1)) {
142 return getResult(nu1, yMu1);
143 }
144
145 qreal nu2 = nu1, xDenomNu2 = xDenomNu1, xMu2 = xMu1, yDenomNu2 = yDenomNu1, yMu2 = yMu1;
146
147 if (!dontCheckOtherNu) {
148 nu2 = (-qB + sqrtD) * m_qD_div;
149
150 xDenomNu2 = xBasedDenom(nu2);
151 xMu2 = xBasedMu(nu2, xDenomNu2);
152
153 bool goodNu2 = inGoodRange(nu2);
154
155 if (goodNu2 && inGoodRange(xMu2)) {
156 return getResult(nu2, xMu2);
157 }
158
159 yDenomNu2 = yBasedDenom(nu2);
160 yMu2 = xBasedMu(nu2, yDenomNu2);
161
162 if (goodNu2 && inGoodRange(yMu2)) {
163 return getResult(nu2, yMu2);
164 }
165 }
166
167
168 const int count = 4;
169 qreal denoms[count] = {xDenomNu1, yDenomNu1, xDenomNu2, yDenomNu2};
170 qreal mus[count] = {xMu1, yMu1, xMu2, yMu2};
171 qreal nus[count] = {nu1, nu1, nu2, nu2};
172 QPointF results[count];
173#ifdef FPIB_DEBUG
174 qCritical() << "For point: x = " << m_dbgOrigX << " y = " << m_dbgOrigY << " | src polygon = " << m_dbgSrcPolygon << " | dst polygon = " << m_dbgDstPolygon;
175 for (int i = 0; i < count; i++) {
176 qCritical() << "for i = " << i << ": denoms[i] = " << denoms[i] << "mus[i] = " << mus[i] << "nus[i] = " << nus[i] << "result: " << getResult(nus[i], mus[i]);
177 }
178#endif
179
180 int bestI = -1;
181 qreal distanceFromCenter = 0.0;
182 QPointF center = fallbackSourcePoint();
183
184 int meaningfulCount = dontCheckOtherNu ? 2 : 4;
185 for (int i = 0; i < meaningfulCount; i++) {
186
187 if (qAbs(denoms[i]) < eps) {
188 continue;
189 }
190
191 results[i] = getResult(nus[i], mus[i]);
192 qreal dist = KisAlgebra2D::normSquared(center - results[i]);
193 if (bestI < 0 || dist < distanceFromCenter) {
194 distanceFromCenter = dist;
195 bestI = i;
196 }
197 }
198#ifdef FPIB_DEBUG
199 if (bestI < 0) {
200 for (int i = 0; i < count; i++) {
201 qCritical() << "for i = " << i << "denom=" << denoms[i] << "mu=" << mus[i] << "nu=" << nus[i] << "result: " << getResult(nus[i], mus[i]);
202 }
203 qCritical() << "Center point: " << center;
204 qCritical() << "dont check other nu: " << dontCheckOtherNu;
205
206 }
207#endif
208
209 // there won't be any sane result here in case we didn't find any bestI
210 KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(bestI >= 0, center);
211
212 return results[bestI];
213 }
qreal D(qreal t, const QPointF &P0, const QPointF &P1, const QPointF &P2, const QPointF &P3, const QPointF &p)
qreal yBasedMu(qreal nu, qreal yBasedDenominator) const
qreal xBasedMu(qreal nu, qreal xBasedDenominator) const
#define KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(cond, val)
Definition kis_assert.h:129
const qreal eps
T pow2(const T &x)
Definition kis_global.h:166
qreal normSquared(const T &a)

References D(), eps, fallbackSourcePoint(), getResult(), inGoodRange(), KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE, m_qA, m_qB_const, m_qB_varX, m_qB_varY, m_qC_varX, m_qC_varY, m_qD_div, KisAlgebra2D::normSquared(), pow2(), xBasedDenom(), xBasedMu(), yBasedDenom(), and yBasedMu().

◆ inGoodRange()

bool KisFourPointInterpolatorBackward::inGoodRange ( qreal value) const
inlineprivate

Definition at line 238 of file kis_four_point_interpolator_backward.h.

238 {
239 return value >= 0.0 && value <= 1.0;
240 }
float value(const T *src, size_t ch)

References value().

◆ isValid()

bool KisFourPointInterpolatorBackward::isValid ( const qreal tolerance = 0.1) const
inline

Checks if linear dimensions of the destination polygon are bigger than tolerance.

Definition at line 57 of file kis_four_point_interpolator_backward.h.

57 {
58 const qreal toleranceSq = pow2(tolerance);
59
60 const qreal sq1 = qAbs(m_qB_const);
61 const qreal sq2 = qAbs(KisAlgebra2D::crossProduct(m_b, m_c - m_b + m_a));
62
63 return sq1 + sq2 > 2 * toleranceSq;
64 }
PointTypeTraits< T >::value_type crossProduct(const T &a, const T &b)

References KisAlgebra2D::crossProduct(), m_a, m_b, m_c, m_qB_const, and pow2().

◆ map()

QPointF KisFourPointInterpolatorBackward::map ( const QPointF & pt)
inline

◆ setX()

void KisFourPointInterpolatorBackward::setX ( qreal x)
inline

Definition at line 76 of file kis_four_point_interpolator_backward.h.

76 {
77#ifdef FPIB_DEBUG
78 m_dbgOrigX = x;
79#endif
80 x -= m_dstBase.x();
81
82 m_qB_varX = - x * m_d.y();
83 m_qC_varX = - x * m_a.y();
84 m_px = x;
85 }

References m_a, m_d, m_dstBase, m_px, m_qB_varX, and m_qC_varX.

◆ setY()

void KisFourPointInterpolatorBackward::setY ( qreal y)
inline

Definition at line 87 of file kis_four_point_interpolator_backward.h.

87 {
88#ifdef FPIB_DEBUG
89 m_dbgOrigY = y;
90#endif
91 y -= m_dstBase.y();
92
93 m_qB_varY = y * m_d.x();
94 m_qC_varY = y * m_a.x();
95 m_py = y;
96 }

References m_a, m_d, m_dstBase, m_py, m_qB_varY, and m_qC_varY.

◆ xBasedDenom()

qreal KisFourPointInterpolatorBackward::xBasedDenom ( qreal nu) const
inlineprivate

Definition at line 218 of file kis_four_point_interpolator_backward.h.

218 {
219 return m_a.x() + nu * m_d.x();
220 }

References m_a, and m_d.

◆ xBasedMu()

qreal KisFourPointInterpolatorBackward::xBasedMu ( qreal nu,
qreal xBasedDenominator ) const
inlineprivate

Definition at line 226 of file kis_four_point_interpolator_backward.h.

226 {
227 return (m_px - nu * m_c.x()) / xBasedDenominator;
228 }

References m_c, and m_px.

◆ yBasedDenom()

qreal KisFourPointInterpolatorBackward::yBasedDenom ( qreal nu) const
inlineprivate

Definition at line 222 of file kis_four_point_interpolator_backward.h.

222 {
223 return m_a.y() + nu * m_d.y();
224 }

References m_a, and m_d.

◆ yBasedMu()

qreal KisFourPointInterpolatorBackward::yBasedMu ( qreal nu,
qreal yBasedDenominator ) const
inlineprivate

Definition at line 230 of file kis_four_point_interpolator_backward.h.

230 {
231 return (m_py - nu * m_c.y()) / yBasedDenominator;
232 }

References m_c, and m_py.

Member Data Documentation

◆ m_a

QPointF KisFourPointInterpolatorBackward::m_a
private

Definition at line 244 of file kis_four_point_interpolator_backward.h.

◆ m_b

QPointF KisFourPointInterpolatorBackward::m_b
private

Definition at line 245 of file kis_four_point_interpolator_backward.h.

◆ m_c

QPointF KisFourPointInterpolatorBackward::m_c
private

Definition at line 246 of file kis_four_point_interpolator_backward.h.

◆ m_d

QPointF KisFourPointInterpolatorBackward::m_d
private

Definition at line 247 of file kis_four_point_interpolator_backward.h.

◆ m_dstBase

QPointF KisFourPointInterpolatorBackward::m_dstBase
private

Definition at line 260 of file kis_four_point_interpolator_backward.h.

◆ m_px

qreal KisFourPointInterpolatorBackward::m_px {0.0}
private

Definition at line 256 of file kis_four_point_interpolator_backward.h.

256{0.0}; // saved relative X coordinate

◆ m_py

qreal KisFourPointInterpolatorBackward::m_py {0.0}
private

Definition at line 257 of file kis_four_point_interpolator_backward.h.

257{0.0}; // saved relative Y coordinate

◆ m_qA

qreal KisFourPointInterpolatorBackward::m_qA {0.0}
private

Definition at line 249 of file kis_four_point_interpolator_backward.h.

249{0.0}; // quadratic equation A coeff

◆ m_qB_const

qreal KisFourPointInterpolatorBackward::m_qB_const {0.0}
private

Definition at line 250 of file kis_four_point_interpolator_backward.h.

250{0.0}; // quadratic equation B coeff, const part

◆ m_qB_varX

qreal KisFourPointInterpolatorBackward::m_qB_varX {0.0}
private

Definition at line 251 of file kis_four_point_interpolator_backward.h.

251{0.0}; // quadratic equation B coeff, X-dep part

◆ m_qB_varY

qreal KisFourPointInterpolatorBackward::m_qB_varY {0.0}
private

Definition at line 252 of file kis_four_point_interpolator_backward.h.

252{0.0}; // quadratic equation B coeff, Y-dep part

◆ m_qC_varX

qreal KisFourPointInterpolatorBackward::m_qC_varX {0.0}
private

Definition at line 253 of file kis_four_point_interpolator_backward.h.

253{0.0}; // quadratic equation C coeff, X-dep part

◆ m_qC_varY

qreal KisFourPointInterpolatorBackward::m_qC_varY {0.0}
private

Definition at line 254 of file kis_four_point_interpolator_backward.h.

254{0.0}; // quadratic equation C coeff, Y-dep part

◆ m_qD_div

qreal KisFourPointInterpolatorBackward::m_qD_div {0.0}
private

Definition at line 255 of file kis_four_point_interpolator_backward.h.

255{0.0}; // inverted divisor of the quadratic equation solution

◆ m_srcBase

QPointF KisFourPointInterpolatorBackward::m_srcBase
private

Definition at line 259 of file kis_four_point_interpolator_backward.h.

◆ m_xCoeff

qreal KisFourPointInterpolatorBackward::m_xCoeff {0.0}
private

Definition at line 261 of file kis_four_point_interpolator_backward.h.

261{0.0};

◆ m_yCoeff

qreal KisFourPointInterpolatorBackward::m_yCoeff {0.0}
private

Definition at line 262 of file kis_four_point_interpolator_backward.h.

262{0.0};

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