-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmake_gradient_arrows.c
246 lines (204 loc) · 6.91 KB
/
make_gradient_arrows.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
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
#include <def_mni.h>
private void usage(
char executable_name[] )
{
print( "Usage: %s input_filename output_filename\n", executable_name );
}
#undef TWO_DIMENSIONS
#define TWO_DIMENSIONS
#define X_SIZE 200
#define Y_SIZE 200
#define X_MIN -40.0
#define X_MAX -10.0
#define Y_MIN -20.0
#define Y_MAX 0.0
#define Z_PLANE 55.0
private void create_gradient_lines(
Volume volume,
BOOLEAN value_range_present,
int min_value,
int max_value,
Real scaling,
Real deriv2_scaling,
int continuity,
lines_struct *lines );
int main(
int argc,
char *argv[] )
{
BOOLEAN value_range_present;
Status status;
int min_value, max_value, continuity;
Real scaling, deriv2_scaling;
char *input_filename;
char *output_filename;
Volume volume;
static STRING dim_names[] = { MIxspace, MIyspace, MIzspace };
object_struct *object;
initialize_argument_processing( argc, argv );
if( !get_string_argument( "", &input_filename ) ||
!get_string_argument( "", &output_filename ) )
{
usage( argv[0] );
return( 1 );
}
if( get_int_argument( 0, &min_value ) && get_int_argument( 0, &max_value ) )
value_range_present = TRUE;
else
value_range_present = FALSE;
(void) get_real_argument( 1.0, &scaling );
(void) get_real_argument( 1.0, &deriv2_scaling );
(void) get_int_argument( 0, &continuity );
/* read the input volume */
if( input_volume( input_filename, dim_names, &volume ) != OK )
return( 1 );
object = create_object( LINES );
create_gradient_lines( volume, value_range_present, min_value, max_value,
scaling, deriv2_scaling, continuity,
get_lines_ptr(object) );
status = output_graphics_file( output_filename, BINARY_FORMAT,
1, &object );
return( status != OK );
}
private void make_arrow(
lines_struct *lines,
Point *centre,
Vector *deriv,
Vector *deriv2 )
{
Point p;
start_new_line( lines );
add_point_to_line( lines, centre );
ADD_POINT_VECTOR( p, *centre, *deriv );
add_point_to_line( lines, &p );
start_new_line( lines );
add_point_to_line( lines, centre );
ADD_POINT_VECTOR( p, *centre, *deriv2 );
add_point_to_line( lines, &p );
}
private void compute_curvature(
Real dx,
Real dy,
Real dxx,
Real dxy,
Real dyy,
Real *x_tangent,
Real *y_tangent,
Real *x_normal,
Real *y_normal )
{
Real tx, ty, mag_tangent;
*x_tangent = -dy;
*y_tangent = dx;
mag_tangent = sqrt( *x_tangent * *x_tangent + *y_tangent * *y_tangent );
if( mag_tangent == 0.0 )
mag_tangent = 1.0;
tx = *x_tangent / mag_tangent * dxx + *y_tangent / mag_tangent * dxy;
ty = *x_tangent / mag_tangent * dxy + *y_tangent / mag_tangent * dyy;
*x_normal = -ty;
*y_normal = tx;
}
private void create_gradient_lines(
Volume volume,
BOOLEAN value_range_present,
int min_value,
int max_value,
Real scaling,
Real deriv2_scaling,
int continuity,
lines_struct *lines )
{
BOOLEAN within_range;
int x, y;
Point centre;
Vector deriv, deriv2;
Real dx, dy, dz, value, ignore;
Real dxx, dxy, dxz, dyy, dyz, dzz;
Real grad_mag, dgx, dgy, dgz;
Real x_world, y_world, z_world;
progress_struct progress;
initialize_lines( lines, WHITE );
initialize_progress_report( &progress, FALSE, X_SIZE,
"Creating Gradient Lines" );
for_less( x, 0, X_SIZE )
{
x_world = X_MIN + (X_MAX - X_MIN) * (Real) x / (Real) (X_SIZE-1);
for_less( y, 0, Y_SIZE )
{
y_world = Y_MIN + (Y_MAX - Y_MIN) * (Real) y / (Real) (Y_SIZE-1);
z_world = Z_PLANE;
#ifdef TWO_DIMENSIONS
(void) evaluate_slice_in_world( volume,
x_world, y_world, z_world, FALSE,
&value, &dx, &dy, &dxx, &dxy, &dyy );
dz = 0.0;
dxz = 0.0;
dyz = 0.0;
dzz = 0.0;
#else
(void) evaluate_volume_in_world( volume,
x_world, y_world, z_world, continuity, FALSE,
&value, &dx, &dy, &dz,
&dxx, &dxy, &dxz, &dyy, &dyz, &dzz );
#endif
if( value_range_present )
{
within_range = (min_value <= value && value <= max_value);
}
else
within_range = TRUE;
#ifdef TWO_DIMENSIONS
if( within_range )
{
compute_curvature( dx, dy, dxx, dxy, dyy,
&ignore, &ignore, &dgx, &dgy );
dx *= scaling;
dy *= scaling;
dz = 0.0;
dgx *= deriv2_scaling;
dgy *= deriv2_scaling;
dgz = 0.0;
fill_Point( centre, x_world, y_world, z_world );
fill_Vector( deriv, dx, dy, dz );
fill_Vector( deriv2, dgx, dgy, dgz );
make_arrow( lines, ¢re, &deriv, &deriv2 );
}
#else
if( within_range )
{
grad_mag = dx * dx + dy * dy + dz * dz;
if( grad_mag != 0.0 )
grad_mag = sqrt( grad_mag );
dgx = dx / grad_mag * dxx +
dy / grad_mag * dxy +
dz / grad_mag * dxz;
dgy = dx / grad_mag * dxy +
dy / grad_mag * dyy +
dz / grad_mag * dyz;
dgz = dx / grad_mag * dxz +
dy / grad_mag * dyz +
dz / grad_mag * dzz;
dgx *= deriv2_scaling;
dgy *= deriv2_scaling;
dgz *= deriv2_scaling;
dx *= scaling;
dy *= scaling;
dz *= scaling;
fill_Point( centre, x_world, y_world, z_world );
fill_Vector( deriv, dx, dy, dz );
fill_Vector( deriv2, dgx, dgy, dgz );
make_arrow( lines, ¢re, &deriv, &deriv2 );
}
#endif
}
update_progress_report( &progress, x + 1 );
}
terminate_progress_report( &progress );
lines->colour_flag = PER_ITEM_COLOURS;
REALLOC( lines->colours, lines->n_items );
for_less( x, 0, lines->n_items )
if( x % 2 == 0 )
lines->colours[x] = WHITE;
else
lines->colours[x] = BLUE;
}