GRASS Programmer's Manual  6.4.2(2012)
gvl.c
Go to the documentation of this file.
00001 
00020 #include <stdlib.h>
00021 
00022 #include <grass/gis.h>
00023 #include <grass/gstypes.h>
00024 
00025 #include "gsget.h"
00026 
00027 #define FIRST_VOL_ID 81721
00028 
00029 static geovol *Vol_top = NULL;
00030 
00039 geovol *gvl_get_vol(int id)
00040 {
00041     geovol *gvl;
00042 
00043     G_debug(5, "gvl_get_vol():");
00044 
00045     for (gvl = Vol_top; gvl; gvl = gvl->next) {
00046         if (gvl->gvol_id == id) {
00047             G_debug(5, "    id=%d", id);
00048             return (gvl);
00049         }
00050     }
00051 
00052     return (NULL);
00053 }
00054 
00063 geovol *gvl_get_prev_vol(int id)
00064 {
00065     geovol *pv;
00066 
00067     G_debug(5, "gvl_get_prev_vol");
00068 
00069     for (pv = Vol_top; pv; pv = pv->next) {
00070         if (pv->gvol_id == id - 1) {
00071             return (pv);
00072         }
00073     }
00074 
00075     return (NULL);
00076 }
00077 
00085 int gvl_getall_vols(geovol ** gvols)
00086 {
00087     geovol *gvl;
00088     int i;
00089 
00090     G_debug(5, "gvl_getall_vols");
00091 
00092     for (i = 0, gvl = Vol_top; gvl; gvl = gvl->next, i++) {
00093         gvols[i] = gvl;
00094     }
00095 
00096     return (i);
00097 }
00098 
00104 int gvl_num_vols(void)
00105 {
00106     geovol *gvl;
00107     int i;
00108 
00109     for (i = 0, gvl = Vol_top; gvl; gvl = gvl->next, i++) ;
00110 
00111     G_debug(5, "gvl_num_vols(): num=%d", i);
00112 
00113     return (i);
00114 }
00115 
00122 geovol *gvl_get_last_vol(void)
00123 {
00124     geovol *lvl;
00125 
00126     G_debug(5, "gvl_get_last_vol");
00127 
00128     if (!Vol_top) {
00129         return (NULL);
00130     }
00131 
00132     for (lvl = Vol_top; lvl->next; lvl = lvl->next) ;
00133 
00134     G_debug(5, "  last vol id: %d", lvl->gvol_id);
00135 
00136     return (lvl);
00137 }
00138 
00145 geovol *gvl_get_new_vol(void)
00146 {
00147     geovol *nvl, *lvl;
00148 
00149     G_debug(5, "gvl_get_new_vol()");
00150 
00151     nvl = (geovol *) G_malloc(sizeof(geovol));  /* G_fatal_error */
00152     if (!nvl) {
00153         return (NULL);
00154     }
00155 
00156     if ((lvl = gvl_get_last_vol())) {
00157         lvl->next = nvl;
00158         nvl->gvol_id = lvl->gvol_id + 1;
00159     }
00160     else {
00161         Vol_top = nvl;
00162         nvl->gvol_id = FIRST_VOL_ID;
00163     }
00164 
00165     nvl->next = NULL;
00166 
00167     G_debug(5, "    id=%d", nvl->gvol_id);
00168     
00169     return (nvl);
00170 }
00171 
00184 int gvl_init_vol(geovol * gvl, double ox, double oy, double oz,
00185                  int rows, int cols, int depths, double xres, double yres,
00186                  double zres)
00187 {
00188     G_debug(5, "gvl_init_vol() id=%d", gvl->gvol_id);
00189 
00190     if (!gvl) {
00191         return (-1);
00192     }
00193 
00194     gvl->ox = ox;
00195     gvl->oy = oy;
00196     gvl->oz = oz;
00197     gvl->rows = rows;
00198     gvl->cols = cols;
00199     gvl->depths = depths;
00200     gvl->xres = xres;
00201     gvl->yres = yres;
00202     gvl->zres = zres;
00203 
00204     gvl->xmin = ox;
00205     gvl->xmax = ox + (cols - 1) * xres;
00206     gvl->xrange = gvl->xmax - gvl->xmin;
00207     gvl->ymin = oy;
00208     gvl->ymax = oy + (rows - 1) * yres;
00209     gvl->yrange = gvl->ymax - gvl->ymin;
00210     gvl->zmin = oz;
00211     gvl->zmax = oz + (depths - 1) * zres;
00212     gvl->zrange = gvl->zmax - gvl->zmin;
00213 
00214     gvl->x_trans = gvl->y_trans = gvl->z_trans = 0.0;
00215 
00216     gvl->n_isosurfs = 0;
00217     G_zero(gvl->isosurf, sizeof(geovol_isosurf *) * MAX_ISOSURFS);
00218     gvl->isosurf_x_mod = 1;
00219     gvl->isosurf_y_mod = 1;
00220     gvl->isosurf_z_mod = 1;
00221     gvl->isosurf_draw_mode = DM_GOURAUD;
00222 
00223     gvl->n_slices = 0;
00224     G_zero(gvl->slice, sizeof(geovol_slice *) * MAX_SLICES);
00225     gvl->slice_x_mod = 1;
00226     gvl->slice_y_mod = 1;
00227     gvl->slice_z_mod = 1;
00228     gvl->slice_draw_mode = DM_GOURAUD;
00229 
00230     gvl->hfile = -1;
00231     gvl->clientdata = NULL;
00232 
00233     return (1);
00234 }
00235 
00241 void gvl_delete_vol(int id)
00242 {
00243     geovol *fvl;
00244 
00245     G_debug(5, "gvl_delete_vol");
00246 
00247     fvl = gvl_get_vol(id);
00248 
00249     if (fvl) {
00250         gvl_free_vol(fvl);
00251     }
00252 
00253     return;
00254 }
00255 
00264 int gvl_free_vol(geovol * fvl)
00265 {
00266     geovol *gvl;
00267     int found = 0;
00268 
00269     G_debug(5, "gvl_free_vol");
00270 
00271     if (Vol_top) {
00272         if (fvl == Vol_top) {
00273             if (Vol_top->next) {
00274                 /* can't free top if last */
00275                 found = 1;
00276                 Vol_top = fvl->next;
00277             }
00278             else {
00279                 gvl_free_volmem(fvl);
00280                 G_free(fvl);
00281                 Vol_top = NULL;
00282             }
00283         }
00284         else {
00285             for (gvl = Vol_top; gvl && !found; gvl = gvl->next) {
00286                 /* can't free top */
00287                 if (gvl->next) {
00288                     if (gvl->next == fvl) {
00289                         found = 1;
00290                         gvl->next = fvl->next;
00291                     }
00292                 }
00293             }
00294         }
00295 
00296         if (found) {
00297             gvl_free_volmem(fvl);
00298             G_free(fvl);
00299             fvl = NULL;
00300         }
00301 
00302         return (1);
00303     }
00304 
00305     return (-1);
00306 }
00307 
00313 void gvl_free_volmem(geovol * fvl)
00314 {
00315     if (0 < fvl->hfile)
00316         gvl_file_free_datah(fvl->hfile);
00317 
00318     return;
00319 }
00320 
00326 void print_vol_fields(geovol * gvl)
00327 {
00328     G_debug(5, "ID: %d", gvl->gvol_id);
00329     G_debug(5, "cols: %d rows: %d depths: %d", gvl->cols, gvl->rows,
00330             gvl->depths);
00331     G_debug(5, "ox: %lf oy: %lf oz: %lf", gvl->ox, gvl->oy, gvl->oz);
00332     G_debug(5, "xres: %lf yres: %lf zres: %lf", gvl->xres, gvl->yres,
00333             gvl->zres);
00334     G_debug(5, "xmin: %f ymin: %f zmin: %f", gvl->xmin, gvl->ymin, gvl->zmin);
00335     G_debug(5, "xmax: %f ymax: %f zmax: %f", gvl->xmax, gvl->ymax, gvl->zmax);
00336     G_debug(5, "x_trans: %f y_trans: %f z_trans: %f", gvl->x_trans,
00337             gvl->y_trans, gvl->z_trans);
00338 
00339     return;
00340 }
00341 
00351 int gvl_get_xextents(geovol * gvl, float *min, float *max)
00352 {
00353     *min = gvl->xmin + gvl->x_trans;
00354     *max = gvl->xmax + gvl->x_trans;
00355 
00356     return (1);
00357 }
00358 
00368 int gvl_get_yextents(geovol * gvl, float *min, float *max)
00369 {
00370     *min = gvl->ymin + gvl->y_trans;
00371     *max = gvl->ymax + gvl->y_trans;
00372 
00373     return (1);
00374 }
00375 
00385 int gvl_get_zextents(geovol * gvl, float *min, float *max)
00386 {
00387     *min = gvl->zmin + gvl->z_trans;
00388     *max = gvl->zmax + gvl->z_trans;
00389 
00390     return (1);
00391 }
00392 
00401 int gvl_get_xrange(float *min, float *max)
00402 {
00403     geovol *gvl;
00404     float tmin, tmax;
00405 
00406     if (Vol_top) {
00407         gvl_get_xextents(Vol_top, &tmin, &tmax);
00408         *min = tmin;
00409         *max = tmax;
00410     }
00411     else {
00412         return (-1);
00413     }
00414 
00415     for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
00416         gvl_get_xextents(gvl, &tmin, &tmax);
00417 
00418         if (tmin < *min) {
00419             *min = tmin;
00420         }
00421 
00422         if (tmax > *max) {
00423             *max = tmax;
00424         }
00425     }
00426 
00427     return (1);
00428 }
00429 
00438 int gvl_get_yrange(float *min, float *max)
00439 {
00440     geovol *gvl;
00441     float tmin, tmax;
00442 
00443     if (Vol_top) {
00444         gvl_get_yextents(Vol_top, &tmin, &tmax);
00445         *min = tmin;
00446         *max = tmax;
00447     }
00448     else {
00449         return (-1);
00450     }
00451 
00452     for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
00453         gvl_get_yextents(gvl, &tmin, &tmax);
00454 
00455         if (tmin < *min) {
00456             *min = tmin;
00457         }
00458 
00459         if (tmax > *max) {
00460             *max = tmax;
00461         }
00462     }
00463 
00464     return (1);
00465 }
00466 
00475 int gvl_get_zrange(float *min, float *max)
00476 {
00477     geovol *gvl;
00478     float tmin, tmax;
00479 
00480     if (Vol_top) {
00481         gvl_get_zextents(Vol_top, &tmin, &tmax);
00482         *min = tmin;
00483         *max = tmax;
00484     }
00485     else {
00486         return (-1);
00487     }
00488 
00489     for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
00490         gvl_get_zextents(gvl, &tmin, &tmax);
00491 
00492         if (tmin < *min) {
00493             *min = tmin;
00494         }
00495 
00496         if (tmax > *max) {
00497             *max = tmax;
00498         }
00499     }
00500 
00501     return (1);
00502 }
00503 
00504 /************************************************************************/
00505 /* ISOSURFACES */
00506 
00507 /************************************************************************/
00508 
00517 int gvl_isosurf_init(geovol_isosurf * isosurf)
00518 {
00519     int i;
00520 
00521     G_debug(5, "gvl_isosurf_init");
00522 
00523     if (!isosurf)
00524         return (-1);
00525 
00526     for (i = 0; i < MAX_ATTS; i++) {
00527         isosurf->att[i].att_src = NOTSET_ATT;
00528         isosurf->att[i].constant = 0.;
00529         isosurf->att[i].hfile = -1;
00530         isosurf->att[i].user_func = NULL;
00531         isosurf->att[i].att_data = NULL;
00532         isosurf->att[i].changed = 0;
00533     }
00534 
00535     isosurf->data = NULL;
00536     isosurf->data_desc = 0;
00537     isosurf->inout_mode = 0;
00538 
00539     return (1);
00540 }
00541 
00550 int gvl_isosurf_freemem(geovol_isosurf * isosurf)
00551 {
00552     int i;
00553 
00554     G_debug(5, "gvl_isosurf_freemem");
00555 
00556     if (!isosurf)
00557         return (-1);
00558 
00559     for (i = 0; i < MAX_ATTS; i++) {
00560         gvl_isosurf_set_att_src(isosurf, i, NOTSET_ATT);
00561     }
00562 
00563     G_free(isosurf->data);
00564 
00565     return (1);
00566 }
00567 
00577 geovol_isosurf *gvl_isosurf_get_isosurf(int id, int isosurf_id)
00578 {
00579     geovol *gvl;
00580 
00581     G_debug(5, "gvl_isosurf_get_isosurf(): id=%d isosurf=%d",
00582             id, isosurf_id);
00583     
00584     gvl = gvl_get_vol(id);
00585 
00586     if (gvl) {
00587         if ((isosurf_id < 0) || (isosurf_id > (gvl->n_isosurfs - 1)))
00588             return (NULL);
00589 
00590         return gvl->isosurf[isosurf_id];
00591     }
00592 
00593     return (NULL);
00594 }
00595 
00605 int gvl_isosurf_get_att_src(geovol_isosurf * isosurf, int desc)
00606 {
00607     G_debug(5, "isosurf_get_att_src");
00608 
00609     if (!LEGAL_ATT(desc)) {
00610         return (-1);
00611     }
00612 
00613     if (isosurf) {
00614         return (isosurf->att[desc].att_src);
00615     }
00616 
00617     return (-1);
00618 }
00619 
00630 int gvl_isosurf_set_att_src(geovol_isosurf * isosurf, int desc, int src)
00631 {
00632     G_debug(5, "gvl_isosurf_set_att_src");
00633 
00634     /* check if old source was MAP_ATT, deattach volfile */
00635     if (MAP_ATT == gvl_isosurf_get_att_src(isosurf, desc)) {
00636         gvl_file_free_datah(isosurf->att[desc].hfile);
00637 
00638         if (desc == ATT_COLOR) {
00639             Gvl_unload_colors_data(isosurf->att[desc].att_data);
00640         }
00641     }
00642 
00643     if (isosurf && LEGAL_SRC(src)) {
00644         isosurf->att[desc].att_src = src;
00645         gvl_isosurf_set_att_changed(isosurf, desc);
00646 
00647         return (1);
00648     }
00649 
00650     return (-1);
00651 }
00652 
00663 int gvl_isosurf_set_att_const(geovol_isosurf * isosurf, int desc,
00664                               float constant)
00665 {
00666     G_debug(5, "gvl_isosurf_set_att_const(): att=%d, const=%f",
00667             desc, constant);
00668 
00669     if (isosurf) {
00670         isosurf->att[desc].constant = constant;
00671 
00672         gvl_isosurf_set_att_src(isosurf, desc, CONST_ATT);
00673 
00674         return (1);
00675     }
00676 
00677     return (-1);
00678 }
00679 
00690 int gvl_isosurf_set_att_map(geovol_isosurf * isosurf, int desc,
00691                             const char *filename)
00692 {
00693     int hfile;
00694 
00695     G_debug(5, "gvl_isosurf_set_att_map(): att=%d map=%s", desc, filename);
00696 
00697     if (isosurf) {
00698         if (0 > (hfile = gvl_file_newh(filename, VOL_FTYPE_G3D)))
00699             return (-1);
00700 
00701         gvl_isosurf_set_att_src(isosurf, desc, MAP_ATT);
00702 
00703         isosurf->att[desc].hfile = hfile;
00704 
00705         if (ATT_COLOR == desc) {
00706             Gvl_load_colors_data(&(isosurf->att[desc].att_data), filename);
00707         }
00708         return (1);
00709     }
00710 
00711     return (-1);
00712 }
00713 
00723 int gvl_isosurf_set_att_changed(geovol_isosurf * isosurf, int desc)
00724 {
00725     int i;
00726 
00727     G_debug(5, "gvl_isosurf_set_att_changed");
00728 
00729     if (isosurf && LEGAL_ATT(desc)) {
00730         isosurf->att[desc].changed = 1;
00731 
00732         if ((desc == ATT_TOPO) || (desc == ATT_MASK)) {
00733             for (i = 1; i < MAX_ATTS; i++)
00734                 isosurf->att[i].changed = 1;
00735         }
00736 
00737         return (1);
00738     }
00739 
00740     return (-1);
00741 }
00742 
00743 /************************************************************************/
00744 /* SLICES */
00745 
00746 /************************************************************************/
00747 
00756 int gvl_slice_init(geovol_slice * slice)
00757 {
00758     G_debug(5, "gvl_slice_init");
00759 
00760     if (!slice)
00761         return (-1);
00762 
00763     slice->data = NULL;
00764     slice->changed = 0;
00765     slice->mode = 1;
00766     slice->transp = 0;
00767 
00768     slice->z1 = 0;
00769     slice->z2 = 99;
00770 
00771     return (1);
00772 }
00773 
00782 int gvl_slice_freemem(geovol_slice * slice)
00783 {
00784     G_debug(5, "gvl_slice_freemem");
00785 
00786     if (!slice)
00787         return (-1);
00788 
00789     G_free(slice->data);
00790 
00791     return (1);
00792 }
00793 
00803 geovol_slice *gvl_slice_get_slice(int id, int slice_id)
00804 {
00805     geovol *gvl;
00806 
00807     gvl = gvl_get_vol(id);
00808 
00809     if (gvl) {
00810         if ((slice_id < 0) || (slice_id > (gvl->n_slices - 1)))
00811             return (NULL);
00812 
00813         return gvl->slice[slice_id];
00814     }
00815 
00816     return (NULL);
00817 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines