|
@@ -591,23 +591,24 @@ class Quaternion {
|
|
|
|
|
|
random() {
|
|
|
|
|
|
- // Derived from http://planning.cs.uiuc.edu/node198.html
|
|
|
- // Note, this source uses w, x, y, z ordering,
|
|
|
- // so we swap the order below.
|
|
|
+ // sets this quaternion to a uniform random unit quaternnion
|
|
|
|
|
|
- const u1 = Math.random();
|
|
|
- const sqrt1u1 = Math.sqrt( 1 - u1 );
|
|
|
- const sqrtu1 = Math.sqrt( u1 );
|
|
|
+ // Ken Shoemake
|
|
|
+ // Uniform random rotations
|
|
|
+ // D. Kirk, editor, Graphics Gems III, pages 124-132. Academic Press, New York, 1992.
|
|
|
|
|
|
- const u2 = 2 * Math.PI * Math.random();
|
|
|
+ const theta1 = 2 * Math.PI * Math.random();
|
|
|
+ const theta2 = 2 * Math.PI * Math.random();
|
|
|
|
|
|
- const u3 = 2 * Math.PI * Math.random();
|
|
|
+ const x0 = Math.random();
|
|
|
+ const r1 = Math.sqrt( 1 - x0 );
|
|
|
+ const r2 = Math.sqrt( x0 );
|
|
|
|
|
|
return this.set(
|
|
|
- sqrt1u1 * Math.cos( u2 ),
|
|
|
- sqrtu1 * Math.sin( u3 ),
|
|
|
- sqrtu1 * Math.cos( u3 ),
|
|
|
- sqrt1u1 * Math.sin( u2 ),
|
|
|
+ r1 * Math.sin( theta1 ),
|
|
|
+ r1 * Math.cos( theta1 ),
|
|
|
+ r2 * Math.sin( theta2 ),
|
|
|
+ r2 * Math.cos( theta2 ),
|
|
|
);
|
|
|
|
|
|
}
|