function [Sol,tim,suc] = transformAFS(data,part,bndpts,hx,hy)
tim = cputime;
suc = 0;
Sol.T = [];
Sol.A = [];

V = data.V;

parts = data.parts;

if parts == 1
    AFS = data.AFS;
    dimP = data.dimP;
    lenP = data.lenP;
    LENP = data.LENP;
    
    Sol.T = [];
    for j = 1:2
        % the extremal points
        [m,id(1)] = min(AFS{j}(:,1));
        [m,id(2)] = max(AFS{j}(:,1));
        [m,id(3)] = min(AFS{j}(:,2));
        [m,id(4)] = max(AFS{j}(:,2));

        Sol.T = [Sol.T; AFS{j}(id,:)];

        % the boundary points
        Sol.T = [Sol.T; AFS{j}(1,:)];
        lb = size(Sol.T,1);
        lt = 1;
        hr = lenP(j)/bndpts;
        i = 1;
        while i < bndpts
            len = sum(LENP(j,1:lt));
            if hr*i < len
                lb = lb+1;
                al = (len-hr*i)/LENP(j,lt);
                Sol.T(lb,:) = al*AFS{j}(lt,:)+(1-al)*AFS{j}(lt+1,:);
                i = i+1;
            else
                lt = lt+1;
            end
        end
    end
    
    % the inner points
    for j=1:2
        lockup{j} = zeros(dimP(j)-1,2);
        for i=1:dimP(j)-1
            lockup{j}(i,:) = [min(AFS{j}(i:i+1,2)) max(AFS{j}(i:i+1,2))];
        end
    end
    ny = ceil((max(AFS{1}(:,2))-min(AFS{1}(:,2)))/hy)+1;
    gridy = linspace(min(AFS{1}(:,2)),max(AFS{1}(:,2)),ny);
    
    for i=2:ny-1
        if (gridy(i) > max(AFS{2}(:,2))) || (gridy(i) < min(AFS{2}(:,2)))
            cuts = [];
            cutp = [];
            for j=1:dimP(1)-1
                if (gridy(i) > lockup{1}(j,1)) && (gridy(i) < lockup{1}(j,2))
                    al = (AFS{1}(j+1,2)-gridy(i))/(AFS{1}(j+1,2)-AFS{1}(j,2));
                    
                    cuts = [cuts; j];
                    cutp = [cutp; al*AFS{1}(j,:)+(1-al)*AFS{1}(j+1,:)];
                end
            end
            nc = length(cuts);

            nx = ceil(abs(cutp(1,1)-cutp(2,1))/hx)+1;
            gridx = linspace(cutp(1,1),cutp(2,1),nx);
            for j=2:nx-1
                lb = lb+1;
                Sol.T(lb,:) = [gridx(j), gridy(i)];
            end
        else
            cuts = [];
            cutp = [];
            for ii = 1:2
                cuts{ii} = [];
                cutp{ii} = [];
                for j=1:dimP(ii)-1
                    if (gridy(i) > lockup{ii}(j,1)) && (gridy(i) < lockup{ii}(j,2))
                        al = (AFS{ii}(j+1,2)-gridy(i))/(AFS{ii}(j+1,2)-AFS{ii}(j,2));

                        cuts{ii} = [cuts{ii}; j];
                        cutp{ii} = [cutp{ii}; al*AFS{ii}(j,:)+(1-al)*AFS{ii}(j+1,:)];
                    end
                end
            end
            nc = length(cuts);

            nx = ceil(abs(cutp{1}(1,1)-cutp{1}(2,1))/hx)+1;
            gridx = linspace(cutp{1}(1,1),cutp{1}(2,1),nx);
            for j=2:nx-1
                if (gridx(j) < min(cutp{2}(:,1))) || (gridx(j) > max(cutp{2}(:,1)))
                    lb = lb+1;
                    Sol.T(lb,:) = [gridx(j), gridy(i)];
                end
            end
        end 
    end
    Sol.A = [ones(lb,1) Sol.T]*V(:,1:3)';
    suc = 1;
elseif parts == 2
    AFS = data.AFS{part};
    
    if part == 1
        m = ceil((AFS(2)-AFS(1))/hx)+1;
    else
        m = ceil((AFS(2)-AFS(1))/hy)+1;
    end
    
    Sol.T = linspace(AFS(1),AFS(2),m)';
    Sol.A = [ones(m,1) Sol.T]*V(:,1:data.s)';
    
    suc = 1;
elseif parts >= 3
    if data.valid(part) < 1
        Sol = [];
        tim = 0;
        suc = 0;
        return;
    end
    suc = 1;
    AFS = data.AFS{part};
    dimP = data.dimP(part);
    lenP = data.lenP(part);
    LENP = data.LENP(part,:);

    % the extremal points
    [m,id(1)] = min(AFS(:,1));
    [m,id(2)] = max(AFS(:,1));
    [m,id(3)] = min(AFS(:,2));
    [m,id(4)] = max(AFS(:,2));

    Sol.T = AFS(id,:);

    % the boundary points
    Sol.T(5,:) = AFS(1,:);
    lb = 5;
    lt = 1;
    hr = lenP/bndpts;
    i = 1;
    
    while (i < bndpts) && (lt < size(AFS,1))
        len = sum(LENP(1:lt));
        if hr*i <= len
            lb = lb+1;
            al = (len-hr*i)/LENP(lt);
            Sol.T(lb,:) = al*AFS(lt,:)+(1-al)*AFS(lt+1,:);
            i = i+1;
        else
            lt = lt+1;
        end
    end

    % the inner points
    lockup = zeros(dimP-1,2);
    for i=1:dimP-1
        lockup(i,:) = [min(AFS(i:i+1,2)) max(AFS(i:i+1,2))];
    end
    ny = ceil((max(AFS(:,2))-min(AFS(:,2)))/hy)+1;
    gridy = linspace(min(AFS(:,2)),max(AFS(:,2)),ny);

    for i=2:ny-1
        cuts = [];
        cutp = [];
        for j=1:dimP-1
            if (gridy(i) > lockup(j,1)) && (gridy(i) < lockup(j,2))
                al = (AFS(j+1,2)-gridy(i))/(AFS(j+1,2)-AFS(j,2));

                cuts = [cuts; j];
                cutp = [cutp; al*AFS(j,:)+(1-al)*AFS(j+1,:)];
            end
        end
        nc = length(cuts);

        nx = ceil(abs(cutp(1,1)-cutp(2,1))/hx)+1;
        gridx = linspace(cutp(1,1),cutp(2,1),nx);
        for j=2:nx-1
            lb = lb+1;
            Sol.T(lb,:) = [gridx(j), gridy(i)];
        end

    end

    Sol.A = [ones(lb,1) Sol.T]*V(:,1:3)';
end

tim = cputime-tim;
    