GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
GK2.c
Go to the documentation of this file.
1 
19 #include <stdlib.h>
20 
21 #include <grass/gis.h>
22 #include <grass/glocale.h>
23 #include <grass/gstypes.h>
24 #include <grass/keyframe.h>
25 #include <grass/kftypes.h>
26 
27 
28 static int _add_key(Keylist *, int, float);
29 static void _remove_key(Keylist *);
30 
31 static Keylist *Keys = NULL;
32 static Keylist *Keytail = NULL;
33 static Viewnode *Views = NULL;
34 static float Keystartpos = 0.0;
35 static float Keyendpos = 1.0;
36 static float Tension = 0.8;
37 static int Viewsteps = 0;
38 static int Numkeys = 0;
39 static int Interpmode = KF_SPLINE;
40 static int Fmode = 0;
41 
42 /* next & prior already initialized to NULL */
43 static int _add_key(Keylist * newk, int force_replace, float precis)
44 {
45  Keylist *k, *tempk, *prev;
46  int found;
47 
48  found = 0;
49  prev = NULL;
50 
51  /* if(Viewsteps) precis = 0.5/Viewsteps; */
52  for (k = Keys; k; k = k->next) {
53  if (k->pos >= newk->pos - precis && k->pos <= newk->pos + precis) {
54  if (force_replace) {
55 
56  if (k->prior) {
57  k->prior->next = newk;
58  newk->prior = prev;
59  }
60  else {
61  Keys = newk;
62  }
63 
64  newk->next = k->next;
65  newk->prior = k->prior;
66  tempk = k;
67  k = newk;
68  free(tempk);
69  }
70  else {
71  free(newk);
72  }
73 
74  return (-1);
75  }
76  }
77 
78  if (Keys) {
79  if (newk->pos < Keys->pos) {
80  /* new will be first */
81  newk->next = Keys;
82  Keys->prior = newk;
83  Keys = newk;
84  }
85  else {
86  prev = k = Keys;
87  while (k && !found) {
88  if (k->pos > newk->pos) {
89  prev->next = newk;
90  newk->next = k;
91  newk->prior = prev;
92  k->prior = newk;
93  found = 1;
94  }
95 
96  prev = k;
97  k = k->next;
98  }
99  if (!found) {
100  Keytail = prev->next = newk;
101  newk->prior = prev;
102  }
103  }
104  }
105  else {
106  Keys = Keytail = newk;
107  }
108 
109  ++Numkeys;
110  return (1);
111 }
112 
113 static void _remove_key(Keylist * k)
114 {
115  if (k->prior) {
116  k->prior->next = k->next;
117  if (k->next) {
118  k->next->prior = k->prior;
119  }
120  else {
121  Keytail = k->prior;
122  }
123  }
124  else {
125  Keys = k->next;
126  if (k->next) {
127  k->next->prior = NULL;
128  }
129  }
130  k->next = k->prior = NULL;
131 
132  return;
133 }
134 
144 {
145  if (KF_LEGAL_MODE(mode)) {
146  Interpmode = mode;
147  return (1);
148  }
149 
150  return (-1);
151 }
152 
158 void GK_set_tension(float tens)
159 {
160  Tension = tens > 1.0 ? 1.0 : (tens < 0.0 ? 0.0 : tens);
161 
162  /* for now */
163  if (Views) {
165  GS_set_draw(GSD_BACK);
166  GS_ready_draw();
168  GS_alldraw_wire();
169 
170  gk_draw_path(Views, Viewsteps, Keys);
171 
172  GS_done_draw();
173  }
174 
175  return;
176 }
177 
179 {
180  return;
181 }
182 
191 {
192  return;
193 }
194 
199 {
200  if (Views) {
202  }
203 
204  return;
205 }
206 
212 void GK_print_keys(const char *name)
213 {
214  Keylist *k;
215  FILE *fp;
216  int cnt = 1;
217 
218  if (NULL == (fp = fopen(name, "w"))) {
219  G_fatal_error(_("Unable to open file <%s> for writing"), name);
220  }
221  /* write a default frame rate of 30 at top of file */
222  fprintf(fp, "30 \n");
223 
224  for (k = Keys; k; k = k->next) {
225 
226  fprintf(fp,
227  "{%f {{FromX %f} {FromY %f} {FromZ %f} {DirX %f} {DirY %f} {DirZ %f} {FOV %f} {TWIST %f} {cplane-0 {{pos_x 0.000000} {pos_y 0.000000} {pos_z 0.000000} {blend_type OFF} {rot 0.000000} {tilt 0.000000}}}} keyanimtag%d 0} ",
228  k->pos, k->fields[KF_FROMX], k->fields[KF_FROMY],
229  k->fields[KF_FROMZ], k->fields[KF_DIRX], k->fields[KF_DIRY],
230  k->fields[KF_DIRZ], k->fields[KF_FOV] / 10.,
231  k->fields[KF_TWIST], cnt);
232  cnt++;
233  }
234 
235  fclose(fp);
236  return;
237 
238 }
239 
247 {
248  Keylist *k;
249  int loop = 0;
250 
251  if (Keys) {
252  if (Numkeys > 1) {
253  k = Keytail;
254  Keyendpos = k->pos;
255 
256  if (k->fields[KF_FROMX] == Keys->fields[KF_FROMX] &&
257  k->fields[KF_FROMY] == Keys->fields[KF_FROMY] &&
258  k->fields[KF_FROMZ] == Keys->fields[KF_FROMZ]) {
259  loop = 1;
260  }
261  }
262 
263  Keystartpos = Keys->pos;
264  }
265 
266  if (Interpmode == KF_LINEAR && Numkeys > 1) {
267  if (Views) {
268  free(Views);
269  Views = NULL;
270  }
271 
272  Views = gk_make_linear_framesfromkeys(Keys, Numkeys, Viewsteps, loop);
273 
274  if (!Views) {
275  G_warning(_("Check no. of frames requested and keyframes marked"));
276  }
277  }
278  else if (Numkeys > 2) {
279  if (Views) {
280  free(Views);
281  Views = NULL;
282  }
283 
284  Views = gk_make_framesfromkeys
285  (Keys, Numkeys, Viewsteps, loop, 1.0 - Tension);
286 
287  if (!Views) {
288  G_warning(_("Check no. of frames requested and keyframes marked"));
289  }
290  }
291 
292  return;
293 }
294 
300 void GK_set_numsteps(int newsteps)
301 {
302  Viewsteps = newsteps;
304 
305  return;
306 }
307 
313 void GK_clear_keys(void)
314 {
315  gk_free_key(Keys);
316  Keys = NULL;
317  Numkeys = 0;
318  free(Views);
319  Views = NULL;
320 
321  Keystartpos = 0.0;
322  Keyendpos = 1.0;
323 
324  return;
325 }
326 
339 int GK_move_key(float oldpos, float precis, float newpos)
340 {
341  Keylist *k;
342 
343  for (k = Keys; k; k = k->next) {
344  if (k->pos >= oldpos - precis && k->pos <= oldpos + precis) {
345  _remove_key(k);
346  k->pos = newpos;
347  _add_key(k, 1, precis);
349  return (1);
350  }
351  }
352 
353  return (0);
354 }
355 
370 int GK_delete_key(float pos, float precis, int justone)
371 {
372  Keylist *k, *next;
373  int cnt;
374 
375  for (cnt = 0, k = Keys; k;) {
376  next = k->next;
377 
378  if (k->pos >= pos - precis && k->pos <= pos + precis) {
379  cnt++;
380  _remove_key(k);
381  free(k);
382  if (justone) {
383  break;
384  }
385  }
386 
387  k = next;
388  }
389 
391  return (cnt);
392 }
393 
432 int GK_add_key(float pos, unsigned long fmask, int force_replace,
433  float precis)
434 {
435  Keylist *newk;
436  float tmp[3];
437 
438  if (NULL == (newk = (Keylist *) malloc(sizeof(Keylist)))) {
439  fprintf(stderr, "Out of memory\n");
440  return (-1);
441  }
442 
443  /* All fields set, don't use mask until making Views */
444 
445  GS_get_from(tmp);
446  newk->fields[KF_FROMX] = tmp[X];
447  newk->fields[KF_FROMY] = tmp[Y];
448  newk->fields[KF_FROMZ] = tmp[Z];
449 
450  G_debug(3, "KEY FROM: %f %f %f", tmp[X], tmp[Y], tmp[Z]);
451 
452  /* Instead of View Dir try get_focus (view center) */
453  /* View Dir is implied from eye and center position */
454  /* GS_get_viewdir(tmp); */
455 
456  /* ACS 1 line: was GS_get_focus(tmp);
457  with this kanimator works also for flythrough navigation
458  also changed in gk.c
459  */
460  GS_get_viewdir(tmp);
461  newk->fields[KF_DIRX] = tmp[X];
462  newk->fields[KF_DIRY] = tmp[Y];
463  newk->fields[KF_DIRZ] = tmp[Z];
464 
465  newk->fields[KF_FOV] = GS_get_fov();
466  newk->fields[KF_TWIST] = GS_get_twist();
467  newk->pos = pos;
468  newk->fieldmask = fmask;
469  newk->next = NULL;
470  newk->prior = NULL;
471 
472  if (0 < _add_key(newk, force_replace, precis)) {
474  return (1);
475  }
476 
477  return (-1);
478 }
479 
489 void GK_do_framestep(int step, int render)
490 {
491  if (Views) {
492  if (step > 0 && step <= Viewsteps) {
493  gk_follow_frames(Views, Viewsteps, Keys, step, 1, render, Fmode);
494  }
495  }
496 
497  return;
498 }
499 
506 {
507  if (flag) {
508  Fmode |= FM_PATH;
509 
510  if (Views) {
511  GS_set_draw(GSD_FRONT);
512  GS_ready_draw();
513 
514  gk_draw_path(Views, Viewsteps, Keys);
515 
516  GS_done_draw();
517 
518  }
519  }
520  else {
521  Fmode &= ~FM_PATH;
522  }
523 
524  return;
525 }
526 
533 {
534  if (flag) {
535  Fmode |= FM_VECT;
536  if (Views) {
537 
538  GS_set_draw(GSD_FRONT);
539  GS_ready_draw();
540 
541  GV_alldraw_vect();
542 
543  GS_done_draw();
544  }
545  }
546  else {
547  Fmode &= ~FM_VECT;
548  }
549 
550  return;
551 }
552 
559 {
560  if (flag) {
561  Fmode |= FM_SITE;
562 
563  if (Views) {
564 
565  GS_set_draw(GSD_FRONT);
566  GS_ready_draw();
567 
568  GP_alldraw_site();
569 
570  GS_done_draw();
571 
572  }
573  }
574  else {
575  Fmode &= ~FM_SITE;
576  }
577 
578  return;
579 }
580 
586 void GK_show_vol(int flag)
587 {
588  if (flag) {
589  Fmode |= FM_VOL;
590 
591  if (Views) {
592 
593  GS_set_draw(GSD_FRONT);
594  GS_ready_draw();
595 
596  GVL_alldraw_vol();
597 
598  GS_done_draw();
599 
600  }
601  }
602  else {
603  Fmode &= ~FM_VOL;
604  }
605 
606  return;
607 }
608 
615 {
616  if (flag) {
617  Fmode |= FM_LABEL;
618 
619  if (Views) {
621  }
622  }
623  else {
624  Fmode &= ~FM_LABEL;
625  }
626 
627  return;
628 }
void gk_free_key(Keylist *ok)
Free keyframe list.
Definition: gk.c:273
void GK_update_tension(void)
Update tension.
Definition: GK2.c:198
string name
Definition: render.py:1314
void GS_done_draw(void)
Draw done, swap buffers.
Definition: GS2.c:2499
void GS_get_from(float *fr)
Get viewpoint &#39;from&#39; position.
Definition: GS2.c:2723
int GS_get_twist(void)
Get twist value.
Definition: GS2.c:2863
Viewnode * gk_make_framesfromkeys(Keylist *keys, int keysteps, int newsteps, int loop, float t)
Generate viewnode from keyframes.
Definition: gk.c:303
#define Y(x)
Definition: display/draw.c:246
tuple pos
Definition: tools.py:1367
#define X(y)
Definition: display/draw.c:248
void GS_clear(int col)
Clear view.
Definition: GS2.c:3416
void GK_showtension_stop(void)
Show tension stop ?
Definition: GK2.c:190
void GP_alldraw_site(void)
Draw all available point sets.
Definition: GP2.c:557
unsigned int GS_background_color(void)
Get background color.
Definition: GS2.c:2447
void GV_alldraw_vect(void)
Draw all vector sets.
Definition: GV2.c:444
int gk_draw_path(Viewnode *views, int steps, Keylist *keys)
Draw path.
Definition: gk.c:740
Viewnode * gk_make_linear_framesfromkeys(Keylist *keys, int keysteps, int newsteps, int loop)
Generate viewnode from keyframe list (linear interpolation)
Definition: gk.c:606
void GS_draw_all_list(void)
Draw all glLists.
Definition: GS2.c:876
void * malloc(YYSIZE_T)
void GVL_alldraw_vol(void)
Draw all volume sets.
Definition: GVL2.c:398
void GK_set_numsteps(int newsteps)
Set the number of frames to be interpolated from keyframes.
Definition: GK2.c:300
void gk_follow_frames(Viewnode *view, int numsteps, Keylist *keys, int step, int onestep, int render, unsigned long mode)
Checks key masks.
Definition: gk.c:143
flag
Definition: tools.py:1403
int GK_delete_key(float pos, float precis, int justone)
Definition: GK2.c:370
int GK_add_key(float pos, unsigned long fmask, int force_replace, float precis)
Add keyframe.
Definition: GK2.c:432
void GS_alldraw_wire(void)
Draw all wires.
Definition: GS2.c:1916
void GK_show_list(int flag)
Show list.
Definition: GK2.c:614
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
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
fclose(fd)
int GK_set_interpmode(int mode)
Set interpolation mode.
Definition: GK2.c:143
void free(void *)
void GK_show_path(int flag)
Draw the current path.
Definition: GK2.c:505
void GK_showtension_start(void)
Definition: GK2.c:178
void GK_update_frames(void)
Recalculate path using the current number of frames requested.
Definition: GK2.c:246
void GK_clear_keys(void)
Deletes all keyframes, resets field masks.
Definition: GK2.c:313
void GK_show_vect(int flag)
Show vector sets.
Definition: GK2.c:532
void GK_print_keys(const char *name)
Print keyframe info.
Definition: GK2.c:212
void GK_do_framestep(int step, int render)
Moves the animation to frame number &quot;step&quot;.
Definition: GK2.c:489
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
void GS_ready_draw(void)
Definition: GS2.c:2486
void GK_set_tension(float tens)
Set value for tension when interpmode is KF_SPLINE.
Definition: GK2.c:158
int GK_move_key(float oldpos, float precis, float newpos)
Move keyframe.
Definition: GK2.c:339
int GS_get_fov(void)
Get fied of view.
Definition: GS2.c:2853
void GK_show_vol(int flag)
Show volumes.
Definition: GK2.c:586
void GK_show_site(int flag)
Show point sets.
Definition: GK2.c:558
void GS_set_draw(int where)
Sets which buffer to draw to.
Definition: GS2.c:2457
tuple mode
Definition: tools.py:1481