GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
gpd.c
Go to the documentation of this file.
1 /*!
2  \file lib/ogsf/gpd.c
3 
4  \brief OGSF library - loading and manipulating point sets (lower level)
5 
6  (C) 1999-2008, 2011 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Bill Brown USACERL, GMSL/University of Illinois (December 1993)
12  \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
13  */
14 
15 #include <stdlib.h>
16 #include <math.h>
17 
18 #include <grass/ogsf.h>
19 
20 #include "rowcol.h"
21 
22 #define CHK_FREQ 50
23 
24 /* BOB -- border allowed outside of viewport */
25 #define v_border 50
26 
27 /*!
28  \brief Check if point is in region
29 
30  Check for cancel every CHK_FREQ points
31 
32  \param gs surface (geosurf)
33  \param pt point (array(X,Y,Z))
34  \param region region settings (array (top,bottom,left,right))
35 
36  \return 0 point outside of region
37  \return 1 point inside region
38  */
39 int gs_point_in_region(geosurf *gs, float *pt, float *region)
40 {
41  float top, bottom, left, right;
42 
43  if (!region) {
44  top = gs->yrange;
45  bottom = VROW2Y(gs, VROWS(gs));
46  left = 0.0;
47  right = VCOL2X(gs, VCOLS(gs));
48  }
49  else {
50  top = region[0];
51  bottom = region[1];
52  left = region[2];
53  right = region[3];
54  }
55 
56  return (pt[X] >= left && pt[X] <= right && pt[Y] >= bottom && pt[Y] <= top);
57 }
58 
59 /*!
60  \brief Draw point representing object
61 
62  Do normal transforms before calling
63 
64  Note gs: NULL if 3d obj or const elev surface
65 
66  \param gs surface (geosurf)
67  \param style object displaying style (highlighted or not)
68  \param pt 3d point (Point3)
69  */
70 void gpd_obj(geosurf *gs, gvstyle *style, Point3 pt)
71 {
72  float sz, lpt[3];
73  float siz[3];
74 
75  gsd_color_func(style->color);
76  sz = GS_global_exag();
77  GS_v3eq(lpt, pt); /* CHANGING Z OF POINT PASSED, so use copy */
78 
79  switch (style->symbol) {
80  case ST_HISTOGRAM:
83 
84  if (sz) {
85  lpt[Z] *= sz;
86  gsd_scale(1.0, 1.0, 1. / sz);
87  }
88 
89  siz[0] = style->size; /*TODO: Fix historgam drawing */
90  siz[1] = style->size;
91  siz[2] = style->size;
92 
93  gsd_box(lpt, style->color, siz);
94 
95  gsd_popmatrix();
97 
98  break;
99  case ST_DIAMOND:
100  /*
101  gsd_colormode(CM_AD);
102  */
104  gsd_pushmatrix();
105 
106  if (sz) {
107  lpt[Z] *= sz;
108  gsd_scale(1.0, 1.0, 1. / sz);
109  }
110 
111  gsd_diamond(lpt, style->color, style->size);
112  gsd_popmatrix();
114 
115  break;
116  case ST_BOX:
118  gsd_pushmatrix();
119 
120  if (sz) {
121  lpt[Z] *= sz;
122  gsd_scale(1.0, 1.0, 1. / sz);
123  }
124 
125  gsd_draw_box(lpt, style->color, style->size);
126  gsd_popmatrix();
127 
128  break;
129  case ST_SPHERE:
130  /*
131  gsd_colormode(CM_AD);
132  */
134  gsd_pushmatrix();
135 
136  if (sz) {
137  lpt[Z] *= sz;
138  gsd_scale(1.0, 1.0, 1. / sz);
139  }
140 
141  gsd_sphere(lpt, style->size);
142  gsd_popmatrix();
144 
145  break;
146  case ST_GYRO:
148  gsd_pushmatrix();
149 
150  if (sz) {
151  lpt[Z] *= sz;
152  gsd_scale(1.0, 1.0, 1. / sz);
153  }
154 
155  gsd_draw_gyro(lpt, style->color, style->size);
156  gsd_popmatrix();
157 
158  break;
159  case ST_ASTER:
161  gsd_pushmatrix();
162 
163  if (sz) {
164  lpt[Z] *= sz;
165  gsd_scale(1.0, 1.0, 1. / sz);
166  }
167 
168  gsd_draw_asterisk(lpt, style->color, style->size);
169  gsd_popmatrix();
170 
171  break;
172  case ST_CUBE:
174  gsd_pushmatrix();
175 
176  if (sz) {
177  lpt[Z] *= sz;
178  gsd_scale(1.0, 1.0, 1. / sz);
179  }
180 
181  gsd_cube(lpt, style->color, style->size);
182  gsd_popmatrix();
184 
185  break;
186  default:
187  case ST_X:
189  gsd_x(gs, lpt, style->color, style->size);
190 
191  break;
192  }
193 
194  return;
195 }
196 
197 /*!
198  \brief Draw 2D point set
199 
200  Need to think about translations - If user translates surface,
201  sites should automatically go with it, but translating sites should
202  translate it relative to surface on which it's displayed
203 
204  Handling mask checking here
205 
206  \todo prevent scaling by 0
207 
208  \param gp site (geosite)
209  \param gs surface (geosurf)
210  \param do_fast (unused)
211 
212  \return 0 on failure
213  \return 1 on success
214  */
215 int gpd_2dsite(geosite *gp, geosurf *gs, int do_fast UNUSED)
216 {
217  float site[4], konst;
218  int src, check;
219  geopoint *gpt;
220  typbuff *buf;
221  GLdouble modelMatrix[16], projMatrix[16];
222  GLint viewport[4];
223  GLint window[4];
224 
225  if (GS_check_cancel()) {
226  return 0;
227  }
228 
229  if (!gs)
230  return 1;
231 
232  gs_update_curmask(gs);
233 
234  src = gs_get_att_src(gs, ATT_TOPO);
235 
236  if (src == CONST_ATT) {
237  konst = gs->att[ATT_TOPO].constant;
238  }
239  else {
240  buf = gs_get_att_typbuff(gs, ATT_TOPO, 0);
241  }
242 
243  /* Get viewport parameters for view check */
244  gsd_getwindow(window, viewport, modelMatrix, projMatrix);
245 
246  gsd_pushmatrix();
247  gsd_do_scale(1);
248  gsd_translate(gs->x_trans, gs->y_trans, gs->z_trans);
249  gsd_linewidth(gp->style->width);
250  check = 0;
251 
252  for (gpt = gp->points; gpt; gpt = gpt->next) {
253  if (!(++check % CHK_FREQ)) {
254  if (GS_check_cancel()) {
255  gsd_linewidth(1);
256  gsd_popmatrix();
257 
258  return 0;
259  }
260  }
261 
262  site[X] = gpt->p3[X] + gp->x_trans - gs->ox;
263  site[Y] = gpt->p3[Y] + gp->y_trans - gs->oy;
264 
265  if (gs_point_is_masked(gs, site)) {
266  continue;
267  }
268 
269  if (src == MAP_ATT) {
270  if (viewcell_tri_interp(gs, buf, site, 1)) {
271  /* returns 0 if outside or masked */
272  site[Z] += gp->z_trans;
273 
274  if (gsd_checkpoint(site, window, viewport, modelMatrix,
275  projMatrix))
276  continue;
277  }
278  }
279  else if (src == CONST_ATT) {
280  if (gs_point_in_region(gs, site, NULL)) {
281  site[Z] = konst + gp->z_trans;
282  if (gsd_checkpoint(site, window, viewport, modelMatrix,
283  projMatrix))
284  continue;
285  }
286  }
287 
288  if (gpt->highlighted > 0)
289  gpd_obj(gs, gp->hstyle, site);
290  else if (gp->tstyle && gp->tstyle->active)
291  gpd_obj(gs, gpt->style, site);
292  else
293  gpd_obj(gs, gp->style, site);
294  }
295 
296  gsd_linewidth(1);
297  gsd_popmatrix();
298 
299  return 1;
300 }
301 
302 /*!
303  \brief Draw 3D point set
304 
305  \param gp site (geosite)
306  \param xo,yo
307  \param do_fast (unused)
308 
309  \return 0 on success
310  \return 1 on failure
311  */
312 int gpd_3dsite(geosite *gp, float xo, float yo, int do_fast UNUSED)
313 {
314  float site[4], tz;
315  int check;
316  geopoint *gpt;
317  GLdouble modelMatrix[16], projMatrix[16];
318  GLint viewport[4];
319  GLint window[4];
320 
321  if (GS_check_cancel()) {
322  return 0;
323  }
324 
325  gsd_getwindow(window, viewport, modelMatrix, projMatrix);
326 
327  gsd_pushmatrix();
328 
329  gsd_do_scale(1);
330 
331  tz = GS_global_exag();
332  site[Z] = 0.0;
333 
334  check = 0;
335 
336  gsd_linewidth(gp->style->width);
337 
338  for (gpt = gp->points; gpt; gpt = gpt->next) {
339  if (!(++check % CHK_FREQ)) {
340  if (GS_check_cancel()) {
341  gsd_linewidth(1);
342  gsd_popmatrix();
343 
344  return (0);
345  }
346  }
347 
348  site[X] = gpt->p3[X] + gp->x_trans - xo;
349  site[Y] = gpt->p3[Y] + gp->y_trans - yo;
350 
351  if (tz) {
352  site[Z] = gpt->p3[Z] + gp->z_trans;
353  }
354 
355  if (gsd_checkpoint(site, window, viewport, modelMatrix, projMatrix))
356  continue;
357  else
358  /* clip points outside default region? */
359  {
360  if (gpt->highlighted > 0)
361  gpd_obj(NULL, gp->hstyle, site);
362  else if (gp->tstyle && gp->tstyle->active)
363  gpd_obj(NULL, gpt->style, site);
364  else
365  gpd_obj(NULL, gp->style, site);
366  }
367  }
368 
369  gsd_linewidth(1);
370  gsd_popmatrix();
371 
372  return 1;
373 }
#define NULL
Definition: ccmath.h:32
void gsd_cube(float *, unsigned long, float)
Draw cube.
Definition: gsd_objs.c:407
int gs_point_is_masked(geosurf *, float *)
Check if point is masked.
Definition: gs.c:1314
void gsd_diamond(float *, unsigned long, float)
Draw diamond symbol.
Definition: gsd_objs.c:305
void gsd_pushmatrix(void)
Push the current matrix stack.
Definition: gsd_prim.c:511
void gsd_box(float *, int, float *)
Draw box.
Definition: gsd_objs.c:1411
void gsd_scale(float, float, float)
Multiply the current matrix by a general scaling matrix.
Definition: gsd_prim.c:525
void gsd_getwindow(int *, int *, double *, double *)
Get viewport.
Definition: gsd_prim.c:554
void gsd_do_scale(int)
Set current scale.
Definition: gsd_views.c:355
void gsd_x(geosurf *, float *, int, float)
Draw X symbol.
Definition: gsd_objs.c:256
void gsd_draw_box(float *, unsigned long, float)
Draw box.
Definition: gsd_objs.c:483
void gsd_sphere(float *, float)
ADD.
Definition: gsd_prim.c:207
int gs_get_att_src(geosurf *, int)
Get attribute source.
Definition: gs.c:656
int gsd_checkpoint(float[4], int[4], int[4], double[16], double[16])
ADD.
Definition: gsd_prim.c:585
void GS_v3eq(float *, float *)
Copy vector values.
Definition: gs_util.c:178
void gsd_draw_gyro(float *, unsigned long, float)
Draw gyro.
Definition: gsd_objs.c:636
int gs_update_curmask(geosurf *)
Update current maps.
Definition: gs_bm.c:231
void gsd_color_func(unsigned int)
Set current color.
Definition: gsd_prim.c:698
void gsd_translate(float, float, float)
Multiply the current matrix by a translation matrix.
Definition: gsd_prim.c:539
void gsd_popmatrix(void)
Pop the current matrix stack.
Definition: gsd_prim.c:501
void gsd_linewidth(short)
Set width of rasterized lines.
Definition: gsd_prim.c:267
int viewcell_tri_interp(geosurf *, typbuff *, Point3, int)
ADD.
Definition: gsdrape.c:503
void gsd_colormode(int)
Set color mode.
Definition: gsd_prim.c:98
typbuff * gs_get_att_typbuff(geosurf *, int, int)
Get attribute data buffer.
Definition: gs.c:681
void gsd_draw_asterisk(float *, unsigned long, float)
Draw asterisk.
Definition: gsd_objs.c:581
int GS_check_cancel(void)
Check for cancel.
Definition: gsx.c:30
float GS_global_exag(void)
Get global z-exag value.
Definition: gs2.c:1997
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
#define CHK_FREQ
Definition: gpd.c:22
void gpd_obj(geosurf *gs, gvstyle *style, Point3 pt)
Draw point representing object.
Definition: gpd.c:70
int gpd_2dsite(geosite *gp, geosurf *gs, int do_fast UNUSED)
Draw 2D point set.
Definition: gpd.c:215
int gs_point_in_region(geosurf *gs, float *pt, float *region)
Check if point is in region.
Definition: gpd.c:39
int gpd_3dsite(geosite *gp, float xo, float yo, int do_fast UNUSED)
Draw 3D point set.
Definition: gpd.c:312
const char * src
Definition: lz4.h:989
#define CM_DIFFUSE
Definition: ogsf.h:151
#define X
Definition: ogsf.h:140
#define ST_CUBE
Definition: ogsf.h:95
#define ATT_TOPO
Definition: ogsf.h:75
#define ST_GYRO
Definition: ogsf.h:100
float Point3[3]
Definition: ogsf.h:205
#define ST_BOX
Definition: ogsf.h:93
#define Z
Definition: ogsf.h:142
#define ST_SPHERE
Definition: ogsf.h:94
#define Y
Definition: ogsf.h:141
#define MAP_ATT
Definition: ogsf.h:85
#define CM_COLOR
Definition: ogsf.h:148
#define ST_HISTOGRAM
Definition: ogsf.h:101
#define ST_DIAMOND
Definition: ogsf.h:96
#define CONST_ATT
Definition: ogsf.h:86
#define ST_ASTER
Definition: ogsf.h:99
#define ST_X
Definition: ogsf.h:92
#define VCOL2X(gs, vcol)
Definition: rowcol.h:40
#define VCOLS(gs)
Definition: rowcol.h:14
#define VROWS(gs)
Definition: rowcol.h:13
#define VROW2Y(gs, vrow)
Definition: rowcol.h:39
Definition: ogsf.h:349
Point3 p3
Definition: ogsf.h:351
struct g_point * next
Definition: ogsf.h:358
gvstyle * style
Definition: ogsf.h:355
signed char highlighted
Definition: ogsf.h:356
Definition: ogsf.h:362
float z_trans
Definition: ogsf.h:371
float y_trans
Definition: ogsf.h:371
float x_trans
Definition: ogsf.h:371
gvstyle * hstyle
Definition: ogsf.h:379
gvstyle_thematic * tstyle
Definition: ogsf.h:377
geopoint * points
Definition: ogsf.h:372
gvstyle * style
Definition: ogsf.h:378
Definition: ogsf.h:256
float x_trans
Definition: ogsf.h:266
float yrange
Definition: ogsf.h:268
double ox
Definition: ogsf.h:263
gsurf_att att[MAX_ATTS]
Definition: ogsf.h:259
double oy
Definition: ogsf.h:263
float z_trans
Definition: ogsf.h:266
float y_trans
Definition: ogsf.h:266
int color
Definition: ogsf.h:286
int symbol
Definition: ogsf.h:287
float size
Definition: ogsf.h:288
int width
Definition: ogsf.h:289
float constant
Definition: ogsf.h:250
Definition: ogsf.h:208