0001 function newM = restoreBoundary(pointMatrix)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 distanceMatrix = max(...
0030 abs(pointMatrix(1, 1:end) - pointMatrix(1, [2:end 1])), ...
0031 abs(pointMatrix(2, 1:end) - pointMatrix(2, [2:end 1])));
0032 insertIndexVector = find(distanceMatrix > 1);
0033
0034
0035 fillIn = cell(1, length(insertIndexVector));
0036 nAdd = 0;
0037 for iInsert = 1:length(insertIndexVector);
0038 thisIndex = insertIndexVector(iInsert);
0039 nextIndex = mod(thisIndex, size(pointMatrix, 2)) + 1;
0040 xDist = pointMatrix(1, thisIndex) - pointMatrix(1, nextIndex);
0041 yDist = pointMatrix(2, thisIndex) - pointMatrix(2, nextIndex);
0042
0043
0044 if yDist < 0
0045 yFill = (pointMatrix(2, thisIndex)+1):...
0046 (pointMatrix(2, nextIndex)-1);
0047 elseif yDist > 0
0048 yFill = (pointMatrix(2, thisIndex)-1):-1:...
0049 (pointMatrix(2, nextIndex)+1);
0050 end
0051
0052 if xDist < 0
0053 xFill = (pointMatrix(1, thisIndex)+1):...
0054 (pointMatrix(1, nextIndex)-1);
0055 elseif xDist > 0
0056 xFill = (pointMatrix(1, thisIndex)-1):-1:...
0057 (pointMatrix(1, nextIndex)+1);
0058 end
0059
0060 if xDist == 0
0061
0062 dbgType(iInsert) = 'v';
0063 dbgCount(iInsert) = abs(yDist)-1;
0064 nAdd = nAdd + abs(yDist)-1;
0065 fillIn{iInsert} = [yFill*0 + pointMatrix(1, thisIndex); ...
0066 yFill];
0067
0068 elseif yDist == 0
0069
0070 dbgType(iInsert) = 'h';
0071 dbgCount(iInsert) = abs(xDist)-1;
0072 nAdd = nAdd + abs(xDist)-1;
0073 fillIn{iInsert} = [xFill; ...
0074 xFill*0 + pointMatrix(2, thisIndex)];
0075 elseif abs(xDist) == abs(yDist)
0076
0077 dbgType(iInsert) = 'd';
0078 dbgCount(iInsert) = abs(xDist)-1;
0079 nAdd = nAdd + abs(xDist)-1;
0080 fillIn{iInsert} = [xFill; yFill];
0081
0082
0083
0084 else
0085
0086 error('elk:boundary:restoreBoundary', ['The input points are not ' ...
0087 'continuously neighboured OR have only gaps in diagonal, ' ...
0088 'horizontal and vertical direction']);
0089 end
0090 end
0091
0092
0093 newM = nan(size(pointMatrix) + [0 nAdd]);
0094 newM(:, 1:insertIndexVector(1)) = pointMatrix(:, 1:insertIndexVector(1));
0095 thisNewIndex = insertIndexVector(1) + 1;
0096
0097 for iInsert = 1:length(insertIndexVector)
0098
0099 nAdd = size(fillIn{iInsert}, 2);
0100 newM(:, (thisNewIndex-1) + (1:nAdd)) = fillIn{iInsert};
0101 thisNewIndex = thisNewIndex + nAdd;
0102
0103 if iInsert < length(insertIndexVector)
0104 nAdd = insertIndexVector(iInsert + 1) - insertIndexVector(iInsert);
0105 else
0106 nAdd = size(pointMatrix, 2) - insertIndexVector(iInsert);
0107 end
0108 newM(:, (thisNewIndex-1) + (1:nAdd)) = ...
0109 pointMatrix(:, insertIndexVector(iInsert) + (1:nAdd));
0110 thisNewIndex = thisNewIndex + nAdd;
0111 end
0112
0113 if any(isnan(newM(:)))
0114 error('elk:boundary:internalError', 'This should not happen.');
0115 end
0116
0117 newM = unique(newM', 'rows')';