Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add randomDirection and random methods to Vector3 and Quaternion #22494

Merged
merged 5 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/api/en/math/Quaternion.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ <h3>[method:Quaternion multiplyQuaternions]( [param:Quaternion a], [param:Quater
<h3>[method:Quaternion premultiply]( [param:Quaternion q] )</h3>
<p>Pre-multiplies this quaternion by [page:Quaternion q].</p>

<h3>[method:Quaternion random]()</h3>
<p>
Sets this quaternion to a uniformly random, normalized quaternion.
</p>

<h3>[method:Quaternion rotateTowards]( [param:Quaternion q], [param:Float step] )</h3>
<p>
[page:Quaternion q] - The target quaternion.<br />
Expand Down
5 changes: 5 additions & 0 deletions docs/api/en/math/Vector3.html
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ <h3>[method:this random]()</h3>
Sets each component of this vector to a pseudo-random value between 0 and 1, excluding 1.
</p>

<h3>[method:this randomDirection]()</h3>
<p>
Sets this vector to a uniformly random point on a unit sphere.
</p>

<h2>Source</h2>

<p>
Expand Down
21 changes: 21 additions & 0 deletions src/math/Quaternion.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,27 @@ class Quaternion {

}

random() {

// Derived from http://planning.cs.uiuc.edu/node198.html

const u1 = Math.random();
const sqrt1u1 = Math.sqrt( 1 - u1 );
const sqrtu1 = Math.sqrt( u1 );

const u2 = 2 * Math.PI * Math.random();

const u3 = 2 * Math.PI * Math.random();

return this.set(
sqrt1u1 * Math.sin( u2 ),
sqrt1u1 * Math.cos( u2 ),
sqrtu1 * Math.sin( u3 ),
sqrtu1 * Math.cos( u3 ),
);

}

equals( quaternion ) {

return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
Expand Down
16 changes: 16 additions & 0 deletions src/math/Vector3.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,22 @@ class Vector3 {

}

randomDirection() {

// Derived from https://mathworld.wolfram.com/SpherePointPicking.html

const u = ( Math.random() - 0.5 ) * 2;
const t = Math.random() * Math.PI * 2;
const f = Math.sqrt( 1 - u ** 2 );

this.x = f * Math.cos( t );
this.y = f * Math.sin( t );
this.z = u;

return this;

}
mrdoob marked this conversation as resolved.
Show resolved Hide resolved

}

Vector3.prototype.isVector3 = true;
Expand Down
17 changes: 17 additions & 0 deletions test/unit/src/math/Quaternion.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,23 @@ export default QUnit.module( 'Maths', () => {

} );

QUnit.test( "random", ( assert ) => {

var a = new Quaternion();

a.random();

var identity = new Quaternion();
assert.notDeepEqual(
a,
identity,
'randomizes at least one component of the quaternion'
);

assert.ok( ( 1 - a.length() ) <= Number.EPSILON, 'produces a normalized quaternion' );

} );

QUnit.test( "equals", ( assert ) => {

var a = new Quaternion( x, y, z, w );
Expand Down
17 changes: 17 additions & 0 deletions test/unit/src/math/Vector3.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,23 @@ export default QUnit.module( 'Maths', () => {

} );

QUnit.test( 'randomDirection', ( assert ) => {

var vec = new Vector3();

vec.randomDirection();

var zero = new Vector3();
assert.notDeepEqual(
vec,
zero,
'randomizes at least one component of the vector'
);

assert.ok( ( 1 - vec.length() ) <= Number.EPSILON, 'produces a unit vector' );

} );

} );

} );