File: shadowing-vis.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 78,756 kB
  • ctags: 27,476
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 812; awk: 525; csh: 355
file content (273 lines) | stat: -rw-r--r-- 7,262 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */

/*
 * shadowing-vis.cc
 * Copyright (C) 2000 by the University of Southern California
 * $Id: shadowing-vis.cc,v 1.8 2010/04/30 17:10:38 tom_henderson Exp $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 *
 * The copyright of this module includes the following
 * linking-with-specific-other-licenses addition:
 *
 * In addition, as a special exception, the copyright holders of
 * this module give you permission to combine (via static or
 * dynamic linking) this module with free software programs or
 * libraries that are released under the GNU LGPL and with code
 * included in the standard release of ns-2 under the Apache 2.0
 * license or under otherwise-compatible licenses with advertising
 * requirements (or modified versions of such code, with unchanged
 * license).  You may copy and distribute such a system following the
 * terms of the GNU GPL for this module and the licenses of the
 * other code concerned, provided that you include the source code of
 * that other code when and as the GNU GPL requires distribution of
 * source code.
 *
 * Note that people who make modified versions of this module
 * are not obligated to grant this special exception for their
 * modified versions; it is their choice whether to do so.  The GNU
 * General Public License gives permission to release a modified
 * version without this exception; this exception also makes it
 * possible to release a modified version which carries forward this
 * exception.
 *
 */

/*
 * Visibility-Shadowing propation model. It has 2 shadowing model instances
 * that represent good (has line-of-sight path) and bad (obstructed)
 * propagation conditions.
 * Wei Ye, weiye@isi.edu, 2000
 */

#include "config.h"

#include <math.h>
#include <fstream>


#include <delay.h>
#include <packet.h>
#include <packet-stamp.h>
#include <antenna.h>
#include <mobilenode.h>
#include <propagation.h>
#include <wireless-phy.h>
#include <shadowing-vis.h>

#define BLACK 0
//#define WHITE 0xff

//#define DEBUG

static class ShadowingVisClass: public TclClass {
public:
	ShadowingVisClass() : TclClass("Propagation/ShadowingVis") {}
	TclObject* create(int, const char*const*) {
		return (new ShadowingVis);
	}
} class_visibility_shadowing;


ShadowingVis::ShadowingVis()
{
	goodProp = NULL;
	badProp = NULL;
}


double ShadowingVis::Pr(PacketStamp *t, PacketStamp *r, WirelessPhy *ifp)
{

	double Xt, Yt, Zt;		// loc of transmitter
	double Xr, Yr, Zr;		// loc of receiver
	double Pr;			// received signal power

	t->getNode()->getLoc(&Xt, &Yt, &Zt);
	r->getNode()->getLoc(&Xr, &Yr, &Zr);

	// Is antenna position relative to node position?
	Xt += t->getAntenna()->getX();
	Yt += t->getAntenna()->getY();
	Xr += r->getAntenna()->getX();
	Yr += r->getAntenna()->getY();
		
	// Since we use a bitmap, it's a 2-d environment.
	// So Zt and Zr are omitted.
	
	//get propagation conidition between T/R
	int propCond = getPropCond(Xt, Yt, Xr, Yr);
	if (propCond == 1) {
		Pr = goodProp->Pr(t, r, ifp);
#ifdef DEBUG
		printf("Good propagation condition!\n");
#endif
	} else {
		Pr = badProp->Pr(t, r, ifp);
#ifdef DEBUG
		printf("Bad propagation condition!\n");
#endif
	}
		
	return Pr;
}


