mirror of
https://git.coom.tech/drummyfish/raycastlib.git
synced 2024-11-21 20:29:59 +01:00
Add tests
This commit is contained in:
parent
c819d05b56
commit
2f351a7d66
2 changed files with 53 additions and 17 deletions
40
raycastlib.h
40
raycastlib.h
|
@ -200,6 +200,8 @@ void render(Camera cam, ArrayFunction arrayFunc, PixelFunction pixelFunc,
|
||||||
uint32_t profile_normalize = 0;
|
uint32_t profile_normalize = 0;
|
||||||
uint32_t profile_vectorsAngleCos = 0;
|
uint32_t profile_vectorsAngleCos = 0;
|
||||||
uint32_t profile_perspectiveScale = 0;
|
uint32_t profile_perspectiveScale = 0;
|
||||||
|
uint32_t profile_wrap = 0;
|
||||||
|
uint32_t profile_divRoundDown = 0;
|
||||||
#define profileCall(c) profile_##c += 1
|
#define profileCall(c) profile_##c += 1
|
||||||
|
|
||||||
#define printProfile() {\
|
#define printProfile() {\
|
||||||
|
@ -217,7 +219,9 @@ void render(Camera cam, ArrayFunction arrayFunc, PixelFunction pixelFunc,
|
||||||
printf(" normalize: %d\n",profile_normalize);\
|
printf(" normalize: %d\n",profile_normalize);\
|
||||||
printf(" vectorsAngleCos: %d\n",profile_vectorsAngleCos);\
|
printf(" vectorsAngleCos: %d\n",profile_vectorsAngleCos);\
|
||||||
printf(" absVal: %d\n",profile_absVal);\
|
printf(" absVal: %d\n",profile_absVal);\
|
||||||
printf(" perspectiveScale: %d\n",profile_perspectiveScale); }
|
printf(" perspectiveScale: %d\n",profile_perspectiveScale);\
|
||||||
|
printf(" wrap: %d\n",profile_wrap);\
|
||||||
|
printf(" divRoundDown: %d\n",profile_divRoundDown); }
|
||||||
#else
|
#else
|
||||||
#define profileCall(c)
|
#define profileCall(c)
|
||||||
#endif
|
#endif
|
||||||
|
@ -226,32 +230,38 @@ Unit clamp(Unit value, Unit valueMin, Unit valueMax)
|
||||||
{
|
{
|
||||||
profileCall(clamp);
|
profileCall(clamp);
|
||||||
|
|
||||||
if (value < valueMin)
|
if (value >= valueMin)
|
||||||
return valueMin;
|
{
|
||||||
|
if (value <= valueMax)
|
||||||
if (value > valueMax)
|
|
||||||
return valueMax;
|
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
else
|
||||||
|
return valueMax;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return valueMin;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Unit absVal(Unit value)
|
inline Unit absVal(Unit value)
|
||||||
{
|
{
|
||||||
profileCall(absVal);
|
profileCall(absVal);
|
||||||
|
|
||||||
return value < 0 ? -1 * value : value;
|
return value >= 0 ? value: -1 * value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like mod, but behaves differently for negative values.
|
/// Like mod, but behaves differently for negative values.
|
||||||
inline Unit wrap(Unit value, Unit mod)
|
inline Unit wrap(Unit value, Unit mod)
|
||||||
{
|
{
|
||||||
return value < 0 ? (mod + (value % mod) - 1) : (value % mod);
|
profileCall(wrap);
|
||||||
|
|
||||||
|
return value >= 0 ? (value % mod) : (mod + (value % mod) - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Performs division, rounding down, NOT towards zero.
|
/// Performs division, rounding down, NOT towards zero.
|
||||||
inline Unit divRoundDown(Unit value, Unit divisor)
|
inline Unit divRoundDown(Unit value, Unit divisor)
|
||||||
{
|
{
|
||||||
return value / divisor - ( (value < 0) ? 1 : 0);
|
profileCall(divRoundDown);
|
||||||
|
|
||||||
|
return value / divisor - ((value >= 0) ? 0 : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bhaskara's cosine approximation formula
|
// Bhaskara's cosine approximation formula
|
||||||
|
@ -830,12 +840,10 @@ Unit perspectiveScale(Unit originalSize, Unit distance)
|
||||||
{
|
{
|
||||||
profileCall(perspectiveScale);
|
profileCall(perspectiveScale);
|
||||||
|
|
||||||
if (distance == 0)
|
return distance != 0 ?
|
||||||
return 0;
|
(originalSize * UNITS_PER_SQUARE) /
|
||||||
|
((VERTICAL_FOV * 2 * distance) / UNITS_PER_SQUARE)
|
||||||
return (originalSize * UNITS_PER_SQUARE) /
|
: 0;
|
||||||
((VERTICAL_FOV * 2 * distance) / UNITS_PER_SQUARE);
|
|
||||||
// ^ approximation of tan function
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
28
test.c
28
test.c
|
@ -167,6 +167,31 @@ void benchmarkMapping()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void pixelFunc(PixelInfo p)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void benchmarkRender()
|
||||||
|
{
|
||||||
|
Camera c;
|
||||||
|
|
||||||
|
c.resolution.x = 640;
|
||||||
|
c.resolution.y = 300;
|
||||||
|
c.position.x = 10;
|
||||||
|
c.position.y = 12;
|
||||||
|
c.direction = 100;
|
||||||
|
c.height = 200;
|
||||||
|
c.fovAngle = UNITS_PER_SQUARE / 3;
|
||||||
|
|
||||||
|
RayConstraints constraints;
|
||||||
|
|
||||||
|
constraints.maxHits = 10;
|
||||||
|
constraints.maxSteps = 12;
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
render(c,testArrayFunc,pixelFunc,constraints);
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
printf("Testing raycastlib.\n");
|
printf("Testing raycastlib.\n");
|
||||||
|
@ -233,6 +258,9 @@ int main()
|
||||||
t = measureTime(benchmarkMapping);
|
t = measureTime(benchmarkMapping);
|
||||||
printf("map point to screen 1000000 times: %ld ms\n",t);
|
printf("map point to screen 1000000 times: %ld ms\n",t);
|
||||||
|
|
||||||
|
t = measureTime(benchmarkRender);
|
||||||
|
printf("render 100 times: %ld ms\n",t);
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printProfile();
|
printProfile();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue