1
0
Fork 0
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:
Miloslav Číž 2018-09-04 19:32:47 +02:00
parent c819d05b56
commit 2f351a7d66
2 changed files with 53 additions and 17 deletions

View file

@ -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)
{
if (value <= valueMax)
return value;
else
return valueMax;
}
else
return valueMin; return valueMin;
if (value > valueMax)
return valueMax;
return value;
} }
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

30
test.c
View file

@ -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");
@ -232,7 +257,10 @@ 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();