shadowing-vis.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 
00003 /*
00004  * shadowing-vis.cc
00005  * Copyright (C) 2000 by the University of Southern California
00006  * $Id: shadowing-vis.cc,v 1.6 2005/08/25 18:58:09 johnh Exp $
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License,
00010  * version 2, as published by the Free Software Foundation.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License along
00018  * with this program; if not, write to the Free Software Foundation, Inc.,
00019  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00020  *
00021  *
00022  * The copyright of this module includes the following
00023  * linking-with-specific-other-licenses addition:
00024  *
00025  * In addition, as a special exception, the copyright holders of
00026  * this module give you permission to combine (via static or
00027  * dynamic linking) this module with free software programs or
00028  * libraries that are released under the GNU LGPL and with code
00029  * included in the standard release of ns-2 under the Apache 2.0
00030  * license or under otherwise-compatible licenses with advertising
00031  * requirements (or modified versions of such code, with unchanged
00032  * license).  You may copy and distribute such a system following the
00033  * terms of the GNU GPL for this module and the licenses of the
00034  * other code concerned, provided that you include the source code of
00035  * that other code when and as the GNU GPL requires distribution of
00036  * source code.
00037  *
00038  * Note that people who make modified versions of this module
00039  * are not obligated to grant this special exception for their
00040  * modified versions; it is their choice whether to do so.  The GNU
00041  * General Public License gives permission to release a modified
00042  * version without this exception; this exception also makes it
00043  * possible to release a modified version which carries forward this
00044  * exception.
00045  *
00046  */
00047 
00048 /*
00049  * Visibility-Shadowing propation model. It has 2 shadowing model instances
00050  * that represent good (has line-of-sight path) and bad (obstructed)
00051  * propagation conditions.
00052  * Wei Ye, weiye@isi.edu, 2000
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 //#define WHITE 0xff
00072 
00073 //#define DEBUG
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;      // loc of transmitter
00095     double Xr, Yr, Zr;      // loc of receiver
00096     double Pr;          // received signal power
00097 
00098     t->getNode()->getLoc(&Xt, &Yt, &Zt);
00099     r->getNode()->getLoc(&Xr, &Yr, &Zr);
00100 
00101     // Is antenna position relative to node position?
00102     Xt += t->getAntenna()->getX();
00103     Yt += t->getAntenna()->getY();
00104     Xr += r->getAntenna()->getX();
00105     Yr += r->getAntenna()->getY();
00106         
00107     // Since we use a bitmap, it's a 2-d environment.
00108     // So Zt and Zr are omitted.
00109     
00110     //get propagation conidition between T/R
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     //unsigned char a;   #Sun OS compilation Fix - fcartegn@univ-valenciennes.fr - Apr 26 02
00199     char a;
00200     image.get(a);   // this byte is not the image
00201 
00202     // the upper-left corner is the origin (0, 0)
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     // Now each time there is only one pixel is test along the line from
00246     // the transmitter to the receiver. If ppm is large, more pixels 
00247     // can be tested: if the line of sight path is just obstructed by
00248     // a very small object, the propagation is still good
00249     int goodProp = 1;
00250 /*
00251     float distSq = 0; 
00252     while (distSq < maxDistSq) {
00253         int xIdx = (int)(xx * ppm);
00254         int yIdx = (int)(yy * ppm);
00255         if ( pixel[xIdx + yIdx * width] != BLACK ) {
00256             goodProp = 0;
00257 #ifdef DEBUG
00258             cout << "xx = " << xx << " yy = " << yy << endl;
00259             printf("pixel = %x\n", pixel[xIdx + yIdx * width]);
00260 #endif
00261             break;
00262         }
00263         xx += dx;
00264         yy += dy;
00265         distSq = (xx - xt) * (xx - xt) + (yy - yt) * (yy - yt);
00266     }
00267 */
00268 /*
00269     int maxRange = (int)(maxDist * ppm); // distance times pixels-per-meter
00270     for (int rng = 0; rng < maxRange; rng++) {
00271         if (pixel[(int)xx + (int)yy * width] != BLACK) {
00272 #ifdef DEBUG
00273             cout << "xx = " << xx << " yy = " << yy << endl;
00274             printf("pixel = %x\n", pixel[(int)xx + (int)yy * width]);
00275 #endif
00276             goodProp = 0;
00277             break;
00278         }
00279         xx += dx;
00280         yy += dy;
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 

Generated on Tue Mar 6 16:47:51 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6