GRASS Programmer's Manual  6.4.2(2012)
Gs3.c
Go to the documentation of this file.
00001 
00019 #include <stdlib.h>
00020 #include <string.h>
00021 
00022 #include <grass/gis.h>
00023 #include <grass/glocale.h>
00024 #include <grass/bitmap.h>
00025 
00026 #include <grass/gsurf.h>
00027 #include <grass/gstypes.h>
00028 /* for geoview & geodisplay in 3dview stuff */
00029 #include "gsget.h"
00030 /* for update_attrange - might be able to move this func now */
00031 
00035 #define INIT_MINMAX(p, nm, size, min, max, found) \
00036         found = 0; \
00037         p+=(size-1); \
00038         while (size--) \
00039         { \
00040             if (!BM_GET_BYOFFSET(nm, size)) \
00041             { \
00042                 min = max = *p; \
00043                 found = 1; \
00044                 break; \
00045             } \
00046             p--; \
00047         }
00048 
00052 #define SET_MINMAX(p, nm, size, min, max) \
00053         p+=(size-1); \
00054         while(size--) \
00055         { \
00056             if (!BM_GET_BYOFFSET(nm, size)) \
00057             { \
00058                 if (*p < min) \
00059                 { \
00060                     min = *p; \
00061                 } \
00062                 else if (*p > max) \
00063                 { \
00064                     max = *p; \
00065                 }  \
00066             } \
00067             p--; \
00068         }
00069 
00070 typedef int FILEDESC;
00071 
00072 #define NO_DATA_COL 0xffffff
00073 
00084 double Gs_distance(double *from, double *to)
00085 {
00086     static int first = 1;
00087 
00088     if (first) {
00089         first = 0;
00090         G_begin_distance_calculations();
00091     }
00092 
00093     return G_distance(from[0], from[1], to[0], to[1]);
00094 }
00095 
00114 int Gs_loadmap_as_float(struct Cell_head *wind, const char *map_name,
00115                         float *buff, struct BM *nullmap, int *has_null)
00116 {
00117     FILEDESC cellfile;
00118     const char *map_set;
00119     char *nullflags;
00120     int offset, row, col;
00121 
00122     G_debug(3, "Gs_loadmap_as_float(): name=%s", map_name);
00123 
00124     map_set = G_find_cell2(map_name, "");
00125     if (!map_set) {
00126         G_warning(_("Raster map <%s> not found"), map_name);
00127         return 0;
00128     }
00129     *has_null = 0;
00130 
00131     nullflags = G_allocate_null_buf();  /* G_fatal_error */
00132     if (!nullflags) {
00133         G_fatal_error(_("Unable to allocate memory for a null buffer"));
00134     }
00135 
00136     if ((cellfile = G_open_cell_old(map_name, map_set)) == -1) {
00137         G_fatal_error(_("Unable to open raster map <%s>"), map_name);
00138     }
00139 
00140     G_message(_("Loading raster map <%s>..."),
00141               G_fully_qualified_name(map_name, map_set));
00142 
00143     for (row = 0; row < wind->rows; row++) {
00144         offset = row * wind->cols;
00145         G_get_f_raster_row(cellfile, &(buff[offset]), row);
00146         G_get_null_value_row(cellfile, nullflags, row);
00147 
00148         G_percent(row, wind->rows, 2);
00149 
00150         for (col = 0; col < wind->cols; col++) {
00151             if (nullflags[col] || G_is_f_null_value(buff + offset + col)) {
00152                 *has_null = 1;
00153                 BM_set(nullmap, col, row, 1);
00154             }
00155             /* set nm */
00156         }
00157     }
00158     G_percent(1, 1, 1);
00159 
00160     G_debug(4, "  has_null=%d", *has_null);
00161 
00162     G_close_cell(cellfile);
00163 
00164     G_free(nullflags);
00165 
00166     return (1);
00167 }
00168 
00189 int Gs_loadmap_as_int(struct Cell_head *wind, const char *map_name, int *buff,
00190                       struct BM *nullmap, int *has_null)
00191 {
00192     FILEDESC cellfile;
00193     const char *map_set;
00194     char *nullflags;
00195     int offset, row, col;
00196 
00197     G_debug(3, "Gs_loadmap_as_int");
00198 
00199     map_set = G_find_cell2(map_name, "");
00200     if (!map_set) {
00201         G_warning(_("Raster map <%s> not found"), map_name);
00202         return 0;
00203     }
00204     *has_null = 0;
00205 
00206     nullflags = G_allocate_null_buf();  /* G_fatal_error */
00207     if (!nullflags) {
00208         G_fatal_error(_("Unable to allocate memory for a null buffer"));
00209     }
00210 
00211     if ((cellfile = G_open_cell_old(map_name, map_set)) == -1) {
00212         G_fatal_error(_("Unable to open raster map <%s>"), map_name);
00213     }
00214 
00215     G_message(_("Loading raster map <%s>..."),
00216               G_fully_qualified_name(map_name, map_set));
00217 
00218     for (row = 0; row < wind->rows; row++) {
00219         offset = row * wind->cols;
00220         G_get_c_raster_row(cellfile, &(buff[offset]), row);
00221         G_get_null_value_row(cellfile, nullflags, row);
00222 
00223         G_percent(row, wind->rows, 2);
00224 
00225         for (col = 0; col < wind->cols; col++) {
00226             if (nullflags[col]) {
00227                 *has_null = 1;
00228                 BM_set(nullmap, col, row, 1);
00229             }
00230 
00231             /* set nm */
00232         }
00233     }
00234     G_percent(1, 1, 1);
00235     
00236     G_close_cell(cellfile);
00237 
00238     G_free(nullflags);
00239 
00240     return (1);
00241 }
00242 
00252 int Gs_numtype(const char *filename, int *negflag)
00253 {
00254     CELL max = 0, min = 0;
00255     struct Range range;
00256     const char *mapset;
00257     int shortbits, charbits, bitplace;
00258     static int max_short, max_char;
00259     static int first = 1;
00260 
00261     if (first) {
00262         max_short = max_char = 1;
00263         shortbits = 8 * sizeof(short);
00264 
00265         for (bitplace = 1; bitplace < shortbits; ++bitplace) {
00266             /*1 bit for sign */
00267             max_short *= 2;
00268         }
00269 
00270         max_short -= 1;
00271 
00272         /* NO bits for sign, using unsigned char */
00273         charbits = 8 * sizeof(unsigned char);
00274 
00275         for (bitplace = 0; bitplace < charbits; ++bitplace) {
00276             max_char *= 2;
00277         }
00278 
00279         max_char -= 1;
00280 
00281         first = 0;
00282     }
00283 
00284     mapset = G_find_cell2(filename, "");
00285     if (!mapset) {
00286         G_warning(_("Raster map <%s> not found"), filename);
00287         return -1;
00288     }
00289 
00290     if (G_raster_map_is_fp(filename, mapset)) {
00291         G_debug(3, "Gs_numtype(): fp map detected");
00292 
00293         return (ATTY_FLOAT);
00294     }
00295 
00296     if (-1 == G_read_range(filename, mapset, &range)) {
00297         return (-1);
00298     }
00299 
00300     G_get_range_min_max(&range, &min, &max);
00301     *negflag = (min < 0);
00302 
00303     if (max < max_char && min > 0) {
00304         return (ATTY_CHAR);
00305     }
00306 
00307     if (max < max_short && min > -max_short) {
00308         return (ATTY_SHORT);
00309     }
00310 
00311     return (ATTY_INT);
00312 }
00313 
00334 int Gs_loadmap_as_short(struct Cell_head *wind, const char *map_name,
00335                         short *buff, struct BM *nullmap, int *has_null)
00336 {
00337     FILEDESC cellfile;
00338     const char *map_set;
00339     char *nullflags;
00340     int *ti, *tmp_buf;
00341     int offset, row, col, val, max_short, overflow, shortsize, bitplace;
00342     short *ts;
00343 
00344     G_debug(3, "Gs_loadmap_as_short");
00345 
00346     overflow = 0;
00347     shortsize = 8 * sizeof(short);
00348 
00349     /* 1 bit for sign */
00350     /* same as 2 << (shortsize-1) */
00351     for (max_short = bitplace = 1; bitplace < shortsize; ++bitplace) {
00352         max_short *= 2;
00353     }
00354 
00355     max_short -= 1;
00356 
00357     map_set = G_find_cell2(map_name, "");
00358     if (!map_set) {
00359         G_warning(_("Raster map <%s> not found"), map_name);
00360         return -1;
00361     }
00362     *has_null = 0;
00363 
00364     nullflags = G_allocate_null_buf();
00365     if (!nullflags) {
00366         G_fatal_error(_("Unable to allocate memory for a null buffer"));
00367     }
00368 
00369     if ((cellfile = G_open_cell_old(map_name, map_set)) == -1) {
00370         G_fatal_error(_("Unable to open raster map <%s>"), map_name);
00371     }
00372 
00373     tmp_buf = (int *)G_malloc(wind->cols * sizeof(int));        /* G_fatal_error */
00374     if (!tmp_buf) {
00375         return -1;
00376     }
00377 
00378     G_message(_("Loading raster map <%s>..."),
00379               G_fully_qualified_name(map_name, map_set));
00380 
00381     for (row = 0; row < wind->rows; row++) {
00382         offset = row * wind->cols;
00383         G_get_c_raster_row(cellfile, tmp_buf, row);
00384         G_get_null_value_row(cellfile, nullflags, row);
00385 
00386         G_percent(row, wind->rows, 2);
00387 
00388         ts = &(buff[offset]);
00389         ti = tmp_buf;
00390 
00391         for (col = 0; col < wind->cols; col++) {
00392             if (nullflags[col]) {
00393                 *has_null = 1;
00394                 BM_set(nullmap, col, row, 1);
00395             }
00396             else {
00397                 val = *ti;
00398                 if (abs(val) > max_short) {
00399                     overflow = 1;
00400                     /* assign floor/ceiling value?
00401                      */
00402                     *ts = (short)(max_short * val / abs(val));
00403                 }
00404                 else {
00405                     *ts = (short)val;
00406                 }
00407             }
00408 
00409             ti++;
00410             ts++;
00411         }
00412     }
00413     G_percent(1, 1, 1);
00414     
00415     G_close_cell(cellfile);
00416 
00417     G_free(tmp_buf);
00418     G_free(nullflags);
00419 
00420     return (overflow ? -2 : 1);
00421 }
00422 
00449 int Gs_loadmap_as_char(struct Cell_head *wind, const char *map_name,
00450                        unsigned char *buff, struct BM *nullmap, int *has_null)
00451 {
00452     FILEDESC cellfile;
00453     const char *map_set;
00454     char *nullflags;
00455     int *ti, *tmp_buf;
00456     int offset, row, col, val, max_char, overflow, charsize, bitplace;
00457     unsigned char *tc;
00458 
00459     G_debug(3, "Gs_loadmap_as_char");
00460 
00461     overflow = 0;
00462     charsize = 8 * sizeof(unsigned char);
00463 
00464     /* 0 bits for sign! */
00465     max_char = 1;
00466 
00467     for (bitplace = 0; bitplace < charsize; ++bitplace) {
00468         max_char *= 2;
00469     }
00470 
00471     max_char -= 1;
00472 
00473     map_set = G_find_cell2(map_name, "");
00474     if (!map_set) {
00475         G_warning(_("Raster map <%s> not found"), map_name);
00476         return -1;
00477     }
00478     *has_null = 0;
00479 
00480     nullflags = G_allocate_null_buf();  /* G_fatal_error */
00481     if (!nullflags) {
00482         G_fatal_error(_("Unable to allocate memory for a null buffer"));
00483     }
00484 
00485     if ((cellfile = G_open_cell_old(map_name, map_set)) == -1) {
00486         G_fatal_error(_("Unable to open raster map <%s>"), map_name);
00487     }
00488 
00489     tmp_buf = (int *)G_malloc(wind->cols * sizeof(int));        /* G_fatal_error */
00490     if (!tmp_buf) {
00491         return -1;
00492     }
00493 
00494     G_message(_("Loading raster map <%s>..."),
00495               G_fully_qualified_name(map_name, map_set));
00496 
00497     for (row = 0; row < wind->rows; row++) {
00498         offset = row * wind->cols;
00499         G_get_c_raster_row(cellfile, tmp_buf, row);
00500         G_get_null_value_row(cellfile, nullflags, row);
00501         tc = (unsigned char *)&(buff[offset]);
00502         ti = tmp_buf;
00503 
00504         G_percent(row, wind->rows, 2);
00505 
00506         for (col = 0; col < wind->cols; col++) {
00507             if (nullflags[col]) {
00508                 *has_null = 1;
00509                 BM_set(nullmap, col, row, 1);
00510             }
00511             else {
00512                 val = *ti;
00513                 if (val > max_char) {
00514                     overflow = 1;
00515                     *tc = (unsigned char)max_char;
00516                 }
00517                 else if (val < 0) {
00518                     overflow = 1;
00519                     *tc = 0;
00520                 }
00521                 else {
00522                     *tc = (unsigned char)val;
00523                 }
00524             }
00525 
00526             ti++;
00527             tc++;
00528         }
00529     }
00530     G_percent(1, 1, 1);
00531     
00532     G_close_cell(cellfile);
00533 
00534     G_free(tmp_buf);
00535     G_free(nullflags);
00536 
00537     return (overflow ? -2 : 1);
00538 }
00539 
00559 int Gs_loadmap_as_bitmap(struct Cell_head *wind, const char *map_name,
00560                          struct BM *buff)
00561 {
00562     FILEDESC cellfile;
00563     const char *map_set;
00564     char *nullflags;
00565     int *tmp_buf;
00566     int row, col;
00567 
00568     G_debug(3, "Gs_loadmap_as_bitmap");
00569 
00570     map_set = G_find_cell2(map_name, "");
00571     if (!map_set) {
00572         G_warning(_("Raster map <%s> not found"), map_name);
00573         return -1;
00574     }
00575 
00576     if ((cellfile = G_open_cell_old(map_name, map_set)) == -1) {
00577         G_fatal_error(_("Unable to open raster map <%s>"), map_name);
00578     }
00579 
00580     tmp_buf = (int *)G_malloc(wind->cols * sizeof(int));        /* G_fatal_error */
00581     if (!tmp_buf) {
00582         return -1;
00583     }
00584 
00585     nullflags = G_allocate_null_buf();
00586     if (!nullflags) {
00587         G_fatal_error(_("Unable to allocate memory for a null buffer"));
00588     }
00589 
00590     G_message(_("Loading raster map <%s>..."),
00591               G_fully_qualified_name(map_name, map_set));
00592 
00593     for (row = 0; row < wind->rows; row++) {
00594         G_get_null_value_row(cellfile, nullflags, row);
00595 
00596         for (col = 0; col < wind->cols; col++) {
00597             if (nullflags[col]) {
00598                 /* no data */
00599                 BM_set(buff, col, row, 1);
00600             }
00601             else {
00602                 BM_set(buff, col, row, 0);
00603             }
00604         }
00605     }
00606 
00607     G_close_cell(cellfile);
00608 
00609     G_free(tmp_buf);
00610     G_free(nullflags);
00611 
00612     return (1);
00613 }
00614 
00627 int Gs_build_256lookup(const char *filename, int *buff)
00628 {
00629     const char *mapset;
00630     struct Colors colrules;
00631     CELL min, max, cats[256];
00632     int i;
00633     unsigned char r[256], g[256], b[256], set[256];
00634 
00635     G_debug(3, "building color table");
00636 
00637     mapset = G_find_cell2(filename, "");
00638     if (!mapset) {
00639         G_warning(_("Raster map <%s> not found"), filename);
00640         return 0;
00641     }
00642 
00643     G_read_colors(filename, mapset, &colrules);
00644     G_get_color_range(&min, &max, &colrules);
00645 
00646     if (min < 0 || max > 255) {
00647         G_warning(_("Color table range doesn't match data (mincol=%d, maxcol=%d"),
00648                   min, max);
00649 
00650         min = min < 0 ? 0 : min;
00651         max = max > 255 ? 255 : max;
00652     }
00653 
00654     G_zero(cats, 256 * sizeof(CELL));
00655 
00656     for (i = min; i <= max; i++) {
00657         cats[i] = i;
00658     }
00659 
00660     G_lookup_colors(cats, r, g, b, set, 256, &colrules);
00661 
00662     for (i = 0; i < 256; i++) {
00663 
00664         if (set[i]) {
00665             buff[i] =
00666                 (r[i] & 0xff) | ((g[i] & 0xff) << 8) | ((b[i] & 0xff) << 16);
00667         }
00668         else {
00669             buff[i] = NO_DATA_COL;
00670         }
00671     }
00672 
00673     return (1);
00674 }
00675 
00687 void Gs_pack_colors(const char *filename, int *buff, int rows, int cols)
00688 {
00689     const char *mapset;
00690     struct Colors colrules;
00691     unsigned char *r, *g, *b, *set;
00692     int *cur, i, j;
00693 
00694     mapset = G_find_cell2(filename, "");
00695     if (!mapset) {
00696         G_warning(_("Raster map <%s> not found"), filename);
00697         return;
00698     }
00699 
00700     r = (unsigned char *)G_malloc(cols);
00701     g = (unsigned char *)G_malloc(cols);
00702     b = (unsigned char *)G_malloc(cols);
00703     set = (unsigned char *)G_malloc(cols);
00704 
00705     G_read_colors(filename, mapset, &colrules);
00706 
00707     cur = buff;
00708 
00709     G_message(_("Translating colors from raster map <%s>..."),
00710               G_fully_qualified_name(filename, mapset));
00711 
00712     for (i = 0; i < rows; i++) {
00713         G_lookup_colors(cur, r, g, b, set, cols, &colrules);
00714         G_percent(i, rows, 2);
00715 
00716         for (j = 0; j < cols; j++) {
00717             if (set[j]) {
00718                 cur[j] =
00719                     (r[j] & 0xff) | ((g[j] & 0xff) << 8) | ((b[j] & 0xff) <<
00720                                                             16);
00721             }
00722             else {
00723                 cur[j] = NO_DATA_COL;
00724             }
00725         }
00726 
00727         cur = &(cur[cols]);
00728     }
00729     G_percent(1, 1, 1);
00730     
00731     G_free_colors(&colrules);
00732 
00733     G_free(r);
00734     G_free(g);
00735     G_free(b);
00736 
00737     G_free(set);
00738 
00739     return;
00740 }
00741 
00756 void Gs_pack_colors_float(const char *filename, float *fbuf, int *ibuf,
00757                           int rows, int cols)
00758 {
00759     const char *mapset;
00760     struct Colors colrules;
00761     unsigned char *r, *g, *b, *set;
00762     int i, j, *icur;
00763     FCELL *fcur;
00764 
00765     mapset = G_find_cell2(filename, "");
00766     if (!mapset) {
00767         G_warning(_("Raster map <%s> not found"), filename);
00768         return;
00769     }
00770 
00771     r = (unsigned char *)G_malloc(cols);
00772     g = (unsigned char *)G_malloc(cols);
00773     b = (unsigned char *)G_malloc(cols);
00774     set = (unsigned char *)G_malloc(cols);
00775 
00776     G_read_colors(filename, mapset, &colrules);
00777 
00778     fcur = fbuf;
00779     icur = ibuf;
00780 
00781     G_message(_("Translating colors from raster map <%s>..."),
00782               G_fully_qualified_name(filename, mapset));
00783     
00784     for (i = 0; i < rows; i++) {
00785         G_lookup_f_raster_colors(fcur, r, g, b, set, cols, &colrules);
00786         G_percent(i, rows, 2);
00787 
00788         for (j = 0; j < cols; j++) {
00789             if (set[j]) {
00790                 icur[j] =
00791                     (r[j] & 0xff) | ((g[j] & 0xff) << 8) | ((b[j] & 0xff) <<
00792                                                             16);
00793             }
00794             else {
00795                 icur[j] = NO_DATA_COL;
00796             }
00797         }
00798 
00799         icur = &(icur[cols]);
00800         fcur = &(fcur[cols]);
00801     }
00802     G_percent(1, 1, 1);
00803     
00804     G_free_colors(&colrules);
00805 
00806     G_free(r);
00807     G_free(g);
00808     G_free(b);
00809     G_free(set);
00810 
00811     return;
00812 }
00813 
00827 int Gs_get_cat_label(const char *filename, int drow, int dcol, char *catstr)
00828 {
00829     struct Categories cats;
00830     const char *mapset;
00831     CELL *buf;
00832     DCELL *dbuf;
00833     RASTER_MAP_TYPE map_type;
00834     int fd;
00835 
00836     if ((mapset = G_find_cell2(filename, "")) == NULL) {
00837         G_warning(_("Raster map <%s> not found"), filename);
00838         return 0;
00839     }
00840 
00841     if (-1 != G_read_cats(filename, mapset, &cats)) {
00842         fd = G_open_cell_old(filename, mapset);
00843         map_type = G_get_raster_map_type(fd);
00844 
00845         if (map_type == CELL_TYPE) {
00846             buf = G_allocate_c_raster_buf();
00847 
00848             if (G_get_c_raster_row(fd, buf, drow) < 0) {
00849                 sprintf(catstr, "error");
00850             }
00851             else if (G_is_c_null_value(&buf[dcol])) {
00852                 sprintf(catstr, "(NULL) %s",
00853                         G_get_c_raster_cat(&buf[dcol], &cats));
00854             }
00855             else {
00856                 sprintf(catstr, "(%d) %s", buf[dcol],
00857                         G_get_c_raster_cat(&buf[dcol], &cats));
00858             }
00859 
00860             G_free(buf);
00861         }
00862 
00863         else {
00864             /* fp map */
00865             dbuf = G_allocate_d_raster_buf();
00866 
00867             if (G_get_d_raster_row(fd, dbuf, drow) < 0) {
00868                 sprintf(catstr, "error");
00869             }
00870             else if (G_is_d_null_value(&dbuf[dcol])) {
00871                 sprintf(catstr, "(NULL) %s",
00872                         G_get_d_raster_cat(&dbuf[dcol], &cats));
00873             }
00874             else {
00875                 sprintf(catstr, "(%g) %s", dbuf[dcol],
00876                         G_get_d_raster_cat(&dbuf[dcol], &cats));
00877             }
00878 
00879             G_free(dbuf);
00880         }
00881     }
00882     else {
00883         strcpy(catstr, "no category label");
00884     }
00885 
00886     /* TODO: may want to keep these around for multiple queries */
00887     G_free_cats(&cats);
00888 
00889     G_close_cell(fd);
00890 
00891     return (1);
00892 }
00893 
00906 int Gs_save_3dview(const char *vname, geoview * gv, geodisplay * gd,
00907                    struct Cell_head *w, geosurf * defsurf)
00908 {
00909     const char *mapset;
00910     struct G_3dview v;
00911     float zmax, zmin;
00912 
00913     GS_get_zrange(&zmin, &zmax, 0);
00914 
00915     G_get_3dview_defaults(&v, w);
00916     mapset = G_mapset();
00917 
00918     if (mapset != NULL) {
00919         if (defsurf) {
00920             if (defsurf->draw_mode & DM_WIRE_POLY) {
00921                 v.display_type = 3;
00922             }
00923             else if (defsurf->draw_mode & DM_WIRE ||
00924                      defsurf->draw_mode & DM_COL_WIRE) {
00925                 v.display_type = 1;
00926             }
00927             else if (defsurf->draw_mode & DM_POLY) {
00928                 v.display_type = 2;
00929             }
00930 
00931             v.mesh_freq = defsurf->x_modw;      /* mesh resolution */
00932             v.poly_freq = defsurf->x_mod;       /* poly resolution */
00933             v.dozero = !(defsurf->nz_topo);
00934             v.colorgrid = (defsurf->draw_mode & DM_COL_WIRE) ? 1 : 0;
00935             v.shading = (defsurf->draw_mode & DM_GOURAUD) ? 1 : 0;
00936         }
00937 
00938         if (gv->infocus) {
00939             GS_v3eq(v.from_to[TO], gv->real_to);
00940             v.from_to[TO][Z] -= zmin;
00941             GS_v3mult(v.from_to[TO], gv->scale);
00942             v.from_to[TO][Z] *= gv->vert_exag;
00943         }
00944         else {
00945             GS_v3eq(v.from_to[TO], gv->from_to[TO]);
00946         }
00947 
00948         gsd_model2real(v.from_to[TO]);
00949 
00950         GS_v3eq(v.from_to[FROM], gv->from_to[FROM]);
00951         gsd_model2real(v.from_to[FROM]);
00952 
00953         v.exag = gv->vert_exag;
00954         v.fov = gv->fov / 10.;
00955         v.twist = gv->twist;
00956         v.fringe = 0;           /* not implemented here */
00957 
00958         v.lightson = 1;         /* always true, curently */
00959 
00960         if (gv->lights[0].position[W] == 1) {
00961             /* local */
00962             v.lightpos[X] = gv->lights[0].position[X];
00963             v.lightpos[Y] = gv->lights[0].position[Y];
00964             v.lightpos[Z] = gv->lights[0].position[Z];
00965             gsd_model2real(v.lightpos);
00966             v.lightpos[W] = 1.0;        /* local */
00967         }
00968         else {
00969             v.lightpos[X] = gv->lights[0].position[X];
00970             v.lightpos[Y] = gv->lights[0].position[Y];
00971             v.lightpos[Z] = gv->lights[0].position[Z];
00972             v.lightpos[W] = 0.0;        /* inf */
00973         }
00974 
00975         v.lightcol[0] = gv->lights[0].color[0];
00976         v.lightcol[1] = gv->lights[0].color[1];
00977         v.lightcol[2] = gv->lights[0].color[2];
00978 
00979         v.ambient = (gv->lights[0].ambient[0] + gv->lights[0].ambient[1] +
00980                      gv->lights[0].ambient[2]) / 3.;
00981         v.shine = gv->lights[0].shine;
00982 
00983         v.surfonly = 0;         /* N/A - now uses constant color */
00984         strcpy((v.pgm_id), "Nvision-ALPHA!");
00985 
00986         return (G_put_3dview(vname, mapset, &v, w));
00987     }
00988     else {
00989         return (-1);
00990     }
00991 }
00992 
01004 int Gs_load_3dview(const char *vname, geoview * gv, geodisplay * gd,
01005                    struct Cell_head *w, geosurf * defsurf)
01006 {
01007     const char *mapset;
01008     struct G_3dview v;
01009     int ret = -1;
01010     float pt[3];
01011 
01012     mapset = G_find_file2("3d.view", vname, "");
01013 
01014     if (mapset != NULL) {
01015         ret = G_get_3dview(vname, mapset, &v);
01016     }
01017 
01018     if (ret >= 0) {
01019         if (strcmp((v.pgm_id), "Nvision-ALPHA!")) {
01020             G_warning(_("View not saved by this program,"
01021                         "there may be some inconsistancies"));
01022         }
01023 
01024         /* set poly and mesh resolutions */
01025         v.mesh_freq = (int)(v.mesh_freq * v.vwin.ns_res / w->ns_res);
01026         v.poly_freq = (int)(v.poly_freq * v.vwin.ns_res / w->ns_res);
01027 
01028         /* Set To and FROM positions */
01029         /* TO */
01030         pt[0] = (v.from_to[TO][X] - w->west) - w->ew_res / 2.;
01031         pt[1] = (v.from_to[TO][Y] - w->south) - w->ns_res / 2.;
01032         pt[2] = v.from_to[TO][Z];
01033         GS_set_focus(pt);
01034 
01035         /* FROM */
01036         pt[0] = (float)v.from_to[FROM][X];
01037         pt[1] = (float)v.from_to[FROM][Y];
01038         pt[2] = (float)v.from_to[FROM][Z];
01039         GS_moveto_real(pt);
01040 
01041         if (defsurf) {
01042             int dmode = 0;
01043 
01044             GS_setall_drawres(v.poly_freq, v.poly_freq,
01045                               v.mesh_freq, v.mesh_freq);
01046 
01047             while (v.display_type >= 10) {
01048                 /* globe stuff not used */
01049                 v.display_type -= 10;
01050             }
01051 
01052             /* set drawing modes */
01053             if (v.colorgrid) {
01054                 dmode |= DM_COL_WIRE;
01055             }
01056 
01057             if (v.shading) {
01058                 dmode |= DM_GOURAUD;
01059             }
01060 
01061             switch (v.display_type) {
01062             case 1:
01063                 dmode |= DM_WIRE;
01064 
01065                 break;
01066             case 2:
01067                 dmode |= DM_POLY;
01068 
01069                 break;
01070             case 3:
01071                 dmode |= DM_WIRE_POLY;
01072 
01073                 break;
01074             }
01075             GS_setall_drawmode(dmode);
01076 
01077             /* should also set nozeros here */
01078         }
01079 
01080         /* set exaggeration */
01081         if (v.exag)
01082             GS_set_global_exag(v.exag);
01083 
01084         /* Set FOV */
01085         if (v.fov) {
01086             GS_set_fov((int)
01087                        (v.fov > 0 ? v.fov * 10. + 0.5 : v.fov * 10. - 0.5));
01088         }
01089         else {
01090             /* TODO: do ortho */
01091         }
01092 
01093         /* Set twist */
01094         if (v.twist)
01095             GS_set_twist((int)(v.twist > 0 ? v.twist + 0.5 : v.twist - 0.5));
01096 
01097 
01098         /* TODO:  OK to here - need to unravel/reverse lights stuff*** */
01099 
01100         if (v.lightson) {
01101             /* Lights are on */
01102 
01103             /* Light Position */
01104             gv->lights[0].position[X] = v.lightpos[X];
01105             gv->lights[0].position[Y] = v.lightpos[Y];
01106             gv->lights[0].position[Z] = v.lightpos[Z];
01107 
01108             /* Light Color */
01109             gv->lights[0].color[0] = v.lightcol[0];
01110             gv->lights[0].color[1] = v.lightcol[1];
01111             gv->lights[0].color[2] = v.lightcol[2];
01112 
01113             /* Light Shininess */
01114             gv->lights[0].shine = v.shine;
01115 
01116             /* Light Ambient */
01117             gv->lights[0].ambient[0] = gv->lights[0].ambient[1] =
01118                 gv->lights[0].ambient[2] = v.ambient * 3.;
01119 
01120 
01121         }                       /* Done with lights */
01122 
01123 
01124         GS_alldraw_wire();
01125 
01126     }                           /* Done with file */
01127     return (1);
01128 
01129 }
01130 
01140 int Gs_update_attrange(geosurf * gs, int desc)
01141 {
01142     long size;
01143     float min, max;
01144     typbuff *tb;
01145     struct BM *nm;
01146     int found;
01147 
01148     gs->att[desc].max_nz = gs->att[desc].min_nz = gs->att[desc].range_nz =
01149         0.0;
01150 
01151     if (CONST_ATT == gs_get_att_src(gs, desc)) {
01152         gs->att[desc].max_nz = gs->att[desc].min_nz = gs->att[desc].constant;
01153         gs->att[desc].range_nz = 0.0;
01154     }
01155     else if (CF_COLOR_PACKED & gsds_get_changed(gs->att[desc].hdata)) {
01156         gs->att[desc].max_nz = 0xFFFFFF;
01157         gs->att[desc].min_nz = 0x010101;
01158         gs->att[desc].range_nz = 0xFFFFFF;
01159     }
01160     else {
01161         if (NULL == (tb = gsds_get_typbuff(gs->att[desc].hdata, 0))) {
01162             return (-1);
01163         }
01164 
01165         nm = tb->nm;
01166 
01167         if (tb->ib) {
01168             int *p;
01169 
01170             size = gs->rows * gs->cols;
01171             p = tb->ib;
01172             INIT_MINMAX(p, nm, size, min, max, found);
01173 
01174             if (!found) {
01175                 /* all nulls! */
01176                 return (-1);
01177             }
01178 
01179             size = gs->rows * gs->cols;
01180             p = tb->ib;
01181             SET_MINMAX(p, nm, size, min, max);
01182         }
01183         else if (tb->sb) {
01184             short *p;
01185 
01186             size = gs->rows * gs->cols;
01187             p = tb->sb;
01188             INIT_MINMAX(p, nm, size, min, max, found);
01189 
01190             if (!found) {
01191                 /* all nulls! */
01192                 return (-1);
01193             }
01194 
01195             size = gs->rows * gs->cols;
01196             p = tb->sb;
01197             SET_MINMAX(p, nm, size, min, max);
01198         }
01199         else if (tb->cb) {
01200             char *p;
01201 
01202             size = gs->rows * gs->cols;
01203             p = (char *)tb->cb;
01204             INIT_MINMAX(p, nm, size, min, max, found);
01205 
01206             if (!found) {
01207                 /* all nulls! */
01208                 return (-1);
01209             }
01210 
01211             size = gs->rows * gs->cols;
01212             p = (char *)tb->cb;
01213             SET_MINMAX(p, nm, size, min, max);
01214         }
01215         else if (tb->fb) {
01216             float *p;
01217 
01218             size = gs->rows * gs->cols;
01219             p = tb->fb;
01220             INIT_MINMAX(p, nm, size, min, max, found);
01221 
01222             if (!found) {
01223                 /* all nulls! */
01224                 return (-1);
01225             }
01226 
01227             size = gs->rows * gs->cols;
01228             p = tb->fb;
01229             SET_MINMAX(p, nm, size, min, max);
01230         }
01231 
01232         gs->att[desc].max_nz = max;
01233         gs->att[desc].min_nz = min;
01234         gs->att[desc].range_nz = gs->att[desc].max_nz - gs->att[desc].min_nz;
01235     }
01236 
01237     if (ATT_TOPO == desc) {
01238         gs->zmin = min;
01239         gs->zmax = max;
01240         gs->zrange = gs->zmax - gs->zmin;
01241         gs->zminmasked = gs->zmin;
01242         gs->zmax_nz = gs->zmax;
01243         gs->zmin_nz = gs->zmin;
01244         gs->zrange_nz = gs->zmax_nz - gs->zmin_nz;
01245     }
01246 
01247     G_debug(3, "Gs_update_attrange(): min=%f max=%f", gs->zmin, gs->zmax);
01248 
01249     return (1);
01250 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines