From 46654531e4e5c17d2ebe1933f3e768f908540f03 Mon Sep 17 00:00:00 2001 From: Geoyeob Kim Date: Mon, 15 May 2017 23:26:35 +0900 Subject: [PATCH] Fix 'dtRandomPointInConvexPoly()' returning a garbage value if s == 1.0f. (#271) --- Detour/Source/DetourCommon.cpp | 4 +-- RecastDemo/Source/NavMeshTesterTool.cpp | 2 +- RecastDemo/premake5.lua | 2 ++ Tests/Detour/Tests_Detour.cpp | 33 +++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 Tests/Detour/Tests_Detour.cpp diff --git a/Detour/Source/DetourCommon.cpp b/Detour/Source/DetourCommon.cpp index 26fe65c..41d0d7b 100644 --- a/Detour/Source/DetourCommon.cpp +++ b/Detour/Source/DetourCommon.cpp @@ -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)) diff --git a/RecastDemo/Source/NavMeshTesterTool.cpp b/RecastDemo/Source/NavMeshTesterTool.cpp index 99d6c52..dbd41d2 100644 --- a/RecastDemo/Source/NavMeshTesterTool.cpp +++ b/RecastDemo/Source/NavMeshTesterTool.cpp @@ -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); diff --git a/RecastDemo/premake5.lua b/RecastDemo/premake5.lua index 7f6e00d..04334b4 100644 --- a/RecastDemo/premake5.lua +++ b/RecastDemo/premake5.lua @@ -202,6 +202,8 @@ project "Tests" "../Tests/*.cpp", "../Tests/Recast/*.h", "../Tests/Recast/*.cpp", + "../Tests/Detour/*.h", + "../Tests/Detour/*.cpp", } -- project dependencies diff --git a/Tests/Detour/Tests_Detour.cpp b/Tests/Detour/Tests_Detour.cpp new file mode 100644 index 0000000..dd2a6eb --- /dev/null +++ b/Tests/Detour/Tests_Detour.cpp @@ -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); + } +}