Vlib/box.c

Go to the documentation of this file.
00001 
00020 #include <stdlib.h>
00021 #include <grass/gis.h>
00022 #include <grass/Vect.h>
00023 
00033 int Vect_point_in_box(double x, double y, double z, BOUND_BOX * Box)
00034 {
00035 
00036     if (x >= Box->W && x <= Box->E &&
00037         y >= Box->S && y <= Box->N && z >= Box->B && z <= Box->T) {
00038         return 1;
00039     }
00040 
00041     return 0;
00042 }
00043 
00053 int Vect_box_overlap(BOUND_BOX * A, BOUND_BOX * B)
00054 {
00055 
00056     if (A->E < B->W || A->W > B->E ||
00057         A->N < B->S || A->S > B->N || A->T < B->B || A->B > B->T) {
00058         return 0;
00059     }
00060 
00061     return 1;
00062 }
00063 
00072 int Vect_box_copy(BOUND_BOX * A, BOUND_BOX * B)
00073 {
00074 
00075     A->N = B->N;
00076     A->S = B->S;
00077     A->E = B->E;
00078     A->W = B->W;
00079     A->T = B->T;
00080     A->B = B->B;
00081 
00082     return 1;
00083 }
00084 
00093 int Vect_box_extend(BOUND_BOX * A, BOUND_BOX * B)
00094 {
00095 
00096     if (B->N > A->N)
00097         A->N = B->N;
00098     if (B->S < A->S)
00099         A->S = B->S;
00100     if (B->E > A->E)
00101         A->E = B->E;
00102     if (B->W < A->W)
00103         A->W = B->W;
00104     if (B->T > A->T)
00105         A->T = B->T;
00106     if (B->B < A->B)
00107         A->B = B->B;
00108 
00109     return 1;
00110 }
00111 
00112 
00137 int
00138 Vect_box_clip(double *x, double *y, double *c_x, double *c_y, BOUND_BOX * Box)
00139 {
00140     int mod;
00141 
00142     mod = 0;
00143 
00144     if (*x < Box->W) {
00145         if (*c_x != *x)
00146             *y = *y + (Box->W - *x) / (*c_x - *x) * (*c_y - *y);
00147         *x = Box->W;
00148         mod = 1;
00149     }
00150     if (*x > Box->E) {
00151         if (*c_x != *x)
00152             *y = *y + (Box->E - *x) / (*c_x - *x) * (*c_y - *y);
00153         *x = Box->E;
00154         mod = 1;
00155     }
00156     if (*c_x < Box->W) {
00157         if (*c_x != *x)
00158             *c_y = *c_y + (Box->W - *c_x) / (*x - *c_x) * (*y - *c_y);
00159         *c_x = Box->W;
00160         mod = 1;
00161     }
00162     if (*c_x > Box->E) {
00163         if (*c_x != *x)
00164             *c_y = *c_y + (Box->E - *c_x) / (*x - *c_x) * (*y - *c_y);
00165         *c_x = Box->E;
00166         mod = 1;
00167     }
00168     if (*y < Box->S) {
00169         if (*c_y != *y)
00170             *x = *x + (Box->S - *y) / (*c_y - *y) * (*c_x - *x);
00171         *y = Box->S;
00172         mod = 1;
00173     }
00174     if (*y > Box->N) {
00175         if (*c_y != *y)
00176             *x = *x + (Box->N - *y) / (*c_y - *y) * (*c_x - *x);
00177         *y = Box->N;
00178         mod = 1;
00179     }
00180     if (*c_y < Box->S) {
00181         if (*c_y != *y)
00182             *c_x = *c_x + (Box->S - *c_y) / (*y - *c_y) * (*x - *c_x);
00183         *c_y = Box->S;
00184         mod = 1;
00185     }
00186     if (*c_y > Box->N) {
00187         if (*c_y != *y)
00188             *c_x = *c_x + (Box->N - *c_y) / (*y - *c_y) * (*x - *c_x);
00189         *c_y = Box->N;
00190         mod = 1;
00191     }
00192 
00193     return (mod);
00194 }
00195 
00196 
00197 
00208 int Vect_get_line_box(struct Map_info *Map, int line, BOUND_BOX * Box)
00209 {
00210     struct Plus_head *Plus;
00211     P_LINE *Line;
00212 
00213     Plus = &(Map->plus);
00214     Line = Plus->Line[line];
00215 
00216     if (Line == NULL) {         /* dead */
00217         Box->N = 0;
00218         Box->S = 0;
00219         Box->E = 0;
00220         Box->W = 0;
00221         Box->T = 0;
00222         Box->B = 0;
00223         return 0;
00224     }
00225     else {
00226         Box->N = Line->N;
00227         Box->S = Line->S;
00228         Box->E = Line->E;
00229         Box->W = Line->W;
00230         Box->T = Line->T;
00231         Box->B = Line->B;
00232     }
00233 
00234     return 1;
00235 }
00236 
00247 int Vect_get_area_box(struct Map_info *Map, int area, BOUND_BOX * Box)
00248 {
00249     struct Plus_head *Plus;
00250     P_AREA *Area;
00251 
00252     Plus = &(Map->plus);
00253     Area = Plus->Area[area];
00254 
00255     if (Area == NULL) {         /* dead */
00256         Box->N = 0;
00257         Box->S = 0;
00258         Box->E = 0;
00259         Box->W = 0;
00260         Box->T = 0;
00261         Box->B = 0;
00262         return 0;
00263     }
00264     else {
00265         Box->N = Area->N;
00266         Box->S = Area->S;
00267         Box->E = Area->E;
00268         Box->W = Area->W;
00269         Box->T = Area->T;
00270         Box->B = Area->B;
00271     }
00272 
00273     return 1;
00274 }
00275 
00286 int Vect_get_isle_box(struct Map_info *Map, int isle, BOUND_BOX * Box)
00287 {
00288     struct Plus_head *Plus;
00289     P_ISLE *Isle;
00290 
00291     Plus = &(Map->plus);
00292     Isle = Plus->Isle[isle];
00293 
00294     if (Isle == NULL) {         /* dead */
00295         Box->N = 0;
00296         Box->S = 0;
00297         Box->E = 0;
00298         Box->W = 0;
00299         Box->T = 0;
00300         Box->B = 0;
00301         return 0;
00302     }
00303     else {
00304         Box->N = Isle->N;
00305         Box->S = Isle->S;
00306         Box->E = Isle->E;
00307         Box->W = Isle->W;
00308         Box->T = Isle->T;
00309         Box->B = Isle->B;
00310     }
00311 
00312     return 1;
00313 }
00314 
00323 int Vect_get_map_box(struct Map_info *Map, BOUND_BOX * Box)
00324 {
00325     struct Plus_head *Plus;
00326 
00327     Plus = &(Map->plus);
00328 
00329     Box->N = Plus->box.N;
00330     Box->S = Plus->box.S;
00331     Box->E = Plus->box.E;
00332     Box->W = Plus->box.W;
00333     Box->T = Plus->box.T;
00334     Box->B = Plus->box.B;
00335 
00336     return 1;
00337 }
00338 
00339 
00348 int Vect_region_box(struct Cell_head *Window, BOUND_BOX * Box)
00349 {
00350 
00351     Box->N = Window->north;
00352     Box->S = Window->south;
00353     Box->E = Window->east;
00354     Box->W = Window->west;
00355     Box->T = PORT_DOUBLE_MAX;
00356     Box->B = -PORT_DOUBLE_MAX;
00357 
00358     return 1;
00359 }
Generated on Tue Apr 6 13:28:09 2010 for GRASS Programmer's Manual by  doxygen 1.6.3