-
Notifications
You must be signed in to change notification settings - Fork 0
/
rasterize.c
329 lines (269 loc) · 9.2 KB
/
rasterize.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
/*
* Long/lat's are projected to pixel centers.
*
* h = rows-1, w = cols-1
*
*
*
*
* LONG / LAT
* ^ 0,0 | w-1,0
* lat | lu-----+ ------+------------------
* | |a | ======> |
* | | b| |
* | +-----rl |
* | |
* ----+-----------------> 0,h-1 |
* 0,0 | long
* lu -> 0,0
* ^-- greenwich rl ->
*
*/
#include "rasterize.h"
#define END_SECT "\n\n"
int main(int argc, char **argv) {
printf("\n");
printf("Welcome to Rasterizer 3000.\n");
printf("===========================\n\n");
Config config;
int rows, //9175,
cols, //6814,
i,
j;
calcImageSize(&config, &rows, &cols);
Index index;
index.nodes = malloc(rows*cols*sizeof(PointNode *));
index.config = &config;
index.rows = rows;
index.cols = cols;
Image image;
image.pixels = malloc(rows*cols*sizeof(float));
image.rows = rows;
image.cols = cols;
image.config = &config;
// Initialize arrays
for (j=0; j<rows; j++) {
for (i=0; i<cols; i++) {
*(index.nodes+(j*cols)+i) = 0;
*(image.pixels+(j*cols)+i) = EMPTY_VAL;
}
}
// data
readPointsFromFile("sample.txt", &index);
printIndexBins(&index);
rasterize(&index, &image);
printImage(&image);
free(image.pixels);
free(index.nodes);
return 0;
}
int readConfig(char *filename, Config *config) {
printf("Reading config from file: %s\n\n", filename);
FILE *file = fopen(filename, "r");
float rx,ry,rz;
int n = 0;
if ( fscanf(file, "lu_long=%d", &(config->lu_long)) != 1) {
printf("Could not read lu_long from config.");
return 1;
}
if ( fscanf(file, "lu_lat=%d", &(config->lu_lat)) != 1) {
printf("Could not read lu_lat from config.");
return 1;
}
if ( fscanf(file, "rl_long=%d", &(config->rl_long)) != 1) {
printf("Could not read rl_long from config.");
return 1;
}
if ( fscanf(file, "rl_lat=%d", &(config->rl_lat)) != 1) {
printf("Could not read rl_lat from config.");
return 1;
}
if ( fscanf(file, "gsd=%d", &(config->gsd)) != 1) {
printf("Could not read gsd from config.");
return 1;
}
fclose(file);
printf("\nDone reading config.\n", n);
printf(END_SECT);
return 0;
}
// float valueUsingKNN(Index *index, int *row, int *col) {
// int k = 5; // k-max
// int k_used = 0;
// float nearest[5];
//
// int i;
// float ksum = 0f;
// for(i=0; i<k_used; i++) {
// ksum += nearest[i];
// }
// return ksum/(float)k_used;
// }
double dist(double x1, double y1, double x2, double y2) {
return sqrt( pow(x1-x2, 2) + pow(y1-y2, 2) );
}
int blockDist(int x1, int y1, int x2, int y2) {
return ceil( sqrt( pow(x1-x2, 2) + pow(y1-y2, 2) ) );
}
Point3D* useNearest(Index *index, Point3D *pixel_rep) {
Point3D *nearest = NULL;
int i,j,
d=1,
row, col;
point2Index(index->config, pixel_rep, &row, &col);
int minj = MAX(0, row-d),
maxj = MIN(row+d, index->rows - 1),
mini = MAX(0, col-d),
maxi = MIN(col+d, index->cols - 1);
double shortestDist = 100000000000;
// Go through relevant bins
for (j = minj; j <= maxj; j++) {
for (i = mini; i <= maxi; i++) {
// Go through one bin
PointNode *node = *(index->nodes+j*(index->cols)+i);
while (node != NULL) {
Point3D *current = &(node->p);
double currentDist = dist(pixel_rep->x, pixel_rep->y, current->x, current->y);
if ( shortestDist > currentDist ) {
nearest = current;
shortestDist = currentDist;
}
node = node->next;
}
}
}
printf("row=%d col=%d ... mini=%d maxi=%d minj=%d maxj=%d\n", row, col, mini, maxi, minj, maxj);
return nearest;
}
void rasterize(Index *index, Image *image) {
printf("Creating raster image from indexed points.\n");
int i,j;
for (j=0; j < index->rows; j++) {
for (i=0; i < index->cols; i++) {
Point3D pixel_rep;
pixel2Point(image, &j, &i, &pixel_rep);
Point3D *nearest = useNearest(index, &pixel_rep);
if(nearest != NULL) {
*(image->pixels+(j*index->cols)+i) = nearest->z;
} else {
*(image->pixels+(j*index->cols)+i) = EMPTY_VAL;
}
}
}
printf(END_SECT);
}
void calcImageSize(Config *config, int *rows, int *cols) {
assert(config->lu_long < config->rl_long && "Longitudes switched.");
assert(config->lu_lat > config->rl_lat && "Latitudes switched.");
printf("Calculating image size ...\n");
printf(" Image extents: left upper corner %6.2f %6.2f\n", config->lu_long, config->lu_lat);
printf(" right lower corner %6.2f %6.2f\n", config->rl_long, config->rl_lat);
printf(" GSD: %6.2f metres.\n\n", config->gsd);
double hor = config->rl_long - config->lu_long;
double ver = config->lu_lat - config->rl_lat;
*rows = floor(hor / config->gsd);
*cols = floor(ver / config->gsd);
printf(" Horizontal extent: %6.2f metres => %d cols.\n", hor, *cols);
printf(" Vertical extent: %6.2f metres => %d rows.\n", ver, *rows);
printf(END_SECT);
}
int binDepth(int d, PointNode *node) {
if(node == 0) {
return d;
} else {
return binDepth(d+1, node->next);
}
}
// Prints out Index struct's bin usages.
void printIndexBins(Index *index) {
printf("Printing out index bin depths (number of links per bin):\n\n");
int i,j,d;
for (j=0; j<index->rows; j++) {
for (i=0; i<index->cols; i++) {
d = binDepth(0, *(index->nodes + index->cols*j + i));
printf("%d ", d);
}
printf("\n");
}
printf(END_SECT);
}
void printImage(Image *image) {
printf("Printing rasterized image:\n\n");
int i,j;
for (j=0; j<image->rows; j++) {
for (i=0; i<image->cols; i++) {
printf("%04.2f ", *(image->pixels+(j*image->cols)+i));
}
printf("\n");
}
printf(END_SECT);
}
void readPointsFromFile(char *filename, Index *index) {
printf("Reading points from file: %s\n\n", filename);
FILE *file = fopen(filename, "r");
float rx,ry,rz;
int n = 0;
int valuesToRead = 3;
while (fscanf(file, "%f %f %f", &rx, &ry, &rz) == valuesToRead) {
Point3D p;
p.x = rx;
p.y = ry;
p.z = rz;
addPointToIndex(index, &p);
if(n <= 10) {
printf("x=%04.2f y=%04.2f z=%04.2f\n", rx, ry, rz);
} else if (n == 11) {
printf("...\n");
} else {
// Gradually increase shown amounts.
if (n <= 100 && n % 10 == 0) { printf("%d.. ", n); }
else if (n > 100 && n <= 1000 && n % 100 == 0) { printf("%d.. ", n); }
else if (n > 1000 && n <= 10000 && n % 1000 == 0) { printf("%d.. ", n); }
else if (n > 10000 && n <= 100000 && n % 10000 == 0) { printf("%d.. ", n); }
else if (n > 100000 && n <= 1000000 && n % 100000 == 0) { printf("%d.. ", n); }
}
n++;
}
fclose(file);
printf("\nDone reading %d points to index.\n", n);
printf(END_SECT);
}
void addPointToIndex(Index *index, Point3D *point) {
int col,row;
point2Index(index->config, point, &row, &col);
assert(row < (index->rows) && col < (index->cols) && "col,row < cols,rows");
PointNode **binstart = (index->nodes)+(row*index->cols)+col;
addToBin(binstart, point);
}
void addToBin(PointNode **current, Point3D* point) {
PointNode *new = malloc(sizeof(PointNode));
(new->p).x = point->x;
(new->p).y = point->y;
(new->p).z = point->z;
new->next = NULL;
if( *current == NULL ) {
*current = new;
} else {
PointNode *i = *current;
while(1) {
if (i->next == NULL) break;
i = i->next;
}
i->next = new;
}
}
void point2Index(Config *config, Point3D *p, int *row, int *col) {
*row = floor((p->x - config->lu_long) / config->gsd);
*col = floor((p->y - config->rl_lat) / config->gsd);
assert(*row >= 0 && *col >= 0 && "Row number positive.");
// printf(" Point x=%4.2f y=%4.2f z=%4.2f -> row=%d col=%d.\n", p->x, p->y, p->z, *row, *col);
}
void pixel2Point(Image *image, int *row, int *col, Point3D *point) {
float col_rel = (float)*col / image->cols;
float row_rel = (float)*row / image->rows;
Config *config = image->config;
point->x = config->lu_long + (config->rl_long - config->lu_long)*col_rel;
point->y = config->lu_lat + (config->rl_lat - config->lu_lat )*row_rel;
point->z = EMPTY_VAL;
printf("pixel2Point: col=%d row=%d -> x=%f y=%f\n", *col, *row, point->x, point->y);
}