function [newOut,combined] = combineldb (rin1,rin2,OPT,varargin)

newOut   = [];
combined = false;

%find the sets of polygons in the first ldb
n1 = find(isnan(rin1.x));
n2 = find(rin1.x == 999.999);
nr1 = unique([n1 n2]);
if ~isempty(nr1)
    n2 = 0;
    for n1 = 1 : length(nr1)
        ind1(n1,1) = n2 + 1;
        ind1(n1,2) = nr1(n1) - 1;
        n2 = nr1(n1);
    end
    if n2 < length(rin1.x)
        ind1(length(nr1)+1,1) = n2 + 1;
        ind1(length(nr1)+1,2) = length(rin1.x);
    end
else
    ind1(1,1) = 1;
    ind1(1,2) = length(rin1.x);
end

%find the sets of polygons in the second ldb
n1 = find(isnan(rin2.x));
n2 = find(rin2.x == 999.999);
nr2 = unique([n1 n2]);
if ~isempty(nr2)
    n2 = 0;
    for n1 = 1 : length(nr2)
        ind2(n1,1) = n2 + 1;
        ind2(n1,2) = nr2(n1) - 1;
        n2 = nr2(n1);
    end
    if n2 < length(rin2.x)
        ind2(length(nr2)+1,1) = n2 + 1;
        ind2(length(nr2)+1,2) = length(rin2.x);
    end
else
    ind2(1,1) = 1;
    ind2(1,2) = length(rin2.x);
end

l1 = size(ind1);
l2 = size(ind2);

% get the wanted shift of the points
mv1 = 0;
mv2 = 0;

mv1 = OPT.shiftldb1;
mv2 = OPT.shiftldb2;

%create all the polygons in arrays
for n1 = 1 : l1(1)
    %for each set number of first ldb
    %remove identical points
    [t1a.x,t1a.y] = removeidentical(rin1.x(ind1(n1,1):ind1(n1,2)),rin1.y(ind1(n1,1):ind1(n1,2)));
    % t1a.x=r1.x;
    % t1a.y=r1.y;
    if mv1 > 0
        r1a(n1).x = t1a.x(mv1+1:end);
        r1a(n1).x = [r1a(n1).x;t1a.x(1:mv1)];
        r1a(n1).y = t1a.y(mv1+1:end);
        r1a(n1).y = [r1a(n1).y;t1a.y(1:mv1)];
    else
        r1a(n1) = t1a;
    end
end
for n2 = 1 : l2(1)
    % for each set number of second ldb
    [t2a.x,t2a.y] = removeidentical(rin2.x(ind2(n2,1):ind2(n2,2)),rin2.y(ind2(n2,1):ind2(n2,2)));
    % t2a.x=r2.x;
    % t2a.y=r2.y;
    if mv2 > 0
        r2a(n2).x = t2a.x(mv2+1:end);
        r2a(n2).x = [r2a(n2).x;t2a.x(1:mv2)];
        r2a(n2).y = t2a.y(mv2+1:end);
        r2a(n2).y = [r2a(n2).y;t2a.y(1:mv2)];
    else
        r2a(n2) = t2a;
    end
end

inUse = ones(l2(1),1);
nrOfNewLdbs = 0;
for n1 = 1 : l1(1)
    combinedN1 = false;
    combinedBefore = false;
    combinedN2     = -1;
    Clockwise1 = polyIsClockwise(r1a(n1).x,r1a(n1).y); %!!!clockwise is tegen de klok in
    if Clockwise1
        r1a(n1).x = flipud(r1a(n1).x);
        r1a(n1).y = flipud(r1a(n1).y);
    end
    for n2 = 1 : l2(1)
        if inUse(n2)
            Clockwise2 = polyIsClockwise(r2a(n2).x,r2a(n2).y);
            if Clockwise2
                r2a(n2).x = flipud(r2a(n2).x);
                r2a(n2).y = flipud(r2a(n2).y);
            end

            [in,im] = getDistance(r1a(n1),r2a(n2),OPT.distance);
            
            if ~(isempty(in) && isempty(im))
                combinedN1 = true;
                if combinedBefore
                    inUse(n2) = 0;
                    Clockwise2 = polyIsClockwise(r2a(combinedN2).x,r2a(combinedN2).y);
                    if Clockwise2
                        r2a(combinedN2).x = flipud(r2a(combinedN2).x);
                        r2a(combinedN2).y = flipud(r2a(combinedN2).y);
                    end

                    [in,im] = getDistance(r2a(n2),r2a(combinedN2),OPT.distance);
                    if ~(isempty(in) && isempty(im))
                        disp 'combineldb>Warn administrator'
                        return
                    end
                    [new,theType] = combine2ldb(r2a(n2),r2a(combinedN2),in,im);
                    if ~isempty(theType)
                        combined = true;
                        r2a(combinedN2) = new;
                    end
                else
                    %combine n1 and n2
                    combinedBefore = true;
                    combinedN2 = n2;
                    combined = true;
                    [new,theType] = combine2ldb(r1a(n1),r2a(n2),in,im);
                    r2a(n2) = new;
                end
            end
        end
    end
    if ~combinedN1
        nrOfNewLdbs = nrOfNewLdbs + 1;
        newLdb(nrOfNewLdbs).x = [r1a(n1).x;r1a(n1).x(1)];
        newLdb(nrOfNewLdbs).y = [r1a(n1).y;r1a(n1).y(1)];
    end
end

for n2 = 1 : l2(1)
    if inUse(n2) 
        nrOfNewLdbs = nrOfNewLdbs + 1;
        newLdb(nrOfNewLdbs).x = [r2a(n2).x;r2a(n2).x(1)];
        newLdb(nrOfNewLdbs).y = [r2a(n2).y;r2a(n2).y(1)];
    end
end

newOut = newLdb;

end