Spline.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Spline from Tween.js, slightly optimized (and trashed)
  3. * http://sole.github.com/tween.js/examples/05_spline.html
  4. *
  5. * @author mrdoob / http://mrdoob.com/
  6. * @author alteredq / http://alteredqualia.com/
  7. */
  8. THREE.Spline = function ( points ) {
  9. this.points = points;
  10. var c = [], v3 = { x: 0, y: 0, z: 0 },
  11. point, intPoint, weight, w2, w3,
  12. pa, pb, pc, pd;
  13. this.initFromArray = function ( a ) {
  14. this.points = [];
  15. for ( var i = 0; i < a.length; i ++ ) {
  16. this.points[ i ] = { x: a[ i ][ 0 ], y: a[ i ][ 1 ], z: a[ i ][ 2 ] };
  17. }
  18. };
  19. this.getPoint = function ( k ) {
  20. point = ( this.points.length - 1 ) * k;
  21. intPoint = Math.floor( point );
  22. weight = point - intPoint;
  23. c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1;
  24. c[ 1 ] = intPoint;
  25. c[ 2 ] = intPoint > this.points.length - 2 ? this.points.length - 1 : intPoint + 1;
  26. c[ 3 ] = intPoint > this.points.length - 3 ? this.points.length - 1 : intPoint + 2;
  27. pa = this.points[ c[ 0 ] ];
  28. pb = this.points[ c[ 1 ] ];
  29. pc = this.points[ c[ 2 ] ];
  30. pd = this.points[ c[ 3 ] ];
  31. w2 = weight * weight;
  32. w3 = weight * w2;
  33. v3.x = interpolate( pa.x, pb.x, pc.x, pd.x, weight, w2, w3 );
  34. v3.y = interpolate( pa.y, pb.y, pc.y, pd.y, weight, w2, w3 );
  35. v3.z = interpolate( pa.z, pb.z, pc.z, pd.z, weight, w2, w3 );
  36. return v3;
  37. };
  38. this.getControlPointsArray = function () {
  39. var i, p, l = this.points.length,
  40. coords = [];
  41. for ( i = 0; i < l; i ++ ) {
  42. p = this.points[ i ];
  43. coords[ i ] = [ p.x, p.y, p.z ];
  44. }
  45. return coords;
  46. };
  47. // approximate length by summing linear segments
  48. this.getLength = function ( nSubDivisions ) {
  49. var i, index, nSamples, position,
  50. point = 0, intPoint = 0, oldIntPoint = 0,
  51. oldPosition = new THREE.Vector3(),
  52. tmpVec = new THREE.Vector3(),
  53. chunkLengths = [],
  54. totalLength = 0;
  55. // first point has 0 length
  56. chunkLengths[ 0 ] = 0;
  57. if ( ! nSubDivisions ) nSubDivisions = 100;
  58. nSamples = this.points.length * nSubDivisions;
  59. oldPosition.copy( this.points[ 0 ] );
  60. for ( i = 1; i < nSamples; i ++ ) {
  61. index = i / nSamples;
  62. position = this.getPoint( index );
  63. tmpVec.copy( position );
  64. totalLength += tmpVec.distanceTo( oldPosition );
  65. oldPosition.copy( position );
  66. point = ( this.points.length - 1 ) * index;
  67. intPoint = Math.floor( point );
  68. if ( intPoint != oldIntPoint ) {
  69. chunkLengths[ intPoint ] = totalLength;
  70. oldIntPoint = intPoint;
  71. }
  72. }
  73. // last point ends with total length
  74. chunkLengths[ chunkLengths.length ] = totalLength;
  75. return { chunks: chunkLengths, total: totalLength };
  76. };
  77. this.reparametrizeByArcLength = function ( samplingCoef ) {
  78. var i, j,
  79. index, indexCurrent, indexNext,
  80. realDistance,
  81. sampling, position,
  82. newpoints = [],
  83. tmpVec = new THREE.Vector3(),
  84. sl = this.getLength();
  85. newpoints.push( tmpVec.copy( this.points[ 0 ] ).clone() );
  86. for ( i = 1; i < this.points.length; i ++ ) {
  87. //tmpVec.copy( this.points[ i - 1 ] );
  88. //linearDistance = tmpVec.distanceTo( this.points[ i ] );
  89. realDistance = sl.chunks[ i ] - sl.chunks[ i - 1 ];
  90. sampling = Math.ceil( samplingCoef * realDistance / sl.total );
  91. indexCurrent = ( i - 1 ) / ( this.points.length - 1 );
  92. indexNext = i / ( this.points.length - 1 );
  93. for ( j = 1; j < sampling - 1; j ++ ) {
  94. index = indexCurrent + j * ( 1 / sampling ) * ( indexNext - indexCurrent );
  95. position = this.getPoint( index );
  96. newpoints.push( tmpVec.copy( position ).clone() );
  97. }
  98. newpoints.push( tmpVec.copy( this.points[ i ] ).clone() );
  99. }
  100. this.points = newpoints;
  101. };
  102. // Catmull-Rom
  103. function interpolate( p0, p1, p2, p3, t, t2, t3 ) {
  104. var v0 = ( p2 - p0 ) * 0.5,
  105. v1 = ( p3 - p1 ) * 0.5;
  106. return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  107. };
  108. };