-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplinepwp.m
199 lines (174 loc) · 6.07 KB
/
splinepwp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
classdef splinepwp < closedcurve
% SPLINEPWP is a periodic piecewise spline with corners.
%
% G = splinepwp(knots, corners)
% The corners array is a set of indicies of the knots array which dictates
% which knots in the list will be corners. The array corners should be
% strictly monotonically increasing with
% 1 <= corners(1) and corners(end) <= numel(knots).
% If corners(1) ~= 1, then the knots and corners arrays will be rearranged
% so that this is the case.
%
% Currently the corner angles are determined strictly by the angles of
% neighboring knots, by pretending we are making a polygon with the knots
% as vertices.
%
% This class is mainly for testing corner cases on boundaries that aren't
% polygons. For instance it is completely arbitrary that the value of the
% tangent at a corner is the average of the incoming and outgoing tangent
% vectors. In fact to create the splines between corners, the endpoint
% tangent vectors arbitrarily have a magnitude of 5, which was selected
% because it gives the "nicest, natural" distribution of points for an
% evenly spaced discrete parameterization.
% Copyright 2015 the ConfMapTk project developers. See the COPYRIGHT
% file at the top-level directory of this distribution and at
% https://github.com/ehkropf/ConfMapTk.
%
% This file is part of ConfMapTk. It is subject to the license terms in
% the LICENSE file found in the top-level directory of this distribution
% and at https://github.com/ehkropf/ConfMapTk. No part of ConfMapTk,
% including this file, may be copied, modified, propagated, or distributed
% except according to the terms contained in the LICENSE file.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE
% file.
properties(SetAccess=protected)
knots % Complex array of spline knots.
corners % Vector of index values for knots indicating corners.
alpha % Array of calculated corner interior angles.
tknots % Parameter values for knots.
cornerTangent % Incoming/outgoing corner tangent vectors.
end
properties(Dependent)
breaks
end
properties(Access=protected)
ppx
ppy
end
methods
function G = splinepwp(knots, corners)
% Constructor.
if ~nargin
return
end
% Check input.
corners = corners(:);
knots = knots(:);
if knots(1) == knots(end)
knots = knots(1:end-1);
end
if any(diff(corners) <= 0)
error('CMT:BadArguemnt', ...
'The corners array must be strictly monotonically increasing.')
end
try
knots(corners);
catch err
if strcmp(err.identifier, 'MATLAB:badsubscript')
error('CMT:BadArgument', ...
'The corners array must be a valid set of indices for knots.')
else
rethrow(err)
end
end
% Constructor work.
G.knots = knots;
G.corners = corners;
G = calculateSplines(G);
end
function tk = get.breaks(G)
tk = [G.tknots(G.corners); 1];
end
function z = point(G, t)
% Point on curve at t.
z = paramEval(G, t, 0);
end
function zt = tangent(G, t)
% Tangent to curve at t. With average corner tangents.
zt = paramEval(G, t, 1);
cavg = sum(G.cornerTangent, 2)/2;
ct = G.breaks;
for k = 1:numel(ct)-1
ctL = abs(t - ct(k)) < 10*eps;
zt(ctL) = cavg(k);
end
end
end
methods(Access=protected)
function G = calculateSplines(G)
v = G.knots;
cv = G.corners;
if cv(1) ~= 1
% Shift corners and knots.
shift = cv(1) - 1;
v = circshift(v, -shift);
cv = cv - shift;
end
% Pseudo arc length parameterization via chordal arclengths.
nv = numel(v);
v = [v; v(1)];
dL = abs(diff(v));
t = [0; cumsum(dL)];
t = t/t(end);
% Corner angles determined by neighboring knots.
pv = mod(cv - 2, nv) + 1;
fv = mod(cv, nv) + 1;
alpha_ = mod(angle((v(pv) - v(cv))./(v(fv) - v(cv)))/pi, 2);
% Outgoing/incoming tangent vectors.
vtan = [ v(fv) - v(cv), v(cv) - v(pv) ];
mu = 5; % (arbitrary) tangent vector magnitude
vtan = mu*vtan./abs(vtan);
% Compute piecewise polynomial splines for segments between corners.
ncv = numel(cv);
ppx_(ncv, 3) = mkpp([1 1], [1 1]);
ppy_ = ppx_;
for k = 1:ncv
if k + 1 <= ncv
vdx = cv(k):cv(k+1);
else
vdx = cv(k):nv+1;
end
vex = [vtan(k,1); v(vdx); vtan(mod(k, ncv)+1,2)];
tk = t(vdx);
% Piecewise polynomials and derivatives.
ppx_(k,1) = spline(tk, real(vex));
ppx_(k,2:3) = splinepwp.ppderivs(ppx_(k,1));
ppy_(k,1) = spline(tk, imag(vex));
ppy_(k,2:3) = splinepwp.ppderivs(ppy_(k,1));
end
G.knots = v(1:end-1);
G.corners = cv;
G.alpha = alpha_;
G.ppx = ppx_;
G.ppy = ppy_;
G.tknots = t;
G.cornerTangent = vtan;
end
function z = paramEval(G, t, d)
% Evaluate curve given parameter using derivative level d.
if nargin < 3
% No derivative.
d = 0;
end
d = d + 1;
t = modparam(G, t);
z = nan(size(t));
brks = G.breaks;
for k = 1:numel(G.corners)
tL = brks(k) <= t & t < brks(k+1);
z(tL) = complex(ppval(G.ppx(k,d), t(tL)), ppval(G.ppy(k,d), t(tL)));
end
end
end
methods(Static, Access=private)
function ppd = ppderivs(pp)
% First and second derivatives of piecewise polynomial pp.
coef = pp.coefs;
ppd = [ mkpp(pp.breaks, [3*coef(:,1), 2*coef(:,2), coef(:,3)]), ...
mkpp(pp.breaks, [6*coef(:,1), 2*coef(:,2)]) ];
end
end
end