CatmullRomCurve3.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { Vector3 } from '../../math/Vector3.js';
  2. import { Curve } from '../core/Curve.js';
  3. /**
  4. * Centripetal CatmullRom Curve - which is useful for avoiding
  5. * cusps and self-intersections in non-uniform catmull rom curves.
  6. * http://www.cemyuksel.com/research/catmullrom_param/catmullrom.pdf
  7. *
  8. * curve.type accepts centripetal(default), chordal and catmullrom
  9. * curve.tension is used for catmullrom which defaults to 0.5
  10. */
  11. /*
  12. Based on an optimized c++ solution in
  13. - http://stackoverflow.com/questions/9489736/catmull-rom-curve-with-no-cusps-and-no-self-intersections/
  14. - http://ideone.com/NoEbVM
  15. This CubicPoly class could be used for reusing some variables and calculations,
  16. but for three.js curve use, it could be possible inlined and flatten into a single function call
  17. which can be placed in CurveUtils.
  18. */
  19. function CubicPoly() {
  20. let c0 = 0, c1 = 0, c2 = 0, c3 = 0;
  21. /*
  22. * Compute coefficients for a cubic polynomial
  23. * p(s) = c0 + c1*s + c2*s^2 + c3*s^3
  24. * such that
  25. * p(0) = x0, p(1) = x1
  26. * and
  27. * p'(0) = t0, p'(1) = t1.
  28. */
  29. function init( x0, x1, t0, t1 ) {
  30. c0 = x0;
  31. c1 = t0;
  32. c2 = - 3 * x0 + 3 * x1 - 2 * t0 - t1;
  33. c3 = 2 * x0 - 2 * x1 + t0 + t1;
  34. }
  35. return {
  36. initCatmullRom: function ( x0, x1, x2, x3, tension ) {
  37. init( x1, x2, tension * ( x2 - x0 ), tension * ( x3 - x1 ) );
  38. },
  39. initNonuniformCatmullRom: function ( x0, x1, x2, x3, dt0, dt1, dt2 ) {
  40. // compute tangents when parameterized in [t1,t2]
  41. let t1 = ( x1 - x0 ) / dt0 - ( x2 - x0 ) / ( dt0 + dt1 ) + ( x2 - x1 ) / dt1;
  42. let t2 = ( x2 - x1 ) / dt1 - ( x3 - x1 ) / ( dt1 + dt2 ) + ( x3 - x2 ) / dt2;
  43. // rescale tangents for parametrization in [0,1]
  44. t1 *= dt1;
  45. t2 *= dt1;
  46. init( x1, x2, t1, t2 );
  47. },
  48. calc: function ( t ) {
  49. const t2 = t * t;
  50. const t3 = t2 * t;
  51. return c0 + c1 * t + c2 * t2 + c3 * t3;
  52. }
  53. };
  54. }
  55. //
  56. const tmp = new Vector3();
  57. const px = new CubicPoly(), py = new CubicPoly(), pz = new CubicPoly();
  58. class CatmullRomCurve3 extends Curve {
  59. constructor( points, closed, curveType, tension ) {
  60. super();
  61. this.type = 'CatmullRomCurve3';
  62. Object.defineProperty( this, 'isCatmullRomCurve3', { value: true } );
  63. this.points = points || [];
  64. this.closed = closed || false;
  65. this.curveType = curveType || 'centripetal';
  66. this.tension = ( tension !== undefined ) ? tension : 0.5;
  67. }
  68. getPoint( t, optionalTarget ) {
  69. const point = optionalTarget || new Vector3();
  70. const points = this.points;
  71. const l = points.length;
  72. const p = ( l - ( this.closed ? 0 : 1 ) ) * t;
  73. let intPoint = Math.floor( p );
  74. let weight = p - intPoint;
  75. if ( this.closed ) {
  76. intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / l ) + 1 ) * l;
  77. } else if ( weight === 0 && intPoint === l - 1 ) {
  78. intPoint = l - 2;
  79. weight = 1;
  80. }
  81. let p0, p3; // 4 points (p1 & p2 defined below)
  82. if ( this.closed || intPoint > 0 ) {
  83. p0 = points[ ( intPoint - 1 ) % l ];
  84. } else {
  85. // extrapolate first point
  86. tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
  87. p0 = tmp;
  88. }
  89. const p1 = points[ intPoint % l ];
  90. const p2 = points[ ( intPoint + 1 ) % l ];
  91. if ( this.closed || intPoint + 2 < l ) {
  92. p3 = points[ ( intPoint + 2 ) % l ];
  93. } else {
  94. // extrapolate last point
  95. tmp.subVectors( points[ l - 1 ], points[ l - 2 ] ).add( points[ l - 1 ] );
  96. p3 = tmp;
  97. }
  98. if ( this.curveType === 'centripetal' || this.curveType === 'chordal' ) {
  99. // init Centripetal / Chordal Catmull-Rom
  100. const pow = this.curveType === 'chordal' ? 0.5 : 0.25;
  101. let dt0 = Math.pow( p0.distanceToSquared( p1 ), pow );
  102. let dt1 = Math.pow( p1.distanceToSquared( p2 ), pow );
  103. let dt2 = Math.pow( p2.distanceToSquared( p3 ), pow );
  104. // safety check for repeated points
  105. if ( dt1 < 1e-4 )
  106. dt1 = 1.0;
  107. if ( dt0 < 1e-4 )
  108. dt0 = dt1;
  109. if ( dt2 < 1e-4 )
  110. dt2 = dt1;
  111. px.initNonuniformCatmullRom( p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2 );
  112. py.initNonuniformCatmullRom( p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2 );
  113. pz.initNonuniformCatmullRom( p0.z, p1.z, p2.z, p3.z, dt0, dt1, dt2 );
  114. } else if ( this.curveType === 'catmullrom' ) {
  115. px.initCatmullRom( p0.x, p1.x, p2.x, p3.x, this.tension );
  116. py.initCatmullRom( p0.y, p1.y, p2.y, p3.y, this.tension );
  117. pz.initCatmullRom( p0.z, p1.z, p2.z, p3.z, this.tension );
  118. }
  119. point.set(
  120. px.calc( weight ),
  121. py.calc( weight ),
  122. pz.calc( weight )
  123. );
  124. return point;
  125. }
  126. copy( source ) {
  127. super.copy( source );
  128. this.points = [];
  129. for ( let i = 0, l = source.points.length; i < l; i ++ ) {
  130. const point = source.points[ i ];
  131. this.points.push( point.clone() );
  132. }
  133. this.closed = source.closed;
  134. this.curveType = source.curveType;
  135. this.tension = source.tension;
  136. return this;
  137. }
  138. toJSON() {
  139. const data = super.toJSON();
  140. data.points = [];
  141. for ( let i = 0, l = this.points.length; i < l; i ++ ) {
  142. const point = this.points[ i ];
  143. data.points.push( point.toArray() );
  144. }
  145. data.closed = this.closed;
  146. data.curveType = this.curveType;
  147. data.tension = this.tension;
  148. return data;
  149. }
  150. fromJSON( json ) {
  151. super.fromJSON( json );
  152. this.points = [];
  153. for ( let i = 0, l = json.points.length; i < l; i ++ ) {
  154. const point = json.points[ i ];
  155. this.points.push( new Vector3().fromArray( point ) );
  156. }
  157. this.closed = json.closed;
  158. this.curveType = json.curveType;
  159. this.tension = json.tension;
  160. return this;
  161. }
  162. }
  163. export { CatmullRomCurve3 };