forked from liangfu/bet2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
betsurf.cpp
1741 lines (1393 loc) · 48.7 KB
/
betsurf.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
/* BET - Brain Extraction Tool
BETv1 Steve Smith
BETv2 Mickael Pechaud, Mark Jenkinson, Steve Smith
FMRIB Image Analysis Group
Copyright (C) 1999-2003 University of Oxford */
/* Part of FSL - FMRIB's Software Library
http://www.fmrib.ox.ac.uk/fsl
fsl@fmrib.ox.ac.uk
Developed at FMRIB (Oxford Centre for Functional Magnetic Resonance
Imaging of the Brain), Department of Clinical Neurology, Oxford
University, Oxford, UK
LICENCE
FMRIB Software Library, Release 5.0 (c) 2012, The University of
Oxford (the "Software")
The Software remains the property of the University of Oxford ("the
University").
The Software is distributed "AS IS" under this Licence solely for
non-commercial use in the hope that it will be useful, but in order
that the University as a charitable foundation protects its assets for
the benefit of its educational and research purposes, the University
makes clear that no condition is made or to be implied, nor is any
warranty given or to be implied, as to the accuracy of the Software,
or that it will be suitable for any particular purpose or for use
under any specific conditions. Furthermore, the University disclaims
all responsibility for the use which is made of the Software. It
further disclaims any liability for the outcomes arising from using
the Software.
The Licensee agrees to indemnify the University and hold the
University harmless from and against any and all claims, damages and
liabilities asserted by third parties (including claims for
negligence) which arise directly or indirectly from the use of the
Software or the sale of any products based on the Software.
No part of the Software may be reproduced, modified, transmitted or
transferred in any form or by any means, electronic or mechanical,
without the express permission of the University. The permission of
the University is not required if the said reproduction, modification,
transmission or transference is done without financial return, the
conditions of this Licence are imposed upon the receiver of the
product, and all original and amended source code is included in any
transmitted product. You may be held legally responsible for any
copyright infringement that is caused or encouraged by your failure to
abide by these terms and conditions.
You are not permitted under this Licence to use this Software
commercially. Use for which any financial return is received shall be
defined as commercial use, and includes (1) integration of all or part
of the source code or the Software into a product for sale or license
by or on behalf of Licensee to third parties or (2) use of the
Software or any derivative of it for research with the final aim of
developing software products for sale or license to a third party or
(3) use of the Software or any derivative of it for research with the
final aim of developing non-software products for sale or license to a
third party, or (4) use of the Software to provide any service to an
external organisation for which payment is received. If you are
interested in using the Software commercially, please contact Isis
Innovation Limited ("Isis"), the technology transfer company of the
University, to negotiate a licence. Contact details are:
innovation@isis.ox.ac.uk quoting reference DE/9564. */
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include "utils/options.h"
#include "newimage/newimageall.h"
#include "meshclass/meshclass.h"
#include "betsurf.h"
using namespace std;
using namespace NEWIMAGE;
using namespace Utilities;
using namespace mesh;
int infxm;
int infym;
int infzm;
double l;
void noMoreMemory()
{
cerr<<"Unable to satisfy request for memory"<<endl;
abort();
}
string title="BETSURF (BET Surface Finder) v2.1 - FMRIB Analysis Group, Oxford";
string examples=" betsurf [options] <t1> <t2> <bet_mesh.off> <t1_to_standard.mat> <output>\n betsurf --t1only [options] <t1> <bet_mesh.off> <t1_to_standard.mat> <output>";
Option<bool> help(string("-h,--help"), false,
string("displays this help, then exits"),
false, no_argument);
Option<bool> verbose(string("-v,--verbose"), false,
string("switch on diagnostic messages"),
false, no_argument);
Option<bool> t1only(string("-1,--t1only"), false,
string("extraction with t1 only"), false, no_argument);
Option<bool> outline(string("-o,--outline"), false,
string("generates all surface outlines"),
false, no_argument);
Option<bool> mask(string("-m,--mask"), false,
string("generates binary masks from the meshes"),
false, no_argument);
Option<bool> skullmask(string("-s,--skullmask"), false,
string("generates skull binary mask"), false, no_argument);
//Option<bool> csf(string("-c,--csf"), false,
// string("generates an approximation of csf"), false, no_argument);
Option<int> precision(string("-p,--increased_precision"), 0,
string("~<int>\tretessellates the meshes the indicated number of times"), false, requires_argument);
void draw_segment(volume<short>& image, const Pt& p1, const Pt& p2)
{
double xdim = (double) image.xdim();
double ydim = (double) image.ydim();
double zdim = (double) image.zdim();
double mininc = min(xdim,min(ydim,zdim)) * .5;
Vec n = p1 - p2;
double d = n.norm();
n.normalize();
for (double i=0; i<=d; i+=mininc)
{
Pt p = p2 + i* n;
image((int) floor((p.X)/xdim +.5),(int) floor((p.Y)/ydim +.5),(int) floor((p.Z)/zdim +.5)) = 1;
}
}
void draw_mesh(volume<short>& image, const Mesh &m)
{
double xdim = (double) image.xdim();
double ydim = (double) image.ydim();
double zdim = (double) image.zdim();
double mininc = min(xdim,min(ydim,zdim)) * .5;
for (list<Triangle*>::const_iterator i = m._triangles.begin(); i!=m._triangles.end(); i++)
{
Vec n = (*(*i)->get_vertice(0) - *(*i)->get_vertice(1));
double d = n.norm();
n.normalize();
for (double j=0; j<=d; j+=mininc)
{
Pt p = (*i)->get_vertice(1)->get_coord() + j* n;
draw_segment(image, p, (*i)->get_vertice(2)->get_coord());
}
}
}
volume<short> make_mask_from_mesh(const volume<short> & image, const Mesh& m)
{
double xdim = (double) image.xdim();
double ydim = (double) image.ydim();
double zdim = (double) image.zdim();
volume<short> mask = image;
int xsize = mask.xsize();
int ysize = mask.ysize();
int zsize = mask.zsize();
vector<Pt> current;
current.clear();
Pt c(0., 0., 0.);
for (vector<Mpoint *>::const_iterator it=m._points.begin(); it!=m._points.end(); it++)
c+=(*it)->get_coord();
c*=(1./m._points.size());
c.X/=xdim; c.Y/=ydim; c.Z/=zdim;
current.push_back(c);
while (!current.empty())
{
Pt pc = current.back();
int x, y, z;
x=(int) pc.X; y=(int) pc.Y; z=(int) pc.Z;
current.pop_back();
mask.value(x, y, z) = 1;
if (0<=x-1 && mask.value(x-1, y, z)==0) current.push_back(Pt(x-1, y, z));
if (0<=y-1 && mask.value(x, y-1, z)==0) current.push_back(Pt(x, y-1, z));
if (0<=z-1 && mask.value(x, y, z-1)==0) current.push_back(Pt(x, y, z-1));
if (xsize>x+1 && mask.value(x+1, y, z)==0) current.push_back(Pt(x+1, y, z));
if (ysize>y+1 && mask.value(x, y+1, z)==0) current.push_back(Pt(x, y+1, z));
if (zsize>z+1 && mask.value(x, y, z+1)==0) current.push_back(Pt(x, y, z+1));
}
return mask;
}
double standard_step_of_computation(const volume<float> & image, Mesh & m, const int iteration_number, const double E,const double F, const float addsmooth, const float speed, const int nb_iter, const int id, const int od, const bool vol, const volume<short> & mask){
double xdim = image.xdim();
double ydim = image.ydim();
double zdim = image.zdim();
if (nb_iter % 50 == 0)
{
double l2 = 0;
int counter = 0;
for (vector<Mpoint*>::iterator i = m._points.begin(); i!=m._points.end(); i++ )
{
counter++;
l2 += (*i)->medium_distance_of_neighbours();
}
l = l2/counter;
}
if (nb_iter % 100 == 0)
{
for (vector<Mpoint*>::iterator i = m._points.begin(); i!=m._points.end(); i++)
{
Vec n = (*i)->local_normal();
Pt point = (*i)->get_coord();
Pt ipoint(point.X/xdim, point.Y/ydim, point.Z/zdim);
Vec in(n.X/xdim, n.Y/ydim, n.Z/zdim);
double max = 0;
Pt c_m1 = ipoint + (-1) * in;
double current = image.interpolate((c_m1.X),(c_m1.Y),(c_m1.Z));
for (double i2 = 1; i2 < 150; i2+=2)
{
if (max > .1) break;
Pt c_p = ipoint + i2 * in;
double tmpp = image.interpolate((c_p.X),(c_p.Y),(c_p.Z));
double tmp = (tmpp - current) * 100;
max = Max(max, tmp);
current = tmpp;
if (tmpp > .1) {max = 1; break;}
}
if (max < .1)
{
//There is a problem here for precision mode, since with the copy, no guarantee that data is non-zero size
//even if mesh.cpp operator = is modified to copy data, after retesselate "new" points will have zero size data member
if ( (*i)->data.size() ) (*i)->data.pop_back();
(*i)->data.push_back(1);
}
else
{
if ( (*i)->data.size() ) (*i)->data.pop_back();
(*i)->data.push_back(0);
}
}
}
for (vector<Mpoint*>::iterator i = m._points.begin(); i!=m._points.end(); i++)
{
Vec sn, st, u1, u2, u3, u;
double f2, f3=0;
Vec n = (*i)->local_normal();
Vec dv = (*i)->difference_vector();
double tmp = dv|n;
sn = n * tmp;
st = dv - sn;
u1 = st*.5;
double rinv = (2 * fabs(sn|n))/(l*l);
f2 = (1+tanh(F*(rinv - E)))*0.5;
u2 = f2 * sn * addsmooth;
if ((*i)->data.back() == 0)
{
//main term of skull_extraction
{
Pt point = (*i)->get_coord();
Pt ipoint(point.X/xdim, point.Y/ydim, point.Z/zdim);
Vec in(n.X/xdim, n.Y/ydim, n.Z/zdim);
Pt c_m = ipoint + (-1.) * in;
Pt c_p = ipoint + 1. * in;
double tmp = image.interpolate((c_p.X ),( c_p.Y),(c_p.Z));
double gradient = tmp - image.interpolate((c_m.X),(c_m.Y), (c_m.Z));
double tmp2 = gradient*100;
f3 = max(-1., min(tmp2, 1.));
if (tmp2 >= 0 && tmp2 < .1 && tmp < .1 ) f3 = speed;
if (vol)
{
double tmpvol = mask.interpolate((ipoint.X ),(ipoint.Y),(ipoint.Z));
if (tmpvol > .0)
{
f3 = Max(Max(tmpvol*.5, .1), f3);
f2 = 0;
}
}
}
}
else
{
f3 = 0;
Pt point = (*i)->get_coord();
Pt ipoint(point.X/xdim, point.Y/ydim, point.Z/zdim);
double tmpvol = mask.interpolate((ipoint.X ),(ipoint.Y),(ipoint.Z));
if (tmpvol > .0)
{
f3 = Max(tmpvol*.5, .1);
f2 = 0;
}
}
u3 = .05 * f3 * n;
u = u1 + u2 + u3;
(*i)->_update_coord = (*i)->get_coord() + u;
}
m.update();
return (0);
}
//extracts profiles in the area around the eyes
vector<double> t1only_special_extract(const volume<float> & t1, const Pt & point, const Vec & n) {
vector<double> resul;
resul.clear();
bool output = true;
const double INNER_DEPTH = 3;
const double OUTER_DEPTH = 100;
Profile pt1;
for (double d = -INNER_DEPTH; d < OUTER_DEPTH; d+=.5)
{
Pt c = point + d * n;
double tmp1 = t1.interpolate(c.X, c.Y, c.Z);
pt1.add (d, tmp1);
}
pt1.init_roi();
//outer skin
double outskin = pt1.last_point_over(pt1.end(), .2);
double check = pt1.last_point_over(outskin - 1.5, .2);
if (outskin - check > 2) output = false;
pt1.set_rroi(outskin);
double inskull = pt1.next_point_under(-INNER_DEPTH, .25);
if (inskull > 5) inskull = 0;
double outskull = pt1.next_point_over(inskull, .35);
resul.push_back(inskull);
resul.push_back(outskull);
resul.push_back(outskin);
return resul;
}
vector<double> t1only_co_ext(const volume<float> & t1, const Pt & point, const Vec & n) {
vector<double> resul;
resul.clear();
bool output = true;
bool alloutput = true;
const double INNER_DEPTH = 3;
const double OUTER_DEPTH = 60;
Profile pt1;
for (double d = -INNER_DEPTH; d < OUTER_DEPTH; d+=.5)
{
Pt c = point + d * n;
double tmp1 = t1.interpolate(c.X, c.Y, c.Z);
pt1.add (d, tmp1);
}
pt1.init_roi();
//outer skin
double outskin = pt1.last_point_over(pt1.end(), .2);
double check = pt1.last_point_over(outskin - 1.5, .2);
if (outskin - check > 2) outskin = check;
const double OUTER_SKIN = outskin;
pt1.set_rroi(OUTER_SKIN);
double inskull = pt1.next_point_under(pt1.begin(), .25);
pt1.set_lroi(inskull);
if (alloutput)
{
//outer skull
//starting from the skin
double outskull2 = 0;
outskull2 = pt1.last_point_over(outskin, .75);
outskull2 = pt1.last_point_under(outskull2, .20);
//starting from the brain
double minabs = pt1.next_point_under(pt1.begin(), .30);
if (minabs == -500) output = false;
minabs = Max(minabs, -INNER_DEPTH);
double localminabs = minabs;//0
if (output)
{
bool stop = false;
const double lowthreshold = pt1.threshold(.15);
const double upthreshold = pt1.threshold(.60);
int test = 0;
for (vector<pro_pair>::const_iterator i = pt1.v.begin(); i != pt1.v.end(); i++)
{
if (!stop && (*i).abs>=minabs && i!=pt1.v.end() && i!=pt1.v.begin())
{
if ((*i).val>upthreshold) stop = true; //avoid climbing skin
if ((*i).val>lowthreshold) test++;
if ((*i).val<lowthreshold) test--;
if (test < 0) test=0;
if (test == 12) {stop = true;}
if ((*i).val<lowthreshold)
{
localminabs = (*i).abs;
}
}
}
}
double outskull = pt1.next_point_over(localminabs, .15);//.20
if (outskull2 - outskull < -2 || outskull2 - outskull >= 2) {output = false;}
if (outskin - outskull2 < 2) output = false;
if (output)
{
resul.push_back(inskull);
resul.push_back(outskull2);
resul.push_back(outskin);
}
else resul.push_back(outskin);
}
return resul;
}
//writes externall skull computed from image on output.
void t1only_write_ext_skull(volume<float> & output_inskull, volume<float> & output_outskull, volume<float> & output_outskin, const volume<float> & t1, const Mesh & m, const trMatrix & M) {
int glob_counter = 0;
int rem_counter = 0;
const double xdim = t1.xdim();
const double ydim = t1.ydim();
const double zdim = t1.zdim();
double imax = t1.max();
if (imax == 0) imax = 1;
volume<short> meshimage;
copyconvert(t1, meshimage);
meshimage = 0;
draw_mesh(meshimage, m);
for (vector<Mpoint*>::const_iterator i = m._points.begin(); i != m._points.end(); i++)
{
(*i)->data.clear();
double max_neighbour = 0;
const Vec normal = (*i)->local_normal();
const Vec n = Vec(normal.X/xdim, normal.Y/ydim, normal.Z/zdim);
for (list<Mpoint*>::const_iterator nei = (*i)->_neighbours.begin(); nei != (*i)->_neighbours.end(); nei++)
max_neighbour = Max(((**i) - (**nei)).norm(), max_neighbour);
max_neighbour = ceil((max_neighbour)/2);
const Pt mpoint((*i)->get_coord().X/xdim,(*i)->get_coord().Y/ydim,(*i)->get_coord().Z/zdim);
for (int ck = (int)floor(mpoint.Z - max_neighbour/zdim); ck <= (int)floor(mpoint.Z + max_neighbour/zdim); ck++)
for (int cj = (int)floor(mpoint.Y - max_neighbour/ydim); cj <= (int)floor(mpoint.Y + max_neighbour/ydim); cj++)
for (int ci = (int)floor(mpoint.X - max_neighbour/xdim); ci <= (int)floor(mpoint.X + max_neighbour/xdim); ci++)
{
bool compute = false;
const Pt point(ci, cj, ck);
const Pt realpoint(ci*xdim, cj*ydim, ck*zdim);
if (meshimage(ci, cj, ck) == 1)
{
double mindist = 10000;
for (list<Mpoint*>::const_iterator nei = (*i)->_neighbours.begin(); nei != (*i)->_neighbours.end(); nei++)
mindist = Min(((realpoint) - (**nei)).norm(), mindist);
if (mindist >= ((realpoint) - (**i)).norm()) compute = true;
}
if (compute)
{
glob_counter ++;
vector<double> val;
if (!special_case(realpoint, normal, M))
val = t1only_co_ext(t1, point, n);
else
{
val = t1only_special_extract(t1, point, n);
}
if (val.size() == 3)
{
Pt opoint(point.X, point.Y, point.Z);
Vec on(n.X, n.Y, n.Z);
Pt c0 = opoint + val[0]*on;
Pt c1 = opoint + val[1]*on;
Pt c2 = opoint + val[2]*on;
output_inskull((int)floor(c0.X + .5) + infxm,(int) floor(c0.Y + .5) + infym,(int) floor(c0.Z + .5) + infzm) +=1;
output_outskull((int)floor(c1.X + .5) + infxm,(int) floor(c1.Y + .5) + infym,(int) floor(c1.Z + .5) + infzm)+=1;
output_outskin((int)floor(c2.X + .5) + infxm,(int) floor(c2.Y + .5) + infym,(int) floor(c2.Z + .5) + infzm) +=1;
}
else {
rem_counter++;
if (val.size()==1)
{
Pt opoint(point.X, point.Y, point.Z);
Vec on(n.X, n.Y, n.Z);
Pt c0 = opoint + val[0]*on;
output_outskin((int)floor(c0.X + .5) + infxm,(int) floor(c0.Y + .5) + infym,(int) floor(c0.Z + .5) + infzm) +=1;
}
}
}
}
}
if (verbose.value())
{
cout<<" nb of profiles : "<<glob_counter<<endl;
cout<<" removed profiles : "<<100. * rem_counter/(double) glob_counter<<"%"<<endl;
}
}
int t1only_main(int argc, char *argv[], int nb_pars, OptionParser & options){
if (argc - nb_pars < 4)
{
cerr<<"too few arguments"<<endl;
options.usage(); return -1;
}
int count_arg = nb_pars;
const string inputt1(argv[count_arg]);
count_arg++;
const string mesh(argv[count_arg]);
count_arg++;
const string matrix(argv[count_arg]);
count_arg++;
const string outputstr(argv[count_arg]);
//load the mesh
Mesh m;
m.load(mesh);
//load the matrix
trMatrix M;
ifstream f(matrix.c_str());
if (f.is_open())
{
f>>M.m11>>M.m12>>M.m13>>M.m14>>M.m21>>M.m22>>M.m23>>M.m24>>M.m31>>M.m32>>M.m33>>M.m34>>M.m41>>M.m42>>M.m43>>M.m44;
f.close();
}
else {cerr<<"unable to open "<<matrix<<endl;return -1;}
//load the volume
volume<float> t1;
if (read_volume(t1,inputt1.c_str())<0) return -1;
t1.setinterpolationmethod(trilinear);
const double xdim = t1.xdim();
const double ydim = t1.ydim();
const double zdim = t1.zdim();
const int xsize = t1.xsize();
const int ysize = t1.ysize();
const int zsize = t1.zsize();
//founding brain robustmax (useful if skull is too bright)
double thr;
{
volume<short> brain_mask;
copyconvert(t1, brain_mask);
brain_mask = 0;
draw_mesh(brain_mask, m);
brain_mask = make_mask_from_mesh(brain_mask, m);
vector<double> brain_hist;
for (int k = 0; k < zsize; k++)
for(int j = 0; j < ysize; j++)
for(int i = 0; i < xsize; i++)
if (brain_mask.value(i, j, k) == 1) brain_hist.push_back(t1.value(i, j, k));
int size = brain_hist.size();
int e98 = (int) ceil(.98 * size);
nth_element(brain_hist.begin(), brain_hist.begin() + e98 - 1, brain_hist.end());
thr = brain_hist[e98 - 1];
thr *= 1.1;
}
for (int k = 0; k < zsize; k++)
for(int j = 0; j < ysize; j++)
for(int i = 0; i < xsize; i++)
if (t1.value(i, j, k) > thr) t1.value(i, j, k) = thr;
infxm=0; infym=0; infzm=0;
int infxp=0, infyp=0, infzp=0;
//checking if the mesh is inside the volume
for (vector<Mpoint *>::const_iterator p = m._points.begin(); p != m._points.end(); p++)
{
if ((*p)->get_coord().X/xdim < 3) infxm = Max(infxm, (int) ceil(3 - (*p)->get_coord().X/xdim));
if ((*p)->get_coord().Y/ydim < 3) infym = Max(infym, (int) ceil(3 - (*p)->get_coord().Y/ydim));
if ((*p)->get_coord().Z/zdim < 3) infzm = Max(infzm, (int) ceil(3 - (*p)->get_coord().Z/zdim));
if (xsize - (*p)->get_coord().X/xdim< 3) infxp = Max(infxp, (int) ceil(3 - xsize + (*p)->get_coord().X/xdim));
if (ysize - (*p)->get_coord().Y/ydim< 3) infyp = Max(infyp, (int) ceil(3 - ysize + (*p)->get_coord().Y/ydim));
if (zsize - (*p)->get_coord().Z/zdim < 3) infzp = Max(infzp, (int) ceil(3 - zsize + (*p)->get_coord().Z/zdim));
}
infxp += 1;
infxm += 1;
infyp += 1;
infym += 1;
infzp += 1;
infzm += 1;
volume<float> write0(xsize + infxm + infxp, ysize + infym + infyp, zsize + infzm + infzp);
write0.setxdim(xdim);
write0.setydim(ydim);
write0.setzdim(zdim);
//write0.setorigin(infxm * xdim, infym * ydim, infzm * zdim);
write0 = 0;
volume<float> write2;
volume<float> write1;
write1 = write2 = write0;
if (verbose.value()) cout<<"extracting profiles"<<endl;
t1only_write_ext_skull(write0, write1, write2, t1, m, M);
const double rmin=3.33;
const double rmax=10;
const double E = (1/rmin + 1/rmax)/2.;
const double F = 6./(1/rmin - 1/rmax);
if (verbose.value()) cout<<"blurring"<<endl;
const double blurring0 = 3;
const double blurring1 = 3;
const double blurring2 = 3;
const double smoothness0 = 1;
const double smoothness1 = 1;
const double smoothness2 = 1;
write0 = smooth(write0, blurring0);
write2 = smooth(write2, blurring2);
const int nb_iter0 = 800;
const int nb_iter1 = 800;
const int nb_iter2 = 1500;
if (verbose.value()) cout<<"computation"<<endl;
for (vector<Mpoint*>::iterator i = m._points.begin(); i!= m._points.end(); i++)
{
(*i)->data.push_back(0);
}
m.translation(xdim * infxm, ydim * infym, zdim * infzm);
if (verbose.value()) cout<<" inner skull"<<endl;
for (int c = 0; c < nb_iter0; c++)
standard_step_of_computation(write0, m, c, E, F, smoothness0, .5, c);
Mesh mprecise;
if (precision.value() > 0)
{
mprecise = m;
if (verbose.value()) cout << " increased precision" << endl;
for (int i = 0; i < precision.value(); i++)
mprecise.retessellate();
for (int c = 0; c < 100; c++)
standard_step_of_computation(write0, mprecise, c, E, F, smoothness0, .5, c);
}
Mesh realmesh;
if (precision.value() == 0)
realmesh = m;
else realmesh = mprecise;
realmesh.translation(- xdim * infxm, - ydim * infym, - zdim * infzm);
realmesh.save((outputstr+"_inskull_mesh.off").c_str());
volume<short> maskinskull;
volume<short> maskinskull2;
maskinskull2 = 0;
{
copyconvert(write0, maskinskull);
draw_mesh(maskinskull, m);
maskinskull = make_mask_from_mesh(maskinskull, m);
volume<short> output1;
copyconvert(t1, output1);
output1 = 0;
if (outline.value() | mask.value()| skullmask.value())
{
draw_mesh(output1, realmesh);
}
if (outline.value())
{
if (verbose.value()) cout<<" outline"<<endl;
if (save_volume(output1, (outputstr+"_inskull_mesh").c_str())<0) return -1;
}
if (mask.value() | skullmask.value())
{
volume<short> smask = make_mask_from_mesh(output1, realmesh);
if (skullmask.value())
{
maskinskull2 = smask;
}
if (mask.value())
{
if (verbose.value()) cout<<" mask"<<endl;
if (save_volume(smask, (outputstr+"_inskull_mask").c_str())<0) return -1;
}
}
}
{
int xsiz = write1.xsize();
int ysiz = write1.ysize();
int zsiz = write1.zsize();
for (int k = 0; k < zsiz; k++)
for (int j = 0; j < ysiz; j++)
for (int i = 0; i < xsiz; i++)
if (write1.value(i, j, k) > 0 && maskinskull.value(i, j, k) == 1)
{
write1.value(i, j, k) = 0;
}
}
write1 = smooth(write1, blurring1);
if (verbose.value()) cout<<" outer skull"<<endl;
for (int c = 0; c < nb_iter1; c++)
standard_step_of_computation(write1, m, c, E, F, smoothness1, .5, c, 5, 15, true, maskinskull);
maskinskull.destroy();
if (precision.value() > 0)
{
if (verbose.value()) cout<<" increased precision"<<endl;
mprecise = m;
for (int i = 0; i < precision.value(); i++)
mprecise.retessellate();
for (int c = 0; c < 100; c++)
standard_step_of_computation(write1, mprecise, c, E, F, smoothness1, .5, c, 5, 15);
}
write1.destroy();
Mesh realmesh2;
if (precision.value() == 0)
realmesh2 = m;
else realmesh2 = mprecise;
realmesh2.translation(- xdim * infxm, - ydim * infym, - zdim * infzm);
realmesh2.save((outputstr+"_outskull_mesh.off").c_str());
volume<short> maskoutskull;
maskoutskull = 0;
volume<short> maskoutskull2;
maskoutskull2 = 0;
volume<short> meshoutskull;
meshoutskull = 0;
{
copyconvert(write0, maskoutskull);
write0.destroy();
draw_mesh(maskoutskull, m);
maskoutskull = make_mask_from_mesh(maskoutskull, m);
volume<short> output1;
copyconvert(t1, output1);
output1 = 0;
if (mask.value() | outline.value() | skullmask.value())
draw_mesh(output1, realmesh2);
if (skullmask.value())
meshoutskull = output1;
if (outline.value())
{
if (verbose.value()) cout<<" outline"<<endl;
if (save_volume(output1, (outputstr+"_outskull_mesh").c_str())<0) return -1;
}
if (mask.value()|skullmask.value())
{
volume<short> smask = make_mask_from_mesh(output1, realmesh2);
if (skullmask.value())
{
maskoutskull2 = smask;
}
if (mask.value())
{
if (verbose.value()) cout<<" mask"<<endl;
if (save_volume(smask, (outputstr+"_outskull_mask").c_str())<0) return -1;
}
}
}
if (skullmask.value())
{
volume<short> smask = maskoutskull2;
smask = 0;
int xsize = t1.xsize(), ysize = t1.ysize(), zsize = t1.zsize();
if (verbose.value()) cout<<" skull mask"<<endl;
for (int k = 0; k < zsize; k++)
for(int j = 0; j < ysize; j++)
for(int i = 0; i < xsize; i++)
smask.value(i, j, k) = Min (1, maskoutskull2.value(i, j, k) * (1 - maskinskull2.value(i, j, k)) + meshoutskull.value(i, j, k));
if (save_volume(smask, (outputstr+"_skull_mask").c_str())<0) return -1;
}
meshoutskull.destroy();
maskoutskull2.destroy();
maskinskull2.destroy();
//computing the starting mesh for outer skull
if (verbose.value()) cout<<" skin"<<endl;
Mesh m2;
make_mesh_from_icosa(5, m2);
for (vector<Mpoint*>::iterator i = m2._points.begin(); i!= m2._points.end(); i++)
{
(*i)->data.push_back(0);
}
Pt p;
int counter=0;
for (vector<Mpoint*>::const_iterator i = m._points.begin(); i!= m._points.end(); i++)
{
counter++;
p+=(*i)->get_coord();
}
p*=(1./counter);
double radius = 0;
for (vector<Mpoint*>::const_iterator i = m._points.begin(); i!= m._points.end(); i++)
radius+=((*i)->get_coord()-p).norm();
radius/=(counter);
radius*=.75;
m2.rescale(radius);
m2.translation(p.X, p.Y, p.Z);
for (int c = 0; c < nb_iter2; c++)
standard_step_of_computation(write2, m2, c, E, F, smoothness2, 1.5, c, 5, 15, true, maskoutskull);
maskoutskull.destroy();
if (precision.value() > 0)
{
if (verbose.value()) cout<<" increased precision"<<endl;
for (int i = 0; i < precision.value(); i++)
m2.retessellate();
for (int c = 0; c < 100; c++)
standard_step_of_computation(write2, m2, c, E, F, smoothness2, 1.5, c, 5, 15);
}
write2.destroy();
m2.translation(-xdim * infxm, -ydim * infym, -zdim * infzm);
m2.save((outputstr+"_outskin_mesh.off").c_str());
if (outline.value() | mask.value())
{
volume<short> output1;
copyconvert(t1, output1);
output1 = 0;
draw_mesh(output1, m2);
if (outline.value())
{
if (verbose.value()) cout<<" outline"<<endl;
if (save_volume(output1, (outputstr+"_outskin_mesh").c_str())<0) return -1;
}
if (mask.value())
{
if (verbose.value()) cout<<" mask"<<endl;
volume<short> mask = make_mask_from_mesh(output1, m);
if (save_volume(mask, (outputstr+"_outskin_mask").c_str())<0) return -1;
}
}
return 0;
}
//extracts profiles in the area around the eyes
vector<double> special_extract(const volume<float> & t1, const volume<float> & t2, const Pt & point, const Vec & n, volume<short> & csfvolume) {
vector<double> resul;
resul.clear();
bool alloutput = true;
const double INNER_DEPTH = 3;
const double OUTER_DEPTH = 100;
Profile pt1;
Profile pt2;
for (double d = -30; d < OUTER_DEPTH; d+=.5)
{
Pt c = point + d * n;
double tmp1 = t1.interpolate(c.X, c.Y, c.Z);
double tmp2 = t2.interpolate(c.X, c.Y, c.Z);
pt1.add (d, tmp1);
pt2.add (d, tmp2);
}
pt1.init_roi();
pt2.init_roi();
pt1.set_lroi(-INNER_DEPTH);
pt2.set_lroi(-INNER_DEPTH);
double inskull=pt1.next_point_under(pt1.begin(), .25);
double inskullt1 = pt2.next_point_under(inskull, .30);
if (inskullt1 > inskull) inskull = inskullt1;
if (inskull > 15) inskull = 0;
//outer skin
double outskin = pt1.last_point_over(pt1.end(), .2);
double check = pt1.last_point_over(outskin - 1.5, .2);
if (outskin - check > 2) alloutput = false;
pt1.set_rroi(outskin);
double val = pt1.next_point_under(inskull, .25);
double outskull = pt1.next_point_over(val, .35);
if (val - inskull >= 7) outskull = inskull + .5;
if (outskull - inskull < 0) outskull = inskull + .5;