SUMO - Simulation of Urban MObility

NBContHelper.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Some methods for traversing lists of edges
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright (C) 2001-2011 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 #ifndef NBContHelper_h
00020 #define NBContHelper_h
00021 
00022 
00023 // ===========================================================================
00024 // included modules
00025 // ===========================================================================
00026 #ifdef _MSC_VER
00027 #include <windows_config.h>
00028 #else
00029 #include <config.h>
00030 #endif
00031 
00032 #include <vector>
00033 #include <iostream>
00034 #include <cmath>
00035 #include <algorithm>
00036 #include <cassert>
00037 #include "NBHelpers.h"
00038 #include "NBCont.h"
00039 #include "NBEdge.h"
00040 #include "NBNode.h"
00041 #include <utils/common/StdDefs.h>
00042 #include <utils/geom/GeomHelper.h>
00043 
00044 
00045 // ===========================================================================
00046 // class definitions
00047 // ===========================================================================
00053 class NBContHelper {
00054 public:
00057     static void nextCW(const EdgeVector& edges,
00058                        EdgeVector::const_iterator& from);
00059 
00062     static void nextCCW(const EdgeVector& edges,
00063                         EdgeVector::const_iterator& from);
00064 
00065     static SUMOReal getMaxSpeed(const EdgeVector& edges);
00066 
00067     static SUMOReal getMinSpeed(const EdgeVector& edges);
00068 
00070     static std::ostream& out(std::ostream& os, const std::vector<bool> &v);
00071 
00076     class edge_by_junction_angle_sorter {
00077     public:
00079         explicit edge_by_junction_angle_sorter(NBNode* n) : myNode(n) {}
00080 
00082         int operator()(NBEdge* e1, NBEdge* e2) const;
00083 
00084     private:
00086         SUMOReal getConvAngle(NBEdge* e) const;
00087 
00088     private:
00090         NBNode* myNode;
00091 
00092     };
00093 
00102     class relative_edge_sorter {
00103     public:
00105         explicit relative_edge_sorter(NBEdge* e, NBNode* n)
00106             : myEdge(e), myNode(n) {}
00107 
00108     public:
00110         int operator()(NBEdge* e1, NBEdge* e2) const {
00111             if (e1==0||e2==0) {
00112                 return -1;
00113             }
00114             SUMOReal relAngle1 = NBHelpers::normRelAngle(
00115                                      myEdge->getAngle(), e1->getAngle());
00116             SUMOReal relAngle2 = NBHelpers::normRelAngle(
00117                                      myEdge->getAngle(), e2->getAngle());
00118             return relAngle1 > relAngle2;
00119         }
00120 
00121     private:
00123         NBEdge* myEdge;
00124 
00126         NBNode* myNode;
00127 
00128     };
00129 
00130 
00135     class edge_by_priority_sorter {
00136     public:
00138         int operator()(NBEdge* e1, NBEdge* e2) const {
00139             if (e1->getPriority()!=e2->getPriority()) {
00140                 return e1->getPriority() > e2->getPriority();
00141             }
00142             if (e1->getSpeed()!=e2->getSpeed()) {
00143                 return e1->getSpeed() > e2->getSpeed();
00144             }
00145             return e1->getNumLanes() > e2->getNumLanes();
00146         }
00147     };
00148 
00149     // ---------------------------
00150 
00158     class edge_opposite_direction_sorter {
00159     public:
00164         explicit edge_opposite_direction_sorter(const NBEdge* const e, const NBNode* const n) throw()
00165             : myNode(n) {
00166             myAngle = getEdgeAngleAt(e, n);
00167         }
00168 
00174         int operator()(NBEdge* e1, NBEdge* e2) const throw() {
00175             SUMOReal d1 = getDiff(e1);
00176             SUMOReal d2 = getDiff(e2);
00177             return d1 > d2;
00178         }
00179 
00180     protected:
00185         SUMOReal getDiff(const NBEdge* const e) const throw() {
00186             SUMOReal d = getEdgeAngleAt(e, myNode);
00187             return GeomHelper::getMinAngleDiff(d, myAngle);
00188         }
00189 
00196         SUMOReal getEdgeAngleAt(const NBEdge* const e, const NBNode* const n) const throw() {
00197             if (e->getFromNode()==n) {
00198                 return e->getGeometry().getBegLine().atan2DegreeAngle();
00199             } else {
00200                 return e->getGeometry().getEndLine().reverse().atan2DegreeAngle();
00201             }
00202         }
00203 
00204     private:
00206         SUMOReal myAngle;
00207 
00209         const NBNode* const myNode;
00210 
00211     };
00212 
00213     // ---------------------------
00214 
00221     class edge_similar_direction_sorter {
00222     public:
00224         explicit edge_similar_direction_sorter(const NBEdge* const e)
00225             : myAngle(e->getAngle()) {}
00226 
00228         int operator()(NBEdge* e1, NBEdge* e2) const {
00229             SUMOReal d1 = GeomHelper::getMinAngleDiff(e1->getAngle(), myAngle);
00230             SUMOReal d2 = GeomHelper::getMinAngleDiff(e2->getAngle(), myAngle);
00231             return d1 < d2;
00232         }
00233 
00234     private:
00236         SUMOReal myAngle;
00237     };
00238 
00239 
00243     class node_with_incoming_finder {
00244     public:
00246         node_with_incoming_finder(const NBEdge* const e);
00247 
00248         bool operator()(const NBNode* const n) const;
00249 
00250     private:
00251         const NBEdge* const myEdge;
00252 
00253     private:
00255         node_with_incoming_finder& operator=(const node_with_incoming_finder& s);
00256 
00257     };
00258 
00259 
00263     class node_with_outgoing_finder {
00264     public:
00266         node_with_outgoing_finder(const NBEdge* const e);
00267 
00268         bool operator()(const NBNode* const n) const;
00269 
00270     private:
00271         const NBEdge* const myEdge;
00272 
00273     private:
00275         node_with_outgoing_finder& operator=(const node_with_outgoing_finder& s);
00276 
00277     };
00278 
00279 
00280 
00281 
00282     class edge_with_destination_finder {
00283     public:
00285         edge_with_destination_finder(NBNode* dest);
00286 
00287         bool operator()(NBEdge* e) const;
00288 
00289     private:
00290         NBNode* myDestinationNode;
00291 
00292     private:
00294         edge_with_destination_finder& operator=(const edge_with_destination_finder& s);
00295 
00296     };
00297 
00298 
00301     static NBEdge* findConnectingEdge(const EdgeVector& edges,
00302                                       NBNode* from, NBNode* to);
00303 
00304 
00306     static SUMOReal maxSpeed(const EdgeVector& ev);
00307 
00314     class same_connection_edge_sorter {
00315     public:
00317         explicit same_connection_edge_sorter() { }
00318 
00320         int operator()(NBEdge* e1, NBEdge* e2) const {
00321             std::pair<SUMOReal, SUMOReal> mm1 = getMinMaxRelAngles(e1);
00322             std::pair<SUMOReal, SUMOReal> mm2 = getMinMaxRelAngles(e2);
00323             if (mm1.first==mm2.first && mm1.second==mm2.second) {
00324                 // ok, let's simply sort them arbitrarily
00325                 return e1->getID() < e2->getID();
00326             }
00327 
00328             assert(
00329                 (mm1.first<=mm2.first&&mm1.second<=mm2.second)
00330                 ||
00331                 (mm1.first>=mm2.first&&mm1.second>=mm2.second));
00332             return (mm1.first>=mm2.first&&mm1.second>=mm2.second);
00333         }
00334 
00338         std::pair<SUMOReal, SUMOReal> getMinMaxRelAngles(NBEdge* e) const {
00339             SUMOReal min = 360;
00340             SUMOReal max = 360;
00341             const EdgeVector& ev = e->getConnectedEdges();
00342             for (EdgeVector::const_iterator i=ev.begin(); i!=ev.end(); ++i) {
00343                 SUMOReal angle = NBHelpers::normRelAngle(
00344                                      e->getAngle(), (*i)->getAngle());
00345                 if (min==360||min>angle) {
00346                     min = angle;
00347                 }
00348                 if (max==360||max<angle) {
00349                     max = angle;
00350                 }
00351             }
00352             return std::pair<SUMOReal, SUMOReal>(min, max);
00353         }
00354     };
00355 
00356 
00357     friend std::ostream& operator<<(std::ostream& os, const EdgeVector& ev);
00358 
00359     class opposite_finder {
00360     public:
00362         opposite_finder(NBEdge* edge, const NBNode* n)
00363             : myReferenceEdge(edge), myAtNode(n) { }
00364 
00365         bool operator()(NBEdge* e) const {
00366             return e->isTurningDirectionAt(myAtNode, myReferenceEdge)||
00367                    myReferenceEdge->isTurningDirectionAt(myAtNode, e);
00368         }
00369 
00370     private:
00371         NBEdge* myReferenceEdge;
00372         const NBNode* myAtNode;
00373 
00374     };
00375 
00376 
00377 };
00378 
00379 
00380 #endif
00381 
00382 /****************************************************************************/
00383 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines