Skip to content

Commit

Permalink
Poolable => Pool in kite, see 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 c08ca9e commit ef3220b
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 63 deletions.
14 changes: 10 additions & 4 deletions js/ops/Boundary.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Bounds2 from '../../../dot/js/Bounds2.js';
import Ray2 from '../../../dot/js/Ray2.js';
import Vector2 from '../../../dot/js/Vector2.js';
import cleanArray from '../../../phet-core/js/cleanArray.js';
import Poolable from '../../../phet-core/js/Poolable.js';
import Pool from '../../../phet-core/js/Pool.js';
import { kite, Subpath } from '../imports.js';

let globaId = 0;
Expand All @@ -20,7 +20,7 @@ class Boundary {
/**
* @public (kite-internal)
*
* NOTE: Use Boundary.createFromPool for most usage instead of using the constructor directly.
* NOTE: Use Boundary.pool.create for most usage instead of using the constructor directly.
*
* @param {Array.<HalfEdge>} halfEdges
*/
Expand Down Expand Up @@ -236,10 +236,16 @@ class Boundary {
}
return new Subpath( segments, null, true );
}

// @public
freeToPool() {
Boundary.pool.freeToPool( this );
}

// @public
static pool = new Pool( Boundary );
}

kite.register( 'Boundary', Boundary );

Poolable.mixInto( Boundary );

export default Boundary;
18 changes: 12 additions & 6 deletions js/ops/Edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author Jonathan Olson <[email protected]>
*/

import Poolable from '../../../phet-core/js/Poolable.js';
import Pool from '../../../phet-core/js/Pool.js';
import { HalfEdge, kite, Line, Segment, Vertex } from '../imports.js';

let globaId = 0;
Expand All @@ -15,7 +15,7 @@ class Edge {
/**
* @public (kite-internal)
*
* NOTE: Use Edge.createFromPool for most usage instead of using the constructor directly.
* NOTE: Use Edge.pool.create for most usage instead of using the constructor directly.
*
* @param {Segment} segment
* @param {Vertex} startVertex
Expand Down Expand Up @@ -58,8 +58,8 @@ class Edge {
this.signedAreaFragment = segment.getSignedAreaFragment();

// @public {HalfEdge|null} - Null when disposed (in pool)
this.forwardHalf = HalfEdge.createFromPool( this, false );
this.reversedHalf = HalfEdge.createFromPool( this, true );
this.forwardHalf = HalfEdge.pool.create( this, false );
this.reversedHalf = HalfEdge.pool.create( this, true );

// @public {boolean} - Used for depth-first search
this.visited = false;
Expand Down Expand Up @@ -141,10 +141,16 @@ class Edge {
assert && assert( !( this.segment instanceof Line ) || this.startVertex !== this.endVertex,
'No line segments for same vertices' );
}

// @public
freeToPool() {
Edge.pool.freeToPool( this );
}

// @public
static pool = new Pool( Edge );
}

kite.register( 'Edge', Edge );

Poolable.mixInto( Edge );

export default Edge;
14 changes: 10 additions & 4 deletions js/ops/Face.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

import cleanArray from '../../../phet-core/js/cleanArray.js';
import Poolable from '../../../phet-core/js/Poolable.js';
import Pool from '../../../phet-core/js/Pool.js';
import { kite } from '../imports.js';

let globaId = 0;
Expand All @@ -21,7 +21,7 @@ class Face {
/**
* @public (kite-internal)
*
* NOTE: Use Face.createFromPool for most usage instead of using the constructor directly.
* NOTE: Use Face.pool.create for most usage instead of using the constructor directly.
*
* @param {Boundary|null} boundary - Null if it's the unbounded face
*/
Expand Down Expand Up @@ -127,10 +127,16 @@ class Face {
this.recursivelyAddHoles( outerBoundary.childBoundaries[ i ] );
}
}

// @public
freeToPool() {
Face.pool.freeToPool( this );
}

