7 #define RAYCASTLIB_PROFILE
10 #include "raycastlib.h"
13 Unit
testArrayFunc(int16_t x
, int16_t 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
;
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
)
41 printf("- casting ray:\n");
44 HitResult h
= castRay(r
,testArrayFunc
);
46 printf("- result:\n");
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
;
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
)
73 c
.resolution
.x
= resX
;
74 c
.resolution
.y
= resY
;
90 printf("- mapping pixel: %d %d %d\n",posX
,posY
,posZ
);
92 p
= mapToScreen(pos
,height
,c
);
94 printf("- result:\n");
97 result
= p
.position
.x
== expectX
&& p
.position
.y
== expectY
&&
103 printf("\nFAIL\n\n");
108 // returns milliseconds
109 long measureTime(void (*func
)(void))
112 struct timeval timecheck
;
114 gettimeofday(&timecheck
, NULL
);
115 start
= (long) timecheck
.tv_sec
* 1000 + (long) timecheck
.tv_usec
/ 1000;
119 gettimeofday(&timecheck
, NULL
);
120 end
= (long) timecheck
.tv_sec
* 1000 + (long) timecheck
.tv_usec
/ 1000;
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()
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;
154 c
.fovAngle
= UNITS_PER_SQUARE
/ 2;
161 pos
.x
= -1024 * UNITS_PER_SQUARE
;
162 pos
.y
= -512 * UNITS_PER_SQUARE
;
165 for (int i
= 0; i
< 1000000; ++i
)
167 p
= mapToScreen(pos
,height
,c
);
171 height
= (height
+ 16) % 1024;
175 void pixelFunc(PixelInfo p
)
179 void benchmarkRender()
183 c
.resolution
.x
= 640;
184 c
.resolution
.y
= 300;
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
);
202 printf("Testing raycastlib.\n");
205 3 * UNITS_PER_SQUARE
+ UNITS_PER_SQUARE
/ 2,
206 4 * UNITS_PER_SQUARE
+ UNITS_PER_SQUARE
/ 2,
232 -4 * UNITS_PER_SQUARE
- UNITS_PER_SQUARE
/ 2,
233 7 * UNITS_PER_SQUARE
+ UNITS_PER_SQUARE
/ 3,
240 if (!testSingleMapping(
243 UNITS_PER_SQUARE
/ 2,
249 UNITS_PER_SQUARE
/ 2,
250 UNITS_PER_SQUARE
/ 4,
257 printf("benchmark:\n");
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
);