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