
if ( ~exist('dist') ),
	cd D:\LAZA\DICKST~1
	path(path, 'D:\LAZA\DICKST~1');
%	cd G:\DICKST~1
%	path(path, 'G:\DICKST~1');
	filename = 'meat102';
	% [dist,k,h,w] = dLoadData(filename);
	disp('Loading data ...');
	load meat102
	more off
	dTeta;		% Calculating goodness of the signal
	dDelBad;	% Removing bad signals from the matrices
end; %if ( ~exist('dist') )

x = zeros(h,k); y = zeros(h,k); z = zeros(h,k); 
R = sqrt(1/3)*mean(mean( dist(1:h,1:h) ));
x1 = R*randn(h,1);
y1 = R*randn(h,1);
z1 = R*randn(h,1);
x(1:h,1) = x1;
y(1:h,1) = y1;
z(1:h,1) = z1;

[x1,y1,z1] = dMinimize( x1,y1,z1, dPlane(dist,1), dPlane(teta,1) );

disp('Calculating points coordinates');
tic;
tmax = k;
for t = 1:tmax,
	d = dPlane(dist,t);
	tt= dPlane(teta,t);
	[x1,y1,z1,e,counter] = ...
		dMinimiz( dtrend(x1),dtrend(y1),dtrend(z1), d , tt );
	x(1:h,t) = x1;
	y(1:h,t) = y1;
	z(1:h,t) = z1;
%    if( rem(k-1,100) == 0),
	disp(sprintf('%d/%d  e = %f, n = %d ',t,k, e, counter));
%    end; %if
end; %for
toc;
disp('Calculating distances between new points...');
d2 = zeros(size(dist));
for t = 1:k,
	d2(h*(t-1)+(1:h), 1:h ) = dDistances(x(:,t),y(:,t),z(:,t));
end;
disp('Calculations done');
toc

disp('Saving Data...');
save dtxyz3 dist teta x y z h k filename

t = 1:h;
xyz = zeros( size(x,2), 3*size(x,1));
xyz(:,3*(t-1)+1) = x';
xyz(:,3*(t-1)+2) = y';
xyz(:,3*(t-1)+3) = z';
save -ascii tx102xyz.txt  xyz

disp('Done');

