MATLAB Answers

How to draw smooth balls (spheres) and sticks (cylinders) around my XYZ points.

35 views (last 30 days)
ankit agrawal
ankit agrawal on 9 Apr 2015
Commented: Chris McComb on 10 Apr 2015
Hi,
I generate the xyz coordinate of a molecule using simulation in C++. Now i have 250 atoms of xyz coordinate in one molecule. (the file attached) I want to draw the smooth surface over my atoms. So, I had written MATLAB code to draw blobby surface in my molecules. The code is following
v=load(molecule.txt);
Radius=1.5, B=-1;
x=v(:,1);
y=v(:,2);
z=v(:,3);
[xg,yg,zg] = meshgrid(min(x)-2 : 1 : max(x)+2,min(y)-2 : 1 : max(y)+2,min(z)-2 : 1 : max(z)+2);
xd=size(xg,1);yd=size(xg,2);zd=size(xg,3);
for ii=1:xd
for jj=1:yd
for kk=1:zd
blobby=0;
for pp=1:length(v)
c=[xg(ii,jj,kk),yg(ii,jj,kk),zg(ii,jj,kk)] - [x(pp) y(pp) z(pp)];
blobby=blobby+exp(B*( (sum(c.^2 )/(Radius^2)) -1 )) ;
end
G(ii,jj,kk)=blobby;
end
end
end
%isosurface(xg,yg,zg,G)
p=patch(isosurface(xg,yg,zg,G));
isonormals(xg,yg,zg,G,p);
set(p,'FaceColor','red','EdgeColor','none');
%daspect([2 2 2])
view(3); axis tight
camlight; lighting phong
The figure is attached for blobby parameter B = -1 and Radius =1.5
Some of the atoms are very far in my case so its not covered in single surface. If i increase the Radius and decrease B then it becomes too much smooth (it becomes sphere).
Now i wanted some new technique like to connect the bond in cylinder and atoms in sphere such that overall structure should be smooth. There is representation in vmd called CPK where i can make such image.
But i cannot make this molecule smooth. Is anyone know how to make such figures in MATLAB with good smoothness?
Thanks in advance.

  3 Comments

Chris McComb
Chris McComb on 10 Apr 2015
Can you also provide information regarding which atoms are bonded? This will be necessary to draw the appropriate cylinders.
ankit agrawal
ankit agrawal on 10 Apr 2015
Hi Chris McComb The atoms are bonded in order of appearance in the data file. Like atom i is bonded to i+1, where i range from 1 to 249.

Sign in to comment.

Answers (1)

Chris McComb
Chris McComb on 10 Apr 2015
Edited: Chris McComb on 10 Apr 2015
This should do what you're looking for regarding the spheres (but see my above comment for cylinders).
% Parameters
SPHERE_RES = 20; % resolution for your spheres
SPHERE_RAD = 2.0; % radius of spheres
% Load the data
v = load('molecule.txt');
% Make base sphere data
[xb, yb, zb] = sphere(SPHERE_RES);
% Make a figure
figure;
hold on;
% Plot spheres
for i=1:1:length(v)
surf(SPHERE_RAD*xb+v(i,1), SPHERE_RAD*yb+v(i,2), SPHERE_RAD*zb+v(i,3), 'facecolor', 'b', 'edgealpha', 0);
end
% Make sure they're smooth and shaded
light;
lighting gouraud;

  2 Comments

ankit agrawal
ankit agrawal on 10 Apr 2015
Thank you Chris for nice replying. This was my first post in matlab central and you were the first person who replied. Thank you once again. I add this snippet in your code.
for i=1:249
[xc,yc,zc]=cylinder2P([1,1],10,v(i,:),v(i+1,:));
surf(xc,yc,zc);
end
where i used the cylinder2P function from here http://www.mathworks.com/matlabcentral/fileexchange/5468-cylinder-between-2-points Again i don't how to remove the edge boundary of atoms and bonds. So, that overall structure looks smooth. Can you do it?
Chris McComb
Chris McComb on 10 Apr 2015
Happy to help! What you're seeing are the edges between panels on the surface. The way I handled that for the spheres was just to make them transparent. In your code, just replace
surf(xc,yc,zc);
with
surf(xc, yc, zc, 'facecolor', 'b', 'edgealpha', 0);
This will make the surface blue ('b'), and make the edges 0% opaque.

Sign in to comment.