-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPosition_Convexhull.m
More file actions
63 lines (61 loc) · 2.15 KB
/
Position_Convexhull.m
File metadata and controls
63 lines (61 loc) · 2.15 KB
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
function [ outPosConvexMat ] = Position_Convexhull( Trajectory16ind )
%POSITION_CONVEXHULL Summary of this function goes here
% Detailed explanation goes here
TraX=Trajectory16ind{1,1};
TraY=Trajectory16ind{1,2};
[N,Length]=size(TraX);
outPosConvexMat=zeros(N,Length);
PointMat = [TraX(:,1),TraY(:,1)];
PointMat=PointMat(~isnan(TraX(:,1)),:);
PointMat=double(PointMat);
if(~isempty(PointMat))
tri = delaunayn(PointMat); % Generate delaunay triangulization
end
for i=2:Length
QueryPoints = double([TraX(:,i),TraY(:,i)]);
prevPoints=double([TraX(:,i-1),TraY(:,i-1)]);
dirVecs = QueryPoints- prevPoints;
for k=1:size(dirVecs,1)
dirVecs(k,:)=dirVecs(k,:)/norm(dirVecs(k,:));
end
groupDirVec=nanmean(dirVecs);
groupprevPoint=nanmean(prevPoints);
parfor k=1:size(dirVecs,1)
indDirVecs(k,:)=QueryPoints(k,:)-groupprevPoint;
indDirVecs(k,:)=indDirVecs(k,:)/norm(indDirVecs(k,:));
diffDegreeVec(k)= acos(dot(indDirVecs(k,:),groupDirVec))*180/pi;
end
headGroup = diffDegreeVec';
headGroup(headGroup<=90)=1;
headGroup(headGroup>90)=-1;
% compare with the previous group centroid
if sum(isnan(QueryPoints(:,1))) == N
tri = [];
continue;
end
if ~isempty(tri)
tn = tsearchn(PointMat, tri, QueryPoints); % Determine which triangle point is within
inConvexInx = tn; % Convert to logical vector
outConvexInx=~isnan(QueryPoints(:,1));
inConvexInx(isnan(inConvexInx))=false;
inConvexInx=logical(inConvexInx);
outConvexInx=outConvexInx & ~ inConvexInx;
outPosConvexMat(:,i)=outConvexInx.*headGroup;
%--- clf
% plot(QueryPoints(~outConvexInx),'r+') % points inside
% hold on
% plot(QueryPoints(outConvexInx.*headGroup==-1),'bo') % points outside
% plot(QueryPoints(outConvexInx.*headGroup==1),'go') % points outside
end
%----------
PointMat=QueryPoints;
PointMat=double(PointMat);
PointMat=PointMat(~isnan(TraX(:,i)),:);
if(~isempty(PointMat)) && size(PointMat,1)>2
tri = delaunayn(PointMat); % Generate delaunay triangulization
else
tri=[];
end
i
end
end