GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
change_view.c
Go to the documentation of this file.
1 
15 #include <math.h>
16 
17 #include <grass/glocale.h>
18 #include <grass/nviz.h>
19 
30 {
31  int ret;
32 
33  ret = 1;
34 
35  if (width < 1 || height < 1) {
36  width = 20;
37  height = 20;
38  ret = 0;
39  }
40 
41  G_debug(1, "Nviz_resize_window(): width = %d height = %d", width, height);
42  GS_set_viewport(0, width, 0, height);
43 
44  /* GS_clear(0x0000FF); causes red flash - debug only */
45  GS_set_draw(GSD_BACK);
46  GS_ready_draw();
48  GS_done_draw();
49 
50  return ret;
51 }
52 
60 int Nviz_update_ranges(nv_data * dc)
61 {
62  float zmin, zmax, exag;
63 
64  GS_get_longdim(&(dc->xyrange));
65 
66  dc->zrange = 0.;
67 
68  /* Zrange is based on a minimum of Longdim */
69  if (GS_global_exag()) {
70  exag = GS_global_exag();
71  dc->zrange = dc->xyrange / exag;
72  }
73  else {
74  exag = 1.0;
75  }
76 
77  GS_get_zrange_nz(&zmin, &zmax); /* actual */
78 
79  zmax = zmin + (3. * dc->xyrange / exag);
80  zmin = zmin - (2. * dc->xyrange / exag);
81 
82  if ((zmax - zmin) > dc->zrange)
83  dc->zrange = zmax - zmin;
84 
85  return 1;
86 }
87 
96 int Nviz_set_viewpoint_position(double x_pos, double y_pos)
97 {
98  float xpos, ypos, from[3];
99  float tempx, tempy;
100 
101  xpos = x_pos;
102  xpos = (xpos < 0) ? 0 : (xpos > 1.0) ? 1.0 : xpos;
103  ypos = 1.0 - y_pos;
104  ypos = (ypos < 0) ? 0 : (ypos > 1.0) ? 1.0 : ypos;
105 
106  if (x_pos < 0.0 || x_pos > 1.0 || y_pos < 0.0 || y_pos > 1.0) {
107  G_debug(3, "Invalid view position coordinates, using %f,%f",
108  xpos, 1.0 - ypos);
109  }
110 
111  G_debug(1, "Nviz_set_viewpoint_position(): x = %f y = %f", x_pos, y_pos);
112  GS_get_from(from);
113 
114  tempx = xpos * RANGE - RANGE_OFFSET;
115  tempy = ypos * RANGE - RANGE_OFFSET;
116 
117  if ((from[X] != tempx) || (from[Y] != tempy)) {
118 
119  from[X] = tempx;
120  from[Y] = tempy;
121 
122  GS_moveto(from);
123 
124  /* Nviz_draw_quick(data); */
125  }
126 
127  return 1;
128 }
129 
130 void Nviz_get_viewpoint_position(double *x_pos, double *y_pos)
131 {
132  float from[3];
133  double xpos, ypos;
134 
135  GS_get_from(from);
136  xpos = (from[X] + RANGE_OFFSET) / RANGE;
137  ypos = (from[Y] + RANGE_OFFSET) / RANGE;
138  *x_pos = xpos;
139  *x_pos = (*x_pos < 0) ? 0 : (*x_pos > 1.0) ? 1.0 : *x_pos;
140  *y_pos = 1.0 - ypos;
141  *y_pos = (*y_pos < 0) ? 0 : (*y_pos > 1.0) ? 1.0 : *y_pos;
142 
143  if (xpos < 0.0 || xpos > 1.0 || ypos < 0.0 || ypos > 1.0) {
144  G_debug(3, "Invalid view position coordinates, using %f,%f",
145  *x_pos, 1.0 - *y_pos);
146  }
147 }
148 
158 {
159  float from[3];
160 
161  G_debug(1, "Nviz_set_viewpoint_height(): value = %f", height);
162 
163  GS_get_from_real(from);
164 
165  if (height != from[Z]) {
166  from[Z] = height;
167 
168  GS_moveto_real(from);
169 
170  /*
171  normalize (from);
172  GS_setlight_position(1, from[X], from[Y], from[Z], 0);
173  */
174 
175  /* Nviz_draw_quick(data); */
176  }
177 
178  return 1;
179 }
180 
182 {
183  float from[3];
184 
185  G_debug(1, "Nviz_get_viewpoint_height():");
186 
187  GS_get_from_real(from);
188 
189  *height = from[Z];
190 }
200 {
201  int fov;
202 
203  G_debug(1, "Nviz_set_viewpoint_persp(): value = %d", persp);
204 
205  fov = (int)(10 * persp);
206  GS_set_fov(fov);
207 
208  /* Nviz_draw_quick(data); */
209 
210  return 1;
211 }
212 
222 {
223  G_debug(1, "Nviz_set_viewpoint_twist(): value = %d", twist);
224  GS_set_twist(10 * twist);
225 
226  /* Nviz_draw_quick(data); */
227 
228  return 1;
229 }
230 
239 int Nviz_change_exag(nv_data * data, double exag)
240 {
241  double temp;
242 
243  G_debug(1, "Nviz_change_exag(): value = %f", exag);
244  temp = GS_global_exag();
245 
246  if (exag != temp) {
247  GS_set_global_exag(exag);
248  Nviz_update_ranges(data);
249 
250  /* Nviz_draw_quick(data); */
251  }
252 
253  return 1;
254 }
262 int Nviz_look_here(double sx, double sy)
263 {
264  G_debug(1, "Nviz_look_here(): screen coordinates = %f %f", sx, sy);
265  GS_look_here(sx, sy);
266  return 1;
267 }
268 
272 void Nviz_get_modelview(double *modelMatrix)
273 {
274  glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
275 }
276 
288 void Nviz_set_rotation(double angle, double x, double y, double z)
289 {
290  G_debug(3, "Nviz_set_rotation(): angle = %f, x = %f, y = %f, z = %f", angle, x, y, z);
291  GS_set_rotation(angle, x, y, z);
292 }
293 
298 {
300 }
301 
306 {
308 }
309 
322 void Nviz_flythrough(nv_data *data, float *fly_info, int *scale, int lateral)
323 {
324  float dir[3], from[4], cur_from[4], cur_dir[4];
325  float speed, h, p, sh, ch, sp, cp;
326  float diff_x, diff_y, diff_z;
327  float quasi_zero;
328 
329  quasi_zero = 0.0001;
330 
331  GS_get_from(cur_from);
332  GS_get_viewdir(cur_dir);
333 
334  p = asin(cur_dir[Z]);
335  h = atan2(- cur_dir[X], - cur_dir[Y]);
336 
337  speed = scale[0] * fly_info[0];
338 
339  h += scale[1] * fly_info[1]; /* change heading */
340  if (!lateral) /* in case of "lateral" doesn't change pitch */
341  p -= scale[1] * fly_info[2];
342 
343  h = fmod(h + M_PI, 2 * M_PI) - M_PI;
344 
345  sh = sin(h);
346  ch = cos(h);
347  sp = sin(p);
348  cp = cos(p);
349 
350  dir[X] = -sh * cp;
351  dir[Y] = -ch * cp;
352  dir[Z] = sp;
353 
354  if (lateral) {
355  from[X] = cur_from[X] + speed * dir[Y];
356  from[Y] = cur_from[Y] - speed * dir[X];
357  from[Z] = cur_from[Z] + scale[0] * fly_info[2];
358  }
359  else {
360  from[X] = cur_from[X] + speed * dir[X];
361  from[Y] = cur_from[Y] + speed * dir[Y];
362  /* not sure how this should behave (change Z coord or not ?)*/
363  from[Z] = cur_from[Z]; /* + speed * dir[Z]*/
364  }
365 
366  diff_x = fabs(cur_dir[X] - dir[X]);
367  diff_y = fabs(cur_dir[Y] - dir[Y]);
368  diff_z = fabs(cur_dir[Z] - dir[Z]);
369 
370  if ( /* something has changed */
371  (diff_x > quasi_zero) || (diff_y > quasi_zero) ||
372  (diff_z > quasi_zero) || (cur_from[X] != from[X]) ||
373  (cur_from[Y] != from[Y]) || (cur_from[Z] != from[Z])
374  ) {
375  GS_moveto(from);
376  GS_set_viewdir(dir); /* calls gsd_set_view */
377  }
378 }
void GS_set_fov(int fov)
Set field of view.
Definition: GS2.c:2841
void GS_set_global_exag(float exag)
Set global z-exag value.
Definition: GS2.c:1974
int Nviz_set_viewpoint_height(double height)
Change viewpoint height.
Definition: change_view.c:157
void GS_init_rotation(void)
Reset scene rotation.
Definition: GS2.c:2907
void GS_get_from_real(float *fr)
Get viewpoint &#39;from&#39; real coordinates.
Definition: GS2.c:2737
void Nviz_get_viewpoint_position(double *x_pos, double *y_pos)
Definition: change_view.c:130
void GS_done_draw(void)
Draw done, swap buffers.
Definition: GS2.c:2499
tuple width
void GS_get_from(float *fr)
Get viewpoint &#39;from&#39; position.
Definition: GS2.c:2723
#define Y(x)
Definition: display/draw.c:246
int Nviz_resize_window(int width, int height)
GL canvas resized.
Definition: change_view.c:29
int Nviz_update_ranges(nv_data *dc)
Update ranges.
Definition: change_view.c:60
#define X(y)
Definition: display/draw.c:248
void Nviz_init_rotation(void)
Stop scene rotation.
Definition: change_view.c:305
void GS_set_rotation(double angle, double x, double y, double z)
Set rotation params.
Definition: GS2.c:2885
int y
Definition: plot.c:34
int Nviz_set_viewpoint_persp(int persp)
Change viewpoint perspective (field of view)
Definition: change_view.c:199
void GS_set_viewport(int left, int right, int bottom, int top)
Set viewport.
Definition: GS2.c:2979
void Nviz_flythrough(nv_data *data, float *fly_info, int *scale, int lateral)
Fly through the scene.
Definition: change_view.c:322
int Nviz_change_exag(nv_data *data, double exag)
Change z-exag value.
Definition: change_view.c:239
tuple data
void Nviz_set_rotation(double angle, double x, double y, double z)
Set rotation parameters.
Definition: change_view.c:288
void Nviz_get_modelview(double *modelMatrix)
Get current modelview matrix.
Definition: change_view.c:272
void GS_set_viewdir(float *dir)
Set viewdir.
Definition: GS2.c:2821
void GS_moveto(float *pt)
Move viewpoint.
Definition: GS2.c:2615
void Nviz_get_viewpoint_height(double *height)
Definition: change_view.c:181
int
Definition: g3dcolor.c:48
void GS_get_zrange_nz(float *min, float *max)
Get Z extents for all loaded surfaces.
Definition: GS2.c:2354
int Nviz_set_viewpoint_twist(int twist)
Change viewpoint twist.
Definition: change_view.c:221
void GS_alldraw_wire(void)
Draw all wires.
Definition: GS2.c:1916
void GS_unset_rotation(void)
Stop scene rotation.
Definition: GS2.c:2899
int Nviz_look_here(double sx, double sy)
Change focused point.
Definition: change_view.c:262
int Nviz_set_viewpoint_position(double x_pos, double y_pos)
Change position of view.
Definition: change_view.c:96
void GS_set_twist(int t)
Set viewpoint twist value.
Definition: GS2.c:2875
int GS_look_here(int sx, int sy)
Send screen coords sx and sy, lib traces through surfaces; sets new center to point of nearest inters...
Definition: GS2.c:3003
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
void GS_get_viewdir(float *dir)
Get viewdir.
Definition: GS2.c:2807
int GS_get_longdim(float *dim)
Get largest dimension.
Definition: GS2.c:140
void Nviz_unset_rotation(void)
Stop scene rotation.
Definition: change_view.c:297
void GS_ready_draw(void)
Definition: GS2.c:2486
int height
void GS_moveto_real(float *pt)
Move position to (real)
Definition: GS2.c:2645
float GS_global_exag(void)
Get global z-exag value.
Definition: GS2.c:1996
void GS_set_draw(int where)
Sets which buffer to draw to.
Definition: GS2.c:2457
tuple h
panel.defaultSize = wx.CheckBox(panel, id = wx.ID_ANY, label = _(&quot;Use default size&quot;)) panel...