Переглянути джерело

Math: Convert to classes. (#21628)

* Math: Convert to classes.

* Tests: Clean up.
Michael Herzog 4 роки тому
батько
коміт
817fa32d05

+ 25 - 36
src/math/Interpolant.js

@@ -19,21 +19,24 @@
  *
  */
 
-function Interpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
+class Interpolant {
 
-	this.parameterPositions = parameterPositions;
-	this._cachedIndex = 0;
+	constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-	this.resultBuffer = resultBuffer !== undefined ?
-		resultBuffer : new sampleValues.constructor( sampleSize );
-	this.sampleValues = sampleValues;
-	this.valueSize = sampleSize;
+		this.parameterPositions = parameterPositions;
+		this._cachedIndex = 0;
 
-}
+		this.resultBuffer = resultBuffer !== undefined ?
+			resultBuffer : new sampleValues.constructor( sampleSize );
+		this.sampleValues = sampleValues;
+		this.valueSize = sampleSize;
+
+		this.settings = null;
+		this.DefaultSettings_ = {};
 
-Object.assign( Interpolant.prototype, {
+	}
 
-	evaluate: function ( t ) {
+	evaluate( t ) {
 
 		const pp = this.parameterPositions;
 		let i1 = this._cachedIndex,
@@ -191,22 +194,15 @@ Object.assign( Interpolant.prototype, {
 
 		return this.interpolate_( i1, t0, t, t1 );
 
-	},
-
-	settings: null, // optional, subclass-specific settings structure
-	// Note: The indirection allows central control of many interpolants.
-
-	// --- Protected interface
-
-	DefaultSettings_: {},
+	}
 
-	getSettings_: function () {
+	getSettings_() {
 
 		return this.settings || this.DefaultSettings_;
 
-	},
+	}
 
-	copySampleValue_: function ( index ) {
+	copySampleValue_( index ) {
 
 		// copies a sample value to the result buffer
 
@@ -223,35 +219,28 @@ Object.assign( Interpolant.prototype, {
 
 		return result;
 
-	},
+	}
 
 	// Template methods for derived classes:
 
-	interpolate_: function ( /* i1, t0, t, t1 */ ) {
+	interpolate_( /* i1, t0, t, t1 */ ) {
 
 		throw new Error( 'call to abstract method' );
 		// implementations shall return this.resultBuffer
 
-	},
+	}
 
-	intervalChanged_: function ( /* i1, t0, t1 */ ) {
+	intervalChanged_( /* i1, t0, t1 */ ) {
 
 		// empty
 
 	}
 
-} );
-
-// DECLARE ALIAS AFTER assign prototype
-Object.assign( Interpolant.prototype, {
-
-	//( 0, t, t0 ), returns this.resultBuffer
-	beforeStart_: Interpolant.prototype.copySampleValue_,
-
-	//( N-1, tN-1, t ), returns this.resultBuffer
-	afterEnd_: Interpolant.prototype.copySampleValue_,
+}
 
-} );
+// ALIAS DEFINITIONS
 
+Interpolant.prototype.beforeStart_ = Interpolant.prototype.copySampleValue_;
+Interpolant.prototype.afterEnd_ = Interpolant.prototype.copySampleValue_;
 
 export { Interpolant };

+ 16 - 19
src/math/interpolants/CubicInterpolant.js

@@ -10,29 +10,27 @@ import { WrapAroundEnding, ZeroSlopeEnding } from '../../constants.js';
  * over their parameter interval.
  */
 
-function CubicInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
+class CubicInterpolant extends Interpolant {
 
-	Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
+	constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-	this._weightPrev = - 0;
-	this._offsetPrev = - 0;
-	this._weightNext = - 0;
-	this._offsetNext = - 0;
+		super( parameterPositions, sampleValues, sampleSize, resultBuffer );
 
-}
-
-CubicInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
+		this._weightPrev = - 0;
+		this._offsetPrev = - 0;
+		this._weightNext = - 0;
+		this._offsetNext = - 0;
 
-	constructor: CubicInterpolant,
+		this.DefaultSettings_ = {
 
-	DefaultSettings_: {
+			endingStart: ZeroCurvatureEnding,
+			endingEnd: ZeroCurvatureEnding
 
-		endingStart: ZeroCurvatureEnding,
-		endingEnd: ZeroCurvatureEnding
+		};
 
-	},
+	}
 
-	intervalChanged_: function ( i1, t0, t1 ) {
+	intervalChanged_( i1, t0, t1 ) {
 
 		const pp = this.parameterPositions;
 		let iPrev = i1 - 2,
@@ -109,9 +107,9 @@ CubicInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype
 		this._offsetPrev = iPrev * stride;
 		this._offsetNext = iNext * stride;
 
-	},
+	}
 
-	interpolate_: function ( i1, t0, t, t1 ) {
+	interpolate_( i1, t0, t, t1 ) {
 
 		const result = this.resultBuffer,
 			values = this.sampleValues,
@@ -148,7 +146,6 @@ CubicInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype
 
 	}
 
-} );
-
+}
 
 export { CubicInterpolant };

+ 6 - 8
src/math/interpolants/DiscreteInterpolant.js

@@ -6,23 +6,21 @@ import { Interpolant } from '../Interpolant.js';
  * the parameter.
  */
 
-function DiscreteInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
+class DiscreteInterpolant extends Interpolant {
 
-	Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
+	constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-}
-
-DiscreteInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
+		super( parameterPositions, sampleValues, sampleSize, resultBuffer );
 
-	constructor: DiscreteInterpolant,
+	}
 
