1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-02-19 09:00:49 +01:00
GPXSee/src/map/ENC/rastertile.cpp

405 lines
10 KiB
C++
Raw Normal View History

2022-11-14 22:29:27 +01:00
#include <QtMath>
2022-11-04 09:03:36 +01:00
#include <QPainter>
2022-11-14 22:29:27 +01:00
#include "common/linec.h"
2022-11-04 09:03:36 +01:00
#include "map/bitmapline.h"
#include "map/textpathitem.h"
#include "map/rectd.h"
2022-11-04 09:03:36 +01:00
#include "style.h"
#include "rastertile.h"
using namespace ENC;
#define TEXT_EXTENT 160
2023-03-25 13:31:15 +01:00
#define TSSLPT_SIZE 0.005 /* ll */
2022-11-04 09:03:36 +01:00
typedef QMap<Coordinates, const MapData::Point*> PointMap;
static const float C1 = 0.866025f; /* sqrt(3)/2 */
2022-11-04 09:03:36 +01:00
static const QColor haloColor(Qt::white);
static struct {
bool operator()(MapData::Point* a, MapData::Point* b) const
{return *a < *b;}
} pointLess;
static QFont pixelSizeFont(int pixelSize)
{
QFont f;
f.setPixelSize(pixelSize);
return f;
}
static QFont *font(Style::FontSize size)
{
/* The fonts must be initialized on first usage (after the QGuiApplication
instance is created) */
static QFont large = pixelSizeFont(16);
static QFont normal = pixelSizeFont(12);
static QFont small = pixelSizeFont(10);
switch (size) {
case Style::None:
return 0;
case Style::Large:
return &large;
case Style::Small:
return &small;
default:
return &normal;
}
}
static const QImage *light()
{
static QImage img(":/marine/light.png");
return &img;
}
2023-03-23 01:05:44 +01:00
static const QImage *signal()
{
static QImage img(":/marine/fog-signal.png");
return &img;
}
2022-11-04 09:03:36 +01:00
static const Style& style()
{
static Style s;
return s;
}
2022-11-14 22:29:27 +01:00
static double area(const QVector<Coordinates> &polygon)
{
double area = 0;
2023-02-19 14:47:57 +01:00
for (int i = 0; i < polygon.size() - 1; i++) {
const Coordinates &pi = polygon.at(i);
const Coordinates &pj = polygon.at(i+1);
area += pi.lon() * pj.lat();
area -= pi.lat() * pj.lon();
2022-11-14 22:29:27 +01:00
}
area /= 2.0;
return area;
}
static Coordinates centroid(const QVector<Coordinates> &polygon)
{
2023-02-19 14:47:57 +01:00
Q_ASSERT(polygon.size() > 3);
Q_ASSERT(polygon.first() == polygon.last());
2022-11-14 22:29:27 +01:00
double cx = 0, cy = 0;
double factor = 1.0 / (6.0 * area(polygon));
2023-02-19 14:47:57 +01:00
for (int i = 0; i < polygon.size() - 1; i++) {
const Coordinates &pi = polygon.at(i);
const Coordinates &pj = polygon.at(i+1);
double f = (pi.lon() * pj.lat() - pj.lon() * pi.lat());
cx += (pi.lon() + pj.lon()) * f;
cy += (pi.lat() + pj.lat()) * f;
2022-11-14 22:29:27 +01:00
}
return Coordinates(cx * factor, cy * factor);
}
static double angle(uint type, const QVariant &param)
2022-12-08 00:29:39 +01:00
{
2023-05-29 23:19:16 +02:00
uint bt = type>>16;
return (bt == RDOCAL || bt == I_RDOCAL || bt == CURENT)
? 90 + param.toDouble() : NAN;
2022-12-08 00:29:39 +01:00
}
2023-03-24 22:54:53 +01:00
static bool showLabel(const QImage *img, const Range &range, int zoom, int type)
2023-03-24 09:18:13 +01:00
{
2023-03-24 22:54:53 +01:00
if (type>>16 == I_DISMAR)
return true;
2023-03-24 09:18:13 +01:00
2023-03-24 22:54:53 +01:00
if ((img || type>>16 == SOUNDG) && zoom < range.mid())
return false;
2023-03-24 09:18:13 +01:00
2023-03-24 22:54:53 +01:00
return true;
2023-03-24 09:18:13 +01:00
}
2022-11-04 09:03:36 +01:00
QPainterPath RasterTile::painterPath(const Polygon &polygon) const
{
QPainterPath path;
for (int i = 0; i < polygon.size(); i++) {
const QVector<Coordinates> &subpath = polygon.at(i);
2023-05-26 21:30:27 +02:00
QVector<QPointF> p(subpath.size());
for (int j = 0; j < subpath.size(); j++)
p[j] = ll2xy(subpath.at(j));
path.addPolygon(p);
2022-11-04 09:03:36 +01:00
}
return path;
}
QPolygonF RasterTile::polyline(const QVector<Coordinates> &path) const
{
QPolygonF polygon;
polygon.reserve(path.size());
for (int i = 0; i < path.size(); i++)
polygon.append(ll2xy(path.at(i)));
return polygon;
}
2022-12-08 00:29:39 +01:00
QPolygonF RasterTile::tsslptArrow(const Coordinates &c, qreal angle) const
2022-11-14 22:29:27 +01:00
{
Coordinates t[3], r[4];
QPolygonF polygon;
t[0] = c;
2022-12-08 00:29:39 +01:00
t[1] = Coordinates(t[0].lon() - qCos(angle - M_PI/3) * TSSLPT_SIZE,
t[0].lat() - qSin(angle - M_PI/3) * TSSLPT_SIZE);
t[2] = Coordinates(t[0].lon() - qCos(angle - M_PI + M_PI/3) * TSSLPT_SIZE,
t[0].lat() - qSin(angle - M_PI + M_PI/3) * TSSLPT_SIZE);
2022-11-14 22:29:27 +01:00
LineC l(t[1], t[2]);
r[0] = l.pointAt(0.25);
r[1] = l.pointAt(0.75);
2022-12-08 00:29:39 +01:00
r[2] = Coordinates(r[0].lon() - C1 * TSSLPT_SIZE * qCos(angle - M_PI/2),
r[0].lat() - C1 * TSSLPT_SIZE * qSin(angle - M_PI/2));
r[3] = Coordinates(r[1].lon() - C1 * TSSLPT_SIZE * qCos(angle - M_PI/2),
r[1].lat() - C1 * TSSLPT_SIZE * qSin(angle - M_PI/2));
2022-11-14 22:29:27 +01:00
polygon << ll2xy(t[0]) << ll2xy(t[2]) << ll2xy(r[1]) << ll2xy(r[3])
<< ll2xy(r[2]) << ll2xy(r[0]) << ll2xy(t[1]);
return polygon;
}
void RasterTile::drawArrows(QPainter *painter,
const QList<MapData::Poly*> &polygons)
2022-11-14 22:29:27 +01:00
{
for (int i = 0; i < polygons.size(); i++) {
const MapData::Poly *poly = polygons.at(i);
2022-11-14 22:29:27 +01:00
if (poly->type()>>16 == TSSLPT) {
2022-12-08 00:29:39 +01:00
QPolygonF polygon(tsslptArrow(centroid(poly->path().first()),
deg2rad(180 - poly->param().toDouble())));
painter->setPen(QPen(QColor("#eb49eb"), 1));
painter->setBrush(QBrush("#80eb49eb"));
2022-11-14 22:29:27 +01:00
painter->drawPolygon(polygon);
}
}
}
void RasterTile::drawPolygons(QPainter *painter,
const QList<MapData::Poly*> &polygons)
2022-11-04 09:03:36 +01:00
{
const Style &s = style();
for (int n = 0; n < s.drawOrder().size(); n++) {
for (int i = 0; i < polygons.size(); i++) {
const MapData::Poly *poly = polygons.at(i);
2022-11-24 09:34:03 +01:00
if (poly->type() != s.drawOrder().at(n))
2022-11-04 09:03:36 +01:00
continue;
2022-11-24 09:34:03 +01:00
const Style::Polygon &style = s.polygon(poly->type());
2022-11-04 09:03:36 +01:00
2022-11-08 00:38:45 +01:00
if (!style.img().isNull()) {
for (int i = 0; i < poly->path().size(); i++)
BitmapLine::draw(painter, polyline(poly->path().at(i)),
style.img());
} else {
painter->setPen(style.pen());
painter->setBrush(style.brush());
painter->drawPath(painterPath(poly->path()));
}
2022-11-04 09:03:36 +01:00
}
}
}
void RasterTile::drawLines(QPainter *painter, const QList<MapData::Line*> &lines)
2022-11-04 09:03:36 +01:00
{
const Style &s = style();
painter->setBrush(Qt::NoBrush);
for (int i = 0; i < lines.size(); i++) {
const MapData::Line *line = lines.at(i);
2022-11-24 09:34:03 +01:00
const Style::Line &style = s.line(line->type());
2022-11-04 09:03:36 +01:00
if (!style.img().isNull()) {
BitmapLine::draw(painter, polyline(line->path()), style.img());
} else if (style.pen() != Qt::NoPen) {
painter->setPen(style.pen());
2022-11-04 09:03:36 +01:00
painter->drawPolyline(polyline(line->path()));
}
}
}
void RasterTile::drawTextItems(QPainter *painter,
const QList<TextItem*> &textItems)
{
for (int i = 0; i < textItems.size(); i++)
textItems.at(i)->paint(painter);
}
void RasterTile::processPolygons(const QList<MapData::Poly*> &polygons,
QList<TextItem*> &textItems)
2022-11-04 09:03:36 +01:00
{
const Style &s = style();
for (int i = 0; i < polygons.size(); i++) {
const MapData::Poly *poly = polygons.at(i);
2022-12-04 23:09:59 +01:00
uint type = poly->type()>>16;
2022-12-10 11:59:48 +01:00
if (!(type == HRBFAC || type == I_TRNBSN
|| poly->type() == SUBTYPE(I_BERTHS, 6)))
2022-12-04 23:09:59 +01:00
continue;
const Style::Point &style = s.point(poly->type());
const QImage *img = style.img().isNull() ? 0 : &style.img();
if (!img)
continue;
TextPointItem *item = new TextPointItem(
ll2xy(centroid(poly->path().first())).toPoint(),
0, 0, img, 0, 0, 0, 0);
if (item->isValid() && !item->collides(textItems))
textItems.append(item);
else
delete item;
}
2022-12-05 08:52:27 +01:00
}
void RasterTile::processPoints(QList<MapData::Point*> &points,
QList<TextItem*> &textItems, QList<TextItem*> &lights)
2022-12-05 08:52:27 +01:00
{
const Style &s = style();
2023-03-23 01:05:44 +01:00
PointMap lightsMap, signalsMap;
int i;
2022-12-05 08:52:27 +01:00
std::sort(points.begin(), points.end(), pointLess);
2022-12-04 23:09:59 +01:00
2023-03-23 01:05:44 +01:00
/* Lights & Signals */
for (i = 0; i < points.size(); i++) {
const MapData::Point *point = points.at(i);
if (point->type()>>16 == LIGHTS)
lightsMap.insert(point->pos(), point);
2023-03-23 01:05:44 +01:00
else if (point->type()>>16 == FOGSIG)
signalsMap.insert(point->pos(), point);
else
break;
}
/* Everything else */
for ( ; i < points.size(); i++) {
const MapData::Point *point = points.at(i);
2023-03-24 22:54:53 +01:00
QPoint pos(ll2xy(point->pos()).toPoint());
2022-11-24 09:34:03 +01:00
const Style::Point &style = s.point(point->type());
2022-11-04 09:03:36 +01:00
const QString *label = point->label().isEmpty() ? 0 : &(point->label());
const QImage *img = style.img().isNull() ? 0 : &style.img();
const QFont *fnt = showLabel(img, _data->zooms(), _zoom, point->type())
2023-03-24 22:54:53 +01:00
? font(style.textFontSize()) : 0;
2022-11-04 09:03:36 +01:00
const QColor *color = &style.textColor();
2022-12-08 00:29:39 +01:00
const QColor *hColor = style.haloColor().isValid()
? &style.haloColor() : 0;
double rotate = angle(point->type(), point->param());
2022-11-04 09:03:36 +01:00
if ((!label || !fnt) && !img)
continue;
TextPointItem *item = new TextPointItem(pos, label, fnt, img, color,
hColor, 0, 2, rotate);
2023-03-24 22:54:53 +01:00
if (item->isValid() && !item->collides(textItems)) {
2022-11-04 09:03:36 +01:00
textItems.append(item);
2023-03-23 01:05:44 +01:00
if (lightsMap.contains(point->pos()))
lights.append(new TextPointItem(pos, 0, 0, light(), 0, 0, 0, 0));
if (signalsMap.contains(point->pos()))
lights.append(new TextPointItem(pos, 0, 0, signal(), 0, 0, 0, 0));
2023-03-24 09:18:13 +01:00
} else
2023-03-24 22:54:53 +01:00
delete item;
2022-11-04 09:03:36 +01:00
}
}
void RasterTile::processLines(const QList<MapData::Line*> &lines,
QList<TextItem*> &textItems)
2022-11-04 09:03:36 +01:00
{
const Style &s = style();
for (int i = 0; i < lines.size(); i++) {
const MapData::Line *line = lines.at(i);
2022-11-24 09:34:03 +01:00
const Style::Line &style = s.line(line->type());
2022-11-04 09:03:36 +01:00
if (style.img().isNull() && style.pen() == Qt::NoPen)
2022-11-04 09:03:36 +01:00
continue;
if (line->label().isEmpty() || style.textFontSize() == Style::None)
continue;
const QFont *fnt = font(style.textFontSize());
const QColor *color = &style.textColor();
TextPathItem *item = new TextPathItem(polyline(line->path()),
&line->label(), _rect, fnt, color, 0);
if (item->isValid() && !item->collides(textItems))
textItems.append(item);
else
delete item;
}
}
void RasterTile::fetchData(QList<MapData::Poly*> &polygons,
QList<MapData::Line*> &lines, QList<MapData::Point*> &points)
{
QPoint ttl(_rect.topLeft());
QRectF polyRect(ttl, QPointF(ttl.x() + _rect.width(), ttl.y()
+ _rect.height()));
RectD polyRectD(_transform.img2proj(polyRect.topLeft()),
_transform.img2proj(polyRect.bottomRight()));
RectC polyRectC(polyRectD.toRectC(_proj, 20));
_data->lines(polyRectC, &lines);
_data->polygons(polyRectC, &polygons);
QRectF pointRect(QPointF(ttl.x() - TEXT_EXTENT, ttl.y() - TEXT_EXTENT),
QPointF(ttl.x() + _rect.width() + TEXT_EXTENT, ttl.y() + _rect.height()
+ TEXT_EXTENT));
RectD pointRectD(_transform.img2proj(pointRect.topLeft()),
_transform.img2proj(pointRect.bottomRight()));
_data->points(pointRectD.toRectC(_proj, 20), &points);
}
2022-11-04 09:03:36 +01:00
void RasterTile::render()
{
QList<MapData::Line*> lines;
QList<MapData::Poly*> polygons;
QList<MapData::Point*> points;
QList<TextItem*> textItems, lights;
2022-11-04 09:03:36 +01:00
_pixmap.setDevicePixelRatio(_ratio);
_pixmap.fill(Qt::transparent);
fetchData(polygons, lines, points);
processPolygons(polygons, textItems);
processPoints(points, textItems, lights);
processLines(lines, textItems);
2022-11-04 09:03:36 +01:00
QPainter painter(&_pixmap);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(-_rect.x(), -_rect.y());
drawPolygons(&painter, polygons);
drawLines(&painter, lines);
drawArrows(&painter, polygons);
2022-11-04 09:03:36 +01:00
drawTextItems(&painter, lights);
2022-11-04 09:03:36 +01:00
drawTextItems(&painter, textItems);
qDeleteAll(textItems);
qDeleteAll(lights);
2022-11-04 09:03:36 +01:00
//painter.setPen(Qt::red);
//painter.setBrush(Qt::NoBrush);
//painter.drawRect(QRect(_rect.topLeft(), _pixmap.size()));
_valid = true;
2022-11-04 09:03:36 +01:00
}