00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #include "config.h"
00056
00057 #include <math.h>
00058 #include <fstream>
00059
00060
00061 #include <delay.h>
00062 #include <packet.h>
00063 #include <packet-stamp.h>
00064 #include <antenna.h>
00065 #include <mobilenode.h>
00066 #include <propagation.h>
00067 #include <wireless-phy.h>
00068 #include <shadowing-vis.h>
00069
00070 #define BLACK 0
00071
00072
00073
00074
00075 static class ShadowingVisClass: public TclClass {
00076 public:
00077 ShadowingVisClass() : TclClass("Propagation/ShadowingVis") {}
00078 TclObject* create(int, const char*const*) {
00079 return (new ShadowingVis);
00080 }
00081 } class_visibility_shadowing;
00082
00083
00084 ShadowingVis::ShadowingVis()
00085 {
00086 goodProp = NULL;
00087 badProp = NULL;
00088 }
00089
00090
00091 double ShadowingVis::Pr(PacketStamp *t, PacketStamp *r, WirelessPhy *ifp)
00092 {
00093
00094 double Xt, Yt, Zt;
00095 double Xr, Yr, Zr;
00096 double Pr;
00097
00098 t->getNode()->getLoc(&Xt, &Yt, &Zt);
00099 r->getNode()->getLoc(&Xr, &Yr, &Zr);
00100
00101
00102 Xt += t->getAntenna()->getX();
00103 Yt += t->getAntenna()->getY();
00104 Xr += r->getAntenna()->getX();
00105 Yr += r->getAntenna()->getY();
00106
00107
00108
00109
00110
00111 int propCond = getPropCond(Xt, Yt, Xr, Yr);
00112 if (propCond == 1) {
00113 Pr = goodProp->Pr(t, r, ifp);
00114 #ifdef DEBUG
00115 printf("Good propagation condition!\n");
00116 #endif
00117 } else {
00118 Pr = badProp->Pr(t, r, ifp);
00119 #ifdef DEBUG
00120 printf("Bad propagation condition!\n");
00121 #endif
00122 }
00123
00124 return Pr;
00125 }
00126
00127
00128 int ShadowingVis::command(int argc, const char*const* argv)
00129 {
00130 TclObject *obj;
00131
00132 if (argc == 3) {
00133 if (strcmp(argv[1], "get-bitmap") == 0) {
00134 loadImg(argv[2]);
00135 return(TCL_OK);
00136 }
00137 if (strcmp(argv[1], "set-ppm") == 0) {
00138 ppm = atoi(argv[2]);
00139 return(TCL_OK);
00140 }
00141 }
00142 if (argc == 4) {
00143 if (strcmp(argv[1], "add-models") == 0) {
00144 if( (obj = TclObject::lookup(argv[2])) == 0) {
00145 fprintf(stderr, "ShadowingVis: %s lookup of %s failed\n", \
00146 argv[1], argv[2]);
00147 return TCL_ERROR;
00148 } else {
00149 goodProp = (Shadowing*) obj;
00150 }
00151 if( (obj = TclObject::lookup(argv[3])) == 0) {
00152 fprintf(stderr, "ShadowingVis: %s lookup of %s failed\n", \
00153 argv[1], argv[3]);
00154 return TCL_ERROR;
00155 } else {
00156 badProp = (Shadowing*) obj;
00157 }
00158 return(TCL_OK);
00159 }
00160 }
00161
00162 return Propagation::command(argc, argv);
00163 }
00164
00165
00166 void ShadowingVis::loadImg(const char* fname)
00167 {
00168 #ifdef DEBUG
00169 printf("loading %s\n", fname);
00170 #endif
00171
00172 ifstream image( fname );
00173
00174 char magicNumber[10];
00175 char comment[256];
00176 int whiteNum;
00177 char terminator;
00178
00179 if( image.fail() ) printf("image bad!\n");
00180
00181 if( strstr( fname, ".pnm" ) ) {
00182 image >> magicNumber;
00183 image.get( terminator );
00184 image.get( comment, 255, '\n');
00185 image.get( terminator );
00186 image >> width >> height >> whiteNum;
00187 } else if( strstr( fname, ".pgm" ) ) {
00188 image >> magicNumber;
00189 image.get( terminator );
00190 image >> width >> height >> whiteNum;
00191 }
00192
00193 #ifdef DEBUG
00194 printf("magic: %s\ncomment: %s\nwidth: %d height: %d white: %d\n", magicNumber, comment, width, height, whiteNum);
00195 #endif
00196
00197 pixel = new unsigned char[width * height];
00198
00199 char a;
00200 image.get(a);
00201
00202
00203 for(int n = 0; n < height; n++) {
00204 for(int m = 0; m < width; m++) {
00205 image.get( a );
00206 pixel[m + n * width] = a;
00207 }
00208 }
00209
00210 #ifdef DEBUG
00211 FILE *testFile;
00212 char *filename = "imgtest.hex";
00213 testFile = fopen(filename, "w");
00214 for (int n = 0; n < height; n++) {
00215 for (int m = 0; m < width; m++)
00216 if (pixel[m + n * height] == BLACK)
00217 fprintf(testFile, "%x", pixel[m + n * height]);
00218 else
00219 fprintf(testFile, "%x", 0xf);
00220 fprintf(testFile, "\n");
00221 }
00222 fclose(testFile);
00223 #endif
00224
00225 image.close();
00226 }
00227
00228
00229 int ShadowingVis::getPropCond(float xt, float yt, float xr, float yr)
00230 {
00231 #ifdef DEBUG
00232 printf("xt = %f yt = %f xr = %f yr = %f\n", xt, yt, xr, yr);
00233 #endif
00234 float dx = xr - xt;
00235 float dy = yr - yt;
00236 float maxDistSq = dx * dx + dy * dy;
00237 float maxDist = sqrt(maxDistSq);
00238 dx /= maxDist;
00239 dy /= maxDist;
00240 maxDistSq = (float)(ppm * ppm) * maxDistSq;
00241
00242 float xtPxl = (float)ppm * xt;
00243 float ytPxl = (float)ppm * yt;
00244
00245
00246
00247
00248
00249 int goodProp = 1;
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284 float distX = 0;
00285 float distY = 0;
00286 float distSq = 0;
00287 while (distSq < maxDistSq) {
00288 int xIdx = (int)(xtPxl + distX);
00289 int yIdx = (int)(ytPxl + distY);
00290 if (pixel[xIdx + yIdx * width] != BLACK) {
00291 goodProp = 0;
00292 #ifdef DEBUG
00293 printf("xx = %f yy = %f\n", xx, yy);
00294 printf("pixel = %x\n", pixel[xIdx + yIdx * width]);
00295 #endif
00296 break;
00297 }
00298 distX += dx;
00299 distY += dy;
00300 distSq = distX * distX + distY * distY;
00301 }
00302
00303 return goodProp;
00304 }
00305