Skip to content

Commit

Permalink
Converting dot Utils usage, see phetsims/dot#4
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanolson committed Feb 14, 2025
1 parent 277788e commit 3d12a8d
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 59 deletions.
4 changes: 2 additions & 2 deletions doc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,9 @@ window.createSceneryDiagram = ( scene, width, height, needsWhiteBackground = fal

// Oops, tricky to get these to match up exactly.
const delta = v2( 108, 10 );
const center = phet.dot.Utils.lineLineIntersection( v2( 10, 10 ), v2( 10, 10 ).plus( delta.perpendicular ), v2( 0, 64 ), v2( 128, 64 ) );
const center = phet.dot.lineLineIntersection( v2( 10, 10 ), v2( 10, 10 ).plus( delta.perpendicular ), v2( 0, 64 ), v2( 128, 64 ) );
const radius = delta.magnitude;
const rightPoint = phet.dot.Utils.lineLineIntersection( center, center.plus( delta ), v2( 128, 0 ), v2( 128, 128 ) );
const rightPoint = phet.dot.lineLineIntersection( center, center.plus( delta ), v2( 128, 0 ), v2( 128, 128 ) );

const radial = new RadialGradient( center.x, center.y, 0, center.x, center.y, radius );
addColorStops( radial );
Expand Down
45 changes: 23 additions & 22 deletions js/cag/ClippableFace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import Bounds2 from '../../../dot/js/Bounds2.js';
import Matrix3 from '../../../dot/js/Matrix3.js';
import Range from '../../../dot/js/Range.js';
import Utils from '../../../dot/js/Utils.js';
import { roundSymmetric } from '../../../dot/js/util/roundSymmetric.js';
import Vector2 from '../../../dot/js/Vector2.js';
import Shape from '../../../kite/js/Shape.js';
import IntentionalAny from '../../../phet-core/js/types/IntentionalAny.js';
Expand All @@ -27,6 +27,7 @@ import { StripeClipping } from '../clip/StripeClipping.js';
import { PolygonBilinear } from '../raster/PolygonBilinear.js';
import { PolygonMitchellNetravali } from '../raster/PolygonMitchellNetravali.js';
import { LinearEdge, SerializedLinearEdge } from './LinearEdge.js';
import { solveQuadraticRootsReal } from '../../../dot/js/util/solveQuadraticRootsReal.js';

// TODO: assertions that all types of ClippableFace give the same results for the same methods

Expand Down Expand Up @@ -838,13 +839,13 @@ export class EdgedFace implements ClippableFace {
const edge = this.edges[ i ];

const startPoint = new Vector2(
Utils.roundSymmetric( edge.startPoint.x / epsilon ) * epsilon,
Utils.roundSymmetric( edge.startPoint.y / epsilon ) * epsilon
roundSymmetric( edge.startPoint.x / epsilon ) * epsilon,
roundSymmetric( edge.startPoint.y / epsilon ) * epsilon
);

const endPoint = new Vector2(
Utils.roundSymmetric( edge.endPoint.x / epsilon ) * epsilon,
Utils.roundSymmetric( edge.endPoint.y / epsilon ) * epsilon
roundSymmetric( edge.endPoint.x / epsilon ) * epsilon,
roundSymmetric( edge.endPoint.y / epsilon ) * epsilon
);

if ( !startPoint.equals( endPoint ) ) {
Expand Down Expand Up @@ -1674,13 +1675,13 @@ export class EdgedClippedFace implements ClippableFace {
const edge = this.edges[ i ];

const startPoint = new Vector2(
Utils.roundSymmetric( edge.startPoint.x / epsilon ) * epsilon,
Utils.roundSymmetric( edge.startPoint.y / epsilon ) * epsilon
roundSymmetric( edge.startPoint.x / epsilon ) * epsilon,
roundSymmetric( edge.startPoint.y / epsilon ) * epsilon
);

const endPoint = new Vector2(
Utils.roundSymmetric( edge.endPoint.x / epsilon ) * epsilon,
Utils.roundSymmetric( edge.endPoint.y / epsilon ) * epsilon
roundSymmetric( edge.endPoint.x / epsilon ) * epsilon,
roundSymmetric( edge.endPoint.y / epsilon ) * epsilon
);

if ( !startPoint.equals( endPoint ) ) {
Expand All @@ -1691,13 +1692,13 @@ export class EdgedClippedFace implements ClippableFace {
// TODO: more code sharing?
this.forEachImplicitEdge( ( startPoint, endPoint ) => {
const roundedStartPoint = new Vector2(
Utils.roundSymmetric( startPoint.x / epsilon ) * epsilon,
Utils.roundSymmetric( startPoint.y / epsilon ) * epsilon
roundSymmetric( startPoint.x / epsilon ) * epsilon,
roundSymmetric( startPoint.y / epsilon ) * epsilon
);

const roundedEndPoint = new Vector2(
Utils.roundSymmetric( endPoint.x / epsilon ) * epsilon,
Utils.roundSymmetric( endPoint.y / epsilon ) * epsilon
roundSymmetric( endPoint.x / epsilon ) * epsilon,
roundSymmetric( endPoint.y / epsilon ) * epsilon
);

if ( !roundedStartPoint.equals( roundedEndPoint ) ) {
Expand All @@ -1707,10 +1708,10 @@ export class EdgedClippedFace implements ClippableFace {

return EdgedClippedFace.fromEdgesWithoutCheck(
edges,
Utils.roundSymmetric( this.minX / epsilon ) * epsilon,
Utils.roundSymmetric( this.minY / epsilon ) * epsilon,
Utils.roundSymmetric( this.maxX / epsilon ) * epsilon,
Utils.roundSymmetric( this.maxY / epsilon ) * epsilon
roundSymmetric( this.minX / epsilon ) * epsilon,
roundSymmetric( this.minY / epsilon ) * epsilon,
roundSymmetric( this.maxX / epsilon ) * epsilon,
roundSymmetric( this.maxY / epsilon ) * epsilon
);
}

Expand Down Expand Up @@ -2440,8 +2441,8 @@ export class PolygonalFace implements ClippableFace {
public getRounded( epsilon: number ): PolygonalFace {
return new PolygonalFace( this.polygons.map( polygon => polygon.map( vertex => {
return new Vector2(
Utils.roundSymmetric( vertex.x / epsilon ) * epsilon,
Utils.roundSymmetric( vertex.y / epsilon ) * epsilon
roundSymmetric( vertex.x / epsilon ) * epsilon,
roundSymmetric( vertex.y / epsilon ) * epsilon
);
} ) ) );
}
Expand Down Expand Up @@ -2773,7 +2774,7 @@ export class CircularClipping {

assert && assert( a > 0, 'We should have a delta, assumed in code below' );

const roots = Utils.solveQuadraticRootsReal( a, b, c );
const roots = solveQuadraticRootsReal( a, b, c );

let isFullyExternal = false;

Expand Down Expand Up @@ -3140,7 +3141,7 @@ export class CircularClipping {

assert && assert( a > 0, 'We should have a delta, assumed in code below' );

const roots = Utils.solveQuadraticRootsReal( a, b, c );
const roots = solveQuadraticRootsReal( a, b, c );

let isFullyExternal = false;

Expand Down Expand Up @@ -3598,7 +3599,7 @@ export class CircularClipping {

assert && assert( a > 0, 'We should have a delta, assumed in code below' );

const roots = Utils.solveQuadraticRootsReal( a, b, c );
const roots = solveQuadraticRootsReal( a, b, c );

let isFullyExternal = false;

Expand Down
10 changes: 5 additions & 5 deletions js/cag/IntegerEdge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import Bounds2 from '../../../dot/js/Bounds2.js';
import Matrix3 from '../../../dot/js/Matrix3.js';
import Utils from '../../../dot/js/Utils.js';
import Vector2 from '../../../dot/js/Vector2.js';
import type { RationalIntersection } from './RationalIntersection.js';
import type { RenderPath } from '../render-program/RenderPath.js';
import type { BoundedSubpath } from './BoundedSubpath.js';
import { BoundsClipping } from '../clip/BoundsClipping.js';
import { alpenglow } from '../alpenglow.js';
import { roundSymmetric } from '../../../dot/js/util/roundSymmetric.js';

export class IntegerEdge {

Expand Down Expand Up @@ -74,10 +74,10 @@ export class IntegerEdge {
const m10 = toIntegerMatrix.m10();
const m11 = toIntegerMatrix.m11();
const m12 = toIntegerMatrix.m12();
const x0 = Utils.roundSymmetric( p0.x * m00 + p0.y * m01 + m02 );
const y0 = Utils.roundSymmetric( p0.x * m10 + p0.y * m11 + m12 );
const x1 = Utils.roundSymmetric( p1.x * m00 + p1.y * m01 + m02 );
const y1 = Utils.roundSymmetric( p1.x * m10 + p1.y * m11 + m12 );
const x0 = roundSymmetric( p0.x * m00 + p0.y * m01 + m02 );
const y0 = roundSymmetric( p0.x * m10 + p0.y * m11 + m12 );
const x1 = roundSymmetric( p1.x * m00 + p1.y * m01 + m02 );
const y1 = roundSymmetric( p1.x * m10 + p1.y * m11 + m12 );
if ( x0 !== x1 || y0 !== y1 ) {
return new IntegerEdge( path, x0, y0, x1, y1 );
}
Expand Down
6 changes: 3 additions & 3 deletions js/cag/LinearEdge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
*/

import Range from '../../../dot/js/Range.js';
import Utils from '../../../dot/js/Utils.js';
import Vector2 from '../../../dot/js/Vector2.js';
import { Line } from '../../../kite/js/segments/Segment.js';
import Shape from '../../../kite/js/Shape.js';
import { alpenglow } from '../alpenglow.js';
import { ClipSimplifier } from '../clip/ClipSimplifier.js';
import { arePointsCollinear } from '../../../dot/js/util/arePointsCollinear.js';

export class LinearEdge {

Expand Down Expand Up @@ -132,8 +132,8 @@ export class LinearEdge {
let overlapped = false;
for ( const outputEdge of outputEdges ) {
// See if the edges are collinear
if ( Utils.arePointsCollinear( edge.startPoint, edge.endPoint, outputEdge.startPoint, epsilon ) &&
Utils.arePointsCollinear( edge.startPoint, edge.endPoint, outputEdge.endPoint, epsilon ) ) {
if ( arePointsCollinear( edge.startPoint, edge.endPoint, outputEdge.startPoint, epsilon ) &&
arePointsCollinear( edge.startPoint, edge.endPoint, outputEdge.endPoint, epsilon ) ) {
const overlaps = Line.getOverlaps( new Line( edge.startPoint, edge.endPoint ), new Line( outputEdge.startPoint, outputEdge.endPoint ), epsilon );

if ( overlaps.length > 0 ) {
Expand Down
8 changes: 4 additions & 4 deletions js/clip/ClipSimplifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import Matrix3 from '../../../dot/js/Matrix3.js';
import Utils from '../../../dot/js/Utils.js';
import Vector2 from '../../../dot/js/Vector2.js';
import { alpenglow } from '../alpenglow.js';
import { arePointsCollinear } from '../../../dot/js/util/arePointsCollinear.js';

const collinearEpsilon = 1e-9;

Expand Down Expand Up @@ -60,7 +60,7 @@ export class ClipSimplifier {
const secondLastPoint = this.points[ this.points.length - 2 ];

if ( this.checkGeneralCollinearity ) {
if ( Utils.arePointsCollinear( new Vector2( x, y ), lastPoint, secondLastPoint, collinearEpsilon ) ) {
if ( arePointsCollinear( new Vector2( x, y ), lastPoint, secondLastPoint, collinearEpsilon ) ) {
lastPoint.x = x;
lastPoint.y = y;
return;
Expand Down Expand Up @@ -141,11 +141,11 @@ export class ClipSimplifier {
const lastPoint = this.points[ this.points.length - 1 ];
const secondLastPoint = this.points[ this.points.length - 2 ];

if ( Utils.arePointsCollinear( secondPoint, firstPoint, lastPoint, collinearEpsilon ) ) {
if ( arePointsCollinear( secondPoint, firstPoint, lastPoint, collinearEpsilon ) ) {
this.points.shift();
changed = true;
}
if ( Utils.arePointsCollinear( firstPoint, lastPoint, secondLastPoint, collinearEpsilon ) ) {
if ( arePointsCollinear( firstPoint, lastPoint, secondLastPoint, collinearEpsilon ) ) {
this.points.pop();
changed = true;
}
Expand Down
18 changes: 9 additions & 9 deletions js/clip/GridClipping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* @author Jonathan Olson <[email protected]>
*/

import Utils from '../../../dot/js/Utils.js';
import { clamp } from '../../../dot/js/util/clamp.js';
import Vector2 from '../../../dot/js/Vector2.js';
import { alpenglow } from '../alpenglow.js';
import { LineClipping } from './LineClipping.js';
import { roundSymmetric } from '../../../dot/js/util/roundSymmetric.js';

const scratchStartPoint = new Vector2( 0, 0 );
const scratchEndPoint = new Vector2( 0, 0 );
Expand Down Expand Up @@ -94,10 +94,10 @@ export class GridClipping {
const maxRawStartStepX = Math.max( rawStartStepX, rawEndStepX );
const maxRawStartStepY = Math.max( rawStartStepY, rawEndStepY );

const roundedMinStepX = Utils.roundSymmetric( minRawStartStepX );
const roundedMinStepY = Utils.roundSymmetric( minRawStartStepY );
const roundedMaxStepX = Utils.roundSymmetric( maxRawStartStepX );
const roundedMaxStepY = Utils.roundSymmetric( maxRawStartStepY );
const roundedMinStepX = roundSymmetric( minRawStartStepX );
const roundedMinStepY = roundSymmetric( minRawStartStepY );
const roundedMaxStepX = roundSymmetric( maxRawStartStepX );
const roundedMaxStepY = roundSymmetric( maxRawStartStepY );

// Integral "step" coordinates - with slight perturbation to expand our region to cover points/lines that lie
// exactly on our grid lines (but not outside of our bounds)
Expand Down Expand Up @@ -516,10 +516,10 @@ export class GridClipping {
const maxRawStartStepX = Math.max( rawStartStepX, rawEndStepX );
const maxRawStartStepY = Math.max( rawStartStepY, rawEndStepY );

const roundedMinStepX = Utils.roundSymmetric( minRawStartStepX );
const roundedMinStepY = Utils.roundSymmetric( minRawStartStepY );
const roundedMaxStepX = Utils.roundSymmetric( maxRawStartStepX );
const roundedMaxStepY = Utils.roundSymmetric( maxRawStartStepY );
const roundedMinStepX = roundSymmetric( minRawStartStepX );
const roundedMinStepY = roundSymmetric( minRawStartStepY );
const roundedMaxStepX = roundSymmetric( maxRawStartStepX );
const roundedMaxStepY = roundSymmetric( maxRawStartStepY );

// Integral "step" coordinates - with slight perturbation to expand our region to cover points/lines that lie
// exactly on our grid lines (but not outside of our bounds)
Expand Down
8 changes: 4 additions & 4 deletions js/render-program/RenderImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import Matrix3 from '../../../dot/js/Matrix3.js';
import Utils from '../../../dot/js/Utils.js';
import Vector2 from '../../../dot/js/Vector2.js';
import Vector4 from '../../../dot/js/Vector4.js';
import { alpenglow } from '../alpenglow.js';
Expand All @@ -23,6 +22,7 @@ import type { RenderExecutionStack } from './RenderExecutionStack.js';
import type { RenderExecutor } from './RenderExecutor.js';
import type { ByteEncoder } from '../webgpu/compute/ByteEncoder.js';
import { clamp } from '../../../dot/js/util/clamp.js';
import { roundSymmetric } from '../../../dot/js/util/roundSymmetric.js';

const emptyChildren: RenderProgram[] = [];

Expand Down Expand Up @@ -270,7 +270,7 @@ export class RenderImage extends RenderProgram {
case RenderExtend.Repeat:
return t - Math.floor( t );
case RenderExtend.Reflect:
return Math.abs( t - 2.0 * Utils.roundSymmetric( 0.5 * t ) );
return Math.abs( t - 2.0 * roundSymmetric( 0.5 * t ) );
// return ( Math.floor( t ) % 2 === 0 ? t : 1 - t ) - Math.floor( t );
default:
throw new Error( 'Unknown RenderExtend' );
Expand Down Expand Up @@ -394,8 +394,8 @@ export class RenderImageLogic {
switch( this.resampleType ) {
case RenderResampleType.NearestNeighbor: {
const localPoint = this.inverseTransformWithHalfOffset.timesVector2( context.centroid );
const roundedX = Utils.roundSymmetric( localPoint.x );
const roundedY = Utils.roundSymmetric( localPoint.y );
const roundedX = roundSymmetric( localPoint.x );
const roundedY = roundSymmetric( localPoint.y );
const x = RenderImage.extendInteger( roundedX, this.image.width, this.extendX );
const y = RenderImage.extendInteger( roundedY, this.image.height, this.extendY );

Expand Down
4 changes: 2 additions & 2 deletions js/webgpu/PerformanceTesting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* @author Jonathan Olson <[email protected]>
*/

import Utils from '../../../dot/js/Utils.js';
import { alpenglow } from '../alpenglow.js';
import { DeviceContext } from './compute/DeviceContext.js';
import { getArrayType, U32Order } from './compute/ConcreteType.js';
Expand All @@ -15,6 +14,7 @@ import { RadixSortModule } from './modules/gpu/RadixSortModule.js';
import { u32S } from './wgsl/WGSLString.js';
import { Routine } from './compute/Routine.js';
import { Procedure } from './compute/Procedure.js';
import { toFixed } from '../../../dot/js/util/toFixed.js';

export class PerformanceTesting {
public static async loopRadixSortTest(
Expand Down Expand Up @@ -96,7 +96,7 @@ export class PerformanceTesting {
const elapsed = now - startTime;
startTime = now;
elapsedTimes.push( elapsed );
console.log( Utils.toFixed( elapsed, 0 ), elapsedTimes.length > 1 ? Utils.toFixed( _.sum( elapsedTimes.slice( 1 ) ) / elapsedTimes.slice( 1 ).length, 0 ) : 0 );
console.log( toFixed( elapsed, 0 ), elapsedTimes.length > 1 ? toFixed( _.sum( elapsedTimes.slice( 1 ) ) / elapsedTimes.slice( 1 ).length, 0 ) : 0 );
}

// TODO: maybe avoid the await on the first frame?
Expand Down
4 changes: 2 additions & 2 deletions js/webgpu/compute/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* @author Jonathan Olson <[email protected]>
*/

import Utils from '../../../../dot/js/Utils.js';
import { optionize3 } from '../../../../phet-core/js/optionize.js';
import { alpenglow } from '../../alpenglow.js';
import type { DeviceContext } from './DeviceContext.js';
Expand All @@ -15,6 +14,7 @@ import { ComputePass } from './ComputePass.js';
import type { TypedBuffer } from './TypedBuffer.js';
import { webgpu } from '../WebGPUAPI.js';
import { ConsoleLogger } from './ConsoleLogger.js';
import { roundSymmetric } from '../../../../dot/js/util/roundSymmetric.js';

export type ExecutorOptions = {
getTimestampWrites?: ( name: string ) => GPUComputePassTimestampWrites | null;
Expand Down Expand Up @@ -137,7 +137,7 @@ export class Executor {
if ( logResult ) {
const data = new Uint32Array( logResult );
const length = data[ 0 ];
const usedMessage = `logging used ${length} of ${data.length - 1} u32s (${Utils.roundSymmetric( 100 * length / ( data.length - 1 ) )}%)`;
const usedMessage = `logging used ${length} of ${data.length - 1} u32s (${roundSymmetric( 100 * length / ( data.length - 1 ) )}%)`;
console.log( usedMessage );

const logData = ConsoleLogger.analyze( logResult );
Expand Down
4 changes: 2 additions & 2 deletions js/webgpu/compute/TimestampLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* @author Jonathan Olson <[email protected]>
*/

import Utils from '../../../../dot/js/Utils.js';
import { alpenglow } from '../../alpenglow.js';
import type { DeviceContext } from './DeviceContext.js';
import { webgpu } from '../WebGPUAPI.js';
import type { BufferLogger } from './BufferLogger.js';
import { ByteEncoder } from './ByteEncoder.js';
import { roundSymmetric } from '../../../../dot/js/util/roundSymmetric.js';

export class TimestampLogger {

Expand Down Expand Up @@ -107,7 +107,7 @@ export class TimestampLoggerResult {
public toString(): string {
const numToTimestamp = ( n: number ): string => {
let result = '';
let digits = '' + Utils.roundSymmetric( n );
let digits = '' + roundSymmetric( n );

while ( digits.length ) {
if ( digits.length > 3 ) {
Expand Down
Loading

0 comments on commit 3d12a8d

Please sign in to comment.