Advertisement

equation for an ellipsoid?

Started by October 07, 2002 05:21 PM
4 comments, last by spiffgq 22 years, 4 months ago
I''m trying to randomly generate x number of points, all of which lie inside an ellipsoid. I have a function that will do what I desire for a sphere. I need to flatten that sphere (imagine the sphere rotating very fast, how it will elongate about two axis and shorten about the third).
  
struct POINT{
   float x, y, z;
};

POINT points[x];
float r, phi, th;

for (int i = 0; i < x; ++i){

   phi = randF (0.0, 2.0*M_PI);
   r   = randF (0.0, 20.0);
   th  = randF (0.0, 2.0*M_PI);

   // Defined elsewhere:

   // float randF (min_value, max_value);


   points[i].x = r * sin (phi) * cos (th);
   points[i].y = r * sin (phi) * sin (th);
   points[i].z = r * cos (phi);
}
  
This uses spherical coordinates to generate the sphere because a sphere in spherical coordinates is very simple and I convert them to rectangular coordinates. The problem I''m having is I don''t know the equation of an ellipsoid. I know a sphere is an ellipsoid with the a, b, and c radii equal, but I need a and b to be equal and c to be less than a (or b). Thanks for your help.
SpiffGQ
First, read http://www.math.niu.edu/~rusin/known-math/96/sph.rand

Then just scale it differently along each axis (rx, ry, rz).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Similar questions have been asked numerous times, and in the end, scaling a distribution is a lot of trouble, and it tends to become non-uniform. The easiest way is to generate random points inside a cube encompassing the ellipsoid, and to discard those which are outside the ellipsoid. It works great.

Cédric
quote:
Original post by cedricl
Similar questions have been asked numerous times, and in the end, scaling a distribution is a lot of trouble, and it tends to become non-uniform. The easiest way is to generate random points inside a cube encompassing the ellipsoid, and to discard those which are outside the ellipsoid. It works great.

Cédric

That would require knowing something about the equation of the ellipsoid and the cube. Therefore, if I knew enough to do this, why wouldn''t I just skip the intersection test all together and just generate the points inside the ellipsoid. Either way, I''m still missing the information I need.

SpiffGQ
your elipsoid have width a in x-direction, b in y-direction and c in z-direction.
generate a cube around it (just take 2*max(a,b,c) to get its size, and place it around 0,0,0 as well.. so its "radius" is max(a,b,c) .. i know, terrible explanation:D).
generate a random point in the cube. (hope you get that:D)


  do { p = generate_point_in_cube(max(a,b,c)); p_unit_sphere = p; p_unit_sphere.x /= a; p_unit_sphere.y /= b; p_unit_sphere.z /= c;while(p_unit_sphere.squared_length()>1.f);  


where the cube func could look like this:


  point generate_point_in_cube(half_width) { p = 0; p.x = srand()*half_width; p.y = srand()*half_width; p.z = srand()*half_width; return p;}  


where

max(a,b,c) == max(a,max(b,c))

and srand returns a value between -1 and 1

that should work and generate random points uniformly distributed in an elipsoid with the sizes a,b,c..

anyone having any problems with that?

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

A parametric equation for an ellipsoid is r(s,t)=(a*cos(s)*sin(t),b*sin(s)*sin(t),c*cos(t)) where 0<=s<=2pi and 0<=t<=pi. The main problem with randomly generating a, b and c within a range is that if you picture it as concentric ellipsoid shells each shell has a differant surface area. The shell where a=b=c=0 has only one point, but it is just as likely to be generated as a point on the outtermost shell. As a result you are far more likely to generate a point towards the center. Thus the reason for generating a point in a cube and dropping points outside the ellipsoid.

The equation above is not very useful for checking if a point is in an ellipsoid. An implicit equation is x^2/a^2+y^2/b^2+z^2/c^2=1. You can find this and other equations at MathWorld. So if you plug in the (x,y,z) of your generated point then if the equation yields a value less than or equal to one the point is inside/on the ellipsoid. That is centered at the origin and aligned with the x, y and z axes. If you want it translated and rotated then it is best to just use a transform matrix after you generate the point.
Keys to success: Ability, ambition and opportunity.

This topic is closed to new replies.

Advertisement