/quat/xyzquat.c

https://gitlab.com/sat-metalab/vrpn · C · 190 lines · 49 code · 36 blank · 105 comment · 2 complexity · 71ad40697162e3f365249e1596186cfd MD5 · raw file

  1. /*****************************************************************************
  2. *
  3. xyzquat.c - source for all operations related to the xyzquat data type
  4. Revision History:
  5. Author Date Comments
  6. ------ -------- ----------------------------
  7. Erik Erikson 06/26/92 Added q_xyz_quat_compose
  8. Stefan Gottschalk
  9. Russ Taylor
  10. Rich Holloway 01/25/91 Initial version
  11. Developed at the University of North Carolina at Chapel Hill, supported
  12. by the following grants/contracts:
  13. DARPA #DAEA18-90-C-0044
  14. ONR #N00014-86-K-0680
  15. NIH #5-R24-RR-02170
  16. *
  17. *****************************************************************************/
  18. #include "quat.h"
  19. /*****************************************************************************
  20. *
  21. q_xyz_quat_invert - invert a vector/quaternion transformation pair
  22. input:
  23. - dest and source pointers
  24. output:
  25. - src is inverted and put into dest
  26. notes:
  27. - src and dest may be same
  28. *
  29. *****************************************************************************/
  30. void q_xyz_quat_invert(q_xyz_quat_type *destPtr, const q_xyz_quat_type *srcPtr)
  31. {
  32. /* invert rotation first */
  33. q_invert(destPtr->quat, srcPtr->quat);
  34. /* vec = -vec */
  35. q_vec_invert(destPtr->xyz, srcPtr->xyz);
  36. /* rotate translation offsets into inverted system */
  37. q_xform(destPtr->xyz, destPtr->quat, destPtr->xyz);
  38. } /* q_xyz_quat_invert */
  39. /*****************************************************************************
  40. *
  41. q_row_matrix_to_xyz_quat - converts a row matrix to an xyz_quat
  42. input:
  43. - pointer to each
  44. output:
  45. - new xyz_quat
  46. notes:
  47. - each call to this function takes about .1 milliseconds on pxpl4
  48. a VAX 3200 GPX, as of 4/11/91.
  49. *
  50. *****************************************************************************/
  51. void q_row_matrix_to_xyz_quat(q_xyz_quat_type *xyzQuatPtr, const q_matrix_type rowMatrix)
  52. {
  53. int i;
  54. q_from_row_matrix(xyzQuatPtr->quat, rowMatrix);
  55. for ( i = 0; i < 3; i++ )
  56. xyzQuatPtr->xyz[i] = rowMatrix[3][i];
  57. } /* q_row_matrix_to_xyz_quat */
  58. /*****************************************************************************
  59. *
  60. q_xyz_quat_to_row_matrix - convert an xyz_quat to a row matrix
  61. input:
  62. - pointer to each
  63. output:
  64. - new row matrix
  65. notes:
  66. - each call to this function takes about .14 milliseconds on pxpl4
  67. a VAX 3200 GPX, as of 4/11/91.
  68. *
  69. *****************************************************************************/
  70. void q_xyz_quat_to_row_matrix(q_matrix_type rowMatrix, const q_xyz_quat_type *xyzQuatPtr)
  71. {
  72. int i;
  73. q_to_row_matrix(rowMatrix, xyzQuatPtr->quat);
  74. for ( i = 0; i < 3; i++ )
  75. rowMatrix[3][i] = xyzQuatPtr->xyz[i];
  76. } /* q_xyz_quat_to_row_matrix */
  77. /*
  78. * q_ogl_matrix_to_xyz_quat
  79. */
  80. void q_ogl_matrix_to_xyz_quat(q_xyz_quat_type *xyzQuatPtr, const qogl_matrix_type matrix )
  81. {
  82. q_from_ogl_matrix( xyzQuatPtr->quat, matrix );
  83. xyzQuatPtr->xyz[Q_X] = matrix[12+Q_X];
  84. xyzQuatPtr->xyz[Q_Y] = matrix[12+Q_Y];
  85. xyzQuatPtr->xyz[Q_Z] = matrix[12+Q_Z];
  86. }
  87. /*
  88. * q_xyz_quat_to_ogl_matrix
  89. */
  90. void q_xyz_quat_to_ogl_matrix(qogl_matrix_type matrix, const q_xyz_quat_type *xyzQuatPtr )
  91. {
  92. q_to_ogl_matrix( matrix, xyzQuatPtr->quat );
  93. matrix[12+Q_X] = xyzQuatPtr->xyz[Q_X];
  94. matrix[12+Q_Y] = xyzQuatPtr->xyz[Q_Y];
  95. matrix[12+Q_Z] = xyzQuatPtr->xyz[Q_Z];
  96. }
  97. /*****************************************************************************
  98. *
  99. q_xyz_quat_compose - compose q_xyz_quat_types CfromB and BfromA to
  100. get CfromA
  101. input:
  102. - pointers to q_xyz_quat_types CfromA, CfromB and BfromA
  103. output:
  104. - CfromA
  105. overview:
  106. - treat the BA q_xyz_quat_type as local, since it's closer to
  107. the point:
  108. newp = CB*BA*p
  109. - first xform the xlate part of BA into the CB system by rotating
  110. and scaling it
  111. - add the xformed local xlate to the unchanged global xlate
  112. - then compose the rotation parts by multiplying global*local
  113. *
  114. *****************************************************************************/
  115. void q_xyz_quat_compose(q_xyz_quat_type *C_from_A_ptr,
  116. const q_xyz_quat_type *C_from_B_ptr, const q_xyz_quat_type *B_from_A_ptr)
  117. {
  118. q_vec_type rotated_BA_vec;
  119. /* rotate local xlate into global */
  120. q_xform(rotated_BA_vec, C_from_B_ptr->quat, B_from_A_ptr->xyz);
  121. /* now add the xformed local vec to the unchanged global vec */
  122. q_vec_add(C_from_A_ptr->xyz, C_from_B_ptr->xyz, rotated_BA_vec);
  123. /* compose the rotations */
  124. /* CA_rotate = CB_rotate . BA_rotate */
  125. q_mult(C_from_A_ptr->quat, C_from_B_ptr->quat, B_from_A_ptr->quat);
  126. q_normalize(C_from_A_ptr->quat, C_from_A_ptr->quat);
  127. } /* q_xyz_quat_compose */
  128. // xform a vector by an xyzquat. dest == src ok.
  129. void q_xyz_quat_xform(q_vec_type dest, const q_xyz_quat_type *xf, const q_vec_type src)
  130. {
  131. q_xform(dest, xf->quat, src);
  132. q_vec_add(dest, xf->xyz, dest);
  133. }