Eclipse SUMO - Simulation of Urban MObility
SUMORTree.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // A RT-tree for efficient storing of SUMO's GL-objects
16 /****************************************************************************/
17 #ifndef SUMORTree_h
18 #define SUMORTree_h
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <fx.h>
28 #include <utils/geom/Boundary.h>
32 
33 #include "RTree.h"
34 
35 
36 #define GUI_RTREE_QUAL RTree<GUIGlObject*, GUIGlObject, float, 2, GUIVisualizationSettings>
37 
38 // specialized implementation for speedup and avoiding warnings
39 
40 template<>
41 inline float GUI_RTREE_QUAL::RectSphericalVolume(Rect* a_rect) {
42  ASSERT(a_rect);
43  const float extent0 = a_rect->m_max[0] - a_rect->m_min[0];
44  const float extent1 = a_rect->m_max[1] - a_rect->m_min[1];
45  return .78539816f * (extent0 * extent0 + extent1 * extent1);
46 }
47 
48 template<>
49 inline GUI_RTREE_QUAL::Rect GUI_RTREE_QUAL::CombineRect(Rect* a_rectA, Rect* a_rectB) {
50  ASSERT(a_rectA && a_rectB);
51  Rect newRect;
52  newRect.m_min[0] = rtree_min(a_rectA->m_min[0], a_rectB->m_min[0]);
53  newRect.m_max[0] = rtree_max(a_rectA->m_max[0], a_rectB->m_max[0]);
54  newRect.m_min[1] = rtree_min(a_rectA->m_min[1], a_rectB->m_min[1]);
55  newRect.m_max[1] = rtree_max(a_rectA->m_max[1], a_rectB->m_max[1]);
56  return newRect;
57 }
58 
59 
60 // ===========================================================================
61 // class definitions
62 // ===========================================================================
69 class SUMORTree : private GUI_RTREE_QUAL, public Boundary {
70 public:
72  SUMORTree() :
73  GUI_RTREE_QUAL(&GUIGlObject::drawGL),
74  myLock(true) {
75  }
76 
78  virtual ~SUMORTree() {
79  // check if lock is locked before insert objects
80  if (myLock.locked()) {
81  // cannot throw exception in destructor
82  WRITE_ERROR("Mutex of SUMORTree is locked during call of the destructor");
83  }
84  // show information in gui testing debug gl mode
85  WRITE_GLDEBUG("Number of objects in SUMORTree during call of the destructor: " + toString(myTreeDebug.size()));
86  }
87 
94  virtual void Insert(const float a_min[2], const float a_max[2], GUIGlObject* const & a_dataId) {
95  FXMutexLock locker(myLock);
96  GUI_RTREE_QUAL::Insert(a_min, a_max, a_dataId);
97  }
98 
105  virtual void Remove(const float a_min[2], const float a_max[2], GUIGlObject* const & a_dataId) {
106  FXMutexLock locker(myLock);
107  GUI_RTREE_QUAL::Remove(a_min, a_max, a_dataId);
108  }
109 
119  virtual int Search(const float a_min[2], const float a_max[2], const GUIVisualizationSettings& c) const {
120  FXMutexLock locker(myLock);
121  return GUI_RTREE_QUAL::Search(a_min, a_max, c);
122  }
123 
128  // check if lock is locked before insert objects
129  if(myLock.locked()) {
130  ProcessError("Mutex of SUMORTree is locked before object insertion");
131  }
132  // lock mutex
133  FXMutexLock locker(myLock);
134  // obtain boundary of object
136  // show information in gui testing debug gl mode
138  if ((b.getWidth() == 0) || (b.getHeight() == 0)) {
139  throw ProcessError("Boundary of GUIGlObject " + o->getMicrosimID() + " has an invalid size");
140  } else if (myTreeDebug.count(o) > 0) {
141  throw ProcessError("GUIGlObject was already inserted");
142  } else {
143  myTreeDebug[o] = b;
144  // write GL Debug
145  WRITE_GLDEBUG("\tInserted " + o->getFullName() + " into SUMORTree with boundary " + toString(b));
146  }
147  }
148  // insert it in Tree
149  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
150  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
151  Insert(cmin, cmax, o);
152  }
153 
158  // check if lock is locked remove insert objects
159  if(myLock.locked()) {
160  ProcessError("Mutex of SUMORTree is locked before object remove");
161  }
162  // lock mutex
163  FXMutexLock locker(myLock);
164  // obtain boundary of object
166  // show information in gui testing debug gl mode
168  if ((b.getWidth() == 0) || (b.getHeight() == 0)) {
169  throw ProcessError("Boundary of GUIGlObject " + o->getMicrosimID() + " has an invalid size");
170  } else if (myTreeDebug.count(o) == 0) {
171  throw ProcessError("GUIGlObject wasn't inserted");
172  } else if (b != myTreeDebug.at(o)) {
173  throw ProcessError("add boundary of GUIGlObject " + o->getMicrosimID() + " is different of removed boundary (" + toString(b) + " != " + toString(myTreeDebug.at(o)) + ")");
174  } else {
175  myTreeDebug.erase(o);
176  WRITE_GLDEBUG("\tRemoved object " + o->getFullName() + " from SUMORTree with boundary " + toString(b));
177  }
178  }
179  // remove it from Tree
180  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
181  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
182  Remove(cmin, cmax, o);
183  }
184 
185 protected:
187  mutable FXMutex myLock;
188 
189 private:
193  std::map<GUIGlObject*, Boundary> myTreeDebug;
194 };
195 
196 
197 #endif
198 
199 /****************************************************************************/
200 
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:131
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:125
Stores the information about how to visualize structures.
void removeAdditionalGLObject(GUIGlObject *o)
Removes an additional object (detector/shape/trigger) from being visualised.
Definition: SUMORTree.h:157
#define WRITE_GLDEBUG(msg)
Definition: MsgHandler.h:247
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:100
double getWidth() const
Returns the width of the boudary (x-axis)
Definition: Boundary.cpp:155
virtual void Remove(const float a_min[2], const float a_max[2], GUIGlObject *const &a_dataId)
Remove entry.
Definition: SUMORTree.h:105
virtual Boundary getCenteringBoundary() const =0
A RT-tree for efficient storing of SUMO&#39;s GL-objects.
Definition: SUMORTree.h:69
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
FXMutex myLock
A mutex avoiding parallel change and traversal of the tree.
Definition: SUMORTree.h:187
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
#define ASSERT
Definition: RTree.h:12
#define rtree_min(a, b)
Definition: RTree.h:20
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:119
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
#define rtree_max(a, b)
Definition: RTree.h:21
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
double getHeight() const
Returns the height of the boundary (y-axis)
Definition: Boundary.cpp:161
void addAdditionalGLObject(GUIGlObject *o)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition: SUMORTree.h:127
SUMORTree()
Constructor.
Definition: SUMORTree.h:72
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:193
#define GUI_RTREE_QUAL
Definition: SUMORTree.h:36
const std::string & getFullName() const
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:137
virtual void Insert(const float a_min[2], const float a_max[2], GUIGlObject *const &a_dataId)
Insert entry.
Definition: SUMORTree.h:94
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:119
virtual ~SUMORTree()
Destructor.
Definition: SUMORTree.h:78