// @public
static pool = new Pool( Face );
}

kite.register( 'Face', Face );

Poolable.mixInto( Face );

export default Face;
56 changes: 28 additions & 28 deletions js/ops/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Graph {
this.loops = [];

// @public {Face}
this.unboundedFace = Face.createFromPool( null );
this.unboundedFace = Face.pool.create( null );

// @public {Array.<Face>}
this.faces = [ this.unboundedFace ];
Expand Down Expand Up @@ -142,7 +142,7 @@ class Graph {
} );

graph.boundaries = obj.boundaries.map( data => {
const boundary = new Boundary( data.halfEdges.map( id => halfEdgeMap[ id ] ) );
const boundary = Boundary.pool.create( data.halfEdges.map( id => halfEdgeMap[ id ] ) );
boundaryMap[ data.id ] = boundary;
boundary.signedArea = data.signedArea;
boundary.bounds = Bounds2.Bounds2IO.fromStateObject( data.bounds );
Expand Down Expand Up @@ -249,27 +249,27 @@ class Graph {

// If they are exactly equal, don't take a chance on floating-point arithmetic
if ( start.equals( end ) ) {
vertices.push( Vertex.createFromPool( start ) );
vertices.push( Vertex.pool.create( start ) );
}
else {
assert && assert( start.distance( end ) < 1e-5, 'Inaccurate start/end points' );
vertices.push( Vertex.createFromPool( start.average( end ) ) );
vertices.push( Vertex.pool.create( start.average( end ) ) );
}
}
if ( !closed ) {
// If we aren't closed, create an "end" vertex since it may be different from the "start"
vertices.push( Vertex.createFromPool( segments[ segments.length - 1 ].end ) );
vertices.push( Vertex.pool.create( segments[ segments.length - 1 ].end ) );
}

// Create the loop object from the vertices, filling in edges
const loop = Loop.createFromPool( shapeId, closed );
const loop = Loop.pool.create( shapeId, closed );
for ( index = 0; index < segments.length; index++ ) {
let nextIndex = index + 1;
if ( closed && nextIndex === segments.length ) {
nextIndex = 0;
}

const edge = Edge.createFromPool( segments[ index ], vertices[ index ], vertices[ nextIndex ] );
const edge = Edge.pool.create( segments[ index ], vertices[ index ], vertices[ nextIndex ] );
loop.halfEdges.push( edge.forwardHalf );
this.addEdge( edge );
}
Expand Down Expand Up @@ -381,19 +381,19 @@ class Graph {
const edge = this.edges[ i ];
if ( edge.forwardHalf.face.filled !== edge.reversedHalf.face.filled ) {
if ( !vertexMap[ edge.startVertex.id ] ) {
const newStartVertex = Vertex.createFromPool( edge.startVertex.point );
const newStartVertex = Vertex.pool.create( edge.startVertex.point );
graph.vertices.push( newStartVertex );
vertexMap[ edge.startVertex.id ] = newStartVertex;
}
if ( !vertexMap[ edge.endVertex.id ] ) {
const newEndVertex = Vertex.createFromPool( edge.endVertex.point );
const newEndVertex = Vertex.pool.create( edge.endVertex.point );
graph.vertices.push( newEndVertex );
vertexMap[ edge.endVertex.id ] = newEndVertex;
}

const startVertex = vertexMap[ edge.startVertex.id ];
const endVertex = vertexMap[ edge.endVertex.id ];
graph.addEdge( Edge.createFromPool( edge.segment, startVertex, endVertex ) );
graph.addEdge( Edge.pool.create( edge.segment, startVertex, endVertex ) );
}
}

Expand Down Expand Up @@ -735,7 +735,7 @@ class Graph {

let beforeVertex;
if ( aBefore && bBefore ) {
beforeVertex = Vertex.createFromPool( middle.start );
beforeVertex = Vertex.pool.create( middle.start );
this.vertices.push( beforeVertex );
}
else if ( aBefore ) {
Expand All @@ -747,7 +747,7 @@ class Graph {

let afterVertex;
if ( aAfter && bAfter ) {
afterVertex = Vertex.createFromPool( middle.end );
afterVertex = Vertex.pool.create( middle.end );
this.vertices.push( afterVertex );
}
else if ( aAfter ) {
Expand All @@ -757,7 +757,7 @@ class Graph {
afterVertex = aEdge.endVertex;
}

const middleEdge = Edge.createFromPool( middle, beforeVertex, afterVertex );
const middleEdge = Edge.pool.create( middle, beforeVertex, afterVertex );
newEdges.push( middleEdge );

let aBeforeEdge;
Expand All @@ -767,19 +767,19 @@ class Graph {

// Add "leftover" edges
if ( aBefore ) {
aBeforeEdge = Edge.createFromPool( aBefore, aEdge.startVertex, beforeVertex );
aBeforeEdge = Edge.pool.create( aBefore, aEdge.startVertex, beforeVertex );
newEdges.push( aBeforeEdge );
}
if ( aAfter ) {
aAfterEdge = Edge.createFromPool( aAfter, afterVertex, aEdge.endVertex );
aAfterEdge = Edge.pool.create( aAfter, afterVertex, aEdge.endVertex );
newEdges.push( aAfterEdge );
}
if ( bBefore ) {
bBeforeEdge = Edge.createFromPool( bBefore, bEdge.startVertex, overlap.a > 0 ? beforeVertex : afterVertex );
bBeforeEdge = Edge.pool.create( bBefore, bEdge.startVertex, overlap.a > 0 ? beforeVertex : afterVertex );
newEdges.push( bBeforeEdge );
}
if ( bAfter ) {
bAfterEdge = Edge.createFromPool( bAfter, overlap.a > 0 ? afterVertex : beforeVertex, bEdge.endVertex );
bAfterEdge = Edge.pool.create( bAfter, overlap.a > 0 ? afterVertex : beforeVertex, bEdge.endVertex );
newEdges.push( bAfterEdge );
}

Expand Down Expand Up @@ -830,12 +830,12 @@ class Graph {

const segments = segment.subdivisions( [ selfIntersection.aT, selfIntersection.bT ] );

const vertex = Vertex.createFromPool( selfIntersection.point );
const vertex = Vertex.pool.create( selfIntersection.point );
this.vertices.push( vertex );

const startEdge = Edge.createFromPool( segments[ 0 ], edge.startVertex, vertex );
const middleEdge = Edge.createFromPool( segments[ 1 ], vertex, vertex );
const endEdge = Edge.createFromPool( segments[ 2 ], vertex, edge.endVertex );
const startEdge = Edge.pool.create( segments[ 0 ], edge.startVertex, vertex );
const middleEdge = Edge.pool.create( segments[ 1 ], vertex, vertex );
const endEdge = Edge.pool.create( segments[ 2 ], vertex, edge.endVertex );

this.removeEdge( edge );

Expand Down Expand Up @@ -1007,7 +1007,7 @@ class Graph {
vertex = bT < 0.5 ? bEdge.startVertex : bEdge.endVertex;
}
else {
vertex = Vertex.createFromPool( point );
vertex = Vertex.pool.create( point );
this.vertices.push( vertex );
}

Expand Down Expand Up @@ -1048,8 +1048,8 @@ class Graph {
const segments = edge.segment.subdivided( t );
assert && assert( segments.length === 2 );

const firstEdge = Edge.createFromPool( segments[ 0 ], edge.startVertex, vertex );
const secondEdge = Edge.createFromPool( segments[ 1 ], vertex, edge.endVertex );
const firstEdge = Edge.pool.create( segments[ 0 ], edge.startVertex, vertex );
const secondEdge = Edge.pool.create( segments[ 1 ], vertex, edge.endVertex );

// Remove old connections
this.removeEdge( edge );
Expand Down Expand Up @@ -1131,7 +1131,7 @@ class Graph {
const distance = vertex.point.distance( otherVertex.point );
if ( distance < 1e-5 ) {

const newVertex = Vertex.createFromPool( distance === 0 ? vertex.point : vertex.point.average( otherVertex.point ) );
const newVertex = Vertex.pool.create( distance === 0 ? vertex.point : vertex.point.average( otherVertex.point ) );
this.vertices.push( newVertex );

arrayRemove( this.vertices, vertex );
Expand All @@ -1146,7 +1146,7 @@ class Graph {
if ( ( edge.segment.bounds.width > 1e-5 || edge.segment.bounds.height > 1e-5 ) &&
( edge.segment instanceof Cubic || edge.segment instanceof Arc || edge.segment instanceof EllipticalArc ) ) {
// Replace it with a new edge that is from the vertex to itself
const replacementEdge = Edge.createFromPool( edge.segment, newVertex, newVertex );
const replacementEdge = Edge.pool.create( edge.segment, newVertex, newVertex );
this.addEdge( replacementEdge );
this.replaceEdgeInLoops( edge, [ replacementEdge.forwardHalf ] );
}
Expand Down Expand Up @@ -1347,13 +1347,13 @@ class Graph {
break;
}
}
const boundary = Boundary.createFromPool( boundaryHalfEdges );
const boundary = Boundary.pool.create( boundaryHalfEdges );
( boundary.signedArea > 0 ? this.innerBoundaries : this.outerBoundaries ).push( boundary );
this.boundaries.push( boundary );
}

for ( let i = 0; i < this.innerBoundaries.length; i++ ) {
this.faces.push( Face.createFromPool( this.innerBoundaries[ i ] ) );
this.faces.push( Face.pool.create( this.innerBoundaries[ i ] ) );
}
}

Expand Down
14 changes: 10 additions & 4 deletions js/ops/HalfEdge.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import Vector2 from '../../../dot/js/Vector2.js';
import Poolable from '../../../phet-core/js/Poolable.js';
import Pool from '../../../phet-core/js/Pool.js';
import { kite } from '../imports.js';

let globaId = 0;
Expand All @@ -17,7 +17,7 @@ class HalfEdge {
/**
* @public (kite-internal)
*
* NOTE: Use HalfEdge.createFromPool for most usage instead of using the constructor directly.
* NOTE: Use HalfEdge.pool.create for most usage instead of using the constructor directly.
*
* @param {Edge} edge
* @param {boolean} isReversed
Expand Down Expand Up @@ -195,10 +195,16 @@ class HalfEdge {
return this.edge.segment;
}
}

// @public
freeToPool() {
HalfEdge.pool.freeToPool( this );
}

// @public
static pool = new Pool( HalfEdge );
}

kite.register( 'HalfEdge', HalfEdge );

Poolable.mixInto( HalfEdge );

export default HalfEdge;
14 changes: 10 additions & 4 deletions js/ops/Loop.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import cleanArray from '../../../phet-core/js/cleanArray.js';
import Poolable from '../../../phet-core/js/Poolable.js';
import Pool from '../../../phet-core/js/Pool.js';
import { kite, Subpath } from '../imports.js';

let globaId = 0;
Expand All @@ -24,7 +24,7 @@ class Loop {
/**
* @public (kite-internal)
*
* NOTE: Use Loop.createFromPool for most usage instead of using the constructor directly.
* NOTE: Use Loop.pool.create for most usage instead of using the constructor directly.
*
* @param {number} shapeId
* @param {boolean} closed
Expand Down Expand Up @@ -102,10 +102,16 @@ class Loop {
cleanArray( this.halfEdges );
this.freeToPool();
}

// @public
freeToPool() {
Loop.pool.freeToPool( this );
}

// @public
static pool = new Pool( Loop );
}

kite.register( 'Loop', Loop );

Poolable.mixInto( Loop );

export default Loop;
Loading

0 comments on commit ef3220b

Please sign in to comment.