Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
3D sphere creation

Subject: 3D sphere creation

From: Gregthom

Date: 31 Jan, 2009 17:59:57

Message: 1 of 3

Greetings

Any one with an idea how to use morphological operators to create a
sphere at some position in a 3D volume ?

Say I create this :

r=linspace(0,10,30);
phi=linspace(0,pi,30);
theta=linspace(0,2*pi,40);
[phi,theta,r]=meshgrid(phi,theta,r);

x=r.*sin(phi).*cos(theta);
y=r.*sin(phi).*sin(theta);
z=r.*cos(phi);

and then my volume is vol = zeros(100,100,100);
and the xyz spatial info for my volume is
X = linspace(-20,20,100);Z = linspace(-20,20,100);,Z = linspace
(-20,20,100);

How can I set the positions occupied by points x,y,z (points in
sphere) in my vol matrix to = 1; and 0 other wise. If I do that then I
have a sphere in my 3D volume . Anyone else perhaps with a simpler way
of doing this ?


GT

Subject: 3D sphere creation

From: Roger Stafford

Date: 31 Jan, 2009 18:49:01

Message: 2 of 3

gregthom <gregthom99@yahoo.com> wrote in message <1c9522f5-4f3a-477c-bf96-a919a0aaecf4@g3g2000pre.googlegroups.com>...
> ......
> and then my volume is vol = zeros(100,100,100);
> and the xyz spatial info for my volume is
> X = linspace(-20,20,100);Z = linspace(-20,20,100);,Z = linspace
> (-20,20,100);
>
> How can I set the positions occupied by points x,y,z (points in
> sphere) in my vol matrix to = 1; and 0 other wise. If I do that then I
> have a sphere in my 3D volume . Anyone else perhaps with a simpler way
> of doing this ?
> ......

  In the question you have posed, Greg, your x, y, z arrays are of no help. To lie inside a sphere, or more generally inside some kind of spherical sector, you need expressions for r, theta, and phi as functions of x, y, and z so that the appropriate inequalities can be tested. Of course in your particular example, all you need is r <= 30, so your test is simply

 [x2,y2,z2] = meshgrid(X,Y,Z);
 r = sqrt(x2.^2+y2.^2+z2.^2);
 vol(r<=30) = 1;

Roger Stafford

Subject: 3D sphere creation

From: Gregthom

Date: 3 Feb, 2009 14:33:05

Message: 3 of 3

thanks for the explanation,
It was all about the confusion between matlab's "axis ij" and "axis xy" . Now I can plot my disc like this:

>> [xi,yi] = ndgrid(0:0.08:511*0.08,0:0.08:511*0.08);
>> im = zeros(size(xi));im((xi-40.88/2).^2 + (yi-40.88/2).^2<=25) = 1;
>> figure;imagesc(im);axis image;colormap gray(256)

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us