Fix compilation errors and warnings for NURBS support. (Robert Bergkvist)
[mesa.git] / src / glu / sgi / libnurbs / interface / bezierEval.cc
1 /*
2 ** License Applicability. Except to the extent portions of this file are
3 ** made subject to an alternative license as permitted in the SGI Free
4 ** Software License B, Version 1.1 (the "License"), the contents of this
5 ** file are subject only to the provisions of the License. You may not use
6 ** this file except in compliance with the License. You may obtain a copy
7 ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8 ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9 **
10 ** http://oss.sgi.com/projects/FreeB
11 **
12 ** Note that, as provided in the License, the Software is distributed on an
13 ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14 ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15 ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16 ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17 **
18 ** Original Code. The Original Code is: OpenGL Sample Implementation,
19 ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20 ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21 ** Copyright in any portions created by third parties is as indicated
22 ** elsewhere herein. All Rights Reserved.
23 **
24 ** Additional Notice Provisions: The application programming interfaces
25 ** established by SGI in conjunction with the Original Code are The
26 ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27 ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28 ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29 ** Window System(R) (Version 1.3), released October 19, 1998. This software
30 ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31 ** published by SGI, but has not been independently verified as being
32 ** compliant with the OpenGL(R) version 1.2.1 Specification.
33 **
34 ** $Date: 2001/11/29 16:16:55 $ $Revision: 1.2 $
35 */
36 /*
37 ** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libnurbs/interface/bezierEval.cc,v 1.2 2001/11/29 16:16:55 kschultz Exp $
38 */
39
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <assert.h>
43 #include <math.h>
44 #include "bezierEval.h"
45
46 #define TOLERANCE 0.0001
47
48 #ifndef MAX_ORDER
49 #define MAX_ORDER 16
50 #endif
51
52 #ifndef MAX_DIMENSION
53 #define MAX_DIMENSION 4
54 #endif
55
56 static void normalize(float vec[3]);
57 static void crossProduct(float x[3], float y[3], float ret[3]);
58 static void bezierCurveEvalfast(float u0, float u1, int order, float *ctlpoints, int stride, int dimension, float u, float retpoint[]);
59
60 static float binomialCoefficients[8][8] = {
61 {1,0,0,0,0,0,0,0},
62 {1,1,0,0,0,0,0,0},
63 {1,2,1,0,0,0,0,0},
64 {1,3,3,1,0,0,0,0},
65 {1,4,6,4,1,0,0,0},
66 {1,5,10,10,5,1,0,0},
67 {1,6,15,20,15,6,1,0},
68 {1,7,21,35,35,21,7,1}
69 };
70
71 void bezierCurveEval(float u0, float u1, int order, float *ctlpoints, int stride, int dimension, float u, float retpoint[])
72 {
73 float uprime = (u-u0)/(u1-u0);
74 float *ctlptr = ctlpoints;
75 float oneMinusX = 1.0f-uprime;
76 float XPower = 1.0f;
77
78 int i,k;
79 for(k=0; k<dimension; k++)
80 retpoint[k] = (*(ctlptr + k));
81
82 for(i=1; i<order; i++){
83 ctlptr += stride;
84 XPower *= uprime;
85 for(k=0; k<dimension; k++) {
86 retpoint[k] = retpoint[k]*oneMinusX + ctlptr[k]* binomialCoefficients[order-1][i] * XPower;
87 }
88 }
89 }
90
91
92
93 /*order = degree +1 >=1.
94 */
95 void bezierCurveEvalfast(float u0, float u1, int order, float *ctlpoints, int stride, int dimension, float u, float retpoint[])
96 {
97 float uprime = (u-u0)/(u1-u0);
98 float buf[MAX_ORDER][MAX_ORDER][MAX_DIMENSION];
99 float* ctlptr = ctlpoints;
100 int r, i,j;
101 for(i=0; i<order; i++) {
102 for(j=0; j<dimension; j++)
103 buf[0][i][j] = ctlptr[j];
104 ctlptr += stride;
105 }
106 for(r=1; r<order; r++){
107 for(i=0; i<order-r; i++) {
108 for(j=0; j<dimension; j++)
109 buf[r][i][j] = (1-uprime)*buf[r-1][i][j] + uprime*buf[r-1][i+1][j];
110 }
111 }
112
113 for(j=0; j<dimension; j++)
114 retpoint[j] = buf[order-1][0][j];
115 }
116
117
118
119 /*order = degree +1 >=1.
120 */
121 void bezierCurveEvalDer(float u0, float u1, int order, float *ctlpoints, int stride, int dimension, float u, float retDer[])
122 {
123 int i,k;
124 float width = u1-u0;
125 float *ctlptr = ctlpoints;
126
127 float buf[MAX_ORDER][MAX_DIMENSION];
128 if(order == 1){
129 for(k=0; k<dimension; k++)
130 retDer[k]=0;
131 }
132 for(i=0; i<order-1; i++){
133 for(k=0; k<dimension; k++) {
134 buf[i][k] = (ctlptr[stride+k] - ctlptr[k])*(order-1)/width;
135 }
136 ctlptr += stride;
137 }
138
139 bezierCurveEval(u0, u1, order-1, (float*) buf, MAX_DIMENSION, dimension, u, retDer);
140 }
141
142 void bezierCurveEvalDerGen(int der, float u0, float u1, int order, float *ctlpoints, int stride, int dimension, float u, float retDer[])
143 {
144 int i,k,r;
145 float *ctlptr = ctlpoints;
146 float width=u1-u0;
147 float buf[MAX_ORDER][MAX_ORDER][MAX_DIMENSION];
148 if(der<0) der=0;
149 for(i=0; i<order; i++){
150 for(k=0; k<dimension; k++){
151 buf[0][i][k] = ctlptr[k];
152 }
153 ctlptr += stride;
154 }
155
156
157 for(r=1; r<=der; r++){
158 for(i=0; i<order-r; i++){
159 for(k=0; k<dimension; k++){
160 buf[r][i][k] = (buf[r-1][i+1][k] - buf[r-1][i][k])*(order-r)/width;
161 }
162 }
163 }
164
165 bezierCurveEval(u0, u1, order-der, (float *) (buf[der]), MAX_DIMENSION, dimension, u, retDer);
166 }
167
168 /*the Bezier bivarite polynomial is:
169 * sum[i:0,uorder-1][j:0,vorder-1] { ctlpoints[i*ustride+j*vstride] * B(i)*B(j)
170 * where B(i) and B(j) are basis functions
171 */
172 void bezierSurfEvalDerGen(int uder, int vder, float u0, float u1, int uorder, float v0, float v1, int vorder, int dimension, float *ctlpoints, int ustride, int vstride, float u, float v, float ret[])
173 {
174 int i;
175 float newPoints[MAX_ORDER][MAX_DIMENSION];
176
177 for(i=0; i<uorder; i++){
178
179 bezierCurveEvalDerGen(vder, v0, v1, vorder, ctlpoints+ustride*i, vstride, dimension, v, newPoints[i]);
180
181 }
182
183 bezierCurveEvalDerGen(uder, u0, u1, uorder, (float *) newPoints, MAX_DIMENSION, dimension, u, ret);
184 }
185
186
187 /*division by w is performed*/
188 void bezierSurfEval(float u0, float u1, int uorder, float v0, float v1, int vorder, int dimension, float *ctlpoints, int ustride, int vstride, float u, float v, float ret[])
189 {
190 bezierSurfEvalDerGen(0, 0, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, ret);
191 if(dimension == 4) /*homogeneous*/{
192 ret[0] /= ret[3];
193 ret[1] /= ret[3];
194 ret[2] /= ret[3];
195 }
196 }
197
198 void bezierSurfEvalNormal(float u0, float u1, int uorder, float v0, float v1, int vorder, int dimension, float *ctlpoints, int ustride, int vstride, float u, float v, float retNormal[])
199 {
200 float partialU[4];
201 float partialV[4];
202 assert(dimension>=3 && dimension <=4);
203 bezierSurfEvalDerGen(1,0, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, partialU);
204 bezierSurfEvalDerGen(0,1, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, partialV);
205
206 if(dimension == 3){/*inhomogeneous*/
207 crossProduct(partialU, partialV, retNormal);
208
209 normalize(retNormal);
210
211 return;
212 }
213 else { /*homogeneous*/
214 float val[4]; /*the point coordinates (without derivative)*/
215 float newPartialU[MAX_DIMENSION];
216 float newPartialV[MAX_DIMENSION];
217 int i;
218 bezierSurfEvalDerGen(0,0, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, val);
219
220 for(i=0; i<=2; i++){
221 newPartialU[i] = partialU[i] * val[3] - val[i] * partialU[3];
222 newPartialV[i] = partialV[i] * val[3] - val[i] * partialV[3];
223 }
224 crossProduct(newPartialU, newPartialV, retNormal);
225 normalize(retNormal);
226 }
227 }
228
229 /*if size is 0, then nothing is done*/
230 static void normalize(float vec[3])
231 {
232 float size = (float)sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
233
234 if(size < TOLERANCE)
235 {
236 #ifdef DEBUG
237 fprintf(stderr, "Warning: in oglBSpline.c normal is 0\n");
238 #endif
239 return;
240 }
241 else {
242 vec[0] = vec[0]/size;
243 vec[1] = vec[1]/size;
244 vec[2] = vec[2]/size;
245 }
246 }
247
248
249 static void crossProduct(float x[3], float y[3], float ret[3])
250 {
251 ret[0] = x[1]*y[2] - y[1]*x[2];
252 ret[1] = x[2]*y[0] - y[2]*x[0];
253 ret[2] = x[0]*y[1] - y[0]*x[1];
254
255 }
256