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