1 #include "projection.h"
6 #define NULL_QTRANSFORM 0,0,0,0,0,0,0,0,0
8 void Transform::simple(const ReferencePoint
&p1
, const ReferencePoint
&p2
)
10 if (p1
.xy().x() == p2
.xy().x() || p1
.xy().y() == p2
.xy().y()) {
11 _errorString
= "Invalid reference points tuple";
15 double sX
= (p1
.xy().x() - p2
.xy().x()) / (p1
.pp().x() - p2
.pp().x());
16 double sY
= (p2
.xy().y() - p1
.xy().y()) / (p2
.pp().y() - p1
.pp().y());
17 double dX
= p2
.xy().x() - p2
.pp().x() * sX
;
18 double dY
= p1
.xy().y() - p1
.pp().y() * sY
;
20 _proj2img
= QTransform(sX
, 0, 0, sY
, dX
, dY
);
21 _img2proj
= _proj2img
.inverted();
24 void Transform::affine(const QList
<ReferencePoint
> &points
)
28 for (size_t i
= 0; i
< c
.h(); i
++) {
29 for (size_t j
= 0; j
< c
.w(); j
++) {
30 for (int k
= 0; k
< points
.size(); k
++) {
33 f
[0] = points
.at(k
).pp().x();
34 f
[1] = points
.at(k
).pp().y();
36 t
[0] = points
.at(k
).xy().x();
37 t
[1] = points
.at(k
).xy().y();
38 c
.m(i
,j
) += f
[i
] * t
[j
];
45 for (int qi
= 0; qi
< points
.size(); qi
++) {
48 v
[0] = points
.at(qi
).pp().x();
49 v
[1] = points
.at(qi
).pp().y();
51 for (size_t i
= 0; i
< Q
.h(); i
++)
52 for (size_t j
= 0; j
< Q
.w(); j
++)
53 Q
.m(i
,j
) += v
[i
] * v
[j
];
56 Matrix M
= Q
.augemented(c
);
58 _errorString
= "Singular transformation matrix";
62 _proj2img
= QTransform(M
.m(0,3), M
.m(0,4), M
.m(1,3), M
.m(1,4), M
.m(2,3),
64 _img2proj
= _proj2img
.inverted();
68 Transform::Transform()
69 : _proj2img(NULL_QTRANSFORM
), _img2proj(NULL_QTRANSFORM
)
73 Transform::Transform(const QList
<ReferencePoint
> &points
)
74 : _proj2img(NULL_QTRANSFORM
), _img2proj(NULL_QTRANSFORM
)
76 if (points
.count() < 2)
77 _errorString
= "Insufficient number of reference points";
78 else if (points
.size() == 2)
79 simple(points
.at(0), points
.at(1));
84 Transform::Transform(const ReferencePoint
&p1
, const ReferencePoint
&p2
)
85 : _proj2img(NULL_QTRANSFORM
), _img2proj(NULL_QTRANSFORM
)
90 Transform::Transform(const ReferencePoint
&p
, const PointD
&scale
)
91 : _proj2img(NULL_QTRANSFORM
), _img2proj(NULL_QTRANSFORM
)
93 if (scale
.x() == 0.0 || scale
.y() == 0.0) {
94 _errorString
= "Invalid scale factor";
98 _img2proj
= QTransform(scale
.x(), 0, 0, -scale
.y(), p
.pp().x() - p
.xy().x()
99 / scale
.x(), p
.pp().y() + p
.xy().x() / scale
.y());
100 _proj2img
= _img2proj
.inverted();
103 Transform::Transform(double matrix
[16])
104 : _proj2img(NULL_QTRANSFORM
), _img2proj(NULL_QTRANSFORM
)
106 _img2proj
= QTransform(matrix
[0], matrix
[1], matrix
[4], matrix
[5],
107 matrix
[3], matrix
[7]);
108 if (!_img2proj
.isInvertible())
109 _errorString
= "Singular transformation matrix";
111 _proj2img
= _img2proj
.inverted();
115 QDebug
operator<<(QDebug dbg
, const ReferencePoint
&p
)
117 dbg
.nospace() << "ReferencePoint(" << p
.xy() << ", " << p
.pp() << ")";
120 #endif // QT_NO_DEBUG