forked from fastalgorithms/rounding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smthpoly.m
93 lines (75 loc) · 2.41 KB
/
smthpoly.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
%
% (c) 2016 Charles L. Epstein and Michael O'Neil
%
% cle@math.upenn.edu
% oneil@cims.nyu.edu
%
% See the corresponding paper for technical information:
%
% C. L. Epstein and M. O'Neil, "Smoothed corners and scattered
% waves", arXiv:1506.08449, 2016.
%
function [psamp,ntot] = smthpoly(v,he,ke,me)
%
% This function smooths a polygon
%
% Input variables
% v are the vertices
% he, ke are smoothing parameters
% 2*me is the number of sample points are each smoothed vertex
%
% Output variables
% psamp are sample points lying on the smoothed polygon
% ntot = sz(psamp)
%
% How many vertices.
nv = sz(v);
% We now set up the local geometries for the edges
for j = 1:nv
j1 = 1+mod(j,nv);
j0 =1+mod(j-2,nv);
X(j,1:2) = (v(j1,1:2)-v(j,1:2));
X(j,1:2)=X(j,1:2)/sqrt(X(j,1:2)*X(j,1:2)');
Y(j,1:2) = (v(j0,1:2)-v(j,1:2));
Y(j,1:2)=Y(j,1:2)/sqrt(Y(j,1:2)*Y(j,1:2)');
end
% We get samples of the function used to smooth the vertices
% We use the part with indices from j0 to j1+1
[as0,j0,j1]= smthabv(he,ke,2*me);
as = as0(j0:j1+1);
s0 = (0:4*me-1)/(2*me)-1;
s = s0(j0:j1+1);
x = as+s;
y = as-s;
% The number of points on the smoothed edges.
npte = j1-j0+2;
% Find the intersections of the smoothed edges near to the vertex
% A tolerance for calculations
% The number of sample points on all the curved parts.
npt1 = nv*npte;
nx2 = ceil(log2(npt1));
nflp = nx2;
% Loop over vertices
for q2 = 1:nv
psamp(1+(q2-1)*(npte+nflp):q2*npte+(q2-1)*nflp,1:2) = ...
ones(npte,1)*v(q2,1:2)+(x(1:npte))'*X(q2,1:2) ...
+(y(1:npte))'*Y(q2,1:2);
% The last point on the current edge
Z1 = v(q2,1:2)+x(npte)*X(q2,1:2)+y(npte)*Y(q2,1:2);
% Next q2 index in cyclic order
q2n=1+mod(q2,nv);
% The first point on the next edge
Z2 = v(q2n,1:2)+x(1)*X(q2n,1:2)+y(1)*Y(q2n,1:2);
% Test code:
Zdif = Z2-Z1;
nZ =sqrt(Zdif*Zdif');
% Construct the straight segment joining the two curved ones
psamp(q2*npte+(q2-1)*nflp+1:q2*(npte+nflp),1:2) = ...
ones(nflp,1)*Z1(1:2)+((1:nflp)'/(1+nflp))*Zdif(1:2);
end
ntot = sz(psamp);
psamp(ntot+1,1:2)=psamp(1,1:2);
ntot = ntot+1;
% To get a closed curve
%psamp(ntot+1)=psamp(1);
end