⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xml2seg.m

📁 这个程序即可以读取XMl文件并转换成matlab结构体
💻 M
📖 第 1 页 / 共 2 页
字号:
tt=cputime;

for filenum = 23 : 23
%     if filenum<10
%         filename=['000',sprintf('%d',filenum)];
%     end
%     if (filenum>=10)&&(filenum<100)
%         filename=['00',sprintf('%d',filenum)];
%     end
%     if (filenum<1000)&&(filenum>=100)
%         filename=['0',sprintf('%d',filenum)];
%     end
%     %fatherpathnum=['00','20'];
%     fatherpathnum=['12-2'];
%     pathname=[sprintf('d:'),sprintf('\\%s',fatherpathnum),sprintf('\\%s',filename)];
%     %         pathname=[sprintf('d:\\0020\\%s',filename)] %this is also ok!
%     cd(pathname);
%     xmlname=[sprintf('%s',filename),'.xml'];
    
    Xmlf=xml_load(xmlname);
    
    % Xmlf=xml_load('easy03.xml');
    %contour的获取
    contH= fopen('contour.seg', 'wt');
    polygonsize=length(Xmlf.polygons);
    if polygonsize==0
        fprintf(contH, '%d\n', 0);
        fclose(contH);
    else
        m=0;
        for i=1:polygonsize
            if length(Xmlf.polygons{1,i})~=0 %排除空polygon
                if (length(Xmlf.polygons{1,i}.cont)==1) %如果polygon是由一条contour组成且该contour是闭合的,说明是一个真正的多边形,而不是水平或垂直的直线
                    if(Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts(1,1)==Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts(1,length(Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts)))&&(Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts(2,1)==Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts(2,length(Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts)))
                        m=m+1;
                    end
                end
                if length(Xmlf.polygons{1,i}.cont)~=1 %如果polygon是由两条以上contour组成,这说明此polygon是真正的多边形
                    m=m+1;
                end
            end
        end
        fprintf(contH, '%d\n', m);
        for i=1:polygonsize
            if length(Xmlf.polygons{1,i})~=0 %排除空polygon
                if (length(Xmlf.polygons{1,i}.cont)==1) %如果polygon是由一条contour组成且该contour是闭合的,说明是一个真正的多边形,而不是水平或垂直的直线
                    if(Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts(1,1)==Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts(1,length(Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts)))&&(Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts(2,1)==Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts(2,length(Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.pts)))
                        fprintf(contH, '%d', i);%把contour的ID存入contour文件中
                        fprintf(contH, '%s',' ');
                        fprintf(contH, '%d', 0);% type=0
                        fprintf(contH, '%s',' ');
                        fprintf(contH, '%d', 1);% Isvalid=1
                        fprintf(contH, '%s',' ');
                        fprintf(contH, '%d', Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.id);%startcurveID
                        fprintf(contH, '%s',' ');
                        fprintf(contH, '%d', Xmlf.contours{1,length(Xmlf.polygons{1,i}.cont)}.id);%endcurveID
                        fprintf(contH, '%s',' ');
                        fprintf(contH, '%d', length(Xmlf.polygons{1,i}.cont));%包含的curve条数
                        fprintf(contH, '%s',' ');
                        for j=1:length(Xmlf.polygons{1,i}.cont)
                            fprintf(contH, '%d', Xmlf.contours{1,Xmlf.polygons{1,i}.cont(j)}.id);
                            fprintf(contH, '%s',' ');
                        end
                        fprintf(contH, '%s\n',' ');
                    end
                end
                if length(Xmlf.polygons{1,i}.cont)~=1
                    fprintf(contH, '%d', i);
                    fprintf(contH, '%s',' ');
                    fprintf(contH, '%d', 0);
                    fprintf(contH, '%s',' ');
                    fprintf(contH, '%d', 1);
                    fprintf(contH, '%s',' ');
                    fprintf(contH, '%d', Xmlf.contours{1,Xmlf.polygons{1,i}.cont(1)}.id);
                    fprintf(contH, '%s',' ');
                    fprintf(contH, '%d', Xmlf.contours{1,length(Xmlf.polygons{1,i}.cont)}.id);
                    fprintf(contH, '%s',' ');
                    fprintf(contH, '%d', length(Xmlf.polygons{1,i}.cont));
                    fprintf(contH, '%s',' ');
                    for j=1:length(Xmlf.polygons{1,i}.cont)
                        fprintf(contH, '%d', Xmlf.contours{1,Xmlf.polygons{1,i}.cont(j)}.id);
                        fprintf(contH, '%s',' ');
                    end
                    fprintf(contH, '%s\n',' ');
                end
            end
        end
    end

    %curve,linelet,node数据文件的获取
    curveH=fopen('curve.seg','wt');
    lineH=fopen('linelet.seg','wt');
    nodeH=fopen('node.seg','wt');

    nodeID=1;
    lineletID=0;

    contoursize=length(Xmlf.contours);

    Numnode=0;
    for kk=1:length(Xmlf.contours)
        if length(Xmlf.contours{1,kk})~=0
            Numnode=Numnode+length(Xmlf.contours{1,kk}.pts);
        end
    end
    fprintf(lineH, '%d', Numnode-length(Xmlf.contours));%linelet的个数存放到linelet文件中
    fprintf(lineH, '%s\n',' ');
    fprintf(nodeH, '%d', Numnode);%linelet的个数存放到linelet文件中
    fprintf(nodeH, '%s\n',' ');

    if contoursize==0
        fprintf(curveH, '%d\n', 0);
        fprintf(lineH, '%d\n', 0);
        fprintf(nodeH, '%d\n', 0);
        fclose(nodeH);
        fclose(lineH);
        fclose(curveH);
    else
        fprintf(curveH, '%d', contoursize);%curve的条数存入curve文件中
        fprintf(curveH, '%s\n',' ');
        for i=1:contoursize
            if length(Xmlf.contours{1,i})~=0 %排除空contour
                fprintf(curveH, '%d', Xmlf.contours{1,i}.id);
                fprintf(curveH, '%s',' ');
                if strcmp(Xmlf.contours{1,i}.type,'depth')||strcmp(Xmlf.contours{1,i}.type,'abutment')||strcmp(Xmlf.contours{1,i}.type,'surface')||strcmp(Xmlf.contours{1,i}.type,'albedo')||strcmp(Xmlf.contours{1,i}.type,'border')
                    if strcmp(Xmlf.contours{1,i}.type,'depth') %为curve的类型赋相应的值
                        fprintf(curveH, '%d', 1);
                        fprintf(curveH, '%s',' ');
                    end
                    if strcmp(Xmlf.contours{1,i}.type,'abutment')
                        fprintf(curveH, '%d', 3);
                        fprintf(curveH, '%s',' ');
                    end
                    if strcmp(Xmlf.contours{1,i}.type,'surface')
                        fprintf(curveH, '%d', 4);
                        fprintf(curveH, '%s',' ');
                    end
                    if strcmp(Xmlf.contours{1,i}.type,'albedo')
                        fprintf(curveH, '%d', 2);
                        fprintf(curveH, '%s',' ');
                    end
                    if strcmp(Xmlf.contours{1,i}.type,'border')
                        fprintf(curveH, '%d', 1);% 0 or 1?
                        fprintf(curveH, '%s',' ');
                    end
                else
                    fprintf(curveH, '%d', 0);
                    fprintf(curveH, '%s',' ');
                end

                fprintf(curveH, '%d', 1);%curve的width属性为1
                fprintf(curveH, '%s',' ');
                fprintf(curveH, '%d', nodeID);%curve的startnodeID
                fprintf(curveH, '%s',' ');

                %把一些开始结点,结束节点和linelet的ID放入curve文件中
                %node文件数据的写入
                if(isempty(Xmlf.contours{1,i}.jids)==1)
                    %if (Xmlf.contours{1,i}.pts(1,1)==Xmlf.contours{1,i}.pts(1,length(Xmlf.contours{1,i}.pts)))&&(Xmlf.contours{1,i}.pts(2,1)==Xmlf.contours{1,i}.pts(2,length(Xmlf.contours{1,i}.pts)))
                    nodenum=length(Xmlf.contours{1,i}.pts)-1;%若contour闭合,则curve的首端点与尾端点为同一点
                    tempnodeID=nodeID;
                else
                    nodenum=length(Xmlf.contours{1,i}.pts);
                end
                for j=1:nodenum %为此curve中的所有node查找或计算其degree值,j为当前node的序号
                    if j==1 %对curve起始端点degree的判断处理
                        fprintf(nodeH, '%d', nodeID);
                        fprintf(nodeH, '%s',' ');
                        start_end_node=nodeID;
                        fprintf(nodeH, '%d', 1); %是curve的起始点,type=1
                        fprintf(nodeH, '%s',' ');
                        m=0;
                        for k=1:length(Xmlf.junctions) %排除border的junction,从一条curve起始点在junction中匹配,若找到匹配点,则得到其degree属性,赋给node的degree
                            if length(Xmlf.junctions{1,k})~=0
                                if (Xmlf.contours{1,i}.pts(1,1)==Xmlf.junctions{1,k}.loc(1,1))&&(Xmlf.contours{1,i}.pts(2,1)==Xmlf.junctions{1,k}.loc(2,1))
                                    fprintf(nodeH, '%d', length(Xmlf.junctions{1,k}.cids)); %得到curve起始端点的degree
                                    fprintf(nodeH, '%s',' ');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -