plus.c

Go to the documentation of this file.
00001 /*
00002 * $Id: plus.c,v 1.15 2006/02/09 03:08:58 glynn Exp $
00003 *
00004 ****************************************************************************
00005 *
00006 * MODULE:       Vector library 
00007 *               
00008 * AUTHOR(S):    Original author CERL, probably Dave Gerdes.
00009 *               Update to GRASS 5.7 Radim Blazek.
00010 *
00011 * PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
00012 *
00013 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00014 *
00015 *               This program is free software under the GNU General Public
00016 *               License (>=v2). Read the file COPYING that comes with GRASS
00017 *               for details.
00018 *
00019 *****************************************************************************/
00020 #include <unistd.h>
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <grass/gis.h>
00024 #include <grass/Vect.h>
00025 
00026 /* Init head structure */
00027 int 
00028 dig_init_plus (struct Plus_head *Plus)
00029 {
00030     G_debug (3, "dig_init_plus()" );
00031     
00032     Plus->Version_Major = 0 ;
00033     Plus->Version_Minor = 0 ;
00034     Plus->Back_Major = 0 ;
00035     Plus->Back_Minor = 0 ;
00036     Plus->with_z = 0 ;
00037     
00038     Plus->box.N = 0;
00039     Plus->box.S = 0;
00040     Plus->box.E = 0;
00041     Plus->box.W = 0;
00042     Plus->box.T = 0;
00043     Plus->box.B = 0;
00044 
00045     Plus->built = GV_BUILD_NONE;
00046     
00047     Plus->Node = NULL ;
00048     Plus->Line = NULL ;
00049     Plus->Area = NULL ;
00050     Plus->Isle = NULL ;
00051     
00052     Plus->n_nodes = 0 ;
00053     Plus->n_edges = 0 ;
00054     Plus->n_lines = 0 ;
00055     Plus->n_areas = 0 ;
00056     Plus->n_isles = 0 ;
00057     Plus->n_volumes = 0 ;
00058     Plus->n_holes = 0 ;
00059 
00060     Plus->alloc_nodes = 0 ;
00061     Plus->alloc_edges = 0 ;
00062     Plus->alloc_lines = 0 ;
00063     Plus->alloc_areas = 0 ;
00064     Plus->alloc_isles = 0 ;
00065     Plus->alloc_volumes = 0 ;
00066     Plus->alloc_holes = 0 ;
00067 
00068     Plus->n_plines = 0 ;
00069     Plus->n_llines = 0 ;
00070     Plus->n_blines = 0 ;
00071     Plus->n_clines = 0 ;
00072     Plus->n_flines = 0 ;
00073     Plus->n_klines = 0 ;
00074 
00075     Plus->Node_offset = 0L ;
00076     Plus->Edge_offset = 0L ;
00077     Plus->Line_offset = 0L ;
00078     Plus->Area_offset = 0L ;
00079     Plus->Isle_offset = 0L ;
00080     Plus->Volume_offset = 0L ;
00081     Plus->Hole_offset = 0L ;
00082 
00083     Plus->Node_spidx_offset = 0L ;
00084     Plus->Edge_spidx_offset = 0L ;
00085     Plus->Line_spidx_offset = 0L ;
00086     Plus->Area_spidx_offset = 0L ;
00087     Plus->Isle_spidx_offset = 0L ;
00088     Plus->Volume_spidx_offset = 0L ;
00089     Plus->Hole_spidx_offset = 0L ;
00090     
00091     dig_spidx_init ( Plus );
00092     dig_cidx_init ( Plus );
00093     
00094     return 1;
00095 }
00096 
00097 void 
00098 dig_free_plus_nodes (struct Plus_head *Plus)
00099 {
00100     int i;    
00101     P_NODE *Node;
00102     
00103     G_debug(2, "dig_free_plus_nodes()");
00104 
00105     /* Nodes */
00106     if ( Plus->Node ) { /* it may be that header only is loaded */
00107         for (i = 1; i <= Plus->n_nodes; i++) {
00108             Node = Plus->Node[i];       
00109             if ( Node == NULL ) continue;
00110             
00111             if ( Node->alloc_lines > 0 ) {
00112                 free ( Node->lines);
00113                 free ( Node->angles);
00114             }
00115             free (Node);
00116         }
00117         free ( Plus->Node );
00118     }
00119     Plus->Node = NULL ;
00120     Plus->n_nodes = 0 ;
00121     Plus->alloc_nodes = 0 ;
00122 }
00123 
00124 void 
00125 dig_free_plus_lines (struct Plus_head *Plus)
00126 {
00127     int i;    
00128     P_LINE *Line;
00129 
00130     G_debug(2, "dig_free_plus_lines()");
00131 
00132     /* Lines */
00133     if ( Plus->Line ) { /* it may be that header only is loaded */
00134         for (i = 1; i <= Plus->n_lines; i++) {
00135             Line = Plus->Line[i];       
00136             if ( Line == NULL ) continue;
00137             
00138             free (Line);
00139         }
00140         free ( Plus->Line );
00141     }
00142 
00143     Plus->Line = NULL ;
00144     Plus->n_lines = 0 ;
00145     Plus->alloc_lines = 0 ;
00146     
00147     Plus->n_plines = 0 ;
00148     Plus->n_llines = 0 ;
00149     Plus->n_blines = 0 ;
00150     Plus->n_clines = 0 ;
00151     Plus->n_flines = 0 ;
00152     Plus->n_klines = 0 ;
00153 }
00154 
00155 void 
00156 dig_free_plus_areas (struct Plus_head *Plus)
00157 {
00158     int i;    
00159     P_AREA *Area;
00160 
00161     G_debug(2, "dig_free_plus_areas()");
00162     
00163     /* Areas */
00164     if ( Plus->Area ) { /* it may be that header only is loaded */
00165         for (i = 1; i <= Plus->n_areas; i++) {
00166             Area = Plus->Area[i];       
00167             if ( Area == NULL ) continue;
00168             
00169             if ( Area->alloc_lines > 0 ) 
00170                 free ( Area->lines);
00171 
00172             if ( Area->alloc_isles > 0 ) 
00173                 free ( Area->isles);
00174             
00175             free (Area);
00176         }
00177         free ( Plus->Area );
00178     }
00179     Plus->Area = NULL ;
00180     Plus->n_areas = 0 ;
00181     Plus->alloc_areas = 0 ;
00182 }
00183 
00184 void 
00185 dig_free_plus_isles (struct Plus_head *Plus)
00186 {
00187     int i;    
00188     P_ISLE *Isle;
00189 
00190     G_debug(2, "dig_free_plus_isles()");
00191 
00192     /* Isles */
00193     if ( Plus->Isle ) { /* it may be that header only is loaded */
00194         for (i = 1; i <= Plus->n_isles; i++) {
00195             Isle = Plus->Isle[i];       
00196             if ( Isle == NULL ) continue;
00197             
00198             if ( Isle->alloc_lines > 0 ) 
00199                 free ( Isle->lines);
00200 
00201             free (Isle);
00202         }
00203         free ( Plus->Isle );
00204     }
00205 
00206     Plus->Isle = NULL ;
00207     Plus->n_isles = 0 ;
00208     Plus->alloc_isles = 0 ;
00209 }
00210 
00211 /* Free head structure. 
00212 *  Structure is not inited and dig_init_plus() should follow.
00213 */
00214 void 
00215 dig_free_plus (struct Plus_head *Plus)
00216 {
00217     G_debug(2, "dig_free_plus()");
00218     dig_free_plus_nodes (Plus);
00219     dig_free_plus_lines (Plus);
00220     dig_free_plus_areas (Plus);
00221     dig_free_plus_isles (Plus);
00222 
00223     dig_cidx_free(Plus);
00224 }
00225 
00226 /* dig_load_plus reads topo file to topo structure
00227 *  
00228 *  Returns : 1 success
00229 *            0 error
00230 */
00231 int 
00232 dig_load_plus ( struct Plus_head *Plus, GVFILE * plus, int head_only)
00233 {
00234   int i;
00235 
00236 
00237   G_debug (1, "dig_load_plus()"); 
00238   /* TODO
00239   if (do_checks)
00240     dig_do_file_checks (map, map->plus_file, map->digit_file);
00241   */
00242  
00243   /* free and init old */ 
00244   dig_init_plus ( Plus );
00245   
00246   /* Now let's begin reading the Plus file nodes, lines, areas and isles */
00247   
00248   if ( dig_Rd_Plus_head (plus, Plus) == -1 ) return 0;
00249 
00250   if ( head_only ) return 1;
00251   
00252   dig_set_cur_port ( &(Plus->port) ); 
00253 
00254   /* Nodes */
00255   if ( dig_fseek (plus, Plus->Node_offset, 0) == -1 )
00256     G_fatal_error ("Cannot read topo for nodes" );
00257     
00258   dig_alloc_nodes ( Plus, Plus->n_nodes );
00259   for (i = 1; i <= Plus->n_nodes; i++)
00260     {
00261       if ( dig_Rd_P_node ( Plus, i, plus) == -1 )
00262           G_fatal_error ("Cannot read topo for node %d", i);
00263     }
00264   
00265   /* Lines */
00266   if ( dig_fseek (plus, Plus->Line_offset, 0) == -1 )
00267     G_fatal_error ("Cannot read topo for lines" );
00268 
00269   dig_alloc_lines ( Plus, Plus->n_lines );
00270   for (i = 1; i <= Plus->n_lines; i++)
00271     {
00272       if ( dig_Rd_P_line ( Plus, i, plus) == -1 )
00273           G_fatal_error ("Cannot read topo for line %d", i);
00274     }
00275   
00276   /* Areas */
00277   if ( dig_fseek (plus, Plus->Area_offset, 0) == -1 )
00278     G_fatal_error ("Cannot read topo for areas" );
00279 
00280   dig_alloc_areas ( Plus, Plus->n_areas );
00281   for (i = 1; i <= Plus->n_areas; i++)
00282     {
00283       if ( dig_Rd_P_area ( Plus, i, plus) == -1 ) 
00284           G_fatal_error ("Cannot read topo for area %d", i);
00285     }
00286   
00287   /* Isles */
00288   if ( dig_fseek (plus, Plus->Isle_offset, 0) == -1 )
00289       G_fatal_error ("Cannot read topo for isles" );
00290   
00291   dig_alloc_isles ( Plus, Plus->n_isles );
00292   for (i = 1; i <= Plus->n_isles; i++)
00293     {
00294       if ( dig_Rd_P_isle ( Plus, i, plus) == -1 )
00295           G_fatal_error ("Cannot read topo for isle %d", i);
00296     }
00297 
00298   return (1);
00299 }
00300 
00301 int 
00302 dig_write_plus_file ( GVFILE * fp_plus,
00303                       struct Plus_head *Plus)
00304 {
00305 
00306   dig_set_cur_port(&(Plus->port));  
00307   dig_rewind (fp_plus);
00308 
00309   if (dig_Wr_Plus_head (fp_plus, Plus) < 0)
00310     {
00311       fprintf (stderr, "\nERROR: Can't write head to plus file.\n");
00312       return (-1);
00313     }
00314 
00315   if (dig_write_nodes (fp_plus, Plus) < 0)
00316     {
00317       fprintf (stderr, "\nERROR: Can't write nodes to plus file.\n");
00318       return (-1);
00319     }
00320 
00321   if (dig_write_lines (fp_plus, Plus) < 0)
00322     {
00323       fprintf (stderr, "\nERROR: Can't write lines to plus file.\n");
00324       return (-1);
00325     }
00326 
00327   if (dig_write_areas (fp_plus, Plus) < 0)
00328     {
00329       fprintf (stderr, "\nERROR: Can't write areas to plus file.\n");
00330       return (-1);
00331     }
00332 
00333   if (dig_write_isles (fp_plus, Plus) < 0)
00334     {
00335       fprintf (stderr, "\nERROR: Can't write isles to plus file.\n");
00336       return (-1);
00337     }
00338 
00339   dig_rewind (fp_plus);
00340   if (dig_Wr_Plus_head (fp_plus, Plus) < 0)
00341     {
00342       fprintf (stderr, "\nERROR: Can't write head to plus file.\n");
00343       return (-1);
00344     }
00345 
00346   dig_fflush (fp_plus);
00347   return (0);
00348 }                               /*  write_plus_file()  */
00349 
00350 
00351 int 
00352 dig_write_nodes ( GVFILE * plus,
00353                   struct Plus_head *Plus)
00354 {
00355   int i;
00356 
00357 
00358   Plus->Node_offset = dig_ftell (plus);
00359 
00360   for (i = 1; i <= Plus->n_nodes; i++)
00361     {
00362       if (dig_Wr_P_node (Plus, i, plus) < 0)
00363         return (-1);
00364     }
00365 
00366   return (0);
00367 }                               /*  write_nodes()  */
00368 
00369 
00370 int 
00371 dig_write_lines ( GVFILE * plus,
00372                   struct Plus_head *Plus)
00373 {
00374   int i;
00375 
00376 
00377   Plus->Line_offset = dig_ftell (plus);
00378 
00379   for (i = 1; i <= Plus->n_lines; i++)
00380     {
00381       if (dig_Wr_P_line (Plus, i, plus) < 0)
00382         return (-1);
00383     }
00384 
00385   return (0);
00386 
00387 }                               /*  write_line()  */
00388 
00389 int 
00390 dig_write_areas ( GVFILE * plus,
00391                   struct Plus_head *Plus)
00392 {
00393   int i;
00394 
00395 
00396   Plus->Area_offset = dig_ftell (plus);
00397 
00398   for (i = 1; i <= Plus->n_areas; i++)
00399     {
00400       if (dig_Wr_P_area (Plus, i, plus) < 0)
00401         return (-1);
00402     }
00403 
00404   return (0);
00405 
00406 }                               /*  write_areas()  */
00407 
00408 
00409 int 
00410 dig_write_isles ( GVFILE * plus,
00411                   struct Plus_head *Plus)
00412 {
00413   int i;
00414 
00415 
00416   Plus->Isle_offset = dig_ftell (plus);
00417 
00418   for (i = 1; i <= Plus->n_isles; i++)
00419     {
00420       if (dig_Wr_P_isle (Plus, i, plus) < 0)
00421         return (-1);
00422     }
00423 
00424   return (0);
00425 
00426 }                               /*  write_isles()  */

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