GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
trans.c
Go to the documentation of this file.
1 
31 #include <math.h>
32 
33 #include <grass/gis.h>
34 #include <grass/glocale.h>
35 #include <grass/gstypes.h>
36 
37 #define MAX_STACK 20
38 
39 
40 /* function prototypes */
41 static void P__transform(int num_vert, float (*in)[4],
42  float (*out)[4], float (*c)[4]);
43 static void P_matrix_copy(float (*from)[4], float (*to)[4], int size);
44 
45 
46 /* global variables */
47 static float c_stack[MAX_STACK][4][4]; /* matrix stack */
48 static int stack_ptr = -1; /* index of curr matrix depth */
49 static float d[4][4]; /* tmp matrix */
50 
51 #define NPI M_PI
52 
53 /*
54  ** Current transformation matrix
55  */
56 static float trans_mat[4][4] = {
57  {1., 0., 0., 0.},
58  {0., 1., 0., 0.},
59  {0., 0., 1., 0.},
60  {0., 0., 0., 1.}
61 };
62 
63 static float ident[4][4] = {
64  {1., 0., 0., 0.},
65  {0., 1., 0., 0.},
66  {0., 0., 1., 0.},
67  {0., 0., 0., 1.}
68 };
69 
75 void P_scale(float x, float y, float z)
76 {
77  d[0][0] = x;
78  d[0][1] = 0.;
79  d[0][2] = 0.;
80  d[0][3] = 0.;
81  d[1][0] = 0.;
82  d[1][1] = y;
83  d[1][2] = 0.;
84  d[1][3] = 0.;
85  d[2][0] = 0.;
86  d[2][1] = 0.;
87  d[2][2] = z;
88  d[2][3] = 0.;
89  d[3][0] = 0.;
90  d[3][1] = 0.;
91  d[3][2] = 0.;
92  d[3][3] = 1.;
93 
94  /*
95  ** will write into 1 down on matrix stack
96  ** and then the popmatrix() will place it as the current T matrix
97  */
98  P_pushmatrix();
99  P__transform(4, d, c_stack[stack_ptr], trans_mat);
100  P_popmatrix();
101 
102  return;
103 }
104 
117 void P_transform(int num_vert, float (*in)[4], float (*out)[4])
118 {
119  P__transform(num_vert, in, out, trans_mat);
120 
121  return;
122 }
123 
136 static void P__transform(int num_vert, float (*in)[4], float (*out)[4],
137  float (*c)[4])
138 {
139  register int k, j, i;
140 
141  for (i = 0; i < num_vert; i++) {
142  for (j = 0; j < 4; j++) {
143  out[i][j] = 0.;
144 
145  for (k = 0; k < 4; k++) {
146  out[i][j] += in[i][k] * c[k][j];
147  }
148  }
149  }
150 
151  return;
152 }
153 
161 static void P_matrix_copy(float (*from)[4], float (*to)[4], int size)
162 {
163  register int i, j;
164 
165  for (i = 0; i < size; i++) {
166  for (j = 0; j < 4; j++) {
167  to[i][j] = from[i][j];
168  }
169  }
170 
171  return;
172 }
173 
177 int P_pushmatrix(void)
178 {
179  if (stack_ptr >= MAX_STACK) {
180  G_warning("P_pushmatrix(): %s", _("Out of matrix stack space"));
181 
182  return (-1);
183  }
184 
185  stack_ptr++;
186  P_matrix_copy(trans_mat, c_stack[stack_ptr], 4);
187 
188  return (0);
189 }
190 
197 int P_popmatrix(void)
198 {
199  if (stack_ptr < 0) {
200  G_warning("P_popmatrix(): %s", _("Tried to pop an empty stack"));
201 
202  return (-1);
203  }
204 
205  P_matrix_copy(c_stack[stack_ptr], trans_mat, 4);
206  stack_ptr--;
207 
208  return (0);
209 }
210 
217 void P_rot(float angle, char axis)
218 {
219  double theta;
220 
221  P_matrix_copy(ident, d, 4);
222 
223  theta = (NPI / 180.) * angle; /* convert to radians */
224 
225  /* optimize to handle rotations of mutliples of 90 deg */
226  switch (axis) {
227  case 'X':
228  case 'x':
229 
230  d[1][1] = cos(theta);
231  d[1][2] = sin(theta);
232  d[2][1] = -sin(theta);
233  d[2][2] = cos(theta);
234 
235  break;
236  case 'Y':
237  case 'y':
238 
239  d[0][0] = cos(theta);
240  d[0][2] = -sin(theta);
241  d[2][0] = sin(theta);
242  d[2][2] = cos(theta);
243  break;
244  case 'Z':
245  case 'z':
246 
247  d[0][0] = cos(theta);
248  d[0][1] = sin(theta);
249  d[1][0] = -sin(theta);
250  d[1][1] = cos(theta);
251 
252  break;
253  }
254 
255  P_pushmatrix();
256  P__transform(4, d, c_stack[stack_ptr], trans_mat);
257  P_popmatrix();
258 
259  return;
260 }
void P_transform(int num_vert, float(*in)[4], float(*out)[4])
Transform array of vectors using current T matrix.
Definition: trans.c:117
#define NPI
Definition: trans.c:51
void P_rot(float angle, char axis)
Rotate matrix.
Definition: trans.c:217
int P_popmatrix(void)
Pop top of matrix stack, placing it into the current transformation matrix.
Definition: trans.c:197
int y
Definition: plot.c:34
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
int P_pushmatrix(void)
Push current transformation matrix onto matrix stack.
Definition: trans.c:177
#define MAX_STACK
Definition: trans.c:37
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
tuple axis
Definition: tools.py:1597
void P_scale(float x, float y, float z)
ADD.
Definition: trans.c:75