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