Transit Planner  1.0
An experiment on transfer patterns robustness
src/StopTree.h
00001 // Copyright 2012: Eugen Sawin, Philip Stahl, Jonas Sternisko
00002 #ifndef SRC_STOPTREE_H_
00003 #define SRC_STOPTREE_H_
00004 
00005 #include <boost/serialization/access.hpp>
00006 #include <kdtree++/kdtree.hpp>
00007 #include <string>
00008 #include <vector>
00009 
00010 class RandomFloatGen;
00011 
00012 using std::string;
00013 using std::vector;
00014 
00015 // A class for a transit stop, e.g. 'Haltestelle Runzmattenweg'.
00016 class Stop {
00017  public:
00018   static const float kInvalidPos;
00019 
00020   // Constructor (standard, short, full)
00021   Stop();
00022   Stop(const string& id, float lat, float lon);
00023   Stop(const string& id, const string& name, float lat, float lon);
00024   string id() const { return _stopId; }
00025   string name() const { return _stopName; }
00026   float lat() const { return _lat; }
00027   float lon() const { return _lon; }
00028   int index() const { return _index; }
00029   void index(const int i) { _index = i; }
00030   const vector<int>& getNodeIndices() const { return _nodeIndices; }
00031   void addNodeIndex(const int id) { _nodeIndices.push_back(id); }
00032   int numNodes() const { return _nodeIndices.size(); }
00033   // Returns the index of the Stop's ith node.
00034   int nodeIndex(const int i) const { return _nodeIndices.at(i); }
00035   vector<int>::iterator nodesBegin() { return _nodeIndices.begin(); }
00036   vector<int>::iterator nodesEnd() { return _nodeIndices.end(); }
00037   // Compare operator.
00038   bool operator==(const Stop& other) const;
00039 
00040  private:
00041   string _stopId;
00042   string _stopName;
00043   float _lat;
00044   float _lon;
00045   // vector of all indices of transit nodes for this stop
00046   vector<int> _nodeIndices;
00047   int _index;
00048 
00049   template<class Archive>
00050   void serialize(Archive& ar, const unsigned int version) {  // NOLINT
00051     ar & _stopId;
00052     ar & _stopName;
00053     ar & _lat;
00054     ar & _lon;
00055     ar & _nodeIndices;
00056     ar & _index;
00057   }
00058   friend class boost::serialization::access;
00059 };
00060 
00061 
00062 std::ostream& operator<<(std::ostream& s, const Stop& stop);  // NOLINT
00063 
00064 
00065 // A node in the kdtree with a position and a pointer to a Node.
00066 class StopTreeNode {
00067  public:
00068   typedef float value_type;
00069 
00070   StopTreeNode() { stopPtr = NULL; }
00071   explicit StopTreeNode(const Stop& stop) {
00072     stopPtr = &stop;
00073     pos[0] = stopPtr->lat();
00074     pos[1] = stopPtr->lon();
00075   }
00076   value_type operator[](size_t i) const {
00077     return pos[i];
00078   }
00079   /*float distance(const StopKDTreeNode& other) {
00080     // NOTE: Here one could use the Great Circle distance, or the Eucleadean
00081     // distance but internally the max over all dimensions is used.
00082     float x = pos[0] - other.pos[0];
00083     float y = pos[1] - other.pos[1];
00084     return sqrt(x * x + y * y);
00085   }*/
00086   value_type pos[2];
00087   const Stop* stopPtr;
00088 };
00089 
00090 
00091 using KDTree::_Node;
00092 // Extension to a 2-d tree in order to implement random descent in the tree.
00093 class StopTree : public KDTree::KDTree<2, StopTreeNode> {
00094  public:
00095   // Performs a descent starting at the node. Returns a random node meaning that
00096   // the direction and depth of the walk depent are choosen probabilistically.
00097   const StopTree::value_type& randomWalk(RandomFloatGen* random) const;
00098 };
00099 
00100 
00101 #endif  // SRC_STOPTREE_H_
 All Classes