function x=allcorners(A,b,validOnly,eps)
%ALLCORNERS Returns all corners to Ax>=b.
%
%x=allcorners(A,b[,validOnly,eps])
%A         - Constraint matrix.
%b         - Constraint right-hand side.
%validOnly - If supplied and non-zero, only returns valid corners.

% v1.0  2000-09-25. Niclas Borlin, niclas@cs.umu.se.
% v1.1  2001-03-20. Added validOnly option.

if (nargin<3)
	validOnly=false;
end

if (nargin<4)
	eps=0;
end

[m,n]=size(A);

% Get all possible row selections.
i=nchoosek(1:m,n);

% Pre-allocate corner matrix.
x=zeros(n,size(i,1));

if (validOnly)
	valid=logical(ones(1,size(i,1)));
	% Calculate corner for each row selection.
	for j=1:size(i,1)
		Aj=A(i(j,:),:);
		bj=b(i(j,:));
		if (rank(Aj)<size(Aj,2))
			xj=nan*ones(n,1);
			r=-inf*ones(m,1);
		else
			xj=Aj\bj;
			r=A*xj-b;
		end
		if (~all(finite(xj)) | any(r<-eps))
			valid(j)=false;
		else
			x(:,j)=xj;
		end
	end
	x=x(:,valid);
else
	% Calculate corner for each row selection.
	for j=1:size(i,1)
		x(:,j)=A(i(j,:),:)\b(i(j,:));
	end
end	