1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-24 11:45:53 +01:00

Compare commits

..

3 Commits

Author SHA1 Message Date
d8f7089450 Code cleanup 2024-02-21 18:54:27 +01:00
3f6eca7404 Do as much as possible in parallel 2024-02-21 18:40:28 +01:00
ed11bbd730 Code cleanup 2024-02-21 18:39:21 +01:00
6 changed files with 40 additions and 33 deletions

View File

@ -28,14 +28,14 @@
#define SRTM_SIZE(samples) \ #define SRTM_SIZE(samples) \
((samples) * (samples) * 2) ((samples) * (samples) * 2)
static qreal interpolate(qreal dx, qreal dy, qreal p0, qreal p1, qreal p2, static double interpolate(double dx, double dy, double p0, double p1, double p2,
qreal p3) double p3)
{ {
return p0 * (1 - dx) * (1 - dy) + p1 * dx * (1 - dy) + p2 * dy * (1 - dx) return p0 * (1.0 - dx) * (1.0 - dy) + p1 * dx * (1.0 - dy)
+ p3 * dx * dy; + p2 * dy * (1.0 - dx) + p3 * dx * dy;
} }
static qreal value(int col, int row, int samples, const QByteArray *data) static double value(int col, int row, int samples, const QByteArray *data)
{ {
int pos = ((samples - 1 - row) * samples + col) * 2; int pos = ((samples - 1 - row) * samples + col) * 2;
qint16 val = qFromBigEndian(*((const qint16*)(data->constData() + pos))); qint16 val = qFromBigEndian(*((const qint16*)(data->constData() + pos)));
@ -43,7 +43,7 @@ static qreal value(int col, int row, int samples, const QByteArray *data)
return (val == -32768) ? NAN : val; return (val == -32768) ? NAN : val;
} }
static qreal height(const Coordinates &c, const QByteArray *data) static double height(const Coordinates &c, const QByteArray *data)
{ {
int samples; int samples;
@ -56,17 +56,17 @@ static qreal height(const Coordinates &c, const QByteArray *data)
else else
return NAN; return NAN;
int row = (int)((c.lat() - qFloor(c.lat())) * (samples - 1)); double lat = (c.lat() - floor(c.lat())) * (samples - 1);
int col = (int)((c.lon() - qFloor(c.lon())) * (samples - 1)); double lon = (c.lon() - floor(c.lon())) * (samples - 1);
qreal dx = ((c.lon() - qFloor(c.lon())) * (samples - 1)) - col; int row = (int)lat;
qreal dy = ((c.lat() - qFloor(c.lat())) * (samples - 1)) - row; int col = (int)lon;
qreal p0 = value(col, row, samples, data); double p0 = value(col, row, samples, data);
qreal p1 = value(col + 1, row, samples, data); double p1 = value(col + 1, row, samples, data);
qreal p2 = value(col, row + 1, samples, data); double p2 = value(col, row + 1, samples, data);
qreal p3 = value(col + 1, row + 1, samples, data); double p3 = value(col + 1, row + 1, samples, data);
return interpolate(dx, dy, p0, p1, p2, p3); return interpolate(lon - col, lat - row, p0, p1, p2, p3);
} }
QMutex DEM::_lock; QMutex DEM::_lock;
@ -111,7 +111,7 @@ void DEM::clearCache()
_data.clear(); _data.clear();
} }
qreal DEM::elevation(const Coordinates &c) double DEM::elevation(const Coordinates &c)
{ {
if (_dir.isEmpty()) if (_dir.isEmpty())
return NAN; return NAN;
@ -127,7 +127,7 @@ qreal DEM::elevation(const Coordinates &c)
if (QFileInfo::exists(zn)) { if (QFileInfo::exists(zn)) {
QZipReader zip(zn, QIODevice::ReadOnly); QZipReader zip(zn, QIODevice::ReadOnly);
ba = new QByteArray(zip.fileData(bn)); ba = new QByteArray(zip.fileData(bn));
qreal ele = height(c, ba); double ele = height(c, ba);
_data.insert(tile, ba, ba->size()); _data.insert(tile, ba, ba->size());
return ele; return ele;
} else { } else {
@ -139,7 +139,7 @@ qreal DEM::elevation(const Coordinates &c)
return NAN; return NAN;
} else { } else {
ba = new QByteArray(file.readAll()); ba = new QByteArray(file.readAll());
qreal ele = height(c, ba); double ele = height(c, ba);
_data.insert(tile, ba, ba->size()); _data.insert(tile, ba, ba->size());
return ele; return ele;
} }

View File

@ -37,7 +37,7 @@ public:
static void setDir(const QString &path); static void setDir(const QString &path);
static void clearCache(); static void clearCache();
static qreal elevation(const Coordinates &c); static double elevation(const Coordinates &c);
static void lock() {_lock.lock();} static void lock() {_lock.lock();}
static void unlock() {_lock.unlock();} static void unlock() {_lock.unlock();}

View File

@ -454,11 +454,16 @@ Matrix RasterTile::elevation() const
int top = _rect.top() - 1; int top = _rect.top() - 1;
int bottom = _rect.bottom() + 1; int bottom = _rect.bottom() + 1;
DEM::lock(); QVector<Coordinates> ll;
ll.reserve(m.w() * m.h());
for (int y = top; y <= bottom; y++) { for (int y = top; y <= bottom; y++) {
for (int x = left; x <= right; x++) for (int x = left; x <= right; x++)
m.m(y - top, x - left) = DEM::elevation(xy2ll(QPointF(x, y))); ll.append(xy2ll(QPointF(x, y)));
} }
DEM::lock();
for (int i = 0; i < ll.size(); i++)
m.m(i) = DEM::elevation(ll.at(i));
DEM::unlock(); DEM::unlock();
return m; return m;

View File

@ -81,18 +81,14 @@ QImage HillShading::render(const Matrix &m, quint8 alpha, double azimuth,
double L = (c.a1 - c.a2 * d.dzdx - c.a3 * d.dzdy) double L = (c.a1 - c.a2 * d.dzdx - c.a3 * d.dzdy)
/ sqrt(1.0 + d.dzdx * d.dzdx + d.dzdy * d.dzdy); / sqrt(1.0 + d.dzdx * d.dzdx + d.dzdy * d.dzdy);
quint8 a, val; quint32 pixel;
if (std::isnan(L)) { if (std::isnan(L))
a = 0; pixel = 0;
val = 0; else {
} else { quint8 val = (L < 0) ? 0 : L * alpha;
if (L < 0) pixel = alpha<<24 | val<<16 | val<<8 | val;
L = 0;
val = L * alpha;
a = alpha;
} }
quint32 pixel = a<<24 | val<<16 | val<<8 | val;
*(quint32*)(bits + (y - 1) * bpl + (x - 1) * 4) = pixel; *(quint32*)(bits + (y - 1) * bpl + (x - 1) * 4) = pixel;
} }
} }

