public function smoothspline(vec:Array, draw:int = 0, S0:int = 2, D0:Number = 4,

fixend:Boolean=false):Spline3D

{

        var spline:Spline3D = new Spline3D;                        

        var knot:Knot3D;

        for (var j:int = 0;j < vec.length; j++) {

                knot = new Knot3D();                                                                                        knot.setTo(vec[j][0], vec[j][1], vec[j][2]);

                spline.knots.push(knot);

        }

        var d:Number;

        var vecc:Vector3D;

        var i1:int;

        var i2:int;

        for (var i:int = S0; i < (vec.length+S0-1); i++) {

                i1 = i - S0 + 1;

                if (i >= vec.length) {

                        i2 = vec.length - 1;

                } else {

                            i2 = i }

                 spline.knots[i1].outVec =

spline.knots[i2].subtract(spline.knots[i - S0]);

                d =

spline.knots[i1].outVec.normalize();

                vecc =

spline.knots[i1].outVec;

                vecc.scaleBy( d / D0);

                spline.knots[i1].outVec =

vecc;

                spline.knots[i1].outVec =

spline.knots[i1].outVec.add(spline.knots[i1]);

                                

                spline.knots[i1].inVec =

spline.knots[i1];                                

                spline.knots[i1].inVec =

spline.knots[i1].inVec.subtract(vecc);                                                        

        }

        if (vec.length > 0 ) {    

                spline.knots[0].inVec =

spline.knots[0];

                spline.knots[0].outVec =

spline.knots[0];

                                

                if (fixend) {                                

                        spline.knots[vec.length -1].inVec =

spline.knots[vec.length -  1];

                        spline.knots[vec.length - 1].outVec =

spline.knots[vec.length - 1];                        

}

        }

        spline.closed = false;

                        

        //draw for test

        if (draw > 0) {

                var cub : Cube;

                var lin: Lines3D;

                for (j = 0;j < vec.length; j++) {

                        cub = new Cube("",0.1,0.1,0.1);

                        cub.setPosition(vec[j][0],

vec[j][1],

vec[j][2]);

                        environment.addChild( cub );

                        

cub = new Cube("",0.02,0.02,1);

cub.setPosition(

spline.knots[j].inVec.x,

spline.knots[j].inVec.y,

spline.knots[j].inVec.z);

                        environment.addChild( cub );

                        cub = new Cube("",0.02,0.02,1);        

                                        cub.setPosition(spline.knots[j].outVec.x,

spline.knots[j].outVec.y,

spline.knots[j].outVec.z);

                        environment.addChild( cub );        

                                        

                        if (draw > 1) {

                                lin = new Lines3D();

                                lin.moveTo(spline.knots[j].inVec.x,

spline.knots[j].inVec.y,

spline.knots[j].inVec.z);

                                lin.lineTo(spline.knots[j].outVec.x,

spline.knots[j].outVec.y,

spline.knots[j].outVec.z);

                                environment.addChild( lin );

                        }

                }

        }

        return spline;

}