function [ ret ] = testnorm2(varargin)
% testnorm([options])
% Tests the diff-metrik to estimate the minkowski-norm.
%
% Options:
%   'RNG',val                       Seed for the random-number generator
%   'nVV',val                       number of points of the polytope. Default=30
%   'dim',val                       the dimension
%   'npts',val                      how many pts to test.
%   'R',val                         radius of points to test
%
% Output:
%   A nice picture
%
% Info:
% This function also needs the function computepolytopenorm()
%
% See also: testnorm, estimatepolytopenorm
%
% Written by: tommsch, 2018

%profile on;
RNG= parsem('RNG',varargin,0); if(RNG); rng(RNG); end;
nVV= parsem('nVV',varargin,40);
dim= parsem('dim',varargin,3);
npts=parsem('npts',varargin,2000);
R=   parsem('R',varargin,5);

pts=randn(dim,npts);
pts=abs(pts);
idx=pts(1,:)>.5; pts(1,idx)=pts(1,idx)-.5;
idx=pts(2,:)>.5; pts(2,idx)=pts(2,idx)-.5;
idx=pts(3,:)>.5; pts(3,idx)=pts(3,idx)-.5;

pts=normalizematrix(pts,'colnorm',2)*R;
pts(1,:)=abs(pts(1,:));

%generate polytope
VV=randn(dim,nVV); 
VV=(normalizematrix(VV,'colnorm',2))/2+randn(size(VV))*.1;

[normval,~]=estimatepolytopenorm(VV, VV, [], 11);
VV(:,normval<1)=[];

tic; [colorval,~]=estimatepolytopenorm(pts, VV, [], 6); toc
tic; [normval,~]=estimatepolytopenorm(pts, VV, [], 11); toc
%normval=R;
for i=1:dim;
    pts(i,:)=pts(i,:)./normval;
end

colorval=30.^colorval;
colorval=[colorval colorval];
pts=[pts pts]; pts(:,1:npts)=-pts(:,1:npts);


colorval=log(abs(colorval));

clf; hold on;

%colorval=diff([colorval(end) colorval]);
%colorval=10.^colorval;


colorval=colorval-min(colorval);
colorval=colorval/max(colorval);
scaleval=colorval*2+1;
colorval=colorval*64;
VV2=[VV -VV];
plotm(VV2,'k.');
if(dim==2)
    scatter(pts(1,:),pts(2,:),ones(1,size(pts,2)),colorval');
elseif(dim==3)
    
    tic; idx=convhulln(VV2.'); toc
    %scatter3(pts(1,:),pts(2,:),pts(3,:),100*ones(1,size(pts,2)),colorval','.');
    scatter3(pts(1,:).*scaleval,pts(2,:).*scaleval,pts(3,:).*scaleval,100*ones(1,size(pts,2)),colorval','.');
    trisurf(idx,VV2(1,:),VV2(2,:),VV2(3,:))
end

colorbar

axis equal
%profile off; profile viewer;

end