Use QVector as the data container in Matrix
[GPXSee.git] / src / map / transform.cpp
blob9515d05e5069a7936733b85352868b2ace6b5063
1 #include "matrix.h"
2 #include "transform.h"
5 #define NULL_QTRANSFORM 0,0,0,0,0,0,0,0,0
7 void Transform::simple(const ReferencePoint &p1, const ReferencePoint &p2)
9 if (p1.xy().x() == p2.xy().x() || p1.xy().y() == p2.xy().y()) {
10 _errorString = "Invalid reference points tuple";
11 return;
14 double sX = (p1.xy().x() - p2.xy().x()) / (p1.pp().x() - p2.pp().x());
15 double sY = (p2.xy().y() - p1.xy().y()) / (p2.pp().y() - p1.pp().y());
16 double dX = p2.xy().x() - p2.pp().x() * sX;
17 double dY = p1.xy().y() - p1.pp().y() * sY;
19 _proj2img = QTransform(sX, 0, 0, sY, dX, dY);
20 _img2proj = _proj2img.inverted();
23 void Transform::affine(const QList<ReferencePoint> &points)
25 Matrix c(3, 2);
26 for (size_t i = 0; i < c.h(); i++) {
27 for (size_t j = 0; j < c.w(); j++) {
28 for (int k = 0; k < points.size(); k++) {
29 double f[3], t[2];
31 f[0] = points.at(k).pp().x();
32 f[1] = points.at(k).pp().y();
33 f[2] = 1.0;
34 t[0] = points.at(k).xy().x();
35 t[1] = points.at(k).xy().y();
36 c.m(i,j) += f[i] * t[j];
41 Matrix Q(3, 3);
42 for (int qi = 0; qi < points.size(); qi++) {
43 double v[3];
45 v[0] = points.at(qi).pp().x();
46 v[1] = points.at(qi).pp().y();
47 v[2] = 1.0;
48 for (size_t i = 0; i < Q.h(); i++)
49 for (size_t j = 0; j < Q.w(); j++)
50 Q.m(i,j) += v[i] * v[j];
53 Matrix M(Q.augemented(c));
54 if (!M.eliminate()) {
55 _errorString = "Singular transformation matrix";
56 return;
59 _proj2img = QTransform(M.m(0,3), M.m(0,4), M.m(1,3), M.m(1,4), M.m(2,3),
60 M.m(2,4));
61 _img2proj = _proj2img.inverted();
64 Transform::Transform()
65 : _proj2img(NULL_QTRANSFORM), _img2proj(NULL_QTRANSFORM)
69 Transform::Transform(const QList<ReferencePoint> &points)
70 : _proj2img(NULL_QTRANSFORM), _img2proj(NULL_QTRANSFORM)
72 if (points.count() < 2)
73 _errorString = "Insufficient number of reference points";
74 else if (points.size() == 2)
75 simple(points.at(0), points.at(1));
76 else
77 affine(points);
80 Transform::Transform(const ReferencePoint &p1, const ReferencePoint &p2)
81 : _proj2img(NULL_QTRANSFORM), _img2proj(NULL_QTRANSFORM)
83 simple(p1, p2);
86 Transform::Transform(const ReferencePoint &p, const PointD &scale)
87 : _proj2img(NULL_QTRANSFORM), _img2proj(NULL_QTRANSFORM)
89 if (scale.x() == 0.0 || scale.y() == 0.0) {
90 _errorString = "Invalid scale factor";
91 return;
94 _img2proj = QTransform(scale.x(), 0, 0, -scale.y(), p.pp().x() - p.xy().x()
95 / scale.x(), p.pp().y() + p.xy().x() / scale.y());
96 _proj2img = _img2proj.inverted();
99 Transform::Transform(double matrix[16])
100 : _proj2img(NULL_QTRANSFORM), _img2proj(NULL_QTRANSFORM)
102 _img2proj = QTransform(matrix[0], matrix[1], matrix[4], matrix[5],
103 matrix[3], matrix[7]);
104 if (!_img2proj.isInvertible())
105 _errorString = "Singular transformation matrix";
106 else
107 _proj2img = _img2proj.inverted();
110 #ifndef QT_NO_DEBUG
111 QDebug operator<<(QDebug dbg, const ReferencePoint &p)
113 dbg.nospace() << "ReferencePoint(" << p.xy() << ", " << p.pp() << ")";
114 return dbg.space();
116 #endif // QT_NO_DEBUG