
[Right_Seeds Left_Seeds] = Find_Left_Right(Seeds);

figure;
plot3(Left_Seeds(1,:),Left_Seeds(2,:),Left_Seeds(3,:),'.b');
hold;
plot3(Right_Seeds(1,:),Right_Seeds(2,:),Right_Seeds(3,:),'.r');
axis equal;

[m, min_ind]=min(Left_Seeds(3,:));

tmp=Left_Seeds(:,1);
Left_Seeds(:,1)=Left_Seeds(:,min_ind);
Left_Seeds(:,min_ind)=tmp;
plot3(Left_Seeds(1,min_ind),Left_Seeds(2,min_ind),Left_Seeds(3,min_ind),'ob');


[m, min_ind]=min(Right_Seeds(3,:));

tmp=Right_Seeds(:,1);
Right_Seeds(:,1)=Right_Seeds(:,min_ind);
Right_Seeds(:,min_ind)=tmp;

plot3(Right_Seeds(1,min_ind),Right_Seeds(2,min_ind),Right_Seeds(3,min_ind),'or');

remaining_Point=Left_Seeds;
refPoint=remaining_Point(:,1);
Sorted_Left_Seeds=refPoint;
remaining_Point(:,1)=[];

while size(remaining_Point,2) > 0
    vec=[];
    for i=1: size(remaining_Point,2)
        vec(i)=norm(remaining_Point(:,i)-refPoint);
    end

    [m min_ind]=min(vec);
    min_ind=min_ind(1);

    refPoint=remaining_Point(:,min_ind);
%     plot3(refPoint(1),refPoint(2),refPoint(3),'or');
%     pause;
    Sorted_Left_Seeds=[Sorted_Left_Seeds refPoint];
    remaining_Point(:,min_ind)=[];

end

remaining_Point=Right_Seeds;
refPoint=remaining_Point(:,1);
Sorted_Right_Seeds=refPoint;
remaining_Point(:,1)=[];

while size(remaining_Point,2) > 0
    vec=[];
    for i=1: size(remaining_Point,2)
        vec(i)=norm(remaining_Point(:,i)-refPoint);
    end

    [m min_ind]=min(vec);
    min_ind=min_ind(1);

    refPoint=remaining_Point(:,min_ind);
    Sorted_Right_Seeds=[Sorted_Right_Seeds refPoint];
    remaining_Point(:,min_ind)=[];

end


figure;
plot3(Sorted_Left_Seeds(1,:),Sorted_Left_Seeds(2,:),Sorted_Left_Seeds(3,:),'b', ...
      Sorted_Right_Seeds(1,:),Sorted_Right_Seeds(2,:),Sorted_Right_Seeds(3,:),'r', ...
      Sorted_Left_Seeds(1,:),Sorted_Left_Seeds(2,:),Sorted_Left_Seeds(3,:),'b.', ...
      Sorted_Right_Seeds(1,:),Sorted_Right_Seeds(2,:),Sorted_Right_Seeds(3,:),'r.');
axis equal;


m=size(Sorted_Right_Seeds,2);
curveFittingFactor=2/3;
stepsize=1/3; % 1/2;

SmoothedPts = fitCurveTo3DPts(Sorted_Right_Seeds', Sorted_Right_Seeds(:,1)', Sorted_Right_Seeds(:,end)', curveFittingFactor*m)';%Sorted_Right_Seeds; 
m=size(SmoothedPts,2);
F=spline((1:m),SmoothedPts);

t=[1:stepsize:m];
Spline_Right=ppval(F,t);

figure;

plot3(Spline_Right(1,:),Spline_Right(2,:),Spline_Right(3,:),'r', ...
    SmoothedPts(1,:),SmoothedPts(2,:),SmoothedPts(3,:),'r*', ...
      Sorted_Right_Seeds(1,:),Sorted_Right_Seeds(2,:),Sorted_Right_Seeds(3,:),'r.');
  
  hold;
  
 m=size(Sorted_Left_Seeds,2);

SmoothedPts = fitCurveTo3DPts(Sorted_Left_Seeds', Sorted_Left_Seeds(:,1)', Sorted_Left_Seeds(:,end)', curveFittingFactor*m)'; %Sorted_Left_Seeds; 
m=size(SmoothedPts,2);
F=spline((1:m),SmoothedPts);

t=[1:stepsize:m];
Spline_Left=ppval(F,t);

plot3(Spline_Left(1,:),Spline_Left(2,:),Spline_Left(3,:),'b', ...
    SmoothedPts(1,:),SmoothedPts(2,:),SmoothedPts(3,:),'b*', ...
      Sorted_Left_Seeds(1,:),Sorted_Left_Seeds(2,:),Sorted_Left_Seeds(3,:),'b.');
  
axis equal;

Spline_Left=round(Spline_Left);
Spline_Right=round(Spline_Right);


[m max_ind]=max(Spline_Left(2,:));
[m min_ind]=min(Spline_Left(2,:));

Left_Posterior=Spline_Left(:,1:max_ind+1);
Left_Superior=Spline_Left(:,max_ind+2:min_ind-2);
Left_Anterior=Spline_Left(:,min_ind-1:min_ind+1);
Left_Inferior=Spline_Left(:,min_ind+2:end);

Act_Left_Posterior=Spline_Left(:,1:max_ind);
Act_Left_Superior=Spline_Left(:,max_ind+1:min_ind-1);
Act_Left_Anterior=Spline_Left(:,min_ind:end);


[m max_ind]=max(Spline_Right(2,:));
[m min_ind]=min(Spline_Right(2,:));

Right_Posterior=Spline_Right(:,1:max_ind+1);
Right_Superior=Spline_Right(:,max_ind+2:min_ind-2);
Right_Anterior=Spline_Right(:,min_ind-1:min_ind+1);
Right_Inferior=Spline_Right(:,min_ind+2:end);

Act_Right_Posterior=Spline_Right(:,1:max_ind);
Act_Right_Superior=Spline_Right(:,max_ind+1:min_ind-1);
Act_Right_Anterior=Spline_Right(:,min_ind:end);

figure;
plot3(Left_Posterior(1,:),Left_Posterior(2,:),Left_Posterior(3,:),'r.');
hold
plot3(Left_Superior(1,:),Left_Superior(2,:),Left_Superior(3,:),'b.');
plot3(Left_Anterior(1,:),Left_Anterior(2,:),Left_Anterior(3,:),'g.');
plot3(Left_Inferior(1,:),Left_Inferior(2,:),Left_Inferior(3,:),'K.');

plot3(Right_Posterior(1,:),Right_Posterior(2,:),Right_Posterior(3,:),'r.');
plot3(Right_Superior(1,:),Right_Superior(2,:),Right_Superior(3,:),'b.');
plot3(Right_Anterior(1,:),Right_Anterior(2,:),Right_Anterior(3,:),'g.');
plot3(Right_Inferior(1,:),Right_Inferior(2,:),Right_Inferior(3,:),'K.');
plot3(Left_Posterior(1,:),Left_Posterior(2,:),Left_Posterior(3,:),'r.');

axis equal;



