function colorspace = generatedata(toppts, fname); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generatedata.m                     %
% written 3/5/00 by Ben Olding       %
% Generates a series of random       %
% points in xyz space.  Uses top 10% %
% as test points for cmy space       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Requires: MasterDataList.mat, findtetra.m,
%           tetravolume.m, coplanar.m, xyz2cmy.m

numpts = 10*toppts;

load MasterDataList;

xmin = min(MasterDataList(:,1));
ymin = min(MasterDataList(:,2));
zmin = min(MasterDataList(:,3));
xmax = max(MasterDataList(:,1));
ymax = max(MasterDataList(:,2));
zmax = max(MasterDataList(:,3));

i = 1;
disp (' ');
while i<=numpts,
  R(i,:)=[unifrnd(xmin,xmax), ...
          unifrnd(ymin,ymax), ...
          unifrnd(zmin,zmax), 0];
  [p1 p2 p3 p4 V] = findtetra(R(i,1:3), MasterDataList);
  if V>=0,
    R(i,4) = V;
    i=i+1;
    if floor(i/toppts)==i/toppts,
      fprintf('.');
    end;
  end;  
end;
disp (' ');

R = sortrows (R,4);

for i=1:toppts,
  [C M Y] = xyz2cmy(R(numpts+1-i,1:3),MasterDataList);
  colorspace(i,:)=[C M Y];
  truexyz (i,:)= R(numpts+1-i,1:3);
end;

save ([fname,'_truexyz'],'truexyz');





