This repository has been archived by the owner on Jul 16, 2022. It is now read-only.
/
Anatomy.cpp
executable file
·1963 lines (1654 loc) · 67.9 KB
/
Anatomy.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
/*
* The Anatomy class implementation.
*
*/
#include "Anatomy.h"
#include "Fibers.h"
#include "../Logger.h"
#include "../main.h"
#include "../dataset/DatasetManager.h"
#include "../gui/MainFrame.h"
#include "../gui/SceneManager.h"
#include "../gui/SelectionObject.h"
#include "../misc/lic/TensorField.h"
#include "../misc/nifti/nifti1_io.h"
#include <GL/glew.h>
#include <wx/textfile.h>
#include <wx/tglbtn.h>
#include <wx/xml/xml.h>
#include <algorithm>
using std::fill;
#include <cassert>
#include <sstream>
using std::ostringstream;
#include <stack>
using std::stack;
#include <vector>
using std::vector;
#define MIN_HEADER_SIZE 348
#define NII_HEADER_SIZE 352
#define LOWER_EQ_THRES 20
#define UPPER_EQ_THRES 255
Anatomy::Anatomy( )
: DatasetInfo(),
m_isSegmentOn( false ),
m_pRoi( NULL ),
m_dataType( 2 ),
m_pTensorField( NULL ),
m_useEqualizedDataset( false ),
m_lowerEqThreshold( LOWER_EQ_THRES ),
m_upperEqThreshold( UPPER_EQ_THRES ),
m_currentLowerEqThreshold( -1 ),
m_currentUpperEqThreshold( -1 ),
m_originalAxialOrientation( ORIENTATION_UNDEFINED )
{
m_bands = 1;
}
Anatomy::Anatomy( const wxString &filename )
: DatasetInfo(),
m_isSegmentOn( false ),
m_pRoi( NULL ),
m_dataType( 2 ),
m_pTensorField( NULL ),
m_useEqualizedDataset( false ),
m_lowerEqThreshold( LOWER_EQ_THRES ),
m_upperEqThreshold( UPPER_EQ_THRES ),
m_currentLowerEqThreshold( -1 ),
m_currentUpperEqThreshold( -1 ),
m_originalAxialOrientation( ORIENTATION_UNDEFINED )
{
m_bands = 1;
m_fullPath = filename;
#ifdef __WXMSW__
m_name = filename.AfterLast( '\\' );
#else
m_name = filename.AfterLast( '/' );
#endif
}
// Seems to be used for the create a Distance Map
Anatomy::Anatomy( const Anatomy * const pAnatomy )
: DatasetInfo(),
m_isSegmentOn( false ),
m_pRoi( NULL ),
m_dataType( 2 ),
m_pTensorField( NULL ),
m_useEqualizedDataset( false ),
m_lowerEqThreshold( LOWER_EQ_THRES ),
m_upperEqThreshold( UPPER_EQ_THRES ),
m_currentLowerEqThreshold( -1 ),
m_currentUpperEqThreshold( -1 ),
m_originalAxialOrientation( ORIENTATION_UNDEFINED )
{
m_columns = pAnatomy->m_columns;
m_rows = pAnatomy->m_rows;
m_frames = pAnatomy->m_frames;
m_bands = 1;
m_isLoaded = true;
m_type = HEAD_BYTE;
createOffset( pAnatomy );
}
Anatomy::Anatomy( std::vector< float >* pDataset,
const int sample )
: DatasetInfo(),
m_isSegmentOn( false ),
m_pRoi( NULL ),
m_dataType( 2 ),
m_pTensorField( NULL ),
m_useEqualizedDataset( false ),
m_lowerEqThreshold( LOWER_EQ_THRES ),
m_upperEqThreshold( UPPER_EQ_THRES ),
m_currentLowerEqThreshold( -1 ),
m_currentUpperEqThreshold( -1 ),
m_originalAxialOrientation( ORIENTATION_UNDEFINED )
{
m_columns = DatasetManager::getInstance()->getColumns();
m_rows = DatasetManager::getInstance()->getRows();
m_frames = DatasetManager::getInstance()->getFrames();
m_bands = 1;
m_type = HEAD_BYTE;
m_isLoaded = true;
m_floatDataset.resize( m_columns * m_frames * m_rows );
std::copy( pDataset->begin(), pDataset->end(), m_floatDataset.begin() );
}
Anatomy::Anatomy( const int type )
: DatasetInfo(),
m_isSegmentOn( false ),
m_pRoi( NULL ),
m_dataType( 2 ),
m_pTensorField( NULL ),
m_useEqualizedDataset( false ),
m_lowerEqThreshold( LOWER_EQ_THRES ),
m_upperEqThreshold( UPPER_EQ_THRES ),
m_currentLowerEqThreshold( -1 ),
m_currentUpperEqThreshold( -1 ),
m_originalAxialOrientation( ORIENTATION_UNDEFINED )
{
m_columns = DatasetManager::getInstance()->getColumns();
m_rows = DatasetManager::getInstance()->getRows();
m_frames = DatasetManager::getInstance()->getFrames();
m_voxelSizeX = DatasetManager::getInstance()->getVoxelX();
m_voxelSizeY = DatasetManager::getInstance()->getVoxelY();
m_voxelSizeZ = DatasetManager::getInstance()->getVoxelZ();
if(type == RGB)
{
m_bands = 3;
m_isLoaded = true;
m_type = type;
m_floatDataset.resize( m_columns * m_frames * m_rows * 3, 0.0f );
}
else if(type == HEAD_BYTE)
{
m_bands = 1;
m_isLoaded = true;
m_type = type;
m_floatDataset.resize( m_columns * m_frames * m_rows, 0.0f );
}
else
{
// Only compiled and runned in debug
assert(false);
}
}
void Anatomy::add( Anatomy* pAnatomy )
{
for( unsigned int i = 0; i < m_floatDataset.size(); ++i )
{
m_floatDataset[i] += pAnatomy->m_floatDataset[i];
}
}
//////////////////////////////////////////////////////////////////////////
float Anatomy::at( const int pos ) const
{
return m_floatDataset[pos];
}
//////////////////////////////////////////////////////////////////////////
std::vector< float >* Anatomy::getFloatDataset()
{
return &m_floatDataset;
}
//////////////////////////////////////////////////////////////////////////
std::vector< float >* Anatomy::getEqualizedDataset()
{
return &m_equalizedDataset;
}
//////////////////////////////////////////////////////////////////////////
GLuint Anatomy::getGLuint()
{
if(0 == m_GLuint)
{
generateTexture();
}
return m_GLuint;
}
//////////////////////////////////////////////////////////////////////////
void Anatomy::setZero( const int sizeX,
const int sizeY,
const int sizeZ )
{
m_columns = sizeX;
m_rows = sizeY;
m_frames = sizeZ;
m_bands = 1;
int datasetSize = m_rows * m_columns * m_frames;
m_floatDataset.clear();
m_floatDataset.resize( datasetSize, 0.0f );
m_equalizedDataset.clear();
m_equalizedDataset.resize( datasetSize, 0.0f );
}
//////////////////////////////////////////////////////////////////////////
void Anatomy::setRGBZero( const int sizeX,
const int sizeY,
const int sizeZ )
{
m_columns = sizeX;
m_rows = sizeY;
m_frames = sizeZ;
m_bands = 3;
int datasetSize = m_rows * m_columns * m_frames;
m_floatDataset.clear();
m_floatDataset.resize( datasetSize * m_bands, 0.0f );
m_equalizedDataset.clear();
m_equalizedDataset.resize( datasetSize * m_bands, 0.0f );
m_dataType = 2;
m_type = RGB;
}
//////////////////////////////////////////////////////////////////////////
TensorField* Anatomy::getTensorField()
{
return m_pTensorField;
}
//////////////////////////////////////////////////////////////////////////
void Anatomy::dilate()
{
int datasetSize(m_columns * m_rows * m_frames);
std::vector<bool> tmp( datasetSize, false );
int curIndex;
for( int c(1); c < m_columns - 1; ++c )
{
for( int r(1); r < m_rows - 1; ++r )
{
for( int f(1); f < m_frames - 1; ++f )
{
curIndex = c + r * m_columns + f * m_columns * m_rows;
if( m_floatDataset[curIndex] == 1.0f )
{
dilateInternal( tmp, curIndex );
}
}
}
}
if( m_equalizedDataset.size() != m_floatDataset.size() )
{
m_equalizedDataset.resize( m_floatDataset.size() );
}
for( int i(0); i < datasetSize; ++i )
{
if ( tmp[i] )
{
m_floatDataset[i] = 1.0f;
m_equalizedDataset[i] = 1.0f;
}
}
const GLuint* pTexId = &m_GLuint;
glDeleteTextures( 1, pTexId );
Logger::getInstance()->printIfGLError( wxT( "Anatomy::dilate - glDeleteTextures") );
generateTexture();
}
//////////////////////////////////////////////////////////////////////////
void Anatomy::erode()
{
int datasetSize = m_columns * m_rows * m_frames;
std::vector<bool> tmp( datasetSize, false );
int curIndex;
for( int c(1); c < m_columns - 1; ++c )
{
for( int r(1); r < m_rows - 1; ++r )
{
for( int f(1); f < m_frames - 1; ++f )
{
curIndex = c + r * m_columns + f * m_columns * m_rows;
if( m_floatDataset[curIndex] == 1.0f )
{
erodeInternal(tmp, curIndex );
}
}
}
}
if( m_equalizedDataset.size() != m_floatDataset.size() )
{
m_equalizedDataset.resize( m_floatDataset.size() );
}
for( int i(0); i < datasetSize; ++i )
{
if( !tmp[i] )
{
m_floatDataset[i] = 0.0f;
m_equalizedDataset[i] = 0.0f;
}
}
const GLuint* pTexId = &m_GLuint;
glDeleteTextures( 1, pTexId );
Logger::getInstance()->printIfGLError( wxT( "Anatomy::erode - glDeleteTextures") );
generateTexture();
}
//////////////////////////////////////////////////////////////////////////
void Anatomy::minimize()
{
if( !DatasetManager::getInstance()->isFibersLoaded() )
{
return;
}
std::vector<bool> workData( m_columns * m_rows * m_frames, false );
long index = MyApp::frame->getCurrentListIndex();
if( -1 != index )
{
Fibers* pFibers = DatasetManager::getInstance()->getSelectedFibers( MyApp::frame->m_pListCtrl->GetItem( index ) );
int curX, curY, curZ, index;
for( int i(0); i < pFibers->getLineCount(); ++i )
{
if( pFibers->isSelected( i ) )
{
for( int j = pFibers->getStartIndexForLine( i );
j < ( pFibers->getStartIndexForLine( i ) + ( pFibers->getPointsPerLine( i )) ); j += 3 )
{
// TODO: Verify that changing from dh to current obj m_rows & al. is ok
curX = std::min( m_columns - 1, std::max( 0, (int)( pFibers->getPointValue( j * 3 ) / m_voxelSizeX ) ) ); // m_dh->m_xVoxel ) );
curY = std::min( m_rows - 1, std::max( 0, (int)( pFibers->getPointValue( j * 3 + 1) / m_voxelSizeY ) ) ); // m_dh->m_yVoxel ) );
curZ = std::min( m_frames - 1, std::max( 0, (int)( pFibers->getPointValue( j * 3 + 2) / m_voxelSizeZ ) ) ); // m_dh->m_zVoxel ) );
index = curX + curY * m_columns + curZ * m_rows * m_columns;
workData[index] = true;
}
}
}
int indx = DatasetManager::getInstance()->createAnatomy();
Anatomy* pNewAnatomy = (Anatomy *)DatasetManager::getInstance()->getDataset( indx );
pNewAnatomy->setZero( m_columns, m_rows, m_frames );
std::vector<float> *pNewAnatDataset = pNewAnatomy->getFloatDataset();
for( int i(0); i < m_columns * m_rows * m_frames; ++i )
{
if( workData[i] && m_floatDataset[i] > 0.0f )
{
pNewAnatDataset->at( i ) = 1.0;
}
}
pNewAnatomy->setName( getName() + _T( "(minimal)" ) );
pNewAnatomy->setType( HEAD_BYTE );
pNewAnatomy->setDataType( 2 );
MyApp::frame->m_pListCtrl->InsertItem( indx );
}
}
//////////////////////////////////////////////////////////////////////////
void Anatomy::flipAxis( AxisType axe )
{
flipAxisInternal( axe, true );
}
void Anatomy::flipAxisInternal( AxisType axe, const bool regenerateDisplayObjects )
{
float tmp;
int curIndex;
int flipIndex;
int row(m_rows);
int col(m_columns);
int frames(m_frames);
switch (axe)
{
case X_AXIS:
col /= 2;
break;
case Y_AXIS:
row /= 2;
break;
case Z_AXIS:
frames /= 2;
break;
default:
Logger::getInstance()->print( wxT( "Cannot flip axis. The given axis is undefined." ), LOGLEVEL_ERROR );
return;
}
for( int f(0); f < frames; ++f )
{
for( int r(0); r < row; ++r )
{
for( int c(0); c < col; ++c )
{
curIndex = (c + r * m_columns + f * m_columns * m_rows) * m_bands;
//Compute the index of the value that will be replaced by the one defined by our current index
switch (axe)
{
case X_AXIS:
flipIndex = ((m_columns - 1 - c) + r * m_columns + f * m_columns * m_rows) * m_bands;
break;
case Y_AXIS:
flipIndex = (c + (m_rows - 1 - r) * m_columns + f * m_columns * m_rows) * m_bands;
break;
case Z_AXIS:
flipIndex = (c + r * m_columns + (m_frames - 1 - f) * m_columns * m_rows) * m_bands;
break;
default:
break;
}
for ( int i(0); i < m_bands; ++i )
{
tmp = m_floatDataset[curIndex + i];
m_floatDataset[curIndex + i] = m_floatDataset[flipIndex + i];
m_floatDataset[flipIndex + i] = tmp;
}
}
}
}
if( 0 != m_equalizedDataset.size() )
{
equalizeHistogram();
}
if( regenerateDisplayObjects )
{
const GLuint* pTexId = &m_GLuint;
glDeleteTextures( 1, pTexId );
Logger::getInstance()->printIfGLError( wxT( "Anatomy::flipAxis - glDeleteTextures") );
generateTexture();
}
}
//////////////////////////////////////////////////////////////////////////
bool Anatomy::load( nifti_image *pHeader, nifti_image *pBody )
{
m_columns = pHeader->dim[1];
m_rows = pHeader->dim[2];
m_frames = pHeader->dim[3];
m_bands = pHeader->dim[4];
m_dataType = pHeader->datatype;
// Fix the case where some nifti files have a value of 0 for the fourth
// dimension. It is a valid case, but some of the code use the number of bands
// to process the data indenpendantly from its dimension.
if( m_bands == 0 )
{
m_bands = 1;
}
m_voxelSizeX = pHeader->dx;
m_voxelSizeY = pHeader->dy;
m_voxelSizeZ = pHeader->dz;
if( DatasetManager::getInstance()->isAnatomyLoaded() )
{
int columns = DatasetManager::getInstance()->getColumns();
int rows = DatasetManager::getInstance()->getRows();
int frames = DatasetManager::getInstance()->getFrames();
float voxelX = DatasetManager::getInstance()->getVoxelX();
float voxelY = DatasetManager::getInstance()->getVoxelY();
float voxelZ = DatasetManager::getInstance()->getVoxelZ();
const float VOXEL_SIZE_EPSILON(0.0001f);
if( m_rows != rows || m_columns != columns || m_frames != frames )
{
Logger::getInstance()->print( wxT( "Dimensions of loaded files must be the same" ), LOGLEVEL_ERROR );
return false;
}
if( m_voxelSizeX != voxelX || m_voxelSizeY != voxelY || m_voxelSizeZ != voxelZ )
{
// NOTE TO THE TEAM: THIS IS NOT A VERY GOOD THING TO DO. We do it to support
// different software that save the metadata with incorrect rounding / conversion.
if( std::abs(m_voxelSizeX - voxelX) < VOXEL_SIZE_EPSILON &&
std::abs(m_voxelSizeY - voxelY) < VOXEL_SIZE_EPSILON &&
std::abs(m_voxelSizeZ - voxelZ) < VOXEL_SIZE_EPSILON )
{
// In this case, we are in the expected error range between something coming from an integer
// and something coming from a float. We accept it, make sure thesizes fit for the new anatomy,
// and still display a debug message for developers.
m_voxelSizeX = voxelX;
m_voxelSizeY = voxelY;
m_voxelSizeZ = voxelZ;
Logger::getInstance()->print( wxT( "Voxel sizes did not exactly fit. In expected float range error. Using the already loaded voxel sizes." ),
LOGLEVEL_DEBUG );
}
else
{
Logger::getInstance()->print( wxT( "Voxel size different from anatomy" ), LOGLEVEL_ERROR );
return false;
}
}
}
// Get the transformation to put the anatomy file in world space.
// The transformation used depends on the one used in the nifti image.
// We currently only use it when loading Mrtrix fibers.
if( pHeader->sform_code > 0 )
{
FMatrix &transform = DatasetManager::getInstance()->getNiftiTransform();
transform( 0, 0 ) = pHeader->sto_xyz.m[0][0];
transform( 0, 1 ) = pHeader->sto_xyz.m[0][1];
transform( 0, 2 ) = pHeader->sto_xyz.m[0][2];
transform( 0, 3 ) = pHeader->sto_xyz.m[0][3];
transform( 1, 0 ) = pHeader->sto_xyz.m[1][0];
transform( 1, 1 ) = pHeader->sto_xyz.m[1][1];
transform( 1, 2 ) = pHeader->sto_xyz.m[1][2];
transform( 1, 3 ) = pHeader->sto_xyz.m[1][3];
transform( 2, 0 ) = pHeader->sto_xyz.m[2][0];
transform( 2, 1 ) = pHeader->sto_xyz.m[2][1];
transform( 2, 2 ) = pHeader->sto_xyz.m[2][2];
transform( 2, 3 ) = pHeader->sto_xyz.m[2][3];
transform( 3, 0 ) = pHeader->sto_xyz.m[3][0];
transform( 3, 1 ) = pHeader->sto_xyz.m[3][1];
transform( 3, 2 ) = pHeader->sto_xyz.m[3][2];
transform( 3, 3 ) = pHeader->sto_xyz.m[3][3];
}
else if( pHeader->qform_code > 0 )
{
FMatrix &transform = DatasetManager::getInstance()->getNiftiTransform();
transform( 0, 0 ) = pHeader->qto_xyz.m[0][0];
transform( 0, 1 ) = pHeader->qto_xyz.m[0][1];
transform( 0, 2 ) = pHeader->qto_xyz.m[0][2];
transform( 0, 3 ) = pHeader->qto_xyz.m[0][3];
transform( 1, 0 ) = pHeader->qto_xyz.m[1][0];
transform( 1, 1 ) = pHeader->qto_xyz.m[1][1];
transform( 1, 2 ) = pHeader->qto_xyz.m[1][2];
transform( 1, 3 ) = pHeader->qto_xyz.m[1][3];
transform( 2, 0 ) = pHeader->qto_xyz.m[2][0];
transform( 2, 1 ) = pHeader->qto_xyz.m[2][1];
transform( 2, 2 ) = pHeader->qto_xyz.m[2][2];
transform( 2, 3 ) = pHeader->qto_xyz.m[2][3];
transform( 3, 0 ) = pHeader->qto_xyz.m[3][0];
transform( 3, 1 ) = pHeader->qto_xyz.m[3][1];
transform( 3, 2 ) = pHeader->qto_xyz.m[3][2];
transform( 3, 3 ) = pHeader->qto_xyz.m[3][3];
}
else
{
Logger::getInstance()->print( wxT( "No transformation encoded in the nifti file. Using identity transform." ), LOGLEVEL_WARNING );
// This is not a typo, the method is called makeIdendity in FMatrix.
DatasetManager::getInstance()->getNiftiTransform().makeIdendity();
}
// Guess the original data orientation from the transformation matrix.
if( pHeader->sform_code > 0 )
{
if( pHeader->sto_xyz.m[0][0] < 0.0 )
{
m_originalAxialOrientation = ORIENTATION_RIGHT_TO_LEFT;
}
else
{
m_originalAxialOrientation = ORIENTATION_LEFT_TO_RIGHT;
}
}
else if( pHeader->qform_code > 0 )
{
if( pHeader->qto_xyz.m[0][0] < 0.0 )
{
m_originalAxialOrientation = ORIENTATION_RIGHT_TO_LEFT;
}
else
{
m_originalAxialOrientation = ORIENTATION_LEFT_TO_RIGHT;
}
}
// Check the data type.
if( pHeader->datatype == 2 )
{
if( pHeader->dim[4] == 1 )
{
m_type = HEAD_BYTE;
}
else if( pHeader->dim[4] == 3 )
{
m_type = RGB;
}
else
{
m_type = BOT_INITIALIZED;
}
}
else if( pHeader->datatype == 4 )
{
m_type = HEAD_SHORT;
}
else if( pHeader->datatype == 16 )
{
if( pHeader->dim[4] == 3 )
{
m_type = VECTORS;
}
else
{
m_type = OVERLAY;
}
}
else
{
m_type = BOT_INITIALIZED;
}
// nifti_image *pFileData = nifti_image_read( pHdrFile, 1 );
// if( !pFileData )
// {
// m_dh->m_lastError = wxT( "nifti file corrupt" );
// return false;
// }
int datasetSize = pHeader->dim[1] * pHeader->dim[2] * pHeader->dim[3];
bool flag = false;
switch( m_type )
{
case HEAD_BYTE:
{
unsigned char* pData = (unsigned char*)pBody->data;
m_floatDataset.resize( datasetSize );
for( int i(0); i < datasetSize; ++i )
{
m_floatDataset[i] = (float)pData[i] / 255.0;
}
flag = true;
m_oldMax = 255;
break;
}
case HEAD_SHORT:
{
short int* pData = (short int*)pBody->data;
int dataMax = 0;
std::vector<int> histo( 65536, 0 );
for( int i(0); i < datasetSize; ++i )
{
dataMax = wxMax(dataMax, pData[i]);
++histo[pData[i]];
}
int fivePercent = (int)( datasetSize * 0.001 );
int newMax = 65535;
int adder = 0;
for( int i(65535); i > 0; --i )
{
adder += histo[i];
newMax = i;
if( adder > fivePercent )
{
break;
}
}
for( int i(0); i < datasetSize; ++i )
{
if ( pData[i] > newMax )
{
pData[i] = newMax;
}
}
m_floatDataset.resize( datasetSize );
for( int i(0); i < datasetSize; ++i )
{
m_floatDataset[i] = (float)pData[i] / (float)newMax;
}
m_oldMax = dataMax;
m_newMax = newMax;
flag = true;
break;
}
case OVERLAY:
{
float* pData = (float*)pBody->data;
m_floatDataset.resize( datasetSize );
for( int i(0); i < datasetSize; ++i )
{
m_floatDataset[i] = (float)pData[i];
}
float dataMax = 0.0f;
for( int i(0); i < datasetSize; ++i )
{
if (m_floatDataset[i] > dataMax)
{
dataMax = m_floatDataset[i];
}
}
for( int i(0); i < datasetSize; ++i )
{
m_floatDataset[i] = m_floatDataset[i] / dataMax;
}
m_oldMax = dataMax;
m_newMax = 1.0;
flag = true;
break;
}
case RGB:
{
unsigned char* pData = (unsigned char*)pBody->data;
m_floatDataset.resize( datasetSize * 3 );
for( int i(0); i < datasetSize; ++i )
{
m_floatDataset[i * 3] = (float)pData[i] / 255.0f;
m_floatDataset[i * 3 + 1] = (float)pData[datasetSize + i] / 255.0f;
m_floatDataset[i * 3 + 2] = (float)pData[(2 * datasetSize) + i] / 255.0f;
}
flag = true;
break;
}
case VECTORS:
{
float* pData = (float*)pBody->data;
m_floatDataset.resize( datasetSize * 3 );
for( int i(0); i < datasetSize; ++i )
{
m_floatDataset[i * 3] = pData[i];
m_floatDataset[i * 3 + 1] = pData[datasetSize + i];
m_floatDataset[i * 3 + 2] = pData[2 * datasetSize + i];
}
flag = true;
break;
}
default:
{
Logger::getInstance()->print( wxT( "Unsupported file format" ), LOGLEVEL_ERROR );
flag = false;
// Will not return now to make sure the pHdrFile pointer is freed.
}
}
m_isLoaded = flag;
// Flip the data if needed.
if( m_originalAxialOrientation == ORIENTATION_RIGHT_TO_LEFT )
{
flipAxisInternal( X_AXIS, false );
}
if( m_isLoaded && m_type == VECTORS )
{
m_pTensorField = new TensorField( m_columns, m_rows, m_frames, &m_floatDataset, 1, 3 );
}
return flag;
}
//////////////////////////////////////////////////////////////////////////
bool Anatomy::save( wxXmlNode *pNode ) const
{
assert( pNode != NULL );
pNode->SetName( wxT( "dataset" ) );
DatasetInfo::save( pNode );
return true;
}
//////////////////////////////////////////////////////////////////////////
void Anatomy::saveNifti( wxString fileName )
{
// Prevents copying the whole vector
vector<float> *pDataset = m_useEqualizedDataset ? &m_equalizedDataset : &m_floatDataset;
int dims[] = { 4, m_columns, m_rows, m_frames, m_bands, 0, 0, 0 };
nifti_image* pImage(NULL);
pImage = nifti_make_new_nim( dims, m_dataType, 1 );
if( !fileName.EndsWith( _T( ".nii" ) ) && !fileName.EndsWith( _T( ".nii.gz" ) ) )
{
fileName += _T( ".nii.gz" );
}
char fn[1024];
strcpy( fn, (const char*)fileName.mb_str( wxConvUTF8 ) );
pImage->qform_code = 1;
pImage->datatype = m_dataType;
pImage->fname = fn;
pImage->dx = m_voxelSizeX;
pImage->dy = m_voxelSizeY;
pImage->dz = m_voxelSizeZ;
if( m_type == HEAD_BYTE )
{
vector<unsigned char> tmp( pDataset->size() );
for(unsigned int i(0); i < pDataset->size(); ++i )
{
tmp[i] = (*pDataset)[i] * 255;
}
// Do not move the call to nifti_image_write out of the
// if, because it will crash, since the temp vector will
// not exist anymore, and pImage->data will point to garbage.
pImage->data = &tmp[0];
nifti_image_write( pImage );
}
else if( m_type == HEAD_SHORT )
{
vector<short> tmp( pDataset->size() );
for(unsigned int i(0); i < pDataset->size(); ++i )
{
tmp[i] = (short)( (*pDataset)[i] * m_newMax );
}
// Do not move the call to nifti_image_write out of the
// if, because it will crash, since the temp vector will
// not exist anymore, and pImage->data will point to garbage.
pImage->data = &tmp[0];
nifti_image_write( pImage );
}
else if( m_type == RGB )
{
vector<unsigned char> tmp( pDataset->size() );
int datasetSize = pDataset->size()/3;
for( int i(0); i < datasetSize; ++i )
{
tmp[i] = (*pDataset)[i * 3] * 255.0f;
tmp[datasetSize + i] = (*pDataset)[i * 3 + 1] * 255.0f;
tmp[2 * datasetSize + i] = (*pDataset)[i * 3 + 2] * 255.0f;
}
// Do not move the call to nifti_image_write out of the
// if, because it will crash, since the temp vector will
// not exist anymore, and pImage->data will point to garbage.
pImage->data = &tmp[0];
nifti_image_write( pImage );
}
else
{
// Do not move the call to nifti_image_write out of the
// if, because it will crash, since the temp vector will
// not exist anymore, and pImage->data will point to garbage.
pImage->data = &(*pDataset)[0];
nifti_image_write( pImage );
}
}
//////////////////////////////////////////////////////////////////////////
void Anatomy::createPropertiesSizer( PropertiesWindow *pParent )
{
DatasetInfo::createPropertiesSizer( pParent );
wxBoxSizer *pBoxMain = new wxBoxSizer( wxVERTICAL );
//////////////////////////////////////////////////////////////////////////
// Init widgets
m_pLowerEqSlider = new wxSlider( pParent, wxID_ANY, m_lowerEqThreshold * .2f, 0, 51, wxDefaultPosition, wxSize( 120, -1 ), wxSL_HORIZONTAL | wxSL_AUTOTICKS );
m_pUpperEqSlider = new wxSlider( pParent, wxID_ANY, m_upperEqThreshold * .2f, 0, 51, wxDefaultPosition, wxSize( 120, -1 ), wxSL_HORIZONTAL | wxSL_AUTOTICKS );
m_pEqualize = new wxToggleButton( pParent, wxID_ANY, wxT( "Equalize" ), wxDefaultPosition, wxSize( 140, -1 ) );
#if !_USE_LIGHT_GUI
m_pBtnDilate = new wxButton( pParent, wxID_ANY, wxT( "Dilate" ), wxDefaultPosition, wxSize( 85, -1 ) );
m_pBtnErode = new wxButton( pParent, wxID_ANY, wxT( "Erode" ), wxDefaultPosition, wxSize( 85, -1 ) );
m_pBtnCut = new wxButton( pParent, wxID_ANY, wxT( "Cut (boxes)" ), wxDefaultPosition, wxSize( 85, -1 ) );
m_pBtnMinimize = new wxButton( pParent, wxID_ANY, wxT( "Minimize (fibers)" ), wxDefaultPosition, wxSize( 85, -1 ) );
m_pBtnNewDistanceMap = new wxButton( pParent, wxID_ANY, wxT( "New Distance Map" ), wxDefaultPosition, wxSize( 140, -1 ) );
#endif
m_pBtnNewOffsetSurface = new wxButton( pParent, wxID_ANY, wxT( "New Offset Surface" ), wxDefaultPosition, wxSize( 140, -1 ) );
m_pBtnNewIsoSurface = new wxButton( pParent, wxID_ANY, wxT( "New Iso Surface" ), wxDefaultPosition, wxSize( 140, -1 ) );
m_pBtnNewVOI = new wxButton( pParent, wxID_ANY, wxT( "New VOI" ), wxDefaultPosition, wxSize( 140, -1 ) );
#if !_USE_LIGHT_GUI
m_pToggleSegment = new wxToggleButton( pParent, wxID_ANY, wxT( "Floodfill" ), wxDefaultPosition, wxSize( 140, -1 ) );
#endif
m_pSliderFlood = new MySlider( pParent, wxID_ANY, 40, 0, 100, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL | wxSL_AUTOTICKS );
setFloodThreshold( 0.2f );
m_pTxtThres = new wxTextCtrl( pParent, wxID_ANY, wxT( "0.20" ), wxDefaultPosition, wxSize( 40, -1 ), wxTE_READONLY );
m_pLblThres = new wxStaticText( pParent, wxID_ANY, wxT( "Threshold" ) );
//////////////////////////////////////////////////////////////////////////
wxFlexGridSizer *pGridSliders = new wxFlexGridSizer( 2 );
pGridSliders->Add( new wxStaticText( pParent, wxID_ANY, wxT( "Lower Threshold" ) ), 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL | wxALL, 1 );
pGridSliders->Add( m_pLowerEqSlider, 0, wxALIGN_CENTER_HORIZONTAL | wxEXPAND | wxALL, 1 );
pGridSliders->Add( new wxStaticText( pParent, wxID_ANY, wxT( "Upper Threshold" ) ), 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL | wxALL, 1 );
pGridSliders->Add( m_pUpperEqSlider, 0, wxALIGN_CENTER_HORIZONTAL | wxEXPAND | wxALL, 1 );
pBoxMain->Add( pGridSliders, 0, wxEXPAND | wxALL, 2 );
//////////////////////////////////////////////////////////////////////////
pBoxMain->Add( m_pEqualize, 0, wxALIGN_CENTER | wxEXPAND | wxRIGHT | wxLEFT, 24 );
#if !_USE_LIGHT_GUI
wxGridSizer *pGridButtons = new wxGridSizer( 2 );
pGridButtons->Add( m_pBtnDilate, 0, wxEXPAND | wxALL, 1 );
pGridButtons->Add( m_pBtnErode, 0, wxEXPAND | wxALL, 1 );
pGridButtons->Add( m_pBtnCut, 0, wxEXPAND | wxALL, 1 );
pGridButtons->Add( m_pBtnMinimize, 0, wxEXPAND | wxALL, 1 );
pBoxMain->Add( pGridButtons, 0, wxEXPAND | wxALL | wxALIGN_CENTER, 2 );
pBoxMain->Add( m_pBtnNewDistanceMap, 0, wxALIGN_CENTER | wxEXPAND | wxRIGHT | wxLEFT, 24 );
#endif
pBoxMain->Add( m_pBtnNewOffsetSurface, 0, wxALIGN_CENTER | wxEXPAND | wxRIGHT | wxLEFT, 24 );
pBoxMain->Add( m_pBtnNewIsoSurface, 0, wxALIGN_CENTER | wxEXPAND | wxRIGHT | wxLEFT, 24 );
pBoxMain->Add( m_pBtnNewVOI, 0, wxALIGN_CENTER | wxEXPAND | wxRIGHT | wxLEFT, 24 );
#if !_USE_LIGHT_GUI
pBoxMain->Add( m_pToggleSegment, 0, wxALIGN_CENTER | wxEXPAND | wxRIGHT | wxLEFT, 24 );
#endif
//////////////////////////////////////////////////////////////////////////
wxBoxSizer *pBoxFlood = new wxBoxSizer( wxHORIZONTAL );