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:
generating a facetted surface

Subject: generating a facetted surface

From: Peter Schreiber

Date: 28 Sep, 2011 23:49:13

Message: 1 of 3

Dear all,
I have written some code to shift and tilt ellipsoids which are intersecting each other, with the goal to generate a facetted surface. One item that I'm still trying to figure out is how to remove the interior pieces of the surfaces after they intersect. Any help would be highly appreciated. All that I want to have left is one surface that has as many facets as ellipsoids are used.

Best Regards,
Peter



a=8;
b=8;
c=50;
d=200;
nf_uv_grid = 31;
%alpha=0/180*pi;
%beta=0/180*pi;
gamma=0/180*pi;

[s_grid_x,s_grid_y]=meshgrid(linspace(-2,2,3),linspace(-2,2,3));
s_grid_z=0.*s_grid_x;

[r_grid_x,r_grid_y]=meshgrid(linspace(-40,40,3),linspace(-40,40,3));
r_grid_z=0.*r_grid_x+0.5*d;

f=sqrt((r_grid_x-s_grid_x).^2+(r_grid_y-s_grid_y).^2+(r_grid_z-s_grid_z).^2);

c=sqrt(f.^2+a^2);

alpha=atan((r_grid_x-s_grid_x)./(r_grid_z-s_grid_z));
beta=atan((r_grid_y-s_grid_y)./(r_grid_z-s_grid_z));

s_grid_x=s_grid_x';
s_grid_y=s_grid_y';

for i=1:size(c,1)
  for j=1:size(c,2)

    if (i==((size(c,1)-1)/2+1) && j==((size(c,2)-1)/2)+1) || i==1 || i==size(c,1) || j==1 || j==size(c,2)
x=a.*cos(u).*cos(v);
y=b.*cos(u).*sin(v);
z=c(i,j).*sin(u)+f(i,j);

xv=reshape(x,1,numel(x));
yv=reshape(y,1,numel(y));
zv=reshape(z,1,numel(z));

shift_M=[1 0 0 s_grid_x(i,j);0 1 0 -s_grid_y(i,j); 0 0 1 0; 0 0 0 1];
Rx=[1 0 0 0; 0 cos(alpha(i,j)) -sin(alpha(i,j)) 0;0 sin(alpha(i,j)) cos(alpha(i,j)) 0; 0 0 0 1];
Ry=[cos(beta(i,j)) 0 sin(beta(i,j)) 0; 0 1 0 0; -sin(beta(i,j)) 0 cos(beta(i,j)) 0; 0 0 0 1];
Rz=[cos(gamma) -sin(gamma) 0 0; sin(gamma) cos(gamma) 0 0; 0 0 1 0; 0 0 0 1];

temp=shift_M*Ry*Rx*[xv;yv;zv;ones(size(xv))];

xs=temp(1,:);
ys=temp(2,:);
zs=temp(3,:);

xs2=reshape(xs,nf_uv_grid,nf_uv_grid);
ys2=reshape(ys,nf_uv_grid,nf_uv_grid);
zs2=reshape(zs,nf_uv_grid,nf_uv_grid);

xs2(zs2>20)=NaN;
ys2(zs2>20)=NaN;
zs2(zs2>20)=NaN;

figure(1)
surf(xs2,ys2,zs2)
xlabel('x')
ylabel('y')
zlabel('z')
axis([-30 30 -30 30 -10 50])
hold on

    else

end
end
end

Subject: generating a facetted surface

From: Bruno Luong

Date: 29 Sep, 2011 04:56:29

Message: 2 of 3

The code you post can't run by someone else:

> Undefined function or variable 'u'.

Bruno

Subject: generating a facetted surface

From: Peter Schreiber

Date: 29 Sep, 2011 09:02:11

Message: 3 of 3

Hi Bruno,
Thanks for the note. As you mentioned the definition of u and v was missing.
I would be very curious if anyone has an elegant way of eliminating the interior surfaces that only the outer facetet surface remains.

Thanks a lot,
Peter

Here is the complete code:




clear all
clc
close all
a=8;
b=8;
c=50;
d=200;
nf_uv_grid = 101;
%alpha=0/180*pi;
%beta=0/180*pi;
gamma=0/180*pi;

[u,v]=meshgrid(linspace(0,2*pi,nf_uv_grid),linspace(0,pi,nf_uv_grid));

[s_grid_x,s_grid_y]=meshgrid(linspace(-2,2,3),linspace(-2,2,3));
s_grid_z=0.*s_grid_x;

[r_grid_x,r_grid_y]=meshgrid(linspace(-40,40,3),linspace(-40,40,3));
r_grid_z=0.*r_grid_x+0.5*d;

f=sqrt((r_grid_x-s_grid_x).^2+(r_grid_y-s_grid_y).^2+(r_grid_z-s_grid_z).^2);

c=sqrt(f.^2+a^2);

alpha=atan((r_grid_x-s_grid_x)./(r_grid_z-s_grid_z));
beta=atan((r_grid_y-s_grid_y)./(r_grid_z-s_grid_z));

s_grid_x=s_grid_x';
s_grid_y=s_grid_y';

for i=1:size(c,1)
  for j=1:size(c,2)

    if (i==((size(c,1)-1)/2+1) && j==((size(c,2)-1)/2)+1) || i==1 || i==size(c,1) || j==1 || j==size(c,2)
x=a.*cos(u).*cos(v);
y=b.*cos(u).*sin(v);
z=c(i,j).*sin(u)+f(i,j);

xv=reshape(x,1,numel(x));
yv=reshape(y,1,numel(y));
zv=reshape(z,1,numel(z));

shift_M=[1 0 0 s_grid_x(i,j);0 1 0 -s_grid_y(i,j); 0 0 1 0; 0 0 0 1];
Rx=[1 0 0 0; 0 cos(alpha(i,j)) -sin(alpha(i,j)) 0;0 sin(alpha(i,j)) cos(alpha(i,j)) 0; 0 0 0 1];
Ry=[cos(beta(i,j)) 0 sin(beta(i,j)) 0; 0 1 0 0; -sin(beta(i,j)) 0 cos(beta(i,j)) 0; 0 0 0 1];
Rz=[cos(gamma) -sin(gamma) 0 0; sin(gamma) cos(gamma) 0 0; 0 0 1 0; 0 0 0 1];

temp=shift_M*Ry*Rx*[xv;yv;zv;ones(size(xv))];

xs=temp(1,:);
ys=temp(2,:);
zs=temp(3,:);

xs2=reshape(xs,nf_uv_grid,nf_uv_grid);
ys2=reshape(ys,nf_uv_grid,nf_uv_grid);
zs2=reshape(zs,nf_uv_grid,nf_uv_grid);

 xs2(zs2>20)=NaN;
 ys2(zs2>20)=NaN;
 zs2(zs2>20)=NaN;

figure(1)
surf(xs2,ys2,zs2)
xlabel('x')
ylabel('y')
zlabel('z')
hold on

    else

end
end
end
 

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