Skip to content

Commit ceb6671

Browse files
committed
initial adoption of closures in the math library. all unit tests pass.
1 parent d1fafc7 commit ceb6671

File tree

9 files changed

+438
-319
lines changed

9 files changed

+438
-319
lines changed

src/math/Box2.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,21 @@ THREE.Box2.prototype = {
6767

6868
},
6969

70-
setFromCenterAndSize: function ( center, size ) {
70+
setFromCenterAndSize: function() {
7171

72-
var halfSize = THREE.Box2.__v1.copy( size ).multiplyScalar( 0.5 );
73-
this.min.copy( center ).sub( halfSize );
74-
this.max.copy( center ).add( halfSize );
72+
var v1 = new THREE.Vector2();
73+
74+
return function ( center, size ) {
7575

76-
return this;
76+
var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
77+
this.min.copy( center ).sub( halfSize );
78+
this.max.copy( center ).add( halfSize );
7779

78-
},
80+
return this;
81+
82+
};
83+
84+
}(),
7985

8086
copy: function ( box ) {
8187

@@ -201,12 +207,18 @@ THREE.Box2.prototype = {
201207

202208
},
203209

204-
distanceToPoint: function ( point ) {
210+
distanceToPoint: function() {
205211

206-
var clampedPoint = THREE.Box2.__v1.copy( point ).clamp( this.min, this.max );
207-
return clampedPoint.sub( point ).length();
212+
var v1 = new THREE.Vector2();
213+
214+
return function ( point ) {
208215

209-
},
216+
var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
217+
return clampedPoint.sub( point ).length();
218+
219+
};
220+
221+
}(),
210222

211223
intersect: function ( box ) {
212224

@@ -247,6 +259,4 @@ THREE.Box2.prototype = {
247259

248260
}
249261

250-
};
251-
252-
THREE.Box2.__v1 = new THREE.Vector2();
262+
};

src/math/Box3.js

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,22 @@ THREE.Box3.prototype = {
7777

7878
},
7979

80-
setFromCenterAndSize: function ( center, size ) {
80+
setFromCenterAndSize: function() {
8181

82-
var halfSize = THREE.Box3.__v1.copy( size ).multiplyScalar( 0.5 );
82+
var v1 = new THREE.Vector3();
83+
84+
return function ( center, size ) {
8385

84-
this.min.copy( center ).sub( halfSize );
85-
this.max.copy( center ).add( halfSize );
86+
var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
8687

87-
return this;
88+
this.min.copy( center ).sub( halfSize );
89+
this.max.copy( center ).add( halfSize );
8890

89-
},
91+
return this;
92+
93+
};
94+
95+
}(),
9096

