Skip to content

Commit 1ed6f8c

Browse files
committed
Point projection solution at terminal points improved. (PolygonPath.m)
1 parent 4e0ba85 commit 1ed6f8c

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

src/PolygonPath.m

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,8 @@
717717

718718
% To find Q, two conditions must be satisfied:
719719
% https://de.wikipedia.org/wiki/Orthogonalprojektion
720-
% (1) Q = P0 + lambda * u, where u := P1-P0
721-
% (2) dot(Q-POI, u) = 0
720+
% (1) Q = P0 + lambda * u, where u := P1-P0
721+
% (2) dot(Q-POI, u) = 0
722722
% Inserting (1) into (2) yields
723723
% lambda = dot(POI - P0, u)/dot(u, u)
724724
P0 = [X(1:end-1), Y(1:end-1)];
@@ -732,8 +732,13 @@
732732
% compile-time assumption was vector(vector) indexing." for
733733
% 2-element paths (i.e. scalar lambdas).
734734
% idx = find((lambdas >= 0) & [lambdas(1:end-1) < 1; lambdas(end) <= 1]);
735-
lambdas(end) = lambdas(end) - eps(lambdas(end));
736-
idx = find((lambdas >= 0) & (lambdas < 1));
735+
if numel(lambdas) > 1
736+
lambdas(end) = lambdas(end) - eps(lambdas(end));
737+
end
738+
idx = find((lambdas(1:end-1) >= 0) & (lambdas(1:end-1) < 1));
739+
if (lambdas(end) >= 0) && (lambdas(end) <= 1)
740+
idx(end+1) = numel(lambdas);
741+
end
737742

738743
% For paths with 2 elements, find can return an array of size
739744
% 0-by-0 which would raise an error in BSXFUN. Avoid by

xUnitTests/PolygonPath/PointProjectionTest.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,36 @@ function testTerminalSolutions(testCase)
2929
testCase.verifySize(dphi, [1 1]);
3030
end%fcn
3131

32+
function testInitalSolution(testCase)
33+
% Test initial solutions with a 2-waypoint path
34+
35+
P0 = [0 0];
36+
P1 = [10 0];
37+
obj = PolygonPath.straight(P0, P1);
38+
39+
% Initial point solution
40+
[Q,idx,tau,dphi] = pointProjection(obj, P0 + [0 1], [], false);
41+
verifyEqual(testCase, Q, P0);
42+
verifyEqual(testCase, idx, 1);
43+
verifyEqual(testCase, tau, 0);
44+
verifySize(testCase, dphi, [1 1]);
45+
end%fcn
46+
47+
function testEndlSolution(testCase)
48+
% Test end solutions with a 2-waypoint path
49+
50+
P0 = [0 0];
51+
P1 = [10 0];
52+
obj = PolygonPath.straight(P0, P1);
53+
54+
% End point solution
55+
[Q,idx,tau,dphi] = pointProjection(obj, P1 + [0 1], [], false);
56+
verifyEqual(testCase, Q, P1, 'RelTol',1e-14);
57+
verifyEqual(testCase, idx, 1);
58+
verifyEqual(testCase, tau, 1, 'RelTol',3e-16);
59+
verifySize(testCase, dphi, [1 1]);
60+
end%fcn
61+
3262
function testMultipleSolutions(testCase)
3363
obj = PolygonPath.circle(3, [-pi pi]/2, 9);
3464
[Q,idx,tau,dphi] = pointProjection(obj, [0 0], [], false);

0 commit comments

Comments
 (0)