0001 function distanceVector = computeDistanceFromHrep2D(hrep, vrep, pointMatrix)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 if isempty(hrep) || isempty(hrep.A)
0024 [tmp distanceVector] = convertCartToPolar(pointMatrix(1,:), pointMatrix(2,:));
0025 return
0026 end
0027
0028 nEdge = size(hrep.A, 1);
0029
0030 distanceMatrix = bsxfun(@minus, hrep.A * pointMatrix, hrep.h);
0031 [distanceVector edgeIndexVector] = max(distanceMatrix, [], 1);
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 [edgeAngleVector ~] = convertCartToPolar(hrep.A(:,1)', hrep.A(:,2)');
0061 [tmp sortedEdgeIndexVector] = sort(edgeAngleVector, 2, 'descend');
0062
0063
0064 incidenceMatrix = abs(bsxfun(@minus, hrep.A * vrep.V', hrep.h)) < elkZerotol;
0065 clockwiseVertexMatrix = nan(2, length(edgeAngleVector));
0066 for iEdge = 1:nEdge
0067 thisEdgeIndex = sortedEdgeIndexVector(iEdge);
0068 nextEdgeIndex = sortedEdgeIndexVector(mod(iEdge, nEdge) + 1);
0069 thisVertexIndex = find(incidenceMatrix(thisEdgeIndex, :));
0070 if incidenceMatrix(nextEdgeIndex, thisVertexIndex(1))
0071 clockwiseVertexMatrix(:, thisEdgeIndex) = vrep.V(thisVertexIndex(1), :)';
0072 elseif incidenceMatrix(nextEdgeIndex, thisVertexIndex(2))
0073 clockwiseVertexMatrix(:, thisEdgeIndex) = vrep.V(thisVertexIndex(2), :)';
0074 else
0075 error('elk:boundary:internalError', ['Subsequent vertex not found.' ...
0076 'This should not happen.']);
0077 end
0078 end
0079 counterclockwiseVertexMatrix = circshift(clockwiseVertexMatrix, [0 -1]);
0080
0081
0082 normalMatrix = hrep.A';
0083 clockwiseNormalMatrix = [0 1;-1 0] * normalMatrix;
0084 counterclockwiseNormalMatrix = -1*clockwiseNormalMatrix;
0085 clockwiseDistanceVector = sum(...
0086 clockwiseNormalMatrix .* clockwiseVertexMatrix, 1);
0087 counterclockwiseDistanceVector = sum(...
0088 counterclockwiseNormalMatrix .* counterclockwiseVertexMatrix, 1);
0089
0090 for iSample = find(distanceVector > 0)
0091 if clockwiseNormalMatrix(:, edgeIndexVector(iSample))'...
0092 *pointMatrix(:, iSample) > clockwiseDistanceVector(edgeIndexVector(iSample))'
0093
0094 distanceVector(iSample) = norm(pointMatrix(:, iSample) - ...
0095 clockwiseVertexMatrix(:, edgeIndexVector(iSample)));
0096 elseif counterclockwiseNormalMatrix(:, edgeIndexVector(iSample))'...
0097 *pointMatrix(:, iSample) > counterclockwiseDistanceVector(edgeIndexVector(iSample))'
0098
0099 distanceVector(iSample) = norm(pointMatrix(:, iSample) - ...
0100 counterclockwiseVertexMatrix(:, edgeIndexVector(iSample)));
0101 else
0102
0103 end
0104 end