forked from johncolby/along-tract-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrk_write.m
82 lines (75 loc) · 2.64 KB
/
trk_write.m
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
function trk_write(header,tracks,savePath)
%TRK_WRITE - Write TrackVis .trk files
%
% Syntax: trk_write(header,tracks,savePath)
%
% Inputs:
% header - Header information for .trk file [struc]
% tracks - Track data struc array [1 x nTracks]
% nPoints - # of points in each track
% matrix - XYZ coordinates (in mm) and associated scalars [nPoints x 3+nScalars]
% props - Properties of the whole tract
% savePath - Path where .trk file will be saved [char]
%
% Output files:
% Saves .trk file to disk at location given by 'savePath'.
%
% Other m-files required: none
% Subfunctions: none
% MAT-files required: none
%
% See also: TRK_READ
% Author: John Colby ([email protected])
% UCLA Developmental Cognitive Neuroimaging Group (Sowell Lab)
% Apr 2010
fid = fopen(savePath, 'w');
% Write header
fwrite(fid, header.id_string, '*char');
fwrite(fid, header.dim, 'short');
fwrite(fid, header.voxel_size, 'float');
fwrite(fid, header.origin, 'float');
fwrite(fid, header.n_scalars , 'short');
fwrite(fid, header.scalar_name', '*char');
fwrite(fid, header.n_properties, 'short');
fwrite(fid, header.property_name', '*char');
fwrite(fid, header.vox_to_ras', 'float');
fwrite(fid, header.reserved, '*char');
fwrite(fid, header.voxel_order, '*char');
fwrite(fid, header.pad2, '*char');
fwrite(fid, header.image_orientation_patient, 'float');
fwrite(fid, header.pad1, '*char');
fwrite(fid, header.invert_x, 'uchar');
fwrite(fid, header.invert_y, 'uchar');
fwrite(fid, header.invert_z, 'uchar');
fwrite(fid, header.swap_xy, 'uchar');
fwrite(fid, header.swap_yz, 'uchar');
fwrite(fid, header.swap_zx, 'uchar');
fwrite(fid, header.n_count, 'int');
fwrite(fid, header.version, 'int');
fwrite(fid, header.hdr_size, 'int');
% Check orientation
[tmp ix] = max(abs(header.image_orientation_patient(1:3)));
[tmp iy] = max(abs(header.image_orientation_patient(4:6)));
iz = 1:3;
iz([ix iy]) = [];
% Write body
for iTrk = 1:header.n_count
% Modify orientation back to LPS for display in TrackVis
header.dim = header.dim([ix iy iz]);
header.voxel_size = header.voxel_size([ix iy iz]);
coords = tracks(iTrk).matrix(:,1:3);
coords = coords(:,[ix iy iz]);
if header.image_orientation_patient(ix) < 0
coords(:,ix) = header.dim(ix)*header.voxel_size(ix) - coords(:,ix);
end
if header.image_orientation_patient(3+iy) < 0
coords(:,iy) = header.dim(iy)*header.voxel_size(iy) - coords(:,iy);
end
tracks(iTrk).matrix(:,1:3) = coords;
fwrite(fid, tracks(iTrk).nPoints, 'int');
fwrite(fid, tracks(iTrk).matrix', 'float');
if header.n_properties
fwrite(fid, tracks(iTrk).props, 'float');
end
end
fclose(fid);