This repository has been archived by the owner on Jul 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Fibers.cpp
executable file
·3872 lines (3268 loc) · 124 KB
/
Fibers.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 Fibers class implementation.
*
*/
#include "Fibers.h"
#include "Anatomy.h"
#include "DatasetManager.h"
#include "RTTrackingHelper.h"
#include "../main.h"
#include "../Logger.h"
#include "../gfx/ShaderHelper.h"
#include "../gui/MainFrame.h"
#include "../gui/SceneManager.h"
#include "../gui/SelectionTree.h"
#include "../misc/Fantom/FMatrix.h"
#include <wx/file.h>
#include <wx/tglbtn.h>
#include <wx/tokenzr.h>
#include <wx/xml/xml.h>
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <fstream>
using std::ofstream;
#include <limits>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using std::string;
#include <sstream>
using std::stringstream;
#include <vector>
using std::vector;
#define DEF_POS wxDefaultPosition
#define DEF_SIZE wxDefaultSize
// TODO replace by const
#define LINEAR_GRADIENT_THRESHOLD 0.085f
#define MIN_ALPHA_VALUE 0.017f
Fibers::Fibers()
: DatasetInfo(),
m_isSpecialFiberDisplay( false ),
m_barycenter(),
m_boxMax(),
m_boxMin(),
m_colorArray(),
m_count( 0 ),
m_countLines( 0 ),
m_countPoints( 0 ),
m_isInitialized( false ),
m_lineArray(),
m_linePointers(),
m_pointArray(),
m_normalArray(),
m_normalsPositive( false ),
m_reverse(),
m_selected(),
m_filtered(),
m_length(),
m_maxLength( 0.0f ),
m_minLength( 0.0f ),
m_localizedAlpha(),
m_cachedThreshold( 0.0f ),
m_fibersInverted( false ),
m_useFakeTubes( false ),
m_useTransparency( false ),
m_isColorationUpdated( false ),
m_fiberColorationMode( NORMAL_COLOR ),
m_pOctree( NULL ),
m_cfDrawDirty( true ),
m_axialShown( SceneManager::getInstance()->isAxialDisplayed() ),
m_coronalShown( SceneManager::getInstance()->isCoronalDisplayed() ),
m_sagittalShown( SceneManager::getInstance()->isSagittalDisplayed() ),
m_useIntersectedFibers( false ),
m_thickness( 2.5f ),
m_xDrawn( 0.0f ),
m_yDrawn( 0.0f ),
m_zDrawn( 0.0f ),
m_cfStartOfLine(),
m_cfPointsPerLine(),
m_constantColor( 0, 0, 0 ),
m_pSliderFibersFilterMin( NULL ),
m_pSliderFibersFilterMax( NULL ),
m_pSliderFibersSampling( NULL ),
m_pSliderInterFibersThickness( NULL ),
m_pToggleLocalColoring( NULL ),
m_pToggleNormalColoring( NULL ),
m_pSelectConstantFibersColor( NULL ),
m_pToggleCrossingFibers( NULL ),
m_pRadNormalColoring( NULL ),
m_pRadDistanceAnchoring( NULL ),
m_pRadMinDistanceAnchoring( NULL ),
m_pRadCurvature( NULL ),
m_pRadTorsion( NULL ),
m_pRadConstant( NULL )
{
m_bufferObjects = new GLuint[3];
}
Fibers::~Fibers()
{
Logger::getInstance()->print( wxT( "Executing fibers destructor" ), LOGLEVEL_DEBUG );
SceneManager::getInstance()->getSelectionTree().removeFiberDataset( getName() );
if( SceneManager::getInstance()->isUsingVBO() )
{
glDeleteBuffers( 3, m_bufferObjects );
}
if( m_pOctree )
{
delete m_pOctree;
m_pOctree = NULL;
}
m_lineArray.clear();
m_linePointers.clear();
m_reverse.clear();
m_pointArray.clear();
m_normalArray.clear();
m_colorArray.clear();
}
bool Fibers::load( const wxString &filename )
{
bool res( false );
wxString extension = filename.AfterLast( '.' );
if( wxT( "fib" ) == extension )
{
if( loadVTK( filename ) )
{
res = true;
}
else
{
res = loadDmri( filename );
}
}
else if( wxT( "bundlesdata" ) == extension )
{
res = loadPTK( filename );
}
else if( wxT( "Bfloat" ) == extension )
{
res = loadCamino( filename );
}
else if( wxT( "trk" ) == extension )
{
res = loadTRK( filename );
}
else if( wxT( "tck" ) == extension )
{
res = loadMRtrix( filename );
}
/* OcTree points classification */
m_pOctree = new Octree( 2, m_pointArray, m_countPoints );
return res;
}
bool Fibers::loadTRK( const wxString &filename )
{
stringstream ss;
Logger::getInstance()->print( wxT( "Loading TRK file..." ), LOGLEVEL_MESSAGE );
wxFile dataFile;
wxFileOffset nSize( 0 );
converterByteINT16 cbi;
converterByteINT32 cbi32;
converterByteFloat cbf;
if( !dataFile.Open( filename ) )
{
return false;
}
nSize = dataFile.Length();
if( nSize == wxInvalidOffset )
{
return false;
}
////
// READ HEADER
////
//Read file header. [1000 bytes]
wxUint8 *pBuffer = new wxUint8[1000];
dataFile.Read( pBuffer, ( size_t )1000 );
//ID String for track file. The first 5 characters must match "TRACK". [6 bytes]
char idString[6];
memcpy( idString, &pBuffer[0], 6 );
ss.str( "" );
ss << "Type: " << idString;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
if( strncmp( idString, "TRACK", 5 ) != 0 )
{
return false;
}
//Dimension of the image volume. [6 bytes]
wxUint16 dim[3];
for( int i = 0; i != 3; ++i )
{
memcpy( cbi.b, &pBuffer[6 + ( i * 2 )], 2 );
dim[i] = cbi.i;
}
ss.str( "" );
ss << "Dim: " << dim[0] << "x" << dim[1] << "x" << dim[2];
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Voxel size of the image volume. [12 bytes]
float voxelSize[3];
for( int i = 0; i != 3; ++i )
{
memcpy( cbf.b, &pBuffer[12 + ( i * 4 )], 4 );
voxelSize[i] = cbf.f;
}
ss.str( "" );
ss << "Voxel size: " << voxelSize[0] << "x" << voxelSize[1] << "x" << voxelSize[2];
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Origin of the image volume. [12 bytes]
float origin[3];
for( int i = 0; i != 3; ++i )
{
memcpy( cbf.b, &pBuffer[24 + ( i * 4 )], 4 );
origin[i] = cbf.f;
}
ss.str( "" );
ss << "Origin: (" << origin[0] << "," << origin[1] << "," << origin[2] << ")";
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Number of scalars saved at each track point. [2 bytes]
wxUint16 nbScalars;
memcpy( cbi.b, &pBuffer[36], 2 );
nbScalars = cbi.i;
ss.str( "" );
ss << "Nb. scalars: " << nbScalars;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Name of each scalar. (20 characters max each, max 10 names) [200 bytes]
char scalarNames[10][20];
memcpy( scalarNames, &pBuffer[38], 200 );
for( int i = 0; i != 10; ++i )
{
ss.str( "" );
ss << "Scalar name #" << i << ": " << scalarNames[i];
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
}
//Number of properties saved at each track. [2 bytes]
wxUint16 nbProperties;
memcpy( cbi.b, &pBuffer[238], 2 );
nbProperties = cbi.i;
ss.str( "" );
ss << "Nb. properties: " << nbProperties;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Name of each property. (20 characters max each, max 10 names) [200 bytes]
char propertyNames[10][20];
memcpy( propertyNames, &pBuffer[240], 200 );
for( int i = 0; i != 10; ++i )
{
ss.str( "" );
ss << "Property name #" << i << ": " << propertyNames[i];
}
//4x4 matrix for voxel to RAS (crs to xyz) transformation.
// If vox_to_ras[3][3] is 0, it means the matrix is not recorded.
// This field is added from version 2. [64 bytes]
float voxToRas[4][4];
for( int i = 0; i != 4; ++i )
{
ss.str( "" );
for( int j = 0; j != 4; ++j )
{
memcpy( cbf.b, &pBuffer[440 + ( i * 4 + j )], 4 );
voxToRas[i][j] = cbf.f;
ss << voxToRas[i][j] << " ";
}
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
}
//Reserved space for future version. [444 bytes]
//char reserved[444];
//pBuffer[504]...
//Storing order of the original image data. [4 bytes]
char voxelOrder[4];
memcpy( voxelOrder, &pBuffer[948], 4 );
ss.str( "" );
ss << "Voxel order: " << voxelOrder;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Paddings [4 bytes]
char pad2[4];
memcpy( pad2, &pBuffer[952], 4 );
ss.str( "" );
ss << "Pad #2: " << pad2;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Image orientation of the original image. As defined in the DICOM header. [24 bytes]
float imageOrientationPatient[6];
ss.str( "" );
ss << "Image orientation patient: ";
for( int i = 0; i != 6; ++i )
{
memcpy( cbf.b, &pBuffer[956 + ( i * 4 )], 4 );
imageOrientationPatient[i] = cbf.f;
ss << imageOrientationPatient[i] << " ";
}
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Paddings. [2 bytes]
char pad1[2];
memcpy( pad1, &pBuffer[980], 2 );
ss.str( "" );
ss << "Pad #1: " << pad1;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Inversion/rotation flags used to generate this track file. [1 byte]
bool invertX = pBuffer[982] > 0;
ss.str( "" );
ss << "Invert X: " << invertX;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Inversion/rotation flags used to generate this track file. [1 byte]
bool invertY = pBuffer[983] > 0;
ss.str( "" );
ss << "Invert Y: " << invertY;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Inversion/rotation flags used to generate this track file. [1 byte]
bool invertZ = pBuffer[984] > 0;
ss.str( "" );
ss << "Invert Z: " << invertZ;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Inversion/rotation flags used to generate this track file. [1 byte]
bool swapXY = pBuffer[985] > 0;
ss.str( "" );
ss << "Swap XY: " << swapXY;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Inversion/rotation flags used to generate this track file. [1 byte]
bool swapYZ = pBuffer[986] > 0;
ss.str( "" );
ss << "Swap YZ: " << swapYZ;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Inversion/rotation flags used to generate this track file. [1 byte]
bool swapZX = pBuffer[987] > 0;
ss.str( "" );
ss << "Swap ZX: " << swapZX;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Number of tracks stored in this track file. 0 means the number was NOT stored. [4 bytes]
wxUint32 nbCount;
memcpy( cbi32.b, &pBuffer[988], 4 );
nbCount = cbi32.i;
ss.str( "" );
ss << "Nb. tracks: " << nbCount;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Version number. Current version is 2. [4 bytes]
wxUint32 version;
memcpy( cbi32.b, &pBuffer[992], 4 );
version = cbi32.i;
ss.str( "" );
ss << "Version: " << version;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
//Size of the header. Used to determine byte swap. Should be 1000. [4 bytes]
wxUint32 hdrSize;
memcpy( cbi32.b, &pBuffer[996], 4 );
hdrSize = cbi32.i;
ss.str( "" );
ss << "HDR size: " << hdrSize;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
////
// READ DATA
////
delete[] pBuffer;
pBuffer = NULL;
vector<float> tmpPoints;
//File size - header size (used when the number of fiber was not stored)
int remainingBytes = nSize - hdrSize;
vector< vector< float > > lines;
m_countPoints = 0;
vector< float > colors;
//if( nbCount == 0 )
//{
// return false; //TODO: handle it. (0 means the number was NOT stored.)
//}
for( unsigned int i = 0; i < nbCount || remainingBytes > 0; ++i )
//for( unsigned int i = 0; i != nbCount ; ++i )
{
//Number of points in this track. [4 bytes]
wxUint32 nbPoints;
dataFile.Read( cbi32.b, ( size_t )4 );
nbPoints = cbi32.i;
//Read data of one track.
size_t ptsSize = 3 + nbScalars;
size_t tractSize = 4 * ( nbPoints * ( ptsSize ) + nbProperties );
pBuffer = new wxUint8[tractSize];
dataFile.Read( pBuffer, tractSize );
vector< float > curLine;
for( unsigned int j = 0; j != nbPoints; ++j )
{
//Read coordinates (x,y,z) and scalars associated to each point.
for( unsigned int k = 0; k != ptsSize; ++k )
{
memcpy( cbf.b, &pBuffer[4 * ( j * ptsSize + k )], 4 );
if( k >= 6 ) //TODO: incorporate other scalars in the navigator.
{
break;
}
else if( k >= 3 ) //RGB color of each point.
{
colors.push_back( cbf.f );
}
else
{
curLine.push_back( cbf.f );
}
}
}
//TODO: incorporate properties in the navigator.
// for( unsigned int j = 0; j != nbProperties; ++j )
// {}
m_countPoints += curLine.size() / 3;
lines.push_back( curLine );
delete[] pBuffer;
pBuffer = NULL;
//Adjust remainingBytes
remainingBytes -= 1*4; //Number of points (4 bytes)
remainingBytes -= 3*nbPoints * 4; //Coordinates (4 bytes)
remainingBytes -= nbScalars*nbPoints * 4; //Scalars (4 bytes)
remainingBytes -= nbProperties * 4; //Properties (4 bytes)
//cout << "i : " << i << ", remainingBytes : " << remainingBytes << "\n";
}
dataFile.Close();
////
//POST PROCESS: set all the data in the right format for the navigator
////
Logger::getInstance()->print( wxT( "Setting data in right format for the navigator..." ), LOGLEVEL_MESSAGE );
m_countLines = lines.size();
m_pointArray.max_size();
m_colorArray.max_size();
m_linePointers.resize( m_countLines + 1 );
m_pointArray.resize( m_countPoints * 3 );
m_colorArray.resize( m_countPoints * 3 );
m_linePointers[m_countLines] = m_countPoints;
m_reverse.resize( m_countPoints );
m_selected.resize( m_countLines, false );
m_filtered.resize( m_countLines, false );
ss.str( "" );
ss << "m_countLines: " << m_countLines;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
ss.str( "" );
ss << "m_countPoints: " << m_countPoints;
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
m_linePointers[0] = 0;
for( int i = 0; i < m_countLines; ++i )
{
m_linePointers[i + 1] = m_linePointers[i] + lines[i].size() / 3;
}
int lineCounter = 0;
for( int i = 0; i < m_countPoints; ++i )
{
if( i == m_linePointers[lineCounter + 1] )
{
++lineCounter;
}
m_reverse[i] = lineCounter;
}
unsigned int pos( 0 );
vector< vector< float > >::iterator it;
for( it = lines.begin(); it < lines.end(); it++ )
{
vector< float >::iterator it2;
for( it2 = ( *it ).begin(); it2 < ( *it ).end(); it2++ )
{
if (colors.size() > 0)
{
m_colorArray[pos] = colors[pos] / 255.;
}
m_pointArray[pos++] = *it2;
}
}
float columns = DatasetManager::getInstance()->getColumns();
float rows = DatasetManager::getInstance()->getRows();
float frames = DatasetManager::getInstance()->getFrames();
float voxelX = DatasetManager::getInstance()->getVoxelX();
float voxelY = DatasetManager::getInstance()->getVoxelY();
float voxelZ = DatasetManager::getInstance()->getVoxelZ();
if( voxelSize[0] == 0 && voxelSize[1] == 0 && voxelSize[2] == 0 )
{
ss.str( "" );
ss << "Using anatomy's voxel size: [" << voxelX << ", " << voxelY << ", " << voxelZ << "]";
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
voxelSize[0] = voxelX;
voxelSize[1] = voxelY;
voxelSize[2] = voxelZ;
ss.str( "" );
ss << "Centering with respect to the anatomy: [" << columns / 2 << "," << rows / 2 << "," << frames / 2 << "]";
Logger::getInstance()->print( wxString( ss.str().c_str(), wxConvUTF8 ), LOGLEVEL_MESSAGE );
origin[0] = columns / 2;
origin[1] = rows / 2;
origin[2] = frames / 2;
}
float flipX = ( invertX ) ? -1. : 1.;
float flipY = ( invertY ) ? -1. : 1.;
float flipZ = ( invertZ ) ? -1. : 1.;
float anatomy[3];
anatomy[0] = ( flipX - 1. ) * columns * voxelX / -2.;
anatomy[1] = ( flipY - 1. ) * rows * voxelY / -2.;
anatomy[2] = ( flipZ - 1. ) * frames * voxelZ / -2.;
for( int i = 0; i < m_countPoints * 3; ++i )
{
m_pointArray[i] = flipX * ( m_pointArray[i] - origin[0] ) * voxelX / voxelSize[0] + anatomy[0];
++i;
m_pointArray[i] = flipY * ( m_pointArray[i] - origin[1] ) * voxelY / voxelSize[1] + anatomy[1];
++i;
m_pointArray[i] = flipZ * ( m_pointArray[i] - origin[2] ) * voxelZ / voxelSize[2] + anatomy[2];
}
Logger::getInstance()->print( wxT( "TRK file loaded" ), LOGLEVEL_MESSAGE );
createColorArray( colors.size() > 0 );
m_type = FIBERS;
m_fullPath = filename;
#ifdef __WXMSW__
m_name = wxT( "-" ) + filename.AfterLast( '\\' );
#else
m_name = wxT( "-" ) + filename.AfterLast( '/' );
#endif
return true;
}
bool Fibers::loadCamino( const wxString &filename )
{
Logger::getInstance()->print( wxT( "Loading Camino file" ), LOGLEVEL_MESSAGE );
wxFile dataFile;
wxFileOffset nSize = 0;
if( dataFile.Open( filename ) )
{
nSize = dataFile.Length();
if( nSize == wxInvalidOffset )
{
return false;
}
}
wxUint8 *pBuffer = new wxUint8[nSize];
dataFile.Read( pBuffer, nSize );
dataFile.Close();
m_countLines = 0; // Number of lines.
m_countPoints = 0; // Number of points.
int cl = 0;
int pc = 0;
converterByteFloat cbf;
vector< float > tmpPoints;
while( pc < nSize )
{
++m_countLines;
cbf.b[3] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[0] = pBuffer[pc++];
cl = ( int )cbf.f;
m_lineArray.push_back( cl );
pc += 4;
for( int i = 0; i < cl; ++i )
{
m_lineArray.push_back( m_countPoints );
++m_countPoints;
cbf.b[3] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[0] = pBuffer[pc++];
tmpPoints.push_back( cbf.f );
cbf.b[3] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[0] = pBuffer[pc++];
tmpPoints.push_back( cbf.f );
cbf.b[3] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[0] = pBuffer[pc++];
tmpPoints.push_back( cbf.f );
if( pc > nSize )
{
break;
}
}
}
m_linePointers.resize( m_countLines + 1 );
m_linePointers[m_countLines] = m_countPoints;
m_reverse.resize( m_countPoints );
m_selected.resize( m_countLines, false );
m_filtered.resize( m_countLines, false );
m_pointArray.resize( tmpPoints.size() );
for( size_t i = 0; i < tmpPoints.size(); ++i )
{
m_pointArray[i] = tmpPoints[i];
}
printf( "%d lines and %d points \n", m_countLines, m_countPoints );
Logger::getInstance()->print( wxT( "Move vertices" ), LOGLEVEL_MESSAGE );
float columns = DatasetManager::getInstance()->getColumns();
float rows = DatasetManager::getInstance()->getRows();
float frames = DatasetManager::getInstance()->getFrames();
float voxelX = DatasetManager::getInstance()->getVoxelX();
float voxelY = DatasetManager::getInstance()->getVoxelY();
float voxelZ = DatasetManager::getInstance()->getVoxelZ();
for( int i = 0; i < m_countPoints * 3; ++i )
{
m_pointArray[i] = columns * voxelX - m_pointArray[i];
++i;
m_pointArray[i] = rows * voxelY - m_pointArray[i];
++i;
m_pointArray[i] = frames * voxelZ - m_pointArray[i];
}
calculateLinePointers();
createColorArray( false );
Logger::getInstance()->print( wxT( "Read all" ), LOGLEVEL_MESSAGE );
delete[] pBuffer;
pBuffer = NULL;
m_type = FIBERS;
m_fullPath = filename;
#ifdef __WXMSW__
m_name = wxT( "-" ) + filename.AfterLast( '\\' );
#else
m_name = wxT( "-" ) + filename.AfterLast( '/' );
#endif
return true;
}
bool Fibers::loadMRtrix( const wxString &filename )
{
Logger::getInstance()->print( wxT( "Loading MRtrix file" ), LOGLEVEL_MESSAGE );
wxFile dataFile;
long int nSize = 0;
long int pc = 0, nodes = 0;
converterByteFloat cbf;
float x, y, z, x2, y2, z2;
std::vector< float > tmpPoints;
vector< vector< float > > lines;
//Open file
FILE *pFs = fopen( filename.ToAscii(), "r" ) ;
////
// read header
////
char lineBuffer[200];
std::string readLine("");
bool countFieldFound( false );
while(readLine.find( "END" ) == std::string::npos)
{
fgets( lineBuffer, 200, pFs );
readLine = std::string( lineBuffer );
if( readLine.find( "file" ) != std::string::npos )
{
sscanf( lineBuffer, "file: . %ld", &pc );
}
if( readLine.find( "count" ) != std::string::npos && !countFieldFound )
{
sscanf( lineBuffer, "count: %i", &m_countLines );
countFieldFound = true;
}
}
fclose( pFs );
if( dataFile.Open( filename ) )
{
nSize = dataFile.Length();
if( nSize < 1 )
{
return false;
}
}
nSize -= pc;
dataFile.Seek( pc );
wxUint8 *pBuffer = new wxUint8[nSize];
dataFile.Read( pBuffer, nSize );
dataFile.Close();
Logger::getInstance()->print( wxT( "Reading fibers" ), LOGLEVEL_DEBUG );
pc = 0;
m_countPoints = 0; // number of points
for( int i = 0; i < m_countLines; i++ )
{
tmpPoints.clear();
nodes = 0;
// read one tract
cbf.b[0] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[3] = pBuffer[pc++];
x = cbf.f;
cbf.b[0] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[3] = pBuffer[pc++];
y = cbf.f;
cbf.b[0] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[3] = pBuffer[pc++];
z = cbf.f;
//add first point
tmpPoints.push_back( x );
tmpPoints.push_back( y );
tmpPoints.push_back( z );
++nodes;
x2 = x;
cbf.f = x2;
//Read points (x,y,z) until x2 equals NaN (0x0000C07F), meaning end of the tract.
while( !( cbf.b[0] == 0x00 && cbf.b[1] == 0x00 && cbf.b[2] == 0xC0 && cbf.b[3] == 0x7F ) )
{
cbf.b[0] = pBuffer[pc++]; // get next float
cbf.b[1] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[3] = pBuffer[pc++];
x2 = cbf.f;
cbf.b[0] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[3] = pBuffer[pc++];
y2 = cbf.f;
cbf.b[0] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[3] = pBuffer[pc++];
z2 = cbf.f;
// downsample fibers: take only points in distance of min 0.75 mm
if( ( ( x - x2 ) * ( x - x2 ) + ( y - y2 ) * ( y - y2 ) + ( z - z2 ) * ( z - z2 ) ) >= 0.2 )
{
x = x2;
y = y2;
z = z2;
tmpPoints.push_back( x );
tmpPoints.push_back( y );
tmpPoints.push_back( z );
++nodes;
}
cbf.f = x2;
}
// put the tract in the line array
lines.push_back( tmpPoints );
for( int i = 0; i < nodes ; i++ )
{
m_countPoints++;
}
}
delete[] pBuffer;
pBuffer = NULL;
////
//POST PROCESS: set all the data in the right format for the navigator
////
m_pointArray.max_size();
m_linePointers.resize( m_countLines + 1 );
m_pointArray.resize( m_countPoints * 3 );
m_linePointers[m_countLines] = m_countPoints;
m_reverse.resize( m_countPoints );
m_selected.resize( m_countLines, false );
m_filtered.resize( m_countLines, false );
m_linePointers[0] = 0;
for( int i = 0; i < m_countLines; ++i )
{
m_linePointers[i + 1] = m_linePointers[i] + lines[i].size() / 3;
}
int lineCounter = 0;
for( int i = 0; i < m_countPoints; ++i )
{
if( i == m_linePointers[lineCounter + 1] )
{
++lineCounter;
}
m_reverse[i] = lineCounter;
}
unsigned int pos = 0;
vector< vector< float > >::iterator it;
for( it = lines.begin(); it < lines.end(); it++ )
{
vector< float >::iterator it2;
for( it2 = ( *it ).begin(); it2 < ( *it ).end(); it2++ )
{
m_pointArray[pos++] = *it2;
}
}
// The MrTrix fibers are defined in the same geometric reference
// as the anatomical file. That is, the fibers coordinates are related to
// the anatomy in world space. The transformation from local to world space
// for the anatomy is encoded in the m_dh->m_niftiTransform member.
// Since we do not consider this tranform when loading the anatomy, we must
// bring back the fibers in the same reference, using the inverse of the
// local to world transformation. A further problem arises when loading an
// anatomy that has voxels with dimensions differing from 1x1x1. The
// scaling factor is encoded in the transformation matrix, but we do not,
// for the moment, use this scaling. Therefore, we must remove it from the
// the transformation matrix before computing its inverse.
FMatrix localToWorld = FMatrix( DatasetManager::getInstance()->getNiftiTransform() );
float voxelX = DatasetManager::getInstance()->getVoxelX();
float voxelY = DatasetManager::getInstance()->getVoxelY();
float voxelZ = DatasetManager::getInstance()->getVoxelZ();
if( voxelX != 1.0 || voxelY != 1.0 || voxelZ != 1.0 )
{
FMatrix rotMat( 3, 3 );
localToWorld.getSubMatrix( rotMat, 0, 0 );
FMatrix scaleInversion( 3, 3 );
scaleInversion( 0, 0 ) = 1.0 / voxelX;
scaleInversion( 1, 1 ) = 1.0 / voxelY;
scaleInversion( 2, 2 ) = 1.0 / voxelZ;
rotMat = scaleInversion * rotMat;
localToWorld.setSubMatrix( 0, 0, rotMat );
}
FMatrix invertedTransform( 4, 4 );
invertedTransform = invert( localToWorld );
for( int i = 0; i < m_countPoints * 3; ++i )
{
FMatrix curPoint( 4, 1 );
curPoint( 0, 0 ) = m_pointArray[i];
curPoint( 1, 0 ) = m_pointArray[i + 1];
curPoint( 2, 0 ) = m_pointArray[i + 2];
curPoint( 3, 0 ) = 1;
FMatrix invertedPoint = invertedTransform * curPoint;
m_pointArray[i] = invertedPoint( 0, 0 );
m_pointArray[i + 1] = invertedPoint( 1, 0 );
m_pointArray[i + 2] = invertedPoint( 2, 0 );
i += 2;
}
Logger::getInstance()->print( wxT( "TCK file loaded" ), LOGLEVEL_MESSAGE );
createColorArray( false );
m_type = FIBERS;
m_fullPath = filename;
#ifdef __WXMSW__
m_name = wxT( "-" ) + filename.AfterLast( '\\' );
#else
m_name = wxT( "-" ) + filename.AfterLast( '/' );
#endif
return true;
}
bool Fibers::loadPTK( const wxString &filename )
{
Logger::getInstance()->print( wxT( "Loading PTK file" ), LOGLEVEL_MESSAGE );
wxFile dataFile;
wxFileOffset nSize = 0;
int pc = 0;
converterByteINT32 cbi;
converterByteFloat cbf;
vector< float > tmpPoints;
if( dataFile.Open( filename ) )
{
nSize = dataFile.Length();
if( nSize == wxInvalidOffset )
return false;
}
wxUint8 *pBuffer = new wxUint8[nSize];
dataFile.Read( pBuffer, nSize );
m_countLines = 0; // Number of lines.
m_countPoints = 0; // Number of points.
while( pc < nSize )
{
++m_countLines;
cbi.b[0] = pBuffer[pc++];
cbi.b[1] = pBuffer[pc++];
cbi.b[2] = pBuffer[pc++];
cbi.b[3] = pBuffer[pc++];
m_lineArray.push_back( cbi.i );
for( size_t i = 0; i < cbi.i; ++i )
{
m_lineArray.push_back( m_countPoints );
++m_countPoints;
cbf.b[0] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[3] = pBuffer[pc++];
tmpPoints.push_back( cbf.f );
cbf.b[0] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[3] = pBuffer[pc++];
tmpPoints.push_back( cbf.f );
cbf.b[0] = pBuffer[pc++];
cbf.b[1] = pBuffer[pc++];
cbf.b[2] = pBuffer[pc++];
cbf.b[3] = pBuffer[pc++];
tmpPoints.push_back( cbf.f );
}
}
m_linePointers.resize( m_countLines + 1 );