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