Fix 'dtRandomPointInConvexPoly()' returning a garbage value if s == 1.0f. (#271)

This commit is contained in:
Geoyeob Kim 2017-05-15 23:26:35 +09:00 committed by Jakob Botsch Nielsen
parent ef3ea40f7a
commit 46654531e4
4 changed files with 38 additions and 3 deletions

View File

@ -342,8 +342,8 @@ void dtRandomPointInConvexPoly(const float* pts, const int npts, float* areas,
// Find sub triangle weighted by area.
const float thr = s*areasum;
float acc = 0.0f;
float u = 0.0f;
int tri = 0;
float u = 1.0f;
int tri = npts - 1;
for (int i = 2; i < npts; i++) {
const float dacc = areas[i];
if (thr >= acc && thr < (acc+dacc))

View File

@ -45,7 +45,7 @@
// Uncomment this to dump all the requests in stdout.
#define DUMP_REQS
// Returns a random number [0..1)
// Returns a random number [0..1]
static float frand()
{
// return ((float)(rand() & 0xffff)/(float)0xffff);

View File

@ -202,6 +202,8 @@ project "Tests"
"../Tests/*.cpp",
"../Tests/Recast/*.h",
"../Tests/Recast/*.cpp",
"../Tests/Detour/*.h",
"../Tests/Detour/*.cpp",
}
-- project dependencies

View File

@ -0,0 +1,33 @@
#include "catch.hpp"
#include "DetourCommon.h"
TEST_CASE("dtRandomPointInConvexPoly")
{
SECTION("Properly works when the argument 's' is 1.0f")
{
const float pts[] = {
0, 0, 0,
0, 0, 1,
1, 0, 0,
};
const int npts = 3;
float areas[6];
float out[3];
dtRandomPointInConvexPoly(pts, npts, areas, 0.0f, 1.0f, out);
REQUIRE(out[0] == 0);
REQUIRE(out[1] == 0);
REQUIRE(out[2] == 1);
dtRandomPointInConvexPoly(pts, npts, areas, 0.5f, 1.0f, out);
REQUIRE(out[0] == 1.0f / 2);
REQUIRE(out[1] == 0);
REQUIRE(out[2] == 1.0f / 2);
dtRandomPointInConvexPoly(pts, npts, areas, 1.0f, 1.0f, out);
REQUIRE(out[0] == 1);
REQUIRE(out[1] == 0);
REQUIRE(out[2] == 0);
}
}