Optimize
[raycastlib.git] / test.c
blobb0c146196ba103b7d3ff570520508db40b2dc5fc
1 /**
2 Tests for raycastlib.
4 license: CC0
5 */
7 #define RAYCASTLIB_PROFILE
9 #include <stdio.h>
10 #include "raycastlib.h"
11 #include <sys/time.h>
13 Unit testArrayFunc(int16_t x, int16_t y)
15 if (x > 12 || y > 12)
16 return x * y;
18 return (x < 0 || y < 0 || x > 9 || y > 9) ? 1 : 0;
21 Unit testArrayFunc2(int16_t x, int16_t y)
23 return testArrayFunc(x,y) + 10 * UNITS_PER_SQUARE;
26 /**
27 Simple automatic test function.
30 int testSingleRay(Unit startX, Unit startY, Unit dirX, Unit dirY,
31 int16_t expectSquareX, int16_t expectSquareY, int16_t expectPointX,
32 int16_t expectPointY, int16_t tolerateError)
34 Ray r;
36 r.start.x = startX;
37 r.start.y = startY;
38 r.direction.x = dirX;
39 r.direction.y = dirY;
41 printf("- casting ray:\n");
42 logRay(r);
44 HitResult h = castRay(r,testArrayFunc);
46 printf("- result:\n");
47 logHitResult(h);
49 int result =
50 h.square.x == expectSquareX &&
51 h.square.y == expectSquareY &&
52 h.position.x <= expectPointX + tolerateError &&
53 h.position.x >= expectPointX - tolerateError &&
54 h.position.y <= expectPointY + tolerateError &&
55 h.position.y >= expectPointY - tolerateError;
57 if (result)
58 printf("\nOK\n\n");
59 else
60 printf("\nFAIL\n\n");
62 return result;
65 int testSingleMapping(Unit posX, Unit posY, Unit posZ, uint32_t resX,
66 uint32_t resY, Unit camX, Unit camY, Unit camZ, Unit camDir, Unit fov,
67 Unit expectX, Unit expectY, Unit expectZ)
69 int result;
71 Camera c;
73 c.resolution.x = resX;
74 c.resolution.y = resY;
75 c.position.x = camX;
76 c.position.y = camY;
77 c.direction = camDir;
78 c.height = camZ;
79 c.fovAngle = fov;
81 Vector2D pos;
82 Unit height;
84 pos.x = posX;
85 pos.y = posY;
86 height = posZ;
88 PixelInfo p;
90 printf("- mapping pixel: %d %d %d\n",posX,posY,posZ);
92 p = mapToScreen(pos,height,c);
94 printf("- result:\n");
95 logPixelInfo(p);
97 result = p.position.x == expectX && p.position.y == expectY &&
98 p.depth == expectZ;
100 if (result)
101 printf("\nOK\n\n");
102 else
103 printf("\nFAIL\n\n");
105 return result;
108 // returns milliseconds
109 long measureTime(void (*func)(void))
111 long start, end;
112 struct timeval timecheck;
114 gettimeofday(&timecheck, NULL);
115 start = (long) timecheck.tv_sec * 1000 + (long) timecheck.tv_usec / 1000;
117 func();
119 gettimeofday(&timecheck, NULL);
120 end = (long) timecheck.tv_sec * 1000 + (long) timecheck.tv_usec / 1000;
122 return end - start;
125 void benchCastRays()
127 Ray r;
129 r.start.x = UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2;
130 r.start.y = 2 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 4;
132 Vector2D directions[8];
134 for (int i = 0; i < 8; ++i)
135 directions[i] = angleToDirection(UNITS_PER_SQUARE / 8 * i);
137 for (int i = 0; i < 1000000; ++i)
139 r.direction = directions[i % 8];
140 castRay(r,testArrayFunc);
144 void benchmarkMapping()
146 Camera c;
148 c.resolution.x = 1024;
149 c.resolution.y = 768;
150 c.position.x = UNITS_PER_SQUARE / 2;
151 c.position.y = UNITS_PER_SQUARE * 2;
152 c.direction = UNITS_PER_SQUARE / 8;
153 c.height = 0;
154 c.fovAngle = UNITS_PER_SQUARE / 2;
156 PixelInfo p;
158 Vector2D pos;
159 Unit height;
161 pos.x = -1024 * UNITS_PER_SQUARE;
162 pos.y = -512 * UNITS_PER_SQUARE;
163 height = 0;
165 for (int i = 0; i < 1000000; ++i)
167 p = mapToScreen(pos,height,c);
169 pos.x += 4;
170 pos.y += 8;
171 height = (height + 16) % 1024;
175 void pixelFunc(PixelInfo p)
179 void benchmarkRender()
181 Camera c;
183 c.resolution.x = 640;
184 c.resolution.y = 300;
185 c.position.x = 10;
186 c.position.y = 12;
187 c.direction = 100;
188 c.height = 200;
189 c.fovAngle = UNITS_PER_SQUARE / 3;
191 RayConstraints constraints;
193 constraints.maxHits = 10;
194 constraints.maxSteps = 12;
196 for (int i = 0; i < 100; ++i)
197 render(c,testArrayFunc,testArrayFunc2,pixelFunc,constraints);
200 int main()
202 printf("Testing raycastlib.\n");
204 if (!testSingleRay(
205 3 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2,
206 4 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 2,
207 100, 50,
208 10, 7,
209 10240, 7936,
210 16))
211 return 1;
213 if (!testSingleRay(
216 100, 100,
217 10, 9,
218 10240, 10240,
219 16))
220 return 1;
222 if (!testSingleRay(
223 400,
224 6811,
225 -629,805,
226 -1, 7,
227 -1, 7325,
228 16))
229 return 1;
231 if (!testSingleRay(
232 -4 * UNITS_PER_SQUARE - UNITS_PER_SQUARE / 2,
233 7 * UNITS_PER_SQUARE + UNITS_PER_SQUARE / 3,
234 100,-100,
235 0, 2,
236 1, 2900,
237 16))
238 return 1;
240 if (!testSingleMapping(
241 -UNITS_PER_SQUARE,
243 UNITS_PER_SQUARE / 2,
244 1280,
245 640,
249 UNITS_PER_SQUARE / 2,
250 UNITS_PER_SQUARE / 4,
251 640,
253 1024
255 return -1;
257 printf("benchmark:\n");
259 long t;
260 t = measureTime(benchCastRays);
261 printf("cast 1000000 rays: %ld ms\n",t);
263 t = measureTime(benchmarkMapping);
264 printf("map point to screen 1000000 times: %ld ms\n",t);
266 t = measureTime(benchmarkRender);
267 printf("render 100 times: %ld ms\n",t);
269 printf("\n");
270 printProfile();
272 return 0;