GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
gvl_file.c
Go to the documentation of this file.
1 /*!
2  \file lib/ogsf/gvl_file.c
3 
4  \brief OGSF library - loading and manipulating volumes (lower level
5  functions)
6 
7  GRASS OpenGL gsurf OGSF Library
8 
9  (C) 1999-2008 by the GRASS Development Team
10 
11  This program is free software under the
12  GNU General Public License (>=v2).
13  Read the file COPYING that comes with GRASS
14  for details.
15 
16  \author Tomas Paudits (February 2004)
17  \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
18  */
19 
20 #include <string.h>
21 #include <stdlib.h>
22 
23 #include <grass/gis.h>
24 #include <grass/ogsf.h>
25 #include <grass/raster3d.h>
26 #include <grass/glocale.h>
27 
28 #define LUCKY 33
29 
30 #define MODE_DIRECT 0
31 #define MODE_SLICE 1
32 #define MODE_FULL 2
33 #define MODE_PRELOAD 3
34 
35 #define MODE_DEFAULT 0
36 
37 #define STATUS_READY 0
38 #define STATUS_BUSY 1
39 
40 /*!
41  \brief structure for slice mode reading from volume file
42  */
43 typedef struct {
44  int num, skip;
45  int crnt, base;
46 
47  void *slice[MAX_VOL_SLICES];
48 } slice_data;
49 
50 static geovol_file *Data[MAX_VOL_FILES];
51 static geovol_file Df[MAX_VOL_FILES]; /* trying to avoid allocation */
52 
53 static int Numfiles = 0;
54 
55 static int Cur_id = LUCKY;
56 static int Cur_max;
57 
58 static int Rows, Cols, Depths;
59 
60 /* local functions prototypes */
61 void *open_g3d_file(const char *, IFLAG *, double *, double *);
62 int close_g3d_file(void *);
63 
64 /*!
65  \brief Initialize volume files
66 
67  \return 1
68  */
69 static int init_volfiles(void)
70 {
71  int i;
72  RASTER3D_Region *w3;
73 
74  for (i = 0; i < MAX_VOL_FILES; i++) {
75  /* avoiding dynamic allocation */
76  Data[i] = &(Df[i]);
77  }
78 
79  Cur_max = MAX_VOL_FILES;
80 
81  /* get window */
82  w3 = GVL_get_window();
83 
84  /* set cols, rows, depths from window */
85  Cols = w3->cols;
86  Rows = w3->rows;
87  Depths = w3->depths;
88 
89  return (1);
90 }
91 
92 /*!
93  \brief Check number of files
94 
95  \return 0
96  */
97 static int check_num_volfiles(void)
98 {
99  if (Numfiles < Cur_max) {
100  return (0);
101  }
102 
103  G_fatal_error(_("Maximum number of datafiles exceeded"));
104 
105  /* This return statement keeps compilers happy, it is never executed */
106  return (0);
107 }
108 
109 /*!
110  \brief Get geovol_file structure for given handle
111 
112  \param id
113 
114  \return pointer to geovol_file struct
115  \return NULL on failure
116  */
118 {
119  int i;
120 
121  for (i = 0; i < Numfiles; i++) {
122  if (Data[i]->data_id == id) {
123  return (Data[i]);
124  }
125  }
126 
127  return (NULL);
128 }
129 
130 /*!
131  \brief Find file with name and type in geovol_file array an return handle
132 
133  \param name file name
134  \param begin
135 
136  \param data id
137  \param -1 not found
138  */
139 int find_datah(const char *name, IFLAG type, int begin)
140 {
141  static int i;
142  int start;
143 
144  start = begin ? 0 : i + 1;
145 
146  for (i = start; i < Numfiles; i++) {
147  if (!strcmp(Data[i]->file_name, name)) {
148  if (Data[i]->file_type == type) {
149  return (Data[i]->data_id);
150  }
151  }
152  }
153 
154  return (-1);
155 }
156 
157 /*!
158  \brief Get file name for given handle
159 
160  \param id handle id
161 
162  \return file name
163  \return NULL on failure
164  */
165 char *gvl_file_get_name(int id)
166 {
167  int i;
168  geovol_file *fvf;
169  static char retstr[GPATH_MAX];
170 
171  for (i = 0; i < Numfiles; i++) {
172  if (Data[i]->data_id == id) {
173  fvf = Data[i];
174  strcpy(retstr, fvf->file_name);
175 
176  return (retstr);
177  }
178  }
179 
180  return (NULL);
181 }
182 
183 /*!
184  \brief Get file type for given handle
185 
186  \param vf pointer to geovol_file struct
187 
188  \return file type
189  */
191 {
192  return (vf->file_type);
193 }
194 
195 /*!
196  \brief Get data type for given handle
197 
198  \param vf pointer to geovol_file struct
199 
200  \return data type
201  */
203 {
204  return (vf->data_type);
205 }
206 
207 /*!
208  \brief Get minimum and maximum value in volume file
209 
210  \param vf pointer to geovol_file struct
211  \param[out] min min value
212  \param[out] max max value
213  */
214 void gvl_file_get_min_max(geovol_file *vf, double *min, double *max)
215 {
216  *min = vf->min;
217  *max = vf->max;
218 }
219 
220 /*!
221  \brief Open 3d raster file
222 
223  \param name file name
224  \param file_type file type
225  \param data_type data type
226  \param[out] min min value
227  \param[out] max max value
228 
229  \return pointer to file
230  \return NULL on failure
231  */
232 void *open_volfile(const char *name, IFLAG file_type, IFLAG *data_type,
233  double *min, double *max)
234 {
235  if (file_type == VOL_FTYPE_RASTER3D) {
236  return open_g3d_file(name, data_type, min, max);
237  }
238 
239  return (NULL);
240 }
241 
242 /*!
243  \brief Close volume file
244 
245  \param map volume filename
246  \param type file type
247 
248  \return
249  \return -1 on failure
250  */
251 int close_volfile(void *map, IFLAG type)
252 {
253  if (type == VOL_FTYPE_RASTER3D) {
254  return close_g3d_file(map);
255  }
256 
257  return (-1);
258 }
259 
260 /*!
261  \brief Get handle for given file name and type
262 
263  \param name volume filename
264  \param file_type file type
265 
266  \return data id
267  \return -1 on failure
268  */
269 int gvl_file_newh(const char *name, IFLAG file_type)
270 {
271  geovol_file *new;
272  static int first = 1;
273  int i, id;
274  void *m;
275  IFLAG data_type;
276  double min, max;
277 
278  if (first) {
279  if (0 > init_volfiles()) {
280  return (-1);
281  }
282 
283  first = 0;
284  }
285 
286  if (0 <= (id = find_datah(name, file_type, 1))) {
287  for (i = 0; i < Numfiles; i++) {
288  if (Data[i]->data_id == id) {
289  new = Data[i];
290  new->count++;
291 
292  return (id);
293  }
294  }
295  }
296 
297  if (0 > check_num_volfiles()) {
298  return (-1);
299  }
300 
301  if (!name) {
302  return (-1);
303  }
304 
305  if ((m = open_volfile(name, file_type, &data_type, &min, &max)) == NULL) {
306  return (-1);
307  }
308 
309  new = Data[Numfiles];
310 
311  if (new) {
312  Numfiles++;
313  new->data_id = Cur_id++;
314 
315  new->file_name = G_store(name);
316  new->file_type = file_type;
317  new->count = 1;
318  new->map = m;
319  new->min = min;
320  new->max = max;
321  new->data_type = data_type;
322 
323  new->status = STATUS_READY;
324  new->buff = NULL;
325 
326  new->mode = 255;
328 
329  return (new->data_id);
330  }
331 
332  return (-1);
333 }
334 
335 /*!
336  \brief Free allocated buffers
337 
338  \param vf pointer to geovol_file struct
339 
340  \return 1
341  */
343 {
344  if (vf->mode == MODE_SLICE) {
345  G_free(vf->buff);
346  vf->buff = NULL;
347  }
348 
349  if (vf->mode == MODE_PRELOAD) {
350  G_free(vf->buff);
351  vf->buff = NULL;
352  }
353 
354  return (1);
355 }
356 
357 /*!
358  \brief Free geovol_file structure for given handle
359 
360  \param id
361 
362  \return
363  */
365 {
366  int i, j, found = -1;
367  geovol_file *fvf;
368 
369  G_debug(5, "gvl_file_free_datah(): id=%d", id);
370 
371  for (i = 0; i < Numfiles; i++) {
372  if (Data[i]->data_id == id) {
373  found = 1;
374  fvf = Data[i];
375 
376  if (fvf->count > 1) {
377  fvf->count--;
378  }
379  else {
380  close_volfile(fvf->map, fvf->file_type);
381  free_volfile_buffs(fvf);
382 
383  G_free(fvf->file_name);
384  fvf->file_name = NULL;
385 
386  fvf->data_id = 0;
387 
388  for (j = i; j < (Numfiles - 1); j++) {
389  Data[j] = Data[j + 1];
390  }
391 
392  Data[j] = fvf;
393 
394  --Numfiles;
395  }
396  }
397  }
398 
399  return (found);
400 }
401 
402 /******************************************************************/
403 /* reading from RASTER3D raster volume files */
404 
405 /******************************************************************/
406 
407 /*!
408  \brief Open 3d raster file
409 
410  \param filename file name
411  \param type data type
412  \param[out] min min value
413  \param[out] max max value
414 
415  \returns pointer to data
416  */
417 void *open_g3d_file(const char *filename, IFLAG *type, double *min, double *max)
418 {
419  const char *mapset;
420  int itype;
421  void *map;
422 
423  /* search for g3d file a return his mapset */
424  mapset = G_find_raster3d(filename, "");
425  if (!mapset) {
426  G_warning(_("3D raster map <%s> not found"), filename);
427  return (NULL);
428  }
429 
430  /* open g3d file */
431  map = Rast3d_open_cell_old(filename, mapset, RASTER3D_DEFAULT_WINDOW,
434  if (!map) {
435  G_warning(_("Unable to open 3D raster map <%s>"), filename);
436  return (NULL);
437  }
438 
439  /* load range into range structure of map */
440  if (!Rast3d_range_load(map)) {
441  G_warning(_("Unable to read range of 3D raster map <%s>"), filename);
442  return (NULL);
443  }
444 
446 
447  /* get file data type */
448  itype = Rast3d_file_type_map(map);
449  if (itype == FCELL_TYPE)
450  *type = VOL_DTYPE_FLOAT;
451  if (itype == DCELL_TYPE)
452  *type = VOL_DTYPE_DOUBLE;
453 
454  return (map);
455 }
456 
457 /*!
458  \brief Close g3d file
459 
460  \param map 3d raster map
461 
462  \return -1 on failure
463  \return 1 on success
464  */
465 int close_g3d_file(void *map)
466 {
467  /* close opened g3d file */
468  if (Rast3d_close((RASTER3D_Map *)map) != 1) {
469  G_warning(_("Unable to close 3D raster map <%s>"),
470  ((RASTER3D_Map *)map)->fileName);
471  return (-1);
472  }
473 
474  return (1);
475 }
476 
477 /*!
478  \brief Eead value from g3d file
479 
480  \param type data type
481  \param map 3D raster map
482  \param x,y,z real coordinates
483  \param[out] value data value
484 
485  \return -1 on failure
486  \return 1 on success
487  */
488 int read_g3d_value(IFLAG type, void *map, int x, int y, int z, void *value)
489 {
490  switch (type) {
491  /* float data type */
492  case (VOL_DTYPE_FLOAT):
493  *((float *)value) = Rast3d_get_float(map, x, y, z);
494  break;
495 
496  /* double data type */
497  case (VOL_DTYPE_DOUBLE):
498  *((double *)value) = Rast3d_get_double(map, x, y, z);
499  break;
500 
501  /* unsupported data type */
502  default:
503  return (-1);
504  }
505 
506  return (1);
507 }
508 
509 /*!
510  \brief Read slice of values at level from g3d file
511 
512  \param type data type
513  \param map 3D raster map
514  \param level
515  \param[out] data
516 
517  \return -1 on failure
518  \return 0 on success
519  */
520 int read_g3d_slice(IFLAG type, void *map, int level, void *data)
521 {
522  int x, y;
523 
524  switch (type) {
525  /* float data type */
526  case (VOL_DTYPE_FLOAT):
527  for (x = 0; x < Cols; x++) {
528  for (y = 0; y < Rows; y++) {
529  ((float *)data)[x + y * Cols] =
530  Rast3d_get_float(map, x, y, level);
531  }
532  }
533 
534  break;
535 
536  /* double data type */
537  case (VOL_DTYPE_DOUBLE):
538  for (x = 0; x < Cols; x++) {
539  for (y = 0; y < Rows; y++) {
540  ((double *)data)[x + y * Cols] =
541  Rast3d_get_double(map, x, y, level);
542  }
543  }
544 
545  break;
546 
547  /* unsupported data type */
548  default:
549  return (-1);
550  }
551 
552  return (1);
553 }
554 
555 /*!
556  \brief Read all values from g3d file
557 
558  \param type data type
559  \param map 3D raster map
560  \param[out] data data buffer
561 
562  \return -1 on failure
563  \return 1 on success
564  */
565 int read_g3d_vol(IFLAG type, void *map, void *data)
566 {
567  int x, y, z;
568 
569  switch (type) {
570  /* float data type */
571  case (VOL_DTYPE_FLOAT):
572  for (x = 0; x < Cols; x++) {
573  for (y = 0; y < Rows; y++) {
574  for (z = 0; z < Depths; z++) {
575  ((float *)data)[x + y * Cols + z * Rows * Cols] =
576  Rast3d_get_float(map, x, y, z);
577  }
578  }
579  }
580 
581  break;
582 
583  /* double data type */
584  case (VOL_DTYPE_DOUBLE):
585  for (x = 0; x < Cols; x++) {
586  for (y = 0; y < Rows; y++) {
587  for (z = 0; z < Depths; z++) {
588  ((double *)data)[x + y * Cols + z * Rows * Cols] =
589  Rast3d_get_double(map, x, y, z);
590  }
591  }
592  }
593 
594  break;
595 
596  /* unsupported data type */
597  default:
598  return (-1);
599  }
600 
601  return (1);
602 }
603 
604 /*!
605  \brief Check for null value
606 
607  \param type data type
608  \param value
609 
610  \return 1 if value is null
611  \return 0 if value is not null
612  \return -1 on failure (unsupported data type
613  */
614 int is_null_g3d_value(IFLAG type, void *value)
615 {
616  switch (type) {
617  /* float data type */
618  case (VOL_DTYPE_FLOAT):
619  return Rast3d_is_null_value_num(value, FCELL_TYPE);
620  break;
621 
622  /* double data type */
623  case (VOL_DTYPE_DOUBLE):
624  return Rast3d_is_null_value_num(value, DCELL_TYPE);
625  break;
626 
627  /* unsupported data type */
628  default:
629  return (-1);
630  }
631 
632  return (-1);
633 }
634 
635 /******************************************************************/
636 /* reading from buffer */
637 
638 /******************************************************************/
639 
640 /*!
641  \brief Get value from buffer
642 
643  \param type data type
644  \param data data buffer
645  \param offset
646  \param value
647 
648  \return -1 on failure (unsupported data type)
649  \return 1 on success
650  */
651 int get_buff_value(IFLAG type, void *data, int offset, void *value)
652 {
653  switch (type) {
654  /* float data type */
655  case (VOL_DTYPE_FLOAT):
656  *((float *)value) = ((float *)data)[offset];
657  break;
658 
659  /* double data type */
660  case (VOL_DTYPE_DOUBLE):
661  *((double *)value) = ((double *)data)[offset];
662  break;
663 
664  /* unsupported data type */
665  default:
666  return (-1);
667  }
668 
669  return (1);
670 }
671 
672 /******************************************************************/
673 /* direct mode reading from volume file */
674 
675 /******************************************************************/
676 
677 /*!
678  \brief Read value direct from volume file
679 
680  \param vf pointer to geovol_file struct
681  \param x,y,z real point
682  \param[out] value data value
683 
684  \return -1 on failure
685  \return 1 on success
686  */
687 int get_direct_value(geovol_file *vf, int x, int y, int z, void *value)
688 {
689  switch (vf->file_type) {
690  /* RASTER3D file type */
691  case (VOL_FTYPE_RASTER3D):
692  if (0 > read_g3d_value(vf->data_type, vf->map, x, y, z, value))
693  return (-1);
694  break;
695 
696  default:
697  return (-1);
698  }
699 
700  return (1);
701 }
702 
703 /******************************************************************/
704 /* full mode reading from volume file */
705 
706 /******************************************************************/
707 
708 /*!
709  \brief Allocate buffer memory for full mode reading
710 
711  \param vf pointer to geovol_file
712 
713  \return -1 on failure
714  \return 1 on success
715  */
717 {
718  switch (vf->data_type) {
719  /* float data type */
720  case (VOL_DTYPE_FLOAT):
721  if ((vf->buff = (float *)G_malloc(sizeof(float) * Cols * Rows *
722  Depths)) == NULL)
723  return (-1);
724  break;
725 
726  /* double data type */
727  case (VOL_DTYPE_DOUBLE):
728  if ((vf->buff = (double *)G_malloc(sizeof(double) * Cols * Rows *
729  Depths)) == NULL)
730  return (-1);
731  break;
732 
733  /* unsupported data type */
734  default:
735  return (-1);
736  }
737 
738  return (1);
739 }
740 
741 /*!
742  \brief Free memory buffer memory
743 
744  \param vf pointer to geovol_file struct
745 
746  \return 1
747  */
749 {
750  G_free(vf->buff);
751 
752  return (1);
753 }
754 
755 /*!
756  \brief Read all values from volume file
757 
758  \param vf pointer to geovol_file struct
759 
760  \return -1 on failure
761  \return 1 on success
762  */
764 {
765  switch (vf->file_type) {
766  /* RASTER3D file format */
767  case (VOL_FTYPE_RASTER3D):
768  if (0 > read_g3d_vol(vf->data_type, vf->map, vf->buff))
769  return (-1);
770  break;
771  /* unsupported file format */
772  default:
773  return (-1);
774  }
775 
776  return (1);
777 }
778 
779 /*!
780  \brief Get value from volume buffer
781 
782  \param vf pointer to geovol_file struct
783  \param x,y,z real point
784  \param[out] value data value
785 
786  \return 1
787  */
788 int get_vol_value(geovol_file *vf, int x, int y, int z, void *value)
789 {
790  get_buff_value(vf->data_type, vf->buff, z * Rows * Cols + y * Cols + x,
791  value);
792 
793  return (1);
794 }
795 
796 /******************************************************************/
797 /* slice mode reading from volume file */
798 
799 /******************************************************************/
800 
801 /*!
802  \brief Allocate buffer for slice mode reading
803 
804  \param vf pointer to geovol_file struct
805 
806  \return -1 on failure
807  \return 1 on success
808  */
810 {
811  int i;
812  slice_data *sd = (slice_data *)vf->buff;
813 
814  switch (vf->data_type) {
815  /* float data type */
816  case (VOL_DTYPE_FLOAT):
817  for (i = 0; i < sd->num; i++) {
818  if ((sd->slice[i] =
819  (float *)G_malloc(sizeof(float) * Cols * Rows)) == NULL)
820  return (-1);
821  }
822  break;
823 
824  /* double data type */
825  case (VOL_DTYPE_DOUBLE):
826  for (i = 0; i < sd->num; i++) {
827  if ((sd->slice[i] =
828  (double *)G_malloc(sizeof(double) * Cols * Rows)) == NULL)
829  return (-1);
830  }
831  break;
832 
833  /* unsupported data type */
834  default:
835  return (-1);
836  }
837 
838  return (1);
839 }
840 
841 /*!
842  \brief Free buffer for slice mode reading
843 
844  \param vf pointer to geovol_file struct
845 
846  \return 1
847  */
849 {
850  int i;
851  slice_data *sd = (slice_data *)vf->buff;
852 
853  for (i = 0; i < sd->num; i++) {
854  G_free(sd->slice[i]);
855  }
856 
857  return (1);
858 }
859 
860 /*!
861  \brief Read slice of values at level from volume file
862 
863  \param vf pointer to geovol_file struct
864  \param s
865  \param l
866 
867  \return -1 on failure
868  \return 1 on success
869  */
870 int read_slice(geovol_file *vf, int s, int l)
871 {
872  /* get slice structure */
873  slice_data *sd = (slice_data *)vf->buff;
874 
875  switch (vf->file_type) {
876  /* RASTER3D file format */
877  case (VOL_FTYPE_RASTER3D):
878  if (0 > read_g3d_slice(vf->data_type, vf->map, l, sd->slice[s]))
879  return (-1);
880  break;
881  /* unsupported file format */
882  default:
883  return (-1);
884  }
885 
886  return (1);
887 }
888 
889 /*!
890  \brief Read new slice into buffer
891 
892  \param vf pointer to geovol_file struct
893  */
895 {
896  void *tmp;
897  int i;
898 
899  slice_data *sd = (slice_data *)vf->buff;
900 
901  /* change pointers to slices */
902  tmp = sd->slice[0];
903  for (i = 0; i < sd->num - 1; i++) {
904  sd->slice[i] = sd->slice[i + 1];
905  }
906  sd->slice[sd->num - 1] = tmp;
907 
908  /* read new slice data */
909  read_slice(vf, sd->num, sd->crnt + 1 + (sd->num - sd->base));
910 
911  /* increase current slice value */
912  sd->crnt++;
913 }
914 
915 /*!
916  \brief Get value from slice buffer
917 
918  \param vf pointer to geovol_file struct
919  \param x,y,z real point
920  \param[out] value data value
921 
922  \return -1 on failure
923  \return 1 on success
924  */
925 int get_slice_value(geovol_file *vf, int x, int y, int z, void *value)
926 {
927  slice_data *sd = (slice_data *)vf->buff;
928 
929  /* value is in loaded slices */
930  if ((z >= sd->crnt - (sd->base - 1)) &&
931  (z <= sd->crnt + sd->num - sd->base)) {
932 
933  get_buff_value(vf->data_type, sd->slice[(z - sd->crnt)], y * Cols + x,
934  value);
935  }
936 
937  /* if value isn't in loaded slices, need read new data slice */
938  else if (z == sd->crnt - (sd->base - 1) + 1) {
939  shift_slices(vf);
940  get_buff_value(vf->data_type, sd->slice[(z - sd->crnt)], y * Cols + x,
941  value);
942  }
943 
944  /* value out of range */
945  else {
946  return (-1);
947  }
948 
949  return (1);
950 }
951 
952 /******************************************************************/
953 /* reading from volume file */
954 
955 /******************************************************************/
956 
957 /*!
958  \brief Start read - allocate memory buffer a read first data into buffer
959 
960  \param vf pointer to geovol_file struct
961 
962  \return -1 on failure
963  \return 1 on success
964  */
966 {
967  int i;
968  slice_data *sd;
969 
970  /* check status */
971  if (vf->status == STATUS_BUSY)
972  return (-1);
973 
974  switch (vf->mode) {
975  /* read whole volume into memory */
976  case (MODE_FULL):
977  /* allocate memory */
978  if (0 > alloc_vol_buff(vf))
979  return (-1);
980 
981  /* read volume */
982  read_vol(vf);
983  break;
984 
985  /* read first data slices into memory */
986  case (MODE_SLICE):
987  /* allocate slices buffer memory */
988  if (0 > alloc_slice_buff(vf))
989  return (-1);
990 
991  /* read volume */
992  sd = (slice_data *)vf->buff;
993  /* set current slice to 0 */
994  sd->crnt = 0;
995 
996  /* read first slices into buffer */
997  for (i = 0; i < (sd->num - sd->base + 1); i++)
998  read_slice(vf, (sd->base - 1) + i, i);
999  break;
1000  }
1001 
1002  /* set status */
1003  vf->status = STATUS_BUSY;
1004 
1005  return (1);
1006 }
1007 
1008 /*!
1009  \brief End read - free buffer memory
1010 
1011  \param vf pointer to geovol_file struct
1012 
1013  \return -1 on failure
1014  \return 1 on success
1015  */
1017 {
1018  /* check status */
1019  if (vf->status == STATUS_READY)
1020  return (-1);
1021 
1022  switch (vf->mode) {
1023  case (MODE_FULL):
1024  if (0 > free_vol_buff(vf))
1025  return (-1);
1026  break;
1027 
1028  case (MODE_SLICE):
1029  /* allocate slices buffer memory */
1030  if (0 > free_slice_buff(vf))
1031  return (-1);
1032  }
1033 
1034  /* set status */
1035  vf->status = STATUS_READY;
1036 
1037  return (1);
1038 }
1039 
1040 /*!
1041  \brief Get value for volume file at x, y, z
1042 
1043  \param vf pointer to geovol_file struct
1044 
1045  \return -1 on failure
1046  \return 1 on success
1047  */
1048 int gvl_file_get_value(geovol_file *vf, int x, int y, int z, void *value)
1049 {
1050  /* check status */
1051  if (vf->status != STATUS_BUSY) {
1052  return (-1);
1053  }
1054 
1055  switch (vf->mode) {
1056  /* read value direct from g3d file */
1057  case (MODE_DIRECT):
1058  if (0 > get_direct_value(vf, x, y, z, value))
1059  return (-1);
1060  break;
1061 
1062  case (MODE_SLICE):
1063  if (0 > get_slice_value(vf, x, y, z, value))
1064  return (-1);
1065  break;
1066 
1067  case (MODE_FULL):
1068  case (MODE_PRELOAD):
1069  if (0 > get_vol_value(vf, x, y, z, value))
1070  return (-1);
1071  break;
1072  }
1073  return (1);
1074 }
1075 
1076 /*!
1077  \brief Check for null value
1078 
1079  \param vf pointer to geovol_file struct
1080  \param value data value
1081 
1082  \return -1 on failure
1083  \return 1 on success
1084  */
1086 {
1087  switch (vf->file_type) {
1088  /* RASTER3D file format */
1089  case (VOL_FTYPE_RASTER3D):
1090  return is_null_g3d_value(vf->file_type, value);
1091  break;
1092  /* unsupported file format */
1093  default:
1094  return (-1);
1095  }
1096 
1097  return (-1);
1098 }
1099 
1100 /******************************************************************/
1101 /* set parameters for reading volumes */
1102 
1103 /******************************************************************/
1104 
1105 /*!
1106  \brief Set read mode
1107 
1108  \param vf pointer to geovol_file struct
1109  \param mode read mode
1110 
1111  \return -1 on failure
1112  \return 1 on success
1113  */
1115 {
1116  slice_data *sd;
1117 
1118  if (vf->status == STATUS_BUSY)
1119  return (-1);
1120 
1121  if (vf->mode == mode)
1122  return (1);
1123 
1124  if (vf->mode == MODE_SLICE)
1125  G_free(vf->buff);
1126 
1127  if (vf->mode == MODE_PRELOAD)
1128  G_free(vf->buff);
1129 
1130  if (mode == MODE_SLICE) {
1131  if ((vf->buff = (slice_data *)G_malloc(sizeof(slice_data))) == NULL)
1132  return (-1);
1133 
1134  sd = (slice_data *)vf->buff;
1135  sd->num = 1;
1136  sd->crnt = 0;
1137  sd->base = 1;
1138  }
1139 
1140  if (mode == MODE_PRELOAD) {
1141  /* allocate memory */
1142  if (0 > alloc_vol_buff(vf))
1143  return (-1);
1144 
1145  /* read volume */
1146  read_vol(vf);
1147  }
1148 
1149  vf->mode = mode;
1150 
1151  return (1);
1152 }
1153 
1154 /*!
1155  \brief Set parameters for slice reading
1156 
1157  \param vf pointer to geovol_file struct
1158  \param n
1159  \param b
1160 
1161  \return -1 on failure
1162  \return 1 on success
1163  */
1165 {
1166  slice_data *sd;
1167 
1168  if (vf->status == STATUS_BUSY)
1169  return (-1);
1170 
1171  if (!(vf->mode == MODE_SLICE))
1172  return (-1);
1173 
1174  sd = (slice_data *)vf->buff;
1175  sd->num = n;
1176  sd->base = b;
1177 
1178  return (1);
1179 }
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition: defs/gis.h:94
const char * G_find_raster3d(const char *, const char *)
Search for a 3D raster map in current search path or in a specified mapset.
Definition: find_rast3d.c:28
int G_debug(int, const char *,...) __attribute__((format(printf
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
void * GVL_get_window(void)
Get window.
Definition: gvl2.c:98
int Rast3d_file_type_map(RASTER3D_Map *)
Returns the type with which tiles of map are stored on file.
Definition: headerinfo.c:276
void * Rast3d_open_cell_old(const char *, const char *, RASTER3D_Region *, int, int)
Opens existing g3d-file name in mapset. Tiles are stored in memory with type which must be any of FCE...
Definition: raster3d/open.c:80
int Rast3d_is_null_value_num(const void *, int)
Definition: null.c:12
int Rast3d_range_load(RASTER3D_Map *)
Loads the range into the range structure of map.
double Rast3d_get_double(RASTER3D_Map *, int, int, int)
Is equivalent to Rast3d_get_value (map, x, y, z, &value, DCELL_TYPE); return value.
Definition: getvalue.c:68
int Rast3d_close(RASTER3D_Map *)
Close 3D raster map files.
float Rast3d_get_float(RASTER3D_Map *, int, int, int)
Is equivalent to Rast3d_get_value (map, x, y, z, &value, FCELL_TYPE); return value.
Definition: getvalue.c:44
void Rast3d_range_min_max(RASTER3D_Map *, double *, double *)
Returns in min and max the minimum and maximum values of the range.
#define min(x, y)
Definition: draw2.c:29
#define max(x, y)
Definition: draw2.c:30
#define GPATH_MAX
Definition: gis.h:194
#define _(str)
Definition: glocale.h:10
int Rows
Definition: gvl_calc.c:79
int Cols
Definition: gvl_calc.c:79
int Depths
Definition: gvl_calc.c:79
int close_g3d_file(void *)
Close g3d file.
Definition: gvl_file.c:465
int read_g3d_value(IFLAG type, void *map, int x, int y, int z, void *value)
Eead value from g3d file.
Definition: gvl_file.c:488
int gvl_file_free_datah(int id)
Free geovol_file structure for given handle.
Definition: gvl_file.c:364
int gvl_file_get_data_type(geovol_file *vf)
Get data type for given handle.
Definition: gvl_file.c:202
#define MODE_PRELOAD
Definition: gvl_file.c:33
int close_volfile(void *map, IFLAG type)
Close volume file.
Definition: gvl_file.c:251
int gvl_file_end_read(geovol_file *vf)
End read - free buffer memory.
Definition: gvl_file.c:1016
void shift_slices(geovol_file *vf)
Read new slice into buffer.
Definition: gvl_file.c:894
int get_vol_value(geovol_file *vf, int x, int y, int z, void *value)
Get value from volume buffer.
Definition: gvl_file.c:788
int get_buff_value(IFLAG type, void *data, int offset, void *value)
Get value from buffer.
Definition: gvl_file.c:651
int gvl_file_newh(const char *name, IFLAG file_type)
Get handle for given file name and type.
Definition: gvl_file.c:269
int read_vol(geovol_file *vf)
Read all values from volume file.
Definition: gvl_file.c:763
void gvl_file_get_min_max(geovol_file *vf, double *min, double *max)
Get minimum and maximum value in volume file.
Definition: gvl_file.c:214
geovol_file * gvl_file_get_volfile(int id)
Get geovol_file structure for given handle.
Definition: gvl_file.c:117
char * gvl_file_get_name(int id)
Get file name for given handle.
Definition: gvl_file.c:165
#define LUCKY
Definition: gvl_file.c:28
int get_direct_value(geovol_file *vf, int x, int y, int z, void *value)
Read value direct from volume file.
Definition: gvl_file.c:687
int gvl_file_set_mode(geovol_file *vf, IFLAG mode)
Set read mode.
Definition: gvl_file.c:1114
int get_slice_value(geovol_file *vf, int x, int y, int z, void *value)
Get value from slice buffer.
Definition: gvl_file.c:925
int free_slice_buff(geovol_file *vf)
Free buffer for slice mode reading.
Definition: gvl_file.c:848
#define STATUS_READY
Definition: gvl_file.c:37
int alloc_slice_buff(geovol_file *vf)
Allocate buffer for slice mode reading.
Definition: gvl_file.c:809
#define MODE_FULL
Definition: gvl_file.c:32
int gvl_file_set_slices_param(geovol_file *vf, int n, int b)
Set parameters for slice reading.
Definition: gvl_file.c:1164
int read_g3d_slice(IFLAG type, void *map, int level, void *data)
Read slice of values at level from g3d file.
Definition: gvl_file.c:520
int free_vol_buff(geovol_file *vf)
Free memory buffer memory.
Definition: gvl_file.c:748
int gvl_file_get_value(geovol_file *vf, int x, int y, int z, void *value)
Get value for volume file at x, y, z.
Definition: gvl_file.c:1048
int gvl_file_start_read(geovol_file *vf)
Start read - allocate memory buffer a read first data into buffer.
Definition: gvl_file.c:965
void * open_g3d_file(const char *, IFLAG *, double *, double *)
Open 3d raster file.
Definition: gvl_file.c:417
#define MODE_DIRECT
Definition: gvl_file.c:30
#define MODE_DEFAULT
Definition: gvl_file.c:35
int find_datah(const char *name, IFLAG type, int begin)
Find file with name and type in geovol_file array an return handle.
Definition: gvl_file.c:139
#define STATUS_BUSY
Definition: gvl_file.c:38
void * open_volfile(const char *name, IFLAG file_type, IFLAG *data_type, double *min, double *max)
Open 3d raster file.
Definition: gvl_file.c:232
int is_null_g3d_value(IFLAG type, void *value)
Check for null value.
Definition: gvl_file.c:614
int gvl_file_get_file_type(geovol_file *vf)
Get file type for given handle.
Definition: gvl_file.c:190
int free_volfile_buffs(geovol_file *vf)
Free allocated buffers.
Definition: gvl_file.c:342
int gvl_file_is_null_value(geovol_file *vf, void *value)
Check for null value.
Definition: gvl_file.c:1085
#define MODE_SLICE
Definition: gvl_file.c:31
int read_slice(geovol_file *vf, int s, int l)
Read slice of values at level from volume file.
Definition: gvl_file.c:870
int read_g3d_vol(IFLAG type, void *map, void *data)
Read all values from g3d file.
Definition: gvl_file.c:565
int alloc_vol_buff(geovol_file *vf)
Allocate buffer memory for full mode reading.
Definition: gvl_file.c:716
const char * name
Definition: named_colr.c:6
#define MAX_VOL_SLICES
Definition: ogsf.h:52
#define VOL_DTYPE_FLOAT
Definition: ogsf.h:134
#define IFLAG
Definition: ogsf.h:71
#define VOL_FTYPE_RASTER3D
Definition: ogsf.h:131
#define VOL_DTYPE_DOUBLE
Definition: ogsf.h:135
#define MAX_VOL_FILES
Definition: ogsf.h:53
#define strcpy
Definition: parson.c:62
double b
Definition: r_raster.c:39
double l
Definition: r_raster.c:39
#define RASTER3D_DEFAULT_WINDOW
Definition: raster3d.h:29
#define RASTER3D_USE_CACHE_DEFAULT
Definition: raster3d.h:19
#define RASTER3D_TILE_SAME_AS_FILE
Definition: raster3d.h:11
#define FCELL_TYPE
Definition: raster.h:12
#define DCELL_TYPE
Definition: raster.h:13
return(psObject)
if(!(yy_init))
Definition: sqlp.yy.c:775
char * file_name
Definition: ogsf.h:386
IFLAG file_type
Definition: ogsf.h:384
void * map
Definition: ogsf.h:389
IFLAG data_type
Definition: ogsf.h:388
IFLAG mode
Definition: ogsf.h:393
IFLAG status
Definition: ogsf.h:392
double min
Definition: ogsf.h:390
unsigned int count
Definition: ogsf.h:385
double max
Definition: ogsf.h:390
void * buff
Definition: ogsf.h:395
int data_id
Definition: ogsf.h:383
#define x