Repository navigation
Expand file tree
/
Copy pathanalyze.cpp
More file actions
824 lines (720 loc) · 31.3 KB
/
Copy pathanalyze.cpp
File metadata and controls
824 lines (720 loc) · 31.3 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
#include "analyze.h"
vector<int> getMulfurcationsCountNearSoma(float dist_thre, XYZ somaCoordinate, V_NeuronSWC_list segments){
vector<int> counts;
map<string, set<size_t> > wholeGrid2SegIDMap;
map<string, bool> isEndPointMap;
set<string> allPoints;
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
wholeGrid2SegIDMap[gridKey].insert(size_t(i));
allPoints.insert(gridKey);
if(j == 0 || j == seg.row.size() - 1){
isEndPointMap[gridKey] = true;
}
}
}
//末端点和分叉点
vector<string> points;
vector<set<int>> linksIndex;
map<string,int> pointsIndexMap;
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
if(j==0 || j==seg.row.size()-1){
//在pointsIndexMap中找不到某个线的末端点
if(pointsIndexMap.find(gridKey) == pointsIndexMap.end()){
points.push_back(gridKey);
linksIndex.push_back(set<int>());
// linksIndexVec.push_back(vector<int>());
pointsIndexMap[gridKey] = points.size() - 1;
}
}else{
if(wholeGrid2SegIDMap[gridKey].size()>1 &&
isEndPointMap.find(gridKey) != isEndPointMap.end() &&
pointsIndexMap.find(gridKey) == pointsIndexMap.end()){
points.push_back(gridKey);
linksIndex.push_back(set<int>());
// linksIndexVec.push_back(vector<int>());
pointsIndexMap[gridKey] = points.size() - 1;
}
}
}
}
qDebug()<<"points size: "<<points.size();
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
vector<int> segIndexs;
set<int> segIndexsSet;
segIndexs.clear();
segIndexsSet.clear();
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
if(pointsIndexMap.find(gridKey) != pointsIndexMap.end()){
int index = pointsIndexMap[gridKey];
if(segIndexsSet.find(index) == segIndexsSet.end()){
segIndexs.push_back(index);
segIndexsSet.insert(index);
}
}
}
// qDebug()<<"i : "<<i<<"seg size: "<<seg.row.size()<<" segIndexsSize: "<<segIndexs.size();
for(size_t j=0; j<segIndexs.size()-1; ++j){
if(segIndexs[j] == 1 || segIndexs[j+1] == 1){
qDebug()<<segIndexs[j]<<" "<<segIndexs[j+1];
}
linksIndex[segIndexs[j]].insert(segIndexs[j+1]);
// linksIndexVec[segIndexs[j]].push_back(segIndexs[j+1]);
linksIndex[segIndexs[j+1]].insert(segIndexs[j]);
// linksIndexVec[segIndexs[j+1]].push_back(segIndexs[j]);
}
}
qDebug()<<"link map end";
int biCount=0;
int mulCount=0;
int othersCount=0;
for(size_t i=0; i<points.size(); ++i){
if(linksIndex[i].size() > 3){
NeuronSWC s;
stringToXYZ(points[i],s.x,s.y,s.z);
if(distance(s.x, somaCoordinate.x, s.y, somaCoordinate.y, s.z, somaCoordinate.z)<dist_thre)
mulCount++;
}else if(linksIndex[i].size() == 3){
NeuronSWC s;
stringToXYZ(points[i],s.x,s.y,s.z);
if(distance(s.x, somaCoordinate.x, s.y, somaCoordinate.y, s.z, somaCoordinate.z)<dist_thre)
biCount++;
}else if(linksIndex[i].size()==2){
NeuronSWC s;
stringToXYZ(points[i],s.x,s.y,s.z);
if(distance(s.x, somaCoordinate.x, s.y, somaCoordinate.y, s.z, somaCoordinate.z)<dist_thre)
othersCount++;
}
}
counts.push_back(othersCount);
counts.push_back(biCount);
counts.push_back(mulCount);
qDebug()<<"counts: "<<counts[0]<<" "<<counts[1]<<" "<<counts[2];
return counts;
}
map<string, set<int>> getColorChangedPoints(V_NeuronSWC_list segments){
map<string, set<int>> point2TypeMap;
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
point2TypeMap[gridKey].insert(seg.row[j].type);
}
}
map<string, set<int>> specPointsMap;
for(auto it=point2TypeMap.begin(); it!=point2TypeMap.end(); it++){
if(it->second.size()<=1)
continue;
specPointsMap[it->first]=it->second;
}
return specPointsMap;
}
set<string> getDissociativeSegEndPoints(V_NeuronSWC_list segments){
map<string, set<size_t> > wholeGrid2SegIDMap;
set<string> dissociativePoints;
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
wholeGrid2SegIDMap[gridKey].insert(size_t(i));
}
}
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
bool flag=true;
string savedgridKey;
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
int size=wholeGrid2SegIDMap[gridKey].size();
if(j == 0)
savedgridKey=gridKey;
if(size > 1){
flag = false;
}
}
if(flag){
dissociativePoints.insert(savedgridKey);
}
}
return dissociativePoints;
}
set<string> getAngleErrPoints(float dist_thre, XYZ somaCoordinate, V_NeuronSWC_list segments){
set<string> angleErrPoints;
map<string, set<int>> point2TypeMap;
map<string, set<string>> parentMap;
map<string, set<string>> childMap;
map<string, set<size_t> > wholeGrid2SegIDMap;
map<string, bool> isEndPointMap;
set<string> allPoints;
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
vector<int> rowN2Index(seg.row.size()+1);
for(size_t j=0; j<seg.row.size(); ++j){
rowN2Index[seg.row[j].n]=j;
}
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
point2TypeMap[gridKey].insert(seg.row[j].type);
wholeGrid2SegIDMap[gridKey].insert(size_t(i));
allPoints.insert(gridKey);
if(j == 0 || j == seg.row.size() - 1){
isEndPointMap[gridKey] = true;
}
if(seg.row[j].parent!=-1){
float x2Label=seg.row[rowN2Index[seg.row[j].parent]].x;
float y2Label=seg.row[rowN2Index[seg.row[j].parent]].y;
float z2Label=seg.row[rowN2Index[seg.row[j].parent]].z;
QString parentKeyQ=QString::number(x2Label) + "_" + QString::number(y2Label) + "_" + QString::number(z2Label);
string parentKey=parentKeyQ.toStdString();
parentMap[gridKey].insert(parentKey);
childMap[parentKey].insert(gridKey);
}
}
}
//末端点和分叉点
vector<string> points;
vector<set<int>> linksIndex;
map<string,int> pointsIndexMap;
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
if(j==0 || j==seg.row.size()-1){
//在pointsIndexMap中找不到某个线的末端点
if(pointsIndexMap.find(gridKey) == pointsIndexMap.end()){
points.push_back(gridKey);
linksIndex.push_back(set<int>());
// linksIndexVec.push_back(vector<int>());
pointsIndexMap[gridKey] = points.size() - 1;
}
}else{
if(wholeGrid2SegIDMap[gridKey].size()>1 &&
isEndPointMap.find(gridKey) != isEndPointMap.end() &&
pointsIndexMap.find(gridKey) == pointsIndexMap.end()){
points.push_back(gridKey);
linksIndex.push_back(set<int>());
// linksIndexVec.push_back(vector<int>());
pointsIndexMap[gridKey] = points.size() - 1;
}
}
}
}
qDebug()<<"points size: "<<points.size();
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
vector<int> segIndexs;
set<int> segIndexsSet;
segIndexs.clear();
segIndexsSet.clear();
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
if(pointsIndexMap.find(gridKey) != pointsIndexMap.end()){
int index = pointsIndexMap[gridKey];
if(segIndexsSet.find(index) == segIndexsSet.end()){
segIndexs.push_back(index);
segIndexsSet.insert(index);
}
}
}
// qDebug()<<"i : "<<i<<"seg size: "<<seg.row.size()<<" segIndexsSize: "<<segIndexs.size();
for(size_t j=0; j<segIndexs.size()-1; ++j){
if(segIndexs[j] == 1 || segIndexs[j+1] == 1){
qDebug()<<segIndexs[j]<<" "<<segIndexs[j+1];
}
linksIndex[segIndexs[j]].insert(segIndexs[j+1]);
// linksIndexVec[segIndexs[j]].push_back(segIndexs[j+1]);
linksIndex[segIndexs[j+1]].insert(segIndexs[j]);
// linksIndexVec[segIndexs[j+1]].push_back(segIndexs[j]);
}
}
qDebug()<<"link map end";
set<string> bifurcationPoints;
for(size_t i=0; i<points.size(); ++i){
if(linksIndex[i].size() == 3){
NeuronSWC s;
stringToXYZ(points[i],s.x,s.y,s.z);
if(distance(s.x, somaCoordinate.x, s.y, somaCoordinate.y, s.z, somaCoordinate.z)>dist_thre)
bifurcationPoints.insert(points[i]);
}
}
for(auto it=bifurcationPoints.begin(); it!=bifurcationPoints.end();){
set<int> types = point2TypeMap[*it];
// qDebug()<<"types: "<<point2TypeMap[*it].size();
if (types.size()!=1) {
it = bifurcationPoints.erase(it); // 通过迭代器去除元素,并返回下一个有效迭代器
}
else if(*types.begin()!=3){
it = bifurcationPoints.erase(it);
}
else {
if(parentMap.find(*it)!=parentMap.end() && parentMap[*it].size()==1
&& childMap.find(*it)!=childMap.end() && childMap[*it].size()==2)
{
set<size_t> segIds = wholeGrid2SegIDMap[*it];
bool flag = true;
if(segIds.size() == 3){
int countNoParent=0;
int countNoChild=0;
for(auto segIt=segIds.begin(); segIt!=segIds.end(); segIt++){
V_NeuronSWC seg = segments.seg[*segIt];
int index = getPointInSegIndex(*it, seg);
if(index==0)
countNoChild++;
if(index==seg.row.size()-1)
countNoParent++;
double length = getSegLength(segments.seg[*segIt]);
if(length < 20){
// it = bifurcationPoints.erase(it);
flag=false;
break;
}
}
if(!(countNoChild==1 && countNoParent==2))
flag=false;
}
else if(segIds.size() == 2){
int countNoParent=0;
for(auto segIt=segIds.begin(); segIt!=segIds.end(); segIt++){
V_NeuronSWC seg = segments.seg[*segIt];
int index = getPointInSegIndex(*it, seg);
if(index==seg.row.size()-1)
countNoParent++;
double length = getPartOfSegLength(seg, index);
if(length < 20){
flag=false;
break;
}
}
if(countNoParent!=1)
flag=false;
}
if(flag)
it++;
else{
it = bifurcationPoints.erase(it);
}
}
else
{
it = bifurcationPoints.erase(it);
}
}
}
for(auto it=bifurcationPoints.begin(); it!=bifurcationPoints.end(); it++){
XYZ curCoor;
stringToXYZ(*it, curCoor.x, curCoor.y, curCoor.z);
XYZ parCoor;
stringToXYZ(*parentMap[*it].begin(), parCoor.x, parCoor.y, parCoor.z);
vector<XYZ> chiCoors;
for(auto chiIt=childMap[*it].begin(); chiIt!=childMap[*it].end(); chiIt++){
XYZ chiCoor;
stringToXYZ(*chiIt, chiCoor.x, chiCoor.y, chiCoor.z);
chiCoors.push_back(chiCoor);
}
QVector3D vector1(parCoor.x-curCoor.x, parCoor.y-curCoor.y, parCoor.z-curCoor.z);
QVector3D vector2(chiCoors[0].x-curCoor.x, chiCoors[0].y-curCoor.y, chiCoors[0].z-curCoor.z);
QVector3D vector3(chiCoors[1].x-curCoor.x, chiCoors[1].y-curCoor.y, chiCoors[1].z-curCoor.z);
float angle1 = calculateAngleofVecs(vector1, vector2);
float angle2 = calculateAngleofVecs(vector1, vector3);
if((angle1>0 && angle1<35) || (angle2>0 && angle2<35)){
angleErrPoints.insert(*it);
}
}
return angleErrPoints;
}
float calculateAngleofVecs(QVector3D vector1, QVector3D vector2){
// 计算两个向量的夹角
qreal dotProduct = QVector3D::dotProduct(vector1, vector2);
qreal magnitude1 = vector1.length();
qreal magnitude2 = vector2.length();
qreal cosineAngle = dotProduct / (magnitude1 * magnitude2);
// 计算弧度值
qreal angleRadians = qAcos(cosineAngle);
// 将弧度转换为角度
qreal angleDegrees = qRadiansToDegrees(angleRadians);
return angleDegrees;
}
//将soma点的半径设置为1.234
bool setSomaPointRadius(QString fileSaveName, V_NeuronSWC_list segments, XYZ somaCoordinate, double dist_thre, CollDetection* detectUtil, QString& msg){
map<string, set<size_t> > wholeGrid2SegIDMap;
map<string, bool> isEndPointMap;
set<string> allPoints;
map<string, set<string>> parentMap;
map<string, set<string>> childMap;
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
vector<int> rowN2Index(seg.row.size()+1);
for(size_t j=0; j<seg.row.size(); ++j){
rowN2Index[seg.row[j].n]=j;
}
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
wholeGrid2SegIDMap[gridKey].insert(size_t(i));
allPoints.insert(gridKey);
if(seg.row[j].parent!=-1){
float x2Label=seg.row[rowN2Index[seg.row[j].parent]].x;
float y2Label=seg.row[rowN2Index[seg.row[j].parent]].y;
float z2Label=seg.row[rowN2Index[seg.row[j].parent]].z;
QString parentKeyQ=QString::number(x2Label) + "_" + QString::number(y2Label) + "_" + QString::number(z2Label);
string parentKey=parentKeyQ.toStdString();
parentMap[gridKey].insert(parentKey);
childMap[parentKey].insert(gridKey);
}
if(j == 0 || j == seg.row.size() - 1){
isEndPointMap[gridKey] = true;
}
}
}
//末端点和分叉点
vector<string> points;
vector<set<int>> linksIndex;
map<string,int> pointsIndexMap;
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
if(j==0 || j==seg.row.size()-1){
//在pointsIndexMap中找不到某个线的末端点
if(pointsIndexMap.find(gridKey) == pointsIndexMap.end()){
points.push_back(gridKey);
linksIndex.push_back(set<int>());
// linksIndexVec.push_back(vector<int>());
pointsIndexMap[gridKey] = points.size() - 1;
}
}else{
if(wholeGrid2SegIDMap[gridKey].size()>1 &&
isEndPointMap.find(gridKey) != isEndPointMap.end() &&
pointsIndexMap.find(gridKey) == pointsIndexMap.end()){
points.push_back(gridKey);
linksIndex.push_back(set<int>());
// linksIndexVec.push_back(vector<int>());
pointsIndexMap[gridKey] = points.size() - 1;
}
}
}
}
qDebug()<<"points size: "<<points.size();
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
vector<int> segIndexs;
set<int> segIndexsSet;
segIndexs.clear();
segIndexsSet.clear();
for(size_t j=0; j<seg.row.size(); ++j){
float xLabel = seg.row[j].x;
float yLabel = seg.row[j].y;
float zLabel = seg.row[j].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
if(pointsIndexMap.find(gridKey) != pointsIndexMap.end()){
int index = pointsIndexMap[gridKey];
if(segIndexsSet.find(index) == segIndexsSet.end()){
segIndexs.push_back(index);
segIndexsSet.insert(index);
}
}
}
// qDebug()<<"i : "<<i<<"seg size: "<<seg.row.size()<<" segIndexsSize: "<<segIndexs.size();
for(size_t j=0; j<segIndexs.size()-1; ++j){
if(segIndexs[j] == 1 || segIndexs[j+1] == 1){
qDebug()<<segIndexs[j]<<" "<<segIndexs[j+1];
}
linksIndex[segIndexs[j]].insert(segIndexs[j+1]);
// linksIndexVec[segIndexs[j]].push_back(segIndexs[j+1]);
linksIndex[segIndexs[j+1]].insert(segIndexs[j]);
// linksIndexVec[segIndexs[j+1]].push_back(segIndexs[j]);
}
}
vector<NeuronSWC> outputSpecialPoints;
for(size_t i=0; i<points.size(); ++i){
// qDebug()<<i<<" link size: "<<linksIndex[i].size();
if(linksIndex[i].size() > 3){
qDebug()<<i<<" link size: "<<linksIndex[i].size();
NeuronSWC s;
stringToXYZ(points[i],s.x,s.y,s.z);
s.type = 8;
if(distance(s.x, somaCoordinate.x, s.y, somaCoordinate.y,
s.z, somaCoordinate.z) > dist_thre)
outputSpecialPoints.push_back(s);
}
}
vector<vector<size_t>> pairs;
set<size_t> pset;
size_t pre_tip_id=-1;
size_t cur_tip_id=-1;
double soma_radius=30;
for(size_t i=0; i<points.size(); i++){
if(linksIndex[i].size() == 3){
pre_tip_id=cur_tip_id;
cur_tip_id=i;
if(pre_tip_id!=-1){
NeuronSWC n1;
stringToXYZ(points[pre_tip_id],n1.x,n1.y,n1.z);
n1.type=6;
NeuronSWC n2;
stringToXYZ(points[cur_tip_id],n2.x,n2.y,n2.z);
n2.type=6;
set<size_t> n1Segs=wholeGrid2SegIDMap[points[pre_tip_id]];
set<size_t> n2Segs=wholeGrid2SegIDMap[points[cur_tip_id]];
int count1=0,count2=0;
for(auto it1=n1Segs.begin();it1!=n1Segs.end();it1++)
{
// qDebug()<<*it1;
// qDebug()<<getSegLength(inputSegList.seg[*it1]);
if(getSegLength(segments.seg[*it1])>40)
count1++;
}
for(auto it2=n2Segs.begin();it2!=n2Segs.end();it2++)
{
// qDebug()<<*it2;
// qDebug()<<getSegLength(inputSegList.seg[*it2]);
if(getSegLength(segments.seg[*it2])>40)
count2++;
}
// qDebug()<<"n2Segs end";
if(!(count1>=2&&count2>=2)){
continue;
}
if(distance(n1.x,somaCoordinate.x,n1.y,somaCoordinate.y,n1.z,somaCoordinate.z)>soma_radius
&&distance(n2.x,somaCoordinate.x,n2.y,somaCoordinate.y,n2.z,somaCoordinate.z)>soma_radius){
double dist=distance(n1.x,n2.x,n1.y,n2.y,n1.z,n2.z);
if(distance((n1.x+n2.x)/2,somaCoordinate.x,(n1.y+n2.y)/2,somaCoordinate.y,(n1.z+n2.z)/2,somaCoordinate.z)>1e-7&&dist<dist_thre){
vector<size_t> v={pre_tip_id,cur_tip_id};
pairs.push_back(v);
pset.insert(pre_tip_id);
pset.insert(cur_tip_id);
}
}
}
}
}
qDebug()<<pairs;
// qDebug()<<points;
for(auto it=pset.begin(); it!=pset.end(); it++){
qDebug()<<*it;
NeuronSWC n;
stringToXYZ(points[*it],n.x,n.y,n.z);
n.type=6;
outputSpecialPoints.push_back(n);
}
vector<NeuronSWC> bifurPoints;
vector<NeuronSWC> mulfurPoints;
for(int i=0;i<outputSpecialPoints.size();i++){
if(outputSpecialPoints[i].type == 6)
bifurPoints.push_back(outputSpecialPoints[i]);
else if(outputSpecialPoints[i].type == 8)
mulfurPoints.push_back(outputSpecialPoints[i]);
}
int count1=0;
int count2=0;
detectUtil->handleMulFurcation(mulfurPoints, count1);
detectUtil->handleNearBifurcation(bifurPoints, count2);
if(outputSpecialPoints.size()!=0){
qDebug()<<"swc exists MulFurcation or NearBifurcation, notice the brown or yellow markers!";
msg = "swc exists MulFurcation or NearBifurcation, notice the brown or yellow markers!";
return false;
}
set<string> targetCoordSet;
for(size_t i=0; i<points.size(); ++i){
if(linksIndex[i].size() >= 3){
NeuronSWC s;
stringToXYZ(points[i],s.x,s.y,s.z);
if(distance(s.x, somaCoordinate.x, s.y, somaCoordinate.y, s.z, somaCoordinate.z)<dist_thre)
targetCoordSet.insert(points[i]);
}
}
if(targetCoordSet.size() == 0){
for(size_t i=0; i<points.size(); ++i){
if(linksIndex[i].size() == 2){
NeuronSWC s;
stringToXYZ(points[i],s.x,s.y,s.z);
if(distance(s.x, somaCoordinate.x, s.y, somaCoordinate.y, s.z, somaCoordinate.z)<dist_thre)
targetCoordSet.insert(points[i]);
}
}
if(targetCoordSet.size()==0){
qDebug()<<"soma is not connected to one point!";
msg = "soma is not connected to one point!";
return false;
}
}
bool isDeleteEnd = false;
while(!isDeleteEnd){
isDeleteEnd = true;
for(int i=0; i<points.size(); ++i){
if(linksIndex[i].size() == 1){
int linkIndex = *(linksIndex[i].begin());
linksIndex[i].clear();
linksIndex[linkIndex].erase(std::find(linksIndex[linkIndex].begin(),linksIndex[linkIndex].end(),i));
isDeleteEnd = false;
}
}
}
//检测3条及3条以上的边构成的环
vector<string> newpoints;
for(size_t i=0; i<points.size(); ++i){
if(linksIndex[i].size()>=2)
newpoints.push_back(points[i]);
}
set<string> specPoints;
for(size_t i=0; i<newpoints.size(); ++i){
specPoints.insert(newpoints[i]);
}
//检测2条边构成的环
for(size_t i=0; i<segments.seg.size(); ++i){
V_NeuronSWC seg = segments.seg[i];
// if(seg.row.size()<4)
// continue;
float xLabel1 = seg.row[0].x;
float yLabel1 = seg.row[0].y;
float zLabel1 = seg.row[0].z;
float xLabel2=seg.row[seg.row.size()-1].x;
float yLabel2=seg.row[seg.row.size()-1].y;
float zLabel2=seg.row[seg.row.size()-1].z;
QString gridKeyQ1 = QString::number(xLabel1) + "_" + QString::number(yLabel1) + "_" + QString::number(zLabel1);
string gridKey1 = gridKeyQ1.toStdString();
QString gridKeyQ2 = QString::number(xLabel2) + "_" + QString::number(yLabel2) + "_" + QString::number(zLabel2);
string gridKey2 = gridKeyQ2.toStdString();
set<size_t> segSet1=wholeGrid2SegIDMap[gridKey1];
set<size_t> segSet2=wholeGrid2SegIDMap[gridKey2];
set<size_t> intersectionSet;
set_intersection(segSet1.begin(),segSet1.end(),segSet2.begin(),segSet2.end(),inserter( intersectionSet , intersectionSet.begin()));
if(intersectionSet.size()>=2){
specPoints.insert(gridKey1);
specPoints.insert(gridKey2);
}
}
outputSpecialPoints.clear();
int count =0;
for(auto it=specPoints.begin(); it!=specPoints.end(); it++){
NeuronSWC s;
stringToXYZ(*it,s.x,s.y,s.z);
s.type=0;
outputSpecialPoints.push_back(s);
}
detectUtil->handleLoop(outputSpecialPoints, count);
if(specPoints.size()!=0){
qDebug()<<"swc exists loop, notice the white markers!";
msg = "swc exists loop, notice the white markers!";
return false;
}
double min_dist = 100;
string somaCoord;
for(auto it=targetCoordSet.begin(); it!=targetCoordSet.end(); it++){
NeuronSWC s;
stringToXYZ(*it,s.x,s.y,s.z);
double dist = distance(s.x, somaCoordinate.x, s.y, somaCoordinate.y, s.z, somaCoordinate.z);
if(dist < min_dist)
{
min_dist = dist;
somaCoord = *it;
}
}
V_NeuronSWC seg = segments.seg[*wholeGrid2SegIDMap[somaCoord].begin()];
for(int i=0; i<seg.row.size(); i++){
float xLabel = seg.row[i].x;
float yLabel = seg.row[i].y;
float zLabel = seg.row[i].z;
QString gridKeyQ = QString::number(xLabel) + "_" + QString::number(yLabel) + "_" + QString::number(zLabel);
string gridKey = gridKeyQ.toStdString();
if(gridKey == somaCoord){
segments.seg[*wholeGrid2SegIDMap[somaCoord].begin()].row[i].r = 1.234;
break;
}
}
writeESWC_file(fileSaveName, V_NeuronSWC_list__2__NeuronTree(segments));
return true;
}
int getSomaNumberFromSwcFile(QString filePath, float r, QString& msg){
int number = -1;
if (filePath.endsWith(".swc") || filePath.endsWith(".SWC") || filePath.endsWith(".eswc") || filePath.endsWith(".ESWC"))
{
QFile qf(filePath);
QString arryRead;
if(!qf.open(QIODevice::ReadOnly|QIODevice::Text)){
msg = "cannot open swc file!";
return -2;
}
arryRead=qf.readAll();
qf.close();
QStringList arryListWrite= arryRead.split("\n");
// for(int i=0;i<arryListWrite.size();i++){
// qDebug()<<arryListWrite.at(i);
// }
// QIODevice::Text:以文本方式打开文件,读取时“\n”被自动翻译为换行符,写入时字符串结束符会自动翻译为系统平台的编码,如 Windows 平台下是“\r\n”
if (!qf.open(QIODevice::WriteOnly | QIODevice::Text))
{
msg = "cannot open swc file!";
return -2;
}
QTextStream streamWrite(&qf);
for(int i=0;i<arryListWrite.size()-1;i++){ //这里到arryListWrite.size()-1是因为arryListWrite数组按照\n分 段时,最后一行尾部有个\n,所以数组最后一个值为空,需要将它去掉
if(arryListWrite.at(i).contains("#")){
streamWrite<<arryListWrite.at(i)<<"\n";
}else{
QString contentWrite= arryListWrite.at(i);
QStringList swcInfo=contentWrite.split(' ',Qt::SkipEmptyParts);
if(swcInfo[5] == QString::number(r))
{
number = swcInfo[0].toInt();
swcInfo[5] = "1.000";
}
contentWrite=swcInfo.join(' ');
streamWrite<<contentWrite<<"\n";
}
}
qf.close();
return number;
}
}