PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/brlcad/tags/rel-6-0-2/anim/anim_orient.c

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git
C | 290 lines | 232 code | 22 blank | 36 comment | 23 complexity | 6269a3f45515aef2460fd64ac68244b9 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 _ O R I E N T . C
  2. *
  3. * Convert between different orientation formats. The formats are:
  4. * quaternion, yaw-pitch-roll, azimuth-elevation-twist, xyz angles,
  5. * pre-multiplication rotation matrices, and transposed matrices (inverses).
  6. * By default, the information is assumed to represent a transformation
  7. * which should be an object which initially faces the x-axis, with the
  8. * z-axis going up. Alternatively, the information can be interpreted as
  9. * transformations which should be applied to an object initially facing the
  10. * negative z-axis, with the y-axis going up.
  11. * The conversion is done by converting each input form to a matrix,
  12. * and then converting that matrix to the desired output form.
  13. * Angles may be specified in radians or degrees.
  14. *
  15. * Author -
  16. * Carl J. Nuzman
  17. *
  18. * Source -
  19. * The U. S. Army Research Laboratory
  20. * Aberdeen Proving Ground, Maryland 21005-5068 USA
  21. *
  22. * Distribution Notice -
  23. * Re-distribution of this software is restricted, as described in
  24. * your "Statement of Terms and Conditions for the Release of
  25. * The BRL-CAD Pacakge" agreement.
  26. *
  27. * Copyright Notice -
  28. * This software is Copyright (C) 1993 by the United States Army
  29. * in all countries except the USA. All rights reserved.
  30. */
  31. #include "conf.h"
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <stdio.h>
  35. #include <math.h>
  36. #include "machine.h"
  37. #include "vmath.h"
  38. #ifndef M_PI
  39. #define M_PI 3.14159265358979323846
  40. #endif
  41. #define YPR 0
  42. #define XYZ 1
  43. #define AET 2
  44. #define QUAT 3
  45. #define MAT 4
  46. #define DEGREES 0
  47. #define RADIANS 1
  48. #define ANIM_NORMAL 0
  49. #define ANIM_ERROR1 1
  50. #define ANIM_ERROR2 2
  51. #define DTOR M_PI/180.0
  52. #define RTOD 180.0/M_PI
  53. int parse_args();
  54. extern void anim_y_p_r2mat();
  55. extern void anim_tran();
  56. extern void anim_v_unpermute();
  57. extern void anim_dirn2mat();
  58. extern void anim_v_permute();
  59. extern int optind;
  60. extern char *optarg;
  61. int upright;
  62. int input_mode, output_mode, length, input_units, output_units;
  63. int input_perm, output_perm, input_inv, output_inv;
  64. int
  65. main(argc,argv)
  66. int argc;
  67. char **argv;
  68. {
  69. int num_read;
  70. fastf_t temp[3], temp2[3],angle[3],quat[4],matrix[16];
  71. void anim_zyx2mat(),anim_ypr2mat(),anim_quat2mat(), anim_mat_print();
  72. int anim_mat2ypr(),anim_mat2zyx(),anim_mat2quat();
  73. if(!parse_args(argc,argv)) {
  74. fprintf(stderr,"Get_args error.\n");
  75. exit(0);
  76. }
  77. /* read data */
  78. num_read = length;
  79. while (1){
  80. switch (input_mode) {
  81. case YPR:
  82. case XYZ:
  83. case AET:
  84. num_read = scanf("%lf %lf %lf",angle,angle+1,angle+2);
  85. /* convert to radians if in degrees */
  86. if (input_units==DEGREES) {
  87. VSCALE(angle,angle,DTOR);
  88. }
  89. break;
  90. case QUAT:
  91. num_read = scanf("%lf %lf %lf %lf", quat,quat+1,quat+2,quat+3);
  92. break;
  93. case MAT:
  94. num_read = 0;
  95. num_read += scanf("%lf %lf %lf %lf",matrix,matrix+1,matrix+2,matrix+3);
  96. num_read += scanf("%lf %lf %lf %lf",matrix+4,matrix+5,matrix+6,matrix+7);
  97. num_read += scanf("%lf %lf %lf %lf",matrix+8,matrix+9,matrix+10,matrix+11);
  98. num_read += scanf("%lf %lf %lf %lf",matrix+12,matrix+13,matrix+14,matrix+15);
  99. break;
  100. }
  101. if (num_read < length)
  102. break;
  103. /* convert to (object) matrix form */
  104. switch (input_mode) {
  105. case YPR:
  106. anim_ypr2mat(matrix,angle);
  107. break;
  108. case AET:
  109. anim_y_p_r2mat(matrix, angle[0]+M_PI, -angle[1],-angle[2]);
  110. break;
  111. case XYZ:
  112. anim_zyx2mat(matrix,angle);
  113. break;
  114. case QUAT:
  115. anim_quat2mat(matrix,quat);
  116. break;
  117. }
  118. if (input_inv){
  119. anim_tran(matrix);
  120. }
  121. if (input_perm){
  122. anim_v_unpermute(matrix);
  123. }
  124. /* end of input conversion, begin output conversion*/
  125. if (upright) { /* force right-side up */
  126. VSET(temp, matrix[0], matrix[4], matrix[8]);
  127. VSET(temp2, matrix[1], matrix[5], matrix[9]);
  128. anim_dirn2mat(matrix,temp,temp2);
  129. }
  130. if (output_perm){
  131. anim_v_permute(matrix);
  132. }
  133. if (output_inv){
  134. anim_tran(matrix);
  135. }
  136. /* convert from matrix form and print result*/
  137. switch (output_mode) {
  138. case YPR:
  139. anim_mat2ypr(angle,matrix);
  140. if (output_units==DEGREES)
  141. VSCALE(angle,angle,RTOD);
  142. printf("%.12g\t%.12g\t%.12g\n",angle[0],angle[1],angle[2]);
  143. break;
  144. case AET:
  145. anim_mat2ypr(angle,matrix);
  146. if (angle[0] > 0.0) {
  147. angle[0] -= M_PI;
  148. } else {
  149. angle[0] += M_PI;
  150. }
  151. angle[1] = -angle[1];
  152. angle[2] = -angle[2];
  153. if (output_units==DEGREES)
  154. VSCALE(angle,angle,RTOD);
  155. printf("%.12g\t%.12g\t%.12g\n",angle[0],angle[1],angle[2]);
  156. break;
  157. case XYZ:
  158. anim_mat2zyx(angle,matrix);
  159. if (output_units==DEGREES)
  160. VSCALE(angle,angle,RTOD);
  161. printf("%.12g\t%.12g\t%.12g\n",angle[0],angle[1],angle[2]);
  162. break;
  163. case QUAT:
  164. anim_mat2quat(quat,matrix);
  165. printf("%.12g\t%.12g\t%.12g\t%.12g\n",quat[0],quat[1],quat[2],quat[3]);
  166. break;
  167. case MAT:
  168. anim_mat_print(stdout,matrix,0);
  169. printf("\n");
  170. }
  171. }
  172. return( 0 );
  173. }
  174. int parse_args(argc,argv)
  175. int argc;
  176. char **argv;
  177. {
  178. int c;
  179. char *cp;
  180. /* defaults */
  181. upright = 0;
  182. input_mode = QUAT;
  183. output_mode = QUAT;
  184. input_units = DEGREES;
  185. output_units = DEGREES;
  186. input_perm = 0;
  187. output_perm = 0;
  188. input_inv = 0;
  189. output_inv = 0;
  190. length = 4;
  191. if (argc > 2) { /*read output mode */
  192. cp = argv[2];
  193. while ( (c=*cp++) ) {
  194. switch (c) {
  195. case 'q':
  196. output_mode = QUAT;
  197. break;
  198. case 'y':
  199. output_mode = YPR;
  200. break;
  201. case 'a':
  202. output_mode = AET;
  203. break;
  204. case 'z':
  205. output_mode = XYZ;
  206. break;
  207. case 'm':
  208. output_mode = MAT;
  209. break;
  210. case 'i':
  211. output_inv = 1;
  212. break;
  213. case 'r':
  214. output_units = RADIANS;
  215. break;
  216. case 'v':
  217. output_perm = 1;
  218. break;
  219. case 'u':
  220. upright = 1;
  221. break;
  222. default:
  223. fprintf(stderr,"anim_orient: unknown output option: %c\n",c);
  224. return(0);
  225. }
  226. }
  227. }
  228. if (argc > 1) { /*read input mode */
  229. cp = argv[1];
  230. while ( (c=*cp++) ) {
  231. switch (c) {
  232. case 'q':
  233. input_mode = QUAT;
  234. length = 4;
  235. break;
  236. case 'y':
  237. input_mode = YPR;
  238. length = 3;
  239. break;
  240. case 'a':
  241. input_mode = AET;
  242. length = 3;
  243. break;
  244. case 'z':
  245. input_mode = XYZ;
  246. length = 3;
  247. break;
  248. case 'm':
  249. input_mode = MAT;
  250. length = 16;
  251. break;
  252. case 'i':
  253. input_inv = 1;
  254. break;
  255. case 'r':
  256. input_units = RADIANS;
  257. break;
  258. case 'v':
  259. input_perm = 1;
  260. break;
  261. default:
  262. fprintf(stderr,"anim_orient: unknown input option: %c\n",c);
  263. return(0);
  264. }
  265. }
  266. }
  267. return(1);
  268. }