Skip to content

Commit f4475ad

Browse files
committed
First steps of building the index.
Signed-off-by: Heikki Salo <[email protected]>
1 parent 7372045 commit f4475ad

File tree

3 files changed

+148
-36
lines changed

3 files changed

+148
-36
lines changed

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
all: rasterize
2+
3+
rasterize: rasterize.c rasterize.h
4+
gcc rasterize.c -o rasterize -lm -Wall

rasterize.c

+131-32
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,29 @@
1919
*
2020
*/
2121

22-
// void append(Point** node, Point* new) {
23-
// if( *node == 0 ) {
24-
// *node = malloc(sizeof(PointNode));
25-
// *node->p = *new;
26-
// *node->next = 0;
27-
// } else {
28-
// append(*node->
29-
// }
30-
// }
3122
#include "rasterize.h"
3223

24+
#define END_SECT "\n\n"
25+
3326
int main(int argc, char **argv) {
34-
printf("Jotain\n");
27+
printf("\n");
28+
printf("Welcome to Rasterizer 3000.\n");
29+
printf("===========================\n\n");
3530

36-
TaskInfo info = { 24.0, 65.0, 25.0, 66.0, 0.05 };
31+
TaskInfo info = { 0.0, 5.0, 5.0, 0.0, 1.0 };
3732

38-
int rows=5, //9175,
39-
cols=5, //6814,
33+
int rows, //9175,
34+
cols, //6814,
4035
i,
4136
j;
4237

38+
calcImageSize(info, &rows, &cols);
39+
4340
Index index;
4441
index.nodes = malloc(rows*cols*sizeof(PointNode *));
4542
index.info = info;
43+
index.rows = rows;
44+
index.cols = cols;
4645

4746
Image image;
4847
image.pixels = malloc(rows*cols*sizeof(float));
@@ -58,44 +57,144 @@ int main(int argc, char **argv) {
5857
}
5958

6059
// data
61-
Point3D a = { 1.7, 2.7, 8 };
62-
Point3D b = { 1.2, 2.2, 6 };
63-
Point3D c = { 1.4, 3.2, 5 };
64-
Point3D d = { 1.4, 3.2, 5 };
65-
Point3D e = { 2.5, 4.5, 3 };
66-
67-
// addPoint(nodes, rows, cols, &a);
68-
// addPoint(nodes, rows, cols, &b);
69-
// addPoint(nodes, rows, cols, &c);
70-
// addPoint(nodes, rows, cols, &d);
71-
// addPoint(nodes, rows, cols, &e);
60+
readPointsFromFile("sample.txt", &index);
7261

7362
// FIXME: muistin vapautus
63+
printIndexBins(index);
7464

7565
printImage(image);
7666

7767
free(image.pixels);
7868
free(index.nodes);
79-
printf("Float %d", sizeof(float));
8069
return 0;
8170
}
8271

72+
void calcImageSize(TaskInfo info, int *rows, int *cols) {
73+
assert(info.lu_long < info.rl_long && "Longitudes switched.");
74+
assert(info.lu_lat > info.rl_lat && "Latitudes switched.");
75+
printf("Calculating image size ...\n");
76+
printf(" Image extents: left upper corner %6.2f %6.2f\n", info.lu_long, info.lu_lat);
77+
printf(" right lower corner %6.2f %6.2f\n", info.rl_long, info.rl_lat);
78+
printf(" GSD: %6.2f metres.\n\n", info.gsd);
79+
80+
double hor = info.rl_long - info.lu_long;
81+
double ver = info.lu_lat - info.rl_lat;
82+
83+
*rows = floor(hor / info.gsd);
84+
*cols = floor(ver / info.gsd);
85+
86+
printf(" Horizontal extent: %6.2f metres => %d cols.\n", hor, *cols);
87+
printf(" Vertical extent: %6.2f metres => %d rows.\n", ver, *rows);
88+
printf(END_SECT);
89+
}
90+
91+
int depth(int d, PointNode *node) {
92+
if(node == 0) {
93+
return d;
94+
} else {
95+
return depth(d+1, node->next);
96+
}
97+
}
98+
99+
// Prints out Index struct's bin usages.
100+
void printIndexBins(Index index) {
101+
printf("Printing number of links per index bins.\n\n");
102+
103+
int i,j,d;
104+
for (j=0; j<index.rows; j++) {
105+
for (i=0; i<index.cols; i++) {
106+
d = depth(0, *(index.nodes + index.cols*j + i));
107+
printf("%d ", d);
108+
}
109+
printf("\n");
110+
}
111+
112+
printf(END_SECT);
113+
}
114+
83115
void printImage(Image im) {
116+
printf("Printing rasterized image:\n\n");
117+
84118
int i,j;
85119
for (j=0; j<im.rows; j++) {
86120
for (i=0; i<im.cols; i++) {
87121
printf("%04.2f ", *(im.pixels+(j*im.cols)+im.rows));
88122
}
89123
printf("\n");
90124
}
125+
126+
printf(END_SECT);
91127
}
92128

