KOSMIndoorMap

scenegraph.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "scenegraph.h"
8 
9 #include <QDebug>
10 #include <QGuiApplication>
11 #include <QPalette>
12 
13 using namespace KOSMIndoorMap;
14 
15 SceneGraph::SceneGraph() = default;
16 SceneGraph::SceneGraph(SceneGraph&&) = default;
17 SceneGraph::~SceneGraph() = default;
18 SceneGraph& SceneGraph::operator=(SceneGraph &&other) = default;
19 
21 {
22  m_items.clear();
23  m_previousItems.clear();
24  m_layerOffsets.clear();
25  m_bgColor = {};
26  m_floorLevel = 0;
27  m_zoomLevel = 0;
28 }
29 
30 void SceneGraph::addItem(SceneGraphItem &&item)
31 {
32  m_items.push_back(std::move(item));
33 }
34 
35 void SceneGraph::zSort()
36 {
37  std::stable_sort(m_items.begin(), m_items.end(), SceneGraph::zOrderCompare);
38  recomputeLayerIndex();
39 }
40 
41 bool SceneGraph::zOrderCompare(const SceneGraphItem &lhs, const SceneGraphItem &rhs)
42 {
43  /* The MapCSS spec says we have to render in the following order:
44  * - Objects with lower layer should always be rendered first.
45  * - Within a layer, first all fills are rendered, then all casings, then all strokes, then all icons and labels.
46  * - Within each of those categories, objects are ordered according to z-index.
47  * - If all of the above are equal, the order is undefined.
48  */
49  if (lhs.level == rhs.level) {
50  if (lhs.layer == rhs.layer) {
51  return lhs.payload->z < rhs.payload->z;
52  }
53  return lhs.layer < rhs.layer;
54  }
55  return lhs.level < rhs.level;
56 }
57 
58 void SceneGraph::beginSwap()
59 {
60  std::swap(m_items, m_previousItems);
61  m_items.clear();
62  std::sort(m_previousItems.begin(), m_previousItems.end(), SceneGraph::itemPoolCompare);
63  m_layerOffsets.clear();
64 }
65 
66 void SceneGraph::endSwap()
67 {
68  m_previousItems.clear();
69 }
70 
71 int SceneGraph::zoomLevel() const
72 {
73  return m_zoomLevel;
74 }
75 
76 void SceneGraph::setZoomLevel(int zoom)
77 {
78  m_zoomLevel = zoom;
79 }
80 
81 int SceneGraph::currentFloorLevel() const
82 {
83  return m_floorLevel;
84 }
85 
86 void SceneGraph::setCurrentFloorLevel(int level)
87 {
88  m_floorLevel = level;
89 }
90 
92 {
93  return m_bgColor;
94 }
95 
96 void SceneGraph::setBackgroundColor(const QColor &bg)
97 {
98  m_bgColor = bg;
99 }
100 
101 void SceneGraph::recomputeLayerIndex()
102 {
103  m_layerOffsets.clear();
104  if (m_items.empty()) {
105  return;
106  }
107 
108  auto prevIndex = 0;
109  for (auto it = m_items.begin(); it != m_items.end();) {
110  it = std::upper_bound(it, m_items.end(), *it, [](const auto &lhs, const auto &rhs) {
111  if (lhs.level == rhs.level) {
112  return lhs.layer < rhs.layer;
113  }
114  return lhs.level < rhs.level;
115  });
116  const auto nextIndex = std::distance(m_items.begin(), it);
117  m_layerOffsets.push_back(std::make_pair(prevIndex, nextIndex));
118  prevIndex = nextIndex;
119  }
120 }
121 
122 const std::vector<SceneGraph::LayerOffset>& SceneGraph::layerOffsets() const
123 {
124  return m_layerOffsets;
125 }
126 
127 SceneGraph::SceneGraphItemIter SceneGraph::itemsBegin(SceneGraph::LayerOffset layer) const
128 {
129  return m_items.begin() + layer.first;
130 }
131 
132 SceneGraph::SceneGraphItemIter SceneGraph::itemsEnd(SceneGraph::LayerOffset layer) const
133 {
134  return m_items.begin() + layer.second;
135 }
136 
137 const std::vector<SceneGraphItem>& SceneGraph::items() const
138 {
139  return m_items;
140 }
141 
142 bool SceneGraph::itemPoolCompare(const SceneGraphItem &lhs, const SceneGraphItem &rhs)
143 {
144  if (lhs.element.type() == rhs.element.type()) {
145  if (lhs.element.id() == rhs.element.id()) {
146  if (lhs.layerSelector == rhs.layerSelector) {
147  return lhs.level < rhs.level;
148  }
149  return lhs.layerSelector < rhs.layerSelector;
150  }
151  return lhs.element.id() < rhs.element.id();
152  }
153  return lhs.element.type() < rhs.element.type();
154 }
OSM-based multi-floor indoor maps for buildings.
void clear()
Clears all data from the scene graph.
Definition: scenegraph.cpp:20
Scene graph of the currently displayed level.
Definition: scenegraph.h:28
Scene graph item description and handle for its content.
QColor backgroundColor() const
Canvas background color.
Definition: scenegraph.cpp:91
OSM::Element element
The OSM::Element this item refers to.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:03:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.