NURBSCurve.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @author renej
  3. * NURBS curve object
  4. *
  5. * Derives from Curve, overriding getPoint and getTangent.
  6. *
  7. * Implementation is based on (x, y [, z=0 [, w=1]]) control points with w=weight.
  8. *
  9. **/
  10. /**************************************************************
  11. * NURBS curve
  12. **************************************************************/
  13. THREE.NURBSCurve = function ( degree, knots /* array of reals */, controlPoints /* array of Vector(2|3|4) */) {
  14. this.degree = degree;
  15. this.knots = knots;
  16. this.controlPoints = [];
  17. for (var i = 0; i < controlPoints.length; ++i) { // ensure Vector4 for control points
  18. var point = controlPoints[i];
  19. this.controlPoints[i] = new THREE.Vector4(point.x, point.y, point.z, point.w);
  20. }
  21. };
  22. THREE.NURBSCurve.prototype = Object.create( THREE.Curve.prototype );
  23. THREE.NURBSCurve.prototype.constructor = THREE.NURBSCurve;
  24. THREE.NURBSCurve.prototype.getPoint = function ( t ) {
  25. var u = this.knots[0] + t * (this.knots[this.knots.length - 1] - this.knots[0]); // linear mapping t->u
  26. // following results in (wx, wy, wz, w) homogeneous point
  27. var hpoint = THREE.NURBSUtils.calcBSplinePoint(this.degree, this.knots, this.controlPoints, u);
  28. if (hpoint.w != 1.0) { // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  29. hpoint.divideScalar(hpoint.w);
  30. }
  31. return new THREE.Vector3(hpoint.x, hpoint.y, hpoint.z);
  32. };
  33. THREE.NURBSCurve.prototype.getTangent = function ( t ) {
  34. var u = this.knots[0] + t * (this.knots[this.knots.length - 1] - this.knots[0]);
  35. var ders = THREE.NURBSUtils.calcNURBSDerivatives(this.degree, this.knots, this.controlPoints, u, 1);
  36. var tangent = ders[1].clone();
  37. tangent.normalize();
  38. return tangent;
  39. };