93-
void addPoint(Index index, Point3D *point) {
94-
// int i = floor(p->x);
95-
// int j = floor(p->y);
96-
// append(&nodes[j][i], p);
129+
void readPointsFromFile(char *filename, Index *index) {
130+
printf("Reading points from file: %s\n\n", filename);
131+
FILE *file = fopen(filename, "r");
132+
float rx,ry,rz;
133+
int n = 0;
134+
135+
int valuesToRead = 3;
136+
while (fscanf(file, "%f %f %f", &rx, &ry, &rz) == valuesToRead) {
137+
Point3D p;
138+
p.x = rx;
139+
p.y = ry;
140+
p.z = rz;
141+
addPointToIndex(index, &p);
142+
143+
if(n < 10) {
144+
printf("x=%04.2f y=%04.2f z=%04.2f\n", rx, ry, rz);
145+
} else if (n == 11) {
146+
printf("...\n");
147+
} else {
148+
// Gradually increase shown amounts.
149+
if (n <= 100 && n % 10) { printf("%d.. ", n); }
150+
else if (n > 100 && n <= 1000 && n % 100 ) { printf("%d.. ", n); }
151+
else if (n > 1000 && n <= 10000 && n % 1000 ) { printf("%d.. ", n); }
152+
else if (n > 10000 && n <= 100000 && n % 10000 ) { printf("%d.. ", n); }
153+
else if (n > 100000 && n <= 1000000 && n % 100000) { printf("%d.. ", n); }
154+
}
155+
n++;
156+
}
157+
158+
fclose(file);
159+
160+
printf("\nDone reading %d points to index.\n", n);
161+
162+
printf(END_SECT);
163+
}
164+
165+
166+
167+
168+
void addPointToIndex(Index *index, Point3D *point) {
169+
int col,row;
170+
transformPoint(index->info, point, &row, &col);
171+
assert(row < (index->rows) && col < (index->cols) && "col,row < cols,rows");
172+
PointNode **binstart = (index->nodes)+(row*index->cols)+col;
173+
addToBin(binstart, point);
97174
}
98175

99-
// void transform(TaskInfo info, Point3D *p, int *row, int *col) {
100-
// printf("asda");
101-
// }
176+
void addToBin(PointNode **current, Point3D* point) {
177+
PointNode *new = malloc(sizeof(PointNode));
178+
(new->p).x = point->x;
179+
(new->p).y = point->y;
180+
(new->p).z = point->z;
181+
new->next = NULL;
182+
183+
if( *current == NULL ) {
184+
*current = new;
185+
} else {
186+
PointNode *i = *current;
187+
while(1) {
188+
if (i->next == NULL) break;
189+
i = (*current)->next;
190+
}
191+
i->next = new;
192+
}
193+
}
194+
195+
void transformPoint(TaskInfo info, Point3D *p, int *row, int *col) {
196+
*row = floor((p->x - info.lu_long) / info.gsd);
197+
*col = floor((p->y - info.rl_lat) / info.gsd);
198+
assert(*row >= 0 && *col >= 0 && "Row number positive.");
199+
printf(" Transformed point %4.2f %4.2f %4.2f to row=%d col=%d.\n", p->x, p->y, p->z, *row, *col);
200+
}

rasterize.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdio.h>
55
#include <stdlib.h>
66
#include <math.h>
7+
#include <assert.h>
78

89
#define EMPTY_VAL 0.0f
910

@@ -19,9 +20,9 @@ typedef struct {
1920
// double y;
2021
// } Point2D;
2122

22-
typedef struct {
23+
typedef struct PointNode_t {
2324
Point3D p;
24-
struct PointNode *next;
25+
struct PointNode_t *next;
2526
} PointNode;
2627

2728
typedef struct {
@@ -47,8 +48,16 @@ typedef struct {
4748

4849
void printImage(Image image);
4950

50-
void addPoint(Index index, Point3D *point);
51+
void printIndexBins(Index index);
52+
53+
void addPointToIndex(Index *index, Point3D *point);
54+
55+
void addToBin(PointNode **current, Point3D* point);
56+
57+
void readPointsFromFile(char *filename, Index *index);
58+
59+
void transformPoint(TaskInfo info, Point3D *p, int *row, int *col);
5160

52-
// void transform(TaskInfo info, Point3D *p, int *row, int *col);
61+
void calcImageSize(TaskInfo info, int *rows, int *cols);
5362

5463
#endif

0 commit comments

Comments
 (0)