SUMO - Simulation of Urban MObility
NBContHelper.h
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // Some methods for traversing lists of edges
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 #ifndef NBContHelper_h
00023 #define NBContHelper_h
00024 
00025 
00026 // ===========================================================================
00027 // included modules
00028 // ===========================================================================
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 #include <vector>
00036 #include <iostream>
00037 #include <cmath>
00038 #include <algorithm>
00039 #include <cassert>
00040 #include "NBHelpers.h"
00041 #include "NBCont.h"
00042 #include "NBEdge.h"
00043 #include "NBNode.h"
00044 #include <utils/common/StdDefs.h>
00045 #include <utils/geom/GeomHelper.h>
00046 
00047 
00048 // ===========================================================================
00049 // class definitions
00050 // ===========================================================================
00056 class NBContHelper {
00057 public:
00060     static void nextCW(const EdgeVector& edges,
00061                        EdgeVector::const_iterator& from);
00062 
00065     static void nextCCW(const EdgeVector& edges,
00066                         EdgeVector::const_iterator& from);
00067 
00068     static SUMOReal getMaxSpeed(const EdgeVector& edges);
00069 
00070     static SUMOReal getMinSpeed(const EdgeVector& edges);
00071 
00073     static std::ostream& out(std::ostream& os, const std::vector<bool> &v);
00074 
00075 
00084     class relative_edge_sorter {
00085     public:
00087         explicit relative_edge_sorter(NBEdge* e, NBNode* n)
00088             : myEdge(e), myNode(n) {}
00089 
00090     public:
00092         int operator()(NBEdge* e1, NBEdge* e2) const {
00093             if (e1 == 0 || e2 == 0) {
00094                 return -1;
00095             }
00096             SUMOReal relAngle1 = NBHelpers::normRelAngle(
00097                                      myEdge->getAngle(), e1->getAngle());
00098             SUMOReal relAngle2 = NBHelpers::normRelAngle(
00099                                      myEdge->getAngle(), e2->getAngle());
00100             return relAngle1 > relAngle2;
00101         }
00102 
00103     private:
00105         NBEdge* myEdge;
00106 
00108         NBNode* myNode;
00109 
00110     };
00111 
00112 
00117     class edge_by_priority_sorter {
00118     public:
00120         int operator()(NBEdge* e1, NBEdge* e2) const {
00121             if (e1->getPriority() != e2->getPriority()) {
00122                 return e1->getPriority() > e2->getPriority();
00123             }
00124             if (e1->getSpeed() != e2->getSpeed()) {
00125                 return e1->getSpeed() > e2->getSpeed();
00126             }
00127             return e1->getNumLanes() > e2->getNumLanes();
00128         }
00129     };
00130 
00131     // ---------------------------
00132 
00140     class edge_opposite_direction_sorter {
00141     public:
00146         explicit edge_opposite_direction_sorter(const NBEdge* const e, const NBNode* const n)
00147             : myNode(n) {
00148             myAngle = getEdgeAngleAt(e, n);
00149         }
00150 
00156         int operator()(NBEdge* e1, NBEdge* e2) const {
00157             SUMOReal d1 = getDiff(e1);
00158             SUMOReal d2 = getDiff(e2);
00159             return d1 > d2;
00160         }
00161 
00162     protected:
00167         SUMOReal getDiff(const NBEdge* const e) const {
00168             SUMOReal d = getEdgeAngleAt(e, myNode);
00169             return GeomHelper::getMinAngleDiff(d, myAngle);
00170         }
00171 
00178         SUMOReal getEdgeAngleAt(const NBEdge* const e, const NBNode* const n) const {
00179             if (e->getFromNode() == n) {
00180                 return e->getGeometry().getBegLine().atan2DegreeAngle();
00181             } else {
00182                 return e->getGeometry().getEndLine().reverse().atan2DegreeAngle();
00183             }
00184         }
00185 
00186     private:
00188         SUMOReal myAngle;
00189 
00191         const NBNode* const myNode;
00192 
00193     };
00194 
00195     // ---------------------------
00196 
00203     class edge_similar_direction_sorter {
00204     public:
00206         explicit edge_similar_direction_sorter(const NBEdge* const e)
00207             : myAngle(e->getAngle()) {}
00208 
00210         int operator()(NBEdge* e1, NBEdge* e2) const {
00211             SUMOReal d1 = GeomHelper::getMinAngleDiff(e1->getAngle(), myAngle);
00212             SUMOReal d2 = GeomHelper::getMinAngleDiff(e2->getAngle(), myAngle);
00213             return d1 < d2;
00214         }
00215 
00216     private:
00218         SUMOReal myAngle;
00219     };
00220 
00221 
00225     class node_with_incoming_finder {
00226     public:
00228         node_with_incoming_finder(const NBEdge* const e);
00229 
00230         bool operator()(const NBNode* const n) const;
00231 
00232     private:
00233         const NBEdge* const myEdge;
00234 
00235     private:
00237         node_with_incoming_finder& operator=(const node_with_incoming_finder& s);
00238 
00239     };
00240 
00241 
00245     class node_with_outgoing_finder {
00246     public:
00248         node_with_outgoing_finder(const NBEdge* const e);
00249 
00250         bool operator()(const NBNode* const n) const;
00251 
00252     private:
00253         const NBEdge* const myEdge;
00254 
00255     private:
00257         node_with_outgoing_finder& operator=(const node_with_outgoing_finder& s);
00258 
00259     };
00260 
00261 
00262 
00263 
00264     class edge_with_destination_finder {
00265     public:
00267         edge_with_destination_finder(NBNode* dest);
00268 
00269         bool operator()(NBEdge* e) const;
00270 
00271     private:
00272         NBNode* myDestinationNode;
00273 
00274     private:
00276         edge_with_destination_finder& operator=(const edge_with_destination_finder& s);
00277 
00278     };
00279 
00280 
00283     static NBEdge* findConnectingEdge(const EdgeVector& edges,
00284                                       NBNode* from, NBNode* to);
00285 
00286 
00288     static SUMOReal maxSpeed(const EdgeVector& ev);
00289 
00296     class same_connection_edge_sorter {
00297     public:
00299         explicit same_connection_edge_sorter() { }
00300 
00302         int operator()(NBEdge* e1, NBEdge* e2) const {
00303             std::pair<SUMOReal, SUMOReal> mm1 = getMinMaxRelAngles(e1);
00304             std::pair<SUMOReal, SUMOReal> mm2 = getMinMaxRelAngles(e2);
00305             if (mm1.first == mm2.first && mm1.second == mm2.second) {
00306                 // ok, let's simply sort them arbitrarily
00307                 return e1->getID() < e2->getID();
00308             }
00309 
00310             assert(
00311                 (mm1.first <= mm2.first && mm1.second <= mm2.second)
00312                 ||
00313                 (mm1.first >= mm2.first && mm1.second >= mm2.second));
00314             return (mm1.first >= mm2.first && mm1.second >= mm2.second);
00315         }
00316 
00320         std::pair<SUMOReal, SUMOReal> getMinMaxRelAngles(NBEdge* e) const {
00321             SUMOReal min = 360;
00322             SUMOReal max = 360;
00323             const EdgeVector& ev = e->getConnectedEdges();
00324             for (EdgeVector::const_iterator i = ev.begin(); i != ev.end(); ++i) {
00325                 SUMOReal angle = NBHelpers::normRelAngle(
00326                                      e->getAngle(), (*i)->getAngle());
00327                 if (min == 360 || min > angle) {
00328                     min = angle;
00329                 }
00330                 if (max == 360 || max < angle) {
00331                     max = angle;
00332                 }
00333             }
00334             return std::pair<SUMOReal, SUMOReal>(min, max);
00335         }
00336     };
00337 
00338 
00339     friend std::ostream& operator<<(std::ostream& os, const EdgeVector& ev);
00340 
00341     class opposite_finder {
00342     public:
00344         opposite_finder(NBEdge* edge, const NBNode* n)
00345             : myReferenceEdge(edge), myAtNode(n) { }
00346 
00347         bool operator()(NBEdge* e) const {
00348             return e->isTurningDirectionAt(myAtNode, myReferenceEdge) ||
00349                    myReferenceEdge->isTurningDirectionAt(myAtNode, e);
00350         }
00351 
00352     private:
00353         NBEdge* myReferenceEdge;
00354         const NBNode* myAtNode;
00355 
00356     };
00357 
00358 
00359 };
00360 
00361 
00362 #endif
00363 
00364 /****************************************************************************/
00365 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines