-
Notifications
You must be signed in to change notification settings - Fork 14
/
polylinelen.m
71 lines (66 loc) · 1.93 KB
/
polylinelen.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
function [len, node, inputreversed]=polylinelen(node, p0, p1, pmid)
%
% [len, node, inputreversed]=polylinelen(node, p0, p1, pmid)
%
% Calculate the polyline line segment length vector in sequential order
%
% author: Qianqian Fang (q.fang at neu.edu)
%
% input:
% node: an N x 3 array defining each vertex of the polyline in
% sequential order
% p0:(optional) a given node to define the start of the polyline, if not
% defined, start position is assumed to be 1st node
% p1:(optional) a given node to define the end of the polyline, if not
% defined, end position is assumed to be last node
% pmid:(optional) a given node sits between p0 and p1, if not
% defined, index of the middle (floored) node is used
%
% output:
% len: the length of each segment between the start and the end points
% node: the node list between the start and end points of the polyline
% inputreversed: if 1, the input node is reversed from p0 to pmid to p1
%
%
% -- this function is part of brain2mesh toolbox (http://mcx.space/brain2mesh)
% License: GPL v3 or later, see LICENSE.txt for details
%
if(nargin<3)
p1=size(node,1);
if(nargin<2)
p0=1;
end
end
if(nargin<4)
pmid=floor((p0+p1)*0.5);
end
if(size(p0,2)==3)
p0=closestnode(node,p0);
end
if(size(p1,2)==3)
p1=closestnode(node,p1);
end
if(size(pmid,2)==3)
pmid=closestnode(node,pmid);
end
if(p0<pmid && pmid<p1)
inputreversed=0;
node=node(p0:p1,:);
elseif(p0<pmid && p1<pmid)
inputreversed=(min(p0,p1) == p0);
node=node([min(p0,p1):-1:1 end:-1:max(p0,p1)],:);
if(~inputreversed)
node=flipud(node);
end
elseif(p0>pmid && pmid>p1)
inputreversed=1;
node=node(p0:-1:p1,:);
elseif(p0>pmid && p1>pmid)
inputreversed=(max(p0,p1) == p1);
node=node([max(p0,p1):end 1:min(p0,p1)],:);
if(inputreversed)
node=flipud(node);
end
end
len=node(1:end-1,:) - node(2:end,:);
len=sqrt(sum(len.*len,2));