1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-03-16 11:37:46 +01:00

Do not pretend Matrix can have bigger indexes than int

This commit is contained in:
Martin Tůma 2024-02-19 22:06:18 +01:00
parent 26d2403fc0
commit f21a155f79
3 changed files with 28 additions and 31 deletions

View File

@ -3,7 +3,7 @@
#define abs(x) ((x)<0 ? -(x) : (x)) #define abs(x) ((x)<0 ? -(x) : (x))
Matrix::Matrix(size_t h, size_t w) Matrix::Matrix(int h, int w)
{ {
_h = h; _h = h;
_w = w; _w = w;
@ -12,35 +12,33 @@ Matrix::Matrix(size_t h, size_t w)
bool Matrix::eliminate(double epsilon) bool Matrix::eliminate(double epsilon)
{ {
size_t i, j, k, maxrow;
double temp; double temp;
for (int i = 0; i < _h; i++) {
for (i = 0; i < _h; i++) { int maxrow = i;
maxrow = i; for (int j = i+1; j < _h; j++)
for (j = i+1; j < _h; j++)
if (abs(m(j, i)) > abs(m(maxrow, i))) if (abs(m(j, i)) > abs(m(maxrow, i)))
maxrow = j; maxrow = j;
for (j = 0; j < _w; j++) { for (int j = 0; j < _w; j++) {
temp = m(i, j); temp = m(i, j);
m(i, j) = m(maxrow, j); m(i, j) = m(maxrow, j);
m(maxrow, j) = temp; m(maxrow, j) = temp;
} }
if (abs(m(i, i)) <= epsilon) if (abs(m(i, i)) <= epsilon)
return false; return false;
for (j = i+1; j<_h; j++) { for (int j = i+1; j<_h; j++) {
temp = m(j, i) / m(i, i); temp = m(j, i) / m(i, i);
for (k = i; k < _w; k++) for (int k = i; k < _w; k++)
m(j, k) -= m(i, k) * temp; m(j, k) -= m(i, k) * temp;
} }
} }
for (i = _h-1; i < i+1; i--) { for (int i = _h-1; i >= 0; i--) {
temp = m(i, i); temp = m(i, i);
for (j = 0; j < i; j++) for (int j = 0; j < i; j++)
for (k = _w-1; k >= i; k--) for (int k = _w-1; k >= i; k--)
m(j, k) -= m(i, k) * m(j, i) / temp; m(j, k) -= m(i, k) * m(j, i) / temp;
m(i, i) /= temp; m(i, i) /= temp;
for (j = _h; j < _w; j++) for (int j = _h; j < _w; j++)
m(i, j) /= temp; m(i, j) /= temp;
} }
@ -54,12 +52,12 @@ Matrix Matrix::augemented(const Matrix &M) const
Matrix A(_h, _w + M._w); Matrix A(_h, _w + M._w);
for (size_t i = 0; i < _h; i++) for (int i = 0; i < _h; i++)
for (size_t j = 0; j < _w; j++) for (int j = 0; j < _w; j++)
A.m(i, j) = m(i, j); A.m(i, j) = m(i, j);
for (size_t i = 0; i < _h; i++) for (int i = 0; i < _h; i++)
for (size_t j = _w; j < A._w; j++) for (int j = _w; j < A._w; j++)
A.m(i, j) = M.m(i, j-_w); A.m(i, j) = M.m(i, j-_w);
return A; return A;
@ -69,8 +67,8 @@ Matrix Matrix::augemented(const Matrix &M) const
QDebug operator<<(QDebug dbg, const Matrix &matrix) QDebug operator<<(QDebug dbg, const Matrix &matrix)
{ {
dbg.nospace() << "Matrix(" << "\n"; dbg.nospace() << "Matrix(" << "\n";
for (size_t i = 0; i < matrix.h(); i++) { for (int i = 0; i < matrix.h(); i++) {
for (size_t j = 0; j < matrix.w(); j++) for (int j = 0; j < matrix.w(); j++)
dbg << "\t" << matrix.m(i, j); dbg << "\t" << matrix.m(i, j);
dbg << "\n"; dbg << "\n";
} }

View File

@ -1,7 +1,6 @@
#ifndef MATRIX_H #ifndef MATRIX_H
#define MATRIX_H #define MATRIX_H
#include <cstddef>
#include <cfloat> #include <cfloat>
#include <QVector> #include <QVector>
#include <QDebug> #include <QDebug>
@ -10,12 +9,12 @@ class Matrix
{ {
public: public:
Matrix() {_h = 0; _w = 0;} Matrix() {_h = 0; _w = 0;}
Matrix(size_t h, size_t w); Matrix(int h, int w);
size_t h() const {return _h;} int h() const {return _h;}
size_t w() const {return _w;} int w() const {return _w;}
double &m(size_t i, size_t j) {return _m[_w * i + j];} double &m(int i, int j) {return _m[_w * i + j];}
double const &m(size_t i, size_t j) const {return _m.at(_w * i + j);} double const &m(int i, int j) const {return _m.at(_w * i + j);}
bool isNull() const {return (_h == 0 || _w == 0);} bool isNull() const {return (_h == 0 || _w == 0);}
@ -24,8 +23,8 @@ public:
private: private:
QVector<double> _m; QVector<double> _m;
size_t _h; int _h;
size_t _w; int _w;
}; };
#ifndef QT_NO_DEBUG #ifndef QT_NO_DEBUG

View File

@ -23,8 +23,8 @@ void Transform::simple(const ReferencePoint &p1, const ReferencePoint &p2)
void Transform::affine(const QList<ReferencePoint> &points) void Transform::affine(const QList<ReferencePoint> &points)
{ {
Matrix c(3, 2); Matrix c(3, 2);
for (size_t i = 0; i < c.h(); i++) { for (int i = 0; i < c.h(); i++) {
for (size_t j = 0; j < c.w(); j++) { for (int j = 0; j < c.w(); j++) {
for (int k = 0; k < points.size(); k++) { for (int k = 0; k < points.size(); k++) {
double f[3], t[2]; double f[3], t[2];
@ -45,8 +45,8 @@ void Transform::affine(const QList<ReferencePoint> &points)
v[0] = points.at(qi).pp().x(); v[0] = points.at(qi).pp().x();
v[1] = points.at(qi).pp().y(); v[1] = points.at(qi).pp().y();
v[2] = 1.0; v[2] = 1.0;
for (size_t i = 0; i < Q.h(); i++) for (int i = 0; i < Q.h(); i++)
for (size_t j = 0; j < Q.w(); j++) for (int j = 0; j < Q.w(); j++)
Q.m(i,j) += v[i] * v[j]; Q.m(i,j) += v[i] * v[j];
} }