hotspotRoaming.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #include "hotspotRoaming.h"
00025 #include "StringConvert.h"
00026 
00027 hotspotRoaming::hotspotRoaming(double areaDimension, double speed, NeighborMap *Neighbors, GlobalCoordinator* coordinator, CollisionList* CollisionRect)
00028               :MovementGenerator(areaDimension, speed, Neighbors, coordinator, CollisionRect)
00029 {
00030     double prob = 0;
00031 
00032     std::vector<std::string> hotspotvec = cStringTokenizer(coordinator->par("Hotspots"), ";").asVector();
00033     for( std::vector<std::string>::iterator it = hotspotvec.begin(); it != hotspotvec.end(); ++it ){
00034         std::vector<std::string> hstr = cStringTokenizer(it->c_str(), ",").asVector();
00035         if( hstr.size() != 4 ) {
00036             throw( cException("Error parsing Hotspots parameter") );
00037         }
00038         // parse string, convert to hotspot data
00039         Hotspot h;
00040         h.center.x = convertString<double>( hstr[0] );
00041         h.center.y = convertString<double>( hstr[1] );
00042         h.radius = convertString<double>( hstr[2] );
00043         h.probability = convertString<double>( hstr[3] );
00044         prob += h.probability;
00045 
00046         // check hotspot bounds
00047         if( h.center.x - h.radius < 0 || h.center.y - h.radius < 0 ||
00048                 h.center.x + h.radius > areaDimension || h.center.y + h.radius > areaDimension ) {
00049          
00050             throw( cException("Error: Hotspot is outside the playground!") );
00051         }
00052         if( prob > 1 ){
00053             throw( cException("Error: Hotspot probabilities add up to > 1!") );
00054         }
00055 
00056         hotspots.push_back(h);
00057     }
00058     curHotspot = hotspots.end();
00059     if( (double) coordinator->par("HotspotStayTime") == (double) 0.0 ) {
00060         stayInHotspot = false;
00061     } else {
00062         stayInHotspot = true;
00063     }
00064     target.x = uniform(0.0, areaDimension);
00065     target.y = uniform(0.0, areaDimension);
00066 }
00067 
00068 void hotspotRoaming::move()
00069 {
00070     flock();
00071     position += direction * speed;
00072     if(testBounds()) {
00073         position += direction * speed * 2;
00074         testBounds();
00075     }
00076 
00077     if(target.distanceSqr(position) < speed * speed) {
00078         // arrived at current destination
00079         
00080         // if we are not inside a hotspot, or do not want to
00081         // stay inside the current hotspot (any more) ...
00082         if ( !stayInHotspot || curHotspot == hotspots.end() ||
00083                   ( hotspotStayTime > 0 && hotspotStayTime < simTime() ))
00084         {
00085             hotspotStayTime = 0;
00086             
00087             // ... select next target hotspot
00088             double rnd = uniform(0, 1);
00089             for( curHotspot = hotspots.begin(); curHotspot != hotspots.end(); ++curHotspot ){
00090                 rnd -= curHotspot->probability;
00091                 if( rnd <= 0 ) break;
00092             }
00093 
00094         } else {
00095             // stay in current hotspot
00096             // start stayTimer if not already done
00097             if ( hotspotStayTime == 0 ) {
00098                 hotspotStayTime = simTime() + coordinator->par("HotspotStayTime");
00099             }
00100         }
00101 
00102         // chose target inside hotspot, or random target if no hotspot was selected
00103         if( curHotspot != hotspots.end() ){
00104             Vector2D dev;
00105             // randomly select point inside the hotspot
00106             double r = uniform( 0, 1 );
00107             double theta = uniform( 0, 2*M_PI );
00108             dev.x = sqrt( r ) * cos( theta );
00109             dev.y = sqrt( r ) * sin( theta );
00110 
00111             target = curHotspot->center + dev*curHotspot->radius;
00112         } else {
00113             target.x = uniform(0.0, areaDimension);
00114             target.y = uniform(0.0, areaDimension);
00115         }
00116     }
00117 }
Generated on Wed May 26 16:21:14 2010 for OverSim by  doxygen 1.6.3