function [outmat]=customcontour(inmat,levels)

%   customcontour


%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.  


close all

if nargin<2
    b=linspace(0,2*pi,300)
    a=meshgrid(b)
    c=a';
    d=c+a;
	surf(d);
	e=sin(d);
	f=cos(rot90(d'));
	g=e+f;
	surf(g);
    inmat = g;
    levels = [-2 -1.5 -1 -0.5 0 0.5 1 1.5 2];
end

%inmat nach levels angleichen
cpyinmat = inmat;
cpyinmat(find(cpyinmat<=levels(1)))=9999991;
for k=2:length(levels)
    cpyinmat(find(cpyinmat<=levels(k) & cpyinmat>levels(k-1)))=str2num(['999999',num2str(k)]);
    
end
% figure
% surf(cpyinmat);



for i=1:size(inmat,1)-1
    for j=1:size(inmat,2)-1
        if i>1 & j>1
            if  cpyinmat(i-1,j)~=9999990 &  cpyinmat(i,j-1)~=9999990 & cpyinmat(i,j)~=9999990 &  cpyinmat(i,j+1)~=9999990 & cpyinmat(i+1,j)~=9999990 &...
                cpyinmat(i-1,j+1)~=9999990 & cpyinmat(i-1,j-1)~=9999990 %& cpyinmat(i+1,j-1)~=9999990 & cpyinmat(i+1,j+1)~=9999990 
                if  cpyinmat(i-1,j)~=cpyinmat(i,j)| cpyinmat(i,j-1)~=cpyinmat(i,j) | cpyinmat(i,j+1)~=cpyinmat(i,j) | ...
                        cpyinmat(i+1,j)~=cpyinmat(i+1,j)  
                    cpyinmat(i,j)=9999990;
                    neighbour = 1;
                    t=i;
                    u=j;
                    dir = [];
                    %-------------
                    %    5  6  7
                    %     \ | /
                    %   4 - x - 8
                    %     / | \
                    %    3  2  1
                    %---------------
                    while neighbour == 1
                        neighbour = 0;
                        
                        while 1
                            
                            %rechtsunten
                            if dir ~= 1 & dir ~= 2 & dir ~=8
                                if cpyinmat(t+1,u)~=cpyinmat(t,u+1) & cpyinmat(t+1,u+1)~=9999990
                                    cpyinmat(t+1,u+1)=9999990;
                                    t=t+1;u=u+1;
                                    if t>1 & t<(size(inmat,1)-1) & u > 1 & u < (size(inmat,2)-1)
                                        neighbour = 1;
                                    end
                                    dir = 5;
                                    break;
                                end
                            end
                            %unten
                             if dir ~= 1 & dir ~= 2 & dir ~=3
                                  if cpyinmat(t+1,u-1)~=cpyinmat(t+1,u+1)  & cpyinmat(t+1,u)~=9999990
                                    cpyinmat(t+1,u)=9999990;
                                    t=t+1;u=u;
                                    if t>1 & t<(size(inmat,1)-1) & u > 1 & u < (size(inmat,2)-1)
                                        neighbour = 1;
                                    end
                                    dir = 6;
                                    break;
                                end
                            end
                            %linksunten
                             if dir ~= 2 & dir ~= 3 & dir ~=4
                                if cpyinmat(t,u-1)~=cpyinmat(t+1,u)  & cpyinmat(t+1,u-1)~=9999990
                                    cpyinmat(t+1,u-1)=9999990;
                                    t=t+1;u=u-1;
                                    if t>1 & t<(size(inmat,1)-1) & u > 1 & u < (size(inmat,2)-1)
                                        neighbour = 1;
                                    end
                                    dir = 7;
                                    break;
                                end
                            end
                            %links
                             if dir ~= 3 & dir ~= 4 & dir ~=5
                                if cpyinmat(t-1,u-1)~=cpyinmat(t+1,u-1)  & cpyinmat(t,u-1)~=9999990
                                    cpyinmat(t,u-1)=9999990;
                                    t=t;u=u-1;
                                    if t>1 & t<(size(inmat,1)-1) & u > 1 & u < (size(inmat,2)-1)
                                        neighbour = 1;
                                    end
                                    dir = 8;
                                    break;
                                end
                            end
                            %linksoben
                             if dir ~= 4 & dir ~= 5 & dir ~=6
                                if cpyinmat(t,u-1)~=cpyinmat(t-1,u-1)  & cpyinmat(t-1,u-1)~=9999990
                                    cpyinmat(t-1,u-1)=9999990;
                                    t=t-1;u=u-1;
                                    if t>1 & t<(size(inmat,1)-1) & u > 1 & u < (size(inmat,2)-1)
                                        neighbour = 1;
                                    end
                                    dir = 1;
                                    break;
                                end
                            end
                             %oben
                             if dir ~= 5 & dir ~= 6 & dir ~=7
                                if cpyinmat(t-1,u-1)~=cpyinmat(t-1,u+1)  & cpyinmat(t-1,u-1)~=9999990
                                    cpyinmat(t-1,u-1)=9999990;
                                    t=t-1;u=u;
                                    if t>1 & t<(size(inmat,1)-1) & u > 1 & u < (size(inmat,2)-1)
                                        neighbour = 1;
                                    end
                                    dir = 2;
                                    break;
                                end
                            end
                              %rechtsoben
                             if dir ~= 6 & dir ~= 7 & dir ~=8
                                if cpyinmat(t-1,u)~=cpyinmat(t,u+1) cpyinmat(t-1,u+1)~=9999990
                                    cpyinmat(t-1,u+1)=9999990;
                                    t=t-1;u=u+1;
                                    if t>1 & t<(size(inmat,1)-1) & u > 1 & u < (size(inmat,2)-1)
                                        neighbour = 1;
                                    end
                                    dir = 3;
                                    break;
                                end
                            end
                               %rechts
                             if dir ~= 7 & dir ~= 8 & dir ~=1
                                if cpyinmat(t-1,u+1)~=cpyinmat(t+1,u+1) cpyinmat(t-1,u+1)~=9999990
                                    cpyinmat(t-1,u+1)=9999990;
                                    t=t;u=u+1;
                                    if t>1 & t<(size(inmat,1)-1) & u > 1 & u < (size(inmat,2)-1)
                                        neighbour = 1;
                                    end
                                    dir = 4;
                                    break;
                                end
                            end
                                
                            break;
                        end
                        
                    end
                end
            end
        end            
    end
end



tmpcpyinmat= cpyinmat-9999990;
surf(inmat,'CData',tmpcpyinmat);
shading flat
view(0,90)
%pause