-
Notifications
You must be signed in to change notification settings - Fork 1
/
stereo_create_verts.c
573 lines (445 loc) · 10.8 KB
/
stereo_create_verts.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include "header.h"
typedef struct vert_pixel_struct {
int vert;
int pixel;
} vert_pixel_struct;
static int compare_vert_pixel(
const void *p1,
const void *p2
)
{
int vert1;
int vert2;
int pixel1;
int pixel2;
/*
Compare the verts
*/
vert1= ((vert_pixel_struct *)p1)->vert;
vert2= ((vert_pixel_struct *)p2)->vert;
if ( vert1 < vert2 )
return -1;
if ( vert1 > vert2 )
return +1;
/*
Compare the pixels
*/
pixel1= ((vert_pixel_struct *)p1)->pixel;
pixel2= ((vert_pixel_struct *)p2)->pixel;
if ( pixel1 < pixel2 )
return -1;
if ( pixel1 > pixel2 )
return +1;
return 0;
}
void stereo_create_verts(
int *image_arr,
int width,
int height,
int sample_rate_spatial,
int sample_rate_range,
int hash_table_size,
mesh_struct **ptr_mesh,
matrix_int_crs_struct **ptr_splat_matrix,
int *blur_matrix[5]
)
/*
Create the bilateral space verts
Build the splat matrix
Build the blur matrices
*/
/*
sample_rate_spatial defines the size of the grid cells
in the spatial dimension
sample_rate_range defines the size of the grid cells
in the range dimension
*/
{
arrayHeader *hash_table;
int img_i;
int img_j;
int ind;
int rgb_ind;
int rgb[3];
double x;
double y;
int grid_cell_ind[5];
int vert;
mesh_struct *mesh;
int nbr_pixels;
int nbr_verts;
matrix_int_crs_struct *splat_matrix;
int dim;
arrayHeader *mesh_verts;
int maxInd;
int ind2;
int dim2;
int grid_cell_ind2[5];
int vert2;
double grid_cell_length[5];
int hind;
int block;
int pixel;
vert_pixel_struct *vert_pixel_arr;
int vert_old;
double val_dbl;
int val_int;
int grid_size[5];
int count;
int max_count;
/*
Get nbr of pixels
*/
nbr_pixels= width*height;
/*
Initialize nbr of verts
*/
nbr_verts= 0;
/*
The splat matrix has
"nbr of verts" rows
"nbr of pixels" columns
but we're gonna use a Compressed Row Storage (CRS) to store it
For that, we are gonna need to first store doublets
containing the pixel and the vert it's associated with
Then, we are gonna sort w/r to vert first and pixel second
Finally, we are gonna build the splat matrix in CRS format
*/
myCalloc(vert_pixel_arr,vert_pixel_struct,nbr_pixels,sizeof(vert_pixel_struct));
/*
We are gonna need to be able to find a bilateral space vert
given coordinates in bilateral space
*/
/*
For that,
it makes sense to create a hash table
A hash table is basically an array of buckets
Each bucket stores a bunch of entries according to a hash key
*/
myCalloc(hash_table,arrayHeader,hash_table_size,sizeof(arrayHeader));
for ( hind= 0 ; hind< hash_table_size ; hind++) {
block= 100;
arrayInit(hash_table[hind],block);
}
/*
Create the mesh to hold the verts
*/
mesh_new(
&mesh
);
/*
Let's get the grid cell length in all dimensions
*/
val_dbl= (double)width/(double)sample_rate_spatial;
val_int= (int)(val_dbl+.5);
grid_size[0]= val_int;
grid_cell_length[0]= (double)width/(double)val_int;
val_dbl= (double)height/(double)sample_rate_spatial;
val_int= (int)(val_dbl+.5);
grid_size[1]= val_int;
grid_cell_length[1]= (double)height/(double)val_int;
val_dbl= 256./(double)sample_rate_range;
val_int= (int)(val_dbl+.5);
grid_size[2]= val_int;
grid_size[3]= val_int;
grid_size[4]= val_int;
grid_cell_length[2]= 256./(double)val_int;
grid_cell_length[3]= 256./(double)val_int;
grid_cell_length[4]= 256./(double)val_int;
fprintf(stdout,"Size of bilateral grid = %d x %d x %d x %d x %d\n",
grid_size[0],grid_size[1],grid_size[2],grid_size[3],grid_size[4]);
/*
Let's go through each pixel of the image
*/
for ( img_i= 0 ; img_i< height ; img_i++ ) {
for ( img_j= 0 ; img_j< width ; img_j++ ) {
pixel= img_i*width+img_j;
for ( rgb_ind= 0 ; rgb_ind< 3 ; rgb_ind++ )
rgb[rgb_ind]= image_arr[3*pixel+rgb_ind];
/*
Get grid cell in 5-dimensional space (x,y,r,g,b)
where this pixel falls in
*/
/*
Let's take care of x coordinate
*/
x= (double)img_j;
/*
Get grid cell index the x coordinate falls in
*/
ind= x/grid_cell_length[0];
if ( ind > grid_size[0]-1 )
ind= grid_size[0]-1;
grid_cell_ind[0]= ind;
/*
Let's take care of x coordinate
*/
y= (double)img_i;
/*
Get grid cell index the y coordinate falls in
*/
ind= y/grid_cell_length[1];
if ( ind > grid_size[1]-1 )
ind= grid_size[1]-1;
grid_cell_ind[1]= ind;
/*
Let's take care of rgb colors
*/
for ( rgb_ind= 0 ; rgb_ind< 3 ; rgb_ind++ ) {
/*
Get grid cell index the range coordinate falls in
*/
ind= (double)rgb[rgb_ind]/grid_cell_length[2+rgb_ind];
if ( ind > grid_size[2+rgb_ind]-1 )
ind= grid_size[2+rgb_ind]-1;
grid_cell_ind[2+rgb_ind]= ind;
}
/*
grid_cell_ind[5] are in fact the coordinates in bilateral space
*/
/*
At this point,
you are supposed to create a vert if it doesn't already exist
*/
/*
Let's see if vert has already been created
*/
hash_table_get_vert(
hash_table,
hash_table_size,
mesh,
grid_size,
grid_cell_ind,
&vert
);
if ( vert == -1 ) {
/*
Create bilateral space vert
*/
mesh_create_vert(
mesh,
grid_cell_ind,
&vert
);
/*
Insert vert into hash table
*/
hash_table_insert_vert(
hash_table,
hash_table_size,
mesh,
grid_size,
vert
);
nbr_verts++;
}
vert_pixel_arr[pixel].vert= vert;
vert_pixel_arr[pixel].pixel= pixel;
}
}
fprintf(stdout,"Nbr of pixels = %d\n",nbr_pixels);
fprintf(stdout,"Nbr of verts = %d\n",nbr_verts);
fprintf(stdout,"Ratio of pixels to verts = %f\n",
(double)nbr_pixels/(double)nbr_verts);
/*
Print some info about the hash table
For each hash table bucket,
print the number of verts that are in
*/
max_count= 0;
for ( hind= 0 ; hind< hash_table_size ; hind++ ) {
maxInd= arrayMaxId(hash_table[hind]);
count= maxInd+1;
if ( count > max_count )
max_count= count;
}
fprintf(stdout,"Max nbr of verts in hash table = %d\n",
max_count);
/*
Sort vert_pixel_arr w/r to vert and pixel
*/
/*
We're gonna use qsort that's part of C's standard library
*/
qsort(
vert_pixel_arr,
nbr_pixels,
sizeof(vert_pixel_struct),
compare_vert_pixel
);
/*
Print the vert pixel array after the sort
to make sure it's correctly sorted
*/
/*
for ( ind= 0 ; ind< nbr_pixels ; ind++ ) {
vert= vert_pixel_arr[ind].vert;
pixel= vert_pixel_arr[ind].pixel;
fprintf(stdout,"vert= %d pixel= %d\n",vert,pixel);
}
*/
/*
Build the splat matrix in Compressed Row Storage (CRS) format
*/
myMalloc(splat_matrix,matrix_int_crs_struct,sizeof(matrix_int_crs_struct));
splat_matrix->nnz= nbr_pixels;
myCalloc(splat_matrix->val,int,nbr_pixels,sizeof(int));
myCalloc(splat_matrix->col_ind,int,nbr_pixels,sizeof(int));
splat_matrix->n= nbr_verts;
myCalloc(splat_matrix->row_ptr,int,nbr_verts+1,sizeof(int));
vert_old= -1;
for ( ind= 0 ; ind< nbr_pixels ; ind++ ) {
vert= vert_pixel_arr[ind].vert;
pixel= vert_pixel_arr[ind].pixel;
/*
Vert is the row
Pixel is the column
for the splat matrix in full form
*/
splat_matrix->val[ind]= 1;
splat_matrix->col_ind[ind]= pixel;
if ( vert != vert_old ) {
/*
ind start the row defined by vert
*/
splat_matrix->row_ptr[vert]= ind;
vert_old= vert;
}
}
splat_matrix->row_ptr[nbr_verts]= nbr_pixels;
/*
We are gonna build the blur matrices
One per dimension
Recall that we have 5 dimensions (x,y,r,g,b)
The blur is a narrow [1,2,1] blur
(don't forget to divide by 4 to get the actual blur)
*/
/*
Allocate memory for each blur matrix
*/
/*
The format of the blur matrix is:
nbr of rows = nbr_verts
nbr of columns = 3
column 0 contains the vert corresponding to the ngb grid cell
column 1 contains vert
column 2 contains the vert corresponding to the ngb grid cell
*/
for ( dim= 0 ; dim< 5 ; dim++ )
myCalloc(blur_matrix[dim],int,3*nbr_verts,sizeof(int));
/*
Process the verts
*/
mesh_verts= mesh->mesh_verts;
maxInd= arrayMaxId(*mesh_verts);
nbr_verts= maxInd+1;
for ( vert= 0 ; vert<= maxInd ; vert++ ) {
mesh_vert_get_data(
mesh,
vert,
grid_cell_ind
);
for ( dim= 0 ; dim< 5 ; dim++ ) {
ind= grid_cell_ind[dim];
if ( ind < 0 ) {
error_handler("stereo_create_verts");
}
if ( ind > grid_size[dim]-1 ) {
error_handler("stereo_create_verts");
}
/*
Store vert in column 1
*/
blur_matrix[dim][vert*3+1]= vert;
/*
Get vert2 that's before vert along that dimension
Vert2 gets a weight of 1
*/
/*
Copy the coordinates
*/
for ( dim2= 0 ; dim2< 5 ; dim2++ )
grid_cell_ind2[dim2]= grid_cell_ind[dim2];
/*
Shift to the left along that dimension
*/
ind2= ind-1;
if ( ind2 < 0 )
ind2= 0;
grid_cell_ind2[dim]= ind2;
/*
Get the vert using the hash table
*/
hash_table_get_vert(
hash_table,
hash_table_size,
mesh,
grid_size,
grid_cell_ind2,
&vert2
);
if ( vert2 == -1 ) {
/*
Do as if vert were the neighboring vert
*/
vert2= vert;
}
/*
Store vert2 in column 0
*/
blur_matrix[dim][vert*3+0]= vert2;
/*
Get vert2 that's after vert along that dimension
Vert2 gets a weight of 1
*/
/*
Copy the coordinates
*/
for ( dim2= 0 ; dim2< 5 ; dim2++ )
grid_cell_ind2[dim2]= grid_cell_ind[dim2];
/*
Shift to the right along that dimension
*/
ind2= ind+1;
if ( ind2 > grid_size[dim]-1 )
ind2= grid_size[dim]-1;
grid_cell_ind2[dim]= ind2;
/*
Get the vert using the hash table
*/
hash_table_get_vert(
hash_table,
hash_table_size,
mesh,
grid_size,
grid_cell_ind2,
&vert2
);
if ( vert2 == -1 ) {
/*
Do as if vert were the neighboring vert
*/
vert2= vert;
}
/*
Store vert2 in column 2
*/
blur_matrix[dim][vert*3+2]= vert2;
}
}
/*
Free the vert pixel array
that was used to build the splat matrix
*/
myFree(vert_pixel_arr);
/*
Free the hash table
*/
for ( hind= 0 ; hind< hash_table_size ; hind++ ) {
arrayDelAll(hash_table[hind]);
}
if ( hash_table_size > 0 )
myFree(hash_table);
(*ptr_mesh)= mesh;
(*ptr_splat_matrix)= splat_matrix;
}