@@ -47,7 +47,7 @@ BernsteinEval._factorial=function(n) {
4747 return result ;
4848}
4949/** @private */
50- BernsteinEval . prototype . getFactor = function ( t , i ) {
50+ BernsteinEval . prototype . getFactor = function ( u , i ) {
5151 var bino ;
5252 if ( i == 0 ) {
5353 return Math . pow ( 1 - u , this . n - i ) ;
@@ -88,26 +88,29 @@ BernsteinEvalSpline.prototype.evaluate=function(t, output){
8888}
8989/** @private */
9090function BernsteinEvalSurface ( controlPoints ) {
91- if ( ! this . controlPoints || this . controlPoints . length == 0 )
91+ if ( ! controlPoints || controlPoints . length == 0 )
9292 throw new Error ( "no control points" )
9393 this . uorder = controlPoints . length ;
9494 this . torder = controlPoints [ 0 ] . length ;
9595 this . k = controlPoints [ 0 ] [ 0 ] . length ;
96+ if ( typeof this . k === "undefined" )
97+ throw new Error ( "probably not a 2D control point array" )
9698 this . points = controlPoints ;
97- this . evaluator = new BernsteinEval ( this . order ) ;
99+ this . evaluatorT = new BernsteinEval ( this . torder ) ;
100+ this . evaluatorU = new BernsteinEval ( this . uorder ) ;
98101 this . bufferT = [ ] ;
99102 this . bufferU = [ ] ;
100103}
101104/** @private */
102105BernsteinEvalSurface . prototype . evaluate = function ( t , u , output ) {
103- var bt = bufferT ;
104- var bu = bufferU ;
106+ var bt = this . bufferT ;
107+ var bu = this . bufferU ;
105108 if ( t == u ) {
106- bu = bufferT ;
107- this . evaluator . getFactors ( t , bufferT ) ;
109+ bu = this . bufferT ;
110+ this . evaluatorT . getFactors ( t , this . bufferT ) ;
108111 } else {
109- this . evaluator . getFactors ( t , bufferT ) ;
110- this . evaluator . getFactors ( u , bufferU ) ;
112+ this . evaluatorT . getFactors ( t , this . bufferT ) ;
113+ this . evaluatorU . getFactors ( u , this . bufferU ) ;
111114 }
112115 for ( var i = 0 ; i < this . k ; i ++ ) {
113116 var value = 0 ;
@@ -141,7 +144,7 @@ BernsteinEvalSurface.prototype.evaluate=function(t, u, output){
141144 * @param {number } [u2] Ending point for the purpose of interpolation; it will correspond to 1.
142145 * May be omitted; default is 1.
143146 */
144- function BezierCurve ( cp , u1 , u2 ) {
147+ var BezierCurve = function ( cp , u1 , u2 ) {
145148 if ( typeof u1 == "undefined" && typeof u2 == "undefined" ) {
146149 this . uoffset = 0 ;
147150 this . umul = 1 ;
@@ -209,7 +212,7 @@ BezierCurve.prototype.evaluate=function(u){
209212 * V-axis; it will correspond to 1.
210213 * May be omitted; default is 1.
211214 */
212- function BezierSurface ( cp , u1 , u2 , v1 , v2 ) {
215+ var BezierSurface = function ( cp , u1 , u2 , v1 , v2 ) {
213216 if ( typeof u1 == "undefined" && typeof u2 == "undefined" &&
214217 typeof v1 == "undefined" && typeof v2 == "undefined" ) {
215218 this . uoffset = 0 ;
@@ -226,7 +229,7 @@ function BezierSurface(cp, u1, u2, v1, v2){
226229 this . voffset = v1 ;
227230 this . vmul = 1.0 / ( v2 - v1 ) ;
228231 }
229- this . evaluator = new BernsteinEvalSpline ( cp ) ;
232+ this . evaluator = new BernsteinEvalSurface ( cp ) ;
230233 this . k = this . evaluator . k ;
231234}
232235/**
@@ -394,15 +397,18 @@ CurveEval.prototype.evalOne=function(mesh,u){
394397 if ( this . vertexCurve ) {
395398 var oldColor = ( color ) ? mesh . color . slice ( 0 , 3 ) : null ;
396399 var oldNormal = ( normal ) ? mesh . normal . slice ( 0 , 3 ) : null ;
397- var oldTexCoord = ( texcoord ) ? mesh . texCoord . slice ( 0 , 3 ) : null ;
400+ var oldTexCoord = ( texcoord ) ? mesh . texCoord . slice ( 0 , 2 ) : null ;
398401 if ( color ) mesh . color3 ( color [ 0 ] , color [ 1 ] , color [ 2 ] ) ;
399402 if ( normal ) mesh . normal3 ( normal [ 0 ] , normal [ 1 ] , normal [ 2 ] ) ;
400- if ( texcoord ) mesh . texCoord3 ( texcoord [ 0 ] , texcoord [ 1 ] , texcoord [ 2 ] ) ;
403+ if ( texcoord ) mesh . texCoord2 ( texcoord [ 0 ] , texcoord [ 1 ] ) ;
401404 var vertex = this . vertexCurve . evaluate ( u ) ;
402- mesh . vertex3 ( vertex [ 0 ] , vertex [ 1 ] , vertex [ 2 ] ) ;
405+ if ( vertex . length == 2 )
406+ mesh . vertex3 ( vertex [ 0 ] , vertex [ 1 ] , 0.0 ) ;
407+ else
408+ mesh . vertex3 ( vertex [ 0 ] , vertex [ 1 ] , vertex [ 2 ] ) ;
403409 if ( oldColor ) mesh . color3 ( oldColor [ 0 ] , oldColor [ 1 ] , oldColor [ 2 ] ) ;
404410 if ( oldNormal ) mesh . normal3 ( oldNormal [ 0 ] , oldNormal [ 1 ] , oldNormal [ 2 ] ) ;
405- if ( oldTexCoord ) mesh . texCoord3 ( oldTexCoord [ 0 ] , oldTexCoord [ 1 ] , oldTexCoord [ 2 ] ) ;
411+ if ( oldTexCoord ) mesh . texCoord2 ( oldTexCoord [ 0 ] , oldTexCoord [ 1 ] ) ;
406412 }
407413 return this ;
408414}
@@ -517,6 +523,11 @@ SurfaceEval.prototype.color=function(evaluator){
517523* named "evaluate", giving 2 values as a result. See {@link SurfaceEvals#vertex}.
518524* </ul>
519525* @return {SurfaceEval } This object.
526+ * @example <caption>The following example sets the surface
527+ * function to a linear evaluator. Thus, coordinates passed to the
528+ * evalOne and evalSurface methods will be interpolated as direct
529+ * texture coordinates.</caption>
530+ * surface.texCoord({"evaluate":function(u,v){ return [u,v] }});
520531*/
521532SurfaceEval . prototype . texCoord = function ( evaluator ) {
522533 this . texCoordSurface = evaluator ;
@@ -628,8 +639,18 @@ SurfaceEval.prototype.evalOne=function(mesh,u,v){
628639 var dv = 0.001
629640 // Find the partial derivatives of u and v
630641 var vu = this . vertexSurface . evaluate ( u + du , v ) ;
642+ if ( vu [ 0 ] == 0 && vu [ 1 ] == 0 && vu [ 2 ] == 0 ) {
643+ // too abrupt, try the other direction
644+ du = - du ;
645+ vu = this . vertexSurface . evaluate ( u + du , v ) ;
646+ }
631647 var vuz = vu [ 2 ] ;
632648 var vv = this . vertexSurface . evaluate ( u , v + dv ) ;
649+ if ( vv [ 0 ] == 0 && vv [ 1 ] == 0 && vv [ 2 ] == 0 ) {
650+ // too abrupt, try the other direction
651+ dv = - dv ;
652+ vv = this . vertexSurface . evaluate ( u , v + dv ) ;
653+ }
633654 GLMath . vec3subInPlace ( vv , vertex ) ;
634655 GLMath . vec3subInPlace ( vu , vertex ) ;
635656 // Divide by the deltas of u and v
@@ -657,11 +678,11 @@ SurfaceEval.prototype.evalOne=function(mesh,u,v){
657678 } else if ( normal ) {
658679 mesh . normal3 ( normal [ 0 ] , normal [ 1 ] , normal [ 2 ] ) ;
659680 }
660- if ( texcoord ) mesh . texCoord3 ( texcoord [ 0 ] , texcoord [ 1 ] , texcoord [ 2 ] ) ;
681+ if ( texcoord ) mesh . texCoord2 ( texcoord [ 0 ] , texcoord [ 1 ] ) ;
661682 mesh . vertex3 ( vertex [ 0 ] , vertex [ 1 ] , vertex [ 2 ] ) ;
662683 if ( oldColor ) mesh . color3 ( oldColor [ 0 ] , oldColor [ 1 ] , oldColor [ 2 ] ) ;
663684 if ( oldNormal ) mesh . normal3 ( oldNormal [ 0 ] , oldNormal [ 1 ] , oldNormal [ 2 ] ) ;
664- if ( oldTexCoord ) mesh . texCoord3 ( oldTexCoord [ 0 ] , oldTexCoord [ 1 ] , oldTexCoord [ 2 ] ) ;
685+ if ( oldTexCoord ) mesh . texCoord2 ( oldTexCoord [ 0 ] , oldTexCoord [ 1 ] ) ;
665686 }
666687 return this ;
667688}
@@ -745,4 +766,6 @@ SurfaceEval.prototype.evalSurface=function(mesh,mode,un,vn,u1,u2,v1,v2){
745766}
746767global . SurfaceEval = SurfaceEval ;
747768global . CurveEval = CurveEval ;
769+ global . BezierCurve = BezierCurve ;
770+ global . BezierSurface = BezierSurface ;
748771} ) ( this ) ;
0 commit comments