9197
copy: function ( box ) {
9298

@@ -215,27 +221,39 @@ THREE.Box3.prototype = {
215221
clampPoint: function ( point, optionalTarget ) {
216222

217223
var result = optionalTarget || new THREE.Vector3();
218-
return new THREE.Vector3().copy( point ).clamp( this.min, this.max );
224+
return result.copy( point ).clamp( this.min, this.max );
219225

220226
},
221227

222-
distanceToPoint: function ( point ) {
228+
distanceToPoint: function() {
223229

224-
var clampedPoint = THREE.Box3.__v1.copy( point ).clamp( this.min, this.max );
225-
return clampedPoint.sub( point ).length();
230+
var v1 = new THREE.Vector3();
231+
232+
return function ( point ) {
226233

227-
},
234+
var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
235+
return clampedPoint.sub( point ).length();
228236

229-
getBoundingSphere: function ( optionalTarget ) {
237+
};
230238

231-
var result = optionalTarget || new THREE.Sphere();
239+
}(),
232240

233-
result.center = this.center();
234-
result.radius = this.size( THREE.Box3.__v0 ).length() * 0.5;
241+
getBoundingSphere: function() {
235242

236-
return result;
243+
var v1 = new THREE.Vector3();
244+
245+
return function ( optionalTarget ) {
237246

238-
},
247+
var result = optionalTarget || new THREE.Sphere();
248+
249+
result.center = this.center();
250+
result.radius = this.size( v1 ).length() * 0.5;
251+
252+
return result;
253+
254+
};
255+
256+
}(),
239257

240258
intersect: function ( box ) {
241259

@@ -255,27 +273,39 @@ THREE.Box3.prototype = {
255273

256274
},
257275

258-
transform: function ( matrix ) {
259-
260-
// NOTE: I am using a binary pattern to specify all 2^3 combinations below
261-
var newPoints = [
262-
THREE.Box3.__v0.set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ),
263-
THREE.Box3.__v0.set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ), // 000
264-
THREE.Box3.__v1.set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ), // 001
265-
THREE.Box3.__v2.set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ), // 010
266-
THREE.Box3.__v3.set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ), // 011
267-
THREE.Box3.__v4.set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ), // 100
268-
THREE.Box3.__v5.set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ), // 101
269-
THREE.Box3.__v6.set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ), // 110
270-
THREE.Box3.__v7.set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ) // 111
271-
];
276+
transform: function() {
277+
278+
var points = [
279+
new THREE.Vector3(),
280+
new THREE.Vector3(),
281+
new THREE.Vector3(),
282+
new THREE.Vector3(),
283+
new THREE.Vector3(),
284+
new THREE.Vector3(),
285+
new THREE.Vector3(),
286+
new THREE.Vector3()
287+
];
288+
289+
return function ( matrix ) {
290+
291+
// NOTE: I am using a binary pattern to specify all 2^3 combinations below
292+
points[0].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
293+
points[1].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
294+
points[2].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
295+
points[3].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
296+
points[4].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
297+
points[5].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
298+
points[6].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
299+
points[7].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
300+
301+
this.makeEmpty();
302+
this.setFromPoints( points );
272303

273-
this.makeEmpty();
274-
this.setFromPoints( newPoints );
304+
return this;
275305

276-
return this;
306+
};
277307

278-
},
308+
}(),
279309

280310
translate: function ( offset ) {
281311

@@ -298,13 +328,4 @@ THREE.Box3.prototype = {
298328

299329
}
300330

301-
};
302-
303-
THREE.Box3.__v0 = new THREE.Vector3();
304-
THREE.Box3.__v1 = new THREE.Vector3();
305-
THREE.Box3.__v2 = new THREE.Vector3();
306-
THREE.Box3.__v3 = new THREE.Vector3();
307-
THREE.Box3.__v4 = new THREE.Vector3();
308-
THREE.Box3.__v5 = new THREE.Vector3();
309-
THREE.Box3.__v6 = new THREE.Vector3();
310-
THREE.Box3.__v7 = new THREE.Vector3();
331+
};

src/math/Matrix3.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,31 @@ THREE.Matrix3.prototype = {
7070

7171
},
7272

73-
multiplyVector3Array: function ( a ) {
73+
multiplyVector3Array: function() {
7474

75-
var tmp = THREE.Matrix3.__v1;
75+
var v1 = new THREE.Vector3();
76+
77+
return function ( a ) {
7678

77-
for ( var i = 0, il = a.length; i < il; i += 3 ) {
79+
for ( var i = 0, il = a.length; i < il; i += 3 ) {
7880

79-
tmp.x = a[ i ];
80-
tmp.y = a[ i + 1 ];
81-
tmp.z = a[ i + 2 ];
81+
v1.x = a[ i ];
82+
v1.y = a[ i + 1 ];
83+
v1.z = a[ i + 2 ];
8284

83-
tmp.applyMatrix3(this);
85+
v1.applyMatrix3(this);
8486

85-
a[ i ] = tmp.x;
86-
a[ i + 1 ] = tmp.y;
87-
a[ i + 2 ] = tmp.z;
87+
a[ i ] = v1.x;
88+
a[ i + 1 ] = v1.y;
89+
a[ i + 2 ] = v1.z;
8890

89-
}
91+
}
9092

91-
return a;
93+
return a;
9294

93-
},
95+
};
96+
97+
}(),
9498

9599
multiplyScalar: function ( s ) {
96100

@@ -210,6 +214,4 @@ THREE.Matrix3.prototype = {
210214

211215
}
212216

213-
};
214-
215-
THREE.Matrix3.__v1 = new THREE.Vector3();
217+
};

0 commit comments

Comments
 (0)