2018-01-08 23:47:45 +01:00
|
|
|
#include "matrix.h"
|
|
|
|
#include "transform.h"
|
|
|
|
|
2018-03-22 20:00:30 +01:00
|
|
|
|
|
|
|
#define NULL_QTRANSFORM 0,0,0,0,0,0,0,0,0
|
|
|
|
|
2018-04-15 16:27:47 +02:00
|
|
|
void Transform::simple(const ReferencePoint &p1, const ReferencePoint &p2)
|
2018-01-08 23:47:45 +01:00
|
|
|
{
|
2018-04-15 16:27:47 +02:00
|
|
|
if (p1.xy().x() == p2.xy().x() || p1.xy().y() == p2.xy().y()) {
|
2018-01-08 23:47:45 +01:00
|
|
|
_errorString = "Invalid reference points tuple";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-15 16:27:47 +02:00
|
|
|
double sX = (p1.xy().x() - p2.xy().x()) / (p1.pp().x() - p2.pp().x());
|
|
|
|
double sY = (p2.xy().y() - p1.xy().y()) / (p2.pp().y() - p1.pp().y());
|
|
|
|
double dX = p2.xy().x() - p2.pp().x() * sX;
|
|
|
|
double dY = p1.xy().y() - p1.pp().y() * sY;
|
2018-01-08 23:47:45 +01:00
|
|
|
|
2018-03-22 20:00:30 +01:00
|
|
|
_proj2img = QTransform(sX, 0, 0, sY, dX, dY);
|
|
|
|
_img2proj = _proj2img.inverted();
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Transform::affine(const QList<ReferencePoint> &points)
|
|
|
|
{
|
2024-05-19 16:14:23 +02:00
|
|
|
MatrixD c(3, 2);
|
2024-02-19 22:06:18 +01:00
|
|
|
for (int i = 0; i < c.h(); i++) {
|
|
|
|
for (int j = 0; j < c.w(); j++) {
|
2018-01-08 23:47:45 +01:00
|
|
|
for (int k = 0; k < points.size(); k++) {
|
|
|
|
double f[3], t[2];
|
|
|
|
|
2018-04-15 16:27:47 +02:00
|
|
|
f[0] = points.at(k).pp().x();
|
|
|
|
f[1] = points.at(k).pp().y();
|
2018-01-08 23:47:45 +01:00
|
|
|
f[2] = 1.0;
|
2018-04-15 16:27:47 +02:00
|
|
|
t[0] = points.at(k).xy().x();
|
|
|
|
t[1] = points.at(k).xy().y();
|
2024-05-19 16:14:23 +02:00
|
|
|
c.at(i,j) += f[i] * t[j];
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-19 16:14:23 +02:00
|
|
|
MatrixD Q(3, 3);
|
2018-01-08 23:47:45 +01:00
|
|
|
for (int qi = 0; qi < points.size(); qi++) {
|
|
|
|
double v[3];
|
|
|
|
|
2018-04-15 16:27:47 +02:00
|
|
|
v[0] = points.at(qi).pp().x();
|
|
|
|
v[1] = points.at(qi).pp().y();
|
2018-01-08 23:47:45 +01:00
|
|
|
v[2] = 1.0;
|
2024-02-19 22:06:18 +01:00
|
|
|
for (int i = 0; i < Q.h(); i++)
|
|
|
|
for (int j = 0; j < Q.w(); j++)
|
2024-05-19 16:14:23 +02:00
|
|
|
Q.at(i,j) += v[i] * v[j];
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
|
|
|
|
2024-05-19 16:14:23 +02:00
|
|
|
MatrixD M(Q.augemented(c));
|
2018-01-08 23:47:45 +01:00
|
|
|
if (!M.eliminate()) {
|
|
|
|
_errorString = "Singular transformation matrix";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-19 16:14:23 +02:00
|
|
|
_proj2img = QTransform(M.at(0,3), M.at(0,4), M.at(1,3), M.at(1,4), M.at(2,3),
|
|
|
|
M.at(2,4));
|
2018-03-22 20:00:30 +01:00
|
|
|
_img2proj = _proj2img.inverted();
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform::Transform()
|
|
|
|
: _proj2img(NULL_QTRANSFORM), _img2proj(NULL_QTRANSFORM)
|
|
|
|
{
|
2018-01-08 23:47:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Transform::Transform(const QList<ReferencePoint> &points)
|
2018-04-16 19:14:27 +02:00
|
|
|
: _proj2img(NULL_QTRANSFORM), _img2proj(NULL_QTRANSFORM)
|
2018-01-08 23:47:45 +01:00
|
|
|
{
|
|
|
|
if (points.count() < 2)
|
|
|
|
_errorString = "Insufficient number of reference points";
|
|
|
|
else if (points.size() == 2)
|
2018-04-15 16:27:47 +02:00
|
|
|
simple(points.at(0), points.at(1));
|
2018-01-08 23:47:45 +01:00
|
|
|
else
|
|
|
|
affine(points);
|
|
|
|
}
|
2018-01-21 00:48:34 +01:00
|
|
|
|
2018-04-15 16:27:47 +02:00
|
|
|
Transform::Transform(const ReferencePoint &p1, const ReferencePoint &p2)
|
2018-04-16 19:14:27 +02:00
|
|
|
: _proj2img(NULL_QTRANSFORM), _img2proj(NULL_QTRANSFORM)
|
2018-04-15 16:27:47 +02:00
|
|
|
{
|
|
|
|
simple(p1, p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform::Transform(const ReferencePoint &p, const PointD &scale)
|
2018-03-22 20:00:30 +01:00
|
|
|
: _proj2img(NULL_QTRANSFORM), _img2proj(NULL_QTRANSFORM)
|
|
|
|
{
|
|
|
|
if (scale.x() == 0.0 || scale.y() == 0.0) {
|
|
|
|
_errorString = "Invalid scale factor";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-15 16:27:47 +02:00
|
|
|
_img2proj = QTransform(scale.x(), 0, 0, -scale.y(), p.pp().x() - p.xy().x()
|
|
|
|
/ scale.x(), p.pp().y() + p.xy().x() / scale.y());
|
2018-03-22 20:00:30 +01:00
|
|
|
_proj2img = _img2proj.inverted();
|
|
|
|
}
|
|
|
|
|
2018-04-15 16:27:47 +02:00
|
|
|
Transform::Transform(double matrix[16])
|
2018-03-22 20:00:30 +01:00
|
|
|
: _proj2img(NULL_QTRANSFORM), _img2proj(NULL_QTRANSFORM)
|
|
|
|
{
|
2018-04-15 16:27:47 +02:00
|
|
|
_img2proj = QTransform(matrix[0], matrix[1], matrix[4], matrix[5],
|
|
|
|
matrix[3], matrix[7]);
|
2018-03-22 20:00:30 +01:00
|
|
|
if (!_img2proj.isInvertible())
|
|
|
|
_errorString = "Singular transformation matrix";
|
|
|
|
else
|
|
|
|
_proj2img = _img2proj.inverted();
|
|
|
|
}
|
|
|
|
|
2018-02-13 23:03:18 +01:00
|
|
|
#ifndef QT_NO_DEBUG
|
2018-01-21 00:48:34 +01:00
|
|
|
QDebug operator<<(QDebug dbg, const ReferencePoint &p)
|
|
|
|
{
|
2018-04-15 16:27:47 +02:00
|
|
|
dbg.nospace() << "ReferencePoint(" << p.xy() << ", " << p.pp() << ")";
|
2018-01-21 11:19:46 +01:00
|
|
|
return dbg.space();
|
2018-01-21 00:48:34 +01:00
|
|
|
}
|
2018-02-13 23:03:18 +01:00
|
|
|
#endif // QT_NO_DEBUG
|