074c4fcb67d1b483421787274c2768030e103e87
[reactos.git] / reactos / dll / glu32 / 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 */
35 /*
36 */
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <assert.h>
41 #include <math.h>
42 #include "bezierEval.h"
43
44 #ifdef __WATCOMC__
45 #pragma warning 14 10
46 #endif
47
48 #define TOLERANCE 0.0001
49
50 #ifndef MAX_ORDER
51 #define MAX_ORDER 16
52 #endif
53
54 #ifndef MAX_DIMENSION
55 #define MAX_DIMENSION 4
56 #endif
57
58 static void normalize(float vec[3]);
59 static void crossProduct(float x[3], float y[3], float ret[3]);
60
61 static float binomialCoefficients[8][8] = {
62 {1,0,0,0,0,0,0,0},
63 {1,1,0,0,0,0,0,0},
64 {1,2,1,0,0,0,0,0},
65 {1,3,3,1,0,0,0,0},
66 {1,4,6,4,1,0,0,0},
67 {1,5,10,10,5,1,0,0},
68 {1,6,15,20,15,6,1,0},
69 {1,7,21,35,35,21,7,1}
70 };
71
72 void bezierCurveEval(float u0, float u1, int order, float *ctlpoints, int stride, int dimension, float u, float retpoint[])
73 {
74 float uprime = (u-u0)/(u1-u0);
75 float *ctlptr = ctlpoints;
76 float oneMinusX = 1.0f-uprime;
77 float XPower = 1.0f;
78
79 int i,k;
80 for(k=0; k<dimension; k++)
81 retpoint[k] = (*(ctlptr + k));
82
83 for(i=1; i<order; i++){
84 ctlptr += stride;
85 XPower *= uprime;
86 for(k=0; k<dimension; k++) {
87 retpoint[k] = retpoint[k]*oneMinusX + ctlptr[k]* binomialCoefficients[order-1][i] * XPower;
88 }
89 }
90 }
91
92
93 /*order = degree +1 >=1.
94 */
95 void bezierCurveEvalDer(float u0, float u1, int order, float *ctlpoints, int stride, int dimension, float u, float retDer[])
96 {
97 int i,k;
98 float width = u1-u0;
99 float *ctlptr = ctlpoints;
100
101 float buf[MAX_ORDER][MAX_DIMENSION];
102 if(order == 1){
103 for(k=0; k<dimension; k++)
104 retDer[k]=0;
105 }
106 for(i=0; i<order-1; i++){
107 for(k=0; k<dimension; k++) {
108 buf[i][k] = (ctlptr[stride+k] - ctlptr[k])*(order-1)/width;
109 }
110 ctlptr += stride;
111 }
112
113 bezierCurveEval(u0, u1, order-1, (float*) buf, MAX_DIMENSION, dimension, u, retDer);
114 }
115
116 void bezierCurveEvalDerGen(int der, float u0, float u1, int order, float *ctlpoints, int stride, int dimension, float u, float retDer[])
117 {
118 int i,k,r;
119 float *ctlptr = ctlpoints;
120 float width=u1-u0;
121 float buf[MAX_ORDER][MAX_ORDER][MAX_DIMENSION];
122 if(der<0) der=0;
123 for(i=0; i<order; i++){
124 for(k=0; k<dimension; k++){
125 buf[0][i][k] = ctlptr[k];
126 }
127 ctlptr += stride;
128 }
129
130
131 for(r=1; r<=der; r++){
132 for(i=0; i<order-r; i++){
133 for(k=0; k<dimension; k++){
134 buf[r][i][k] = (buf[r-1][i+1][k] - buf[r-1][i][k])*(order-r)/width;
135 }
136 }
137 }
138
139 bezierCurveEval(u0, u1, order-der, (float *) (buf[der]), MAX_DIMENSION, dimension, u, retDer);
140 }
141
142 /*the Bezier bivarite polynomial is:
143 * sum[i:0,uorder-1][j:0,vorder-1] { ctlpoints[i*ustride+j*vstride] * B(i)*B(j)
144 * where B(i) and B(j) are basis functions
145 */
146 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[])
147 {
148 int i;
149 float newPoints[MAX_ORDER][MAX_DIMENSION];
150
151 for(i=0; i<uorder; i++){
152
153 bezierCurveEvalDerGen(vder, v0, v1, vorder, ctlpoints+ustride*i, vstride, dimension, v, newPoints[i]);
154
155 }
156
157 bezierCurveEvalDerGen(uder, u0, u1, uorder, (float *) newPoints, MAX_DIMENSION, dimension, u, ret);
158 }
159
160
161 /*division by w is performed*/
162 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[])
163 {
164 bezierSurfEvalDerGen(0, 0, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, ret);
165 if(dimension == 4) /*homogeneous*/{
166 ret[0] /= ret[3];
167 ret[1] /= ret[3];
168 ret[2] /= ret[3];
169 }
170 }
171
172 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[])
173 {
174 float partialU[4];
175 float partialV[4];
176 assert(dimension>=3 && dimension <=4);
177 bezierSurfEvalDerGen(1,0, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, partialU);
178 bezierSurfEvalDerGen(0,1, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, partialV);
179
180 if(dimension == 3){/*inhomogeneous*/
181 crossProduct(partialU, partialV, retNormal);
182
183 normalize(retNormal);
184
185 return;
186 }
187 else { /*homogeneous*/
188 float val[4]; /*the point coordinates (without derivative)*/
189 float newPartialU[MAX_DIMENSION];
190 float newPartialV[MAX_DIMENSION];
191 int i;
192 bezierSurfEvalDerGen(0,0, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, val);
193
194 for(i=0; i<=2; i++){
195 newPartialU[i] = partialU[i] * val[3] - val[i] * partialU[3];
196 newPartialV[i] = partialV[i] * val[3] - val[i] * partialV[3];
197 }
198 crossProduct(newPartialU, newPartialV, retNormal);
199 normalize(retNormal);
200 }
201 }
202
203 /*if size is 0, then nothing is done*/
204 static void normalize(float vec[3])
205 {
206 float size = (float)sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
207
208 if(size < TOLERANCE)
209 {
210 #ifdef DEBUG
211 fprintf(stderr, "Warning: in oglBSpline.c normal is 0\n");
212 #endif
213 return;
214 }
215 else {
216 vec[0] = vec[0]/size;
217 vec[1] = vec[1]/size;
218 vec[2] = vec[2]/size;
219 }
220 }
221
222
223 static void crossProduct(float x[3], float y[3], float ret[3])
224 {
225 ret[0] = x[1]*y[2] - y[1]*x[2];
226 ret[1] = x[2]*y[0] - y[2]*x[0];
227 ret[2] = x[0]*y[1] - y[0]*x[1];
228
229 }
230