theta = fRand( 2*M_PI ); tMin = cos( randomAngle * M_PI_180 ); - randomAngle is the "threshold angle" t = fRand( tMin, 1 ); phi = acos( t ); sinPhi = sin( phi ); return Vector3( sinPhi * cos( theta ), sinPhi * sin( theta ), cos( phi ) );Also, it uses a lot of trig functions. Is there any more efficient ways of doing this that work with any vector? Thanks.

**0**

# Generating random points uniformly distributed over a section of a sphere

Started by Gumgo, Jan 06 2010 06:28 PM

6 replies to this topic

###
#1
Members - Reputation: **964**

Posted 06 January 2010 - 06:28 PM

I need some way of generating points on the surface of a sphere (actually, vectors, but if I have points on the surface I have vectors) that are uniformly distributed on a "circular section" of the sphere's surface. This diagram should explain what I mean:
The diagram is in 2d, but the same applies in 3d. The blue line is the vector given. Theta is the "threshold angle" - the randomly generated vectors are allowed to form an angle of theta or less with the given vector. The red part is the surface of the sphere (in the image, the arc of the circle) that the points are allowed to be on, and must be uniformly distributed on that area.
I have one current solution, but it only works for the vector (0,0,1):

###
#2
Moderators - Reputation: **492**

Posted 06 January 2010 - 08:01 PM

Gumgo,

I'm not sure what you mean by uniformly distributed, but I'm assuming you mean that the point on the surface of the sphere that is randomly generated is the same distance in longitude as it is latitude away from the starting vector.

In either event, I think the best way to go about solving this is to use spherical coordinates.

Spherical coordinates represent a point on a sphere by the ordered pair:

(r, theta, phi)

Where r is the radius, theta is the angle on the YZ-plane (latitude), and phi is the angle on the XY-plane (longitude). To solve this you only need to convert from cartesian coordinates to spherical, add in your random angle, and then convert back into cartesian coordinates.

The equations to convert from cartesian to spherical as follows:

r = sqrt( x^2 + y^2 + z^2)

theta = arccos( z / r )

phi = atan2(y,x)

Now, it's relatively easy to normalize your vectors first, however as you can see above it's not necessary, as the calculations are part of the conversion. But, in the event you're working with a unit circle, r = 1, which means theta is just the arccos(z).

So simplified:

r = 1

theta = arccos( z )

phi = atan2( y, x )

This gives you theta and phi, which is the position of your existing vector in spherical coordinates. Now, compute your random angle with something like

angle = random(-threshold, threshold)

This'll give you a random angle that lies in between the threshold. Now, just add the random angle to your existing spherical coordinates.

theta1 = theta + angle

phi1 = phi + angle

Now convert back to cartesian coordinates using the following:

x = r * sin(theta1) * cos(phi1)

y = r * sin(theta1) * sin(phi1)

z = r * cos(theta1)

As you can see, this is again greatly simplified if you're using a unit circle, as r = 1, however either way it works, and you can cut down on the number of computations by caching intermediate calculations. So the total method would look something like.

float angle = fRand(-threshold, threshold);

float r = sqrt( x^2 + y^2 + z^2);

float theta = acos( z / r ) + angle;

float phi = atan2(y, x) + angle;

float rSinTheta = r * sin(theta);

x = rSintheta * cos(phi);

y = rSinTheta * sin(phi);

z = r * cos(theta);

This requires:

1 square root

1 acos

1 atan2

2 cos

2 sin

If you're using a unit circle (r=1), the above is reduced to:

float angle = fRand(-threshold, threshold);

float theta = acos( z ) + angle;

float phi = atan2(y, x) + angle;

float sinTheta = sin(theta);

x = sinTheta * cos(phi);

y = sinTheta * sin(phi);

z = cos(theta);

Either way this method uses 2x cos, 2x sin, 1x acos, and 1x atan2.

Well, I hope this has been helpful. It's not really any more efficient than your method, but it's straight forward, easy to read, and should work on all 3 axis, regardless of the starting vector.

Cheers and Good Luck!

I'm not sure what you mean by uniformly distributed, but I'm assuming you mean that the point on the surface of the sphere that is randomly generated is the same distance in longitude as it is latitude away from the starting vector.

