/brlcad/tags/rel-7-12-2/src/anim/anim_keyread.c

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git · C · 175 lines · 99 code · 28 blank · 48 comment · 21 complexity · bd583dbafb32753746436753bb03024c MD5 · raw file

  1. /* A N I M _ K E Y R E A D . C
  2. * BRL-CAD
  3. *
  4. * Copyright (c) 1993-2008 United States Government as represented by
  5. * the U.S. Army Research Laboratory.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * version 2.1 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this file; see the file named COPYING for more
  18. * information.
  19. *
  20. */
  21. /** @file anim_keyread.c
  22. * Convert an list of mged-style 'savekey' keyframes into an
  23. * animation table
  24. *
  25. * The output table specifies the orientation in one of three ways:
  26. *
  27. * default - quaternions in the order x, y, z, w.
  28. *
  29. * -z option - Eulers angles, in the order xyz. The model axes are
  30. * considered to be rotated about the camera's z, y, and
  31. * x axes (in that order).
  32. *
  33. * -y option - Eulers angles in the form yaw, pitch, roll.
  34. *
  35. * This is more or less a special case of anim_orient.c
  36. *
  37. */
  38. #include "common.h"
  39. #include <stdio.h>
  40. #include <math.h>
  41. #include "vmath.h"
  42. #include "bu.h"
  43. #ifndef M_PI
  44. #define M_PI 3.14159265358979323846
  45. #endif
  46. #define YPR 0
  47. #define XYZ 1
  48. #define QUATERNION 2
  49. #define DEGREES 0
  50. #define RADIANS 1
  51. #define NORMAL 0
  52. #define ERROR1 1
  53. #define ERROR2 2
  54. #define DTOR M_PI/180.0
  55. #define RTOD 180.0/M_PI
  56. int mode;
  57. int units;
  58. extern int bu_optind;
  59. extern char *bu_optarg;
  60. int get_args(int argc, char **argv);
  61. extern void anim_v_unpermute(fastf_t *);
  62. int
  63. main(int argc, char **argv)
  64. {
  65. int c;
  66. fastf_t time, viewsize;
  67. fastf_t eyept[3], viewrot[16], angle[3], quat[4];
  68. int anim_mat2ypr(fastf_t *, fastf_t *), anim_mat2zyx(const fastf_t *, fastf_t *), anim_mat2quat(fastf_t *, const fastf_t *);
  69. if (!get_args(argc, argv))
  70. fprintf(stderr, "anim_keyread: get_args error");
  71. while (!feof(stdin)) {
  72. /* read one keyframe */
  73. scanf("%lf", &time);
  74. scanf("%lf", &viewsize);
  75. scanf("%lf %lf %lf", eyept, eyept+1, eyept+2);
  76. /* read in transposed matrix */
  77. scanf("%lf %lf %lf %lf", viewrot+0, viewrot+4, viewrot+8, viewrot+12);
  78. scanf("%lf %lf %lf %lf", viewrot+1, viewrot+5, viewrot+9, viewrot+13);
  79. scanf("%lf %lf %lf %lf", viewrot+2, viewrot+6, viewrot+10, viewrot+14);
  80. scanf("%lf %lf %lf %lf", viewrot+3, viewrot+7, viewrot+11, viewrot+15);
  81. if (feof(stdin)) break;
  82. printf("%.10g\t%.10g\t%.10g\t%.10g\t%.10g\t", time, viewsize,
  83. eyept[0], eyept[1], eyept[2]);
  84. if (mode==YPR) {
  85. anim_v_unpermute(viewrot);
  86. c = anim_mat2ypr(angle, viewrot);
  87. if (c==ERROR1)
  88. fprintf(stderr, "Warning: yaw and roll arbitrarily defined at time = %f.\n", time);
  89. else if (c==ERROR2)
  90. fprintf(stderr, "Keyread: can't interpret matrix at time = %f.\n", time);
  91. if (units == DEGREES)
  92. VSCALE(angle, angle, RTOD);
  93. printf("%.10g\t%.10g\t%.10g\n", angle[0], angle[1], angle[2]);
  94. }
  95. else if (mode==XYZ) {
  96. c = anim_mat2zyx(angle, viewrot);
  97. if (c==ERROR1)
  98. fprintf(stderr, "Warning: x and z rotations arbitrarily defined at time = %f.\n", time);
  99. else if (c==ERROR2)
  100. fprintf(stderr, "Keyread: can't interpret matrix at time = %f\n.", time);
  101. if (units == DEGREES)
  102. VSCALE(angle, angle, RTOD);
  103. printf("%.10g\t%.10g\t%.10g\n", angle[X], angle[Y], angle[Z]);
  104. }
  105. else if (mode==QUATERNION) {
  106. anim_mat2quat(quat, viewrot);
  107. printf("%.10g\t%.10g\t%.10g\t%.10g\n", quat[X], quat[Y], quat[Z], quat[W]);
  108. }
  109. }
  110. return( 0 );
  111. }
  112. #define OPT_STR "yzqr"
  113. int get_args(int argc, char **argv)
  114. {
  115. int c;
  116. mode = QUATERNION; /* default */
  117. units = DEGREES;
  118. while ( (c=bu_getopt(argc, argv, OPT_STR)) != EOF) {
  119. switch (c) {
  120. case 'y':
  121. mode = YPR;
  122. break;
  123. case 'z':
  124. mode = XYZ;
  125. break;
  126. case 'q':
  127. mode = QUATERNION;
  128. break;
  129. case 'r':
  130. units = RADIANS;
  131. break;
  132. default:
  133. fprintf(stderr, "Unknown option: -%c\n", c);
  134. return(0);
  135. }
  136. }
  137. return(1);
  138. }
  139. /*
  140. * Local Variables:
  141. * mode: C
  142. * tab-width: 8
  143. * indent-tabs-mode: t
  144. * c-file-style: "stroustrup"
  145. * End:
  146. * ex: shiftwidth=4 tabstop=8
  147. */