PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/brlcad/branches/bottie/src/anim/anim_script.c

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
C | 270 lines | 192 code | 27 blank | 51 comment | 33 complexity | b6428e2aac344e31bc74693cd8b0c34c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, Apache-2.0, AGPL-3.0, LGPL-3.0, GPL-3.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, 0BSD, BSD-3-Clause
  1. /* A N I M _ S C R I P T . C
  2. * BRL-CAD
  3. *
  4. * Copyright (c) 1993-2011 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_script.c
  22. *
  23. * Turn an animation table into an animation script suitable for
  24. * use by rt. Anim_script.c makes a script for one object at a time (or the
  25. * virtual camera). Some of the available options include rotation
  26. * only, translation only, automatic steering, and specifying reference
  27. * coordinates.
  28. *
  29. */
  30. #include "common.h"
  31. #include <math.h>
  32. #include <string.h>
  33. #include <bio.h>
  34. #include "vmath.h"
  35. #include "bu.h"
  36. #include "bn.h"
  37. #include "anim.h"
  38. #define OPT_STR "a:b:c:d:f:m:pqrstv:"
  39. /* info from command line args */
  40. int relative_a, relative_c, axes, translate, quaternion, rotate;/*flags*/
  41. int steer, view, readview, permute; /* flags*/
  42. int first_frame;
  43. fastf_t viewsize;
  44. vect_t centroid, rcentroid, front;
  45. mat_t m_axes, m_rev_axes; /* rotational analogue of centroid */
  46. char mat_cmd[10]; /* default is lmul */
  47. int
  48. get_args(int argc, char **argv)
  49. {
  50. int c, i, yes;
  51. double yaw, pch, rll;
  52. void anim_dx_y_z2mat(fastf_t *, double, double, double), anim_dz_y_x2mat(fastf_t *, double, double, double);
  53. rotate = translate = 1; /* defaults */
  54. quaternion = permute = 0;
  55. bu_strlcpy(mat_cmd, "lmul", sizeof(mat_cmd));
  56. while ((c=bu_getopt(argc, argv, OPT_STR)) != EOF) {
  57. i=0;
  58. switch (c) {
  59. case 'a':
  60. bu_optind -= 1;
  61. sscanf(argv[bu_optind+(i++)], "%lf", &yaw);
  62. sscanf(argv[bu_optind+(i++)], "%lf", &pch);
  63. sscanf(argv[bu_optind+(i++)], "%lf", &rll);
  64. bu_optind += 3;
  65. anim_dx_y_z2mat(m_axes, rll, -pch, yaw);
  66. anim_dz_y_x2mat(m_rev_axes, -rll, pch, -yaw);
  67. axes = 1;
  68. relative_a = 1;
  69. break;
  70. case 'b':
  71. bu_optind -= 1;
  72. sscanf(argv[bu_optind+(i++)], "%lf", &yaw);
  73. sscanf(argv[bu_optind+(i++)], "%lf", &pch);
  74. sscanf(argv[bu_optind+(i++)], "%lf", &rll);
  75. bu_optind += 3;
  76. anim_dx_y_z2mat(m_axes, rll, -pch, yaw);
  77. anim_dz_y_x2mat(m_rev_axes, -rll, pch, -yaw);
  78. axes = 1;
  79. relative_a = 0;
  80. break;
  81. case 'c':
  82. bu_optind -= 1;
  83. sscanf(argv[bu_optind+(i++)], "%lf", centroid);
  84. sscanf(argv[bu_optind+(i++)], "%lf", centroid+1);
  85. sscanf(argv[bu_optind+(i++)], "%lf", centroid+2);
  86. bu_optind += 3;
  87. VREVERSE(rcentroid, centroid);
  88. relative_c = 1;
  89. break;
  90. case 'd':
  91. bu_optind -= 1;
  92. sscanf(argv[bu_optind+(i++)], "%lf", centroid);
  93. sscanf(argv[bu_optind+(i++)], "%lf", centroid+1);
  94. sscanf(argv[bu_optind+(i++)], "%lf", centroid+2);
  95. bu_optind += 3;
  96. VREVERSE(rcentroid, centroid);
  97. relative_c = 0;
  98. break;
  99. case 'f':
  100. sscanf(bu_optarg, "%d", &first_frame);
  101. break;
  102. case 'm':
  103. bu_strlcpy(mat_cmd, bu_optarg, sizeof(mat_cmd));
  104. break;
  105. case 'p':
  106. permute = 1;
  107. break;
  108. case 'q':
  109. quaternion = 1;
  110. break;
  111. case 'r':
  112. rotate = 1;
  113. translate = 0;
  114. break;
  115. case 's':
  116. steer = 1;
  117. relative_a = 0;
  118. rotate = 0;
  119. translate = 1;
  120. break;
  121. case 't':
  122. translate = 1;
  123. rotate = 0;
  124. break;
  125. case 'v':
  126. yes = sscanf(bu_optarg, "%lf", &viewsize);
  127. if (!yes) viewsize = 0.0;
  128. if (viewsize < 0.0)
  129. readview = 1;
  130. view = 1;
  131. break;
  132. default:
  133. fprintf(stderr, "Unknown option: -%c\n", c);
  134. return 0;
  135. }
  136. }
  137. return 1;
  138. }
  139. int
  140. main(int argc, char *argv[])
  141. {
  142. fastf_t yaw, pitch, roll;
  143. vect_t point, zero;
  144. quat_t quat;
  145. mat_t a, m_x;
  146. int val, go, frame, last_steer;
  147. frame=last_steer=go=view=relative_a=relative_c=axes=0;
  148. VSETALL(centroid, 0);
  149. VSETALL(rcentroid, 0);
  150. VSETALL(front, 0);
  151. VSETALL(point, 0);
  152. VSETALL(zero, 0);
  153. yaw = pitch = roll = 0.0;
  154. MAT_IDN(m_axes);
  155. MAT_IDN(m_rev_axes);
  156. MAT_IDN(a);
  157. if (!get_args(argc, argv))
  158. fprintf(stderr, "anim_script: Get_args error\n");
  159. frame = (steer) ? first_frame -1 : first_frame;
  160. if (view && (viewsize > 0.0))
  161. printf("viewsize %.10g;\n", viewsize);
  162. while (1) {
  163. /* read one line of table */
  164. val = scanf("%*f"); /*ignore time */
  165. if (readview)
  166. val = scanf("%lf", &viewsize);
  167. if (translate)
  168. val = scanf("%lf %lf %lf", point, point+1, point+2);
  169. if (rotate&&quaternion) {
  170. val = scanf("%lf %lf %lf %lf", quat, quat+1, quat+2, quat+3);
  171. val -= 1;
  172. } else if (rotate) {
  173. val = scanf("%lf %lf %lf", &yaw, &pitch, &roll);
  174. }
  175. if (val < 3 && !readview) {
  176. /* ie. scanf not completely successful */
  177. /* with steering option, must go extra loop after end of file */
  178. if (steer && !last_steer)
  179. last_steer = 1;
  180. else break;
  181. }
  182. /* calculate basic rotation matrix a */
  183. if (steer)
  184. go = anim_steer_mat(a, point, last_steer); /* warning: point changed by anim_steer_mat */
  185. else if (quaternion) {
  186. anim_quat2mat(a, quat);
  187. go = 1;
  188. } else {
  189. anim_dx_y_z2mat(a, roll, -pitch, yaw);/* make ypr matrix */
  190. go = 1;
  191. }
  192. /* if input orientation (presumably from quaternion) was
  193. * designed to manipulate the view, first move the object
  194. * to the default object position */
  195. if (permute)
  196. anim_v_unpermute(a);
  197. /* make final matrix, including translation etc */
  198. if (axes) {
  199. /* add pre-rotation from original axes */
  200. bn_mat_mul(m_x, a, m_rev_axes);
  201. MAT_MOVE(a, m_x);
  202. }
  203. anim_add_trans(a, point, rcentroid); /* add translation */
  204. if (axes && relative_a) {
  205. /* add post-rotation back to original axes */
  206. bn_mat_mul(m_x, m_axes, a);
  207. MAT_MOVE(a, m_x);
  208. }
  209. if (relative_c)
  210. anim_add_trans(a, centroid, zero); /* final translation */
  211. /* print one frame of script */
  212. if (go && view) {
  213. printf("start %d;\n", frame);
  214. printf("clean;\n");
  215. if (readview)
  216. printf("viewsize %.10g;\n", viewsize);
  217. printf("eye_pt %.10g %.10g %.10g;\n", a[3], a[7], a[11]);
  218. /* implicit anim_v_permute */
  219. printf("viewrot %.10g %.10g %.10g 0\n", -a[1], -a[5], -a[9]);
  220. printf("%.10g %.10g %.10g 0\n", a[2], a[6], a[10]);
  221. printf("%.10g %.10g %.10g 0\n", -a[0], -a[4], -a[8]);
  222. printf("0 0 0 1;\n");
  223. printf("end;\n");
  224. } else if (go) {
  225. printf("start %d;\n", frame);
  226. printf("clean;\n");
  227. printf("anim %s matrix %s\n", *(argv+bu_optind), mat_cmd);
  228. anim_mat_print(stdout, a, 1);
  229. printf("end;\n");
  230. }
  231. frame++;
  232. }
  233. return 0;
  234. }
  235. /*
  236. * Local Variables:
  237. * mode: C
  238. * tab-width: 8
  239. * indent-tabs-mode: t
  240. * c-file-style: "stroustrup"
  241. * End:
  242. * ex: shiftwidth=4 tabstop=8
  243. */