diff --git a/src/map/scene/scenecontroller.cpp b/src/map/scene/scenecontroller.cpp index 0537369..4094938 100644 --- a/src/map/scene/scenecontroller.cpp +++ b/src/map/scene/scenecontroller.cpp @@ -1,441 +1,449 @@ /* Copyright (C) 2020 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "scenecontroller.h" #include "scenegeometry.h" #include "scenegraph.h" #include "scenegraphitem.h" #include "../loader/mapdata.h" #include "../renderer/view.h" #include "../style/mapcssdeclaration.h" #include "../style/mapcssstyle.h" #include "../style/mapcssstate.h" #include #include #include +#include #include #include using namespace KOSMIndoorMap; SceneController::SceneController() = default; SceneController::~SceneController() = default; void SceneController::setDataSet(const MapData *data) { m_data = data; } void SceneController::setStyleSheet(const MapCSSStyle *styleSheet) { m_styleSheet = styleSheet; } void SceneController::setView(const View *view) { m_view = view; } void SceneController::updateScene(SceneGraph &sg) const { + QElapsedTimer sgUpdateTimer; + sgUpdateTimer.start(); + sg.clear(); // TODO reuse what is still valid updateCanvas(sg); // find all intermediate levels below or above the currently selected "full" level auto it = m_data->m_levelMap.find(MapLevel(m_view->level())); if (it == m_data->m_levelMap.end()) { return; } auto beginIt = it; if (beginIt != m_data->m_levelMap.begin()) { do { --beginIt; } while (!(*beginIt).first.isFullLevel() && beginIt != m_data->m_levelMap.begin()); ++beginIt; } auto endIt = it; for (++endIt; endIt != m_data->m_levelMap.end(); ++endIt) { if ((*endIt).first.isFullLevel()) { break; } } + // for each level, update or create scene graph elements for (auto it = beginIt; it != endIt; ++it) { for (auto e : (*it).second) { updateElement(e, (*it).first.numericLevel(), sg); } } sg.zSort(); + + qDebug() << "updated scenegraph took" << sgUpdateTimer.elapsed() << "ms"; } void SceneController::updateCanvas(SceneGraph &sg) const { sg.setBackgroundColor(QGuiApplication::palette().color(QPalette::Base)); m_defaultTextColor = QGuiApplication::palette().color(QPalette::Text); m_defaultFont = QGuiApplication::font(); m_styleSheet->evaluateCanvas(m_styleResult); for (auto decl : m_styleResult.declarations()) { switch (decl->property()) { case MapCSSDeclaration::FillColor: sg.setBackgroundColor(decl->colorValue()); break; case MapCSSDeclaration::TextColor: m_defaultTextColor = decl->colorValue(); break; default: break; } } } void SceneController::updateElement(OSM::Element e, int level, SceneGraph &sg) const { MapCSSState state; state.element = e; state.zoomLevel = m_view->zoomLevel(); m_styleSheet->evaluate(state, m_styleResult); if (m_styleResult.hasAreaProperties()) { PolygonBaseItem *item; if (e.type() == OSM::Type::Relation && e.tagValue("type") == QLatin1String("multipolygon")) { auto i = new MultiPolygonItm; i->path = createPath(e, m_labelPlacementPath); item = i; } else { auto i = new PolygonItem; i->polygon = createPolygon(e); m_labelPlacementPath = i->polygon; item = i; } double lineOpacity = 1.0; double fillOpacity = 1.0; initializePen(item->pen); for (auto decl : m_styleResult.declarations()) { applyGenericStyle(decl, item); applyPenStyle(decl, item->pen, lineOpacity); switch (decl->property()) { case MapCSSDeclaration::FillColor: item->brush.setColor(decl->colorValue()); item->brush.setStyle(Qt::SolidPattern); break; case MapCSSDeclaration::FillOpacity: fillOpacity = decl->doubleValue(); break; default: break; } } finalizePen(item->pen, lineOpacity); if (item->brush.style() == Qt::SolidPattern && fillOpacity < 1.0) { auto c = item->brush.color(); c.setAlphaF(c.alphaF() * fillOpacity); item->brush.setColor(c); } addItem(sg, e, level, item); } else if (m_styleResult.hasLineProperties()) { auto item = new PolylineItem; item->path = createPolygon(e); double lineOpacity = 1.0; double casingOpacity = 1.0; initializePen(item->pen); initializePen(item->casingPen); for (auto decl : m_styleResult.declarations()) { applyGenericStyle(decl, item); applyPenStyle(decl, item->pen, lineOpacity); applyCasingPenStyle(decl, item->casingPen, casingOpacity); } finalizePen(item->pen, lineOpacity); finalizePen(item->casingPen, casingOpacity); m_labelPlacementPath = item->path; addItem(sg, e, level, item); } if (m_styleResult.hasLabelProperties()) { QString text; auto textDecl = m_styleResult.declaration(MapCSSDeclaration::Text); if (!textDecl) { textDecl = m_styleResult.declaration(MapCSSDeclaration::ShieldText); } if (textDecl) { if (!textDecl->keyValue().isEmpty()) { text = e.tagValue(textDecl->keyValue().constData()); } else { text = textDecl->stringValue(); } } if (!text.isEmpty()) { auto item = new LabelItem; item->text = text; item->font = m_defaultFont; item->color = m_defaultTextColor; if (m_styleResult.hasAreaProperties()) { item->pos = SceneGeometry::polygonCentroid(m_labelPlacementPath); } else if (m_styleResult.hasLineProperties()) { // TODO compute placement at half distance along the path } if (item->pos.isNull()) { item->pos = m_view->mapGeoToScene(e.center()); // node or something failed above } double textOpacity = 1.0; double shieldOpacity = 1.0; for (auto decl : m_styleResult.declarations()) { applyGenericStyle(decl, item); applyFontStyle(decl, item->font); switch (decl->property()) { case MapCSSDeclaration::TextColor: item->color = decl->colorValue(); break; case MapCSSDeclaration::TextOpacity: textOpacity = decl->doubleValue(); break; case MapCSSDeclaration::ShieldCasingColor: item->casingColor = decl->colorValue(); break; case MapCSSDeclaration::ShieldCasingWidth: item->casingWidth = decl->doubleValue(); break; case MapCSSDeclaration::ShieldColor: item->shieldColor = decl->colorValue(); break; case MapCSSDeclaration::ShieldOpacity: shieldOpacity = decl->doubleValue(); break; case MapCSSDeclaration::ShieldFrameColor: item->frameColor = decl->colorValue(); break; case MapCSSDeclaration::ShieldFrameWidth: item->frameWidth = decl->doubleValue(); break; case MapCSSDeclaration::TextPosition: if (decl->textFollowsLine() && m_labelPlacementPath.size() > 1) { item->angle = SceneGeometry::angleForPath(m_labelPlacementPath); } break; default: break; } } if (item->color.isValid() && textOpacity < 1.0) { auto c = item->color; c.setAlphaF(c.alphaF() * textOpacity); item->color = c; } if (item->shieldColor.isValid() && shieldOpacity < 1.0) { auto c = item->shieldColor; c.setAlphaF(c.alphaF() * shieldOpacity); item->shieldColor = c; } addItem(sg, e, level, item); } } } QPolygonF SceneController::createPolygon(OSM::Element e) const { const auto path = e.outerPath(m_data->dataSet()); QPolygonF poly; poly.reserve(path.size()); for (auto node : path) { poly.push_back(m_view->mapGeoToScene(node->coordinate)); } return poly; } // @see https://wiki.openstreetmap.org/wiki/Relation:multipolygon QPainterPath SceneController::createPath(const OSM::Element e, QPolygonF &outerPath) const { assert(e.type() == OSM::Type::Relation); outerPath = createPolygon(e); QPainterPath path; path.addPolygon(outerPath); // assemble the outer polygon, which can be represented as a set of unsorted lines here even for (const auto &mem : e.relation()->members) { const bool isInner = mem.role == QLatin1String("inner"); if (mem.type != OSM::Type::Way || !isInner) { continue; } auto wayIt = std::lower_bound(m_data->dataSet().ways.begin(), m_data->dataSet().ways.end(), mem.id); if (wayIt == m_data->dataSet().ways.end() || (*wayIt).id != mem.id) { continue; } const auto subPoly = createPolygon(OSM::Element(&(*wayIt))); QPainterPath subPath; subPath.addPolygon(subPoly); path = path.subtracted(subPath); } return path; } void SceneController::applyGenericStyle(const MapCSSDeclaration *decl, SceneGraphItem *item) const { if (decl->property() == MapCSSDeclaration::ZIndex) { item->z = decl->intValue(); } } void SceneController::applyPenStyle(const MapCSSDeclaration *decl, QPen &pen, double &opacity) const { switch (decl->property()) { case MapCSSDeclaration::Color: pen.setColor(decl->colorValue()); break; case MapCSSDeclaration::Width: pen.setWidthF(decl->doubleValue()); break; case MapCSSDeclaration::Dashes: pen.setDashPattern(decl->dashesValue()); break; case MapCSSDeclaration::LineCap: pen.setCapStyle(decl->capStyle()); break; case MapCSSDeclaration::LineJoin: pen.setJoinStyle(decl->joinStyle()); break; case MapCSSDeclaration::Opacity: opacity = decl->doubleValue(); break; default: break; } } void SceneController::applyCasingPenStyle(const MapCSSDeclaration *decl, QPen &pen, double &opacity) const { switch (decl->property()) { case MapCSSDeclaration::CasingColor: pen.setColor(decl->colorValue()); break; case MapCSSDeclaration::CasingWidth: pen.setWidthF(decl->doubleValue()); break; case MapCSSDeclaration::CasingDashes: pen.setDashPattern(decl->dashesValue()); break; case MapCSSDeclaration::CasingLineCap: pen.setCapStyle(decl->capStyle()); break; case MapCSSDeclaration::CasingLineJoin: pen.setJoinStyle(decl->joinStyle()); break; case MapCSSDeclaration::CasingOpacity: opacity = decl->doubleValue(); break; default: break; } } void SceneController::applyFontStyle(const MapCSSDeclaration *decl, QFont &font) const { switch (decl->property()) { case MapCSSDeclaration::FontFamily: font.setFamily(decl->stringValue()); break; case MapCSSDeclaration::FontSize: font.setPointSizeF(decl->doubleValue()); // TODO unit support break; case MapCSSDeclaration::FontWeight: font.setBold(decl->isBoldStyle()); break; case MapCSSDeclaration::FontStyle: font.setItalic(decl->isItalicStyle()); break; case MapCSSDeclaration::FontVariant: font.setCapitalization(decl->capitalizationStyle()); break; case MapCSSDeclaration::TextDecoration: font.setUnderline(decl->isUnderlineStyle()); break; case MapCSSDeclaration::TextTransform: font.setCapitalization(decl->capitalizationStyle()); break; default: break; } } void SceneController::initializePen(QPen &pen) const { pen.setColor(Qt::transparent); // default according to spec pen.setCapStyle(Qt::FlatCap); pen.setJoinStyle(Qt::RoundJoin); pen.setStyle(Qt::SolidLine); } void SceneController::finalizePen(QPen &pen, double opacity) const { if (pen.color().isValid() && opacity < 1.0) { auto c = pen.color(); c.setAlphaF(c.alphaF() * opacity); pen.setColor(c); } if (pen.color().alphaF() == 0.0) { pen.setStyle(Qt::NoPen); // so the renderer can skip this entirely } } void SceneController::addItem(SceneGraph &sg, OSM::Element e, int level, SceneGraphItem *item) const { + item->element = e; item->level = level; // get the OSM layer, if set const auto layerStr = e.tagValue(QLatin1String("layer")); if (!layerStr.isEmpty()) { bool success = false; const auto layer = layerStr.toInt(&success); if (success) { // ### Ignore layer information when it matches the level // This is very wrong according to the specification, however it looks that in many places // layer and level tags aren't correctly filled, possibly a side-effect of layer pre-dating // level and layers not having been properly updated when retrofitting level information // Strictly following the MapCSS rendering order yields sub-optimal results in that case, with // relevant elements being hidden. // // Ideally we find a way to detect the presence of that problem, and only then enabling this // workaround, but until we have this, this seems to produce better results in all tests. if (level != layer * 10) { item->layer = layer; } } else { qWarning() << "Invalid layer:" << e.url() << layerStr; } } sg.addItem(item); } diff --git a/src/map/scene/scenegraph.cpp b/src/map/scene/scenegraph.cpp index 818ebcf..e807646 100644 --- a/src/map/scene/scenegraph.cpp +++ b/src/map/scene/scenegraph.cpp @@ -1,89 +1,100 @@ /* Copyright (C) 2020 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "scenegraph.h" #include "scenegraphitem.h" #include #include #include using namespace KOSMIndoorMap; SceneGraph::SceneGraph() = default; SceneGraph::SceneGraph(SceneGraph&&) = default; SceneGraph::~SceneGraph() = default; SceneGraph& SceneGraph::operator=(SceneGraph &&other) = default; void SceneGraph::addItem(SceneGraphItem *item) { m_items.push_back(std::unique_ptr(item)); } void SceneGraph::zSort() { /* The MapCSS spec says we have to render in the following order: * - Objects with lower layer should always be rendered first. * - Within a layer, first all fills are rendered, then all casings, then all strokes, then all icons and labels. * - Within each of those categories, objects are ordered according to z-index. * - If all of the above are equal, the order is undefined. */ std::stable_sort(m_items.begin(), m_items.end(), [](const auto &lhs, const auto &rhs) { if (lhs->level == rhs->level) { if (lhs->layer == rhs->layer) { return lhs->z < rhs->z; } return lhs->layer < rhs->layer; } return lhs->level < rhs->level; }); recomputeLayerIndex(); } void SceneGraph::clear() { m_items.clear(); m_layerOffsets.clear(); } void SceneGraph::setBackgroundColor(const QColor &bg) { m_bgColor = bg; } void SceneGraph::recomputeLayerIndex() { m_layerOffsets.clear(); if (m_items.empty()) { return; } auto prevLayer = m_items.front()->layer; auto prevIndex = 0; for (auto it = m_items.begin(); it != m_items.end();) { it = std::upper_bound(it, m_items.end(), prevLayer, [](auto lhs, const auto &rhs) { return lhs < rhs->layer; }); const auto nextIndex = std::distance(m_items.begin(), it); m_layerOffsets.push_back(std::make_pair(prevIndex, nextIndex)); prevIndex = nextIndex; if (it != m_items.end()) { prevLayer = (*it)->layer; } } } + +void SceneGraph::itemsAt(QPointF pos) +{ + // ### temporary for testing + for (const auto &item : m_items) { + if (item->inSceneSpace() && item->boundingRect().contains(pos)) { + qDebug() << item->element.url() << item->element.tagValue("name"); + } + // TODO HUD space elements + } +} diff --git a/src/map/scene/scenegraph.h b/src/map/scene/scenegraph.h index 37b69ad..16f444f 100644 --- a/src/map/scene/scenegraph.h +++ b/src/map/scene/scenegraph.h @@ -1,60 +1,67 @@ /* Copyright (C) 2020 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef KOSMINDOORMAP_SCENEGRAPH_H #define KOSMINDOORMAP_SCENEGRAPH_H #include #include #include +class QPointF; + namespace KOSMIndoorMap { class SceneGraphItem; /** Scene graph of the currently displayed level. */ class SceneGraph { public: explicit SceneGraph(); SceneGraph(const SceneGraph&) = delete; SceneGraph(SceneGraph&&); ~SceneGraph(); SceneGraph& operator=(const SceneGraph&) = delete; SceneGraph& operator=(SceneGraph &&other); void addItem(SceneGraphItem *item); void zSort(); void clear(); /** Canvas background color. */ void setBackgroundColor(const QColor &bg); + /** Items at scene coordinate @p pos. + * TODO this still needs a lot of work to be useful, mostly for debugging atm. + */ + void itemsAt(QPointF pos); + private: void recomputeLayerIndex(); friend class PainterRenderer; // TODO std::vector> m_items; std::vector> m_layerOffsets; QColor m_bgColor; }; } #endif // KOSMINDOORMAP_SCENEGRAPH_H diff --git a/src/map/scene/scenegraphitem.h b/src/map/scene/scenegraphitem.h index e4256e4..ba0c207 100644 --- a/src/map/scene/scenegraphitem.h +++ b/src/map/scene/scenegraphitem.h @@ -1,135 +1,140 @@ /* Copyright (C) 2020 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef KOSMINDOORMAP_SCENEGRAPHITEM_H #define KOSMINDOORMAP_SCENEGRAPHITEM_H +#include + #include #include #include #include #include #include #include namespace KOSMIndoorMap { /** Base class for scene graph items. */ class SceneGraphItem { public: virtual ~SceneGraphItem(); /** See MapCSS spec: "Within a layer, first all fills are rendered, then all casings, then all strokes, then all icons and labels." .*/ enum RenderPhase : uint8_t { NoPhase = 0, FillPhase = 1, CasingPhase = 2, StrokePhase = 4, LabelPhase = 8, }; /** Returns in which phase this item needs to be rendered (can be multiple). */ virtual uint8_t renderPhases() const = 0; /** Bounding box of this item in scene coordinates. * Performance trumps precision here, so estimating this slightly larger rather than computing it expensively makes sense. */ virtual QRectF boundingRect() const = 0; /** Is this item drawn in scene coordinates (as oposed to HUD coordinates)? */ bool inSceneSpace() const; /** Is this item drawn in HUD coordinates (as oposed to scene coordinates)? */ bool inHUDSpace() const; + /** The OSM::Element this item refers to. */ + OSM::Element element; + // TODO we probably don't need the full 32bit for those int level = 0; int layer = 0; int z = 0; }; /** A path/way/line item in the scenegraph. */ class PolylineItem : public SceneGraphItem { public: uint8_t renderPhases() const override; QRectF boundingRect() const override; QPolygonF path; QPen pen; QPen casingPen; }; /** Base item for filled polygons. */ class PolygonBaseItem : public SceneGraphItem { public: uint8_t renderPhases() const override; QBrush brush = Qt::NoBrush; QPen pen; }; /** A single filled polygon. */ class PolygonItem : public PolygonBaseItem { public: QRectF boundingRect() const override; QPolygonF polygon; }; /** Multi-polygon item, used for polygons with "holes" in them. */ class MultiPolygonItm : public PolygonBaseItem { public: QRectF boundingRect() const override; QPainterPath path; }; /** A text or item label */ class LabelItem : public SceneGraphItem { public: uint8_t renderPhases() const override; QRectF boundingRect() const override; QPointF pos; QString text; QColor color; QFont font; double casingWidth = 0.0; QColor casingColor = Qt::transparent; double frameWidth = 0.0; QColor frameColor = Qt::transparent; QColor shieldColor = Qt::transparent; mutable QRectF bbox; bool hasFineBbox = false; double angle = 0.0; }; } #endif // KOSMINDOORMAP_SCENEGRAPHITEM_H diff --git a/tests/indoormap.cpp b/tests/indoormap.cpp index 6fead2a..5c8eb20 100644 --- a/tests/indoormap.cpp +++ b/tests/indoormap.cpp @@ -1,149 +1,157 @@ /* Copyright (C) 2020 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include #include #include #include #include #include