NURBSCurve.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.getPoint = function ( t ) {
  24. var u = this.knots[0] + t * (this.knots[this.knots.length - 1] - this.knots[0]); // linear mapping t->u
  25. // following results in (wx, wy, wz, w) homogeneous point
  26. var hpoint = THREE.NURBSUtils.calcBSplinePoint(this.degree, this.knots, this.controlPoints, u);
  27. if (hpoint.w != 1.0) { // project to 3D space: (wx, wy, wz, w) -> (x, y, z, 1)
  28. hpoint.divideScalar(hpoint.w);
  29. }
  30. return new THREE.Vector3(hpoint.x, hpoint.y, hpoint.z);
  31. };
  32. THREE.NURBSCurve.prototype.getTangent = function ( t ) {
  33. var u = this.knots[0] + t * (this.knots[this.knots.length - 1] - this.knots[0]);
  34. var ders = THREE.NURBSUtils.calcNURBSDerivatives(this.degree, this.knots, this.controlPoints, u, 1);
  35. var tangent = ders[1].clone();
  36. tangent.normalize();
  37. return tangent;
  38. };