19
19
*
20
20
*/
21
21
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
- // }
31
22
#include "rasterize.h"
32
23
24
+ #define END_SECT "\n\n"
25
+
33
26
int main (int argc , char * * argv ) {
34
- printf ("Jotain\n" );
27
+ printf ("\n" );
28
+ printf ("Welcome to Rasterizer 3000.\n" );
29
+ printf ("===========================\n\n" );
35
30
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 };
37
32
38
- int rows = 5 , //9175,
39
- cols = 5 , //6814,
33
+ int rows , //9175,
34
+ cols , //6814,
40
35
i ,
41
36
j ;
42
37
38
+ calcImageSize (info , & rows , & cols );
39
+
43
40
Index index ;
44
41
index .nodes = malloc (rows * cols * sizeof (PointNode * ));
45
42
index .info = info ;
43
+ index .rows = rows ;
44
+ index .cols = cols ;
46
45
47
46
Image image ;
48
47
image .pixels = malloc (rows * cols * sizeof (float ));
@@ -58,44 +57,144 @@ int main(int argc, char **argv) {
58
57
}
59
58
60
59
// 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 );
72
61
73
62
// FIXME: muistin vapautus
63
+ printIndexBins (index );
74
64
75
65
printImage (image );
76
66
77
67
free (image .pixels );
78
68
free (index .nodes );
79
- printf ("Float %d" , sizeof (float ));
80
69
return 0 ;
81
70
}
82
71
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
+
83
115
void printImage (Image im ) {
116
+ printf ("Printing rasterized image:\n\n" );
117
+
84
118
int i ,j ;
85
119
for (j = 0 ; j < im .rows ; j ++ ) {
86
120
for (i = 0 ; i < im .cols ; i ++ ) {
87
121
printf ("%04.2f " , * (im .pixels + (j * im .cols )+ im .rows ));
88
122
}
89
123
printf ("\n" );
90
124
}
125
+
126
+ printf (END_SECT );
91
127
}
92
128
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 );
97
174
}
98
175
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
+ }
0 commit comments