Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
SUMORTree.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
18// A RT-tree for efficient storing of SUMO's GL-objects
19/****************************************************************************/
20#pragma once
21#include <config.h>
22
25#include <utils/geom/Boundary.h>
29
30#include "RTree.h"
31
32
33#define GUI_RTREE_QUAL RTree<GUIGlObject*, GUIGlObject, float, 2, GUIVisualizationSettings>
34
35// specialized implementation for speedup and avoiding warnings
36
37template<>
38inline float GUI_RTREE_QUAL::RectSphericalVolume(Rect* a_rect) {
39 ASSERT(a_rect);
40 const float extent0 = a_rect->m_max[0] - a_rect->m_min[0];
41 const float extent1 = a_rect->m_max[1] - a_rect->m_min[1];
42 return .78539816f * (extent0 * extent0 + extent1 * extent1);
43}
44
45template<>
46inline GUI_RTREE_QUAL::Rect GUI_RTREE_QUAL::CombineRect(Rect* a_rectA, Rect* a_rectB) {
47 ASSERT(a_rectA && a_rectB);
48 Rect newRect;
49 newRect.m_min[0] = rtree_min(a_rectA->m_min[0], a_rectB->m_min[0]);
50 newRect.m_max[0] = rtree_max(a_rectA->m_max[0], a_rectB->m_max[0]);
51 newRect.m_min[1] = rtree_min(a_rectA->m_min[1], a_rectB->m_min[1]);
52 newRect.m_max[1] = rtree_max(a_rectA->m_max[1], a_rectB->m_max[1]);
53 return newRect;
54}
55
56
57// ===========================================================================
58// class definitions
59// ===========================================================================
66class SUMORTree : private GUI_RTREE_QUAL, public Boundary {
67public:
71 myLock(true) {
72 }
73
75 virtual ~SUMORTree() {
76 // check if lock is locked before insert objects
77 if (myLock.locked()) {
78 // cannot throw exception in destructor
79 WRITE_ERROR("Mutex of SUMORTree is locked during call of the destructor");
80 }
81 // show information in gui testing debug gl mode
82 WRITE_GLDEBUG("Number of objects in SUMORTree during call of the destructor: " + toString(myTreeDebug.size()));
83 }
84
91 virtual void Insert(const float a_min[2], const float a_max[2], GUIGlObject* const & a_dataId) {
92 FXMutexLock locker(myLock);
93 GUI_RTREE_QUAL::Insert(a_min, a_max, a_dataId);
94 }
95
102 virtual void Remove(const float a_min[2], const float a_max[2], GUIGlObject* const & a_dataId) {
103 FXMutexLock locker(myLock);
104 GUI_RTREE_QUAL::Remove(a_min, a_max, a_dataId);
105 }
106
116 virtual int Search(const float a_min[2], const float a_max[2], const GUIVisualizationSettings& c) const {
117 FXMutexLock locker(myLock);
118 return GUI_RTREE_QUAL::Search(a_min, a_max, c);
119 }
120
124 void addAdditionalGLObject(GUIGlObject *o, const double exaggeration = 1) {
125 // check if lock is locked before insert objects
126 if (myLock.locked()) {
127 throw ProcessError("Mutex of SUMORTree is locked before object insertion");
128 }
129 // lock mutex
130 FXMutexLock locker(myLock);
131 // obtain boundary of object
133 // grow using exaggeration
134 if (exaggeration > 1) {
135 b.scale(exaggeration);
136 }
137 // show information in gui testing debug gl mode
139 if (!b.isInitialised()) {
140 throw ProcessError(StringUtils::format("Boundary of GUIGlObject % is not initialised (insertion)", o->getMicrosimID()));
141 } else if ((b.getWidth() == 0) || (b.getHeight() == 0)) {
142 throw ProcessError(StringUtils::format("Boundary of GUIGlObject % has an invalid size (insertion)", o->getMicrosimID()));
143 } else if (myTreeDebug.count(o) > 0) {
144 throw ProcessError("GUIGlObject was already inserted");
145 } else {
146 myTreeDebug[o] = b;
147 // write GL Debug
148 WRITE_GLDEBUG("\tInserted " + o->getFullName() + " into SUMORTree with boundary " + toString(b));
149 }
150 }
151 // insert it in Tree
152 const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
153 const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
154 Insert(cmin, cmax, o);
155 // update tree size
156 myTreeSize++;
157 }
158
162 void removeAdditionalGLObject(GUIGlObject *o, const double exaggeration = 1) {
163 // check if lock is locked remove insert objects
164 if (myLock.locked()) {
165 throw ProcessError("Mutex of SUMORTree is locked before object remove");
166 }
167 // lock mutex
168 FXMutexLock locker(myLock);
169 // obtain boundary of object
171 // grow using exaggeration
172 if (exaggeration > 1) {
173 b.scale(exaggeration);
174 }
175 // show information in gui testing debug gl mode
177 if (!b.isInitialised()) {
178 throw ProcessError(StringUtils::format("Boundary of GUIGlObject % is not initialised (deletion)", o->getMicrosimID()));
179 } else if ((b.getWidth() == 0) || (b.getHeight() == 0)) {
180 throw ProcessError(StringUtils::format("Boundary of GUIGlObject % has an invalid size (deletion)", o->getMicrosimID()));
181 } else if (myTreeDebug.count(o) == 0) {
182 throw ProcessError("GUIGlObject wasn't inserted");
183 } else if (toString(b) != toString(myTreeDebug.at(o))) {
184 // show information in console before throwing exception
185 std::cout << "Tree: " << toString(myTreeDebug.at(o)) << " original: " << toString(b) << std::endl;
186 throw ProcessError("add boundary of GUIGlObject " + o->getMicrosimID() + " is different of removed boundary (" + toString(b) + " != " + toString(myTreeDebug.at(o)) + ")");
187 } else {
188 myTreeDebug.erase(o);
189 WRITE_GLDEBUG("\tRemoved object " + o->getFullName() + " from SUMORTree with boundary " + toString(b));
190 }
191 }
192 // remove it from Tree
193 const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
194 const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
195 Remove(cmin, cmax, o);
196 // update tree size
197 myTreeSize--;
198 }
199
202 // declare vector with glObjects to update
203 std::vector<GUIGlObject*> glObjects;
204 glObjects.reserve(myTreeSize);
205 // declare iterator
206 GUI_RTREE_QUAL::Iterator it;
207 GetFirst(it);
208 // iterate over entire tree and keep glObject in glObjects
209 while (!IsNull(it)) {
210 const auto glType = (*it)->getType();
211 if ((glType == type) ||
212 ((glType > GLO_ADDITIONALELEMENT) && (glType < GLO_SHAPE)) || // Additionals
213 ((glType >= GLO_TAZ) && (glType < GLO_LOCKICON))) { // TAZ Elements
214 glObjects.push_back(*it);
215 }
216 GetNext(it);
217 }
218 // remove and insert all elements again with the new boundary
219 for (const auto &glObject : glObjects) {
220 removeAdditionalGLObject(glObject);
222 addAdditionalGLObject(glObject);
223 }
224 }
225
226protected:
228 mutable FXMutex myLock;
229
231 int myTreeSize = 0;
232
233private:
237 std::map<GUIGlObject*, Boundary> myTreeDebug;
238
241 for (auto it = myTreeDebug.begin(); it != myTreeDebug.end(); it++) {
242 if (it->first == obj) {
243 myTreeDebug.erase(it);
244 return true;
245 }
246 }
247 return false;
248 }
249};
GUIGlObjectType
@ GLO_TAZ
Traffic Assignment Zones (TAZs)
@ GLO_ADDITIONALELEMENT
reserved GLO type for packing all additionals elements
@ GLO_SHAPE
reserved GLO type to pack shapes
@ GLO_LOCKICON
Lock icon (used in netedit)
#define WRITE_ERROR(msg)
Definition MsgHandler.h:304
#define WRITE_GLDEBUG(msg)
Definition MsgHandler.h:307
#define rtree_min(a, b)
Definition RTree.h:20
#define rtree_max(a, b)
Definition RTree.h:21
#define ASSERT
Definition RTree.h:12
#define GUI_RTREE_QUAL
Definition SUMORTree.h:33
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
A class that stores a 2D geometrical boundary.
Definition Boundary.h:39
bool isInitialised() const
check if Boundary is Initialised
Definition Boundary.cpp:259
double ymin() const
Returns minimum y-coordinate.
Definition Boundary.cpp:130
double xmin() const
Returns minimum x-coordinate.
Definition Boundary.cpp:118
double getHeight() const
Returns the height of the boundary (y-axis)
Definition Boundary.cpp:160
double getWidth() const
Returns the width of the boudary (x-axis)
Definition Boundary.cpp:154
Boundary & scale(double by)
scale the boundary by the given amount
Definition Boundary.cpp:354
double ymax() const
Returns maximum y-coordinate.
Definition Boundary.cpp:136
double xmax() const
Returns maximum x-coordinate.
Definition Boundary.cpp:124
const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
virtual Boundary getCenteringBoundary() const =0
const std::string & getFullName() const
Definition GUIGlObject.h:94
Stores the information about how to visualize structures.
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition MsgHandler.h:97
A RT-tree for efficient storing of SUMO's GL-objects.
Definition SUMORTree.h:66
void updateBoundaries(GUIGlObjectType type)
update boundaries
Definition SUMORTree.h:201
void addAdditionalGLObject(GUIGlObject *o, const double exaggeration=1)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition SUMORTree.h:124
int myTreeSize
number of inserted elements
Definition SUMORTree.h:231
virtual void Remove(const float a_min[2], const float a_max[2], GUIGlObject *const &a_dataId)
Remove entry.
Definition SUMORTree.h:102
std::map< GUIGlObject *, Boundary > myTreeDebug
Map only used for check that SUMORTree works as expected, only is used if option "gui-testing-debug-g...
Definition SUMORTree.h:237
SUMORTree()
Constructor.
Definition SUMORTree.h:69
virtual void Insert(const float a_min[2], const float a_max[2], GUIGlObject *const &a_dataId)
Insert entry.
Definition SUMORTree.h:91
void removeAdditionalGLObject(GUIGlObject *o, const double exaggeration=1)
Removes an additional object (detector/shape/trigger) from being visualised.
Definition SUMORTree.h:162
virtual int Search(const float a_min[2], const float a_max[2], const GUIVisualizationSettings &c) const
Find all within search rectangle.
Definition SUMORTree.h:116
FXMutex myLock
A mutex avoiding parallel change and traversal of the tree.
Definition SUMORTree.h:228
virtual ~SUMORTree()
Destructor.
Definition SUMORTree.h:75
bool removeObjectFromTreeDebug(const GUIGlObject *obj)
remove object from TreeDebug
Definition SUMORTree.h:240
static const std::string format(const std::string &format, T value, Targs... Fargs)
adds a new formatted message