Skip to content

Commit ad2dd4c

Browse files
committed
Small changes
1 parent 475102c commit ad2dd4c

File tree

2 files changed

+66
-33
lines changed

2 files changed

+66
-33
lines changed

rasterize.c

+57-22
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@ int main(int argc, char **argv) {
2929
printf("Welcome to Rasterizer 3000.\n");
3030
printf("===========================\n\n");
3131

32-
Extents extents = { 0.0, 5.0, 5.0, 0.0, 1.0 };
32+
Config config;
3333

3434
int rows, //9175,
3535
cols, //6814,
3636
i,
3737
j;
3838

39-
calcImageSize(&extents, &rows, &cols);
39+
calcImageSize(&config, &rows, &cols);
4040

4141
Index index;
4242
index.nodes = malloc(rows*cols*sizeof(PointNode *));
43-
index.extents = &extents;
43+
index.config = &config;
4444
index.rows = rows;
4545
index.cols = cols;
4646

4747
Image image;
4848
image.pixels = malloc(rows*cols*sizeof(float));
4949
image.rows = rows;
5050
image.cols = cols;
51-
image.extents = &extents;
51+
image.config = &config;
5252

5353
// Initialize arrays
5454
for (j=0; j<rows; j++) {
@@ -72,6 +72,41 @@ int main(int argc, char **argv) {
7272
return 0;
7373
}
7474

75+
int readConfig(char *filename, Config *config) {
76+
printf("Reading config from file: %s\n\n", filename);
77+
FILE *file = fopen(filename, "r");
78+
float rx,ry,rz;
79+
int n = 0;
80+
81+
if ( fscanf(file, "lu_long=%d", &(config->lu_long)) != 1) {
82+
printf("Could not read lu_long from config.");
83+
return 1;
84+
}
85+
if ( fscanf(file, "lu_lat=%d", &(config->lu_lat)) != 1) {
86+
printf("Could not read lu_lat from config.");
87+
return 1;
88+
}
89+
if ( fscanf(file, "rl_long=%d", &(config->rl_long)) != 1) {
90+
printf("Could not read rl_long from config.");
91+
return 1;
92+
}
93+
if ( fscanf(file, "rl_lat=%d", &(config->rl_lat)) != 1) {
94+
printf("Could not read rl_lat from config.");
95+
return 1;
96+
}
97+
if ( fscanf(file, "gsd=%d", &(config->gsd)) != 1) {
98+
printf("Could not read gsd from config.");
99+
return 1;
100+
}
101+
102+
fclose(file);
103+
104+
printf("\nDone reading config.\n", n);
105+
printf(END_SECT);
106+
107+
return 0;
108+
}
109+
75110
// float valueUsingKNN(Index *index, int *row, int *col) {
76111
// int k = 5; // k-max
77112
// int k_used = 0;
@@ -99,7 +134,7 @@ Point3D* useNearest(Index *index, Point3D *pixel_rep) {
99134
d=1,
100135
row, col;
101136

102-
point2Index(index->extents, pixel_rep, &row, &col);
137+
point2Index(index->config, pixel_rep, &row, &col);
103138

104139
int minj = MAX(0, row-d),
105140
maxj = MIN(row+d, index->rows - 1),
@@ -153,20 +188,20 @@ void rasterize(Index *index, Image *image) {
153188
printf(END_SECT);
154189
}
155190

156-
void calcImageSize(Extents *extents, int *rows, int *cols) {
157-
assert(extents->lu_long < extents->rl_long && "Longitudes switched.");
158-
assert(extents->lu_lat > extents->rl_lat && "Latitudes switched.");
191+
void calcImageSize(Config *config, int *rows, int *cols) {
192+
assert(config->lu_long < config->rl_long && "Longitudes switched.");
193+
assert(config->lu_lat > config->rl_lat && "Latitudes switched.");
159194

160195
printf("Calculating image size ...\n");
161-
printf(" Image extents: left upper corner %6.2f %6.2f\n", extents->lu_long, extents->lu_lat);
162-
printf(" right lower corner %6.2f %6.2f\n", extents->rl_long, extents->rl_lat);
163-
printf(" GSD: %6.2f metres.\n\n", extents->gsd);
196+
printf(" Image extents: left upper corner %6.2f %6.2f\n", config->lu_long, config->lu_lat);
197+
printf(" right lower corner %6.2f %6.2f\n", config->rl_long, config->rl_lat);
198+
printf(" GSD: %6.2f metres.\n\n", config->gsd);
164199

165-
double hor = extents->rl_long - extents->lu_long;
166-
double ver = extents->lu_lat - extents->rl_lat;
200+
double hor = config->rl_long - config->lu_long;
201+
double ver = config->lu_lat - config->rl_lat;
167202

168-
*rows = floor(hor / extents->gsd);
169-
*cols = floor(ver / extents->gsd);
203+
*rows = floor(hor / config->gsd);
204+
*cols = floor(ver / config->gsd);
170205

171206
printf(" Horizontal extent: %6.2f metres => %d cols.\n", hor, *cols);
172207
printf(" Vertical extent: %6.2f metres => %d rows.\n", ver, *rows);
@@ -251,7 +286,7 @@ void readPointsFromFile(char *filename, Index *index) {
251286

252287
void addPointToIndex(Index *index, Point3D *point) {
253288
int col,row;
254-
point2Index(index->extents, point, &row, &col);
289+
point2Index(index->config, point, &row, &col);
255290
assert(row < (index->rows) && col < (index->cols) && "col,row < cols,rows");
256291
PointNode **binstart = (index->nodes)+(row*index->cols)+col;
257292
addToBin(binstart, point);
@@ -276,19 +311,19 @@ void addToBin(PointNode **current, Point3D* point) {
276311
}
277312
}
278313

279-
void point2Index(Extents *extents, Point3D *p, int *row, int *col) {
280-
*row = floor((p->x - extents->lu_long) / extents->gsd);
281-
*col = floor((p->y - extents->rl_lat) / extents->gsd);
314+
void point2Index(Config *config, Point3D *p, int *row, int *col) {
315+
*row = floor((p->x - config->lu_long) / config->gsd);
316+
*col = floor((p->y - config->rl_lat) / config->gsd);
282317
assert(*row >= 0 && *col >= 0 && "Row number positive.");
283318
// printf(" Point x=%4.2f y=%4.2f z=%4.2f -> row=%d col=%d.\n", p->x, p->y, p->z, *row, *col);
284319
}
285320

286321
void pixel2Point(Image *image, int *row, int *col, Point3D *point) {
287322
float col_rel = (float)*col / image->cols;
288323
float row_rel = (float)*row / image->rows;
289-
Extents *extents = image->extents;
290-
point->x = extents->lu_long + (extents->rl_long - extents->lu_long)*col_rel;
291-
point->y = extents->lu_lat + (extents->rl_lat - extents->lu_lat )*row_rel;
324+
Config *config = image->config;
325+
point->x = config->lu_long + (config->rl_long - config->lu_long)*col_rel;
326+
point->y = config->lu_lat + (config->rl_lat - config->lu_lat )*row_rel;
292327
point->z = EMPTY_VAL;
293328
printf("pixel2Point: col=%d row=%d -> x=%f y=%f\n", *col, *row, point->x, point->y);
294329
}

rasterize.h

+9-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <math.h>
77
#include <assert.h>
88
#include <sys/param.h>
9+
#include <glib-2.0/glib.h>
910

1011
#define EMPTY_VAL 0.0f
1112

@@ -15,12 +16,6 @@ typedef struct {
1516
double z;
1617
} Point3D;
1718

18-
// typedef struct
19-
// {
20-
// double x;
21-
// double y;
22-
// } Point2D;
23-
2419
typedef struct PointNode_t {
2520
Point3D p;
2621
struct PointNode_t *next;
@@ -32,18 +27,19 @@ typedef struct {
3227
double rl_long;
3328
double rl_lat;
3429
double gsd; // in metres
35-
} Extents;
30+
char *pointsFileName;
31+
} Config;
3632

3733
typedef struct {
3834
float *pixels;
3935
int rows;
4036
int cols;
41-
Extents *extents;
37+
Config *config;
4238
} Image;
4339

4440
typedef struct {
4541
PointNode **nodes;
46-
Extents *extents;
42+
Config *config;
4743
int rows;
4844
int cols;
4945
} Index;
@@ -60,9 +56,11 @@ void addToBin(PointNode **current, Point3D* point);
6056

6157
void readPointsFromFile(char *filename, Index *index);
6258

63-
void calcImageSize(Extents *extents, int *rows, int *cols);
59+
void readConfig(char *filename, Config *config);
60+
61+
void calcImageSize(Config *config, int *rows, int *cols);
6462

65-
void point2Index(Extents *extents, Point3D *point, int *row, int *col);
63+
void point2Index(Config *config, Point3D *point, int *row, int *col);
6664

6765
void pixel2Point(Image *image, int *row, int *col, Point3D *point);
6866

0 commit comments

Comments
 (0)