GRASS Programmer's Manual  6.4.2(2012)
gv.c
Go to the documentation of this file.
00001 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 
00022 #include <grass/gstypes.h>
00023 #include "gsget.h"
00024 
00025 #define FIRST_VECT_ID 20656
00026 
00027 static geovect *Vect_top = NULL;
00028 
00037 geovect *gv_get_vect(int id)
00038 {
00039     geovect *gv;
00040 
00041     G_debug(5, "gv_get_vect() id=%d", id);
00042 
00043     for (gv = Vect_top; gv; gv = gv->next) {
00044         if (gv->gvect_id == id) {
00045             return (gv);
00046         }
00047     }
00048 
00049     return (NULL);
00050 }
00051 
00060 geovect *gv_get_prev_vect(int id)
00061 {
00062     geovect *pv;
00063 
00064     G_debug(5, "gv_get_prev_vect(): id=%d", id);
00065 
00066     for (pv = Vect_top; pv; pv = pv->next) {
00067         if (pv->gvect_id == id - 1) {
00068             return (pv);
00069         }
00070     }
00071 
00072     return (NULL);
00073 }
00074 
00080 int gv_num_vects(void)
00081 {
00082     geovect *gv;
00083     int i;
00084 
00085     for (i = 0, gv = Vect_top; gv; gv = gv->next, i++) ;
00086 
00087     G_debug(5, "gv_num_vects(): num=%d", i);
00088 
00089     return (i);
00090 }
00091 
00098 geovect *gv_get_last_vect(void)
00099 {
00100     geovect *lv;
00101 
00102     if (!Vect_top) {
00103         return (NULL);
00104     }
00105 
00106     for (lv = Vect_top; lv->next; lv = lv->next) ;
00107 
00108     G_debug(5, "gv_get_last_vect(): id=%d", lv->gvect_id);
00109 
00110     return (lv);
00111 }
00112 
00119 geovect *gv_get_new_vect(void)
00120 {
00121     geovect *nv, *lv;
00122 
00123     nv = (geovect *) G_malloc(sizeof(geovect));
00124     if (!nv) {
00125         /* G_fatal_error */
00126         return (NULL);
00127     }
00128 
00129     if ((lv = gv_get_last_vect())) {
00130         lv->next = nv;
00131         nv->gvect_id = lv->gvect_id + 1;
00132     }
00133     else {
00134         Vect_top = nv;
00135         nv->gvect_id = FIRST_VECT_ID;
00136     }
00137 
00138     nv->next = NULL;
00139 
00140     G_debug(5, "gv_get_new_vect() id=%d", nv->gvect_id);
00141 
00142     return (nv);
00143 }
00144 
00150 void gv_update_drapesurfs(void)
00151 {
00152     geovect *gv;
00153     int i, j;
00154 
00155     for (gv = Vect_top; gv; gv = gv->next) {
00156         if (gv->n_surfs) {
00157             for (i = 0; i < gv->n_surfs; i++) {
00158                 if (gv->drape_surf_id[i]) {
00159                     if (NULL == gs_get_surf(gv->drape_surf_id[i])) {
00160                         for (j = i; j < gv->n_surfs - 1; j++) {
00161                             gv->drape_surf_id[j] = gv->drape_surf_id[j + 1];
00162                         }
00163 
00164                         gv->n_surfs = gv->n_surfs - 1;
00165                     }
00166                 }
00167             }
00168         }
00169     }
00170 }
00171 
00180 int gv_set_defaults(geovect * gv)
00181 {
00182     int i;
00183 
00184     if (!gv) {
00185         return (-1);
00186     }
00187     
00188     G_debug(5, "gv_set_defaults() id=%d", gv->gvect_id);
00189 
00190     gv->filename = NULL;
00191     gv->n_lines = gv->n_surfs = gv->use_mem = 0;
00192     gv->x_trans = gv->y_trans = gv->z_trans = 0.0;
00193     gv->lines = NULL;
00194     gv->fastlines = NULL;
00195     gv->width = 1;
00196     gv->color = 0xFFFFFF;
00197     gv->flat_val = 0;
00198 
00199     for (i = 0; i < MAX_SURFS; i++) {
00200         gv->drape_surf_id[i] = 0;
00201     }
00202 
00203     return (0);
00204 }
00205 
00214 int gv_init_vect(geovect * gv)
00215 {
00216     if (!gv) {
00217         return (-1);
00218     }
00219 
00220     G_debug(5, "gv_init_vect() id=%d", gv->gvect_id);
00221 
00222     return (0);
00223 }
00224 
00230 void gv_delete_vect(int id)
00231 {
00232     geovect *fv;
00233 
00234     G_debug(5, "gv_delete_vect(): id=%d", id);
00235 
00236     fv = gv_get_vect(id);
00237 
00238     if (fv) {
00239         gv_free_vect(fv);
00240     }
00241 
00242     return;
00243 }
00244 
00253 int gv_free_vect(geovect * fv)
00254 {
00255     geovect *gv;
00256     int found = 0;
00257 
00258     if (Vect_top) {
00259         if (fv == Vect_top) {
00260             if (Vect_top->next) {
00261                 /* can't free top if last */
00262                 found = 1;
00263                 Vect_top = fv->next;
00264             }
00265             else {
00266                 gv_free_vectmem(fv);
00267                 G_free(fv);
00268                 Vect_top = NULL;
00269             }
00270         }
00271         else {
00272             for (gv = Vect_top; gv && !found; gv = gv->next) {
00273                 /* can't free top */
00274                 if (gv->next) {
00275                     if (gv->next == fv) {
00276                         found = 1;
00277                         gv->next = fv->next;
00278                     }
00279                 }
00280             }
00281         }
00282 
00283         if (found) {
00284             G_debug(5, "gv_free_vect(): id=%d", fv->gvect_id);
00285             gv_free_vectmem(fv);
00286             G_free(fv);
00287             fv = NULL;
00288         }
00289 
00290         return (1);
00291     }
00292 
00293     return (-1);
00294 }
00295 
00301 void gv_free_vectmem(geovect * fv)
00302 {
00303     geoline *gln, *tmpln;
00304 
00305     G_free((void *)fv->filename);
00306     fv->filename = NULL;
00307 
00308     if (fv->lines) {
00309         for (gln = fv->lines; gln;) {
00310             if (gln->dims == 2) {
00311                 sub_Vectmem(gln->npts * sizeof(Point2));
00312                 G_free(gln->p2);
00313             }
00314 
00315             if (gln->dims == 3) {
00316                 G_free(gln->p3);
00317             }
00318 
00319             tmpln = gln;
00320             gln = gln->next;
00321             sub_Vectmem(sizeof(geoline));
00322             G_free(tmpln);
00323         }
00324 
00325         fv->n_lines = 0;
00326         fv->lines = NULL;
00327     }
00328 
00329     return;
00330 }
00331 
00339 void gv_set_drapesurfs(geovect * gv, int *hsurfs, int nsurfs)
00340 {
00341     int i;
00342 
00343     for (i = 0; i < nsurfs && i < MAX_SURFS; i++) {
00344         gv->drape_surf_id[i] = hsurfs[i];
00345     }
00346 
00347     return;
00348 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines