-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathelastic_distance_curve.m
70 lines (64 loc) · 1.83 KB
/
elastic_distance_curve.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
function [dy, dx] = elastic_distance_curve(beta1, beta2, closed, rotation, scale, method)
% ELASTIC_DISTANCE_CURVE Calculates the two elastic distances between two
% curves
% -------------------------------------------------------------------------
% This functions calculates the distances between curves,
% \eqn{D_y} and \eqn{D_x}, where curve 1 is aligned to curve 2
%
% Usage: [dy, dx] = elastic_distance_curve(beta1, beta2)
% [dy, dx] = elastic_distance_curve(beta1, beta2, closed)
%
% Input:
% beta1: sample curve 1 (nxN matrix) n is dimension and N is number of
% amples
% beta2: sample curve 1
% closed: boolean if curve is closed (default = false)
% rotation: compute optimal rotation (default = true)
% scale: include scale
% method: controls which optimization method (default="DP") options are
% Dynamic Programming ("DP") and Riemannian BFGS
% ("RBFGSM")
%
% Output
% dy: shape distance
% dx: phase distance
if nargin < 3
closed = false;
rotation = true;
scale = false;
method = 'DP';
elseif nargin < 4
rotation = true;
scale = false;
method = 'DP';
elseif nargin < 5
scale = false;
method = 'DP';
elseif nargin < 6
method = 'DP';
end
N = size(beta1,2);
a=-calculateCentroid(beta1);
beta1 = beta1 + repmat(a,1,N);
a=-calculateCentroid(beta2);
beta2 = beta2 + repmat(a,1,N);
[q1, ~, lenq1] = curve_to_q(beta1);
[~, ~, lenq2] = curve_to_q(beta2);
% Compute shooting vector from mu to q_i
[~,qn_t,~,gam] = Find_Rotation_and_Seed_coord(beta1,beta2,true,rotation,closed,method);
q1dotq2=InnerProd_Q(q1,qn_t);
% Compute shooting vector
if q1dotq2>1
q1dotq2=1;
elseif q1dotq2<-1
q1dotq2=-1;
end
if scale
dy = sqrt(acos(q1dotq2)^2+log(lenq1/lenq2)^2);
else
dy = acos(q1dotq2);
end
time1 = linspace(0,1,N);
binsize = mean(diff(time1));
psi = sqrt(gradient(gam,binsize));
dx = acos(trapz(time1,psi));