function plotmolecule(atoms,specs,bonds)
%PLOTMOLECULE Simple plotting of a molecule.
%
%plotmolecule(atoms,specs,bonds)
%atoms - the atoms in the molecule.
%specs - atom specification (not used)
%bonds - bonds specification.

% v1.0  2000-10-30. Niclas Borlin, niclas@cs.umu.se.

% Plot all atoms as 'x'.
plot3(atoms.pos(:,1),atoms.pos(:,2),atoms.pos(:,3),'x');
axis equal

n=length(atoms.type);
maxRange=max(max(bonds.range));

% Output some progress
done=0;
total=n*(n-1)/2;
step=floor(sqrt(total));

for i=1:n-1
	type1=atoms.type{i};
	pos1=atoms.pos(i,:);

	for j=i+1:n
		type2=atoms.type{j};
		pos2=atoms.pos(j,:);

		% Check if distance is within any bond range.
		dist=norm(pos1-pos2);
		if (dist<=maxRange)
			% Construct sorted lookup string.
			sorted=cellstr(sort(strvcat(upper(type1),upper(type2),';')));
			lookup=strcat(sorted{[2,1,3]});

			% Check if bond type is specified.
			k=strmatch(lookup,bonds.type,'exakt');
			if (~isempty(k))
				% At least one match found.
				if (length(k)>1)
					error('Internal error. Multiple defined bond.');
				end
				% Get range to check.
				range=bonds.range(k,:);
				% Check if distance is within bond range.
				if (dist>=min(range) & dist<=max(range))
					% Bond is active. Draw a line.
					line([pos1(1),pos2(1)],[pos1(2),pos2(2)],...
						 [pos1(3),pos2(3)],'linestyle','-');
				end
			end
		end
		done=done+1;
		if (rem(done,step)==0)
			disp(sprintf('Done %d%% (%d/%d).',floor(done/total*100),...
						 done,total));
		end
	end
end