-	interpolate_: function ( i1 /*, t0, t, t1 */ ) {
+	interpolate_( i1 /*, t0, t, t1 */ ) {
 
 		return this.copySampleValue_( i1 - 1 );
 
 	}
 
-} );
+}
 
 
 export { DiscreteInterpolant };

+ 6 - 8
src/math/interpolants/LinearInterpolant.js

@@ -1,16 +1,14 @@
 import { Interpolant } from '../Interpolant.js';
 
-function LinearInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
+class LinearInterpolant extends Interpolant {
 
-	Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
+	constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-}
-
-LinearInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
+		super( parameterPositions, sampleValues, sampleSize, resultBuffer );
 
-	constructor: LinearInterpolant,
+	}
 
-	interpolate_: function ( i1, t0, t, t1 ) {
+	interpolate_( i1, t0, t, t1 ) {
 
 		const result = this.resultBuffer,
 			values = this.sampleValues,
@@ -34,7 +32,7 @@ LinearInterpolant.prototype = Object.assign( Object.create( Interpolant.prototyp
 
 	}
 
-} );
+}
 
 
 export { LinearInterpolant };

+ 6 - 8
src/math/interpolants/QuaternionLinearInterpolant.js

@@ -5,17 +5,15 @@ import { Quaternion } from '../Quaternion.js';
  * Spherical linear unit quaternion interpolant.
  */
 
-function QuaternionLinearInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
+class QuaternionLinearInterpolant extends Interpolant {
 
-	Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
+	constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-}
-
-QuaternionLinearInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), {
+		super( parameterPositions, sampleValues, sampleSize, resultBuffer );
 
-	constructor: QuaternionLinearInterpolant,
+	}
 
-	interpolate_: function ( i1, t0, t, t1 ) {
+	interpolate_( i1, t0, t, t1 ) {
 
 		const result = this.resultBuffer,
 			values = this.sampleValues,
@@ -35,7 +33,7 @@ QuaternionLinearInterpolant.prototype = Object.assign( Object.create( Interpolan
 
 	}
 
-} );
+}
 
 
 export { QuaternionLinearInterpolant };

+ 6 - 4
test/unit/src/math/Interpolant.tests.js

@@ -9,13 +9,15 @@ export default QUnit.module( 'Maths', () => {
 		// Since this is an abstract base class, we have to make it concrete in order
 		// to QUnit.test its functionality...
 
-		function Mock( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
+		class Mock extends Interpolant {
 
-			Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer );
+			constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
 
-		}
+				super( parameterPositions, sampleValues, sampleSize, resultBuffer );
+
+			}
 
-		Mock.prototype = Object.create( Interpolant.prototype );
+		}
 
 		Mock.prototype.intervalChanged_ = function intervalChanged( i1, t0, t1 ) {