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 double hotspotRoaming::getDistanceFromHotspot()
00069 {
00070     double minDist=areaDimension;
00071     for( std::vector<Hotspot>::iterator it = hotspots.begin(); it != hotspots.end(); ++it) {
00072         double dist = sqrt(position.distanceSqr( it->center )) - it->radius;
00073         if( dist < minDist ) minDist = dist;
00074     }
00075 
00076     return minDist;
00077 }
00078 
00079 void hotspotRoaming::move()
00080 {
00081     flock();
00082     position += direction * speed;
00083     if(testBounds()) {
00084         position += direction * speed * 2;
00085         testBounds();
00086     }
00087 
00088     if(target.distanceSqr(position) < speed * speed) {
00089         // arrived at current destination
00090         
00091         // if we are not inside a hotspot, or do not want to
00092         // stay inside the current hotspot (any more) ...
00093         if ( !stayInHotspot || curHotspot == hotspots.end() ||
00094                   ( hotspotStayTime > 0 && hotspotStayTime < simTime() ))
00095         {
00096             hotspotStayTime = 0;
00097             
00098             // ... select next target hotspot
00099             double rnd = uniform(0, 1);
00100             for( curHotspot = hotspots.begin(); curHotspot != hotspots.end(); ++curHotspot ){
00101                 rnd -= curHotspot->probability;
00102                 if( rnd <= 0 ) break;
00103             }
00104 
00105         } else {
00106             // stay in current hotspot
00107             // start stayTimer if not already done
00108             if ( hotspotStayTime == 0 ) {
00109                 hotspotStayTime = simTime() + coordinator->par("HotspotStayTime");
00110             }
00111         }
00112 
00113         // chose target inside hotspot, or random target if no hotspot was selected
00114         if( curHotspot != hotspots.end() ){
00115             Vector2D dev;
00116             // randomly select point inside the hotspot
00117             double r = uniform( 0, 1 );
00118             double theta = uniform( 0, 2*M_PI );
00119             dev.x = sqrt( r ) * cos( theta );
00120             dev.y = sqrt( r ) * sin( theta );
00121 
00122             target = curHotspot->center + dev*curHotspot->radius;
00123         } else {
00124             target.x = uniform(0.0, areaDimension);
00125             target.y = uniform(0.0, areaDimension);
00126         }
00127     }
00128 }