00001
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "test.h"
00037 #include "configuration.h"
00038
00039
00040 #define ACCURACY 0.0000000001
00041 #define PERCENT_ERR 0.001 // i.e. one tenth of one percent (1m per km)
00042 #define MAX_SCALE 13
00043
00044
00045 int main(int argc, char *argv[])
00046 {
00047 int i, j, error = 0;
00048 int max_scale = MAX_SCALE;
00049 double lat, lon, x, y, new_lat, new_lon;
00050 Configuration conf;
00051
00052 if (argc > 1)
00053 max_scale = atoi(argv[1]);
00054
00055 printf("\n");
00056
00057 if ((error = conf.open("test.cfg")) < 0) {
00058 printf(" Failed to load test configuration.\n");
00059 goto end;
00060 }
00061 if ((error = conf.save("result.cfg")) < 0) {
00062 printf(" Failed to save test configuration.\n");
00063 goto end;
00064 }
00065
00066 printf(" WARNING: test-xml is incomplete\n");
00067
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00087
00088
00089
00091
00092
00093 lat = conf.get_origin().lat;
00094 lon = conf.get_origin().lon;
00095 for (i = 0; i <= 7; i++) {
00096 conf.get_origin().coords2meters(lat, lon, &x, &y);
00097 conf.get_origin().meters2coords(x, y, &new_lat, &new_lon);
00098
00099 double orig_dist =
00100 conf.get_origin().coord_distance(conf.get_origin().lat,
00101 conf.get_origin().lon,
00102 lat, lon);
00103 double meter_dist = sqrtl(powl(x, 2) + powl(y, 2));
00104 double percent_error = fabs(orig_dist - meter_dist) /
00105 orig_dist;
00106
00107 if (new_lat - lat > ACCURACY || new_lon - lon >ACCURACY) {
00108 error = -1;
00109 printf(" Conversion error: latitude %lf vs %lf,\n\t\t"
00110 "longitude %lf vs %lf\n", new_lat, lat,
00111 new_lon, lon);
00112 }
00113 if (percent_error > PERCENT_ERR) {
00114 printf(" Conversion error %lf %%.\n",
00115 percent_error * 100);
00116 if (orig_dist > 1000.0)
00117 printf(" (Rule of thumb: use coordinates [not "
00118 "meters] for \n\tdistances greater than "
00119 "a couple of kilometers.)\n");
00120 else
00121 error = -1;
00122 }
00123
00124
00125 if (i == 0) {
00126 lat = 35.08735;
00127 lon = -106.65055;
00128 } else if (i == 1) {
00129 lat = 35.08725;
00130 lon = -106.65065;
00131 } else if (i == 2) {
00132 lat = 35.08725;
00133 lon = -106.65055;
00134 } else if (i == 3) {
00135 lat = 35.08805;
00136 lon = -106.65135;
00137 } else if (i == 4) {
00138 lat = 35.08705;
00139 lon = -106.65185;
00140 } else if (i == 5) {
00141 lat = 35.08825;
00142 lon = -106.64965;
00143 } else if (i == 6) {
00144 lat = 35.86548;
00145 lon = -106.31967;
00146 }
00147 }
00148 if (error >= 0)
00149 printf(" Sucessfully converted coordinates to meters and back.\n");
00150
00151
00152 for (i = 2; i <= max_scale; i++) {
00153 int num_nodes = (int) pow(2.0, (double) i);
00154 FILE *file;
00155 Configuration conf2("WGS 84");
00156
00157 if ((file = fopen("test_tmp.cfg", "w")) == NULL) {
00158 perror("test_tmp.cfg");
00159 return -errno;
00160 }
00161
00162 fprintf(file, "<?xml version=\"1.0\"?>\n");
00163 fprintf(file, "<sensor_net>\n");
00164 fprintf(file, " <topology>\n");
00165 fprintf(file, " <name>Test %d</name>\n", i);
00166 fprintf(file, " <viewpoint>\n");
00167 fprintf(file, " <latitude>35.871334</latitude>\n");
00168 fprintf(file, " <longitude>-106.326067</longitude>\n");
00169 fprintf(file, " <range>500</range>\n");
00170 fprintf(file, " <tilt>0</tilt>\n");
00171 fprintf(file, " <heading>0</heading>\n");
00172 fprintf(file, " </viewpoint>\n");
00173 for (j = 0; j < num_nodes; j++) {
00174 fprintf(file, " <node id=\"%d\">\n", j);
00175 fprintf(file, " <random size=\"500\"/>\n");
00176 fprintf(file, " </node>\n");
00177 }
00178 fprintf(file, " </topology>\n");
00179 fprintf(file, " <network_data>\n");
00180 fprintf(file, " </network_data>\n");
00181 fprintf(file, "</sensor_net>\n");
00182 fclose(file);
00183
00184
00185
00186 if ((error = conf2.open("test_tmp.cfg")) < 0) {
00187 printf(" Failed to load %d-node config.\n",
00188 num_nodes);
00189 goto end;
00190 }
00191 }
00192 printf(" Sucessfully loaded up to a %d-node configuration.\n",
00193 (int) pow(2.0, (double) i - 1));
00194
00195 end:
00196 return error;
00197 }