-
Notifications
You must be signed in to change notification settings - Fork 0
/
trajectory_segmentation_constant_len.m
55 lines (51 loc) · 1.86 KB
/
trajectory_segmentation_constant_len.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
function segments = trajectory_segmentation_constant_len( traj, lseg, ovlp)
%SEGMENT_TRAJECTORY Splits the trajectory in segments of length
% lseg with an overlap of ovlp %
% Returns an array of instances of the same trajectory class (now repesenting segments)
n = size(traj.points, 1);
% compute cumulative distance vector
cumdist = zeros(1, n);
for i = 2:n
cumdist(i) = cumdist(i - 1) + norm( traj.points(i, 2:3) - traj.points(i - 1, 2:3) );
end
% step size
off = lseg*(1. - ovlp);
% total number of segments - at least 1
if cumdist(end) > lseg
nseg = ceil((cumdist(end) - lseg) / off) + 1;
off = off + (cumdist(end) - lseg - off*(nseg - 1))/nseg;
else
nseg = 1;
end
% segments are trajectories again -> construct empty object
segments = trajectories([]);
for seg = 0:(nseg - 1)
starti = 0;
seg_off = 0;
pts = [];
if nseg == 1
% special case: only 1 segment, don't discard it
pts = traj.points;
else
for i = 1:n
if cumdist(i) >= seg*off
if starti == 0
starti = i;
end
if cumdist(i) > (seg*off + lseg)
% done we are
break;
end
if isempty(pts)
seg_off = cumdist(i);
end
% otherwise append point to segment
pts = [pts; traj.points(i, :)];
end
end
end
segments = segments.append(...
trajectory(pts, traj.set, traj.track, traj.group, traj.id, traj.trial, traj.session, seg + 1, seg_off, starti, traj.trial_type) ...
);
end
end