File indexing completed on 2024-05-12 15:58:16

0001 /*
0002  *  SPDX-FileCopyrightText: 2014 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef __KIS_FOUR_POINT_INTERPOLATOR_FORWARD_H
0008 #define __KIS_FOUR_POINT_INTERPOLATOR_FORWARD_H
0009 
0010 #include <QPolygon>
0011 #include <QPointF>
0012 
0013 
0014 
0015 /**
0016  *    A-----B         The polygons must be initialized in this order:
0017  *    |     |
0018  *    |     |         polygon << A << B << D << C;
0019  *    C-----D
0020  */
0021 
0022 class KisFourPointInterpolatorForward
0023 {
0024 public:
0025     KisFourPointInterpolatorForward(const QPolygonF &srcPolygon, const QPolygonF &dstPolygon) {
0026         m_srcBase = srcPolygon[0];
0027         m_dstBase = dstPolygon[0];
0028 
0029         m_h0 = dstPolygon[1] - dstPolygon[0]; // BA
0030         m_h1 = dstPolygon[2] - dstPolygon[3]; // DC
0031 
0032         m_v0 = dstPolygon[3] - dstPolygon[0]; // CA
0033 
0034         m_forwardCoeffX = 1.0 / (srcPolygon[1].x() - srcPolygon[0].x());
0035         m_forwardCoeffY = 1.0 / (srcPolygon[3].y() - srcPolygon[0].y());
0036 
0037         m_xProp = 0;
0038         m_yProp = 0;
0039     }
0040 
0041     inline QPointF map(const QPointF &pt) {
0042         setX(pt.x());
0043         setY(pt.y());
0044         return getValue();
0045     }
0046 
0047     inline void setX(qreal x) {
0048         qreal diff = x - m_srcBase.x();
0049         m_xProp = diff * m_forwardCoeffX;
0050     }
0051 
0052     inline void setY(qreal y) {
0053         qreal diff = y - m_srcBase.y();
0054         m_yProp = diff * m_forwardCoeffY;
0055     }
0056 
0057     inline QPointF getValue() const {
0058         QPointF dstPoint = m_dstBase +
0059             m_yProp * m_v0 +
0060             m_xProp * (m_yProp * m_h1 + (1.0 - m_yProp) * m_h0);
0061 
0062         return dstPoint;
0063     }
0064 
0065 private:
0066     QPointF m_srcBase;
0067     QPointF m_dstBase;
0068 
0069     QPointF m_h0;
0070     QPointF m_h1;
0071     QPointF m_v0;
0072 
0073     qreal m_forwardCoeffX;
0074     qreal m_forwardCoeffY;
0075 
0076     qreal m_xProp;
0077     qreal m_yProp;
0078 };
0079 
0080 #endif /* __KIS_FOUR_POINT_INTERPOLATOR_FORWARD_H */