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-2018 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 
30 #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:
73  }
74 
76  virtual ~SUMORTree() {
77  // check if lock is locked before insert objects
78  if(myLock.locked()) {
79  ProcessError("Mutex of SUMORTree is locked during call destructor (Lock value = " + toString(myLock.lockCount())+ ")");
80  }
81  // show information in gui testing debug gl mode
82  WRITE_GLDEBUG("Number of objects in SUMORTree during call destructor: " + toString(myTreeDebug.size()));
83  }
84 
91  virtual void Insert(const float a_min[2], const float a_max[2], GUIGlObject* const & a_dataId) {
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) {
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 {
118  return GUI_RTREE_QUAL::Search(a_min, a_max, c);
119  }
120 
125  // check if lock is locked before insert objects
126  if(myLock.locked()) {
127  ProcessError("Mutex of SUMORTree is locked before object insertion (Lock value = " + toString(myLock.lockCount())+ ")");
128  }
129  // lock mutex
131  // obtain boundary of object
133  // show information in gui testing debug gl mode
135  if ((b.getWidth() == 0) || (b.getHeight() == 0)) {
136  throw ProcessError("boundary of GUIGlObject " + o->getMicrosimID() + " has an invalid size");
137  } else {
138  myTreeDebug[o] = b;
139  // write GL Debug
140  WRITE_GLDEBUG("Inserted " + o->getFullName() + " into SUMORTree with boundary " + toString(b));
141  }
142  }
143  // insert it in Tree
144  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
145  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
146  Insert(cmin, cmax, o);
147  }
148 
153  // check if lock is locked remove insert objects
154  if(myLock.locked()) {
155  ProcessError("Mutex of SUMORTree is locked before object remove (Lock value = " + toString(myLock.lockCount())+ ")");
156  }
157  // lock mutex
159  // obtain boundary of object
161  // show information in gui testing debug gl mode
162  if (MsgHandler::writeDebugGLMessages() && (myTreeDebug.count(o) != 0)) {
163  if (b != myTreeDebug.at(o)) {
164  throw ProcessError("add boundary of GUIGlObject " + o->getMicrosimID() + " is different of remove boundary (" + toString(b) + " != " + toString(myTreeDebug.at(o)) + ")");
165  } else {
166  myTreeDebug.erase(o);
167  WRITE_GLDEBUG("Removed object " + o->getFullName() + " from SUMORTree with boundary " + toString(b));
168  }
169  }
170  // remove it from Tree
171  const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
172  const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
173  Remove(cmin, cmax, o);
174  }
175 
176 protected:
178  mutable MFXMutex myLock;
179 
180 private:
184  std::map<GUIGlObject*, Boundary> myTreeDebug;
185 };
186 
187 
188 #endif
189 
190 /****************************************************************************/
191 
MFXMutex myLock
A mutex avoiding parallel change and traversal of the tree.
Definition: SUMORTree.h:178
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:152
#define WRITE_GLDEBUG(msg)
Definition: MsgHandler.h:249
static bool writeDebugGLMessages()
check whether to enable/disable gl-debug messages
Definition: MsgHandler.h:93
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:102
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
FXuint lockCount()
return current lock value
Definition: MFXMutex.h:68
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
#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
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:124
A mutex encapsulator which locks/unlocks the given mutex on construction/destruction, respectively.
Definition: AbstractMutex.h:59
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:184
#define GUI_RTREE_QUAL
Definition: SUMORTree.h:36
FXbool locked()
check if mutex is locked
Definition: MFXMutex.h:63
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:91
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
virtual ~SUMORTree()
Destructor.
Definition: SUMORTree.h:76