Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math: Convert to classes. #21628

Merged
merged 2 commits into from
Apr 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 25 additions & 36 deletions src/math/Interpolant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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 };
35 changes: 16 additions & 19 deletions src/math/interpolants/CubicInterpolant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -148,7 +146,6 @@ CubicInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype

}

} );

}

export { CubicInterpolant };
14 changes: 6 additions & 8 deletions src/math/interpolants/DiscreteInterpolant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
14 changes: 6 additions & 8 deletions src/math/interpolants/LinearInterpolant.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -34,7 +32,7 @@ LinearInterpolant.prototype = Object.assign( Object.create( Interpolant.prototyp

}

} );
}


export { LinearInterpolant };
14 changes: 6 additions & 8 deletions src/math/interpolants/QuaternionLinearInterpolant.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,7 +33,7 @@ QuaternionLinearInterpolant.prototype = Object.assign( Object.create( Interpolan

}

} );
}


export { QuaternionLinearInterpolant };
10 changes: 6 additions & 4 deletions test/unit/src/math/Interpolant.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand Down