1 /* $Id: m_eval.c,v 1.6 2001/09/18 23:06:14 kschultz Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
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).
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.
45 static GLfloat inv_tab
[MAX_EVAL_ORDER
];
50 * Horner scheme for Bezier curves
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.
59 * A cubic Bezier curve with control points b0, b1, b2, b3 can be
62 * (([3] [3] ) [3] ) [3]
63 * c(t) = (([0]*s*b0 + [1]*t*b1)*s + [2]*t^2*b2)*s + [3]*t^2*b3
66 * where s=1-t and the binomial coefficients [i]. These can
67 * be computed iteratively using the identity:
70 * [i] = (n-i+1)/i * [i-1] and [0] = 1
75 _math_horner_bezier_curve(const GLfloat
* cp
, GLfloat
* out
, GLfloat t
,
76 GLuint dim
, GLuint order
)
78 GLfloat s
, powert
, bincoeff
;
82 bincoeff
= (GLfloat
) (order
- 1);
85 for (k
= 0; k
< dim
; k
++)
86 out
[k
] = s
* cp
[k
] + bincoeff
* t
* cp
[dim
+ k
];
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
];
93 for (k
= 0; k
< dim
; k
++)
94 out
[k
] = s
* out
[k
] + bincoeff
* powert
* cp
[k
];
97 else { /* order=1 -> constant curve */
99 for (k
= 0; k
< dim
; k
++)
105 * Tensor product Bezier surfaces
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.
113 * To store the curve control polygon additional storage
114 * for max(uorder,vorder) points is needed in the
119 _math_horner_bezier_surf(GLfloat
* cn
, GLfloat
* out
, GLfloat u
, GLfloat v
,
120 GLuint dim
, GLuint uorder
, GLuint vorder
)
122 GLfloat
*cp
= cn
+ uorder
* vorder
* dim
;
123 GLuint i
, uinc
= vorder
* dim
;
125 if (vorder
> uorder
) {
127 GLfloat s
, poweru
, bincoeff
;
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
];
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);
139 for (k
= 0; k
< dim
; k
++)
140 cp
[j
* dim
+ k
] = s
* ucp
[k
] + bincoeff
* u
* ucp
[uinc
+ k
];
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
];
147 for (k
= 0; k
< dim
; k
++)
149 s
* cp
[j
* dim
+ k
] + bincoeff
* poweru
* ucp
[k
];
153 /* Evaluate curve point in v */
154 _math_horner_bezier_curve(cp
, out
, v
, dim
, vorder
);
156 else /* uorder=1 -> cn defines a curve in v */
157 _math_horner_bezier_curve(cn
, out
, v
, dim
, vorder
);
159 else { /* vorder <= uorder */
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 */
170 _math_horner_bezier_curve(cn
, &cp
[i
* dim
], v
, dim
, vorder
);
173 /* Evaluate curve point in u */
174 _math_horner_bezier_curve(cp
, out
, u
, dim
, uorder
);
176 else /* vorder=1 -> cn defines a curve in u */
177 _math_horner_bezier_curve(cn
, out
, u
, dim
, uorder
);
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.
191 * De Casteljau needs additional storage for uorder*vorder
192 * values in the control net cn.
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
)
200 GLfloat
*dcn
= cn
+ uorder
* vorder
* dim
;
201 GLfloat us
= 1.0F
- u
, vs
= 1.0F
- v
;
203 GLuint minorder
= uorder
< vorder
? uorder
: vorder
;
204 GLuint uinc
= vorder
* dim
;
205 GLuint dcuinc
= vorder
;
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. */
213 #define CN(I,J,K) cn[(I)*uinc+(J)*dim+(K)]
214 #define DCN(I, J) dcn[(I)*dcuinc+(J)]
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
));
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
));
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
));
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
);
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);
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);
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);
253 /* for the `point' */
254 DCN(0, j
) = vs
* DCN(0, j
) + v
* DCN(0, j
+ 1);
257 /* derivative direction in v */
258 dv
[k
] = DCN(0, 1) - DCN(0, 0);
260 /* derivative direction in u */
261 du
[k
] = vs
* DCN(1, 0) + v
* DCN(1, 1);
263 /* last linear de Casteljau step */
264 out
[k
] = vs
* DCN(0, 0) + v
* DCN(0, 1);
267 else { /* minorder == vorder */
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);
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);
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);
289 /* for the `point' */
290 DCN(i
, 0) = us
* DCN(i
, 0) + u
* DCN(i
+ 1, 0);
293 /* derivative direction in u */
294 du
[k
] = DCN(1, 0) - DCN(0, 0);
296 /* derivative direction in v */
297 dv
[k
] = us
* DCN(0, 1) + u
* DCN(1, 1);
299 /* last linear de Casteljau step */
300 out
[k
] = us
* DCN(0, 0) + u
* DCN(1, 0);
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);
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);
325 /* derivative direction in u */
326 du
[k
] = vs
* (DCN(1, 0) - DCN(0, 0)) + v
* (DCN(1, 1) - DCN(0, 1));
328 /* derivative direction in v */
329 dv
[k
] = us
* (DCN(0, 1) - DCN(0, 0)) + u
* (DCN(1, 1) - DCN(1, 0));
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));
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);
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);
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);
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);
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);
376 /* for the `point' */
377 DCN(0, j
) = vs
* DCN(0, j
) + v
* DCN(0, j
+ 1);
380 /* derivative direction in v */
381 dv
[k
] = DCN(0, 1) - DCN(0, 0);
383 /* derivative direction in u */
384 du
[k
] = vs
* DCN(2, 0) + v
* DCN(2, 1);
386 /* last linear de Casteljau step */
387 out
[k
] = vs
* DCN(0, 0) + v
* DCN(0, 1);
390 else { /* minorder == vorder */
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);
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);
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);
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);
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);
431 /* for the `point' */
432 DCN(i
, 0) = us
* DCN(i
, 0) + u
* DCN(i
+ 1, 0);
435 /* derivative direction in u */
436 du
[k
] = DCN(1, 0) - DCN(0, 0);
438 /* derivative direction in v */
439 dv
[k
] = us
* DCN(0, 2) + u
* DCN(1, 2);
441 /* last linear de Casteljau step */
442 out
[k
] = us
* DCN(0, 0) + u
* DCN(1, 0);
451 * Do one-time initialization for evaluators.
454 _math_init_eval(void)
458 /* KW: precompute 1/x for useful x.
460 for (i
= 1; i
< MAX_EVAL_ORDER
; i
++)
461 inv_tab
[i
] = 1.0F
/ i
;