/brlcad/tags/rel-4-4/anim/anim_orient.c

https://bitbucket.org/vrrm/brl-cad-copy-for-fast-history-browsing-in-git · C · 226 lines · 171 code · 23 blank · 32 comment · 66 complexity · 63e43afeefd91de5013208fa817a58b6 MD5 · raw file

  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, xyz angles, pre-multiplication
  5. * (object) matrices, and post-multiplication (view) matrices.
  6. * The conversion is done by converting each input form to a matrix,
  7. * and then converting that matrix to the desired output form.
  8. * Options include specifying angles in radians and applying a special
  9. * permutation to the interior matrix in order to handle the virtual camera
  10. * correctly in yaw-pitch-roll form.
  11. *
  12. * Author -
  13. * Carl J. Nuzman
  14. *
  15. * Source -
  16. * The U. S. Army Research Laboratory
  17. * Aberdeen Proving Ground, Maryland 21005-5068 USA
  18. *
  19. * Distribution Notice -
  20. * Re-distribution of this software is restricted, as described in
  21. * your "Statement of Terms and Conditions for the Release of
  22. * The BRL-CAD Pacakge" agreement.
  23. *
  24. * Copyright Notice -
  25. * This software is Copyright (C) 1993 by the United States Army
  26. * in all countries except the USA. All rights reserved.
  27. */
  28. #include "conf.h"
  29. #include <stdio.h>
  30. #include <math.h>
  31. #include "machine.h"
  32. #include "vmath.h"
  33. #ifndef M_PI
  34. #define M_PI 3.14159265358979323846
  35. #endif
  36. #define YPR 0
  37. #define XYZ 1
  38. #define QUAT 2
  39. #define V_MAT 3
  40. #define O_MAT 4
  41. #define DEGREES 0
  42. #define RADIANS 1
  43. #define NORMAL 0
  44. #define ERROR1 1
  45. #define ERROR2 2
  46. #define DTOR M_PI/180.0
  47. #define RTOD 180.0/M_PI
  48. extern int optind;
  49. extern char *optarg;
  50. int input_mode, output_mode, length, input_units, output_units, permute;
  51. main(argc,argv)
  52. int argc;
  53. char **argv;
  54. {
  55. int num_read;
  56. fastf_t angle[3],quat[4],matrix[16];
  57. void zyx2mat(),ypr2mat(),quat2mat(), an_mat_print();
  58. int mat2ypr(),mat2zyx(),mat2quat();
  59. if(!get_args(argc,argv))
  60. fprintf(stderr,"Get_args error.\n");
  61. /* read data */
  62. num_read = length;
  63. while (1){
  64. if ((input_mode==YPR)||(input_mode==XYZ)){
  65. num_read = scanf("%lf %lf %lf",angle,angle+1,angle+2);
  66. }
  67. else if (input_mode==QUAT){
  68. num_read = scanf("%lf %lf %lf %lf", quat,quat+1,quat+2,quat+3);
  69. }
  70. else if ((input_mode==V_MAT)||(input_mode==O_MAT)){
  71. num_read = 0;
  72. num_read += scanf("%lf %lf %lf %lf",matrix,matrix+1,matrix+2,matrix+3);
  73. num_read += scanf("%lf %lf %lf %lf",matrix+4,matrix+5,matrix+6,matrix+7);
  74. num_read += scanf("%lf %lf %lf %lf",matrix+8,matrix+9,matrix+10,matrix+11);
  75. num_read += scanf("%lf %lf %lf %lf",matrix+12,matrix+13,matrix+14,matrix+15);
  76. }
  77. if (num_read < length)
  78. break;
  79. /* convert to radians if in degrees */
  80. if (input_units==DEGREES) {
  81. angle[0] *= DTOR;
  82. angle[1] *= DTOR;
  83. angle[2] *= DTOR;
  84. }
  85. /* convert to (object) matrix form */
  86. if (input_mode==YPR){
  87. ypr2mat(matrix,angle);
  88. }
  89. else if (input_mode==XYZ){
  90. zyx2mat(matrix,angle);
  91. }
  92. else if (input_mode==QUAT){
  93. quat2mat(matrix,quat);
  94. }
  95. else if (input_mode==V_MAT){
  96. transpose(matrix);
  97. }
  98. if (permute){
  99. if (input_mode==YPR)
  100. v_permute(matrix);
  101. else if (output_mode==YPR)
  102. un_v_permute(matrix);
  103. }
  104. /* convert from matrix form and print result*/
  105. if (output_mode==YPR){
  106. mat2ypr(angle,matrix);
  107. if (output_units==DEGREES)
  108. VSCALE(angle,angle,RTOD);
  109. printf("%f\t%f\t%f\n",angle[0],angle[1],angle[2]);
  110. }
  111. else if (output_mode==XYZ){
  112. mat2zyx(angle,matrix);
  113. if (output_units==DEGREES)
  114. VSCALE(angle,angle,RTOD);
  115. printf("%f\t%f\t%f\n",angle[0],angle[1],angle[2]);
  116. }
  117. else if (output_mode==QUAT){
  118. mat2quat(quat,matrix);
  119. printf("%f\t%f\t%f\t%f\n",quat[0],quat[1],quat[2],quat[3]);
  120. }
  121. else if (output_mode==V_MAT){
  122. transpose(matrix);
  123. an_mat_print(matrix,0);
  124. printf("\n");
  125. }
  126. else if (output_mode==O_MAT){
  127. an_mat_print(matrix,0);
  128. printf("\n");
  129. }
  130. }
  131. }
  132. #define OPT_STR "i:o:pr"
  133. int get_args(argc,argv)
  134. int argc;
  135. char **argv;
  136. {
  137. int c;
  138. /* defaults */
  139. input_mode = QUAT;
  140. output_mode = QUAT;
  141. input_units = DEGREES;
  142. output_units = DEGREES;
  143. permute = 0;
  144. while ( (c=getopt(argc,argv,OPT_STR)) != EOF) {
  145. switch(c){
  146. case 'i':
  147. if (*optarg == 'y'){
  148. input_mode = YPR;
  149. length = 3;
  150. if (*(optarg+1)=='r')
  151. input_units = RADIANS;
  152. }
  153. else if (*optarg == 'z'){
  154. input_mode = XYZ;
  155. length = 3;
  156. if (*(optarg+1)=='r')
  157. input_units = RADIANS;
  158. }
  159. else if (*optarg == 'q'){
  160. input_mode = QUAT;
  161. length = 4;
  162. }
  163. else if (*optarg == 'v'){
  164. input_mode = V_MAT;
  165. length = 16;
  166. }
  167. else if (*optarg == 'o'){
  168. input_mode = O_MAT;
  169. length = 16;
  170. }
  171. break;
  172. case 'o':
  173. if (*optarg == 'y'){
  174. output_mode = YPR;
  175. if (*(optarg+1)=='r')
  176. output_units = RADIANS;
  177. }
  178. else if (*optarg == 'z'){
  179. output_mode = XYZ;
  180. if (*(optarg+1)=='r')
  181. output_units = RADIANS;
  182. }
  183. else if (*optarg == 'q')
  184. output_mode = QUAT;
  185. else if (*optarg == 'v')
  186. output_mode = V_MAT;
  187. else if (*optarg == 'o')
  188. output_mode = O_MAT;
  189. break;
  190. case 'p':
  191. permute = 1;
  192. break;
  193. case 'r':
  194. input_units = RADIANS;
  195. output_units = RADIANS;
  196. break;
  197. default:
  198. fprintf(stderr,"Unknown option: -%c\n",c);
  199. return(0);
  200. }
  201. }
  202. return(1);
  203. }