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 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
00090
00091
00092
00093 if ( !stayInHotspot || curHotspot == hotspots.end() ||
00094 ( hotspotStayTime > 0 && hotspotStayTime < simTime() ))
00095 {
00096 hotspotStayTime = 0;
00097
00098
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
00107
00108 if ( hotspotStayTime == 0 ) {
00109 hotspotStayTime = simTime() + coordinator->par("HotspotStayTime");
00110 }
00111 }
00112
00113
00114 if( curHotspot != hotspots.end() ){
00115 Vector2D dev;
00116
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 }