function [vertexmat,patchmat]=ase2x(filepath,filename,displaystatus);

%   ase2x

%   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.                                                                      
patchzaehler=1;
punktezaehler=1;
enablevert = 0;
enablepatch = 0;
enabletvert = 1;
global vertexmat patchmat
if nargin<1
    [ASEfilename,ASEfilepath]=uigetfile('*.ASE','Bitte wählen Sie den ASCI-Export von 3dmax aus:');
end
    
    ASEfile=[ASEfilepath,ASEfilename];
fid = fopen(ASEfile,'rt');


linestring = fgetl(fid);
while isempty(findstr('MESH_NUMVERTEX',linestring))
    linestring = fgetl(fid);
end
whitespace=findstr(linestring,' ');
numvertex=str2num(linestring(whitespace+1:end));
linestring = fgetl(fid);
whitespace=findstr(linestring,' ');
numpatch = str2num(linestring(whitespace+1:end));

if enablevert
	%read vertices
	%-----------------
	while isempty(findstr('MESH_VERTEX_LIST',linestring))
        linestring = fgetl(fid);
	end
	linestring=fgetl(fid);
	vertexzaehler=1;
	vertexmat=[];
	clc;
	while isempty(findstr(linestring,'}'))
        linestring=strrep(linestring,'*MESH_VERTEX','');
        while ~isempty(findstr(linestring,'  '))
            linestring=strrep(linestring,'  ',' ');
        end
        data=str2num(linestring);
        vertexmat(vertexzaehler,:)=data(2:4);
        vertexzaehler=vertexzaehler+1;
        linestring=fgetl(fid);
        if mod(vertexzaehler,20)==0
            clc;
            fprintf([num2str(vertexzaehler),' of ',num2str(numvertex),' vertices read...','\n']);
            pause(0.001)
        end
	end
    fprintf('Done reading vertices!');
    pause(1);
end

clc;



if enablepatch
	%read faces
	%------------
	while isempty(findstr('MESH_FACE_LIST',linestring))
        linestring = fgetl(fid);
	end
	
	linestring=fgetl(fid);
	patchzaehler=1;
	patchmat=[];
	while isempty(findstr(linestring,'}'))
        linestring=strrep(linestring,'*MESH_FACE','');
        while ~isempty(findstr(linestring,'  '))
            linestring=strrep(linestring,'  ',' ');
        end
        a=findstr(linestring,'A:');
        b=findstr(linestring,'B:');
        c=findstr(linestring,'C:');
        ab=findstr(linestring,'AB:');
        patchmat(patchzaehler,1)=str2num(linestring(a+2:b-1));
        patchmat(patchzaehler,2)=str2num(linestring(b+2:c-1));
        patchmat(patchzaehler,3)=str2num(linestring(c+2:ab-1));
        patchzaehler=patchzaehler+1;
        linestring=fgetl(fid);
        if mod(patchzaehler,20)==0
            if mod(patchzaehler,20)==0
                clc;
                fprintf([num2str(patchzaehler),' of ',num2str(numpatch),' patches read...','\n']);
                pause(0.001)
            end
        end
	end
	fprintf('Done reading patches!');
	pause(0.001);
end



if enabletvert
	%read texture coordinates
	%------------
	while isempty(findstr('MESH_TVERTLIST',linestring))
        linestring = fgetl(fid);
	end
	
	linestring=fgetl(fid);
	tvertexzaehler=1;
	tvertexmat=[];
	while isempty(findstr(linestring,'}'))
        linestring=strrep(linestring,'*MESH_TVERT ','');
        while ~isempty(findstr(linestring,'  '))
            linestring=strrep(linestring,'  ',' ');
        end
        tmpvec = str2num(linestring);
        tvertexmat(tvertexzaehler,1)=tmpvec(2);
        tvertexmat(tvertexzaehler,2)=tmpvec(3);
        tvertexmat(tvertexzaehler,3)=tmpvec(4);
        tvertexzaehler=tvertexzaehler+1;
        linestring=fgetl(fid);
        if mod(tvertexzaehler,20)==0
            if mod(tvertexzaehler,20)==0
                clc;
                fprintf([num2str(tvertexzaehler),' of ',num2str(numvertex),' t_vertices read...','\n']);
                pause(0.001)
            end
        end
	end
end



[savefilename,savefilepath]=uiputfile('*.mat','Please name the 3d-file:');
if isempty(findstr(savefilename,'.mat'))
    savefilename=[savefilename,'.mat'];
end
save([savefilepath,savefilename],'vertexmat','patchmat','tvertexmat');




if nargout<1
	warning off
    patchmat = patchmat +1;
	t=patch('Vertices',vertexmat,'Faces',patchmat,'FaceVertexCData',hsv(1),'FaceColor','flat','EdgeColor','none');
	h = light('position',[0.1 0 0],'style','infinite','Color',[1 1 0]);
	view(45,20);
    patchmat = patchmat-1;
end



fclose(fid);
return