hotspotRoaming.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
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
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
00079
00080
00081
00082 if ( !stayInHotspot || curHotspot == hotspots.end() ||
00083 ( hotspotStayTime > 0 && hotspotStayTime < simTime() ))
00084 {
00085 hotspotStayTime = 0;
00086
00087
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
00096
00097 if ( hotspotStayTime == 0 ) {
00098 hotspotStayTime = simTime() + coordinator->par("HotspotStayTime");
00099 }
00100 }
00101
00102
00103 if( curHotspot != hotspots.end() ){
00104 Vector2D dev;
00105
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 }