Interpolant.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * Abstract base class of interpolants over parametric samples.
  3. *
  4. * The parameter domain is one dimensional, typically the time or a path
  5. * along a curve defined by the data.
  6. *
  7. * The sample values can have any dimensionality and derived classes may
  8. * apply special interpretations to the data.
  9. *
  10. * This class provides the interval seek in a Template Method, deferring
  11. * the actual interpolation to derived classes.
  12. *
  13. * Time complexity is O(1) for linear access crossing at most two points
  14. * and O(log N) for random access, where N is the number of positions.
  15. *
  16. * References:
  17. *
  18. * http://www.oodesign.com/template-method-pattern.html
  19. *
  20. * @author tschw
  21. */
  22. THREE.Interpolant = function(
  23. parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  24. this.parameterPositions = parameterPositions;
  25. this._cachedIndex = 0;
  26. this.resultBuffer = resultBuffer !== undefined ?
  27. resultBuffer : new sampleValues.constructor( sampleSize );
  28. this.sampleValues = sampleValues;
  29. this.valueSize = sampleSize;
  30. };
  31. THREE.Interpolant.prototype = {
  32. constructor: THREE.Interpolant,
  33. evaluate: function( t ) {
  34. var pp = this.parameterPositions,
  35. i1 = this._cachedIndex,
  36. t1 = pp[ i1 ],
  37. t0 = pp[ i1 - 1 ];
  38. validate_interval: {
  39. seek: {
  40. var right;
  41. linear_scan: {
  42. //- See http://jsperf.com/comparison-to-undefined/3
  43. //- slower code:
  44. //-
  45. //- if ( t >= t1 || t1 === undefined ) {
  46. forward_scan: if ( ! ( t < t1 ) ) {
  47. for ( var giveUpAt = i1 + 2; ;) {
  48. if ( t1 === undefined ) {
  49. if ( t < t0 ) break forward_scan;
  50. // after end
  51. i1 = pp.length;
  52. this._cachedIndex = i1;
  53. return this.afterEnd_( i1 - 1, t, t0 );
  54. }
  55. if ( i1 === giveUpAt ) break; // this loop
  56. t0 = t1;
  57. t1 = pp[ ++ i1 ];
  58. if ( t < t1 ) {
  59. // we have arrived at the sought interval
  60. break seek;
  61. }
  62. }
  63. // prepare binary search on the right side of the index
  64. right = pp.length;
  65. break linear_scan;
  66. }
  67. //- slower code:
  68. //- if ( t < t0 || t0 === undefined ) {
  69. if ( ! ( t >= t0 ) ) {
  70. // looping?
  71. var t1global = pp[ 1 ];
  72. if ( t < t1global ) {
  73. i1 = 2; // + 1, using the scan for the details
  74. t0 = t1global;
  75. }
  76. // linear reverse scan
  77. for ( var giveUpAt = i1 - 2; ;) {
  78. if ( t0 === undefined ) {
  79. // before start
  80. this._cachedIndex = 0;
  81. return this.beforeStart_( 0, t, t1 );
  82. }
  83. if ( i1 === giveUpAt ) break; // this loop
  84. t1 = t0;
  85. t0 = pp[ -- i1 - 1 ];
  86. if ( t >= t0 ) {
  87. // we have arrived at the sought interval
  88. break seek;
  89. }
  90. }
  91. // prepare binary search on the left side of the index
  92. right = i1;
  93. i1 = 0;
  94. break linear_scan;
  95. }
  96. // the interval is valid
  97. break validate_interval;
  98. } // linear scan
  99. // binary search
  100. while ( i1 < right ) {
  101. var mid = ( i1 + right ) >>> 1;
  102. if ( t < pp[ mid ] ) {
  103. right = mid;
  104. } else {
  105. i1 = mid + 1;
  106. }
  107. }
  108. t1 = pp[ i1 ];
  109. t0 = pp[ i1 - 1 ];
  110. // check boundary cases, again
  111. if ( t0 === undefined ) {
  112. this._cachedIndex = 0;
  113. return this.beforeStart_( 0, t, t1 );
  114. }
  115. if ( t1 === undefined ) {
  116. i1 = pp.length;
  117. this._cachedIndex = i1;
  118. return this.afterEnd_( i1 - 1, t0, t );
  119. }
  120. } // seek
  121. this._cachedIndex = i1;
  122. this.intervalChanged_( i1, t0, t1 );
  123. } // validate_interval
  124. return this.interpolate_( i1, t0, t, t1 );
  125. },
  126. settings: null, // optional, subclass-specific settings structure
  127. // Note: The indirection allows central control of many interpolants.
  128. // --- Protected interface
  129. DefaultSettings_: {},
  130. getSettings_: function() {
  131. return this.settings || this.DefaultSettings_;
  132. },
  133. copySampleValue_: function( index ) {
  134. // copies a sample value to the result buffer
  135. var result = this.resultBuffer,
  136. values = this.sampleValues,
  137. stride = this.valueSize,
  138. offset = index * stride;
  139. for ( var i = 0; i !== stride; ++ i ) {
  140. result[ i ] = values[ offset + i ];
  141. }
  142. return result;
  143. },
  144. // Template methods for derived classes:
  145. interpolate_: function( i1, t0, t, t1 ) {
  146. throw new Error( "call to abstract method" );
  147. // implementations shall return this.resultBuffer
  148. },
  149. intervalChanged_: function( i1, t0, t1 ) {
  150. // empty
  151. }
  152. };
  153. Object.assign( THREE.Interpolant.prototype, {
  154. beforeStart_: //( 0, t, t0 ), returns this.resultBuffer
  155. THREE.Interpolant.prototype.copySampleValue_,
  156. afterEnd_: //( N-1, tN-1, t ), returns this.resultBuffer
  157. THREE.Interpolant.prototype.copySampleValue_
  158. } );
粤ICP备19079148号