trans.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*!
  2. \file trans.c
  3. \brief OGSF library - matrix transformation (higher level functions)
  4. GRASS OpenGL gsurf OGSF Library
  5. NOTE: This file should be REMOVED and any calls to the functions in this
  6. file should be replaced with appropriate OpenGL calls.
  7. This routine should be available in GL!
  8. Arguments are same as GL counterparts
  9. I threw this code together in January at the beginning of this
  10. class. I was still learning about GL at the time.
  11. There are many places where the code could be improved.
  12. (C) 1999-2008 by the GRASS Development Team
  13. This program is free software under the
  14. GNU General Public License (>=v2).
  15. Read the file COPYING that comes with GRASS
  16. for details.
  17. \author Dave Gerdes Jan 1990 All rights reserved, US Army Construction Engineering Research Lab
  18. \author Bill Brown USACERL (November 1993)
  19. \author Doxygenized by Martin Landa <landa.martin gmail.com> (May 2008)
  20. */
  21. #include <math.h>
  22. #include <grass/gis.h>
  23. #include <grass/glocale.h>
  24. #include <grass/ogsf.h>
  25. #define MAX_STACK 20
  26. /* function prototypes */
  27. static void P__transform(int num_vert, float (*in)[4],
  28. float (*out)[4], float (*c)[4]);
  29. static void P_matrix_copy(float (*from)[4], float (*to)[4], int size);
  30. /* global variables */
  31. static float c_stack[MAX_STACK][4][4]; /* matrix stack */
  32. static int stack_ptr = -1; /* index of curr matrix depth */
  33. static float d[4][4]; /* tmp matrix */
  34. #define NPI M_PI
  35. /*
  36. ** Current transformation matrix
  37. */
  38. static float trans_mat[4][4] = {
  39. {1., 0., 0., 0.},
  40. {0., 1., 0., 0.},
  41. {0., 0., 1., 0.},
  42. {0., 0., 0., 1.}
  43. };
  44. static float ident[4][4] = {
  45. {1., 0., 0., 0.},
  46. {0., 1., 0., 0.},
  47. {0., 0., 1., 0.},
  48. {0., 0., 0., 1.}
  49. };
  50. /*!
  51. \brief ADD
  52. \param x,y,z
  53. */
  54. void P_scale(float x, float y, float z)
  55. {
  56. d[0][0] = x;
  57. d[0][1] = 0.;
  58. d[0][2] = 0.;
  59. d[0][3] = 0.;
  60. d[1][0] = 0.;
  61. d[1][1] = y;
  62. d[1][2] = 0.;
  63. d[1][3] = 0.;
  64. d[2][0] = 0.;
  65. d[2][1] = 0.;
  66. d[2][2] = z;
  67. d[2][3] = 0.;
  68. d[3][0] = 0.;
  69. d[3][1] = 0.;
  70. d[3][2] = 0.;
  71. d[3][3] = 1.;
  72. /*
  73. ** will write into 1 down on matrix stack
  74. ** and then the popmatrix() will place it as the current T matrix
  75. */
  76. P_pushmatrix();
  77. P__transform(4, d, c_stack[stack_ptr], trans_mat);
  78. P_popmatrix();
  79. return;
  80. }
  81. /*!
  82. \brief Transform array of vectors using current T matrix
  83. Multiply 'in' matrix (homogenous coordinate generally) by
  84. the current transformation matrix, placing the result in 'out'
  85. [in][trans_mat] => [out]
  86. \param num_vert
  87. \param in
  88. \param out
  89. */
  90. void P_transform(int num_vert, float (*in)[4], float (*out)[4])
  91. {
  92. P__transform(num_vert, in, out, trans_mat);
  93. return;
  94. }
  95. /*!
  96. \brief Transform array of vectors using current T matrix
  97. Multiply 'in' matrix (homogenous coordinate generally) by
  98. the current transformation matrix, placing the result in 'out'
  99. [in][trans_mat] => [out]
  100. \param num_vert
  101. \param in
  102. \param out
  103. */
  104. static void P__transform(int num_vert, float (*in)[4], float (*out)[4],
  105. float (*c)[4])
  106. {
  107. register int k, j, i;
  108. for (i = 0; i < num_vert; i++) {
  109. for (j = 0; j < 4; j++) {
  110. out[i][j] = 0.;
  111. for (k = 0; k < 4; k++) {
  112. out[i][j] += in[i][k] * c[k][j];
  113. }
  114. }
  115. }
  116. return;
  117. }
  118. /*!
  119. \brief Copy matrix
  120. \param from 'from' matrix
  121. \param to 'to' matrix
  122. \param size number of rows (ncols=4)
  123. */
  124. static void P_matrix_copy(float (*from)[4], float (*to)[4], int size)
  125. {
  126. register int i, j;
  127. for (i = 0; i < size; i++) {
  128. for (j = 0; j < 4; j++) {
  129. to[i][j] = from[i][j];
  130. }
  131. }
  132. return;
  133. }
  134. /*!
  135. \brief Push current transformation matrix onto matrix stack
  136. */
  137. int P_pushmatrix(void)
  138. {
  139. if (stack_ptr >= MAX_STACK) {
  140. G_warning("P_pushmatrix(): %s", _("Out of matrix stack space"));
  141. return (-1);
  142. }
  143. stack_ptr++;
  144. P_matrix_copy(trans_mat, c_stack[stack_ptr], 4);
  145. return (0);
  146. }
  147. /*!
  148. \brief Pop top of matrix stack, placing it into the current transformation matrix
  149. \return -1 on failure
  150. \return 0 on success
  151. */
  152. int P_popmatrix(void)
  153. {
  154. if (stack_ptr < 0) {
  155. G_warning("P_popmatrix(): %s", _("Tried to pop an empty stack"));
  156. return (-1);
  157. }
  158. P_matrix_copy(c_stack[stack_ptr], trans_mat, 4);
  159. stack_ptr--;
  160. return (0);
  161. }
  162. /*!
  163. \brief Rotate matrix
  164. \param angle angle value
  165. \param axis ('x, 'y', 'z')
  166. */
  167. void P_rot(float angle, char axis)
  168. {
  169. double theta;
  170. P_matrix_copy(ident, d, 4);
  171. theta = (NPI / 180.) * angle; /* convert to radians */
  172. /* optimize to handle rotations of mutliples of 90 deg */
  173. switch (axis) {
  174. case 'X':
  175. case 'x':
  176. d[1][1] = cos(theta);
  177. d[1][2] = sin(theta);
  178. d[2][1] = -sin(theta);
  179. d[2][2] = cos(theta);
  180. break;
  181. case 'Y':
  182. case 'y':
  183. d[0][0] = cos(theta);
  184. d[0][2] = -sin(theta);
  185. d[2][0] = sin(theta);
  186. d[2][2] = cos(theta);
  187. break;
  188. case 'Z':
  189. case 'z':
  190. d[0][0] = cos(theta);
  191. d[0][1] = sin(theta);
  192. d[1][0] = -sin(theta);
  193. d[1][1] = cos(theta);
  194. break;
  195. }
  196. P_pushmatrix();
  197. P__transform(4, d, c_stack[stack_ptr], trans_mat);
  198. P_popmatrix();
  199. return;
  200. }