In either event, I think the best way to go about solving this is to use spherical coordinates.

Spherical coordinates represent a point on a sphere by the ordered pair:

(r, theta, phi)

Where r is the radius, theta is the angle on the YZ-plane (latitude), and phi is the angle on the XY-plane (longitude). To solve this you only need to convert from cartesian coordinates to spherical, add in your random angle, and then convert back into cartesian coordinates.

The equations to convert from cartesian to spherical as follows:

r = sqrt( x^2 + y^2 + z^2)

theta = arccos( z / r )

phi = atan2(y,x)

Now, it's relatively easy to normalize your vectors first, however as you can see above it's not necessary, as the calculations are part of the conversion. But, in the event you're working with a unit circle, r = 1, which means theta is just the arccos(z).

So simplified:

r = 1

theta = arccos( z )

phi = atan2( y, x )

This gives you theta and phi, which is the position of your existing vector in spherical coordinates. Now, compute your random angle with something like

angle = random(-threshold, threshold)

This'll give you a random angle that lies in between the threshold. Now, just add the random angle to your existing spherical coordinates.

theta1 = theta + angle

phi1 = phi + angle

Now convert back to cartesian coordinates using the following:

x = r * sin(theta1) * cos(phi1)

y = r * sin(theta1) * sin(phi1)

z = r * cos(theta1)

As you can see, this is again greatly simplified if you're using a unit circle, as r = 1, however either way it works, and you can cut down on the number of computations by caching intermediate calculations. So the total method would look something like.

float angle = fRand(-threshold, threshold);

float r = sqrt( x^2 + y^2 + z^2);

float theta = acos( z / r ) + angle;

float phi = atan2(y, x) + angle;

float rSinTheta = r * sin(theta);

x = rSintheta * cos(phi);

y = rSinTheta * sin(phi);

z = r * cos(theta);

This requires:

1 square root

1 acos

1 atan2

2 cos

2 sin

If you're using a unit circle (r=1), the above is reduced to:

float angle = fRand(-threshold, threshold);

float theta = acos( z ) + angle;

float phi = atan2(y, x) + angle;

float sinTheta = sin(theta);

x = sinTheta * cos(phi);

y = sinTheta * sin(phi);

z = cos(theta);

Either way this method uses 2x cos, 2x sin, 1x acos, and 1x atan2.

Well, I hope this has been helpful. It's not really any more efficient than your method, but it's straight forward, easy to read, and should work on all 3 axis, regardless of the starting vector.

Cheers and Good Luck!

**Jeromy Walsh**

Sr. Tools & Engine Programmer | Software Engineer

Microsoft Windows Phone Team

Chronicles of Elyria (An In-development MMORPG)

GameDevelopedia.com - Blog & Tutorials

GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop

"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints

###
#4
Crossbones+ - Reputation: **18614**

Posted 07 January 2010 - 01:54 AM

Quote:

Original post by Gumgo

[...]

I have one current solution, but it only works for the vector (0,0,1):

theta = fRand( 2*M_PI );

tMin = cos( randomAngle * M_PI_180 ); - randomAngle is the "threshold angle"

t = fRand( tMin, 1 );

phi = acos( t );

sinPhi = sin( phi );

return Vector3( sinPhi * cos( theta ), sinPhi * sin( theta ), cos( phi ) );

I don't think that distribution is uniform. [EDIT: Oh, actually it is. See Erik Rufelt's pseudo-code below for a version with fewer trigonometric operations.]

Quote:

Also, it uses a lot of trig functions. Is there any more efficient ways of doing this that work with any vector? Thanks.

I would concentrate on getting it right for (0,0,1) first. You can then apply a rotation to the result.

[Edited by - alvaro on January 7, 2010 11:54:36 AM]

###
#5
Members - Reputation: **158**

Posted 07 January 2010 - 03:11 AM

As you said, you can think of each point as a vector from the centre of the sphere to the edge. Each random point on the area on the surface can be specified by an angle made with the centre vector (the vector from the centre of the sphere to the centre of the circular area on the surtface), and a rotation around the centre point on the surface of the sphere.

If you just use a random value for each of these angles, the distibution is not uniform, as you'll tend to get more points towards the centre. The second value, the rotation around the centre vector can be an ordinary random value, but for the first angle - the angle made with the centre vector, you need to make the probability of picking a given value decrease proportionally to the circular area that angle describes on the surface.

