forked from sandywang/conglomerate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_landmark_volume.c
209 lines (172 loc) · 7.02 KB
/
create_landmark_volume.c
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
#include <bicpl.h>
private void print_ellipse_parameters( Point *, Point * );
int main(
int argc,
char *argv[] )
{
Status status;
char *input_filename, *landmark_filename, *output_filename;
Real separations[N_DIMENSIONS];
Real voxel[N_DIMENSIONS];
Real min_voxel[N_DIMENSIONS];
Real max_voxel[N_DIMENSIONS];
char history[10000];
BOOLEAN first;
Point min_point, max_point;
Volume volume, new_volume;
volume_input_struct volume_input;
int n_objects;
object_struct **object_list;
int i, c, x, y, z;
int src_sizes[N_DIMENSIONS], dest_sizes[N_DIMENSIONS];
int offset[N_DIMENSIONS];
marker_struct *marker;
progress_struct progress;
static STRING in_dim_names[] = { MIxspace, MIyspace, MIzspace };
General_transform *voxel_to_world_transform;
Transform translation;
initialize_argument_processing( argc, argv );
if( !get_string_argument( "", &input_filename ) ||
!get_string_argument( "", &landmark_filename ) ||
!get_string_argument( "", &output_filename ) )
{
print( "Need arguments.\n" );
return( 1 );
}
status = start_volume_input( input_filename, in_dim_names,
FALSE, &volume, &volume_input );
get_volume_sizes( volume, src_sizes );
get_volume_separations( volume, separations );
voxel_to_world_transform = get_voxel_to_world_transform( volume );
if( filename_extension_matches( landmark_filename,
get_default_landmark_file_suffix() ) )
{
print( "Reading OLD format Landmark file %s\n", landmark_filename );
status = input_landmark_file( volume, landmark_filename,
GREEN, 1.0, BOX_MARKER,
&n_objects, &object_list );
}
else
{
print( "Reading TAG file %s\n", landmark_filename );
status = input_tag_file( landmark_filename,
GREEN, 1.0, BOX_MARKER,
&n_objects, &object_list );
}
if( status != OK )
return( 1 );
first = TRUE;
for_less( i, 0, n_objects )
{
if( object_list[i]->object_type == MARKER )
{
marker = get_marker_ptr( object_list[i] );
convert_world_to_voxel( volume,
Point_x(marker->position),
Point_y(marker->position),
Point_z(marker->position),
&voxel[X], &voxel[Y], &voxel[Z] );
if( voxel_is_within_volume( volume, voxel ) )
{
if( first )
{
min_point = marker->position;
max_point = marker->position;
first = FALSE;
}
else
{
apply_point_to_min_and_max( &marker->position,
&min_point, &max_point );
}
}
else
print( "Landmark[%d] outside volume: %g %g %g\n", i,
voxel[X], voxel[Y], voxel[Z] );
}
}
/* --- create the cropped volume */
convert_world_to_voxel( volume, Point_x(min_point),
Point_y(min_point), Point_z(min_point),
&min_voxel[X], &min_voxel[Y], &min_voxel[Z] );
convert_world_to_voxel( volume, Point_x(max_point),
Point_y(max_point), Point_z(max_point),
&max_voxel[X], &max_voxel[Y], &max_voxel[Z] );
for_less( c, 0, N_DIMENSIONS )
{
offset[c] = MAX( 0, ROUND(min_voxel[c]) - 1 );
dest_sizes[c] = MIN( src_sizes[c]-1, ROUND(max_voxel[c]) + 1 ) -
offset[c] + 1;
}
new_volume = create_volume( 3, in_dim_names, NC_BYTE, FALSE,
0.0, 0.0 );
set_volume_size( new_volume, NC_UNSPECIFIED, FALSE, dest_sizes );
alloc_volume_data( new_volume );
for_less( x, 0, dest_sizes[X] )
for_less( y, 0, dest_sizes[Y] )
for_less( z, 0, dest_sizes[Z] )
SET_VOXEL_3D( new_volume, x, y, z, 0.0 );
new_volume->min_voxel = 0.0;
new_volume->max_voxel = 255.0;
new_volume->value_scale = 1.0;
new_volume->value_translation = 0.0;
new_volume->separation[X] = separations[X];
new_volume->separation[Y] = separations[Y];
new_volume->separation[Z] = separations[Z];
make_translation_transform( (Real) offset[X], (Real) offset[Y],
(Real) offset[Z], &translation );
concat_transforms( &voxel_to_world_transform, &translation,
&voxel_to_world_transform );
new_volume->voxel_to_world_transform = voxel_to_world_transform;
initialize_progress_report( &progress, FALSE, n_objects, "Voxelating" );
for_less( i, 0, n_objects )
{
if( object_list[i]->object_type == MARKER )
{
marker = get_marker_ptr( object_list[i] );
convert_world_to_voxel( volume,
Point_x(marker->position),
Point_y(marker->position),
Point_z(marker->position),
&voxel[X], &voxel[Y], &voxel[Z] );
voxel[X] -= (Real) offset[X];
voxel[Y] -= (Real) offset[Y];
voxel[Z] -= (Real) offset[Z];
if( voxel_is_within_volume( new_volume, voxel ) )
{
SET_VOXEL_3D( new_volume, ROUND(voxel[X]),
ROUND(voxel[Y]), ROUND(voxel[Z]), 255.0 );
}
}
update_progress_report( &progress, i+1 );
}
terminate_progress_report( &progress );
cancel_volume_input( volume, &volume_input );
delete_object_list( n_objects, object_list );
print( "Writing %s\n", output_filename );
(void) strcpy( history, "Created by: " );
for_less( i, 0, argc )
{
(void) strcat( history, " " );
(void) strcat( history, argv[i] );
}
status = output_volume( output_filename, TRUE, new_volume, history );
print_ellipse_parameters( &min_point, &max_point );
return( status != OK );
}
private void print_ellipse_parameters(
Point *min_point,
Point *max_point )
{
int c;
Real radius[N_DIMENSIONS];
Point centroid;
INTERPOLATE_POINTS( centroid, *min_point, *max_point, 0.5 );
for_less( c, 0, N_DIMENSIONS )
{
radius[c] = 1.0 * (Point_coord(*max_point,c) - Point_coord(centroid,c));
}
print( "%g %g %g %g %g %g\n",
Point_x(centroid), Point_y(centroid), Point_z(centroid),
radius[X], radius[Y], radius[Z] );
}