33
44const fs = require ( 'fs' ) ;
55const compactStringify = require ( 'json-stringify-pretty-compact' ) ;
6- const { Composite, Constraint } = require ( '../src/module/main' ) ;
76
87const comparePath = './test/__compare__' ;
98const compareCommand = 'open http://localhost:8000/?compare' ;
109const diffSaveCommand = 'npm run test-save' ;
1110const diffCommand = 'code -n -d test/__compare__/examples-build.json test/__compare__/examples-dev.json' ;
1211const equalityThreshold = 0.99999 ;
1312
14- const intrinsicProps = [
15- // Common
16- 'id' , 'label' ,
17-
18- // Constraint
19- 'angularStiffness' , 'bodyA' , 'bodyB' , 'damping' , 'length' , 'stiffness' ,
20-
21- // Body
22- 'area' , 'axes' , 'collisionFilter' , 'category' , 'mask' ,
23- 'group' , 'density' , 'friction' , 'frictionAir' , 'frictionStatic' , 'inertia' , 'inverseInertia' , 'inverseMass' , 'isSensor' ,
24- 'isSleeping' , 'isStatic' , 'mass' , 'parent' , 'parts' , 'restitution' , 'sleepThreshold' , 'slop' ,
25- 'timeScale' , 'vertices' ,
26-
27- // Composite
28- 'bodies' , 'constraints' , 'composites'
29- ] ;
30-
3113const colors = { Red : 31 , Green : 32 , Yellow : 33 , White : 37 , BrightWhite : 90 , BrightCyan : 36 } ;
3214const color = ( text , number ) => number ? `\x1b[${ number } m${ text } \x1b[0m` : text ;
33- const limit = ( val , precision = 3 ) => parseFloat ( val . toPrecision ( precision ) ) ;
3415const toPercent = val => ( 100 * val ) . toFixed ( 3 ) ;
3516const toPercentRound = val => Math . round ( 100 * val ) ;
3617
@@ -47,90 +28,6 @@ const noiseThreshold = (val, threshold) => {
4728 return sign * Math . max ( 0 , magnitude - threshold ) / ( 1 - threshold ) ;
4829} ;
4930
50- const engineCapture = ( engine ) => ( {
51- timestamp : limit ( engine . timing . timestamp ) ,
52- extrinsic : worldCaptureExtrinsic ( engine . world ) ,
53- intrinsic : worldCaptureIntrinsic ( engine . world )
54- } ) ;
55-
56- const worldCaptureExtrinsic = world => ( {
57- bodies : Composite . allBodies ( world ) . reduce ( ( bodies , body ) => {
58- bodies [ body . id ] = [
59- body . position . x ,
60- body . position . y ,
61- body . positionPrev . x ,
62- body . positionPrev . y ,
63- body . angle ,
64- body . anglePrev ,
65- ...body . vertices . reduce ( ( flat , vertex ) => ( flat . push ( vertex . x , vertex . y ) , flat ) , [ ] )
66- ] ;
67-
68- return bodies ;
69- } , { } ) ,
70- constraints : Composite . allConstraints ( world ) . reduce ( ( constraints , constraint ) => {
71- const positionA = Constraint . pointAWorld ( constraint ) ;
72- const positionB = Constraint . pointBWorld ( constraint ) ;
73-
74- constraints [ constraint . id ] = [
75- positionA . x ,
76- positionA . y ,
77- positionB . x ,
78- positionB . y
79- ] ;
80-
81- return constraints ;
82- } , { } )
83- } ) ;
84-
85- const worldCaptureIntrinsic = world => worldCaptureIntrinsicBase ( {
86- bodies : Composite . allBodies ( world ) . reduce ( ( bodies , body ) => {
87- bodies [ body . id ] = body ;
88- return bodies ;
89- } , { } ) ,
90- constraints : Composite . allConstraints ( world ) . reduce ( ( constraints , constraint ) => {
91- constraints [ constraint . id ] = constraint ;
92- return constraints ;
93- } , { } ) ,
94- composites : Composite . allComposites ( world ) . reduce ( ( composites , composite ) => {
95- composites [ composite . id ] = {
96- bodies : Composite . allBodies ( composite ) . map ( body => body . id ) ,
97- constraints : Composite . allConstraints ( composite ) . map ( constraint => constraint . id ) ,
98- composites : Composite . allComposites ( composite ) . map ( composite => composite . id )
99- } ;
100- return composites ;
101- } , { } )
102- } ) ;
103-
104- const worldCaptureIntrinsicBase = ( obj , depth = 0 ) => {
105- if ( obj === Infinity ) {
106- return 'Infinity' ;
107- } else if ( typeof obj === 'number' ) {
108- return limit ( obj ) ;
109- } else if ( Array . isArray ( obj ) ) {
110- return obj . map ( item => worldCaptureIntrinsicBase ( item , depth + 1 ) ) ;
111- } else if ( typeof obj !== 'object' ) {
112- return obj ;
113- }
114-
115- const result = Object . entries ( obj )
116- . filter ( ( [ key ] ) => depth <= 1 || intrinsicProps . includes ( key ) )
117- . reduce ( ( cleaned , [ key , val ] ) => {
118- if ( val && val . id && String ( val . id ) !== key ) {
119- val = val . id ;
120- }
121-
122- if ( Array . isArray ( val ) && ! [ 'composites' , 'constraints' , 'bodies' ] . includes ( key ) ) {
123- val = `[${ val . length } ]` ;
124- }
125-
126- cleaned [ key ] = worldCaptureIntrinsicBase ( val , depth + 1 ) ;
127- return cleaned ;
128- } , { } ) ;
129-
130- return Object . keys ( result ) . sort ( )
131- . reduce ( ( sorted , key ) => ( sorted [ key ] = result [ key ] , sorted ) , { } ) ;
132- } ;
133-
13431const similarity = ( a , b ) => {
13532 const distance = Math . sqrt ( a . reduce (
13633 ( sum , _val , i ) => sum + Math . pow ( ( a [ i ] || 0 ) - ( b [ i ] || 0 ) , 2 ) , 0 )
@@ -325,6 +222,6 @@ const comparisonReport = (capturesDev, capturesBuild, devSize, buildSize, buildV
325222} ;
326223
327224module . exports = {
328- requireUncached, engineCapture , comparisonReport, logReport,
225+ requireUncached, comparisonReport, logReport,
329226 toMatchExtrinsics, toMatchIntrinsics
330227} ;
0 commit comments