Browse Source

Gitter: timing tests

Johannes Kimmel 9 years ago
parent
commit
d9fe2a71b5
4 changed files with 68 additions and 26 deletions
  1. 2
    2
      gitter/main.c
  2. 6
    4
      gitter/route.c
  3. 58
    20
      gitter/screen.c
  4. 2
    0
      gitter/screen.h

+ 2
- 2
gitter/main.c View File

@@ -3,8 +3,8 @@
3 3
 #include "route.h"
4 4
 
5 5
 int main(int argc, char *argv[]) {
6
-	GLOBALS.WIDTH  = 1024;
7
-	GLOBALS.HEIGHT = 1024;
6
+	GLOBALS.WIDTH  = 768;
7
+	GLOBALS.HEIGHT = 768;
8 8
 
9 9
 	sdl_init();
10 10
 	main_loop();

+ 6
- 4
gitter/route.c View File

@@ -46,17 +46,17 @@ waypoint_t* go_around(pixel_t* A, pixel_t* B, pixel_t* C) {
46 46
 	double r = dividing_ratio(A, B, C);
47 47
 	pixel_t X = { A->x + r*(B->x - A->x), A->y + r*(B->y - A->y) };
48 48
 	double d = dist(&X, C);
49
-	pixel_t W = {C->x + 15*sqrt(2)*(X.x - C->x) / d, C->y + 15*sqrt(2)*(X.y - C->y) / d};
49
+	pixel_t W = {C->x + safety_radius*sqrt(2)*(X.x - C->x) / d, C->y + safety_radius*sqrt(2)*(X.y - C->y) / d};
50 50
 	waypoint_t* wp = malloc(sizeof(waypoint_t));
51 51
 	wp->next = NULL;
52 52
 	wp->point.x = W.x;
53 53
 	wp->point.y = W.y;
54
-	printf("Suggest you go via (%f,%f) to avoid (%f,%f)\n", W.x, W.y, C->x, C->y);
54
+	//printf("Suggest you go via (%f,%f) to avoid (%f,%f)\n", W.x, W.y, C->x, C->y);
55 55
 	return wp;
56 56
 }
57 57
 
58 58
 waypoint_t* route(pixel_t* start, pixel_t* stop, pixel_t* points, int n_points) {
59
-	printf("Running route\n");
59
+	//printf("Running route\n");
60 60
 
61 61
 	/*
62 62
 	pixel_t A = {0, 0};
@@ -85,7 +85,7 @@ waypoint_t* route(pixel_t* start, pixel_t* stop, pixel_t* points, int n_points)
85 85
 		double r = dividing_ratio(start, stop, &(points[i]));
86 86
 		if (r > 0 && r < 1) {
87 87
 			double d = dist_to_line(start, stop, &(points[i]));
88
-			if (fabs(d) < 15) {
88
+			if (fabs(d) < safety_radius) {
89 89
 				//printf("Point #%d at (%f,%f) is an obstacle %f pixel away from the course\n", i, points[i].x, points[i].y, d);
90 90
 				if(r < r_min) {
91 91
 					i_min = i;
@@ -132,6 +132,7 @@ waypoint_t* plotCourse(pixel_t* start, pixel_t* stop, pixel_t* points, int n) {
132 132
 	wp_stop->point.x = stop->x;
133 133
 	wp_stop->point.y = stop->y;
134 134
 	wp_start->next = route(start, stop, points, n_points);
135
+	wp_stop->next = NULL;
135 136
 	waypoint_t* t = wp_start;
136 137
 
137 138
 	while (t->next != NULL) {
@@ -192,6 +193,7 @@ void find_face(pixel_t *p, pixel_t *points, int n, int *x, int *y) {
192 193
 
193 194
 				printf("edge: %d, r %f, x %d, y %d\n", edge, r, face_x, face_y);
194 195
 				retry = 1;
196
+				count++;
195 197
 
196 198
 				switch (edge) {
197 199
 					case 0 :

+ 58
- 20
gitter/screen.c View File

@@ -1,4 +1,5 @@
1 1
 #include <stdio.h>
2
+#include <sys/time.h>
2 3
 #include <SDL/SDL.h>
3 4
 #include <SDL/SDL_gfxPrimitives.h>
4 5
 #include <errno.h>
@@ -14,6 +15,7 @@
14 15
  */
15 16
 
16 17
 SDL_Surface *screen;
18
+int safety_radius;
17 19
 
18 20
 void sdl_setup() {
19 21
 	const SDL_VideoInfo *info;
@@ -53,7 +55,7 @@ void draw_line(pixel_t *p1, pixel_t *p2) {
53 55
 
54 56
 void draw_blob(pixel_t *p1) {
55 57
 	aacircleRGBA(screen, p1->x, p1->y, 2, 255, 255, 255, 255);
56
-	//aacircleRGBA(screen, p1->x, p1->y, 10, 64, 64, 64, 255);
58
+	aacircleRGBA(screen, p1->x, p1->y, safety_radius, 64, 64, 64, 255);
57 59
 }
58 60
 
59 61
 void draw_points(pixel_t *points, int n) {
@@ -81,6 +83,16 @@ void draw_grid(pixel_t *points, int n) {
81 83
 	}
82 84
 }
83 85
 
86
+void draw_route(waypoint_t* route, int r, int g, int b) {
87
+	waypoint_t* t = route;
88
+	while (t->next != NULL) {
89
+		aacircleRGBA(screen, t->point.x, t->point.y, 2, r, g, b, 255);
90
+		aalineRGBA(screen, t->point.x, t->point.y, t->next->point.x, t->next->point.y, r, g, b, 64);
91
+		t = t->next;
92
+	}
93
+	aacircleRGBA(screen, t->point.x, t->point.y, 2, r, g, g, 255);
94
+}
95
+
84 96
 void randomize(pixel_t *points, int n, float fac) {
85 97
 	int i;
86 98
 
@@ -104,7 +116,7 @@ void set(pixel_t *points, int n) {
104 116
 
105 117
 int main_loop() {
106 118
 	int quit = 0;
107
-	int n = 5;
119
+	int n = 16;
108 120
 	int view_grid = 0;
109 121
 	int view_points  = 1;
110 122
 	float fac = GLOBALS.WIDTH / (n + 1) * 0.3;
@@ -112,13 +124,20 @@ int main_loop() {
112 124
 	pixel_t *points;
113 125
 	pixel_t start = {0, 0};
114 126
 	pixel_t stop = {0, 0};
115
-	waypoint_t* route = NULL;
127
+	waypoint_t* route1 = NULL;
128
+	waypoint_t* route1s = NULL;
129
+	waypoint_t* route2 = NULL;
130
+	waypoint_t* route2s = NULL;
131
+
132
+	struct timeval t1, t2;
133
+	double elapsedTime;
116 134
 
117 135
 	points = (pixel_t *) malloc (sizeof(pixel_t) * n * n);
118 136
 
119 137
 	set(points, n);
120 138
 
121 139
 	while (!quit) {
140
+		safety_radius = GLOBALS.HEIGHT / (n + 1) / 5 ;
122 141
 		SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );
123 142
 		if (view_grid) {
124 143
 			draw_grid(points, n);
@@ -128,21 +147,11 @@ int main_loop() {
128 147
 			draw_points(points, n);
129 148
 		}
130 149
 
131
-		if (route != NULL) {
132
-			waypoint_t* r = route;
133
-			while (r->next != NULL) {
134
-				aacircleRGBA(screen, r->point.x, r->point.y, 2, 0, 0, 255, 255);
135
-				aalineRGBA(screen, r->point.x, r->point.y, r->next->point.x, r->next->point.y, 0, 0, 255, 64);
136
-				r = r->next;
137
-			}
138
-			aacircleRGBA(screen, r->point.x, r->point.y, 2, 0, 0, 255, 255);
139
-			r = smooth(route, 5);
140
-			while (r->next != NULL) {
141
-				aacircleRGBA(screen, r->point.x, r->point.y, 2, 0, 255, 255, 255);
142
-				aalineRGBA(screen, r->point.x, r->point.y, r->next->point.x, r->next->point.y, 0, 255, 255, 64);
143
-				r = r->next;
144
-			}
145
-			aacircleRGBA(screen, r->point.x, r->point.y, 2, 0, 0, 255, 255);
150
+		if (route1s != NULL) {
151
+			draw_route(route1s, 0, 0, 255);
152
+		}
153
+		if (route2s != NULL) {
154
+			draw_route(route2s, 255, 255, 0);
146 155
 		}
147 156
 
148 157
 		if (start.x != 0 && start.y != 0) {
@@ -171,13 +180,42 @@ int main_loop() {
171 180
 						break;
172 181
 				}
173 182
 				if (start.x != 0 && start.y != 0 && stop.x != 0 && stop.y != 0) {
174
-					printf("Route from (%f,%f) to (%f,%f)\n", start.x, start.y, stop.x, stop.y);
175
-					route = plotCourse_gridbased(&start, &stop, points, n);
183
+					printf("\n\nRoute from (%f,%f) to (%f,%f)\n", start.x, start.y, stop.x, stop.y);
184
+
185
+					gettimeofday(&t1, NULL);
186
+					route1 = plotCourse(&start, &stop, points, n);
187
+					gettimeofday(&t2, NULL);
188
+					elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
189
+					elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
190
+					printf("plotCourse scanline based took %f ms\n", elapsedTime);
191
+
192
+					gettimeofday(&t1, NULL);
193
+					route1s = smooth(route1, 5);
194
+					gettimeofday(&t2, NULL);
195
+					elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
196
+					elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
197
+					printf("smoth scanline based route took %f ms\n", elapsedTime);
198
+
199
+					gettimeofday(&t1, NULL);
200
+					route2 = plotCourse_gridbased(&start, &stop, points, n);
201
+					gettimeofday(&t2, NULL);
202
+					elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
203
+					elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
204
+					printf("plotCourse gridline based took %f ms\n", elapsedTime);
205
+
206
+					gettimeofday(&t1, NULL);
207
+					route2s = smooth(route2, 5);
208
+					gettimeofday(&t2, NULL);
209
+					elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;
210
+					elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;
211
+					printf("smooth gridline based route took %f ms\n", elapsedTime);
212
+					/*
176 213
 					waypoint_t* r = route;
177 214
 					while (r != NULL) {
178 215
 						printf("Waypoint (%f,%f)\n", r->point.x, r->point.y);
179 216
 						r = r->next;
180 217
 					}
218
+					*/
181 219
 				}
182 220
 				break;
183 221
 			case SDL_KEYDOWN :

+ 2
- 0
gitter/screen.h View File

@@ -8,6 +8,8 @@ typedef struct {
8 8
 	float y;
9 9
 } pixel_t;
10 10
 
11
+extern int safety_radius;
12
+
11 13
 void sdl_init();
12 14
 int main_loop();
13 15