GRASS Programmer's Manual  6.4.4(2014)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
gs.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 #include <stdio.h>
21 
22 #include <grass/gstypes.h>
23 #include <grass/glocale.h>
24 
25 #include "gsget.h"
26 #include "rowcol.h"
27 
28 #define FIRST_SURF_ID 110658
29 
30 static geosurf *Surf_top;
31 static int Invertmask;
32 
33 /***********************************************************************/
34 void gs_err(const char *msg)
35 {
36  G_warning("%s", msg);
37 
38  return;
39 }
40 
47 void gs_init(void)
48 {
49  Surf_top = NULL;
50 
51  return;
52 }
53 
62 geosurf *gs_get_surf(int id)
63 {
64  geosurf *gs;
65 
66  G_debug(5, "gs_get_surf():");
67 
68  for (gs = Surf_top; gs; gs = gs->next) {
69  if (gs->gsurf_id == id) {
70  G_debug(5, " id=%d", id);
71  return (gs);
72  }
73  }
74 
75  return (NULL);
76 }
77 
86 geosurf *gs_get_prev_surface(int id)
87 {
88  geosurf *ps;
89 
90  G_debug(5, "gs_get_prev_surface");
91 
92  for (ps = Surf_top; ps; ps = ps->next) {
93  if (ps->gsurf_id == id - 1) {
94  return (ps);
95  }
96  }
97 
98  return (NULL);
99 }
100 
108 int gs_getall_surfaces(geosurf ** gsurfs)
109 {
110  geosurf *gs;
111  int i;
112 
113  for (i = 0, gs = Surf_top; gs; gs = gs->next, i++) {
114  gsurfs[i] = gs;
115  }
116 
117  G_debug(5, "gs_num_surfaces(): num=%d", i);
118 
119  return (i);
120 }
121 
128 {
129  geosurf *gs;
130  int i;
131 
132  for (i = 0, gs = Surf_top; gs; gs = gs->next, i++) ;
133 
134  G_debug(5, "gs_num_surfaces(): num=%d", i);
135 
136  return (i);
137 }
138 
139 
149 int gs_att_is_set(geosurf * surf, IFLAG att)
150 {
151  geosurf *gs;
152 
153  if (surf) {
154  return (NOTSET_ATT != surf->att[att].att_src);
155  }
156 
157  /* if surf == NULL, look at all surfs */
158  for (gs = Surf_top; gs; gs = gs->next) {
159  if (NOTSET_ATT != gs->att[att].att_src) {
160  return (1);
161  }
162  }
163 
164  return (0);
165 }
166 
172 geosurf *gs_get_last_surface(void)
173 {
174  geosurf *ls;
175 
176  if (!Surf_top) {
177  return (NULL);
178  }
179 
180  for (ls = Surf_top; ls->next; ls = ls->next) ;
181 
182  G_debug(5, "gs_get_last_surface(): last surface id=%d", ls->gsurf_id);
183 
184  return (ls);
185 }
186 
187 
193 geosurf *gs_get_new_surface(void)
194 {
195  geosurf *ns, *ls;
196 
197  ns = (geosurf *) G_malloc(sizeof(geosurf)); /* G_fatal_error */
198  if (!ns) {
199  return (NULL);
200  }
201 
202  if ((ls = gs_get_last_surface())) {
203  ls->next = ns;
204  ns->gsurf_id = ls->gsurf_id + 1;
205  }
206  else {
207  Surf_top = ns;
208  ns->gsurf_id = FIRST_SURF_ID;
209  }
210 
211  ns->next = NULL;
212 
213  G_debug(5, "gs_get_new_surface(): id=%d", ns->gsurf_id);
214 
215  return (ns);
216 }
217 
232 int gs_init_surf(geosurf * gs, double ox, double oy, int rows, int cols,
233  double xres, double yres)
234 {
235  geosurf *ps;
236  int i;
237 
238  if (!gs) {
239  return (-1);
240  }
241 
242  G_debug(5, "gs_init_surf() id=%d", gs->gsurf_id);
243 
244  /* default attributes */
245  for (i = 0; i < MAX_ATTS; i++) {
246  gs->att[i].att_src = NOTSET_ATT;
247  gs->att[i].att_type = ATTY_INT;
248  gs->att[i].hdata = -1;
249  gs->att[i].user_func = NULL;
250  gs->att[i].constant = 0.;
251  gs->att[i].lookup = NULL;
252  gs->att[i].min_nz = gs->att[i].max_nz = gs->att[i].range_nz = 0;
253  gs->att[i].default_null = 0.;
254  }
255 
256  /* default values */
257  gs->ox = ox;
258  gs->oy = oy;
259  gs->rows = rows;
260  gs->cols = cols;
261  gs->xres = xres;
262  gs->yres = yres;
263  gs->x_mod = 2;
264  gs->y_mod = 2;
265  gs->x_modw = rows / 30;
266  gs->y_modw = rows / 30;
267  gs->xmin = ox;
268  gs->xmax = ox + (cols - 1) * xres;
269  gs->xrange = gs->xmax - gs->xmin;
270  gs->ymin = oy;
271  gs->ymax = oy + (rows - 1) * yres;
272  gs->yrange = gs->ymax - gs->ymin;
273  gs->zmin = gs->zmin_nz = gs->zminmasked = 0;
274  gs->zmax = gs->zmax_nz = 0;
275  gs->zrange = gs->zrange_nz = 0;
276  gs->wire_color = 0x00888888;
277  gs->x_trans = gs->y_trans = gs->z_trans = 0.0;
278  gs->nz_topo = gs->nz_color = 0;
279  gs->norm_needupdate = 1;
280  gs->mask_needupdate = 1;
281  gs->curmask = NULL;
282  gs->norms = NULL;
283 
284  gs->draw_mode = DM_GOURAUD;
285 
286  /* default z_exag value */
287  if (gs->gsurf_id == FIRST_SURF_ID) {
288  gs->z_exag = 1.0;
289  }
290  else {
291  ps = gs_get_prev_surface(gs->gsurf_id);
292  gs->z_exag = ps->z_exag;
293  }
294 
295  return (0);
296 }
297 
306 int gs_init_normbuff(geosurf * gs)
307 {
308  long size;
309 
310  if (!gs) {
311  return (0);
312  }
313 
314  if (gs->norms) {
315  G_free(gs->norms);
316  }
317 
318  size = gs->rows * gs->cols * sizeof(unsigned long);
319 
320  gs->norms = (unsigned long *)G_malloc(size); /* G_fatal_error */
321  if (!gs->norms) {
322  return (-1);
323  }
324 
325  gs->norm_needupdate = 1;
326 
327  return (1);
328 }
329 
337 void print_frto(float (*ft)[4])
338 {
339  fprintf(stderr, "FROM: %f, %f, %f\n", ft[FROM][X], ft[FROM][Y],
340  ft[FROM][Z]);
341  fprintf(stderr, "TO: %f, %f, %f\n", ft[TO][X], ft[TO][Y], ft[TO][Z]);
342 
343  return;
344 }
345 
353 void print_realto(float *rt)
354 {
355  fprintf(stderr, "REAL TO: %f, %f, %f\n", rt[X], rt[Y], rt[Z]);
356 
357  return;
358 }
359 
367 void print_256lookup(int *buff)
368 {
369  int i;
370 
371  for (i = 0; i < 256; i++) {
372  if (!(i % 8)) {
373  fprintf(stderr, "\n");
374  }
375 
376  fprintf(stderr, "%x ", buff[i]);
377  }
378 
379  fprintf(stderr, "\n");
380 
381  return;
382 }
383 
391 void print_surf_fields(geosurf * s)
392 {
393  fprintf(stderr, "ID: %d\n", s->gsurf_id);
394  fprintf(stderr, "rows: %d cols: %d\n", s->rows, s->cols);
395  fprintf(stderr, "draw_mode: %x\n", s->draw_mode);
396  fprintf(stderr, "wire_color: %lx\n", s->wire_color);
397  fprintf(stderr, "ox: %lf oy: %lf\n", s->ox, s->oy);
398  fprintf(stderr, "xres: %lf yres: %lf\n", s->xres, s->yres);
399  fprintf(stderr, "z_exag: %f \n", s->z_exag);
400  fprintf(stderr, "x_trans: %f y_trans: %f z_trans: %f\n",
401  s->x_trans, s->y_trans, s->z_trans);
402  fprintf(stderr, "xmin: %f ymin: %f zmin: %f\n",
403  s->xmin, s->ymin, s->zmin);
404  fprintf(stderr, "xmax: %f ymax: %f zmax: %f\n",
405  s->xmax, s->ymax, s->zmax);
406  fprintf(stderr, "x_mod: %d y_mod: %d x_modw: %d y_modw: %d\n",
407  s->x_mod, s->y_mod, s->x_modw, s->y_modw);
408 
409  return;
410 }
411 
419 void print_view_fields(geoview * gv)
420 {
421  fprintf(stderr, "coord_sys: %d\n", gv->coord_sys);
422  fprintf(stderr, "view_proj: %d\n", gv->view_proj);
423  fprintf(stderr, "infocus: %d\n", gv->infocus);
424  print_frto(gv->from_to);
425  fprintf(stderr, "twist: %d fov: %d\n", gv->twist, gv->fov);
426  fprintf(stderr, "incl: %d look: %d\n", gv->incl, gv->look);
427  fprintf(stderr, "real_to: %f %f %f\n",
428  gv->real_to[X], gv->real_to[Y], gv->real_to[Z]);
429  fprintf(stderr, "vert_exag: %f scale: %f \n", gv->vert_exag, gv->scale);
430 
431  return;
432 }
433 
441 void gs_set_defaults(geosurf * gs, float *defs, float *null_defs)
442 {
443  int i;
444 
445  G_debug(5, "gs_set_defaults(): id=%d", gs->gsurf_id);
446 
447  for (i = 0; i < MAX_ATTS; i++) {
448  gs->att[i].constant = defs[i];
449  gs->att[i].default_null = null_defs[i];
450  gs->att[i].lookup = NULL;
451  gs->att[i].hdata = -1;
452  gs->att[i].att_src = NOTSET_ATT;
453  }
454 
455  return;
456 }
457 
463 void gs_delete_surf(int id)
464 {
465  geosurf *fs;
466 
467  G_debug(5, "gs_delete_surf");
468 
469  fs = gs_get_surf(id);
470 
471  if (fs) {
472  gs_free_surf(fs);
473  }
474 
475  return;
476 }
477 
487 int gs_free_surf(geosurf * fs)
488 {
489  geosurf *gs;
490  int found = 0;
491 
492  G_debug(5, "gs_free_surf");
493 
494  if (Surf_top) {
495  if (fs == Surf_top) {
496  if (Surf_top->next) {
497  /* can't free top if last */
498  found = 1;
499  Surf_top = fs->next;
500  }
501  else {
503 
504  if (fs->curmask) {
505  G_free(fs->curmask);
506  }
507 
508  if (fs->norms) {
509  G_free(fs->norms);
510  }
511 
512  G_free(fs);
513  Surf_top = NULL;
514  }
515  }
516  else {
517  for (gs = Surf_top; gs && !found; gs = gs->next) {
518  if (gs->next) {
519  if (gs->next == fs) {
520  found = 1;
521  gs->next = fs->next;
522  }
523  }
524  }
525  }
526 
527  if (found) {
529 
530  if (fs->curmask) {
531  G_free(fs->curmask);
532  }
533 
534  if (fs->norms) {
535  G_free(fs->norms);
536  }
537 
538  G_free(fs);
539  fs = NULL;
540  }
541 
542  return (found);
543  }
544 
545  return (-1);
546 }
547 
557 void gs_free_unshared_buffs(geosurf * fs)
558 {
559  geosurf *gs;
560  int i, j, same;
561  int old_datah;
562 
563  G_debug(5, "gs_free_unshared_buffs");
564 
565  /* for each attribute
566  if !same, free buff
567  */
568  for (i = 0; i < MAX_ATTS; i++) {
569  same = 0;
570 
571  if (0 < (old_datah = fs->att[i].hdata)) {
572  /* for ea att of all other surfs */
573  for (gs = Surf_top; gs; gs = gs->next) {
574  for (j = 0; j < MAX_ATTS; j++) {
575  if ((old_datah == gs->att[j].hdata) && (fs != gs)) {
576  same = 1;
577  }
578  }
579  }
580 
581  if (!same) {
582  gsds_free_datah(old_datah);
583  }
584  }
585  }
586 
587  return;
588 }
589 
598 {
599  geosurf *gs;
600  int ref, j;
601 
602  G_debug(5, "gs_num_datah_reused");
603 
604  /* for each attribute
605  if same, ++reference
606  */
607  /* for ea att of all surfs */
608  ref = 0;
609 
610  for (gs = Surf_top; gs; gs = gs->next) {
611  for (j = 0; j < MAX_ATTS; j++) {
612  if (dh == gs->att[j].hdata) {
613  ref++;
614  }
615  }
616  }
617 
618  return (ref);
619 }
620 
630 int gs_get_att_type(geosurf * gs, int desc)
631 {
632  G_debug(5, "gs_get_att_type");
633 
634  if (!LEGAL_ATT(desc)) {
635  return (-1);
636  }
637 
638  if (gs) {
639  if (gs->att[desc].att_src != NOTSET_ATT) {
640  return (gs->att[desc].att_type);
641  }
642  }
643 
644  return (-1);
645 }
646 
656 int gs_get_att_src(geosurf * gs, int desc)
657 {
658  if (gs)
659  G_debug(5, "gs_get_att_src(): id=%d, desc=%d", gs->gsurf_id, desc);
660  if (!LEGAL_ATT(desc)) {
661  return (-1);
662  }
663 
664  if (gs) {
665  return (gs->att[desc].att_src);
666  }
667 
668  return (-1);
669 }
670 
681 typbuff *gs_get_att_typbuff(geosurf * gs, int desc, int to_write)
682 {
683  typbuff *tb;
684  geosurf *gsref;
685 
686  if (gs) {
687  G_debug(5, "gs_get_att_typbuff(): id=%d desc=%d to_write=%d",
688  gs->gsurf_id, desc, to_write);
689  if ((tb = gsds_get_typbuff(gs->att[desc].hdata, to_write))) {
690  tb->tfunc = NULL;
691 
692  if (desc == ATT_TOPO) {
693  gsref = gsdiff_get_SDref();
694 
695  if (gsref && gsref != gs) {
696  tb->tfunc = gsdiff_do_SD;
697  }
698  }
699 
700  return (tb);
701  }
702  }
703 
704  return (NULL);
705 }
706 
716 int gs_malloc_att_buff(geosurf * gs, int desc, int type)
717 {
718  int hdata, dims[2], ndims;
719 
720  G_debug(5, "gs_malloc_att_buff");
721 
722  if (gs) {
723  if (0 < (hdata = gs->att[desc].hdata)) {
724  dims[0] = gs->rows;
725  dims[1] = gs->cols;
726  ndims = 2;
727  gs_set_att_type(gs, desc, type);
728 
729  return (gsds_alloc_typbuff(hdata, dims, ndims, type));
730  }
731  }
732 
733  return (-1);
734 }
735 
745 int gs_malloc_lookup(geosurf * gs, int desc)
746 {
747  int size;
748 
749  G_debug(5, "gs_malloc_lookup");
750 
751  if (gs) {
752  if (gs->att[desc].lookup) {
753  G_free(gs->att[desc].lookup);
754  gs->att[desc].lookup = NULL;
755  }
756 
757  switch (gs->att[desc].att_type) {
758  case (ATTY_SHORT):
759  size = 32768 * sizeof(int);
760 
761  /* positive integers only, because use as array index */
762  gs->att[desc].lookup = (int *)G_malloc(size); /* G_fatal_error */
763  if (!gs->att[desc].lookup) {
764  return (-1);
765  }
766 
767  break;
768  case (ATTY_CHAR):
769  size = 256 * sizeof(int);
770 
771  /* unsigned char */
772  gs->att[desc].lookup = (int *)G_malloc(size);
773  if (!gs->att[desc].lookup) {
774  return (-1);
775  }
776 
777  break;
778  default:
779  G_warning("bad type: gs_malloc_lookup");
780  return (-1);
781  }
782 
783  if (gs->att[desc].lookup) {
784  return (0);
785  }
786 
787  }
788 
789  return (-1);
790 }
791 
802 int gs_set_att_type(geosurf * gs, int desc, int type)
803 {
804 
805  G_debug(5, "gs_set_att_type(): desc=%d, type=%d", desc, type);
806 
807  if (gs && LEGAL_TYPE(type)) {
808  gs->att[desc].att_type = type;
809 
810  return (0);
811  }
812 
813  return (-1);
814 }
815 
826 int gs_set_att_src(geosurf * gs, int desc, int src)
827 {
828  if (gs)
829  G_debug(5, "gs_set_att_src(): id=%d desc=%d src=%d",
830  gs->gsurf_id, desc, src);
831 
832  /* check if old source was MAP_ATT, free buff */
833  if (MAP_ATT == gs_get_att_src(gs, desc)) {
834  if (1 == gs_num_datah_reused(gs->att[desc].hdata)) {
835  /* only reference */
836  G_debug(5, "gs_set_att_src(): replacing existing map");
837  gsds_free_datah(gs->att[desc].hdata);
838  }
839 
840  if (ATT_TOPO == desc) {
841  if (gs->norms) {
842  G_free(gs->norms);
843  }
844 
845  gs->norms = NULL;
846  gs->norm_needupdate = 0;
847  }
848  }
849 
850  if (gs && LEGAL_SRC(src)) {
851  gs->att[desc].att_src = src;
852 
853  return (0);
854  }
855 
856  return (-1);
857 }
858 
871 int gs_set_att_const(geosurf * gs, int desc, float constant)
872 {
873 
874  if (gs) {
875  G_debug(5, "gs_set_att_const(): id=%d, desc=%d, const=%f",
876  gs->gsurf_id, desc, constant);
877  gs->att[desc].constant = constant;
878 
879  if (ATT_MASK == desc) {
880  gs->mask_needupdate = 1;
881  }
882  else {
883  gs_set_att_src(gs, desc, CONST_ATT);
884  }
885 
886  Gs_update_attrange(gs, desc);
887 
888  return (0);
889  }
890 
891  return (-1);
892 }
893 
899 void gs_set_maskmode(int invert)
900 {
901  Invertmask = invert;
902 
903  return;
904 }
905 
914 int gs_mask_defined(geosurf * gs)
915 {
916  return (gs->att[ATT_MASK].att_src != NOTSET_ATT);
917 }
918 
932 int gs_masked(typbuff * tb, int col, int row, int offset)
933 {
934  int ret;
935 
936  ret = 1;
937 
938  if (tb->bm) {
939  ret = BM_get(tb->bm, col, row);
940  }
941  else if (tb->cb) {
942  ret = tb->cb[offset];
943  }
944  else if (tb->sb) {
945  ret = tb->sb[offset];
946  }
947  else if (tb->ib) {
948  ret = tb->ib[offset];
949  }
950  else if (tb->fb) {
951  ret = tb->fb[offset];
952  }
953 
954  return (Invertmask ? ret : !ret);
955 }
956 
968 int gs_mapcolor(typbuff * cobuff, gsurf_att * coloratt, int offset)
969 {
970  if (coloratt->lookup) {
971  /* for now, but may add larger color lookup capabilities later,
972  so would have to use GET_MAPATT */
973  return (coloratt->lookup[cobuff->cb[offset]]);
974  }
975 
976  return (cobuff->ib[offset]);
977 }
978 
979 /*
980  In the following functions, "extents" refers to translated extents for
981  a single surface, while "range" refers to accumulated extents of all
982  loaded surfaces
983  */
984 
997 int gs_get_zextents(geosurf * gs, float *min, float *max, float *mid)
998 {
999  *min = gs->zmin + gs->z_trans;
1000  *max = gs->zmax + gs->z_trans;
1001  *mid = (*max + *min) / 2.;
1002 
1003  return (1);
1004 }
1005 
1015 int gs_get_xextents(geosurf * gs, float *min, float *max)
1016 {
1017  *min = gs->xmin + gs->x_trans;
1018  *max = gs->xmax + gs->x_trans;
1019 
1020  return (1);
1021 }
1022 
1032 int gs_get_yextents(geosurf * gs, float *min, float *max)
1033 {
1034  *min = gs->ymin + gs->y_trans;
1035  *max = gs->ymax + gs->y_trans;
1036 
1037  return (1);
1038 }
1039 
1040 
1053 int gs_get_zrange0(float *min, float *max)
1054 {
1055  geosurf *gs;
1056 
1057  if (Surf_top) {
1058  *min = Surf_top->zmin;
1059  *max = Surf_top->zmax;
1060  }
1061  else {
1062  return (-1);
1063  }
1064 
1065  for (gs = Surf_top->next; gs; gs = gs->next) {
1066  if (gs->zmin < *min) {
1067  *min = gs->zmin;
1068  }
1069 
1070  if (gs->zmax > *max) {
1071  *max = gs->zmax;
1072  }
1073  }
1074 
1075  return (1);
1076 }
1077 
1087 int gs_get_zrange(float *min, float *max)
1088 {
1089  geosurf *gs;
1090  float tmin, tmax, tmid;
1091 
1092  if (Surf_top) {
1093  gs_get_zextents(Surf_top, &tmin, &tmax, &tmid);
1094  *min = tmin;
1095  *max = tmax;
1096  }
1097  else {
1098  return (-1);
1099  }
1100 
1101  for (gs = Surf_top->next; gs; gs = gs->next) {
1102  gs_get_zextents(gs, &tmin, &tmax, &tmid);
1103 
1104  if (tmin < *min) {
1105  *min = tmin;
1106  }
1107 
1108  if (tmax > *max) {
1109  *max = tmax;
1110  }
1111  }
1112 
1113  return (1);
1114 }
1115 
1125 int gs_get_xrange(float *min, float *max)
1126 {
1127  geosurf *gs;
1128  float tmin, tmax;
1129 
1130  if (Surf_top) {
1131  gs_get_xextents(Surf_top, &tmin, &tmax);
1132  *min = tmin;
1133  *max = tmax;
1134  }
1135  else {
1136  return (-1);
1137  }
1138 
1139  for (gs = Surf_top->next; gs; gs = gs->next) {
1140  gs_get_xextents(gs, &tmin, &tmax);
1141 
1142  if (tmin < *min) {
1143  *min = tmin;
1144  }
1145 
1146  if (tmax > *max) {
1147  *max = tmax;
1148  }
1149  }
1150 
1151  return (1);
1152 }
1153 
1163 int gs_get_yrange(float *min, float *max)
1164 {
1165  geosurf *gs;
1166  float tmin, tmax;
1167 
1168  if (Surf_top) {
1169  gs_get_yextents(Surf_top, &tmin, &tmax);
1170  *min = tmin;
1171  *max = tmax;
1172  }
1173  else {
1174  return (-1);
1175  }
1176 
1177  for (gs = Surf_top->next; gs; gs = gs->next) {
1178  gs_get_yextents(gs, &tmin, &tmax);
1179 
1180  if (tmin < *min) {
1181  *min = tmin;
1182  }
1183 
1184  if (tmax > *max) {
1185  *max = tmax;
1186  }
1187  }
1188 
1189  return (1);
1190 }
1191 
1202 int gs_get_data_avg_zmax(float *azmax)
1203 {
1204  float zmax;
1205  int i;
1206  geosurf *gs;
1207 
1208  zmax = *azmax = 0.0;
1209 
1210  if (Surf_top) {
1211  for (i = 0, gs = Surf_top; gs; i++, gs = gs->next) {
1212  zmax += (gs->zmax + gs->z_trans);
1213  }
1214 
1215  *azmax = zmax / i;
1216 
1217  return (1);
1218  }
1219 
1220  return (-1);
1221 }
1222 
1231 int gs_get_datacenter(float *cen)
1232 {
1233  float zmin, zmax, ymin, ymax, xmin, xmax;
1234  geosurf *gs;
1235 
1236  if (Surf_top) {
1237  zmin = Surf_top->zmin;
1238  zmax = Surf_top->zmax;
1239  ymin = Surf_top->ymin;
1240  ymax = Surf_top->ymax;
1241  xmin = Surf_top->xmin;
1242  xmax = Surf_top->xmax;
1243 
1244  for (gs = Surf_top->next; gs; gs = gs->next) {
1245  if (gs->zmin < zmin) {
1246  zmin = gs->zmin;
1247  }
1248 
1249  if (gs->zmax > zmax) {
1250  zmax = gs->zmax;
1251  }
1252 
1253  if (gs->ymin < ymin) {
1254  ymin = gs->ymin;
1255  }
1256 
1257  if (gs->ymax > ymax) {
1258  ymax = gs->ymax;
1259  }
1260 
1261  if (gs->xmin < xmin) {
1262  xmin = gs->xmin;
1263  }
1264 
1265  if (gs->xmax > xmax) {
1266  xmax = gs->xmax;
1267  }
1268  }
1269 
1270  cen[X] = (xmin + xmax) / 2. - xmin;
1271  cen[Y] = (ymin + ymax) / 2. - ymin;
1272  cen[Z] = (zmin + zmax) / 2.;
1273 
1274  return (1);
1275  }
1276 
1277  cen[X] = cen[Y] = cen[Z] = 0.0;
1278 
1279  return (-1);
1280 }
1281 
1282 
1290 {
1291  geosurf *gs;
1292 
1293  if (Surf_top) {
1294  Surf_top->norm_needupdate = 1;
1295  }
1296  else {
1297  return (-1);
1298  }
1299 
1300  for (gs = Surf_top->next; gs; gs = gs->next) {
1301  gs->norm_needupdate = 1;
1302  }
1303 
1304  return (1);
1305 }
1306 
1316 int gs_point_is_masked(geosurf * gs, float *pt)
1317 {
1318  int vrow, vcol, drow, dcol;
1319  int retmask = 0, npts = 0;
1320  float p2[2];
1321 
1322  if (!gs->curmask) {
1323  return (0);
1324  }
1325 
1326  vrow = Y2VROW(gs, pt[Y]);
1327  vcol = X2VCOL(gs, pt[X]);
1328 
1329  /* check right & bottom edges */
1330  if (pt[X] == VCOL2X(gs, VCOLS(gs))) {
1331  /* right edge */
1332  vcol -= 1;
1333  }
1334 
1335  if (pt[Y] == VROW2Y(gs, VROWS(gs))) {
1336  /* bottom edge */
1337  vrow -= 1;
1338  }
1339 
1340  drow = VROW2DROW(gs, vrow);
1341  dcol = VCOL2DCOL(gs, vcol);
1342 
1343  if (BM_get(gs->curmask, dcol, drow)) {
1344  retmask |= MASK_TL;
1345  npts++;
1346  }
1347 
1348  dcol = VCOL2DCOL(gs, vcol + 1);
1349 
1350  if (BM_get(gs->curmask, dcol, drow)) {
1351  retmask |= MASK_TR;
1352  npts++;
1353  }
1354 
1355  drow = VROW2DROW(gs, vrow + 1);
1356 
1357  if (BM_get(gs->curmask, dcol, drow)) {
1358  retmask |= MASK_BR;
1359  npts++;
1360  }
1361 
1362  dcol = VCOL2DCOL(gs, vcol);
1363 
1364  if (BM_get(gs->curmask, dcol, drow)) {
1365  retmask |= MASK_BL;
1366  npts++;
1367  }
1368 
1369  if (npts != 1) {
1370  /* zero or masked */
1371  return (retmask | npts);
1372  }
1373 
1374  p2[X] = VCOL2X(gs, vcol);
1375  p2[Y] = VROW2Y(gs, vrow + 1);
1376 
1377  switch (retmask) {
1378  case MASK_TL:
1379  if ((pt[X] - p2[X]) / VXRES(gs) > (pt[Y] - p2[Y]) / VYRES(gs)) {
1380  /* lower triangle */
1381  return (0);
1382  }
1383 
1384  return (retmask | npts);
1385  case MASK_TR:
1386 
1387  return (retmask | npts);
1388  case MASK_BR:
1389  if ((pt[X] - p2[X]) / VXRES(gs) <= (pt[Y] - p2[Y]) / VYRES(gs)) {
1390  /* upper triangle */
1391  return (0);
1392  }
1393 
1394  return (retmask | npts);
1395  case MASK_BL:
1396 
1397  return (retmask | npts);
1398  }
1399 
1400  /* Assume that if we get here it is an error */
1401  return (0);
1402 }
1403 
1416 int gs_distance_onsurf(geosurf * gs, float *p1, float *p2, float *dist,
1417  int use_exag)
1418 {
1419  Point3 *tmp;
1420  int np, i;
1421  float exag, length;
1422 
1423  if (in_vregion(gs, p1) && in_vregion(gs, p2)) {
1424  if (NULL == (tmp = gsdrape_get_segments(gs, p1, p2, &np))) {
1425  return (0);
1426  }
1427 
1428  length = 0.;
1429 
1430  if (use_exag) {
1431  exag = GS_global_exag();
1432  tmp[0][Z] *= exag;
1433 
1434  for (i = 0; i < (np - 1); i++) {
1435  tmp[i + 1][Z] *= exag;
1436  length += GS_distance(tmp[i], tmp[i + 1]);
1437  }
1438  }
1439  else {
1440  for (i = 0; i < (np - 1); i++) {
1441  length += GS_distance(tmp[i], tmp[i + 1]);
1442  }
1443  }
1444 
1445  *dist = length;
1446 
1447  return (1);
1448  }
1449 
1450  return (0);
1451 }
#define VXRES(gs)
Definition: rowcol.h:7
int gs_init_surf(geosurf *gs, double ox, double oy, int rows, int cols, double xres, double yres)
Initialize allocated geosurf struct.
Definition: gs.c:232
int BM_get(struct BM *map, int x, int y)
Gets 'val' from the bitmap.
Definition: bitmap.c:220
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int gs_num_datah_reused(int dh)
Get number of reused values.
Definition: gs.c:597
void gs_delete_surf(int id)
Remove geosurf struct from list.
Definition: gs.c:463
#define NULL
Definition: strings.c:26
#define VCOL2X(gs, vcol)
Definition: rowcol.h:38
int gs_get_datacenter(float *cen)
Get data center point.
Definition: gs.c:1231
void gs_set_maskmode(int invert)
Set geosurf mask mode.
Definition: gs.c:899
int gs_get_data_avg_zmax(float *azmax)
Get average z-max value.
Definition: gs.c:1202
int in_vregion(geosurf *gs, float *pt)
ADD.
Definition: gsdrape.c:696
int gs_point_is_masked(geosurf *gs, float *pt)
Check if point is masked.
Definition: gs.c:1316
#define min(x, y)
Definition: draw2.c:68
#define VROWS(gs)
Definition: rowcol.h:11
#define VCOLS(gs)
Definition: rowcol.h:12
int gs_mask_defined(geosurf *gs)
Check if mask is defined.
Definition: gs.c:914
int gs_malloc_att_buff(geosurf *gs, int desc, int type)
Allocate attribute buffer.
Definition: gs.c:716
#define Y(x)
Definition: display/draw.c:246
int gs_get_yextents(geosurf *gs, float *min, float *max)
Get y-extent values.
Definition: gs.c:1032
#define X(y)
Definition: display/draw.c:248
geosurf * gs_get_prev_surface(int id)
Get previous geosurf struct.
Definition: gs.c:86
int gs_get_yrange(float *min, float *max)
Get y-range.
Definition: gs.c:1163
int gs_att_is_set(geosurf *surf, IFLAG att)
Check if attribute is set.
Definition: gs.c:149
typbuff * gsds_get_typbuff(int id, IFLAG change_flag)
Get data buffer.
Definition: gsds.c:281
void print_frto(float(*ft)[4])
Debugging, print 'from/to' model coordinates to stderr.
Definition: gs.c:337
#define max(x, y)
Definition: draw2.c:69
void gs_set_defaults(geosurf *gs, float *defs, float *null_defs)
Set default attribute values.
Definition: gs.c:441
int gsds_alloc_typbuff(int id, int *dims, int ndims, int type)
Allocates correct buffer according to type, keeps track of total mem.
Definition: gsds.c:481
void gs_init(void)
Initialize library.
Definition: gs.c:47
int Gs_update_attrange(geosurf *gs, int desc)
Update no_zero ranges for attribute (actually no_null now)
Definition: Gs3.c:1140
int G_warning(const char *msg,...)
Print a warning message to stderr.
int gs_set_att_type(geosurf *gs, int desc, int type)
Set attribute type.
Definition: gs.c:802
#define VYRES(gs)
Definition: rowcol.h:8
void print_view_fields(geoview *gv)
Debugging, print geoview fields to stderr.
Definition: gs.c:419
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
geosurf * gsdiff_get_SDref(void)
ADD.
Definition: gsdiff.c:77
#define VROW2Y(gs, vrow)
Definition: rowcol.h:37
#define FIRST_SURF_ID
Definition: gs.c:28
void print_surf_fields(geosurf *s)
Debugging, print geosurf fields to stderr.
Definition: gs.c:391
typbuff * gs_get_att_typbuff(geosurf *gs, int desc, int to_write)
Get attribute data buffer.
Definition: gs.c:681
void gs_err(const char *msg)
Definition: gs.c:34
#define Y2VROW(gs, py)
Definition: rowcol.h:25
#define X2VCOL(gs, px)
Definition: rowcol.h:26
int gs_num_surfaces(void)
Get number of surfaces.
Definition: gs.c:127
int gs_masked(typbuff *tb, int col, int row, int offset)
Should only be called when setting up the current mask (gs_bm.c)
Definition: gs.c:932
void print_realto(float *rt)
Debugging, print 'to' real coordinates to stderr.
Definition: gs.c:353
int gs_set_att_const(geosurf *gs, int desc, float constant)
Set attribute constant value.
Definition: gs.c:871
int gs_getall_surfaces(geosurf **gsurfs)
Get array of geosurf structs.
Definition: gs.c:108
int gs_free_surf(geosurf *fs)
Free geosurf struct.
Definition: gs.c:487
int gs_get_xrange(float *min, float *max)
Get x-range.
Definition: gs.c:1125
int gs_get_zextents(geosurf *gs, float *min, float *max, float *mid)
Get z-extent values.
Definition: gs.c:997
int gs_set_att_src(geosurf *gs, int desc, int src)
Set attribute source.
Definition: gs.c:826
int gs_malloc_lookup(geosurf *gs, int desc)
Allocate attribute lookup.
Definition: gs.c:745
int gs_get_xextents(geosurf *gs, float *min, float *max)
Get x-extent values.
Definition: gs.c:1015
void print_256lookup(int *buff)
Debugging, 256 interger values from buffer.
Definition: gs.c:367
int gs_get_att_type(geosurf *gs, int desc)
Get attribute type.
Definition: gs.c:630
geosurf * gs_get_surf(int id)
Get geosurf struct.
Definition: gs.c:62
int gs_get_zrange0(float *min, float *max)
Get z-range.
Definition: gs.c:1053
int gs_setall_norm_needupdate(void)
Set for geosurf need-to-update mark.
Definition: gs.c:1289
Point3 * gsdrape_get_segments(geosurf *gs, float *bgn, float *end, int *num)
ADD.
Definition: gsdrape.c:350
tuple msg
Definition: wxnviz.py:32
tuple cols
geosurf * gs_get_last_surface(void)
Get last allocated geosurf struct from list.
Definition: gs.c:172
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int gs_get_att_src(geosurf *gs, int desc)
Get attribute source.
Definition: gs.c:656
float gsdiff_do_SD(float val, int offset)
ADD.
Definition: gsdiff.c:94
#define VCOL2DCOL(gs, vcol)
Definition: rowcol.h:30
int gs_init_normbuff(geosurf *gs)
Init geosurf normbuff.
Definition: gs.c:306
float GS_distance(float *from, float *to)
Calculate distance.
Definition: GS_util.c:141
geosurf * gs_get_new_surface(void)
Allocate new geosurf struct.
Definition: gs.c:193
#define FROM
Definition: y.tab.c:161
void gs_free_unshared_buffs(geosurf *fs)
Free unshared buffers of geosurf struct.
Definition: gs.c:557
int gs_mapcolor(typbuff *cobuff, gsurf_att *coloratt, int offset)
Call this one when you already know att_src is MAP_ATT.
Definition: gs.c:968
#define VROW2DROW(gs, vrow)
Definition: rowcol.h:29
int gs_get_zrange(float *min, float *max)
Get z-range.
Definition: gs.c:1087
int gs_distance_onsurf(geosurf *gs, float *p1, float *p2, float *dist, int use_exag)
Calculate distance on surface.
Definition: gs.c:1416
int gsds_free_datah(int id)
Free allocated dataset.
Definition: gsds.c:329
float GS_global_exag(void)
Get global z-exag value.
Definition: GS2.c:1996