User Manual, Developers Guide and API Documentation

Crossing.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 5, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 
00029 #include <sstream>
00030 
00031 #include <RISE/scenario/mobility/roadmap/Crossing.hpp>
00032 #include <RISE/scenario/mobility/roadmap/Map.hpp>
00033 #include <RISE/scenario/mobility/Mobility.hpp>
00034 
00035 #include <WNS/pyconfig/Sequence.hpp>
00036 #include <WNS/distribution/Uniform.hpp>
00037 #include <WNS/Assure.hpp>
00038 
00039 using namespace rise::scenario::mobility::roadmap;
00040 using namespace std;
00041 
00042 matrix rise::scenario::mobility::roadmap::getMatrix(const wns::pyconfig::View& aView, const std::string& aName)
00043 {
00044     matrix result;
00045     for (int i=0; i<aView.len(aName); ++i)
00046     {
00047         std::stringstream index;
00048         index << i;
00049 
00050         std::string subviewName = aName +
00051             std::string("[") + index.str() + std::string("]");
00052 
00053         std::vector<double> subVector;
00054         for (int j=0; j<aView.len(subviewName); ++j){
00055             subVector.push_back(aView.get<double>(subviewName,j));
00056         }
00057         result.push_back(subVector);
00058     }
00059     return result;
00060 }
00061 
00062 Crossing::Crossing(const wns::pyconfig::View& crossingView, Map* _map) :
00063     MapObject(crossingView.get<MapObject::ID>("ID"), _map),
00064     position(crossingView.getView("position")),
00065     connections(),
00066     probMatrix(getMatrix(crossingView,std::string("probMatrix"))),
00067     uniDis(NULL)
00068 {
00069     // int connection list
00070     for (int i=0; i<crossingView.len("connections"); ++i)
00071         connections.push_back(crossingView.get<MapObject::ID>("connections",i));
00072 
00073     // check consistency of probability Matrix
00074     assure(probMatrix.size()==connections.size(), "Mismatch in connections and probability Matrix.");
00075     for (unsigned int i=0; i<probMatrix.size(); ++i)
00076         assure(probMatrix[i].size()==connections.size(), "Mismatch in connections and probability Matrix.");
00077 
00078     // init the distribution function to determine where to turn
00079     wns::rng::RNGen* rng = wns::simulator::getRegistry()->find<wns::rng::RNGen*>("MOBILITY-RNG");
00080     uniDis = new wns::distribution::StandardUniform(rng);
00081 }
00082 
00084 wns::Position Crossing::getNextPosition(MapUser* user, simTimeType dt) const
00085 {
00086     // where do we come from?
00087     MapObject::ID from = user->currentObject;
00088     unsigned int index;
00089     // determine index of object we come from
00090     for (index=0 ; index<connections.size(); ++index)
00091         if (connections.at(index)==from) break;
00092     // store relevant probability vector handy
00093     std::vector<double> probVector = probMatrix.at(index);
00094     // Choose next connected Object
00095     double randomNumber = (*uniDis)();
00096     unsigned int j = 0;
00097     double aWeight = probVector.at(0);
00098     for ( ; j < probVector.size()-1; ++j)
00099     {
00100         if (randomNumber<aWeight)
00101             break;
00102         aWeight += probVector.at(j+1);
00103     }
00104     // now j should be the index of the next object.
00105     MapObject* next = this->map->getObject(connections.at(j));
00106     user->currentObject = this->id; // my own ID
00107     user->percent = 0.0; // entering a new street
00108     user->heading = Headings::Forward(); // the next street will re-set this
00109     // velocity remains unchanged
00110 
00111     MESSAGE_BEGIN(NORMAL, log, m, "MapUser arrived");
00112     m << " from street " << from 
00113       << " at Crossing " << id 
00114       << " continuing on street " << connections.at(j);
00115     MESSAGE_END();
00116 
00117     // delegate the final decision to the next street.
00118     return next->getNextPosition(user, dt);
00119 }
00120 
00122 #ifndef WNS_NDEBUG
00123 wns::Position Crossing::getPosition(const MapUser* user) const
00124 #else
00125 wns::Position Crossing::getPosition(const MapUser* /*user*/) const
00126 #endif // NOT defined WNS_NDEBUG
00127 {
00128     assure(user->currentObject==this->id, "User/Crossing mismatch.");
00129     return position;
00130 }
00131 
00133 std::vector<MapObject::ID> Crossing::getDependencies() const
00134 {
00135     return connections;
00136 }

Generated on Tue May 22 03:31:53 2012 for openWNS by  doxygen 1.5.5