topography.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) 1997 Regents of the University of California.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. All advertising materials mentioning features or use of this software
00015  *    must display the following acknowledgement:
00016  *  This product includes software developed by the Computer Systems
00017  *  Engineering Group at Lawrence Berkeley Laboratory.
00018  * 4. Neither the name of the University nor of the Laboratory may be used
00019  *    to endorse or promote products derived from this software without
00020  *    specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00023  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00025  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00026  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00028  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00029  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00031  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00032  * SUCH DAMAGE.
00033  */
00034 /* Ported from CMU/Monarch's code, nov'98 -Padma.*/
00035 
00036 #include <math.h>
00037 #include <stdlib.h>
00038 
00039 #include "object.h"
00040 
00041 #include "dem.h"
00042 #include "topography.h"
00043 
00044 
00045 static class TopographyClass : public TclClass {
00046 public:
00047         TopographyClass() : TclClass("Topography") {}
00048         TclObject* create(int, const char*const*) {
00049                 return (new Topography);
00050         }
00051 } class_topography;
00052 
00053 
00054 double
00055 Topography::height(double x, double y) {
00056 
00057     if(grid == 0)
00058         return 0.0;
00059 
00060 #ifndef WIN32
00061     int a = (int) rint(x/grid_resolution);
00062     int b = (int) rint(y/grid_resolution);
00063     int c = (int) rint(maxY);
00064 #else 
00065     int a = (int) ceil(x/grid_resolution+0.5);
00066     int b = (int) ceil(y/grid_resolution+0.5);
00067     int c = (int) ceil(maxY+0.5);
00068 #endif /* !WIN32 */
00069 
00070     return (double) grid[a * c + b];
00071 }
00072 
00073 
00074 int
00075 Topography::load_flatgrid(int x, int y, int res)
00076 {
00077     /* No Reason to malloc a grid */
00078 
00079     grid_resolution = res;  // default is 1 meter
00080     maxX = (double) x;
00081     maxY = (double) y;
00082 
00083     return 0;
00084 }
00085 
00086 
00087 int
00088 Topography::load_demfile(const char *fname)
00089 {
00090     DEMFile *dem;
00091 
00092     fprintf(stderr, "Opening DEM file %s...\n", fname);
00093 
00094     dem = new DEMFile((char*) fname); 
00095     if(dem == 0)
00096         return 1;
00097 
00098     fprintf(stderr, "Processing DEM file...\n");
00099 
00100     grid = dem->process();
00101     if(grid == 0) {
00102         delete dem;
00103         return 1;
00104     }
00105     dem->dump_ARecord();
00106 
00107     double tx, ty; tx = maxX; ty = maxY;
00108     dem->range(tx, ty);         // Get the grid size
00109     maxX = tx; maxY = ty;
00110 
00111     dem->resolution(grid_resolution);   // Get the resolution of each entry
00112 
00113     /* Close the DEM file */
00114     delete dem;
00115 
00116     /*
00117      * Sanity Check
00118      */
00119     if(maxX <= 0.0 || maxY <= 0.0 || grid_resolution <= 0.0)
00120         return 1;
00121 
00122     fprintf(stderr, "DEM File processing complete...\n");
00123 
00124     return 0;
00125 }
00126 
00127 
00128 void 
00129 Topography::updateNodesList(class MobileNode* mn, double oldX)
00130 {
00131     channel_->updateNodesList(mn, oldX);
00132 }
00133 
00134 
00135 int
00136 Topography::command(int argc, const char*const* argv)
00137 {
00138     if(argc == 3) {
00139 
00140         if(strcmp(argv[1], "load_demfile") == 0) {
00141             if(load_demfile(argv[2]))
00142                 return TCL_ERROR;
00143             return TCL_OK;
00144         } else if (strcmp(argv[1], "channel") == 0) {
00145             WirelessChannel *chan;
00146             chan = (WirelessChannel *)TclObject::lookup(argv[2]);
00147             if (chan == 0) {
00148                 fprintf(stderr, "%s lookup of channel failed\n", argv[1]);
00149                 return TCL_ERROR;
00150             } else {
00151                 channel_ = chan;
00152                 return TCL_OK;
00153             }
00154         }
00155     }
00156     else if(argc == 4) {
00157         if(strcmp(argv[1], "load_flatgrid") == 0) {
00158             if(load_flatgrid(atoi(argv[2]), atoi(argv[3])))
00159                 return TCL_ERROR;
00160             return TCL_OK;
00161         }
00162     }
00163     else if(argc == 5) {
00164         if(strcmp(argv[1], "load_flatgrid") == 0) {
00165             if(load_flatgrid(atoi(argv[2]), atoi(argv[3]), atoi(argv[4])))
00166                 return TCL_ERROR;
00167             return TCL_OK;
00168         }
00169     }
00170     return TclObject::command(argc, argv);
00171 }

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