Vlib/box.c

Go to the documentation of this file.
00001 /*
00002 ****************************************************************************
00003 *
00004 * MODULE:       Vector library 
00005 *               
00006 * AUTHOR(S):    Radim Blazek
00007 *
00008 * PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
00009 *
00010 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00011 *
00012 *               This program is free software under the GNU General Public
00013 *               License (>=v2). Read the file COPYING that comes with GRASS
00014 *               for details.
00015 *
00016 *****************************************************************************/
00017 #include <stdlib.h>
00018 #include <grass/gis.h>
00019 #include <grass/Vect.h>
00020 
00027 int 
00028 Vect_point_in_box (double x, double y, double z, BOUND_BOX *Box)
00029 {
00030   
00031     if ( x >= Box->W && x <= Box->E &&
00032          y >= Box->S && y <= Box->N &&
00033          z >= Box->B && z <= Box->T )
00034     {
00035         return 1;
00036     }
00037 
00038     return 0;
00039 }
00040 
00047 int 
00048 Vect_box_overlap (BOUND_BOX *A, BOUND_BOX *B)
00049 {
00050 
00051     if ( A->E < B->W || A->W > B->E ||
00052          A->N < B->S || A->S > B->N ||
00053          A->T < B->B || A->B > B->T )
00054     {
00055         return 0;
00056     }
00057 
00058     return 1;
00059 }
00060 
00067 int 
00068 Vect_box_copy (BOUND_BOX *A, BOUND_BOX *B)
00069 {
00070 
00071     A->N = B->N;
00072     A->S = B->S;
00073     A->E = B->E;
00074     A->W = B->W;
00075     A->T = B->T;
00076     A->B = B->B;
00077 
00078     return 1;
00079 }
00080 
00087 int 
00088 Vect_box_extend (BOUND_BOX *A, BOUND_BOX *B)
00089 {
00090 
00091     if ( B->N > A->N ) A->N = B->N;
00092     if ( B->S < A->S ) A->S = B->S;
00093     if ( B->E > A->E ) A->E = B->E;
00094     if ( B->W < A->W ) A->W = B->W;
00095     if ( B->T > A->T ) A->T = B->T;
00096     if ( B->B < A->B ) A->B = B->B;
00097 
00098     return 1;
00099 }
00100 
00101 
00127 int
00128 Vect_box_clip (double *x, double *y, double *c_x, double *c_y, BOUND_BOX *Box)
00129 {
00130         int mod ;
00131 
00132         mod = 0 ;
00133 
00134         if (*x < Box->W)
00135         {
00136                 if (*c_x != *x)
00137                         *y = *y + (Box->W - *x)/(*c_x - *x) * (*c_y - *y) ;
00138                 *x = Box->W ;
00139                 mod = 1 ;
00140         }
00141         if (*x > Box->E)
00142         {
00143                 if (*c_x != *x)
00144                         *y = *y + (Box->E - *x)/(*c_x - *x) * (*c_y - *y) ;
00145                 *x = Box->E ;
00146                 mod = 1 ;
00147         }
00148         if (*c_x < Box->W)
00149         {
00150                 if (*c_x != *x)
00151                         *c_y = *c_y + (Box->W - *c_x)/(*x - *c_x) * (*y - *c_y) ;
00152                 *c_x = Box->W ;
00153                 mod = 1 ;
00154         }
00155         if (*c_x > Box->E)
00156         {
00157                 if (*c_x != *x)
00158                         *c_y = *c_y + (Box->E - *c_x)/(*x - *c_x) * (*y - *c_y) ;
00159                 *c_x = Box->E ;
00160                 mod = 1 ;
00161         }
00162         if (*y < Box->S)
00163         {
00164                 if (*c_y != *y)
00165                         *x = *x + (Box->S - *y)/(*c_y - *y) * (*c_x - *x) ;
00166                 *y = Box->S ;
00167                 mod = 1 ;
00168         }
00169         if (*y > Box->N)
00170         {
00171                 if (*c_y != *y)
00172                         *x = *x + (Box->N - *y)/(*c_y - *y) * (*c_x - *x) ;
00173                 *y = Box->N ;
00174                 mod = 1 ;
00175         }
00176         if (*c_y < Box->S)
00177         {
00178                 if (*c_y != *y)
00179                         *c_x = *c_x + (Box->S - *c_y)/(*y - *c_y) * (*x - *c_x) ;
00180                 *c_y = Box->S ;
00181                 mod = 1 ;
00182         }
00183         if (*c_y > Box->N)
00184         {
00185                 if (*c_y != *y)
00186                         *c_x = *c_x + (Box->N - *c_y)/(*y - *c_y) * (*x - *c_x) ;
00187                 *c_y = Box->N ;
00188                 mod = 1 ;
00189         }
00190 
00191         return (mod) ;
00192 }
00193 
00194 
00195 
00202 int 
00203 Vect_get_line_box (struct Map_info *Map, int line, BOUND_BOX *Box )
00204 {
00205     struct    Plus_head *Plus;
00206     P_LINE    *Line;
00207     
00208     Plus = &(Map->plus);
00209     Line = Plus->Line[line];
00210     
00211     if ( Line == NULL ) {  /* dead */
00212         Box->N = 0;
00213         Box->S = 0;
00214         Box->E = 0;
00215         Box->W = 0;
00216         Box->T = 0;
00217         Box->B = 0;
00218         return 0;
00219     } else {
00220         Box->N = Line->N;
00221         Box->S = Line->S;
00222         Box->E = Line->E;
00223         Box->W = Line->W;
00224         Box->T = Line->T;
00225         Box->B = Line->B;
00226     }
00227 
00228     return 1;
00229 }
00230 
00237 int 
00238 Vect_get_area_box (struct Map_info *Map, int area, BOUND_BOX *Box )
00239 {
00240     struct    Plus_head *Plus;
00241     P_AREA    *Area;
00242     
00243     Plus = &(Map->plus);
00244     Area = Plus->Area[area];
00245     
00246     if ( Area == NULL ) {  /* dead */
00247         Box->N = 0;
00248         Box->S = 0;
00249         Box->E = 0;
00250         Box->W = 0;
00251         Box->T = 0;
00252         Box->B = 0;
00253         return 0;
00254     } else {
00255         Box->N = Area->N;
00256         Box->S = Area->S;
00257         Box->E = Area->E;
00258         Box->W = Area->W;
00259         Box->T = Area->T;
00260         Box->B = Area->B;
00261     }
00262 
00263     return 1;
00264 }
00265 
00272 int 
00273 Vect_get_isle_box (struct Map_info *Map, int isle, BOUND_BOX *Box )
00274 {
00275     struct    Plus_head *Plus;
00276     P_ISLE    *Isle;
00277     
00278     Plus = &(Map->plus);
00279     Isle = Plus->Isle[isle];
00280     
00281     if ( Isle == NULL ) {  /* dead */
00282         Box->N = 0;
00283         Box->S = 0;
00284         Box->E = 0;
00285         Box->W = 0;
00286         Box->T = 0;
00287         Box->B = 0;
00288         return 0;
00289     } else {
00290         Box->N = Isle->N;
00291         Box->S = Isle->S;
00292         Box->E = Isle->E;
00293         Box->W = Isle->W;
00294         Box->T = Isle->T;
00295         Box->B = Isle->B;
00296     }
00297 
00298     return 1;
00299 }
00300 
00307 int 
00308 Vect_get_map_box (struct Map_info *Map, BOUND_BOX *Box )
00309 {
00310     struct    Plus_head *Plus;
00311     
00312     Plus = &(Map->plus);
00313     
00314     Box->N = Plus->box.N;
00315     Box->S = Plus->box.S;
00316     Box->E = Plus->box.E;
00317     Box->W = Plus->box.W;
00318     Box->T = Plus->box.T;
00319     Box->B = Plus->box.B;
00320 
00321     return 1;
00322 }
00323 
00324 
00331 int 
00332 Vect_region_box ( struct Cell_head *Window, BOUND_BOX *Box )
00333 {
00334     
00335     Box->N = Window->north;
00336     Box->S = Window->south;
00337     Box->E = Window->east;
00338     Box->W = Window->west;
00339     Box->T = PORT_DOUBLE_MAX;
00340     Box->B = -PORT_DOUBLE_MAX;
00341 
00342     return 1;
00343 }
00344 

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5