3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
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).
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.
40 #include "main/glheader.h"
41 #include "main/config.h"
44 static GLfloat inv_tab[MAX_EVAL_ORDER];
49 * Horner scheme for Bezier curves
51 * Bezier curves can be computed via a Horner scheme.
52 * Horner is numerically less stable than the de Casteljau
53 * algorithm, but it is faster. For curves of degree n
54 * the complexity of Horner is O(n) and de Casteljau is O(n^2).
55 * Since stability is not important for displaying curve
56 * points I decided to use the Horner scheme.
58 * A cubic Bezier curve with control points b0, b1, b2, b3 can be
61 * (([3] [3] ) [3] ) [3]
62 * c(t) = (([0]*s*b0 + [1]*t*b1)*s + [2]*t^2*b2)*s + [3]*t^2*b3
65 * where s=1-t and the binomial coefficients [i]. These can
66 * be computed iteratively using the identity:
69 * [i] = (n-i+1)/i * [i-1] and [0] = 1
74 _math_horner_bezier_curve(const GLfloat * cp, GLfloat * out, GLfloat t,
75 GLuint dim, GLuint order)
77 GLfloat s, powert, bincoeff;
81 bincoeff = (GLfloat) (order - 1);
84 for (k = 0; k < dim; k++)
85 out[k] = s * cp[k] + bincoeff * t * cp[dim + k];
87 for (i = 2, cp += 2 * dim, powert = t * t; i < order;
88 i++, powert *= t, cp += dim) {
89 bincoeff *= (GLfloat) (order - i);
90 bincoeff *= inv_tab[i];
92 for (k = 0; k < dim; k++)
93 out[k] = s * out[k] + bincoeff * powert * cp[k];
96 else { /* order=1 -> constant curve */
98 for (k = 0; k < dim; k++)
104 * Tensor product Bezier surfaces
106 * Again the Horner scheme is used to compute a point on a
107 * TP Bezier surface. First a control polygon for a curve
108 * on the surface in one parameter direction is computed,
109 * then the point on the curve for the other parameter
110 * direction is evaluated.
112 * To store the curve control polygon additional storage
113 * for max(uorder,vorder) points is needed in the
118 _math_horner_bezier_surf(GLfloat * cn, GLfloat * out, GLfloat u, GLfloat v,
119 GLuint dim, GLuint uorder, GLuint vorder)
121 GLfloat *cp = cn + uorder * vorder * dim;
122 GLuint i, uinc = vorder * dim;
124 if (vorder > uorder) {
126 GLfloat s, poweru, bincoeff;
129 /* Compute the control polygon for the surface-curve in u-direction */
130 for (j = 0; j < vorder; j++) {
131 GLfloat *ucp = &cn[j * dim];
133 /* Each control point is the point for parameter u on a */
134 /* curve defined by the control polygons in u-direction */
135 bincoeff = (GLfloat) (uorder - 1);
138 for (k = 0; k < dim; k++)
139 cp[j * dim + k] = s * ucp[k] + bincoeff * u * ucp[uinc + k];
141 for (i = 2, ucp += 2 * uinc, poweru = u * u; i < uorder;
142 i++, poweru *= u, ucp += uinc) {
143 bincoeff *= (GLfloat) (uorder - i);
144 bincoeff *= inv_tab[i];
146 for (k = 0; k < dim; k++)
148 s * cp[j * dim + k] + bincoeff * poweru * ucp[k];
152 /* Evaluate curve point in v */
153 _math_horner_bezier_curve(cp, out, v, dim, vorder);
155 else /* uorder=1 -> cn defines a curve in v */
156 _math_horner_bezier_curve(cn, out, v, dim, vorder);
158 else { /* vorder <= uorder */
163 /* Compute the control polygon for the surface-curve in u-direction */
164 for (i = 0; i < uorder; i++, cn += uinc) {
165 /* For constant i all cn[i][j] (j=0..vorder) are located */
166 /* on consecutive memory locations, so we can use */
167 /* horner_bezier_curve to compute the control points */
169 _math_horner_bezier_curve(cn, &cp[i * dim], v, dim, vorder);
172 /* Evaluate curve point in u */
173 _math_horner_bezier_curve(cp, out, u, dim, uorder);
175 else /* vorder=1 -> cn defines a curve in u */
176 _math_horner_bezier_curve(cn, out, u, dim, uorder);
181 * The direct de Casteljau algorithm is used when a point on the
182 * surface and the tangent directions spanning the tangent plane
183 * should be computed (this is needed to compute normals to the
184 * surface). In this case the de Casteljau algorithm approach is
185 * nicer because a point and the partial derivatives can be computed
186 * at the same time. To get the correct tangent length du and dv
187 * must be multiplied with the (u2-u1)/uorder-1 and (v2-v1)/vorder-1.
188 * Since only the directions are needed, this scaling step is omitted.
190 * De Casteljau needs additional storage for uorder*vorder
191 * values in the control net cn.
195 _math_de_casteljau_surf(GLfloat * cn, GLfloat * out, GLfloat * du,
196 GLfloat * dv, GLfloat u, GLfloat v, GLuint dim,
197 GLuint uorder, GLuint vorder)
199 GLfloat *dcn = cn + uorder * vorder * dim;
200 GLfloat us = 1.0F - u, vs = 1.0F - v;
202 GLuint minorder = uorder < vorder ? uorder : vorder;
203 GLuint uinc = vorder * dim;
204 GLuint dcuinc = vorder;
206 /* Each component is evaluated separately to save buffer space */
207 /* This does not drasticaly decrease the performance of the */
208 /* algorithm. If additional storage for (uorder-1)*(vorder-1) */
209 /* points would be available, the components could be accessed */
210 /* in the innermost loop which could lead to less cache misses. */
212 #define CN(I,J,K) cn[(I)*uinc+(J)*dim+(K)]
213 #define DCN(I, J) dcn[(I)*dcuinc+(J)]
215 if (uorder == vorder) {
216 for (k = 0; k < dim; k++) {
217 /* Derivative direction in u */
218 du[k] = vs * (CN(1, 0, k) - CN(0, 0, k)) +
219 v * (CN(1, 1, k) - CN(0, 1, k));
221 /* Derivative direction in v */
222 dv[k] = us * (CN(0, 1, k) - CN(0, 0, k)) +
223 u * (CN(1, 1, k) - CN(1, 0, k));
225 /* bilinear de Casteljau step */
226 out[k] = us * (vs * CN(0, 0, k) + v * CN(0, 1, k)) +
227 u * (vs * CN(1, 0, k) + v * CN(1, 1, k));
230 else if (minorder == uorder) {
231 for (k = 0; k < dim; k++) {
232 /* bilinear de Casteljau step */
233 DCN(1, 0) = CN(1, 0, k) - CN(0, 0, k);
234 DCN(0, 0) = us * CN(0, 0, k) + u * CN(1, 0, k);
236 for (j = 0; j < vorder - 1; j++) {
237 /* for the derivative in u */
238 DCN(1, j + 1) = CN(1, j + 1, k) - CN(0, j + 1, k);
239 DCN(1, j) = vs * DCN(1, j) + v * DCN(1, j + 1);
241 /* for the `point' */
242 DCN(0, j + 1) = us * CN(0, j + 1, k) + u * CN(1, j + 1, k);
243 DCN(0, j) = vs * DCN(0, j) + v * DCN(0, j + 1);
246 /* remaining linear de Casteljau steps until the second last step */
247 for (h = minorder; h < vorder - 1; h++)
248 for (j = 0; j < vorder - h; j++) {
249 /* for the derivative in u */
250 DCN(1, j) = vs * DCN(1, j) + v * DCN(1, j + 1);
252 /* for the `point' */
253 DCN(0, j) = vs * DCN(0, j) + v * DCN(0, j + 1);
256 /* derivative direction in v */
257 dv[k] = DCN(0, 1) - DCN(0, 0);
259 /* derivative direction in u */
260 du[k] = vs * DCN(1, 0) + v * DCN(1, 1);
262 /* last linear de Casteljau step */
263 out[k] = vs * DCN(0, 0) + v * DCN(0, 1);
266 else { /* minorder == vorder */
268 for (k = 0; k < dim; k++) {
269 /* bilinear de Casteljau step */
270 DCN(0, 1) = CN(0, 1, k) - CN(0, 0, k);
271 DCN(0, 0) = vs * CN(0, 0, k) + v * CN(0, 1, k);
272 for (i = 0; i < uorder - 1; i++) {
273 /* for the derivative in v */
274 DCN(i + 1, 1) = CN(i + 1, 1, k) - CN(i + 1, 0, k);
275 DCN(i, 1) = us * DCN(i, 1) + u * DCN(i + 1, 1);
277 /* for the `point' */
278 DCN(i + 1, 0) = vs * CN(i + 1, 0, k) + v * CN(i + 1, 1, k);
279 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
282 /* remaining linear de Casteljau steps until the second last step */
283 for (h = minorder; h < uorder - 1; h++)
284 for (i = 0; i < uorder - h; i++) {
285 /* for the derivative in v */
286 DCN(i, 1) = us * DCN(i, 1) + u * DCN(i + 1, 1);
288 /* for the `point' */
289 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
292 /* derivative direction in u */
293 du[k] = DCN(1, 0) - DCN(0, 0);
295 /* derivative direction in v */
296 dv[k] = us * DCN(0, 1) + u * DCN(1, 1);
298 /* last linear de Casteljau step */
299 out[k] = us * DCN(0, 0) + u * DCN(1, 0);
303 else if (uorder == vorder) {
304 for (k = 0; k < dim; k++) {
305 /* first bilinear de Casteljau step */
306 for (i = 0; i < uorder - 1; i++) {
307 DCN(i, 0) = us * CN(i, 0, k) + u * CN(i + 1, 0, k);
308 for (j = 0; j < vorder - 1; j++) {
309 DCN(i, j + 1) = us * CN(i, j + 1, k) + u * CN(i + 1, j + 1, k);
310 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
314 /* remaining bilinear de Casteljau steps until the second last step */
315 for (h = 2; h < minorder - 1; h++)
316 for (i = 0; i < uorder - h; i++) {
317 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
318 for (j = 0; j < vorder - h; j++) {
319 DCN(i, j + 1) = us * DCN(i, j + 1) + u * DCN(i + 1, j + 1);
320 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
324 /* derivative direction in u */
325 du[k] = vs * (DCN(1, 0) - DCN(0, 0)) + v * (DCN(1, 1) - DCN(0, 1));
327 /* derivative direction in v */
328 dv[k] = us * (DCN(0, 1) - DCN(0, 0)) + u * (DCN(1, 1) - DCN(1, 0));
330 /* last bilinear de Casteljau step */
331 out[k] = us * (vs * DCN(0, 0) + v * DCN(0, 1)) +
332 u * (vs * DCN(1, 0) + v * DCN(1, 1));
335 else if (minorder == uorder) {
336 for (k = 0; k < dim; k++) {
337 /* first bilinear de Casteljau step */
338 for (i = 0; i < uorder - 1; i++) {
339 DCN(i, 0) = us * CN(i, 0, k) + u * CN(i + 1, 0, k);
340 for (j = 0; j < vorder - 1; j++) {
341 DCN(i, j + 1) = us * CN(i, j + 1, k) + u * CN(i + 1, j + 1, k);
342 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
346 /* remaining bilinear de Casteljau steps until the second last step */
347 for (h = 2; h < minorder - 1; h++)
348 for (i = 0; i < uorder - h; i++) {
349 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
350 for (j = 0; j < vorder - h; j++) {
351 DCN(i, j + 1) = us * DCN(i, j + 1) + u * DCN(i + 1, j + 1);
352 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
356 /* last bilinear de Casteljau step */
357 DCN(2, 0) = DCN(1, 0) - DCN(0, 0);
358 DCN(0, 0) = us * DCN(0, 0) + u * DCN(1, 0);
359 for (j = 0; j < vorder - 1; j++) {
360 /* for the derivative in u */
361 DCN(2, j + 1) = DCN(1, j + 1) - DCN(0, j + 1);
362 DCN(2, j) = vs * DCN(2, j) + v * DCN(2, j + 1);
364 /* for the `point' */
365 DCN(0, j + 1) = us * DCN(0, j + 1) + u * DCN(1, j + 1);
366 DCN(0, j) = vs * DCN(0, j) + v * DCN(0, j + 1);
369 /* remaining linear de Casteljau steps until the second last step */
370 for (h = minorder; h < vorder - 1; h++)
371 for (j = 0; j < vorder - h; j++) {
372 /* for the derivative in u */
373 DCN(2, j) = vs * DCN(2, j) + v * DCN(2, j + 1);
375 /* for the `point' */
376 DCN(0, j) = vs * DCN(0, j) + v * DCN(0, j + 1);
379 /* derivative direction in v */
380 dv[k] = DCN(0, 1) - DCN(0, 0);
382 /* derivative direction in u */
383 du[k] = vs * DCN(2, 0) + v * DCN(2, 1);
385 /* last linear de Casteljau step */
386 out[k] = vs * DCN(0, 0) + v * DCN(0, 1);
389 else { /* minorder == vorder */
391 for (k = 0; k < dim; k++) {
392 /* first bilinear de Casteljau step */
393 for (i = 0; i < uorder - 1; i++) {
394 DCN(i, 0) = us * CN(i, 0, k) + u * CN(i + 1, 0, k);
395 for (j = 0; j < vorder - 1; j++) {
396 DCN(i, j + 1) = us * CN(i, j + 1, k) + u * CN(i + 1, j + 1, k);
397 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
401 /* remaining bilinear de Casteljau steps until the second last step */
402 for (h = 2; h < minorder - 1; h++)
403 for (i = 0; i < uorder - h; i++) {
404 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
405 for (j = 0; j < vorder - h; j++) {
406 DCN(i, j + 1) = us * DCN(i, j + 1) + u * DCN(i + 1, j + 1);
407 DCN(i, j) = vs * DCN(i, j) + v * DCN(i, j + 1);
411 /* last bilinear de Casteljau step */
412 DCN(0, 2) = DCN(0, 1) - DCN(0, 0);
413 DCN(0, 0) = vs * DCN(0, 0) + v * DCN(0, 1);
414 for (i = 0; i < uorder - 1; i++) {
415 /* for the derivative in v */
416 DCN(i + 1, 2) = DCN(i + 1, 1) - DCN(i + 1, 0);
417 DCN(i, 2) = us * DCN(i, 2) + u * DCN(i + 1, 2);
419 /* for the `point' */
420 DCN(i + 1, 0) = vs * DCN(i + 1, 0) + v * DCN(i + 1, 1);
421 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
424 /* remaining linear de Casteljau steps until the second last step */
425 for (h = minorder; h < uorder - 1; h++)
426 for (i = 0; i < uorder - h; i++) {
427 /* for the derivative in v */
428 DCN(i, 2) = us * DCN(i, 2) + u * DCN(i + 1, 2);
430 /* for the `point' */
431 DCN(i, 0) = us * DCN(i, 0) + u * DCN(i + 1, 0);
434 /* derivative direction in u */
435 du[k] = DCN(1, 0) - DCN(0, 0);
437 /* derivative direction in v */
438 dv[k] = us * DCN(0, 2) + u * DCN(1, 2);
440 /* last linear de Casteljau step */
441 out[k] = us * DCN(0, 0) + u * DCN(1, 0);
450 * Do one-time initialization for evaluators.
453 _math_init_eval(void)
457 /* KW: precompute 1/x for useful x.
459 for (i = 1; i < MAX_EVAL_ORDER; i++)
460 inv_tab[i] = 1.0F / i;