-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurvspace_app.m
105 lines (86 loc) · 2.7 KB
/
curvspace_app.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
function q = curvspace_app(app,p,N)
%% initial settings %%
currentpt = p(1,:); % current point
indfirst = 2; % index of the most closest point in p from curpt
len = size(p,1); % length of p
q = currentpt; % output point
k = 0;
%% distance between points in p %%
dist_bet_pts=NaN(len-1,1);
for k0 = 1:len-1
dist_bet_pts(k0) = distance_app(app,p(k0,:),p(k0+1,:));
end
totaldist = nansum(dist_bet_pts);
%% interval %%
intv = totaldist./(N-1);
%% iteration %%
for k = 1:N-1
newpt = []; distsum = 0;
ptnow = currentpt;
kk = 0;
pttarget = p(indfirst,:);
remainder = intv; % remainder of distance that should be accumulated
while isempty(newpt)
% calculate the distance from active point to the most
% closest point in p
disttmp = distance_app(app,ptnow,pttarget);
distsum = distsum + disttmp;
% if distance is enough, generate newpt. else, accumulate
% distance
if distsum >= intv
newpt = interpintv_app(app,ptnow,pttarget,remainder);
else
remainder = remainder - disttmp;
ptnow = pttarget;
kk = kk + 1;
if indfirst+kk > len
newpt = p(len,:);
else
pttarget = p(indfirst+kk,:);
end
end
end
% add to the output points
q = [q; newpt];
% update currentpt and indfirst
currentpt = newpt;
indfirst = indfirst + kk;
end
%%%%%%%%%%%%%%%%%%%%%%%%%
%% SUBFUNCTIONS %%
%%%%%%%%%%%%%%%%%%%%%%%%%
function l = distance_app(app,x,y)
% DISTANCE Calculate the distance.
% distance_app(app,X,Y) calculates the distance between two
% points X and Y. X should be a 1 x 2 (2D) or a 1 x 3 (3D)
% vector. Y should be n x 2 matrix (for 2D), or n x 3 matrix
% (for 3D), where n is the number of points. When n > 1,
% distance between X and all the points in Y are returned.
%
% (Example)
% x = [1 1 1];
% y = [1+sqrt(3) 2 1];
% l = distance_app(app,x,y)
%
% 11 Mar 2005, Yo Fukushima
%% calculate distance %%
if size(x,2) == 2
l = sqrt((x(1)-y(:,1)).^2+(x(2)-y(:,2)).^2);
elseif size(x,2) == 3
l = sqrt((x(1)-y(:,1)).^2+(x(2)-y(:,2)).^2+(x(3)-y(:,3)).^2);
else
error('Number of dimensions should be 2 or 3.');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function newpt = interpintv_app(app,pt1,pt2,intv)
% Generate a point between pt1 and pt2 in such a way that
% the distance between pt1 and new point is intv.
% pt1 and pt2 should be 1x3 or 1x2 vector.
dirvec = pt2 - pt1;
dirvec = dirvec./norm(dirvec);
l = dirvec(1); m = dirvec(2);
newpt = [intv*l+pt1(1),intv*m+pt1(2)];
if length(pt1) == 3
n = dirvec(3);
newpt = [newpt,intv*n+pt1(3)];
end