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 #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
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
00078
00079 grid_resolution = res;
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);
00109 maxX = tx; maxY = ty;
00110
00111 dem->resolution(grid_resolution);
00112
00113
00114 delete dem;
00115
00116
00117
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 }