-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreate_surface_interpolation_lsq.c
359 lines (292 loc) · 11.5 KB
/
create_surface_interpolation_lsq.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
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
#include <volume_io/internal_volume_io.h>
#include <bicpl.h>
#define DEFAULT_SMOOTHNESS 1.0
typedef float lsq_type;
private Real create_surface_interpolation(
object_struct *object,
int n_points,
Point points[],
Real values[],
Real smoothness,
int n_iters,
BOOLEAN node_values_initialized,
Real node_values[] );
private void usage(
STRING executable )
{
static STRING usage_str = "\n\
Usage: %s surface.obj xyz+values.txt output.txt [smoothness]\n\
\n\n";
print_error( usage_str, executable );
}
int main(
int argc,
char *argv[] )
{
STRING surface_filename, xyz_filename;
STRING output_filename, initial_values;
File_formats format;
FILE *file;
int n_objects, n_points, point, n_iters;
Point *points;
Real *values, value, x, y, z, *node_values, smoothness;
object_struct **objects;
polygons_struct *polygons;
initialize_argument_processing( argc, argv );
if( !get_string_argument( NULL, &surface_filename ) ||
!get_string_argument( NULL, &xyz_filename ) ||
!get_string_argument( NULL, &output_filename ) )
{
usage( argv[0] );
return( 1 );
}
(void) get_real_argument( DEFAULT_SMOOTHNESS, &smoothness );
(void) get_int_argument( 10, &n_iters );
(void) get_string_argument( NULL, &initial_values );
if( input_graphics_file( surface_filename,
&format, &n_objects, &objects ) != OK ||
n_objects != 1 || get_object_type(objects[0]) != POLYGONS )
{
print_error( "File %s must contain 1 polygons object.\n",
surface_filename );
return( 1 );
}
n_points = 0;
points = NULL;
values = NULL;
if( open_file( xyz_filename, READ_FILE, ASCII_FORMAT, &file ) != OK )
return( 1 );
while( input_real( file, &x ) == OK )
{
if( input_real( file, &y ) != OK ||
input_real( file, &z ) != OK ||
input_real( file, &value ) != OK )
{
print_error( "Error reading %s\n", xyz_filename );
return( 1 );
}
SET_ARRAY_SIZE( points, n_points, n_points+1, DEFAULT_CHUNK_SIZE );
SET_ARRAY_SIZE( values, n_points, n_points+1, DEFAULT_CHUNK_SIZE );
fill_Point( points[n_points], x, y, z );
values[n_points] = value;
++n_points;
}
(void) close_file( file );
polygons = get_polygons_ptr( objects[0] );
ALLOC( node_values, polygons->n_points );
if( initial_values != NULL )
{
if( open_file( initial_values, READ_FILE, ASCII_FORMAT, &file ) != OK )
return( 1 );
for_less( point, 0, polygons->n_points )
{
if( input_real( file, &node_values[point] ) != OK )
{
print_error( "End of file in values file.\n" );
return( 1 );
}
}
(void) close_file( file );
}
(void) create_surface_interpolation( objects[0], n_points, points, values,
smoothness, n_iters,
initial_values != NULL, node_values );
if( open_file( output_filename, WRITE_FILE, ASCII_FORMAT, &file ) != OK )
return( 1 );
for_less( point, 0, polygons->n_points )
{
if( output_real( file, node_values[point] ) != OK ||
output_newline( file ) != OK )
return( 1 );
}
(void) close_file( file );
return( 0 );
}
private void create_coefficients(
Real interp_weight,
Real smooth_weight,
int n_interp_points,
Point points[],
Real values[],
object_struct *object,
Real total_length,
int n_neighbours[],
int **neighbours,
Smallest_int interior_flags[],
Real *constant,
lsq_type *linear_terms[],
lsq_type *square_terms[],
int *n_cross_terms[],
int **cross_parms[],
lsq_type **cross_terms[] )
{
int i, poly, node, p, size;
int n_parameters, *indices;
polygons_struct *polygons;
Point polygon_points[MAX_POINTS_PER_POLYGON];
Point neigh_points[MAX_POINTS_PER_POLYGON];
Real *weights, dist, avg_dist, weight;
Real x_flat[MAX_POINTS_PER_POLYGON];
Real y_flat[MAX_POINTS_PER_POLYGON];
Real consistency_weights[MAX_POINTS_PER_POLYGON];
Point point_on_surface;
polygons = get_polygons_ptr( object );
n_parameters = polygons->n_points;
initialize_lsq_terms_float( n_parameters, constant, linear_terms,
square_terms, n_cross_terms, cross_parms,
cross_terms );
ALLOC( indices, n_parameters );
ALLOC( weights, n_parameters );
for_less( p, 0, n_interp_points )
{
dist = find_closest_point_on_object( &points[p], object,
&poly, &point_on_surface );
if( dist > 1.0 )
print_error( "Distance too large: ? <%g>\n", dist );
size = get_polygon_points( polygons, poly, polygon_points );
get_polygon_interpolation_weights( &point_on_surface, size,
polygon_points, weights );
for_less( i, 0, size )
{
indices[i] = polygons->indices[POINT_INDEX(
polygons->end_indices,poly,i)];
weights[i] *= interp_weight;
}
add_to_lsq_terms_float( n_parameters, constant,
*linear_terms, *square_terms,
*n_cross_terms, *cross_parms, *cross_terms,
size, indices, weights,
-values[p] * interp_weight, 5 );
}
FREE( polygons->normals );
ALLOC( polygons->normals, 1 );
FREE( polygons->end_indices );
ALLOC( polygons->end_indices, 1 );
FREE( polygons->indices );
ALLOC( polygons->end_indices, 1 );
for_less( node, 0, polygons->n_points )
{
for_less( p, 0, n_neighbours[node] )
neigh_points[p] = polygons->points[neighbours[node][p]];
avg_dist = 0.0;
for_less( p, 0, n_neighbours[node] )
avg_dist += distance_between_points( &polygons->points[node],
&neigh_points[p] );
avg_dist /= (Real) n_neighbours[node];
weight = smooth_weight / sqrt( avg_dist / total_length );
indices[0] = node;
weights[0] = weight;
#define FLATTEN
#ifdef FLATTEN
flatten_around_vertex( &polygons->points[node],
n_neighbours[node], neigh_points,
(BOOLEAN) interior_flags[node],
x_flat, y_flat );
if( !get_interpolation_weights_2d( 0.0, 0.0, n_neighbours[node],
x_flat, y_flat,
consistency_weights ) )
{
print_error( "Error in interpolation weights, using avg..\n" );
for_less( p, 0, n_neighbours[node] )
consistency_weights[p] = 1.0 / (Real) n_neighbours[node];
}
#else
for_less( p, 0, n_neighbours[node] )
consistency_weights[p] = 1.0 / (Real) n_neighbours[node];
#endif
for_less( p, 0, n_neighbours[node] )
{
indices[1+p] = neighbours[node][p];
weights[1+p] = -weight * consistency_weights[p];
}
add_to_lsq_terms_float( n_parameters, constant,
*linear_terms, *square_terms,
*n_cross_terms, *cross_parms, *cross_terms,
n_neighbours[node]+1, indices, weights, 0.0,
5 );
}
FREE( weights );
FREE( indices );
realloc_lsq_terms_float( n_parameters,
*n_cross_terms, *cross_parms, *cross_terms );
}
private Real create_surface_interpolation(
object_struct *object,
int n_interp_points,
Point points[],
Real values[],
Real smoothness,
int n_iters,
BOOLEAN node_values_initialized,
Real node_values[] )
{
polygons_struct *polygons;
Real total_length, sum_x, sum_xx, variance;
Real dist, interp_weight, smooth_weight, fit, constant;
int point, *n_point_neighbours, **point_neighbours;
int neigh, n_edges;
int n_points;
Smallest_int *interior_flags;
int *n_cross_terms, **cross_parms;
lsq_type *linear_terms, *square_terms, **cross_terms;
polygons = get_polygons_ptr( object );
create_polygons_bintree( polygons,
(int) ((Real) polygons->n_items * 0.3) );
create_polygon_point_neighbours( polygons, FALSE, &n_point_neighbours,
&point_neighbours, &interior_flags, NULL );
total_length = 0.0;
n_edges = 0;
n_points = polygons->n_points;
for_less( point, 0, n_points )
{
n_edges += n_point_neighbours[point];
for_less( neigh, 0, n_point_neighbours[point] )
{
dist = distance_between_points(
&polygons->points[point],
&polygons->points[point_neighbours[point][neigh]] );
total_length += dist;
}
}
sum_x = 0.0;
sum_xx = 0.0;
for_less( point, 0, n_interp_points )
{
sum_x += values[point];
sum_xx += values[point] * values[point];
}
if( n_interp_points == 1 )
variance = 1.0;
else
variance = (sum_xx - sum_x * sum_x / (Real) n_interp_points) /
(Real) (n_interp_points - 1);
if( variance == 0.0 )
variance = 1.0;
if( !node_values_initialized )
{
for_less( point, 0, n_points )
node_values[point] = sum_x / (Real) n_interp_points;
}
interp_weight = 1.0 / (Real) n_interp_points / variance;
smooth_weight = smoothness / (Real) n_points / variance;
interp_weight = sqrt( interp_weight );
smooth_weight = sqrt( smooth_weight );
create_coefficients( interp_weight, smooth_weight,
n_interp_points, points, values,
object, total_length,
n_point_neighbours, point_neighbours,
interior_flags,
&constant, &linear_terms, &square_terms,
&n_cross_terms, &cross_parms,
&cross_terms );
delete_polygon_point_neighbours( polygons, n_point_neighbours,
point_neighbours, interior_flags, NULL );
delete_object( object );
fit = minimize_lsq_float( n_points, constant, linear_terms, square_terms,
n_cross_terms, cross_parms, cross_terms,
-1.0, n_iters, node_values );
delete_lsq_terms_float( n_points, linear_terms,
square_terms, n_cross_terms, cross_parms,
cross_terms );
return( fit );
}