-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathneurontracing_mip_plugin.cpp
More file actions
1307 lines (1133 loc) · 48.1 KB
/
Copy pathneurontracing_mip_plugin.cpp
File metadata and controls
1307 lines (1133 loc) · 48.1 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
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* neurontracing_mip_plugin.cpp
* This is a test plugin, you can use it as a demo.
* 2014-06-17 : by Zhi Zhou
*/
#include "v3d_message.h"
#include <vector>
#include "neurontracing_mip_plugin.h"
#include "my_surf_objs.h"
#include "smooth_curve.h"
#include "stackutil.h"
#include "volimg_proc.h"
//#include "../../../hackathon/zhi/APP2_large_scale/readRawfile_func.h"
#include "fastmarching_linker.h"
using namespace std;
Q_EXPORT_PLUGIN2(neurontracing_mip, neurontracing_mip);
struct Point;
struct Point
{
double x,y,z,r;
V3DLONG type;
Point* p;
V3DLONG childNum;
};
struct APP2_LS_PARA
{
int is_gsdt;
int is_break_accept;
int bkg_thresh;
double length_thresh;
int cnn_type;
int channel;
double SR_ratio;
int b_256cube;
int b_RadiusFrom2D;
int root_1st[3];
int mip_plane; //0 for XY, 1 for XZ, 2 for YZ
QString in_markerfile;
int b_resample;
int link_dist;
QString out_file;
Image4DSimple * image;
QString inimg_file;
QString mip_image_file;
};
typedef vector<Point*> Segment;
typedef vector<Point*> Tree;
void autotrace_largeScale_mip(V3DPluginCallback2 &callback, QWidget *parent,APP2_LS_PARA &p,bool bmenu);
void autotrace_largeScale_raw(V3DPluginCallback2 &callback, QWidget *parent,APP2_LS_PARA &p,bool bmenu);
QString getAppPath();
QStringList neurontracing_mip::menulist() const
{
return QStringList()
<<tr("trace_mip")
<<tr("about");
}
QStringList neurontracing_mip::funclist() const
{
return QStringList()
<<tr("trace_mip")
<<tr("help");
}
bool saveSWC_file_TreMap(string swc_file, vector<MyMarker*> & outmarkers)
{
if(swc_file.find_last_of(".dot") == swc_file.size() - 1) return saveDot_file(swc_file, outmarkers);
cout<<"marker num = "<<outmarkers.size()<<", save swc file to "<<swc_file<<endl;
map<MyMarker*, int> ind;
ofstream ofs(swc_file.c_str());
if(ofs.fail())
{
cout<<"open swc file error"<<endl;
return false;
}
ofs<<"#name "<<"TreMap_Tracing"<<endl;
ofs<<"#comment "<<endl;
ofs<<"##n,type,x,y,z,radius,parent"<<endl;
for(int i = 0; i < outmarkers.size(); i++) ind[outmarkers[i]] = i+1;
for(int i = 0; i < outmarkers.size(); i++)
{
MyMarker * marker = outmarkers[i];
int parent_id;
if(marker->parent == 0) parent_id = -1;
else parent_id = ind[marker->parent];
if(parent_id == 0) parent_id = -1;
ofs<<i+1<<" "<<marker->type<<" "<<marker->x<<" "<<marker->y<<" "<<marker->z<<" "<<marker->radius<<" "<<parent_id<<endl;
}
ofs.close();
return true;
}
void neurontracing_mip::domenu(const QString &menu_name, V3DPluginCallback2 &callback, QWidget *parent)
{
if (menu_name == tr("trace_mip"))
{
APP2_LS_PARA P;
bool bmenu = true;
mipTracingeDialog dialog(callback, parent);
if (!dialog.image)
return;
// if(dialog.listLandmarks.count() ==0)
// return;
// LocationSimple tmpLocation(0,0,0);
// tmpLocation = dialog.listLandmarks.at(0);
// tmpLocation.getCoord(P.root_1st[0],P.root_1st[1],P.root_1st[2]);
if (dialog.exec()!=QDialog::Accepted)
return;
P.inimg_file = dialog.image->getFileName();
P.is_gsdt = dialog.is_gsdt;
P.is_break_accept = dialog.is_break_accept;
P.bkg_thresh = dialog.bkg_thresh;
P.length_thresh = dialog.length_thresh;
P.cnn_type = dialog.cnn_type;
P.channel = dialog.channel;
P.SR_ratio = dialog.SR_ratio;
P.b_256cube = dialog.b_256cube;
P.b_RadiusFrom2D = dialog.b_RadiusFrom2D;
P.mip_plane = dialog.mip_plane;
autotrace_largeScale_mip(callback,parent,P,bmenu);
}
else
{
v3d_msg(tr("This is a test plugin, you can use it as a demo.. "
"Developed by Zhi Zhou, 2014-06-17"));
}
}
bool neurontracing_mip::dofunc(const QString & func_name, const V3DPluginArgList & input, V3DPluginArgList & output, V3DPluginCallback2 & callback, QWidget * parent)
{
if (func_name == tr("trace_mip"))
{
APP2_LS_PARA P;
bool bmenu = false;
vector<char*> * pinfiles = (input.size() >= 1) ? (vector<char*> *) input[0].p : 0;
vector<char*> * pparas = (input.size() >= 2) ? (vector<char*> *) input[1].p : 0;
vector<char*>* outfiles = (output.size() >= 1) ? (vector<char*>*) output[0].p: 0;
vector<char*> infiles = (pinfiles != 0) ? * pinfiles : vector<char*>();
vector<char*> paras = (pparas != 0) ? * pparas : vector<char*>();
P.inimg_file = infiles[0];
P.out_file = outfiles != 0 ? outfiles->at(0): "";
int k=0;
//try to use as much as the default value in the PARA_APP2 constructor as possible
P.in_markerfile = (paras.size() >= k+1) ? paras[k]: "NULL"; k++;
P.mip_plane = (paras.size() >= k+1) ? atoi(paras[k]) : 0; k++;
P.channel = (paras.size() >= k+1) ? atoi(paras[k]) : 1; k++;
P.bkg_thresh = paras.size() >= k+1 ? atoi(paras[k]) : 10; k++;
P.b_256cube = (paras.size() >= k+1) ? atoi(paras[k]) : 0; k++;
P.is_gsdt = (paras.size() >= k+1) ? atoi(paras[k]) : 0; k++;
P.is_break_accept = (paras.size() >= k+1) ? atoi(paras[k]) : 0; k++;
P.length_thresh = (paras.size() >= k+1) ? atof(paras[k]) : 5; k++;
P.b_resample = (paras.size() >= k+1) ? atoi(paras[k]) : 1; k++;
P.link_dist = (paras.size() >= k+1) ? atoi(paras[k]) : 20; k++;
P.cnn_type = 2;
P.SR_ratio = 3.0/9.0;
P.b_RadiusFrom2D = 1;
autotrace_largeScale_mip(callback,parent,P,bmenu);
}
// else if (func_name == tr("trace_raw"))
// {
// APP2_LS_PARA P;
// bool bmenu = false;
// vector<char*> * pinfiles = (input.size() >= 1) ? (vector<char*> *) input[0].p : 0;
// vector<char*> * pparas = (input.size() >= 2) ? (vector<char*> *) input[1].p : 0;
// vector<char*> infiles = (pinfiles != 0) ? * pinfiles : vector<char*>();
// vector<char*> paras = (pparas != 0) ? * pparas : vector<char*>();
// P.inimg_file = infiles[0];
// int k=0;
// //try to use as much as the default value in the PARA_APP2 constructor as possible
// P.mip_image_file = paras[0]; k++;
// P.channel = (paras.size() >= k+1) ? atoi(paras[k]) : 1; k++;
// P.bkg_thresh = (paras.size() >= k+1) ? atoi(paras[k]) : 10; k++;
// P.b_256cube = (paras.size() >= k+1) ? atoi(paras[k]) : 0; k++;
// P.is_gsdt = (paras.size() >= k+1) ? atoi(paras[k]) : 0; k++;
// P.is_break_accept = (paras.size() >= k+1) ? atoi(paras[k]) : 0; k++;
// P.length_thresh = (paras.size() >= k+1) ? atof(paras[k]) : 5; k++;
// P.cnn_type = 2;
// P.SR_ratio = 3.0/9.0;
// P.b_RadiusFrom2D = 1;
// autotrace_largeScale_raw(callback,parent,P,bmenu);
// }
else if (func_name == tr("help"))
{
printf("\n**** Usage of TReMAP tracing ****\n");
printf("vaa3d -x plugin_name -f trace_mip -i <inimg_file> -p <in_markerfile> <mip_plane> <channel> <bkg_thresh> <b_256cube> <is_gsdt> <is_gap> <length_thresh> <b_resample> <link_dist>\n");
printf("inimg_file Should be 8/16/32bit image\n");
printf("in_markerfile If no input marker file, please set this para to NULL and it will detect soma automatically. \n"
" When the file is set, then the first marker is used as root/soma.\n");
printf("mip_plane Maximum projection plane, 0 for XY plane, 1 for XZ plane, 2 for YZ plane, Default 0\n");
printf("channel Data channel for tracing. Start from 1 (default 1).\n");
printf("bkg_thresh Default 10 (if specified as 0 then auto-thresolding)\n");
printf("b_256cube If trace in a auto-downsampled volume (1 for yes, and 0 for no. Default 0.)\n");
printf("is_gsdt If use gray-scale distance transform (1 for yes and 0 for no. Default 0.)\n");
printf("is_gap If allow gap (1 for yes and 0 for no. Default 0.)\n");
printf("length_thresh Default 5\n");
printf("b_resample Whether to do resample(1 for yes and 0 for no). Default 1\n");
printf("link_dist In the final sorting of swc, max dist gap between the broken nodes to be linked. Default 20.\n");
printf("outswc_file Will be named automatically based on the input image file name, so you don't have to specify it.\n\n");
}
else return false;
return true;
}
void autotrace_largeScale_mip(V3DPluginCallback2 &callback, QWidget *parent,APP2_LS_PARA &Para,bool bmenu)
{
unsigned char* data1d = 0;
V3DLONG N,M,P,C;
QString image_name = Para.inimg_file;
if(bmenu)
{
v3dhandle curwin = callback.currentImageWindow();
Image4DSimple* p4DImage = callback.getImage(curwin);
data1d = p4DImage->getRawData();
N = p4DImage->getXDim();
M = p4DImage->getYDim();
P = p4DImage->getZDim();
C = p4DImage->getCDim();
}
else
{
V3DLONG im_sz[4];
int datatype = 0;
if (!simple_loadimage_wrapper(callback,image_name.toStdString().c_str(), data1d, im_sz, datatype))
{
fprintf (stderr, "Error happens in reading the subject file [%s]. Exit. \n",image_name.toStdString().c_str());
return;
}
N = im_sz[0];
M = im_sz[1];
P = im_sz[2];
C = im_sz[3];
}
double imgAve, imgStd;
if (Para.bkg_thresh == 0)
if (Para.channel > 0 && Para.channel <= C)
{
mean_and_std(data1d + N*M*P*(C-1), N*M*P, imgAve, imgStd);
Para.bkg_thresh = imgAve+0.5*imgStd ; //(imgAve < imgStd)? imgAve : (imgAve+imgStd)*.5;
printf("auto thr = %d\n", Para.bkg_thresh);
}
else
{
fprintf(stderr, "Channel out of range. Exit. \n");
return;
}
int th = Para.bkg_thresh;
QString tmpfolder = QFileInfo(image_name).path()+("/") + QFileInfo(image_name).completeBaseName()+("_tmp");
system(qPrintable(QString("mkdir %1").arg(tmpfolder.toStdString().c_str())));
if(tmpfolder.isEmpty())
{
printf("Can not create a tmp folder!\n");
return;
}
//V3DLONG tmpx,tmpy,tmpz;
//tmpx = Para.root_1st[0];
//tmpy = Para.root_1st[1];
//tmpz = Para.root_1st[2];
V3DLONG mip_sz[3];
mip_sz[0] = N;
mip_sz[1] = M;
mip_sz[2] = P;
switch (Para.mip_plane)
{
case 0: mip_sz[2] = 1; break;
case 1: mip_sz[1] = 1; break;
case 2: mip_sz[0] = 1; break;
default:
return;
}
V3DLONG pagesz_mip = mip_sz[0]*mip_sz[1]*mip_sz[2];
unsigned char *image_mip=0;
try {image_mip = new unsigned char [pagesz_mip];}
catch(...) {v3d_msg("cannot allocate memory for image_mip.", bmenu); return;}
switch (Para.mip_plane)
{
case 0:
for(V3DLONG iy = 0; iy < M; iy++)
{
V3DLONG offsetj = iy*N;
for(V3DLONG ix = 0; ix < N; ix++)
{
int max_mip = 0;
for(V3DLONG iz = 0; iz < P; iz++)
{
V3DLONG offsetk = iz*M*N;
if(data1d[offsetk + offsetj + ix] >= max_mip)
{
image_mip[iy*N + ix] = data1d[offsetk + offsetj + ix];
max_mip = data1d[offsetk + offsetj + ix];
}
}
}
}
break;
case 1:
for(V3DLONG iz = 0; iz < P; iz++)
{
V3DLONG offsetk = iz*M*N;
for(V3DLONG ix = 0; ix < N; ix++)
{
int max_mip = 0;
for(V3DLONG iy = 0; iy < M; iy++)
{
V3DLONG offsetj = iy*N;
if(data1d[offsetk + offsetj + ix] >= max_mip)
{
image_mip[iz*N + ix] = data1d[offsetk + offsetj + ix];
max_mip = data1d[offsetk + offsetj + ix];
}
}
}
}
break;
case 2:
for(V3DLONG iz = 0; iz < P; iz++)
{
V3DLONG offsetk = iz*M*N;
for(V3DLONG iy = 0; iy < M; iy++)
{
V3DLONG offsetj = iy*N;
int max_mip = 0;
for(V3DLONG ix = 0; ix < N; ix++)
{
if(data1d[offsetk + offsetj + ix] >= max_mip)
{
image_mip[iz*M + iy] = data1d[offsetk + offsetj + ix];
max_mip = data1d[offsetk + offsetj + ix];
}
}
}
}
break;
default:
return;
}
unsigned char *image_binary=0;
try {image_binary = new unsigned char [pagesz_mip];}
catch(...) {v3d_msg("cannot allocate memory for image_binary.", bmenu); return;}
for(V3DLONG i = 0; i < pagesz_mip; i++)
{
if(image_mip[i] > th)
image_binary[i] = 255;
else
image_binary[i] = 0;
}
unsigned char *image_binary_median=0;
try {image_binary_median = new unsigned char [pagesz_mip];}
catch(...) {v3d_msg("cannot allocate memory for image_binary_median.", bmenu); return;}
int *arr,tmp;
int ii,jj;
int size = 3*3;
arr = new int[size];
for(V3DLONG iz = 0; iz < mip_sz[2]; iz++)
{
V3DLONG offsetk = iz*mip_sz[1]*mip_sz[0];
for(V3DLONG iy = 0; iy < mip_sz[1]; iy++)
{
V3DLONG offsetj = iy*mip_sz[0];
for(V3DLONG ix = 0; ix < mip_sz[0]; ix++)
{
V3DLONG xb = ix-1; if(xb<0) xb = 0;
V3DLONG xe = ix+1; if(xe>=mip_sz[0]-1) xe = mip_sz[0]-1;
V3DLONG yb = iy-1; if(yb<0) yb = 0;
V3DLONG ye = iy+1; if(ye>=mip_sz[1]-1) ye = mip_sz[1]-1;
V3DLONG zb = iz-1; if(zb<0) zb = 0;
V3DLONG ze = iz+1; if(ze>=mip_sz[2]-1) ze = mip_sz[2]-1;
ii = 0;
for(V3DLONG k=zb; k<=ze; k++)
{
V3DLONG offsetkl = k*mip_sz[1]*mip_sz[0];
for(V3DLONG j=yb; j<=ye; j++)
{
V3DLONG offsetjl = j*mip_sz[0];
for(V3DLONG i=xb; i<=xe; i++)
{
int dataval = image_binary[offsetkl + offsetjl + i];
arr[ii] = dataval;
if (ii>0)
{
jj = ii;
while(jj > 0 && arr[jj-1]>arr[jj])
{
tmp = arr[jj];
arr[jj] = arr[jj-1];
arr[jj-1] = tmp;
jj--;
}
}
ii++;
}
}
}
V3DLONG index_pim = offsetk + offsetj + ix;
image_binary_median[index_pim] = arr[int(0.5*ii)+1];
}
}
}
if(image_binary) {delete []image_binary; image_binary = 0;}
V3DLONG in_sz[4];
in_sz[0] = N;
in_sz[1] = M;
in_sz[2] = P;
in_sz[3] = 1;
switch (Para.mip_plane)
{
case 0: in_sz[2] = 1; break;
case 1: in_sz[1] = 1; break;
case 2: in_sz[0] = 1; break;
default:
return;
}
QString input_image_name = tmpfolder + "/binary_median.raw";
simple_saveimage_wrapper(callback, input_image_name.toStdString().c_str(), (unsigned char *)image_binary_median, in_sz, V3D_UINT8);
if(image_binary_median) {delete []image_binary_median; image_binary_median = 0;}
QString output_image_name = tmpfolder +"/region.raw";
#if defined(Q_OS_LINUX)
QString cmd_region = QString("%1/vaa3d -x regiongrow -f rg -i %2 -o %3 -p 1 0 1 400").arg(getAppPath().toStdString().c_str()).arg(input_image_name.toStdString().c_str()).arg(output_image_name.toStdString().c_str());
system(qPrintable(cmd_region));
#elif defined(Q_OS_MAC)
QString cmd_region = QString("%1/vaa3d64.app/Contents/MacOS/vaa3d64 -x regiongrow -f rg -i %2 -o %3 -p 1 0 1 400").arg(getAppPath().toStdString().c_str()).arg(input_image_name.toStdString().c_str()).arg(output_image_name.toStdString().c_str());
system(qPrintable(cmd_region));
#else
v3d_msg("The OS is not Linux or Mac. Do nothing.", bmenu);
return;
#endif
unsigned char * image_region = 0;
int datatype;
V3DLONG in_zz[4];
if(!simple_loadimage_wrapper(callback, output_image_name.toStdString().c_str(), image_region, in_zz, datatype))
{
v3d_msg("Fail to load image", bmenu);
return;
}
int groupNum = 0;
for(V3DLONG i = 0; i < pagesz_mip; i++)
{
if(image_region[i] > groupNum)
groupNum = image_region[i];
}
int *groupArray = new int[groupNum];
int *groupIndex = new int[groupNum];
for(int i = 0; i < groupNum; i++)
{
groupArray[i] = 0;
groupIndex[i] = i+1;
}
for(V3DLONG i = 0; i < pagesz_mip; i++)
{
if(image_region[i] > 0)
groupArray[image_region[i] - 1] += 1;
}
int tmp_index;
for(V3DLONG i = 0; i < groupNum; i++)
{
if (i > 0)
{
V3DLONG j = i;
while(j > 0 && groupArray[j-1]<groupArray[j])
{
tmp = groupArray[j];
groupArray[j] = groupArray[j-1];
groupArray[j-1] = tmp;
tmp_index = groupIndex[j];
groupIndex[j] = groupIndex[j-1];
groupIndex[j-1] = tmp_index;
j--;
}
}
}
int groupmax = 30;
if(groupNum <= groupmax) groupmax = groupNum;
vector<MyMarker*> outswc_final;
for(int dd = 0; dd < groupmax; dd++)
{
unsigned char *image_region_one = new unsigned char [pagesz_mip];
V3DLONG group_type = groupIndex[dd];
for(V3DLONG i = 0; i < pagesz_mip*datatype; i++)
{
if(image_region[i] == group_type)
image_region_one[int(i/datatype)] = image_mip[int(i/datatype)];
else
image_region_one[int(i/datatype)] = 0;
}
/* switch (Para.mip_plane)
{
case 0: group_type = image_region[tmpy*N + tmpx]; break;
case 1: group_type = image_region[tmpz*N + tmpx]; break;
case 2: group_type = image_region[tmpz*M + tmpy];break;
default:
return;
}
tmpx /= groupArray[0];
tmpy /= groupArray[0];
tmpz = 0;
v3d_msg(QString("x is %1 and y is %2").arg(tmpx).arg(tmpy));
return;
ImageMarker S;
QList <ImageMarker> marklist;
S.x = tmpx;
S.y = tmpy;
S.z = tmpz;
switch (Para.mip_plane)
{
case 0: S.z = 0; break;
case 1: S.y = 0; break;
case 2: S.x = 0; break;
default:
return;
}
marklist.append(S);
QString markerpath = tmpfolder +("/root.marker");
writeMarker_file(markerpath.toStdString().c_str(),marklist);*/
QString APP2_image_name = tmpfolder + "/group_one.raw";
simple_saveimage_wrapper(callback, APP2_image_name.toStdString().c_str(), (unsigned char *)image_region_one, in_sz, V3D_UINT8);
if(image_region_one) {delete []image_region_one; image_region_one = 0;}
QString APP2_swc = APP2_image_name + QString("_group_%1.swc").arg(group_type);
int thr = Para.bkg_thresh - 5;
if (thr < 0) thr = 0;
printf("app2_bkg_thr = %d\n", thr);
#if defined(Q_OS_LINUX)
QString cmd_APP2 = QString("%1/vaa3d -x Vaa3D_Neuron2 -f app2 -i %2 -o %3 -p NULL %4 %5 %6 %7 %8 %9 %10 %11").arg(getAppPath().toStdString().c_str()).arg(APP2_image_name.toStdString().c_str()).arg(APP2_swc.toStdString().c_str())
.arg(Para.channel-1).arg(thr).arg(Para.b_256cube).arg(Para.b_RadiusFrom2D).arg(Para.is_gsdt).arg(Para.is_break_accept).arg(Para.length_thresh).arg(Para.b_resample);
system(qPrintable(cmd_APP2));
if (Para.b_resample)
{
QString cmd_resample = QString("%1/vaa3d -x resample_swc -f resample_swc -i %2 -o %3 -p 2").arg(getAppPath().toStdString().c_str()).arg(APP2_swc.toStdString().c_str()).arg(APP2_swc.toStdString().c_str());
system(qPrintable(cmd_resample));
}
#elif defined(Q_OS_MAC)
QString cmd_APP2 = QString("%1/vaa3d64.app/Contents/MacOS/vaa3d64 -x Vaa3D_Neuron2 -f app2 -i %2 -o %3 -p NULL %4 %5 %6 %7 %8 %9 %10 %11").arg(getAppPath().toStdString().c_str()).arg(APP2_image_name.toStdString().c_str()).arg(APP2_swc.toStdString().c_str())
.arg(Para.channel-1).arg(thr).arg(Para.b_256cube).arg(Para.b_RadiusFrom2D).arg(Para.is_gsdt).arg(Para.is_break_accept).arg(Para.length_thresh).arg(Para.b_resample);
system(qPrintable(cmd_APP2));
if (Para.b_resample)
{
system(qPrintable(cmd_resample));
QString cmd_resample = QString("%1//vaa3d64.app/Contents/MacOS/vaa3d64 -x resample_swc -f resample_swc -i %2 -o %3 -p 2").arg(getAppPath().toStdString().c_str()).arg(APP2_swc.toStdString().c_str()).arg(APP2_swc.toStdString().c_str());
}
#else
v3d_msg("The OS is not Linux or Mac. Do nothing.", bmenu);
return;
#endif
/*QString APP2_swc;
switch (Para.mip_plane)
{
case 0: APP2_swc = APP2_image_name + QString("_x%1_y%2_z%3_app2.swc").arg(S.x-1).arg(S.y-1).arg(S.z); break;
case 1: APP2_swc = APP2_image_name + QString("_x%1_y%2_z%3_app2.swc").arg(S.x-1).arg(S.y).arg(S.z-1); break;
case 2: APP2_swc = APP2_image_name + QString("_x%1_y%2_z%3_app2.swc").arg(S.x).arg(S.y-1).arg(S.z-1); break;
default:
return;
}*/
NeuronTree nt = readSWC_file(APP2_swc);
V3DLONG siz = nt.listNeuron.size();
Tree tree;
for (V3DLONG i=0;i<siz;i++)
{
NeuronSWC s = nt.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 (nt.listNeuron[i].pn<0) continue;
V3DLONG pid = nt.hashNeuron.value(nt.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);
}
}
vector<MyMarker*> outswc;
for (V3DLONG i=0;i<seg_list.size();i++)
{
vector<MyMarker> nearpos_vec, farpos_vec; // for near/far locs testing
nearpos_vec.clear();
farpos_vec.clear();
if(seg_list[i]->size() > 2)
{
for (V3DLONG j=0;j<seg_list[i]->size();j++)
{
Point* node = seg_list[i]->at(j);
XYZ loc0_t, loc1_t;
loc0_t = XYZ(node->x, node->y, node->z);
switch (Para.mip_plane)
{
case 0: loc1_t = XYZ(node->x, node->y, P-1); break;
case 1: loc1_t = XYZ(node->x, M-1, node->z); break;
case 2: loc1_t = XYZ(N-1, node->y, node->z); break;
default:
return;
}
XYZ loc0 = loc0_t;
XYZ loc1 = loc1_t;
nearpos_vec.push_back(MyMarker(loc0.x, loc0.y, loc0.z));
farpos_vec.push_back(MyMarker(loc1.x, loc1.y, loc1.z));
}
fastmarching_drawing_dynamic(nearpos_vec, farpos_vec, (unsigned char*)data1d, outswc, N,M,P, 1, 5);
// smooth_curve(outswc,5);
for(V3DLONG d = 0; d <outswc.size(); d++)
{
outswc[d]->radius = 2;
outswc[d]->type = dd + 2;
outswc_final.push_back(outswc[d]);
}
outswc.clear();
}
else if(seg_list[i]->size() == 2)
{
Point* node1 = seg_list[i]->at(0);
Point* node2 = seg_list[i]->at(1);
for (V3DLONG j=0;j<3;j++)
{
XYZ loc0_t, loc1_t;
if(j ==0)
{
loc0_t = XYZ(node1->x, node1->y, node1->z);
switch (Para.mip_plane)
{
case 0: loc1_t = XYZ(node1->x, node1->y, P-1); break;
case 1: loc1_t = XYZ(node1->x, M-1, node1->z); break;
case 2: loc1_t = XYZ(N-1, node1->y, node1->z); break;
default:
return;
}
}
else if(j ==1)
{
loc0_t = XYZ(0.5*(node1->x + node2->x), 0.5*(node1->y + node2->y), 0.5*(node1->z + node2->z));
switch (Para.mip_plane)
{
case 0: loc1_t = XYZ(0.5*(node1->x + node2->x), 0.5*(node1->y + node2->y), P-1); break;
case 1: loc1_t = XYZ(0.5*(node1->x + node2->x), M-1, 0.5*(node1->z + node2->z)); break;
case 2: loc1_t = XYZ(N-1, 0.5*(node1->y + node2->y), 0.5*(node1->z + node2->z)); break;
default:
return;
}
}
else
{
loc0_t = XYZ(node2->x, node2->y, node2->z);
switch (Para.mip_plane)
{
case 0: loc1_t = XYZ(node2->x, node2->y, P-1); break;
case 1: loc1_t = XYZ(node2->x, M-1, node2->z); break;
case 2: loc1_t = XYZ(N-1, node2->y, node2->z); break;
default:
return;
} }
XYZ loc0 = loc0_t;
XYZ loc1 = loc1_t;
nearpos_vec.push_back(MyMarker(loc0.x, loc0.y, loc0.z));
farpos_vec.push_back(MyMarker(loc1.x, loc1.y, loc1.z));
}
fastmarching_drawing_dynamic(nearpos_vec, farpos_vec, (unsigned char*)data1d, outswc, N,M,P, 1, 5);
// smooth_curve(outswc,5);
for(V3DLONG d = 0; d <outswc.size(); d++)
{
outswc[d]->radius = 2;
outswc[d]->type = dd + 2;
outswc_final.push_back(outswc[d]);
}
outswc.clear();
}
}
// clear
for (int i = 0; i < seg_list.size(); ++i)
{
if (seg_list[i])
{
delete seg_list[i];
seg_list[i] = NULL;
}
}
for (int i = 0; i < tree.size(); ++i)
{
if (tree[i])
{
delete tree[i];
tree[i] = NULL;
}
}
// QString group_swc = tmpfolder + QString("/raw_%1.swc").arg(dd);
// saveSWC_file(group_swc.toStdString(), outswc_final);
// outswc_final.clear();
}
if(image_mip) {delete []image_mip; image_mip = 0;}
if(image_region) {delete []image_region; image_region = 0;}
if(groupArray) {delete []groupArray; groupArray = 0;}
if(groupIndex) {delete []groupIndex; groupIndex = 0;}
QString swc_2D,final_swc;
if (Para.out_file != "")
final_swc = Para.out_file;
else
switch (Para.mip_plane)
{
case 0: swc_2D = image_name + "_XY_2D_mip.swc"; final_swc = image_name + "_XY_3D_TreMap.swc"; break;
case 1: swc_2D = image_name + "_XZ_2D_mip.swc"; final_swc = image_name + "_XZ_3D_TreMap.swc"; break;
case 2: swc_2D = image_name + "_YZ_2D_mip.swc"; final_swc = image_name + "_YZ_3D_TreMap.swc"; break;
default:
return;
}
saveSWC_file(final_swc.toStdString(), outswc_final);
outswc_final.clear();
// system(qPrintable(QString("mv %1 %2").arg(APP2_swc.toStdString().c_str()).arg(swc_2D.toStdString().c_str())));
system(qPrintable(QString("rm -r %1").arg(tmpfolder.toStdString().c_str())));
V3DPluginArgItem arg;
V3DPluginArgList input_resample;
V3DPluginArgList input_sort;
V3DPluginArgList output;
std:: string fileName_Qstring(final_swc.toStdString());char* fileName_string = new char[fileName_Qstring.length() + 1]; strcpy(fileName_string, fileName_Qstring.c_str());
if (Para.b_resample)
{
arg.type = "random";std::vector<char*> arg_input_resample;
arg_input_resample.push_back(fileName_string);
arg.p = (void *) & arg_input_resample; input_resample<< arg;
arg.type = "random";std::vector<char*> arg_resample_para; arg_resample_para.push_back("10");arg.p = (void *) & arg_resample_para; input_resample << arg;
arg.type = "random";std::vector<char*> arg_output;arg_output.push_back(fileName_string); arg.p = (void *) & arg_output; output<< arg;
QString full_plugin_name_resample = "resample_swc";
QString func_name_resample = "resample_swc";
callback.callPluginFunc(full_plugin_name_resample,func_name_resample,input_resample,output);
}
int minn = -1;
if(Para.in_markerfile != "NULL")
{
vector<MyMarker> file_inmarkers = readMarker_file(string(qPrintable(Para.in_markerfile)));
vector<LocationSimple> landmarks;
LocationSimple t;
for(int i = 0; i < file_inmarkers.size(); i++)
{
t.x = file_inmarkers[i].x;
t.y = file_inmarkers[i].y;
t.z = file_inmarkers[i].z;
if(t.x<0 || t.x>=N || t.y<0 || t.y>=M || t.z<0 || t.z>=P)
{
if(i==0)
{
v3d_msg("The first marker is invalid.", bmenu);
return;
}
else continue;
}
landmarks.push_back(t);
}
if (landmarks.size() > 0)
{
t = landmarks[0];
NeuronTree nt = readSWC_file(final_swc);
float mindist = 0;
for (int i = 0; i < nt.listNeuron.size(); ++i)
{
float xx = t.x - nt.listNeuron[i].x;
float yy = t.y - nt.listNeuron[i].y;
float zz = t.z - nt.listNeuron[i].z;
float dist = xx * xx + yy * yy + zz * zz;
if (dist < mindist || minn == -1)
{
mindist = dist;
minn = nt.listNeuron[i].n;
}
}
}
}
arg.type = "random";std::vector<char*> arg_input_sort;
arg_input_sort.push_back(fileName_string);
arg.p = (void *) & arg_input_sort; input_sort<< arg;
arg.type = "random";std::vector<char*> arg_sort_para;
QString link_dist = QString::number(Para.link_dist);
char* link_dist_str = new char[link_dist.length() + 1];
strcpy(link_dist_str, link_dist.toStdString().c_str());
arg_sort_para.push_back(link_dist_str);
QString minn_qs = QString::number(minn);
char* minn_str = new char[minn_qs.length() + 1];
if (minn != -1)
{
strcpy(minn_str, minn_qs.toStdString().c_str());
arg_sort_para.push_back(minn_str);
}
arg.p = (void *) & arg_sort_para; input_sort << arg;
arg.type = "random";std::vector<char*> arg_output;arg_output.push_back(fileName_string); arg.p = (void *) & arg_output; output<< arg;
QString full_plugin_name_sort = "sort_neuron_swc";
QString func_name_sort = "sort_swc";
callback.callPluginFunc(full_plugin_name_sort,func_name_sort, input_sort,output);
vector<MyMarker*> temp_out_swc = readSWC_file(final_swc.toStdString());
saveSWC_file_TreMap(final_swc.toStdString(), temp_out_swc);
temp_out_swc.clear();
if (link_dist_str)
{
delete [] link_dist_str;
link_dist_str = NULL;
}
if (minn_str)
{
delete [] minn_str;
minn_str = NULL;
}
if(!bmenu)
{
if(data1d) {delete []data1d; data1d = 0;}
}
if (fileName_string)
{
delete [] fileName_string;
fileName_string = NULL;
}
v3d_msg(QString("Now you can drag and drop the generated swc fle [%1] into Vaa3D.").arg(final_swc.toStdString().c_str()),bmenu);
return;
}
//void autotrace_largeScale_raw(V3DPluginCallback2 &callback, QWidget *parent,APP2_LS_PARA &Para,bool bmenu)
//{
// QString image_name = Para.inimg_file;
// QString image_mip_name = Para.mip_image_file;
// unsigned char *image_mip=0;
// V3DLONG *im_sz = 0;
// int datatype = 0;
// if (loadImage(const_cast<char *>(image_mip_name.toStdString().c_str()), image_mip, im_sz, datatype)!=true)
// {
// fprintf (stderr, "Error happens in reading the subject file [%s]. Exit. \n",image_name.toStdString().c_str());
// return;
// }
// QString tmpfolder = QFileInfo(image_name).path()+("/") + QFileInfo(image_name).completeBaseName()+("_tmp");
// system(qPrintable(QString("mkdir %1").arg(tmpfolder.toStdString().c_str())));
// if(tmpfolder.isEmpty())
// {
// printf("Can not create a tmp folder!\n");
// return;
// }
// QString output_image_name = tmpfolder +"/region.raw";
// #if defined(Q_OS_LINUX)
// QString cmd_region = QString("%1/vaa3d -x regiongrow -f rg -i %2 -o %3 -p 1 2 1 200 %4").arg(getAppPath().toStdString().c_str()).arg(image_mip_name.toStdString().c_str()).arg(output_image_name.toStdString().c_str()).arg(Para.bkg_thresh);
// system(qPrintable(cmd_region));
// #elif defined(Q_OS_MAC)
// QString cmd_region = QString("%1/vaa3d64.app/Contents/MacOS/vaa3d64 -x regiongrow -f rg -i %2 -o %3 -p 1 2 1 200 %4").arg(getAppPath().toStdString().c_str()).arg(image_mip_name.toStdString().c_str()).arg(output_image_name.toStdString().c_str()).arg(Para.bkg_thresh);
// system(qPrintable(cmd_region));
// #else
// v3d_msg("The OS is not Linux or Mac. Do nothing.");
// return;
// #endif
// unsigned char * image_region = 0;
// V3DLONG in_zz[4];
// if(!simple_loadimage_wrapper(callback, output_image_name.toStdString().c_str(), image_region, in_zz, datatype))
// {
// v3d_msg("Fail to load image");
// return;
// }
// V3DLONG pagesz_mip = in_zz[0]*in_zz[1]*in_zz[2];
// int groupNum = 0;
// for(V3DLONG i = 0; i < pagesz_mip; i++)
// {
// if(image_region[i] > groupNum)
// groupNum = image_region[i];
// }
// int *groupArray = new int[groupNum];
// int *groupIndex = new int[groupNum];
// for(int i = 0; i < groupNum; i++)
// {
// groupArray[i] = 0;
// groupIndex[i] = i+1;
// }
// for(V3DLONG i = 0; i < pagesz_mip; i++)
// {