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 /****************************************************************************/
14 // A RT-tree for efficient storing of SUMO's GL-objects
15 /****************************************************************************/
16 #ifndef SUMORTree_h
17 #define SUMORTree_h
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <fx.h>
27 #include <utils/geom/Boundary.h>
31 
32 #include "RTree.h"
33 
34 
35 #define GUI_RTREE_QUAL RTree<GUIGlObject*, GUIGlObject, float, 2, GUIVisualizationSettings>
36 
37 // specialized implementation for speedup and avoiding warnings
38 
39 template<>
40 inline float GUI_RTREE_QUAL::RectSphericalVolume(Rect* a_rect) {
41  ASSERT(a_rect);
42  const float extent0 = a_rect->m_max[0] - a_rect->m_min[0];
43  const float extent1 = a_rect->m_max[1] - a_rect->m_min[1];
44  return .78539816f * (extent0 * extent0 + extent1 * extent1);
45 }
46 
47 template<>
48 inline GUI_RTREE_QUAL::Rect GUI_RTREE_QUAL::CombineRect(Rect* a_rectA, Rect* a_rectB) {
49  ASSERT(a_rectA && a_rectB);
50  Rect newRect;
51  newRect.m_min[0] = rtree_min(a_rectA->m_min[0], a_rectB->m_min[0]);
52  newRect.m_max[0] = rtree_max(a_rectA->m_max[0], a_rectB->m_max[0]);
53  newRect.m_min[1] = rtree_min(a_rectA->m_min[1], a_rectB->m_min[1]);
54  newRect.m_max[1] = rtree_max(a_rectA->m_max[1], a_rectB->m_max[1]);
55  return newRect;
56 }
57 
58 
59 // ===========================================================================
60 // class definitions
61 // ===========================================================================
68 class SUMORTree : private GUI_RTREE_QUAL, public Boundary {
69 public:
71  SUMORTree() :
72  GUI_RTREE_QUAL(&GUIGlObject::drawGL),
73  myLock(true) {
74  }
75 
77  virtual ~SUMORTree() {
78  // check if lock is locked before insert objects
79  if (myLock.locked()) {
80  // cannot throw exception in destructor
81  WRITE_ERROR("Mutex of SUMORTree is locked during call of the destructor");
82  }
83  // show information in gui testing debug gl mode
84  WRITE_GLDEBUG("Number of objects in SUMORTree during call of the destructor: " + toString(myTreeDebug.size()));
85  }
86 
93  virtual void Insert(const float a_min[2], const float a_max[2], GUIGlObject* const & a_dataId) {
94  FXMutexLock locker(myLock);
95  GUI_RTREE_QUAL::Insert(a_min, a_max, a_dataId);
96  }
97 
104  virtual void Remove(const float a_min[2], const float a_max[2], GUIGlObject* const & a_dataId) {
105  FXMutexLock locker(myLock);
106  GUI_RTREE_QUAL::Remove(a_min, a_max, a_dataId);
107  }
108 
118  virtual int Search(const float a_min[2], const float a_max[2], const GUIVisualizationSettings& c) const {
119  FXMutexLock locker(myLock);
120  return GUI_RTREE_QUAL::Search(a_min, a_max, c);
121  }
122 
127  // check if lock is locked before insert objects
128  if (myLock.locked()) {
129  throw ProcessError("Mutex of SUMORTree is locked before object insertion");
130  }
131  // lock mutex
132  FXMutexLock locker(myLock);
133  // obtain boundary of object
135  // show information in gui testing debug gl mode
137  if ((b.getWidth() == 0) || (b.getHeight() == 0)) {
138  throw ProcessError("Boundary of GUIGlObject " + o->getMicrosimID() + " has an invalid size");
139  } else if (myTreeDebug.count(o) > 0) {
140  throw ProcessError("GUIGlObject was already inserted");
141  } else {
142  myTreeDebug[o] = b;
143  // write GL Debug
144  WRITE_GLDEBUG("\tInserted " + o->getFullName() + " into SUMORTree with boundary " + toString(b));
145  }
146  }
147  // insert it in Tree
148  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
149  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
150  Insert(cmin, cmax, o);
151  }
152 
157  // check if lock is locked remove insert objects
158  if (myLock.locked()) {
159  throw ProcessError("Mutex of SUMORTree is locked before object remove");
160  }
161  // lock mutex
162  FXMutexLock locker(myLock);
163  // obtain boundary of object
165  // show information in gui testing debug gl mode
167  if ((b.getWidth() == 0) || (b.getHeight() == 0)) {
168  throw ProcessError("Boundary of GUIGlObject " + o->getMicrosimID() + " has an invalid size");
169  } else if (myTreeDebug.count(o) == 0) {
170  throw ProcessError("GUIGlObject wasn't inserted");
171  } else if (b != myTreeDebug.at(o)) {
172  throw ProcessError("add boundary of GUIGlObject " + o->getMicrosimID() + " is different of removed boundary (" + toString(b) + " != " + toString(myTreeDebug.at(o)) + ")");
173  } else {
174  myTreeDebug.erase(o);
175  WRITE_GLDEBUG("\tRemoved object " + o->getFullName() + " from SUMORTree with boundary " + toString(b));
176  }
177  }
178  // remove it from Tree
179  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
180  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
181  Remove(cmin, cmax, o);
182  }
183 
184 protected:
186  mutable FXMutex myLock;
187 
188 private:
192  std::map<GUIGlObject*, Boundary> myTreeDebug;
193 };
194 
195 
196 #endif
197 
198 /****************************************************************************/
199 
Boundary.h
GUIGlObject.h
GUI_RTREE_QUAL
#define GUI_RTREE_QUAL
Definition: SUMORTree.h:35
WRITE_GLDEBUG
#define WRITE_GLDEBUG(msg)
Definition: MsgHandler.h:285
Boundary::ymin
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:130
SUMORTree::Remove
virtual void Remove(const float a_min[2], const float a_max[2], GUIGlObject *const &a_dataId)
Remove entry.
Definition: SUMORTree.h:104
MsgHandler.h
Boundary::xmax
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:124
Boundary::getHeight
double getHeight() const
Returns the height of the boundary (y-axis)
Definition: Boundary.cpp:160
GUIGlObject::getFullName
const std::string & getFullName() const
Definition: GUIGlObject.cpp:137
GUIVisualizationSettings.h
SUMORTree::SUMORTree
SUMORTree()
Constructor.
Definition: SUMORTree.h:71
rtree_min
#define rtree_min(a, b)
Definition: RTree.h:20
RTree.h
SUMORTree
A RT-tree for efficient storing of SUMO's GL-objects.
Definition: SUMORTree.h:68
SUMORTree::Insert
virtual void Insert(const float a_min[2], const float a_max[2], GUIGlObject *const &a_dataId)
Insert entry.
Definition: SUMORTree.h:93
Boundary::xmin
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:118
SUMORTree::removeAdditionalGLObject
void removeAdditionalGLObject(GUIGlObject *o)
Removes an additional object (detector/shape/trigger) from being visualised.
Definition: SUMORTree.h:156
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
Boundary::getWidth
double getWidth() const
Returns the width of the boudary (x-axis)
Definition: Boundary.cpp:154
ProcessError
Definition: UtilExceptions.h:39
SUMORTree::~SUMORTree
virtual ~SUMORTree()
Destructor.
Definition: SUMORTree.h:77
SUMORTree::myLock
FXMutex myLock
A mutex avoiding parallel change and traversal of the tree.
Definition: SUMORTree.h:186
SUMORTree::myTreeDebug
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:192
GUIIOGlobals.h
rtree_max
#define rtree_max(a, b)
Definition: RTree.h:21
GUIGlObject
Definition: GUIGlObject.h:65
ASSERT
#define ASSERT
Definition: RTree.h:12
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
GUIGlObject::getCenteringBoundary
virtual Boundary getCenteringBoundary() const =0
MsgHandler::writeDebugGLMessages
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:96
SUMORTree::addAdditionalGLObject
void addAdditionalGLObject(GUIGlObject *o)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition: SUMORTree.h:126
config.h
SUMORTree::Search
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:118
GUIVisualizationSettings
Stores the information about how to visualize structures.
Definition: GUIVisualizationSettings.h:345
GUIGlObject::getMicrosimID
virtual const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
Definition: GUIGlObject.cpp:163
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
Boundary::ymax
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:136