42 #include "bezierEval.h"
48 #define TOLERANCE 0.0001
55 #define MAX_DIMENSION 4
58 static void normalize(
float vec[3]);
59 static void crossProduct(
float x[3],
float y[3],
float ret[3]);
61 static void bezierCurveEvalfast(
float u0,
float u1,
int order,
float *ctlpoints,
int stride,
int dimension,
float u,
float retpoint[]);
64 static float binomialCoefficients[8][8] = {
75 void bezierCurveEval(
float u0,
float u1,
int order,
float *ctlpoints,
int stride,
int dimension,
float u,
float retpoint[])
77 float uprime = (u-u0)/(u1-u0);
78 float *ctlptr = ctlpoints;
79 float oneMinusX = 1.0f-uprime;
83 for(k=0; k<dimension; k++)
84 retpoint[k] = (*(ctlptr + k));
86 for(i=1; i<order; i++){
89 for(k=0; k<dimension; k++) {
90 retpoint[k] = retpoint[k]*oneMinusX + ctlptr[k]* binomialCoefficients[order-1][i] * XPower;
99 void bezierCurveEvalfast(
float u0,
float u1,
int order,
float *ctlpoints,
int stride,
int dimension,
float u,
float retpoint[])
101 float uprime = (u-u0)/(u1-u0);
102 float buf[MAX_ORDER][MAX_ORDER][MAX_DIMENSION];
103 float* ctlptr = ctlpoints;
105 for(i=0; i<order; i++) {
106 for(j=0; j<dimension; j++)
107 buf[0][i][j] = ctlptr[j];
110 for(r=1; r<order; r++){
111 for(i=0; i<order-r; i++) {
112 for(j=0; j<dimension; j++)
113 buf[r][i][j] = (1-uprime)*buf[r-1][i][j] + uprime*buf[r-1][i+1][j];
117 for(j=0; j<dimension; j++)
118 retpoint[j] = buf[order-1][0][j];
125 void bezierCurveEvalDer(
float u0,
float u1,
int order,
float *ctlpoints,
int stride,
int dimension,
float u,
float retDer[])
129 float *ctlptr = ctlpoints;
131 float buf[MAX_ORDER][MAX_DIMENSION];
133 for(k=0; k<dimension; k++)
136 for(i=0; i<order-1; i++){
137 for(k=0; k<dimension; k++) {
138 buf[i][k] = (ctlptr[stride+k] - ctlptr[k])*(order-1)/width;
143 bezierCurveEval(u0, u1, order-1, (
float*) buf, MAX_DIMENSION, dimension, u, retDer);
146 void bezierCurveEvalDerGen(
int der,
float u0,
float u1,
int order,
float *ctlpoints,
int stride,
int dimension,
float u,
float retDer[])
149 float *ctlptr = ctlpoints;
151 float buf[MAX_ORDER][MAX_ORDER][MAX_DIMENSION];
153 for(i=0; i<order; i++){
154 for(k=0; k<dimension; k++){
155 buf[0][i][k] = ctlptr[k];
161 for(r=1; r<=der; r++){
162 for(i=0; i<order-r; i++){
163 for(k=0; k<dimension; k++){
164 buf[r][i][k] = (buf[r-1][i+1][k] - buf[r-1][i][k])*(order-r)/width;
169 bezierCurveEval(u0, u1, order-der, (
float *) (buf[der]), MAX_DIMENSION, dimension, u, retDer);
176 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[])
179 float newPoints[MAX_ORDER][MAX_DIMENSION];
181 for(i=0; i<uorder; i++){
183 bezierCurveEvalDerGen(vder, v0, v1, vorder, ctlpoints+ustride*i, vstride, dimension, v, newPoints[i]);
187 bezierCurveEvalDerGen(uder, u0, u1, uorder, (
float *) newPoints, MAX_DIMENSION, dimension, u, ret);
192 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[])
194 bezierSurfEvalDerGen(0, 0, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, ret);
202 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[])
206 assert(dimension>=3 && dimension <=4);
207 bezierSurfEvalDerGen(1,0, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, partialU);
208 bezierSurfEvalDerGen(0,1, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, partialV);
211 crossProduct(partialU, partialV, retNormal);
213 normalize(retNormal);
219 float newPartialU[MAX_DIMENSION];
220 float newPartialV[MAX_DIMENSION];
222 bezierSurfEvalDerGen(0,0, u0, u1, uorder, v0, v1, vorder, dimension, ctlpoints, ustride, vstride, u, v, val);
225 newPartialU[i] = partialU[i] * val[3] - val[i] * partialU[3];
226 newPartialV[i] = partialV[i] * val[3] - val[i] * partialV[3];
228 crossProduct(newPartialU, newPartialV, retNormal);
229 normalize(retNormal);
234 static void normalize(
float vec[3])
236 float size = (float)sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
241 fprintf(stderr,
"Warning: in oglBSpline.c normal is 0\n");
246 vec[0] = vec[0]/size;
247 vec[1] = vec[1]/size;
248 vec[2] = vec[2]/size;
253 static void crossProduct(
float x[3],
float y[3],
float ret[3])
255 ret[0] = x[1]*y[2] - y[1]*x[2];
256 ret[1] = x[2]*y[0] - y[2]*x[0];
257 ret[2] = x[0]*y[1] - y[0]*x[1];