[SDK] Provide .const macro for gas
[reactos.git] / dll / opengl / mesa / math / m_eval.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 3.5
5 *
6 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /*
28 * eval.c was written by
29 * Bernd Barsuhn (bdbarsuh@cip.informatik.uni-erlangen.de) and
30 * Volker Weiss (vrweiss@cip.informatik.uni-erlangen.de).
31 *
32 * My original implementation of evaluators was simplistic and didn't
33 * compute surface normal vectors properly. Bernd and Volker applied
34 * used more sophisticated methods to get better results.
35 *
36 * Thanks guys!
37 */
38
39 #include <precomp.h>
40
41 #include <main/config.h>
42
43 static GLfloat inv_tab[MAX_EVAL_ORDER];
44
45 /*
46 * Horner scheme for Bezier curves
47 *
48 * Bezier curves can be computed via a Horner scheme.
49 * Horner is numerically less stable than the de Casteljau
50 * algorithm, but it is faster. For curves of degree n
51 * the complexity of Horner is O(n) and de Casteljau is O(n^2).
52 * Since stability is not important for displaying curve
53 * points I decided to use the Horner scheme.
54 *
55 * A cubic Bezier curve with control points b0, b1, b2, b3 can be
56 * written as
57 *
58 * (([3] [3] ) [3] ) [3]
59 * c(t) = (([0]*s*b0 + [1]*t*b1)*s + [2]*t^2*b2)*s + [3]*t^2*b3
60 *
61 * [n]
62 * where s=1-t and the binomial coefficients [i]. These can
63 * be computed iteratively using the identity:
64 *
65 * [n] [n ] [n]
66 * [i] = (n-i+1)/i * [i-1] and [0] = 1
67 */
68
69
70 void
71 _math_horner_bezier_curve(const GLfloat * cp, GLfloat * out, GLfloat t,
72 GLuint dim, GLuint order)
73 {
74 GLfloat s, powert, bincoeff;
75 GLuint i, k;
76
77 if (order >= 2) {
78 bincoeff = (GLfloat) (order - 1);
79 s = 1.0F - t;
80
81 for (k = 0; k < dim; k++)
82 out[k] = s * cp[k] + bincoeff * t * cp[dim + k];
83
84 for (i = 2, cp += 2 * dim, powert = t * t; i < order;
85 i++, powert *= t, cp += dim) {
86 bincoeff *= (GLfloat) (order - i);
87 bincoeff *= inv_tab[i];
88
89 for (k = 0; k < dim; k++)
90 out[k] = s * out[k] + bincoeff * powert * cp[k];
91 }
92 }
93 else { /* order=1 -> constant curve */
94
95 for (k = 0; k < dim; k++)
96 out[k] = cp[k];
97 }
98 }
99
100 /*
101 * Tensor product Bezier surfaces
102 *
103 * Again the Horner scheme is used to compute a point on a
104 * TP Bezier surface. First a control polygon for a curve
105 * on the surface in one parameter direction is computed,
106 * then the point on the curve for the other parameter
107 * direction is evaluated.
108 *
109 * To store the curve control polygon additional storage
110 * for max(uorder,vorder) points is needed in the
111 * control net cn.
112 */
113
114 void
115 _math_horner_bezier_surf(GLfloat * cn, GLfloat * out, GLfloat u, GLfloat v,
116 GLuint dim, GLuint uorder, GLuint vorder)
117 {
118 GLfloat *cp = cn + uorder * vorder * dim;
119 GLuint i, uinc = vorder * dim;
120
121 if (vorder > uorder) {
122 if (uorder >= 2) {
123 GLfloat s, poweru, bincoeff;
124 GLuint j, k;
125
126 /* Compute the control polygon for the surface-curve in u-direction */
127 for (j = 0; j < vorder; j++) {
128 GLfloat *ucp = &cn[j * dim];
129
130 /* Each control point is the point for parameter u on a */
131 /* curve defined by the control polygons in u-direction */
132 bincoeff = (GLfloat) (uorder - 1);
133 s = 1.0F - u;
134
135 for (k = 0; k < dim; k++)
136 cp[j * dim + k] = s * ucp[k] + bincoeff * u * ucp[uinc + k];
137
138 for (i = 2, ucp += 2 * uinc, poweru = u * u; i < uorder;
139 i++, poweru *= u, ucp += uinc) {
140 bincoeff *= (GLfloat) (uorder - i);
141 bincoeff *= inv_tab[i];
142
143 for (k = 0; k < dim; k++)
144 cp[j * dim + k] =
145 s * cp[j * dim + k] + bincoeff * poweru * ucp[k];
146 }
147 }
148
149 /* Evaluate curve point in v */
150 _math_horner_bezier_curve(cp, out, v, dim, vorder);
151 }
152 else /* uorder=1 -> cn defines a curve in v */
153 _math_horner_bezier_curve(cn, out, v, dim, vorder);
154 }
155 else { /* vorder <= uorder */
156
157 if (vorder > 1) {
158 GLuint i;
159
160 /* Compute the control polygon for the surface-curve in u-direction */
161 for (i = 0; i < uorder; i++, cn += uinc) {
162 /* For constant i all cn[i][j] (j=0..vorder) are located */
163 /* on consecutive memory locations, so we can use */
164 /* horner_bezier_curve to compute the control points */
165
166 _math_horner_bezier_curve(cn, &cp[i * dim], v, dim, vorder);
167 }
168
169 /* Evaluate curve point in u */
170 _math_horner_bezier_curve(cp, out, u, dim, uorder);
171 }
172 else /* vorder=1 -> cn defines a curve in u */
173 _math_horner_bezier_curve(cn, out, u, dim, uorder);
174 }
175 }
176
177 /*
178 * The direct de Casteljau algorithm is used when a point on the
179 * surface and the tangent directions spanning the tangent plane
180 * should be computed (this is needed to compute normals to the
181 * surface). In this case the de Casteljau algorithm approach is
182 * nicer because a point and the partial derivatives can be computed
183 * at the same time. To get the correct tangent length du and dv
184 * must be multiplied with the (u2-u1)/uorder-1 and (v2-v1)/vorder-1.
185 * Since only the directions are needed, this scaling step is omitted.
186 *
187 * De Casteljau needs additional storage for uorder*vorder
188 * values in the control net cn.
189 */
190
191 void
192 _math_de_casteljau_surf(GLfloat * cn, GLfloat * out, GLfloat * du,
193 GLfloat * dv, GLfloat u, GLfloat v, GLuint dim,
194 GLuint uorder, GLuint vorder)
195 {
196 GLfloat *dcn = cn + uorder * vorder * dim;
197 GLfloat us = 1.0F - u, vs = 1.0F - v;
198 GLuint h, i, j, k;
199 GLuint minorder = uorder < vorder ? uorder : vorder;
200 GLuint uinc = vorder * dim;
201 GLuint dcuinc = vorder;
202
203 /* Each component is evaluated separately to save buffer space */
204 /* This does not drasticaly decrease the performance of the */
205 /* algorithm. If additional storage for (uorder-1)*(vorder-1) */
206 /* points would be available, the components could be accessed */
207 /* in the innermost loop which could lead to less cache misses. */
208
209 #define CN(I,J,K) cn[(I)*uinc+(J)*dim+(K)]
210 #define DCN(I, J) dcn[(I)*dcuinc+(J)]
211 if (minorder < 3) {
212 if (uorder == vorder) {
213 for (k = 0; k < dim; k++) {
214 /* Derivative direction in u */
215 du[k] = vs * (CN(1, 0, k) - CN(0, 0, k)) +
216 v * (CN(1, 1, k) - CN(0, 1, k));
217
218 /* Derivative direction in v */
219 dv[k] = us * (CN(0, 1, k) - CN(0, 0, k)) +
220 u * (CN(1, 1, k) - CN(1, 0, k));
221
222 /* bilinear de Casteljau step */
223 out[k] = us * (vs * CN(0, 0, k) + v * CN(0, 1, k)) +
224 u * (vs * CN(1, 0, k) + v * CN(1, 1, k));
225 }
226 }
227 else if (minorder == uorder) {
228 for (k = 0; k < dim; k++) {
229 /* bilinear de Casteljau step */
230 DCN(1, 0) = CN(1, 0, k) - CN(0, 0, k);
231 DCN(0, 0) = us * CN(0, 0, k) + u * CN(1, 0, k);
232
233 for (j = 0; j < vorder - 1; j++) {
234 /* for the derivative in u */
235 DCN(1, j + 1) = CN(1, j + 1, k) - CN(0, j + 1, k);
236 DCN(1, j) = vs * DCN(1, j) + v * DCN(1, j + 1);
237
238 /* for the `point' */
239 DCN(0, j + 1) = us * CN(0, j + 1, k) + u * CN(1, j + 1, k);
240 DCN(0, j) = vs * DCN(0, j) + v * DCN(0, j + 1);
241 }
242
243 /* remaining linear de Casteljau steps until the second last step */
244 for (h = minorder; h < vorder - 1; h++)
245 for (j = 0; j < vorder - h; j++) {
246 /* for the derivative in u */
247 DCN(1, j) = vs * DCN(1, j) + v * DCN(1, j + 1);
248
249 /* for the `point' */
250 DCN(0, j) = vs * DCN(0, j) + v * DCN(0, j + 1);
251 }
252
253 /* derivative direction in v */
254 dv[k] = DCN(0, 1) - DCN(0, 0);
255
256 /* derivative direction in u */
257 du[k] = vs * DCN(1, 0) + v * DCN(1, 1);
258
259 /* last linear de Casteljau step */
260 out[k] = vs * DCN(0, 0) + v * DCN(0, 1);
261 }
262 }
263 else { /* minorder == vorder */
264
265 for (k = 0; k < dim; k++) {
266 /* bilinear de Casteljau step */
267 DCN(0, 1) = CN(0, 1, k) - CN(0, 0, k);
268 DCN(0, 0) = vs * CN(0, 0, k) + v * CN(0, 1, k);
269 for (i = 0; i < uorder - 1; i++) {
270 /* for the derivative in v */
271 DCN(i + 1, 1) = CN(i + 1, 1, k) - CN(i + 1, 0, k);
272 DCN(i, 1) = us * DCN(i, 1) + u * DCN(i + 1, 1);
273
274 /* for the `point' */
275 DCN(i + 1, 0) = vs * CN(i + 1, 0, k) + v * CN(i + 1, 1, k);
276 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
277 }
278
279 /* remaining linear de Casteljau steps until the second last step */
280 for (h = minorder; h < uorder - 1; h++)
281 for (i = 0; i < uorder - h; i++) {
282 /* for the derivative in v */
283 DCN(i, 1) = us * DCN(i, 1) + u * DCN(i + 1, 1);
284
285 /* for the `point' */
286 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
287 }
288
289 /* derivative direction in u */
290 du[k] = DCN(1, 0) - DCN(0, 0);
291
292 /* derivative direction in v */
293 dv[k] = us * DCN(0, 1) + u * DCN(1, 1);
294
295 /* last linear de Casteljau step */
296 out[k] = us * DCN(0, 0) + u * DCN(1, 0);
297 }
298 }
299 }
300 else if (uorder == vorder) {
301 for (k = 0; k < dim; k++) {
302 /* first bilinear de Casteljau step */
303 for (i = 0; i < uorder - 1; i++) {
304 DCN(i, 0) = us * CN(i, 0, k) + u * CN(i + 1, 0, k);
305 for (j = 0; j < vorder - 1; j++) {
306 DCN(i, j + 1) = us * CN(i, j + 1, k) + u * CN(i + 1, j + 1, k);
307 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
308 }
309 }
310
311 /* remaining bilinear de Casteljau steps until the second last step */
312 for (h = 2; h < minorder - 1; h++)
313 for (i = 0; i < uorder - h; i++) {
314 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
315 for (j = 0; j < vorder - h; j++) {
316 DCN(i, j + 1) = us * DCN(i, j + 1) + u * DCN(i + 1, j + 1);
317 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
318 }
319 }
320
321 /* derivative direction in u */
322 du[k] = vs * (DCN(1, 0) - DCN(0, 0)) + v * (DCN(1, 1) - DCN(0, 1));
323
324 /* derivative direction in v */
325 dv[k] = us * (DCN(0, 1) - DCN(0, 0)) + u * (DCN(1, 1) - DCN(1, 0));
326
327 /* last bilinear de Casteljau step */
328 out[k] = us * (vs * DCN(0, 0) + v * DCN(0, 1)) +
329 u * (vs * DCN(1, 0) + v * DCN(1, 1));
330 }
331 }
332 else if (minorder == uorder) {
333 for (k = 0; k < dim; k++) {
334 /* first bilinear de Casteljau step */
335 for (i = 0; i < uorder - 1; i++) {
336 DCN(i, 0) = us * CN(i, 0, k) + u * CN(i + 1, 0, k);
337 for (j = 0; j < vorder - 1; j++) {
338 DCN(i, j + 1) = us * CN(i, j + 1, k) + u * CN(i + 1, j + 1, k);
339 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
340 }
341 }
342
343 /* remaining bilinear de Casteljau steps until the second last step */
344 for (h = 2; h < minorder - 1; h++)
345 for (i = 0; i < uorder - h; i++) {
346 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
347 for (j = 0; j < vorder - h; j++) {
348 DCN(i, j + 1) = us * DCN(i, j + 1) + u * DCN(i + 1, j + 1);
349 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
350 }
351 }
352
353 /* last bilinear de Casteljau step */
354 DCN(2, 0) = DCN(1, 0) - DCN(0, 0);
355 DCN(0, 0) = us * DCN(0, 0) + u * DCN(1, 0);
356 for (j = 0; j < vorder - 1; j++) {
357 /* for the derivative in u */
358 DCN(2, j + 1) = DCN(1, j + 1) - DCN(0, j + 1);
359 DCN(2, j) = vs * DCN(2, j) + v * DCN(2, j + 1);
360
361 /* for the `point' */
362 DCN(0, j + 1) = us * DCN(0, j + 1) + u * DCN(1, j + 1);
363 DCN(0, j) = vs * DCN(0, j) + v * DCN(0, j + 1);
364 }
365
366 /* remaining linear de Casteljau steps until the second last step */
367 for (h = minorder; h < vorder - 1; h++)
368 for (j = 0; j < vorder - h; j++) {
369 /* for the derivative in u */
370 DCN(2, j) = vs * DCN(2, j) + v * DCN(2, j + 1);
371
372 /* for the `point' */
373 DCN(0, j) = vs * DCN(0, j) + v * DCN(0, j + 1);
374 }
375
376 /* derivative direction in v */
377 dv[k] = DCN(0, 1) - DCN(0, 0);
378
379 /* derivative direction in u */
380 du[k] = vs * DCN(2, 0) + v * DCN(2, 1);
381
382 /* last linear de Casteljau step */
383 out[k] = vs * DCN(0, 0) + v * DCN(0, 1);
384 }
385 }
386 else { /* minorder == vorder */
387
388 for (k = 0; k < dim; k++) {
389 /* first bilinear de Casteljau step */
390 for (i = 0; i < uorder - 1; i++) {
391 DCN(i, 0) = us * CN(i, 0, k) + u * CN(i + 1, 0, k);
392 for (j = 0; j < vorder - 1; j++) {
393 DCN(i, j + 1) = us * CN(i, j + 1, k) + u * CN(i + 1, j + 1, k);
394 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
395 }
396 }
397
398 /* remaining bilinear de Casteljau steps until the second last step */
399 for (h = 2; h < minorder - 1; h++)
400 for (i = 0; i < uorder - h; i++) {
401 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
402 for (j = 0; j < vorder - h; j++) {
403 DCN(i, j + 1) = us * DCN(i, j + 1) + u * DCN(i + 1, j + 1);
404 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
405 }
406 }
407
408 /* last bilinear de Casteljau step */
409 DCN(0, 2) = DCN(0, 1) - DCN(0, 0);
410 DCN(0, 0) = vs * DCN(0, 0) + v * DCN(0, 1);
411 for (i = 0; i < uorder - 1; i++) {
412 /* for the derivative in v */
413 DCN(i + 1, 2) = DCN(i + 1, 1) - DCN(i + 1, 0);
414 DCN(i, 2) = us * DCN(i, 2) + u * DCN(i + 1, 2);
415
416 /* for the `point' */
417 DCN(i + 1, 0) = vs * DCN(i + 1, 0) + v * DCN(i + 1, 1);
418 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
419 }
420
421 /* remaining linear de Casteljau steps until the second last step */
422 for (h = minorder; h < uorder - 1; h++)
423 for (i = 0; i < uorder - h; i++) {
424 /* for the derivative in v */
425 DCN(i, 2) = us * DCN(i, 2) + u * DCN(i + 1, 2);
426
427 /* for the `point' */
428 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
429 }
430
431 /* derivative direction in u */
432 du[k] = DCN(1, 0) - DCN(0, 0);
433
434 /* derivative direction in v */
435 dv[k] = us * DCN(0, 2) + u * DCN(1, 2);
436
437 /* last linear de Casteljau step */
438 out[k] = us * DCN(0, 0) + u * DCN(1, 0);
439 }
440 }
441 #undef DCN
442 #undef CN
443 }
444
445
446 /*
447 * Do one-time initialization for evaluators.
448 */
449 void
450 _math_init_eval(void)
451 {
452 GLuint i;
453
454 /* KW: precompute 1/x for useful x.
455 */
456 for (i = 1; i < MAX_EVAL_ORDER; i++)
457 inv_tab[i] = 1.0F / i;
458 }