Skip to content

Commit 6096c49

Browse files
committed
Add method clear(); fixes.
1 parent 89ab926 commit 6096c49

File tree

4 files changed

+62
-7
lines changed

4 files changed

+62
-7
lines changed

src/Path2D.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
%
1313
% Path2D methods (modify path object):
1414
% append - Append paths.
15+
% clear - Clear path.
1516
% derivative - Derivative of path.
1617
% frenetOffset - Instantiate PolygonPath from given path and offsets.
1718
% restrict - Restrict path domain.
@@ -625,6 +626,11 @@
625626
% See also FRENET2CART, POINTPROJECTION.
626627
[sd,Q,idx,tau] = cart2frenet(obj, xy, phiMax)
627628

629+
% CLEAR Clear path.
630+
% OBJ = CLEAR(OBJ) clears all segments from the path so that you
631+
% are left with an empty path.
632+
obj = clear(obj)
633+
628634
% DERIVATIVE Derivative of path.
629635
% OBJD = DERIVATIVE(OBJ,N) returns the Nth derivative OBJD of the
630636
% path OBJ w.r.t. the path parameter.

src/PolygonPath.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
obj.ArcLengths = zeros(0,1);
7878
else
7979
obj.ArcLengths = cumsum(hypot(diff(x(:),1,1), diff(y(:),1,1)));
80-
end
80+
end
8181

8282
if nargin < 5
8383
obj = obj.setIsCircuit(1e-5);
@@ -152,6 +152,14 @@
152152

153153
end%fcn
154154

155+
function obj = clear(obj)
156+
obj.x = [];
157+
obj.y = [];
158+
obj.head = [];
159+
obj.curv = [];
160+
obj.ArcLengths = [];
161+
end%fcn
162+
155163
function obj = derivative(obj, n)
156164

157165
if nargin < 2
@@ -1158,6 +1166,11 @@ function write2file(obj, fn)
11581166
end%fcn
11591167

11601168
function obj = xy2Path(x, y)
1169+
1170+
% We cannot estimate heading/curvature from a single waypoint
1171+
assert(numel(x) ~= 1 && numel(y) ~= 1, ...
1172+
'XY2PATH:X and Y must not be scalars!')
1173+
11611174
[~,g1XY] = gradient([x(:) y(:)]);
11621175
[~,g2XY] = gradient(g1XY);
11631176
gx = g1XY(:,1);

src/SplinePath.m

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,16 @@
7575

7676
function obj = append(obj, obj2)
7777

78+
if obj.isempty()
79+
ds = 0;
80+
else
81+
[~,P1] = obj.termPoints();
82+
dP = obj2.termPoints() - P1;
83+
ds = hypot(dP(1), dP(2)) + obj.length();
84+
end
85+
7886
breaks = obj.Breaks;
7987
coefs = obj.Coefs;
80-
arcLen = obj.ArcLengths;
81-
[~,P1] = obj.termPoints();
8288
breaks2 = obj2.Breaks;
8389
coefs2 = obj2.Coefs;
8490

@@ -90,9 +96,7 @@
9096
cat(3, zeros(2,n1,k2-k1), coefs), ...
9197
cat(3, zeros(2,n2,k1-k2), coefs2));
9298

93-
dP = obj2.termPoints() - P1;
94-
ds = hypot(dP(1), dP(2));
95-
obj.ArcLengths = [arcLen; obj2.ArcLengths + arcLen(end) + ds];
99+
obj.ArcLengths = [obj.ArcLengths; obj2.ArcLengths + ds];
96100

97101
end%fcn
98102

@@ -143,6 +147,13 @@
143147

144148
end%fcn
145149

150+
function obj = clear(obj)
151+
obj.Breaks = 0;
152+
obj.Coefs(:,1:end,:) = [];
153+
obj.ArcLengths = zeros(0,1);
154+
obj.IsCircuit = false;
155+
end%fcn
156+
146157
function obj = derivative(obj, n)
147158
%DERIVATIVE Derivative of path.
148159
% OBJ = DERIVATIVE(OBJ) returns the derivative of path OBJ.

xUnitTests/SplinePath/AppendTestSpline.m

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
classdef AppendTestSpline < matlab.unittest.TestCase
22

3+
properties (TestParameter)
4+
PathEmpty = {SplinePath()}
5+
PathStraight = {SplinePath.straight([1 2], [5 4])}
6+
end
7+
8+
9+
310
methods (Test)
4-
function testAppend(testCase)
11+
function testAppendNonEmpty(testCase)
512

613
% Create two "arbitrary" single segment paths with different
714
% polynomial degrees
@@ -34,6 +41,24 @@ function testAppend(testCase)
3441
% The path lengths must match
3542
verifyEqual(testCase, obj.length(), obj0.length() + obj1.length())
3643
end%fcn
44+
45+
function testAppendToEmptyPath(testCase, PathEmpty, PathStraight)
46+
% Append non-empty path to empty path.
47+
48+
obj = PathEmpty.append(PathStraight);
49+
verifyEqual(testCase, obj.Breaks, PathStraight.Breaks)
50+
verifyEqual(testCase, obj.Coefs(:,:,end-1:end), PathStraight.Coefs)
51+
verifyEqual(testCase, obj.cumlengths(), PathStraight.cumlengths())
52+
end%fcn
53+
54+
function testAppendEmptyPath(testCase, PathEmpty, PathStraight)
55+
% Append empty path to non-empty path.
56+
57+
obj = PathStraight.append(PathEmpty);
58+
verifyEqual(testCase, obj.Breaks, PathStraight.Breaks)
59+
verifyEqual(testCase, obj.Coefs(:,:,end-1:end), PathStraight.Coefs)
60+
verifyEqual(testCase, obj.cumlengths(), PathStraight.cumlengths())
61+
end%fcn
3762
end
3863

3964
end%class

0 commit comments

Comments
 (0)