-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresample_swc.cpp
More file actions
135 lines (124 loc) · 3.81 KB
/
Copy pathresample_swc.cpp
File metadata and controls
135 lines (124 loc) · 3.81 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "resample_swc.h"
#define DISTP(a,b) sqrt(((a)->x-(b)->x)*((a)->x-(b)->x)+((a)->y-(b)->y)*((a)->y-(b)->y)+((a)->z-(b)->z)*((a)->z-(b)->z))
void resample_path(Segment * seg, double step)
{
char c;
Segment seg_r;
double path_length = 0;
Point* start = seg->at(0);
Point* seg_par = seg->back()->p;
V3DLONG iter_old = 0;
seg_r.push_back(start);
while (iter_old < seg->size() && start && start->p)
{
path_length += DISTP(start,start->p);
if (path_length<=seg_r.size()*step)
{
start = start->p;
iter_old++;
}
else//a new point should be created
{
path_length -= DISTP(start,start->p);
Point* pt = new Point;
double rate = (seg_r.size()*step-path_length)/(DISTP(start,start->p));
pt->x = start->x + rate*(start->p->x-start->x);
pt->y = start->y + rate*(start->p->y-start->y);
pt->z = start->z + rate*(start->p->z-start->z);
pt->r = start->r*(1-rate) + start->p->r*rate;//intepolate the radius
pt->p = start->p;
if (rate<0.5) pt->type = start->type;
else pt->type = start->p->type;
seg_r.back()->p = pt;
seg_r.push_back(pt);
path_length += DISTP(start,pt);
start = pt;
}
}
seg_r.back()->p = seg_par;
for (V3DLONG i=0;i<seg->size();i++)
if (!seg->at(i)) {delete seg->at(i); seg->at(i) = NULL;}
*seg = seg_r;
}
NeuronTree resample(NeuronTree input, double step)
{
NeuronTree result;
V3DLONG siz = input.listNeuron.size();
Tree tree;
for (V3DLONG i=0;i<siz;i++)
{
NeuronSWC s = input.listNeuron[i];
Point* pt = new Point;
pt->x = s.x;
pt->y = s.y;
pt->z = s.z;
pt->r = s.r;
pt ->type = s.type;
pt->p = NULL;
pt->childNum = 0;
tree.push_back(pt);
}
for (V3DLONG i=0;i<siz;i++)
{
if (input.listNeuron[i].pn<0) continue;
V3DLONG pid = input.hashNeuron.value(input.listNeuron[i].pn);
tree[i]->p = tree[pid];
tree[pid]->childNum++;
}
// printf("tree constructed.\n");
vector<Segment*> seg_list;
for (V3DLONG i=0;i<siz;i++)
{
if (tree[i]->childNum!=1)//tip or branch point
{
Segment* seg = new Segment;
Point* cur = tree[i];
do
{
seg->push_back(cur);
cur = cur->p;
}
while(cur && cur->childNum==1);
seg_list.push_back(seg);
}
}
// printf("segment list constructed.\n");
for (V3DLONG i=0;i<seg_list.size();i++)
{
resample_path(seg_list[i], step);
}
// printf("resample done.\n");
tree.clear();
map<Point*, V3DLONG> index_map;
for (V3DLONG i=0;i<seg_list.size();i++)
for (V3DLONG j=0;j<seg_list[i]->size();j++)
{
tree.push_back(seg_list[i]->at(j));
index_map.insert(pair<Point*, V3DLONG>(seg_list[i]->at(j), tree.size()-1));
}
for (V3DLONG i=0;i<tree.size();i++)
{
NeuronSWC S;
Point* p = tree[i];
S.n = i+1;
if (p->p==NULL) S.pn = -1;
else
S.pn = index_map[p->p]+1;
if (p->p==p) printf("Resample_swc(): There is loop in the tree!\n");
S.x = p->x;
S.y = p->y;
S.z = p->z;
S.r = p->r;
S.type = p->type;
result.listNeuron.push_back(S);
}
for (V3DLONG i=0;i<tree.size();i++)
{
if (tree[i]) {delete tree[i]; tree[i]=NULL;}
}
for (V3DLONG j=0;j<seg_list.size();j++)
if (seg_list[j]) {delete seg_list[j]; seg_list[j] = NULL;}
for (V3DLONG i=0;i<result.listNeuron.size();i++)
result.hashNeuron.insert(result.listNeuron[i].n, i);
return result;
}