/
NeuronAssembler_NeuTube_plugin.cpp
1394 lines (1134 loc) · 52.2 KB
/
NeuronAssembler_NeuTube_plugin.cpp
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
#include "v3d_message.h"
#include <vector>
#include "stackutil.h"
#include <boost/lexical_cast.hpp>
using namespace std;
#include "NeuronAssembler_NeuTube_plugin.h"
#include "../../../hackathon/zhi/APP2_large_scale/readRawfile_func.h"
#include "../../../released_plugins/v3d_plugins/istitch/y_imglib.h"
#include "../../../released_plugins/v3d_plugins/neurontracing_vn2/app2/my_surf_objs.h"
#include "../../../hackathon/zhi/neuronassembler_plugin_creator/sort_swc.h"
Q_EXPORT_PLUGIN2(NeuronAssembler_NeuTube, NeuronAssembler_NeuTube);
//Q_EXPORT_PLUGIN2(neuronassembler, neuronassembler);
struct root_node
{
V3DLONG root_x;
V3DLONG root_y;
V3DLONG root_z;
V3DLONG tc_index;
V3DLONG ref_index;
V3DLONG start[3];
V3DLONG end[3];
V3DLONG parent;
QString tilename;
int direction; //1 left, 2 right, 3 up, and 4 down
struct root_node* next;
};
struct NA_PARA
{
// int bkg_thresh;
int is_entire;
int ch;
int merge;
int block_size;
int root_1st[3];
QString tcfilename,inimg_file,rawfilename,markerfilename;
};
struct Point;
struct Point
{
double x,y,z,r;
V3DLONG type;
Point* p;
V3DLONG childNum;
};
typedef vector<Point*> Segment;
typedef vector<Point*> Tree;
// group images blending function
template <class SDATATYPE>
int region_groupfusing(SDATATYPE *pVImg, Y_VIM<REAL, V3DLONG, indexed_t<V3DLONG, REAL>, LUT<V3DLONG> > vim, unsigned char *relative1d,
V3DLONG vx, V3DLONG vy, V3DLONG vz, V3DLONG vc, V3DLONG rx, V3DLONG ry, V3DLONG rz, V3DLONG rc,
V3DLONG tile2vi_zs, V3DLONG tile2vi_ys, V3DLONG tile2vi_xs,
V3DLONG z_start, V3DLONG z_end, V3DLONG y_start, V3DLONG y_end, V3DLONG x_start, V3DLONG x_end, V3DLONG *start)
{
//
SDATATYPE *prelative = (SDATATYPE *)relative1d;
if(x_end<x_start || y_end<y_start || z_end<z_start)
return false;
// update virtual image pVImg
V3DLONG offset_volume_v = vx*vy*vz;
V3DLONG offset_volume_r = rx*ry*rz;
V3DLONG offset_pagesz_v = vx*vy;
V3DLONG offset_pagesz_r = rx*ry;
for(V3DLONG c=0; c<rc; c++)
{
V3DLONG o_c = c*offset_volume_v;
V3DLONG o_r_c = c*offset_volume_r;
for(V3DLONG k=z_start; k<z_end; k++)
{
V3DLONG o_k = o_c + (k-start[2])*offset_pagesz_v;
V3DLONG o_r_k = o_r_c + (k-tile2vi_zs)*offset_pagesz_r;
for(V3DLONG j=y_start; j<y_end; j++)
{
V3DLONG o_j = o_k + (j-start[1])*vx;
V3DLONG o_r_j = o_r_k + (j-tile2vi_ys)*rx;
for(V3DLONG i=x_start; i<x_end; i++)
{
V3DLONG idx = o_j + i-start[0];
V3DLONG idx_r = o_r_j + (i-tile2vi_xs);
if(pVImg[idx])
{
pVImg[idx] = 0.5*(pVImg[idx] + prelative[idx_r]); // Avg. Intensity
}
else
{
pVImg[idx] = prelative[idx_r];
}
}
}
}
}
return true;
}
bool assembler_tc(V3DPluginCallback2 &callback, QWidget *parent,NA_PARA &p,bool bmenu);
bool assembler_raw(V3DPluginCallback2 &callback, QWidget *parent,NA_PARA &p,bool bmenu);
void save_region(V3DPluginCallback2 &callback, V3DLONG *start, V3DLONG *end, QString &tcfile,QString ®ion_name,
Y_VIM<REAL, V3DLONG, indexed_t<V3DLONG, REAL>, LUT<V3DLONG> > vim, NA_PARA &p);
NeuronTree eliminate(NeuronTree input, double length);
//QStringList neuronassembler::menulist() const
QStringList NeuronAssembler_NeuTube::menulist() const
{
return QStringList()
<<tr("trace_tc")
<<tr("trace_raw")
<<tr("about");
}
//QStringList neuronassembler::funclist() const
QStringList NeuronAssembler_NeuTube::funclist() const
{
return QStringList()
<<tr("trace_tc")
<<tr("trace_raw")
<<tr("help");
}
void NeuronAssembler_NeuTube::domenu(const QString &menu_name, V3DPluginCallback2 &callback, QWidget *parent)
//void neuronassembler::domenu(const QString &menu_name, V3DPluginCallback2 &callback, QWidget *parent)
{
if (menu_name == tr("trace_tc"))
{
NA_PARA P;
bool bmenu = true;
NeuronAssemblerDialog dialog(callback, parent);
if (!dialog.image)
return;
if(dialog.listLandmarks.count() ==0)
return;
if (dialog.exec()!=QDialog::Accepted)
return;
P.inimg_file = dialog.image->getFileName();
P.tcfilename = dialog.tcfilename;
if(P.tcfilename.isEmpty())
{
v3d_msg("Please select the tc file.");
return;
}
// P.bkg_thresh = dialog.bkg_thresh;
P.ch = dialog.ch;
P.merge = dialog.merge;
// P.channel = dialog.channel;
P.block_size = dialog.block_size;
LocationSimple tmpLocation(0,0,0);
tmpLocation = dialog.listLandmarks.at(0);
tmpLocation.getCoord(P.root_1st[0],P.root_1st[1],P.root_1st[2]);
assembler_tc(callback,parent,P,bmenu);
}
else if (menu_name == tr("trace_raw"))
{
NA_PARA P;
bool bmenu = true;
NeuronAssemblerDialog_raw dialog(callback, parent);
if (dialog.exec()!=QDialog::Accepted)
return;
if(dialog.rawfilename.isEmpty())
{
v3d_msg("Please select the image file.");
return;
}
if(dialog.markerfilename.isEmpty() || dialog.markerfilename == "NULL")
{
P.root_1st[0] = -1;
P.root_1st[1] = -1;
P.root_1st[2] = -1;
}
else
{
vector<MyMarker> file_inmarkers;
file_inmarkers = readMarker_file(string(qPrintable(dialog.markerfilename)));
P.root_1st[0] = file_inmarkers[0].x;
P.root_1st[1] = file_inmarkers[0].y;
P.root_1st[2] = file_inmarkers[0].z;
}
P.ch = dialog.ch;
P.merge = dialog.merge;
P.block_size = dialog.block_size;
P.inimg_file = dialog.rawfilename;
P.is_entire = dialog.is_entire;
assembler_raw(callback,parent,P,bmenu);
}
else
{
v3d_msg(tr("This is a test plugin, you can use it as a demo.. "
"Developed by Zhi Zhou, 2015-09-29"));
}
}
bool NeuronAssembler_NeuTube::dofunc(const QString & func_name, const V3DPluginArgList & input, V3DPluginArgList & output, V3DPluginCallback2 & callback, QWidget * parent)
// v3d_msg(tr("This is a test plugin, you can use it as a demo.. "
// "Developed by Zhi Zhou, 2014-11-09"));
// }
//}
//bool neuronassembler::dofunc(const QString & func_name, const V3DPluginArgList & input, V3DPluginArgList & output, V3DPluginCallback2 & callback, QWidget * parent)
{
if (func_name == tr("trace_tc"))
{
NA_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*>();
int k=0;
QString inmarker_file = paras.empty() ? "" : paras[k]; if(inmarker_file == "NULL") inmarker_file = ""; k++;
if(inmarker_file.isEmpty())
{
cerr<<"Need a marker file"<<endl;
return false;
}
P.tcfilename = paras.empty() ? "" : paras[k]; k++;
if(infiles.empty())
{
vector<MyMarker> file_inmarkers;
file_inmarkers = readMarker_file(string(qPrintable(inmarker_file)));
int soma_x = file_inmarkers[0].x;
int soma_y = file_inmarkers[0].y;
Y_VIM<REAL, V3DLONG, indexed_t<V3DLONG, REAL>, LUT<V3DLONG> > vim;
if( !vim.y_load( P.tcfilename.toStdString()) )
{
printf("Wrong stitching configuration file to be load!\n");
return false;
}
QString curFilePath = QFileInfo( P.tcfilename).path();
curFilePath.append("/");
for(V3DLONG ii=0; ii<vim.number_tiles; ii++)
{
if(soma_x > vim.lut[ii].start_pos[0] && soma_y > vim.lut[ii].start_pos[1] && soma_x < vim.lut[ii].end_pos[0] && soma_y < vim.lut[ii].end_pos[1])
{
P.inimg_file = curFilePath + QString(vim.lut[ii].fn_img.c_str());
v3d_msg(P.inimg_file,0);
inmarker_file = P.inimg_file + ".marker";
break;
}
}
}
else
{
P.inimg_file = infiles[0];
}
vector<MyMarker> file_inmarkers;
file_inmarkers = readMarker_file(string(qPrintable(inmarker_file)));
P.root_1st[0] = file_inmarkers[0].x;
P.root_1st[1] = file_inmarkers[0].y;
P.root_1st[2] = file_inmarkers[0].z;
// P.tracing_method = (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) ? atof(paras[k]) : 10; k++;
P.block_size = (paras.size() >= k+1) ? atoi(paras[k]) : 1024; k++;
P.ch = (paras.size() >= k+1) ? atoi(paras[k]):1; k++;
P.merge = (paras.size() >= k+1) ? atoi(paras[k]):1; k++;
assembler_tc(callback,parent,P,bmenu);
}
else if (func_name == tr("trace_raw"))
{
NA_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*>();
if(infiles.empty())
{
cerr<<"Need input image"<<endl;
return false;
}
P.inimg_file = infiles[0];
int k=0;
QString inmarker_file = paras.empty() ? "" : paras[k]; if(inmarker_file == "NULL") inmarker_file = ""; k++;
if(inmarker_file.isEmpty())
{
P.root_1st[0] = -1;
P.root_1st[1] = -1;
P.root_1st[2] = -1;
}
else
{
vector<MyMarker> file_inmarkers;
file_inmarkers = readMarker_file(string(qPrintable(inmarker_file)));
P.root_1st[0] = file_inmarkers[0].x;
P.root_1st[1] = file_inmarkers[0].y;
P.root_1st[2] = file_inmarkers[0].z;
}
P.block_size = (paras.size() >= k+1) ? atof(paras[k]) : 1024; k++;
P.is_entire = (paras.size() >= k+1) ? atoi(paras[k]) : 0; k++;
P.ch = (paras.size() >= k+1) ? atoi(paras[k]):1; k++;
P.merge = (paras.size() >= k+1) ? atoi(paras[k]):1; k++;
assembler_raw(callback,parent,P,bmenu);
}
else if (func_name == tr("help"))
{
printf("\n**** Usage of Neuron Assembler ****\n");
// printf("vaa3d -x plugin_name -f trace_tc -i <inimg_file> -p <inmarker_file> <tc_file> <tracing_method> <channel> <bkg_thresh> <block size>\n");
printf("vaa3d -x NeuronAssembler_NeuTube -f trace_tc -i <inimg_file> -p <inmarker_file> <tc_file> <block size> <ch> <merge>\n");
printf("inimg_file Should be 8 bit image\n");
printf("inmarker_file Please specify the path of the marker file\n");
printf("tc_file Please specify the path of the tc file\n");
printf("block size Default 1024\n");
printf("ch Required by the tracing algorithm. Default value is 1\n");
printf("merge Required by the tracing algorithm. Default value is 1\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");
printf("vaa3d -x NeuronAssembler_NeuTube -f trace_raw -i <inimg_file> -p <inmarker_file> <block size> <tracing_entire_image> <ch> <merge>\n");
printf("inimg_file Should be 8 bit v3draw/raw image\n");
printf("inmarker_file Please specify the path of the marker file, Default value is NULL\n");
printf("block size Default 1024\n");
printf("tracing_entire_image YES:1, NO:0. Default value is 0\n");
printf("ch Required by the tracing algorithm. Default value is 1\n");
printf("merge Required by the tracing algorithm. Default value is 1\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");
// printf("inimg_file Should be 8 bit image\n");
// printf("inmarker_file Please specify the path of the marker file\n");
// printf("tc_file Please specify the path of the tc file\n");
// printf("tracing_method Tracing method.0 for MOST tracing, 1 for NeuTube tracing, 2 for Farsight snake tracing, (default 0).\n");
// printf("channel Data channel for tracing. Start from 1 (default 1).\n");
// printf("bkg_thresh Default 10 (is specified as -1 then auto-thresolding)\n");
// printf("block size Default 1024\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");
// printf("vaa3d -x plugin_name -f trace_raw -i <inimg_file> -p <inmarker_file> <tracing_method> <channel> <bkg_thresh> <block size>\n");
// printf("inimg_file Should be 8 bit image\n");
// printf("inmarker_file Please specify the path of the marker file\n");
// printf("tracing_method Tracing method.0 for MOST tracing, 1 for NeuTube tracing, 2 for Farsight snake tracing, (default 0).\n");
// printf("channel Data channel for tracing. Start from 1 (default 1).\n");
// printf("bkg_thresh Default 10 (is specified as -1 then auto-thresolding)\n");
// printf("block size Default 1024\n");
// printf("outswc_file Will be named automatically based on the input image file name, so you don't have to specify it.\n");
}
else return false;
return true;
}
bool assembler_tc(V3DPluginCallback2 &callback, QWidget *parent,NA_PARA &P,bool bmenu)
{
QElapsedTimer timer1;
timer1.start();
QString tcfile = P.tcfilename;
QString fileOpenName = P.inimg_file;
Y_VIM<REAL, V3DLONG, indexed_t<V3DLONG, REAL>, LUT<V3DLONG> > vim;
if( !vim.y_load(tcfile.toStdString()) )
{
printf("Wrong stitching configuration file to be load!\n");
return false;
}
int soma_index = -1;
for(V3DLONG ii=0; ii<vim.number_tiles; ii++)
{
QString curPath = QFileInfo(tcfile).path();
QString curtilename = curPath.append("/").append(QString(vim.lut[ii].fn_img.c_str()));
if(curtilename == fileOpenName)
{
soma_index = ii;
break;
}
}
if(soma_index == -1 )
{
printf("Can not find the selected image in the tc file!\n");
return false;
}
V3DLONG start[3], end[3];
start[0] = vim.lut[soma_index].start_pos[0] - vim.min_vim[0] + P.root_1st[0] - int(P.block_size/2);
start[1] = vim.lut[soma_index].start_pos[1] - vim.min_vim[1] + P.root_1st[1] - int(P.block_size/2);
start[2] = 0;
end[0] = start[0] + P.block_size - 1;
end[1] = start[1] + P.block_size - 1;
end[2] = vim.sz[2] - 1;
struct root_node *head = new root_node[1];
struct root_node *walker;
QString startingpos="", tmps;
tmps.setNum(start[0]).prepend("x"); startingpos += tmps;
tmps.setNum(start[1]).prepend("_y"); startingpos += tmps;
QString region_name = startingpos + ".raw";
head->root_x = int(P.block_size/2);
head->root_y = int(P.block_size/2);
head->root_z = vim.lut[soma_index].start_pos[2] - vim.min_vim[2] + P.root_1st[2] ;
head->tc_index = soma_index;
// head->tilename = QFileInfo(tcfile).path().append("/tmp/").append(QString(region_name));
head->ref_index = -1;
head->start[0] = start[0];
head->start[1] = start[1];
head->start[2] = start[2];
head->end[0] = end[0];
head->end[1] = end[1];
head->end[2] = end[2];
head->parent = -1;
head->direction = 0;
head->next = NULL;
walker = head;
QString rootposstr="", tmps2;
tmps2.setNum(int(P.root_1st[0]+0.5)).prepend("_x"); rootposstr += tmps2;
tmps2.setNum(int(P.root_1st[1]+0.5)).prepend("_y"); rootposstr += tmps2;
tmps2.setNum(int(P.root_1st[2]+0.5)).prepend("_z"); rootposstr += tmps2;
// QString finalswcfilename = fileOpenName + rootposstr + "_NeuronAssembler_MOST.swc";
QString finalswcfilename = fileOpenName + rootposstr + "_NeuronAssembler_NeuTube.swc";
QString tmpfolder = QFileInfo(tcfile).path()+("/tmp_NeuronAssembler_NeuTube");
head->tilename = QFileInfo(tcfile).path().append("/tmp_NeuronAssembler_NeuTube/").append(QString(region_name));
// QString tmpfolder = QFileInfo(tcfile).path()+("/tmp");
system(qPrintable(QString("mkdir %1").arg(tmpfolder.toStdString().c_str())));
if(tmpfolder.isEmpty())
{
printf("Can not create a tmp folder!\n");
return false;
}
int swc_type = 2;
while(walker != NULL)
{
ifstream ifs_image(walker->tilename.toStdString().c_str());
if(!ifs_image)
save_region(callback,walker->start,walker->end,tcfile,walker->tilename,vim, P);
else
{
walker = walker->next;
continue;
}
// QString swcfilename = walker->tilename + QString("_MOST.swc");
QString swcfilename = walker->tilename + QString("_neutube.swc");
V3DPluginArgItem arg;
V3DPluginArgList input;
V3DPluginArgList output;
QString full_plugin_name;
QString func_name;
arg.type = "random";std::vector<char*> arg_input;
std:: string fileName_Qstring(walker->tilename.toStdString());char* fileName_string = new char[fileName_Qstring.length() + 1]; strcpy(fileName_string, fileName_Qstring.c_str());
arg_input.push_back(fileName_string);
arg.p = (void *) & arg_input; input<< arg;
char* char_swcout = new char[swcfilename.length() + 1];strcpy(char_swcout, swcfilename.toStdString().c_str());
arg.type = "random";std::vector<char*> arg_output;arg_output.push_back(char_swcout); arg.p = (void *) & arg_output; output<< arg;
arg.type = "random";
std::vector<char*> arg_para;
// char channel = '0' + P.channel;
string S_ch = boost::lexical_cast<string>(P.ch);
char* C_ch = new char[S_ch.length() + 1];
strcpy(C_ch,S_ch.c_str());
arg_para.push_back(C_ch);
string S_merge = boost::lexical_cast<string>(P.merge);
char* C_merge = new char[S_merge.length() + 1];
strcpy(C_merge,S_merge.c_str());
arg_para.push_back(C_merge);
full_plugin_name = "neuTube"; func_name = "neutube_trace";
// string T_background = boost::lexical_cast<string>(P.bkg_thresh);
// char* Th = new char[T_background.length() + 1];
// strcpy(Th, T_background.c_str());
// arg_para.push_back(&channel);arg_para.push_back(Th);
arg.p = (void *) & arg_para; input << arg;
// full_plugin_name = "mostVesselTracer"; func_name = "MOST_trace";
if(!callback.callPluginFunc(full_plugin_name,func_name,input,output))
{
printf("Can not find the tracing plugin!\n");
return false;
}
ifstream ifs_swc(swcfilename.toStdString().c_str());
if(!ifs_swc)
{
walker = walker->next;
continue;
}
NeuronTree nt = readSWC_file(swcfilename);
if(nt.listNeuron.empty())
{
remove(swcfilename.toStdString().c_str());
walker = walker->next;
continue;
}
NeuronTree nt_pruned = eliminate(nt,3.0);
struct root_node *walker_inside;
struct root_node *newNode;
walker_inside = head;
while(walker_inside->next != NULL)
{
walker_inside = walker_inside->next;
}
bool left = 0, right = 0,up = 0,down = 0;
for(V3DLONG i = 0; i < nt_pruned.listNeuron.size(); i++)
{
NeuronSWC curr = nt_pruned.listNeuron.at(i);
newNode = new root_node[1];
if(curr.x < 0.05* P.block_size && left == 0 && walker->direction !=2)
{
if(walker->end[0] == vim.sz[0] - 1 || walker->start[0] == 0) continue;
newNode->start[0] = walker->start[0] - P.block_size;
newNode->start[1] = walker->start[1];
newNode->start[2] = walker->start[2];
newNode->end[0] = newNode->start[0] + P.block_size - 1;;
newNode->end[1] = walker->end[1];
newNode->end[2] = walker->end[2];
if( newNode->start[0] < 0) newNode->start[0] = 0;
QString startingpos="", tmps;
tmps.setNum(newNode->start[0]).prepend("x"); startingpos += tmps;
tmps.setNum(newNode->start[1]).prepend("_y"); startingpos += tmps;
QString region_name = startingpos + ".raw";
newNode->tilename = QFileInfo(fileOpenName).path().append("/tmp_NeuronAssembler_NeuTube/").append(QString(region_name));
// newNode->tilename = QFileInfo(fileOpenName).path().append("/tmp/").append(QString(region_name));
newNode->ref_index = walker->tc_index;
newNode->direction = 2;
newNode->next = NULL;
walker_inside->next = newNode;
walker_inside = walker_inside->next;
left = 1;
}
else if(curr.x > 0.95 * P.block_size && right == 0 && walker->direction !=1)
{
if(walker->end[0] == vim.sz[0] - 1 || walker->start[0] == 0) continue;
newNode->start[0] = walker->start[0] + P.block_size;
newNode->start[1] = walker->start[1];
newNode->start[2] = walker->start[2];
newNode->end[0] = newNode->start[0] + P.block_size - 1;
newNode->end[1] = walker->end[1];
newNode->end[2] = walker->end[2];
if( newNode->end[0] > vim.sz[0] - 1) newNode->end[0] = vim.sz[0] - 1;
QString startingpos="", tmps;
tmps.setNum(newNode->start[0]).prepend("x"); startingpos += tmps;
tmps.setNum(newNode->start[1]).prepend("_y"); startingpos += tmps;
QString region_name = startingpos + ".raw";
newNode->tilename = QFileInfo(fileOpenName).path().append("/tmp_NeuronAssembler_NeuTube/").append(QString(region_name));
// newNode->tilename = QFileInfo(fileOpenName).path().append("/tmp/").append(QString(region_name));
newNode->ref_index = walker->tc_index;
newNode->direction = 1;
newNode->next = NULL;
walker_inside->next = newNode;
walker_inside = walker_inside->next;
right = 1;
}
else if(curr.y < 0.05* P.block_size && up == 0 && walker->direction !=4)
{
if(walker->end[1] == vim.sz[1] - 1 || walker->start[1] == 0) continue;
newNode->start[0] = walker->start[0];
newNode->start[1] = walker->start[1] - P.block_size;
newNode->start[2] = walker->start[2];
newNode->end[0] = walker->end[0];
newNode->end[1] = newNode->start[1]+ P.block_size - 1;
newNode->end[2] = walker->end[2];
if(newNode->start[1] < 0) newNode->start[1] = 0;
QString startingpos="", tmps;
tmps.setNum(newNode->start[0]).prepend("x"); startingpos += tmps;
tmps.setNum(newNode->start[1]).prepend("_y"); startingpos += tmps;
QString region_name = startingpos + ".raw";
newNode->tilename = QFileInfo(fileOpenName).path().append("/tmp_NeuronAssembler_NeuTube/").append(QString(region_name));
// newNode->tilename = QFileInfo(fileOpenName).path().append("/tmp/").append(QString(region_name));
newNode->ref_index = walker->tc_index;
newNode->direction = 4;
newNode->next = NULL;
walker_inside->next = newNode;
walker_inside = walker_inside->next;
up = 1;
}
else if(curr.y > 0.95 * P.block_size && down == 0 && walker->direction !=3)
{
if(walker->end[1] == vim.sz[1] - 1 || walker->start[1] == 0) continue;
newNode->start[0] = walker->start[0];
newNode->start[1] = walker->start[1] + P.block_size;
newNode->start[2] = walker->start[2];
newNode->end[0] = walker->end[0];
newNode->end[1] = newNode->start[1] + P.block_size - 1;
newNode->end[2] = walker->end[2];
if( newNode->end[1] > vim.sz[1] - 1) newNode->end[1] = vim.sz[1] - 1;
QString startingpos="", tmps;
tmps.setNum(newNode->start[0]).prepend("x"); startingpos += tmps;
tmps.setNum(newNode->start[1]).prepend("_y"); startingpos += tmps;
QString region_name = startingpos + ".raw";
newNode->tilename = QFileInfo(fileOpenName).path().append("/tmp_NeuronAssembler_NeuTube/").append(QString(region_name));
// newNode->tilename = QFileInfo(fileOpenName).path().append("/tmp/").append(QString(region_name));
newNode->ref_index = walker->tc_index;
newNode->direction = 3;
newNode->next = NULL;
walker_inside->next = newNode;
walker_inside = walker_inside->next;
down = 1;
}
}
vector<MyMarker*> temp_out_swc = readSWC_file(swcfilename.toStdString());
ifstream ifs(finalswcfilename.toStdString().c_str());
if(walker->ref_index == -1)
{
if(ifs)
remove(finalswcfilename.toStdString().c_str());
for(int j = 0; j < temp_out_swc.size(); j++)
{
temp_out_swc[j]->x = temp_out_swc[j]->x + walker->start[0];
temp_out_swc[j]->y = temp_out_swc[j]->y + walker->start[1];
temp_out_swc[j]->z = temp_out_swc[j]->z + walker->start[2];
temp_out_swc[j]->type = swc_type;
}
saveSWC_file(finalswcfilename.toStdString(), temp_out_swc);
}
else
{
vector<MyMarker*> final_out_swc = readSWC_file(finalswcfilename.toStdString());
vector<MyMarker*> final_out_swc_updated = final_out_swc;
// temp_out_swc[0]->parent = final_out_swc[walker->parent];
for(int j = 0; j < temp_out_swc.size(); j++)
{
temp_out_swc[j]->x = temp_out_swc[j]->x + walker->start[0];
temp_out_swc[j]->y = temp_out_swc[j]->y + walker->start[1];
temp_out_swc[j]->z = temp_out_swc[j]->z + walker->start[2];
temp_out_swc[j]->type = swc_type;
final_out_swc_updated.push_back(temp_out_swc[j]);
}
saveSWC_file(finalswcfilename.toStdString(), final_out_swc_updated);
}
swc_type++;
walker = walker->next;
}
// NeuronTree final_swc = readSWC_file(finalswcfilename);
// NeuronTree final_swc_sorted;
// SortSWC(final_swc.listNeuron, final_swc_sorted.listNeuron,1, 30);
// writeSWC_file(finalswcfilename, final_swc_sorted);
v3d_msg(QString("Now you can drag and drop the generated swc fle [%1] into Vaa3D.").arg(finalswcfilename),bmenu);
system(qPrintable(QString("rm -r %1").arg(tmpfolder.toStdString().c_str())));
return true;
}
bool assembler_raw(V3DPluginCallback2 &callback, QWidget *parent,NA_PARA &P,bool bmenu)
{
QString fileOpenName = P.inimg_file;
unsigned char * datald = 0;
V3DLONG *in_zz = 0;
V3DLONG *in_sz = 0;
int datatype;
if (!loadRawRegion(const_cast<char *>(P.inimg_file.toStdString().c_str()), datald, in_zz, in_sz,datatype,0,0,0,1,1,1))
{
return false;
}
if(datald) {delete []datald; datald = 0;}
if(P.root_1st[0] == -1) { P.root_1st[0] = in_zz[0]/2;P.root_1st[1] = in_zz[1]/2;P.root_1st[2] = in_zz[2]/2;}
V3DLONG start[3], end[3];
start[0] = P.root_1st[0] - int(P.block_size/2);
start[1] = P.root_1st[1] - int(P.block_size/2);
start[2] = 0;
end[0] = start[0] + P.block_size - 1;
end[1] = start[1] + P.block_size - 1;
end[2] = in_zz[2] - 1;
struct root_node *head = new root_node[1];
struct root_node *walker;
QString startingpos="", tmps;
tmps.setNum(start[0]).prepend("x"); startingpos += tmps;
tmps.setNum(start[1]).prepend("_y"); startingpos += tmps;
QString region_name = startingpos + ".raw";
head->root_x = int(P.block_size/2);
head->root_y = int(P.block_size/2);
head->root_z = P.root_1st[2] ;
//head->tilename = QFileInfo(fileOpenName).path().append("/tmp/").append(QString(region_name));
head->ref_index = -1;
head->start[0] = start[0];
head->start[1] = start[1];
head->start[2] = start[2];
head->end[0] = end[0];
head->end[1] = end[1];
head->end[2] = end[2];
head->parent = -1;
head->direction = 0;
head->next = NULL;
walker = head;
QString rootposstr="", tmps2;
tmps2.setNum(int(P.root_1st[0]+0.5)).prepend("_x"); rootposstr += tmps2;
tmps2.setNum(int(P.root_1st[1]+0.5)).prepend("_y"); rootposstr += tmps2;
tmps2.setNum(int(P.root_1st[2]+0.5)).prepend("_z"); rootposstr += tmps2;
// QString finalswcfilename = fileOpenName + rootposstr + "_NeuronAssembler_MOST.swc";
QString finalswcfilename = fileOpenName + rootposstr + "_NeuronAssembler_NeuTube.swc";
QString tmpfolder = QFileInfo(fileOpenName).path()+("/tmp_NeuronAssembler_NeuTube");
head->tilename = QFileInfo(fileOpenName).path().append("/tmp_NeuronAssembler_NeuTube/").append(QString(region_name));
// QString tmpfolder = QFileInfo(fileOpenName).path()+("/tmp");
system(qPrintable(QString("mkdir %1").arg(tmpfolder.toStdString().c_str())));
if(tmpfolder.isEmpty())
{
printf("Can not create a tmp folder!\n");
return false;
}
int swc_type = 2;
while(walker != NULL)
{
ifstream ifs_image(walker->tilename.toStdString().c_str());
if(!ifs_image)
{
unsigned char * datald = 0;
V3DLONG *in_zz = 0;
V3DLONG *in_sz = 0;
int datatype;
if (!loadRawRegion(const_cast<char *>(P.inimg_file.toStdString().c_str()), datald, in_zz, in_sz,datatype,walker->start[0],walker->start[1],walker->start[2],walker->end[0]+1,walker->end[1]+1,walker->end[2]+1))
{
printf("can not load the region");
if(datald) {delete []datald; datald = 0;}
continue;//return false;
}
simple_saveimage_wrapper(callback, walker->tilename.toStdString().c_str(), (unsigned char *)datald, in_sz, V3D_UINT8);
if(datald) {delete []datald; datald = 0;}
}
else
{
walker = walker->next;
continue;
}
QString swcfilename = walker->tilename + QString("_neutube.swc");
// QString swcfilename = walker->tilename + QString("_MOST.swc");;
V3DPluginArgItem arg;
V3DPluginArgList input;
V3DPluginArgList output;
QString full_plugin_name;
QString func_name;
arg.type = "random";std::vector<char*> arg_input;
std:: string fileName_Qstring(walker->tilename.toStdString());char* fileName_string = new char[fileName_Qstring.length() + 1]; strcpy(fileName_string, fileName_Qstring.c_str());
arg_input.push_back(fileName_string);
arg.p = (void *) & arg_input; input<< arg;
char* char_swcout = new char[swcfilename.length() + 1];strcpy(char_swcout, swcfilename.toStdString().c_str());
arg.type = "random";std::vector<char*> arg_output;arg_output.push_back(char_swcout); arg.p = (void *) & arg_output; output<< arg;
arg.type = "random";
std::vector<char*> arg_para;
// char channel = '0' + P.channel;
string S_ch = boost::lexical_cast<string>(P.ch);
char* C_ch = new char[S_ch.length() + 1];
strcpy(C_ch,S_ch.c_str());
arg_para.push_back(C_ch);
string S_merge = boost::lexical_cast<string>(P.merge);
char* C_merge = new char[S_merge.length() + 1];
strcpy(C_merge,S_merge.c_str());
arg_para.push_back(C_merge);
full_plugin_name = "neuTube"; func_name = "neutube_trace";
// string T_background = boost::lexical_cast<string>(P.bkg_thresh);
// char* Th = new char[T_background.length() + 1];
// strcpy(Th, T_background.c_str());
// arg_para.push_back(&channel);arg_para.push_back(Th);
arg.p = (void *) & arg_para; input << arg;
// full_plugin_name = "mostVesselTracer"; func_name = "MOST_trace";
if(!callback.callPluginFunc(full_plugin_name,func_name,input,output))
{
printf("Can not find the tracing plugin!\n");
return false;
}
struct root_node *walker_inside;
struct root_node *newNode;
walker_inside = head;
while(walker_inside->next != NULL)
{
walker_inside = walker_inside->next;
}
if(P.is_entire ==1)
{
for (int i =0; i<4; i++)
{
newNode = new root_node[1];
if(i ==0)
{
if(walker->end[0] == in_zz[0] - 1|| walker->start[0] == 0) continue;
newNode->start[0] = walker->start[0] - P.block_size;
newNode->start[1] = walker->start[1];
newNode->start[2] = walker->start[2];
newNode->end[0] = newNode->start[0] + P.block_size - 1;;
newNode->end[1] = walker->end[1];
newNode->end[2] = walker->end[2];
if( newNode->start[0] < 0) newNode->start[0] = 0;
}else if(i==1)
{
if(walker->end[0] == in_zz[0] - 1|| walker->start[0] == 0) continue;
newNode->start[0] = walker->start[0] + P.block_size;
newNode->start[1] = walker->start[1];
newNode->start[2] = walker->start[2];
newNode->end[0] = newNode->start[0] + P.block_size - 1;
newNode->end[1] = walker->end[1];
newNode->end[2] = walker->end[2];
if( newNode->end[0] > in_zz[0] - 1) newNode->end[0] = in_zz[0] - 1;
}else if(i==2)
{
if(walker->end[1] == in_zz[1] - 1 || walker->start[1] == 0) continue;
newNode->start[0] = walker->start[0];
newNode->start[1] = walker->start[1] - P.block_size;
newNode->start[2] = walker->start[2];
newNode->end[0] = walker->end[0];
newNode->end[1] = newNode->start[1]+ P.block_size - 1;
newNode->end[2] = walker->end[2];
if(newNode->start[1] < 0) newNode->start[1] = 0;
}else if(i==3)
{
if(walker->end[1] == in_zz[1] - 1 || walker->start[1] == 0) continue;
newNode->start[0] = walker->start[0];
newNode->start[1] = walker->start[1] + P.block_size;
newNode->start[2] = walker->start[2];
newNode->end[0] = walker->end[0];
newNode->end[1] = newNode->start[1] + P.block_size - 1;
newNode->end[2] = walker->end[2];
if( newNode->end[1] > in_zz[1] - 1) newNode->end[1] = in_zz[1] - 1;
}
QString startingpos="", tmps;
tmps.setNum(newNode->start[0]).prepend("x"); startingpos += tmps;
tmps.setNum(newNode->start[1]).prepend("_y"); startingpos += tmps;
QString region_name = startingpos + ".raw";
newNode->tilename = QFileInfo(fileOpenName).path().append("/tmp_NeuronAssembler_NeuTube/").append(QString(region_name));
newNode->ref_index = walker->tc_index;
newNode->next = NULL;
walker_inside->next = newNode;
walker_inside = walker_inside->next;
}
}else
{
ifstream ifs_swc(swcfilename.toStdString().c_str());
if(!ifs_swc)
{
walker = walker->next;
continue;
}
NeuronTree nt = readSWC_file(swcfilename);
if(nt.listNeuron.empty())
{
remove(swcfilename.toStdString().c_str());
walker = walker->next;
continue;
}
NeuronTree nt_pruned = eliminate(nt,3.0);
bool left = 0, right = 0,up = 0,down = 0;
for(V3DLONG i = 0; i < nt_pruned.listNeuron.size(); i++)
{
NeuronSWC curr = nt_pruned.listNeuron.at(i);
newNode = new root_node[1];
if((curr.x < 0.05* P.block_size || P.is_entire ==1) && left == 0 && walker->direction !=2)
{
if(walker->end[0] == in_zz[0] - 1|| walker->start[0] == 0) continue;
newNode->start[0] = walker->start[0] - P.block_size;