View File

@ -480,11 +480,16 @@ Matrix RasterTile::elevation() const
int top = _rect.top() - 1; int top = _rect.top() - 1;
int bottom = _rect.bottom() + 1; int bottom = _rect.bottom() + 1;
DEM::lock(); QVector<Coordinates> ll;
ll.reserve(m.w() * m.h());
for (int y = top; y <= bottom; y++) { for (int y = top; y <= bottom; y++) {
for (int x = left; x <= right; x++) for (int x = left; x <= right; x++)
m.m(y - top, x - left) = DEM::elevation(xy2ll(QPointF(x, y))); ll.append(xy2ll(QPointF(x, y)));
} }
DEM::lock();
for (int i = 0; i < ll.size(); i++)
m.m(i) = DEM::elevation(ll.at(i));
DEM::unlock(); DEM::unlock();
return m; return m;

View File

@ -13,6 +13,7 @@ public:
int h() const {return _h;} int h() const {return _h;}
int w() const {return _w;} int w() const {return _w;}
double &m(int n) {return _m[n];}
double &m(int i, int j) {return _m[_w * i + j];} double &m(int i, int j) {return _m[_w * i + j];}
double const &m(int i, int j) const {return _m.at(_w * i + j);} double const &m(int i, int j) const {return _m.at(_w * i + j);}