User Manual, Developers Guide and API Documentation

Map.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 #include <RISE/scenario/mobility/roadmap/Map.hpp>
00029 #include <RISE/scenario/mobility/roadmap/Street.hpp>
00030 #include <RISE/scenario/mobility/roadmap/Crossing.hpp>
00031 #include <RISE/scenario/mobility/Mobility.hpp>
00032 
00033 #include <WNS/module/Base.hpp>
00034 #include <WNS/pyconfig/View.hpp>
00035 
00036 using namespace rise::scenario::mobility::roadmap;
00037 using namespace std;
00038 
00040 Map::Map(const wns::pyconfig::View& roadmapView) :
00041     mapObjectContainer(),
00042     streetIDContainer(),
00043     streetDist(),
00044     headingDist(),
00045     entryPointDist(),
00046     log(roadmapView.getView("logger"))
00047 {
00048     mapObjectContainer.resize(roadmapView.len("crossings")+roadmapView.len("streets"));
00049     // 1.) read street and crossing Objects from pyconfig::Parser
00050     for (int s=0; s<roadmapView.len("streets"); ++s)
00051     {
00052         Street* aStreet = new Street(roadmapView.getView("streets",s), this);
00053         if (aStreet->getID() >= mapObjectContainer.size())
00054             mapObjectContainer.resize(aStreet->getID()+1);
00055         mapObjectContainer.at(aStreet->getID()) = aStreet;
00056         streetIDContainer.push_back(aStreet->getID());
00057     }
00058     for (int c=0; c<roadmapView.len("crossings"); ++c)
00059     {
00060         Crossing* aCrossing = new Crossing(roadmapView.getView("crossings",c), this);
00061         if (aCrossing->getID() >= mapObjectContainer.size())
00062             mapObjectContainer.resize(aCrossing->getID()+1);
00063         mapObjectContainer.at(aCrossing->getID()) = aCrossing;
00064     }
00065     // 2.) check Consistency of Map structure
00066     this->checkConsistency();
00067     // 3.) initialize random distributions
00068     wns::rng::RNGen* rng = wns::simulator::getRegistry()->find<wns::rng::RNGen*>("MOBILITY-RNG");
00069     streetDist = 
00070         new wns::distribution::DiscreteUniform(0, streetIDContainer.size()-1, rng);
00072     headingDist = 
00073         new wns::distribution::DiscreteUniform(0, 1, rng);
00075     entryPointDist = new wns::distribution::StandardUniform(rng);
00076 
00077 }
00078 
00080 Map::~Map()
00081 {
00082     delete streetDist;
00083     delete headingDist;
00084     delete entryPointDist;
00085 }
00086 
00087 
00088 void
00089 Map::checkConsistency()
00090 {
00091     MESSAGE_BEGIN(NORMAL, log, m, "Starting Roadmap Integrity Check: ");
00092     MESSAGE_END();
00093     int mocs = mapObjectContainer.size();
00094     size_t streetCounter=0;
00095     int crossingCounter=0;
00096 
00097     for (int i=0; i<mocs; ++i){
00098         // visit and inspect all mapobjects
00099         if (mapObjectContainer[i]){
00100             Street* s = dynamic_cast<Street*>(mapObjectContainer[i]);
00101             if (s){
00102                 // found a street
00103                 ++streetCounter;
00104                 continue;
00105             }
00106             Crossing* c = dynamic_cast<Crossing*>(mapObjectContainer[i]);
00107             if (c){
00108                 // found a crossing
00109                 ++crossingCounter;
00110                 continue;
00111             }
00112             // get the objects connected to the currently inspected ones
00113             std::vector<MapObject::ID> deps = mapObjectContainer[i]->getDependencies();
00114             for (unsigned int j=0; j<deps.size(); ++j){
00115                 // assure that all objects we are connected with exist in the container
00116                 assure( mapObjectContainer.at(deps[i]), "object has undefined references!");
00117             }
00118         }
00119     }
00120 
00121     // another safety check to make sure we have all streets
00122     assure(streetCounter == streetIDContainer.size(), "Error after street counting!");
00123 
00124     MESSAGE_BEGIN(NORMAL, log, m, "Successfully passed Roadmap integrity check.");
00125     m << "nStreets: " << streetCounter << " nCrossings: " << crossingCounter;
00126     MESSAGE_END();
00127 }
00128 
00132 MapUser*
00133 Map::createNewUser(double velocity)
00134 {
00135     // randomly select a street
00136     MapObject::ID aStreet = streetIDContainer.at(int((*streetDist)()));
00137     MapUser::Heading aHeading;
00138     // randomly select a heading, the distribution returns 0 or 1
00139     if ( int((*headingDist)()) ){
00140         aHeading = Headings::Forward();
00141     } else {
00142         aHeading = Headings::Reverse();
00143     }
00144     // randomly select an entryPoint into the street
00145     double aPercentage = (*entryPointDist)();
00146 
00147     MapUser* newUser = new MapUser(aStreet, aPercentage, aHeading, velocity);
00148 
00149     MESSAGE_BEGIN(NORMAL, log, m, "MapUser created at ");
00150     m << getPosition(newUser) << " with velocity "
00151       << velocity << " m/s, heading "
00152       << (aHeading==Headings::Forward() ? "forward, " : "reverse, ")
00153       << int(100.0*aPercentage) << "% into street " << aStreet;
00154     MESSAGE_END();
00155 
00156     return newUser;
00157 }
00158 
00159 
00160 
00164 wns::Position
00165 Map::getNextPosition(MapUser* user, simTimeType dt)
00166 {
00167     wns::Position newPosition = getObject(user->currentObject)->getNextPosition(user,dt);
00168     MESSAGE_BEGIN(NORMAL, log, m, "User moved to ");
00169     m << newPosition
00170       << " with velocity " << user->velocity << " m/s, "
00171       << int(100.0*user->percent) << "% into street " << user->currentObject;
00172     MESSAGE_END();
00173     return newPosition;
00174 }
00175 
00176 wns::Position
00177 Map::getPosition(MapUser* user)
00178 {
00179     return getObject(user->currentObject)->getPosition(user);
00180 }
00181 
00184 MapObject*
00185 Map::getObject(const MapObject::ID id)
00186 {
00187     assure(mapObjectContainer.at(id), "You searched for a non-existing MapObject!");
00188     return mapObjectContainer.at(id);
00189 }
00190 
00191 
00192 
00193 
00194 

Generated on Sun May 27 03:31:39 2012 for openWNS by  doxygen 1.5.5