int ShadowingVis::command(int argc, const char*const* argv)
{
	TclObject *obj;
	
	if (argc == 3) {
		if (strcmp(argv[1], "get-bitmap") == 0) {
			loadImg(argv[2]);
			return(TCL_OK);
		}
		if (strcmp(argv[1], "set-ppm") == 0) {
			ppm = atoi(argv[2]);
			return(TCL_OK);
		}
	}
	if (argc == 4) {
		if (strcmp(argv[1], "add-models") == 0) {
			if( (obj = TclObject::lookup(argv[2])) == 0) {
				fprintf(stderr, "ShadowingVis: %s lookup of %s failed\n", \
					argv[1], argv[2]);
				return TCL_ERROR;
			} else {
				goodProp = (Shadowing*) obj;
			}
			if( (obj = TclObject::lookup(argv[3])) == 0) {
				fprintf(stderr, "ShadowingVis: %s lookup of %s failed\n", \
					argv[1], argv[3]);
				return TCL_ERROR;
			} else {
				badProp = (Shadowing*) obj;
			}
			return(TCL_OK);
		}
	}
	
	return Propagation::command(argc, argv);
}


void ShadowingVis::loadImg(const char* fname)
{
#ifdef DEBUG
	printf("loading %s\n", fname);
#endif

	ifstream image( fname );

	char magicNumber[10];
	char comment[256];
	int whiteNum;
	char terminator;

	if( image.fail() ) printf("image bad!\n");

	if( strstr( fname, ".pnm" ) ) {
		image >> magicNumber;
		image.get( terminator );
		image.get( comment, 255, '\n');
		image.get( terminator );
		image >> width >> height >> whiteNum;
	} else if( strstr( fname, ".pgm" ) ) {
		image >> magicNumber;
		image.get( terminator );
		image >> width >> height >> whiteNum;
	}

#ifdef DEBUG
    printf("magic: %s\ncomment: %s\nwidth: %d height: %d white: %d\n", magicNumber, comment, width, height, whiteNum);
#endif

	pixel = new unsigned char[width * height];
	//unsigned char a;   #Sun OS compilation Fix - fcartegn@univ-valenciennes.fr - Apr 26 02
	char a;
	image.get(a);	// this byte is not the image

	// the upper-left corner is the origin (0, 0)
	for(int n = 0; n < height; n++) {
		for(int m = 0; m < width; m++) {
			image.get( a );
			pixel[m + n * width] = a;
		}
	}

#ifdef DEBUG
	FILE *testFile;
	char *filename = "imgtest.hex";
	testFile = fopen(filename, "w");
	for (int n = 0; n < height; n++) {
	  for (int m = 0; m < width; m++)
		if (pixel[m + n * height] == BLACK)
			fprintf(testFile, "%x", pixel[m + n * height]);
		else
			fprintf(testFile, "%x", 0xf);
		fprintf(testFile, "\n");
	}
	fclose(testFile);
#endif

	image.close();
}


int ShadowingVis::getPropCond(float xt, float yt, float xr, float yr)
{
#ifdef DEBUG
	printf("xt = %f yt = %f xr = %f yr = %f\n", xt, yt, xr, yr);
#endif
	float dx = xr - xt;
	float dy = yr - yt;
	float maxDistSq = dx * dx + dy * dy;
	float maxDist = sqrt(maxDistSq);
	dx /= maxDist;
	dy /= maxDist;
	maxDistSq = (float)(ppm * ppm) * maxDistSq;

	float xtPxl = (float)ppm * xt;
	float ytPxl = (float)ppm * yt;

	// Now each time there is only one pixel is test along the line from
	// the transmitter to the receiver. If ppm is large, more pixels 
	// can be tested: if the line of sight path is just obstructed by
	// a very small object, the propagation is still good
	int goodProp = 1;
	float distX = 0;
	float distY = 0;
	float distSq = 0;
	while (distSq < maxDistSq) {
		int xIdx = (int)(xtPxl + distX);
		int yIdx = (int)(ytPxl + distY);
		if (pixel[xIdx + yIdx * width] != BLACK) {
			goodProp = 0;
			break;
		}
		distX += dx;
		distY += dy;
		distSq = distX * distX + distY * distY;
	}
	
	return goodProp;
}


double ShadowingVis::getDist(double , double , double , double , double , double , double , double )
{
	return DBL_MAX;
}