GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvl.c
Go to the documentation of this file.
1 
20 #include <stdlib.h>
21 
22 #include <grass/gis.h>
23 #include <grass/gstypes.h>
24 
25 #include "gsget.h"
26 
27 #define FIRST_VOL_ID 81721
28 
29 static geovol *Vol_top = NULL;
30 
39 geovol *gvl_get_vol(int id)
40 {
41  geovol *gvl;
42 
43  G_debug(5, "gvl_get_vol():");
44 
45  for (gvl = Vol_top; gvl; gvl = gvl->next) {
46  if (gvl->gvol_id == id) {
47  G_debug(5, " id=%d", id);
48  return (gvl);
49  }
50  }
51 
52  return (NULL);
53 }
54 
63 geovol *gvl_get_prev_vol(int id)
64 {
65  geovol *pv;
66 
67  G_debug(5, "gvl_get_prev_vol");
68 
69  for (pv = Vol_top; pv; pv = pv->next) {
70  if (pv->gvol_id == id - 1) {
71  return (pv);
72  }
73  }
74 
75  return (NULL);
76 }
77 
85 int gvl_getall_vols(geovol ** gvols)
86 {
87  geovol *gvl;
88  int i;
89 
90  G_debug(5, "gvl_getall_vols");
91 
92  for (i = 0, gvl = Vol_top; gvl; gvl = gvl->next, i++) {
93  gvols[i] = gvl;
94  }
95 
96  return (i);
97 }
98 
104 int gvl_num_vols(void)
105 {
106  geovol *gvl;
107  int i;
108 
109  for (i = 0, gvl = Vol_top; gvl; gvl = gvl->next, i++) ;
110 
111  G_debug(5, "gvl_num_vols(): num=%d", i);
112 
113  return (i);
114 }
115 
122 geovol *gvl_get_last_vol(void)
123 {
124  geovol *lvl;
125 
126  G_debug(5, "gvl_get_last_vol");
127 
128  if (!Vol_top) {
129  return (NULL);
130  }
131 
132  for (lvl = Vol_top; lvl->next; lvl = lvl->next) ;
133 
134  G_debug(5, " last vol id: %d", lvl->gvol_id);
135 
136  return (lvl);
137 }
138 
145 geovol *gvl_get_new_vol(void)
146 {
147  geovol *nvl, *lvl;
148 
149  G_debug(5, "gvl_get_new_vol()");
150 
151  nvl = (geovol *) G_malloc(sizeof(geovol)); /* G_fatal_error */
152  if (!nvl) {
153  return (NULL);
154  }
155 
156  if ((lvl = gvl_get_last_vol())) {
157  lvl->next = nvl;
158  nvl->gvol_id = lvl->gvol_id + 1;
159  }
160  else {
161  Vol_top = nvl;
162  nvl->gvol_id = FIRST_VOL_ID;
163  }
164 
165  nvl->next = NULL;
166 
167  G_debug(5, " id=%d", nvl->gvol_id);
168 
169  return (nvl);
170 }
171 
184 int gvl_init_vol(geovol * gvl, double ox, double oy, double oz,
185  int rows, int cols, int depths, double xres, double yres,
186  double zres)
187 {
188  G_debug(5, "gvl_init_vol() id=%d", gvl->gvol_id);
189 
190  if (!gvl) {
191  return (-1);
192  }
193 
194  gvl->ox = ox;
195  gvl->oy = oy;
196  gvl->oz = oz;
197  gvl->rows = rows;
198  gvl->cols = cols;
199  gvl->depths = depths;
200  gvl->xres = xres;
201  gvl->yres = yres;
202  gvl->zres = zres;
203 
204  gvl->xmin = ox;
205  gvl->xmax = ox + (cols - 1) * xres;
206  gvl->xrange = gvl->xmax - gvl->xmin;
207  gvl->ymin = oy;
208  gvl->ymax = oy + (rows - 1) * yres;
209  gvl->yrange = gvl->ymax - gvl->ymin;
210  gvl->zmin = oz;
211  gvl->zmax = oz + (depths - 1) * zres;
212  gvl->zrange = gvl->zmax - gvl->zmin;
213 
214  gvl->x_trans = gvl->y_trans = gvl->z_trans = 0.0;
215 
216  gvl->n_isosurfs = 0;
217  G_zero(gvl->isosurf, sizeof(geovol_isosurf *) * MAX_ISOSURFS);
218  gvl->isosurf_x_mod = 1;
219  gvl->isosurf_y_mod = 1;
220  gvl->isosurf_z_mod = 1;
221  gvl->isosurf_draw_mode = DM_GOURAUD;
222 
223  gvl->n_slices = 0;
224  G_zero(gvl->slice, sizeof(geovol_slice *) * MAX_SLICES);
225  gvl->slice_x_mod = 1;
226  gvl->slice_y_mod = 1;
227  gvl->slice_z_mod = 1;
228  gvl->slice_draw_mode = DM_GOURAUD;
229 
230  gvl->hfile = -1;
231  gvl->clientdata = NULL;
232 
233  return (1);
234 }
235 
241 void gvl_delete_vol(int id)
242 {
243  geovol *fvl;
244 
245  G_debug(5, "gvl_delete_vol");
246 
247  fvl = gvl_get_vol(id);
248 
249  if (fvl) {
250  gvl_free_vol(fvl);
251  }
252 
253  return;
254 }
255 
264 int gvl_free_vol(geovol * fvl)
265 {
266  geovol *gvl;
267  int found = 0;
268 
269  G_debug(5, "gvl_free_vol");
270 
271  if (Vol_top) {
272  if (fvl == Vol_top) {
273  if (Vol_top->next) {
274  /* can't free top if last */
275  found = 1;
276  Vol_top = fvl->next;
277  }
278  else {
279  gvl_free_volmem(fvl);
280  G_free(fvl);
281  Vol_top = NULL;
282  }
283  }
284  else {
285  for (gvl = Vol_top; gvl && !found; gvl = gvl->next) {
286  /* can't free top */
287  if (gvl->next) {
288  if (gvl->next == fvl) {
289  found = 1;
290  gvl->next = fvl->next;
291  }
292  }
293  }
294  }
295 
296  if (found) {
297  gvl_free_volmem(fvl);
298  G_free(fvl);
299  fvl = NULL;
300  }
301 
302  return (1);
303  }
304 
305  return (-1);
306 }
307 
313 void gvl_free_volmem(geovol * fvl)
314 {
315  if (0 < fvl->hfile)
316  gvl_file_free_datah(fvl->hfile);
317 
318  return;
319 }
320 
326 void print_vol_fields(geovol * gvl)
327 {
328  G_debug(5, "ID: %d", gvl->gvol_id);
329  G_debug(5, "cols: %d rows: %d depths: %d", gvl->cols, gvl->rows,
330  gvl->depths);
331  G_debug(5, "ox: %lf oy: %lf oz: %lf", gvl->ox, gvl->oy, gvl->oz);
332  G_debug(5, "xres: %lf yres: %lf zres: %lf", gvl->xres, gvl->yres,
333  gvl->zres);
334  G_debug(5, "xmin: %f ymin: %f zmin: %f", gvl->xmin, gvl->ymin, gvl->zmin);
335  G_debug(5, "xmax: %f ymax: %f zmax: %f", gvl->xmax, gvl->ymax, gvl->zmax);
336  G_debug(5, "x_trans: %f y_trans: %f z_trans: %f", gvl->x_trans,
337  gvl->y_trans, gvl->z_trans);
338 
339  return;
340 }
341 
351 int gvl_get_xextents(geovol * gvl, float *min, float *max)
352 {
353  *min = gvl->xmin + gvl->x_trans;
354  *max = gvl->xmax + gvl->x_trans;
355 
356  return (1);
357 }
358 
368 int gvl_get_yextents(geovol * gvl, float *min, float *max)
369 {
370  *min = gvl->ymin + gvl->y_trans;
371  *max = gvl->ymax + gvl->y_trans;
372 
373  return (1);
374 }
375 
385 int gvl_get_zextents(geovol * gvl, float *min, float *max)
386 {
387  *min = gvl->zmin + gvl->z_trans;
388  *max = gvl->zmax + gvl->z_trans;
389 
390  return (1);
391 }
392 
401 int gvl_get_xrange(float *min, float *max)
402 {
403  geovol *gvl;
404  float tmin, tmax;
405 
406  if (Vol_top) {
407  gvl_get_xextents(Vol_top, &tmin, &tmax);
408  *min = tmin;
409  *max = tmax;
410  }
411  else {
412  return (-1);
413  }
414 
415  for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
416  gvl_get_xextents(gvl, &tmin, &tmax);
417 
418  if (tmin < *min) {
419  *min = tmin;
420  }
421 
422  if (tmax > *max) {
423  *max = tmax;
424  }
425  }
426 
427  return (1);
428 }
429 
438 int gvl_get_yrange(float *min, float *max)
439 {
440  geovol *gvl;
441  float tmin, tmax;
442 
443  if (Vol_top) {
444  gvl_get_yextents(Vol_top, &tmin, &tmax);
445  *min = tmin;
446  *max = tmax;
447  }
448  else {
449  return (-1);
450  }
451 
452  for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
453  gvl_get_yextents(gvl, &tmin, &tmax);
454 
455  if (tmin < *min) {
456  *min = tmin;
457  }
458 
459  if (tmax > *max) {
460  *max = tmax;
461  }
462  }
463 
464  return (1);
465 }
466 
475 int gvl_get_zrange(float *min, float *max)
476 {
477  geovol *gvl;
478  float tmin, tmax;
479 
480  if (Vol_top) {
481  gvl_get_zextents(Vol_top, &tmin, &tmax);
482  *min = tmin;
483  *max = tmax;
484  }
485  else {
486  return (-1);
487  }
488 
489  for (gvl = Vol_top->next; gvl; gvl = gvl->next) {
490  gvl_get_zextents(gvl, &tmin, &tmax);
491 
492  if (tmin < *min) {
493  *min = tmin;
494  }
495 
496  if (tmax > *max) {
497  *max = tmax;
498  }
499  }
500 
501  return (1);
502 }
503 
504 /************************************************************************/
505 /* ISOSURFACES */
506 
507 /************************************************************************/
508 
517 int gvl_isosurf_init(geovol_isosurf * isosurf)
518 {
519  int i;
520 
521  G_debug(5, "gvl_isosurf_init");
522 
523  if (!isosurf)
524  return (-1);
525 
526  for (i = 0; i < MAX_ATTS; i++) {
527  isosurf->att[i].att_src = NOTSET_ATT;
528  isosurf->att[i].constant = 0.;
529  isosurf->att[i].hfile = -1;
530  isosurf->att[i].user_func = NULL;
531  isosurf->att[i].att_data = NULL;
532  isosurf->att[i].changed = 0;
533  }
534 
535  isosurf->data = NULL;
536  isosurf->data_desc = 0;
537  isosurf->inout_mode = 0;
538 
539  return (1);
540 }
541 
550 int gvl_isosurf_freemem(geovol_isosurf * isosurf)
551 {
552  int i;
553 
554  G_debug(5, "gvl_isosurf_freemem");
555 
556  if (!isosurf)
557  return (-1);
558 
559  for (i = 0; i < MAX_ATTS; i++) {
560  gvl_isosurf_set_att_src(isosurf, i, NOTSET_ATT);
561  }
562 
563  G_free(isosurf->data);
564 
565  return (1);
566 }
567 
577 geovol_isosurf *gvl_isosurf_get_isosurf(int id, int isosurf_id)
578 {
579  geovol *gvl;
580 
581  G_debug(5, "gvl_isosurf_get_isosurf(): id=%d isosurf=%d",
582  id, isosurf_id);
583 
584  gvl = gvl_get_vol(id);
585 
586  if (gvl) {
587  if ((isosurf_id < 0) || (isosurf_id > (gvl->n_isosurfs - 1)))
588  return (NULL);
589 
590  return gvl->isosurf[isosurf_id];
591  }
592 
593  return (NULL);
594 }
595 
605 int gvl_isosurf_get_att_src(geovol_isosurf * isosurf, int desc)
606 {
607  G_debug(5, "isosurf_get_att_src");
608 
609  if (!LEGAL_ATT(desc)) {
610  return (-1);
611  }
612 
613  if (isosurf) {
614  return (isosurf->att[desc].att_src);
615  }
616 
617  return (-1);
618 }
619 
630 int gvl_isosurf_set_att_src(geovol_isosurf * isosurf, int desc, int src)
631 {
632  G_debug(5, "gvl_isosurf_set_att_src");
633 
634  /* check if old source was MAP_ATT, deattach volfile */
635  if (MAP_ATT == gvl_isosurf_get_att_src(isosurf, desc)) {
636  gvl_file_free_datah(isosurf->att[desc].hfile);
637 
638  if (desc == ATT_COLOR) {
639  Gvl_unload_colors_data(isosurf->att[desc].att_data);
640  }
641  }
642 
643  if (isosurf && LEGAL_SRC(src)) {
644  isosurf->att[desc].att_src = src;
645  gvl_isosurf_set_att_changed(isosurf, desc);
646 
647  return (1);
648  }
649 
650  return (-1);
651 }
652 
663 int gvl_isosurf_set_att_const(geovol_isosurf * isosurf, int desc,
664  float constant)
665 {
666  G_debug(5, "gvl_isosurf_set_att_const(): att=%d, const=%f",
667  desc, constant);
668 
669  if (isosurf) {
670  isosurf->att[desc].constant = constant;
671 
672  gvl_isosurf_set_att_src(isosurf, desc, CONST_ATT);
673 
674  return (1);
675  }
676 
677  return (-1);
678 }
679 
690 int gvl_isosurf_set_att_map(geovol_isosurf * isosurf, int desc,
691  const char *filename)
692 {
693  int hfile;
694 
695  G_debug(5, "gvl_isosurf_set_att_map(): att=%d map=%s", desc, filename);
696 
697  if (isosurf) {
698  if (0 > (hfile = gvl_file_newh(filename, VOL_FTYPE_G3D)))
699  return (-1);
700 
701  gvl_isosurf_set_att_src(isosurf, desc, MAP_ATT);
702 
703  isosurf->att[desc].hfile = hfile;
704 
705  if (ATT_COLOR == desc) {
706  Gvl_load_colors_data(&(isosurf->att[desc].att_data), filename);
707  }
708  return (1);
709  }
710 
711  return (-1);
712 }
713 
723 int gvl_isosurf_set_att_changed(geovol_isosurf * isosurf, int desc)
724 {
725  int i;
726 
727  G_debug(5, "gvl_isosurf_set_att_changed");
728 
729  if (isosurf && LEGAL_ATT(desc)) {
730  isosurf->att[desc].changed = 1;
731 
732  if ((desc == ATT_TOPO) || (desc == ATT_MASK)) {
733  for (i = 1; i < MAX_ATTS; i++)
734  isosurf->att[i].changed = 1;
735  }
736 
737  return (1);
738  }
739 
740  return (-1);
741 }
742 
743 /************************************************************************/
744 /* SLICES */
745 
746 /************************************************************************/
747 
756 int gvl_slice_init(geovol_slice * slice)
757 {
758  G_debug(5, "gvl_slice_init");
759 
760  if (!slice)
761  return (-1);
762 
763  slice->data = NULL;
764  slice->changed = 0;
765  slice->mode = 1;
766  slice->transp = 0;
767 
768  slice->z1 = 0;
769  slice->z2 = 99;
770 
771  return (1);
772 }
773 
782 int gvl_slice_freemem(geovol_slice * slice)
783 {
784  G_debug(5, "gvl_slice_freemem");
785 
786  if (!slice)
787  return (-1);
788 
789  G_free(slice->data);
790 
791  return (1);
792 }
793 
803 geovol_slice *gvl_slice_get_slice(int id, int slice_id)
804 {
805  geovol *gvl;
806 
807  gvl = gvl_get_vol(id);
808 
809  if (gvl) {
810  if ((slice_id < 0) || (slice_id > (gvl->n_slices - 1)))
811  return (NULL);
812 
813  return gvl->slice[slice_id];
814  }
815 
816  return (NULL);
817 }
int gvl_get_yrange(float *min, float *max)
Get volume y-range value.
Definition: gvl.c:438
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int gvl_getall_vols(geovol **gvols)
Get all volumes.
Definition: gvl.c:85
geovol * gvl_get_prev_vol(int id)
Get previous volume.
Definition: gvl.c:63
int gvl_get_xrange(float *min, float *max)
Get volume x-range value.
Definition: gvl.c:401
int gvl_num_vols(void)
Get number of loaded volume sets.
Definition: gvl.c:104
#define min(x, y)
Definition: draw2.c:68
int Gvl_unload_colors_data(void *color_data)
Unload color table.
Definition: Gvl3.c:64
#define FIRST_VOL_ID
Definition: gvl.c:27
int gvl_isosurf_set_att_map(geovol_isosurf *isosurf, int desc, const char *filename)
Set attribute map.
Definition: gvl.c:690
int gvl_get_zrange(float *min, float *max)
Get volume z-range value.
Definition: gvl.c:475
#define max(x, y)
Definition: draw2.c:69
int G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:29
int gvl_isosurf_set_att_src(geovol_isosurf *isosurf, int desc, int src)
Set attribute source.
Definition: gvl.c:630
geovol * gvl_get_last_vol(void)
Get last volume set from the list.
Definition: gvl.c:122
geovol * gvl_get_new_vol(void)
Allocate new volume set and add it to the list.
Definition: gvl.c:145
int Gvl_load_colors_data(void **color_data, const char *name)
Load color table.
Definition: Gvl3.c:33
int gvl_slice_init(geovol_slice *slice)
Initialize geovol_slice struct.
Definition: gvl.c:756
int gvl_isosurf_get_att_src(geovol_isosurf *isosurf, int desc)
Get attribute source.
Definition: gvl.c:605
void print_vol_fields(geovol *gvl)
Debug volume fields.
Definition: gvl.c:326
int gvl_file_newh(const char *name, IFLAG file_type)
Get handle for given file name and type.
Definition: gvl_file.c:270
int gvl_isosurf_set_att_changed(geovol_isosurf *isosurf, int desc)
Set attribute changed.
Definition: gvl.c:723
int gvl_init_vol(geovol *gvl, double ox, double oy, double oz, int rows, int cols, int depths, double xres, double yres, double zres)
Initialize geovol structure.
Definition: gvl.c:184
geovol * gvl_get_vol(int id)
Get volume set structure.
Definition: gvl.c:39
geovol_slice * gvl_slice_get_slice(int id, int slice_id)
Get geovol_slice struct.
Definition: gvl.c:803
int gvl_slice_freemem(geovol_slice *slice)
Free geovol_slice struct.
Definition: gvl.c:782
int gvl_free_vol(geovol *fvl)
Free geovol struct.
Definition: gvl.c:264
int gvl_get_xextents(geovol *gvl, float *min, float *max)
Get volume x-extent value.
Definition: gvl.c:351
int gvl_file_free_datah(int id)
Free geovol_file structure for given handle.
Definition: gvl_file.c:365
void gvl_free_volmem(geovol *fvl)
Free geovol struct memory.
Definition: gvl.c:313
int gvl_get_zextents(geovol *gvl, float *min, float *max)
Get volume z-extent value.
Definition: gvl.c:385
int gvl_isosurf_init(geovol_isosurf *isosurf)
Initialize geovol_isosurf struct.
Definition: gvl.c:517
geovol_isosurf * gvl_isosurf_get_isosurf(int id, int isosurf_id)
Get isosurface of given volume set.
Definition: gvl.c:577
int gvl_isosurf_set_att_const(geovol_isosurf *isosurf, int desc, float constant)
Set isosurface attribute constant.
Definition: gvl.c:663
return NULL
Definition: dbfopen.c:1394
int gvl_get_yextents(geovol *gvl, float *min, float *max)
Get volume y-extent value.
Definition: gvl.c:368
int gvl_isosurf_freemem(geovol_isosurf *isosurf)
Free geovol_isosurf struct.
Definition: gvl.c:550
tuple cols
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
void gvl_delete_vol(int id)
Remove volume set from list.
Definition: gvl.c:241