For small angles, the area on the surface can be approximated to a flat circle, so the probability should just increase proportional to 1/(sqrt)r, where r is the distance from the centre point on the surface. If the angle is larger, then you'll have to take into account the curved surface - the formula for a circular area on a curved surface I don't know off the top of my head, but it shouldn't be too hard to find.

[Edited by - BattleMetalChris on January 7, 2010 9:11:34 AM]

If you just use a random value for each of these angles, the distibution is not uniform, as you'll tend to get more points towards the centre. The second value, the rotation around the centre vector can be an ordinary random value, but for the first angle - the angle made with the centre vector, you need to make the probability of picking a given value decrease proportionally to the circular area that angle describes on the surface.

For small angles, the area on the surface can be approximated to a flat circle, so the probability should just increase proportional to 1/(sqrt)r, where r is the distance from the centre point on the surface. If the angle is larger, then you'll have to take into account the curved surface - the formula for a circular area on a curved surface I don't know off the top of my head, but it shouldn't be too hard to find.

[Edited by - BattleMetalChris on January 7, 2010 9:11:34 AM]

###
#6
Members - Reputation: **192**

Posted 07 January 2010 - 03:35 AM

Because it's not perfectly obvious (and needs some short scrolling) in the article I linked to, here the quotes I ment:

Both approaches can rather easily be adapted to work only on sections. (In the first by deleting those not matching the respective angles as well, in the second more direct by limiting the angles.)

Quote:

Uniform Distribution

A simple way to randomly (uniform) distribute points on sphere is called the "hypercube rejection method". To apply this to a unit cube at the origin, choose coordinates (x,y,z) each uniformly distributed on the interval [-1,1]. If the length of this vector is greater than 1 then reject it, otherwise normalise it and use it as a sample.

[...]

Orion Elenzil proposes that by choosing uniformly distributed polar coordinates theta (0 <= theta < 360) and phi (0 <= phi <= pi/2) but the using the sqrt(phi) results in points uniformly distributed on the surface of a hemisphere. If the poles lie along the z axis then the position on a unit hemisphere sphere is

x = cos(sqrt(phi)) cos(theta)

y = cos(sqrt(phi)) sin(theta)

z = sin(sqrt(phi))

A whole sphere is obtained by simply randomising the sign of z.

Both approaches can rather easily be adapted to work only on sections. (In the first by deleting those not matching the respective angles as well, in the second more direct by limiting the angles.)

###
#7
Crossbones+ - Reputation: **5792**

Posted 07 January 2010 - 04:18 AM

Quote:

Original post by alvaro

I would concentrate on getting it right for (0,0,1) first. You can then apply a rotation to the result.

I second this. It's easy to find uniformly distributed points on a sphere, so if you create a uniform distribution around 0,0,1, and then pick a random point on the sphere, and rotate all the points to center on that instead, you have a uniform distribution around that point.

So what you need to do is only to find uniformly distributed points on parts of the z > 0 hemisphere, where z > Z, and Z can be calculated from the acceptable angle from 0,0,1.

(There might be some mistake in the following, but I think it should be correct)

Uniform distribution of random points on a unit-sphere:

`u = rand [-1, 1]`

a = rand [0, 2pi]

x = sqrt(1 - u*u) * cos(a)

y = sqrt(1 - u*u) * sin(a)

z = u

a = rand [0, 2pi]

x = sqrt(1 - u*u) * cos(a)

y = sqrt(1 - u*u) * sin(a)

z = u

For uniform distribution of points on part of the sphere where z > Z, you just need to pick

`u = rand [Z, 1]`, and the rest is exactly as above.

So the complete algorithm would be:

`0. Calculate Z from the angle (a cos or sin)`

1. pick N random points on z > Z

2. pick one random point on the sphere

3. rotate the points from step 1, as to align 0,0,1 on the random point chosen in 2

1. pick N random points on z > Z

2. pick one random point on the sphere

3. rotate the points from step 1, as to align 0,0,1 on the random point chosen in 2

EDIT: I was thinking the points should be centered around a random point too.. if you already have that point you can skip step 2. =)