forked from sandywang/conglomerate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deform_line.c
272 lines (230 loc) · 9.12 KB
/
deform_line.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
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
#include <volume_io/internal_volume_io.h>
#include <deform.h>
private void perturb_line_points(
int axis,
lines_struct *lines,
Real curvature_factors[],
Point new_points[],
Real fractional_step,
Real max_step,
Real max_search_distance,
int degrees_continuity,
deform_data_struct *deform_data,
boundary_definition_struct *boundary_def,
deformation_model_struct *deformation_model,
deform_stats *stats );
private Real one_iteration_lines(
lines_struct *lines,
deform_struct *deform_parms,
int iteration );
public void deform_lines(
lines_struct *lines,
deform_struct *deform_parms )
{
int iteration;
Real max_error;
iteration = 0;
do
{
++iteration;
max_error = one_iteration_lines( lines, deform_parms, iteration );
}
while( max_error > deform_parms->stop_threshold &&
iteration < deform_parms->max_iterations );
}
public void deform_lines_one_iteration(
lines_struct *lines,
deform_struct *deform_parms,
int iteration )
{
(void) one_iteration_lines( lines, deform_parms, iteration );
}
private Real one_iteration_lines(
lines_struct *lines,
deform_struct *deform_parms,
int iteration )
{
int axis;
Point *new_points, *tmp;
Real *curvature_factors;
deform_stats stats;
if( lines->n_items > 1 ||
lines->end_indices[0] != lines->n_points &&
lines->end_indices[0] != lines->n_points+1 )
{
print_error( "Must use on single line.\n" );
return( 0.0 );
}
if( lines->n_points <= 0 )
{
print_error( "Must use on nonempty line.\n" );
return( 0.0 );
}
if( !check_correct_deformation_lines( lines,
&deform_parms->deformation_model ) )
{
return( 0.0 );
}
ALLOC( new_points, lines->n_points );
ALLOC( curvature_factors, lines->n_points );
axis = find_axial_plane( lines );
initialize_deform_stats( &stats );
perturb_line_points( axis, lines, curvature_factors, new_points,
deform_parms->fractional_step,
deform_parms->max_step,
deform_parms->max_search_distance,
deform_parms->degrees_continuity,
&deform_parms->deform_data,
&deform_parms->boundary_definition,
&deform_parms->deformation_model,
&stats );
tmp = lines->points;
lines->points = new_points;
new_points = tmp;
print( "Iteration %d: ", iteration );
print_deform_stats( &stats, lines->n_points );
FREE( new_points );
FREE( curvature_factors );
return( stats.maximum );
}
public void get_line_equilibrium_point(
lines_struct *lines,
int axis,
int point_index,
int neighbours[],
Real curvature_factors[],
Real max_search_distance,
int degrees_continuity,
Volume volume,
Volume label_volume,
boundary_definition_struct *boundary_def,
deformation_model_struct *deformation_model,
Point *equilibrium_point,
Point *boundary_point )
{
BOOLEAN found_flag;
Real base_length, model_distance, boundary_distance;
Point centroid, model_point, search_origin;
Vector normal, pos_model_dir, neg_model_dir;
compute_line_centroid_and_normal( lines, axis, neighbours[0], neighbours[1],
¢roid, &normal, &base_length );
get_model_point( deformation_model, lines->points, point_index,
2, neighbours, curvature_factors,
¢roid, &normal, base_length, &model_point );
compute_model_dirs( ¢roid, &normal, base_length, &model_point,
&model_distance, &search_origin,
&pos_model_dir, &neg_model_dir );
found_flag = find_boundary_in_direction( volume, label_volume, NULL,
NULL, NULL, model_distance,
&search_origin, &pos_model_dir, &neg_model_dir,
max_search_distance, max_search_distance,
degrees_continuity,
boundary_def, &boundary_distance );
if( boundary_point != (Point *) NULL )
get_model_shape_point( &model_point, &pos_model_dir, &neg_model_dir,
boundary_distance, boundary_point );
compute_equilibrium_point( point_index,
found_flag, boundary_distance, base_length,
model_distance, &pos_model_dir, &neg_model_dir,
¢roid, deformation_model,
equilibrium_point );
}
private void perturb_line_points(
int axis,
lines_struct *lines,
Real curvature_factors[],
Point new_points[],
Real fractional_step,
Real max_step,
Real max_search_distance,
int degrees_continuity,
deform_data_struct *deform_data,
boundary_definition_struct *boundary_def,
deformation_model_struct *deformation_model,
deform_stats *stats )
{
int point_index, vertex_index;
int size, start_index, end_index;
int neighbours[2], i;
Point equilibrium_point;
BOOLEAN closed;
progress_struct progress;
Real dist_from_equil;
for_less( point_index, 0, lines->n_points )
new_points[point_index] = lines->points[point_index];
i = 0;
size = GET_OBJECT_SIZE( *lines, i );
closed = (size == lines->n_points+1);
if( closed )
{
start_index = 0;
end_index = size-2;
}
else
{
start_index = 1;
end_index = size-2;
}
if( deformation_model_includes_average(deformation_model) )
{
for_inclusive( vertex_index, start_index, end_index )
{
get_neighbours_of_line_vertex( lines, vertex_index, neighbours );
point_index = lines->indices[vertex_index];
curvature_factors[point_index] = compute_line_curvature(
lines, axis, point_index, neighbours[0], neighbours[1] );
}
}
initialize_progress_report( &progress, TRUE, end_index-start_index+1,
"Deforming Line Points" );
for_inclusive( vertex_index, start_index, end_index )
{
get_neighbours_of_line_vertex( lines, vertex_index, neighbours );
point_index = lines->indices[vertex_index];
get_line_equilibrium_point( lines, axis, point_index, neighbours,
curvature_factors,
max_search_distance, degrees_continuity,
deform_data->volume,
deform_data->label_volume,
boundary_def,
deformation_model, &equilibrium_point,
(Point *) NULL );
dist_from_equil = deform_point( point_index, lines->points,
&equilibrium_point,
fractional_step, max_step,
deformation_model->position_constrained,
deformation_model->max_position_offset,
deformation_model->original_positions,
&new_points[point_index] );
record_error_in_deform_stats( stats, dist_from_equil );
update_progress_report( &progress, vertex_index - start_index + 1 );
}
terminate_progress_report( &progress );
}
public int find_axial_plane(
lines_struct *lines )
{
int axis, p;
BOOLEAN found_axis;
found_axis = FALSE;
for_less( axis, 0, 3 )
{
found_axis = TRUE;
for_less( p, 0, lines->n_points-1 )
{
if( Point_coord(lines->points[p],axis) !=
Point_coord(lines->points[p+1],axis) )
{
found_axis = FALSE;
break;
}
}
if( found_axis ) break;
}
if( !found_axis )
{
print_error( "No axis found.\n" );
axis = X;
}
return( axis );
}