Interpolant.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. */
  21. class Interpolant {
  22. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  23. this.parameterPositions = parameterPositions;
  24. this._cachedIndex = 0;
  25. this.resultBuffer = resultBuffer !== undefined ?
  26. resultBuffer : new sampleValues.constructor( sampleSize );
  27. this.sampleValues = sampleValues;
  28. this.valueSize = sampleSize;
  29. this.settings = null;
  30. this.DefaultSettings_ = {};
  31. }
  32. evaluate( t ) {
  33. const pp = this.parameterPositions;
  34. let i1 = this._cachedIndex,
  35. t1 = pp[ i1 ],
  36. t0 = pp[ i1 - 1 ];
  37. validate_interval: {
  38. seek: {
  39. let right;
  40. linear_scan: {
  41. //- See http://jsperf.com/comparison-to-undefined/3
  42. //- slower code:
  43. //-
  44. //- if ( t >= t1 || t1 === undefined ) {
  45. forward_scan: if ( ! ( t < t1 ) ) {
  46. for ( let giveUpAt = i1 + 2; ; ) {
  47. if ( t1 === undefined ) {
  48. if ( t < t0 ) break forward_scan;
  49. // after end
  50. i1 = pp.length;
  51. this._cachedIndex = i1;
  52. return this.afterEnd_( i1 - 1, t, t0 );
  53. }
  54. if ( i1 === giveUpAt ) break; // this loop
  55. t0 = t1;
  56. t1 = pp[ ++ i1 ];
  57. if ( t < t1 ) {
  58. // we have arrived at the sought interval
  59. break seek;
  60. }
  61. }
  62. // prepare binary search on the right side of the index
  63. right = pp.length;
  64. break linear_scan;
  65. }
  66. //- slower code:
  67. //- if ( t < t0 || t0 === undefined ) {
  68. if ( ! ( t >= t0 ) ) {
  69. // looping?
  70. const t1global = pp[ 1 ];
  71. if ( t < t1global ) {
  72. i1 = 2; // + 1, using the scan for the details
  73. t0 = t1global;
  74. }
  75. // linear reverse scan
  76. for ( let giveUpAt = i1 - 2; ; ) {
  77. if ( t0 === undefined ) {
  78. // before start
  79. this._cachedIndex = 0;
  80. return this.beforeStart_( 0, t, t1 );
  81. }
  82. if ( i1 === giveUpAt ) break; // this loop
  83. t1 = t0;
  84. t0 = pp[ -- i1 - 1 ];
  85. if ( t >= t0 ) {
  86. // we have arrived at the sought interval
  87. break seek;
  88. }
  89. }
  90. // prepare binary search on the left side of the index
  91. right = i1;
  92. i1 = 0;
  93. break linear_scan;
  94. }
  95. // the interval is valid
  96. break validate_interval;
  97. } // linear scan
  98. // binary search
  99. while ( i1 < right ) {
  100. const mid = ( i1 + right ) >>> 1;
  101. if ( t < pp[ mid ] ) {
  102. right = mid;
  103. } else {
  104. i1 = mid + 1;
  105. }
  106. }
  107. t1 = pp[ i1 ];
  108. t0 = pp[ i1 - 1 ];
  109. // check boundary cases, again
  110. if ( t0 === undefined ) {
  111. this._cachedIndex = 0;
  112. return this.beforeStart_( 0, t, t1 );
  113. }
  114. if ( t1 === undefined ) {
  115. i1 = pp.length;
  116. this._cachedIndex = i1;
  117. return this.afterEnd_( i1 - 1, t0, t );
  118. }
  119. } // seek
  120. this._cachedIndex = i1;
  121. this.intervalChanged_( i1, t0, t1 );
  122. } // validate_interval
  123. return this.interpolate_( i1, t0, t, t1 );
  124. }
  125. getSettings_() {
  126. return this.settings || this.DefaultSettings_;
  127. }
  128. copySampleValue_( index ) {
  129. // copies a sample value to the result buffer
  130. const result = this.resultBuffer,
  131. values = this.sampleValues,
  132. stride = this.valueSize,
  133. offset = index * stride;
  134. for ( let i = 0; i !== stride; ++ i ) {
  135. result[ i ] = values[ offset + i ];
  136. }
  137. return result;
  138. }
  139. // Template methods for derived classes:
  140. interpolate_( /* i1, t0, t, t1 */ ) {
  141. throw new Error( 'call to abstract method' );
  142. // implementations shall return this.resultBuffer
  143. }
  144. intervalChanged_( /* i1, t0, t1 */ ) {
  145. // empty
  146. }
  147. }
  148. // ALIAS DEFINITIONS
  149. Interpolant.prototype.beforeStart_ = Interpolant.prototype.copySampleValue_;
  150. Interpolant.prototype.afterEnd_ = Interpolant.prototype.copySampleValue_;
  151. export { Interpolant };
粤ICP备19079148号