Skip to content

Commit

Permalink
Pooling improvements for phetsims/phet-core#103
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Mar 1, 2023
1 parent 8407dcd commit 517b1fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
15 changes: 11 additions & 4 deletions js/Matrix3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export default class Matrix3 implements TPoolable {
this.type = Matrix3Type.IDENTITY;
}

public initialize(): this {
return this;
}

/**
* Convenience getter for the individual 0,0 entry of the matrix.
*/
Expand Down Expand Up @@ -1268,7 +1272,7 @@ export default class Matrix3 implements TPoolable {
}

public static pool = new Pool( Matrix3, {
initialize: Matrix3.prototype.rowMajor,
initialize: Matrix3.prototype.initialize,
useDefaultConstruction: true,
maxSize: 300
} );
Expand Down Expand Up @@ -1471,11 +1475,14 @@ export default class Matrix3 implements TPoolable {

dot.register( 'Matrix3', Matrix3 );

const m3 = Matrix3.pool.create.bind( Matrix3.pool );
dot.register( 'm3', m3 );

const fromPool = Matrix3.pool.fetch.bind( Matrix3.pool );

const m3 = ( v00: number, v01: number, v02: number, v10: number, v11: number, v12: number, v20: number, v21: number, v22: number, type?: Matrix3Type ): Matrix3 => {
return fromPool().rowMajor( v00, v01, v02, v10, v11, v12, v20, v21, v22, type );
};
export { m3 };
dot.register( 'm3', m3 );

Matrix3.IDENTITY = Matrix3.identity().makeImmutable();
Matrix3.X_REFLECTION = m3(
-1, 0, 0,
Expand Down
24 changes: 12 additions & 12 deletions js/Matrix3Tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Sam Reid (PhET Interactive Simulations)
*/

import Matrix3, { Matrix3Type } from './Matrix3.js';
import Matrix3, { Matrix3Type, m3 } from './Matrix3.js';
import Vector2 from './Vector2.js';

QUnit.module( 'Matrix3' );
Expand All @@ -28,23 +28,23 @@ function approximateMatrixEqual( assert, a, b, msg ) {

// test matrices, randomly generated
function A() {
return Matrix3.pool.create(
return m3(
0.216673, -0.455249, -0.0897734,
-0.261922, -0.208968, -0.0790977,
-0.0689069, -0.620147, 0.275399
);
}

function B() {
return Matrix3.pool.create(
return m3(
0.366511, -0.872824, 0.490591,
0.0543773, 0.610759, 0.961396,
0.880586, 0.991026, -0.358927
);
}

function C() {
return Matrix3.pool.create(
return m3(
0.521806, 0.523286, -0.275077,
0.270099, 0.135544, 0.614202,
0, 0, 1
Expand All @@ -63,7 +63,7 @@ QUnit.test( 'Affine detection', assert => {
} );

QUnit.test( 'Row-major', assert => {
const m = Matrix3.pool.create( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
const m = m3( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
assert.equal( m.m00(), 1, 'm00' );
assert.equal( m.m01(), 2, 'm01' );
assert.equal( m.m02(), 3, 'm02' );
Expand All @@ -76,7 +76,7 @@ QUnit.test( 'Row-major', assert => {
} );

QUnit.test( 'Column-major', assert => {
const m = Matrix3.pool.create();
const m = m3();
m.columnMajor( 1, 4, 7, 2, 5, 8, 3, 6, 9 );
assert.equal( m.m00(), 1, 'm00' );
assert.equal( m.m01(), 2, 'm01' );
Expand All @@ -102,7 +102,7 @@ QUnit.test( 'Rotation', assert => {
QUnit.test( 'plus / add', assert => {
const a = A();
const b = B();
const result = Matrix3.pool.create( 0.583184, -1.32807, 0.400818, -0.207545, 0.401791, 0.882298, 0.81168, 0.370878, -0.0835274 );
const result = m3( 0.583184, -1.32807, 0.400818, -0.207545, 0.401791, 0.882298, 0.81168, 0.370878, -0.0835274 );

approximateMatrixEqual( assert, a.plus( b ), result, 'plus' );
approximateMatrixEqual( assert, a, A(), 'verifying immutability' );
Expand All @@ -114,7 +114,7 @@ QUnit.test( 'plus / add', assert => {
QUnit.test( 'minus / subtract', assert => {
const a = A();
const b = B();
const result = Matrix3.pool.create( -0.149837, 0.417574, -0.580365, -0.3163, -0.819726, -1.04049, -0.949493, -1.61117, 0.634326 );
const result = m3( -0.149837, 0.417574, -0.580365, -0.3163, -0.819726, -1.04049, -0.949493, -1.61117, 0.634326 );

approximateMatrixEqual( assert, a.minus( b ), result, 'minus' );
approximateMatrixEqual( assert, a, A(), 'verifying immutability' );
Expand All @@ -125,7 +125,7 @@ QUnit.test( 'minus / subtract', assert => {

QUnit.test( 'transposed / transpose', assert => {
const a = A();
const result = Matrix3.pool.create( 0.216673, -0.261922, -0.0689069, -0.455249, -0.208968, -0.620147, -0.0897734, -0.0790977, 0.275399 );
const result = m3( 0.216673, -0.261922, -0.0689069, -0.455249, -0.208968, -0.620147, -0.0897734, -0.0790977, 0.275399 );

approximateMatrixEqual( assert, a.transposed(), result, 'transposed' );
approximateMatrixEqual( assert, a, A(), 'verifying immutability' );
Expand All @@ -136,7 +136,7 @@ QUnit.test( 'transposed / transpose', assert => {

QUnit.test( 'negated / negate', assert => {
const a = A();
const result = Matrix3.pool.create( -0.216673, 0.455249, 0.0897734, 0.261922, 0.208968, 0.0790977, 0.0689069, 0.620147, -0.275399 );
const result = m3( -0.216673, 0.455249, 0.0897734, 0.261922, 0.208968, 0.0790977, 0.0689069, 0.620147, -0.275399 );

approximateMatrixEqual( assert, a.negated(), result, 'negated' );
approximateMatrixEqual( assert, a, A(), 'verifying immutability' );
Expand All @@ -147,7 +147,7 @@ QUnit.test( 'negated / negate', assert => {

QUnit.test( 'inverted / invert', assert => {
const a = A();
const result = Matrix3.pool.create( 1.48663, -2.52483, -0.240555, -1.08195, -0.745893, -0.566919, -2.06439, -2.31134, 2.29431 );
const result = m3( 1.48663, -2.52483, -0.240555, -1.08195, -0.745893, -0.566919, -2.06439, -2.31134, 2.29431 );

approximateMatrixEqual( assert, a.inverted(), result, 'inverted' );
approximateMatrixEqual( assert, a, A(), 'verifying immutability' );
Expand All @@ -159,7 +159,7 @@ QUnit.test( 'inverted / invert', assert => {
QUnit.test( 'timesMatrix / multiplyMatrix', assert => {
const a = A();
const b = B();
const result = Matrix3.pool.create( -0.0243954, -0.556133, -0.299155, -0.177013, 0.0225954, -0.301007, 0.183536, -0.0456892, -0.72886 );
const result = m3( -0.0243954, -0.556133, -0.299155, -0.177013, 0.0225954, -0.301007, 0.183536, -0.0456892, -0.72886 );

approximateMatrixEqual( assert, a.timesMatrix( b ), result, 'timesMatrix' );
approximateMatrixEqual( assert, a, A(), 'verifying immutability' );
Expand Down
16 changes: 8 additions & 8 deletions js/Transform3Tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @author Sam Reid (PhET Interactive Simulations)
*/

import Matrix3 from './Matrix3.js';
import Matrix3, { m3 } from './Matrix3.js';
import Ray2 from './Ray2.js';
import Transform3 from './Transform3.js';
import Vector2 from './Vector2.js';
Expand All @@ -25,7 +25,7 @@ function approximateRayEqual( assert, a, b, msg ) {
}

QUnit.test( 'Ray2 transforms', assert => {
const transform = new Transform3( Matrix3.pool.create( 0, -2, 5, 3, 0, 8, 0, 0, 1 ) );
const transform = new Transform3( m3( 0, -2, 5, 3, 0, 8, 0, 0, 1 ) );
const ray = new Ray2( new Vector2( 2, 7 ), new Vector2( -5, 2 ).normalized() );

const tray = transform.transformRay2( ray );
Expand All @@ -43,7 +43,7 @@ QUnit.test( 'Ray2 transforms', assert => {
} );

QUnit.test( 'Transform x/y', assert => {
const t = new Transform3( Matrix3.pool.create( 2, 0, 10, 0, 3, 1, 0, 0, 1 ) );
const t = new Transform3( m3( 2, 0, 10, 0, 3, 1, 0, 0, 1 ) );
assert.equal( t.transformX( 5 ), 20 );
assert.equal( t.transformY( 5 ), 16 );
assert.equal( t.inverseX( 20 ), 5 );
Expand All @@ -59,8 +59,8 @@ QUnit.test( 'Transform x/y', assert => {
} );

QUnit.test( 'Transform delta', assert => {
const t1 = new Transform3( Matrix3.pool.create( 2, 1, 0, -2, 5, 0, 0, 0, 1 ) );
const t2 = new Transform3( Matrix3.pool.create( 2, 1, 52, -2, 5, -61, 0, 0, 1 ) );
const t1 = new Transform3( m3( 2, 1, 0, -2, 5, 0, 0, 0, 1 ) );
const t2 = new Transform3( m3( 2, 1, 52, -2, 5, -61, 0, 0, 1 ) );

assert.ok( t1.transformDelta2( Vector2.ZERO ).equalsEpsilon( Vector2.ZERO, 1e-7 ), 'ensuring linearity at 0, no translation' );
assert.ok( t2.transformDelta2( Vector2.ZERO ).equalsEpsilon( Vector2.ZERO, 1e-7 ), 'ensuring linearity at 0, with translation' );
Expand All @@ -74,7 +74,7 @@ QUnit.test( 'Transform delta', assert => {
} );

QUnit.test( 'Transform delta x/y', assert => {
const t = new Transform3( Matrix3.pool.create( 2, 0, 52, 0, 5, -61, 0, 0, 1 ) );
const t = new Transform3( m3( 2, 0, 52, 0, 5, -61, 0, 0, 1 ) );

approximateEqual( assert, t.transformDeltaX( 1 ), 2, 'deltaX' );
approximateEqual( assert, t.transformDeltaY( 1 ), 5, 'deltaY' );
Expand All @@ -92,11 +92,11 @@ QUnit.test( 'Transform setMatrix ensuring matrix instance equivalence', assert =

const m = t.getMatrix();

t.setMatrix( Matrix3.pool.create( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) );
t.setMatrix( m3( 1, 2, 3, 4, 5, 6, 7, 8, 9 ) );
assert.equal( t.getMatrix(), m );
assert.equal( t.getMatrix().m00(), 1 );
assert.equal( t.getMatrix().m01(), 2 );
t.setMatrix( Matrix3.pool.create( 9, 8, 7, 6, 5, 4, 3, 2, 1 ) );
t.setMatrix( m3( 9, 8, 7, 6, 5, 4, 3, 2, 1 ) );
assert.equal( t.getMatrix(), m );
assert.equal( t.getMatrix().m00(), 9 );
assert.equal( t.getMatrix().m01(), 8 );
Expand Down

0 comments on commit 517b1fa

Please sign in to comment.