Skip to content

Commit 6bbf48b

Browse files
committed
Add method simplify() for PolygonPath.
1 parent d6b59dc commit 6bbf48b

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

PolygonPath.m

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
% perpendicularDistance - Distance of path waypoints to line.
2222
% rdp - Ramer-Douglas-Peucker point reduction.
2323
% rdpIter - Iterative Ramer-Douglas-Peucker line simplification.
24+
% simplify - Simplify path.
2425
% write2file - Write path to file.
2526
% See superclass for more methods.
2627
%
@@ -878,6 +879,22 @@
878879

879880
end%fcn
880881

882+
function obj = simplify(obj)
883+
%SIMPLIFY Simplify path.
884+
% OBJ = SIMPLIFY(OBJ) removes intermediate points from line
885+
% segments of the path OBJ.
886+
887+
h = atan2(diff(obj.y), diff(obj.x));
888+
dh = [true; diff(h); true];
889+
keep = (dh ~= 0);
890+
891+
obj.x = obj.x(keep);
892+
obj.y = obj.y(keep);
893+
obj.head = obj.head(keep);
894+
obj.curv = obj.curv(keep);
895+
obj.ArcLengths = obj.ArcLengths(keep(2:end));
896+
end%fcn
897+
881898
function [tau,idx] = s2tau(obj, s)
882899

883900
sObj = obj.arcLengths0();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<Info>
3+
<Category UUID="FileClassCategory">
4+
<Label UUID="test" />
5+
</Category>
6+
</Info>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
classdef SimplifyTest < matlab.unittest.TestCase
2+
3+
methods (Test)
4+
function testLine(testCase)
5+
% Test simplification of a straight path.
6+
7+
% Create a line with intermediate points
8+
obj0 = PolygonPath.xy2Path(0:10, 0:10);
9+
10+
obj1 = obj0.simplify();
11+
12+
% The terminal points must match
13+
[exp0,exp1] = obj0.termPoints();
14+
[act0,act1] = obj1.termPoints();
15+
verifyEqual(testCase, act0, exp0);
16+
verifyEqual(testCase, act1, exp1);
17+
18+
% The length must match
19+
verifyEqual(testCase, obj1.length(), obj0.length())
20+
21+
% Exactly two points, i.e. one segment, must survive
22+
verifyEqual(testCase, obj1.numel(), 1)
23+
end%fcn
24+
25+
function testNothingToRemove(testCase)
26+
% Test path which cannot be simplified.
27+
28+
% Create a path where each line segment contains only two
29+
% points -> no simplification possible
30+
obj0 = PolygonPath.xy2Path(...
31+
[0 1 1 2 2 3 3 4 4], ...
32+
[0 0 1 1 2 2 3 3 4]);
33+
34+
verifyEqual(testCase, obj0, obj0.simplify())
35+
end%fcn
36+
end
37+
38+
end%class

0 commit comments

Comments
 (0)