From: Eunki, Hong Date: Wed, 19 Apr 2023 07:38:10 +0000 (+0900) Subject: Let navigation mesh use same type for Index X-Git-Tag: dali_2.2.27~9^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F19%2F291619%2F6;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git Let navigation mesh use same type for Index navigation mesh itself use each Vertex/Edge/Face index as uint16_t, and PathFinder use the face index as uint32_t. To avoid annoying type conversion, Let we make both case use uint16_t, which is define at navigation-mesh.h Change-Id: Ic7346361fa214934f601dd0083a838445c047025 Signed-off-by: Eunki, Hong --- diff --git a/automated-tests/src/dali-scene3d/utc-Dali-NavigationMesh.cpp b/automated-tests/src/dali-scene3d/utc-Dali-NavigationMesh.cpp index f2f6cd3..957332d 100644 --- a/automated-tests/src/dali-scene3d/utc-Dali-NavigationMesh.cpp +++ b/automated-tests/src/dali-scene3d/utc-Dali-NavigationMesh.cpp @@ -15,10 +15,10 @@ * */ -#include "dali-scene3d/public-api/algorithm/navigation-mesh.h" -#include "dali-scene3d/public-api/loader/navigation-mesh-factory.h" #include #include +#include "dali-scene3d/public-api/algorithm/navigation-mesh.h" +#include "dali-scene3d/public-api/loader/navigation-mesh-factory.h" using namespace Dali; using namespace Dali::Scene3D::Algorithm; using namespace Dali::Scene3D::Loader; @@ -41,14 +41,14 @@ struct SysOverride } } - void SetReturnValue( R value, uint32_t n ) + void SetReturnValue(R value, uint32_t n) { if(overrideEnabled) { tet_infoline("Warning! Overriding return value is already enabled! Ignoring!\n"); return; } - result = value; + result = value; overrideCounter = n; overrideEnabled = true; } @@ -70,29 +70,29 @@ struct SysOverride } std::string funcNameStr; - R result{R{}}; - F* func{nullptr}; - uint32_t overrideCounter = 0; - bool overrideEnabled = false; + R result{R{}}; + F* func{nullptr}; + uint32_t overrideCounter = 0; + bool overrideEnabled = false; }; // Override fseek() static thread_local SysOverride call_fseek("fseek"); -extern "C" int fseek (FILE *s, long int o, int w) +extern "C" int fseek(FILE* s, long int o, int w) { - return call_fseek.Invoke( s, o, w ); + return call_fseek.Invoke(s, o, w); } // Override ftell() static thread_local SysOverride call_ftell("ftell"); -extern "C" long int ftell(FILE *s) +extern "C" long int ftell(FILE* s) { - return call_ftell.Invoke( s ); + return call_ftell.Invoke(s); } // Override fread() static thread_local SysOverride call_fread("fread"); -extern "C" size_t fread (void *__restrict p, size_t s, size_t n, FILE *__restrict st) +extern "C" size_t fread(void* __restrict p, size_t s, size_t n, FILE* __restrict st) { return call_fread.Invoke(p, s, n, st); } @@ -176,15 +176,15 @@ int UtcDaliNavigationMeshCreateFromBufferP(void) { tet_infoline("UtcDaliNavigationMeshCreateFromBufferP: Creates navigation mesh using binary buffer"); - auto fin = fopen("resources/navmesh-test.bin", "rb"); - [[maybe_unused]] auto err = fseek(fin, 0, SEEK_END); - auto length = ftell(fin); - fseek( fin, 0, SEEK_SET); + auto fin = fopen("resources/navmesh-test.bin", "rb"); + [[maybe_unused]] auto err = fseek(fin, 0, SEEK_END); + auto length = ftell(fin); + fseek(fin, 0, SEEK_SET); std::vector buffer; buffer.resize(length); - fread( buffer.data(), 1, length, fin ); + fread(buffer.data(), 1, length, fin); fclose(fin); - auto result = NavigationMeshFactory::CreateFromBuffer( buffer ); + auto result = NavigationMeshFactory::CreateFromBuffer(buffer); DALI_TEST_CHECK(result != nullptr); END_TEST; @@ -199,12 +199,12 @@ int UtcDaliNavigationMeshCountersP(void) DALI_TEST_CHECK(result != nullptr); auto vertexCount = result->GetVertexCount(); - auto edgeCount = result->GetEdgeCount(); - auto faceCount = result->GetFaceCount(); + auto edgeCount = result->GetEdgeCount(); + auto faceCount = result->GetFaceCount(); - DALI_TEST_EQUALS( vertexCount, 132, TEST_LOCATION ); - DALI_TEST_EQUALS( edgeCount, 300, TEST_LOCATION ); - DALI_TEST_EQUALS( faceCount, 165, TEST_LOCATION ); + DALI_TEST_EQUALS(vertexCount, 132, TEST_LOCATION); + DALI_TEST_EQUALS(edgeCount, 300, TEST_LOCATION); + DALI_TEST_EQUALS(faceCount, 165, TEST_LOCATION); END_TEST; } @@ -219,7 +219,7 @@ int UtcDaliNavigationMeshGetVertexP(void) auto vertexCount = navmesh->GetVertexCount(); - DALI_TEST_EQUALS( vertexCount, 132, TEST_LOCATION ); + DALI_TEST_EQUALS(vertexCount, 132, TEST_LOCATION); // List of coords, must be verified with Blender exporter // clang-format off @@ -235,12 +235,12 @@ int UtcDaliNavigationMeshGetVertexP(void) // clang-format on auto j = 0; - for( auto i = 0u; i < 132; i+= 10, j+= 3) + for(auto i = 0u; i < 132; i += 10, j += 3) { const auto* vertex = navmesh->GetVertex(i); - Vector3 v0(vertex->co); - Vector3 v1(vertexData[j], vertexData[j+1], vertexData[j+2]); - DALI_TEST_EQUALS( v0, v1, TEST_LOCATION); + Vector3 v0(vertex->coordinates); + Vector3 v1(vertexData[j], vertexData[j + 1], vertexData[j + 2]); + DALI_TEST_EQUALS(v0, v1, TEST_LOCATION); } END_TEST; @@ -256,11 +256,11 @@ int UtcDaliNavigationMeshGetEdgeP(void) auto edgeCount = navmesh->GetEdgeCount(); - DALI_TEST_EQUALS( edgeCount, 300, TEST_LOCATION ); + DALI_TEST_EQUALS(edgeCount, 300, TEST_LOCATION); // List of coords, must be verified with Blender exporter // clang-format off - std::vector edgeData = { + std::vector edgeData = { 2, 65535, 8, 1, 8, 109, 124, 108, 10, 158, 32, 35, @@ -274,18 +274,18 @@ int UtcDaliNavigationMeshGetEdgeP(void) }; // clang-format on auto j = 0; - for( auto i = 0u; i < 300; i+= 30, j+= 4) + for(auto i = 0u; i < 300; i += 30, j += 4) { const auto* edge = navmesh->GetEdge(i); - auto e0 = edge->face[0]; - auto e1 = edge->face[1]; - auto v0 = edge->vertex[0]; - auto v1 = edge->vertex[1]; - - DALI_TEST_EQUALS(e0, edgeData[j+0], TEST_LOCATION); - DALI_TEST_EQUALS(e1, edgeData[j+1], TEST_LOCATION); - DALI_TEST_EQUALS(v0, edgeData[j+2], TEST_LOCATION); - DALI_TEST_EQUALS(v1, edgeData[j+3], TEST_LOCATION); + auto e0 = edge->face[0]; + auto e1 = edge->face[1]; + auto v0 = edge->vertex[0]; + auto v1 = edge->vertex[1]; + + DALI_TEST_EQUALS(e0, edgeData[j + 0], TEST_LOCATION); + DALI_TEST_EQUALS(e1, edgeData[j + 1], TEST_LOCATION); + DALI_TEST_EQUALS(v0, edgeData[j + 2], TEST_LOCATION); + DALI_TEST_EQUALS(v1, edgeData[j + 3], TEST_LOCATION); } END_TEST; @@ -301,7 +301,7 @@ int UtcDaliNavigationMeshGetFaceP(void) auto faceCount = navmesh->GetFaceCount(); - DALI_TEST_EQUALS( faceCount, 165, TEST_LOCATION ); + DALI_TEST_EQUALS(faceCount, 165, TEST_LOCATION); // List of coords, must be verified with Blender exporter // clang-format off @@ -321,31 +321,30 @@ int UtcDaliNavigationMeshGetFaceP(void) }; // clang-format on auto j = 0; - for( auto i = 0u; i < 165; i+= 16, j++) + for(auto i = 0u; i < 165; i += 16, j++) { const auto* face = navmesh->GetFace(i); - Vector3 n0(face->normal); - Vector3 c0(face->center); + Vector3 n0(face->normal); + Vector3 c0(face->center); Vector3 n1(faceData[j].normal); Vector3 c1(faceData[j].center); - DALI_TEST_EQUALS( n0, n1, TEST_LOCATION); - DALI_TEST_EQUALS( c0, c1, TEST_LOCATION); + DALI_TEST_EQUALS(n0, n1, TEST_LOCATION); + DALI_TEST_EQUALS(c0, c1, TEST_LOCATION); - DALI_TEST_EQUALS( faceData[j].vertex[0], face->vertex[0], TEST_LOCATION); - DALI_TEST_EQUALS( faceData[j].vertex[1], face->vertex[1], TEST_LOCATION); - DALI_TEST_EQUALS( faceData[j].vertex[2], face->vertex[2], TEST_LOCATION); + DALI_TEST_EQUALS(faceData[j].vertex[0], face->vertex[0], TEST_LOCATION); + DALI_TEST_EQUALS(faceData[j].vertex[1], face->vertex[1], TEST_LOCATION); + DALI_TEST_EQUALS(faceData[j].vertex[2], face->vertex[2], TEST_LOCATION); - DALI_TEST_EQUALS( faceData[j].edge[0], face->edge[0], TEST_LOCATION); - DALI_TEST_EQUALS( faceData[j].edge[1], face->edge[1], TEST_LOCATION); - DALI_TEST_EQUALS( faceData[j].edge[2], face->edge[2], TEST_LOCATION); + DALI_TEST_EQUALS(faceData[j].edge[0], face->edge[0], TEST_LOCATION); + DALI_TEST_EQUALS(faceData[j].edge[1], face->edge[1], TEST_LOCATION); + DALI_TEST_EQUALS(faceData[j].edge[2], face->edge[2], TEST_LOCATION); } END_TEST; } - int UtcDaliNavigationGetGravityP(void) { tet_infoline("UtcDaliNavigationGetGravityP: Tests gravity vector"); @@ -353,9 +352,9 @@ int UtcDaliNavigationGetGravityP(void) auto gravity = navmesh->GetGravityVector(); // navmesh-test.bin is exported in Blender and the default gravity is Z = -1 - Dali::Vector3 expectedGravity( 0.0f, 0.0f, -1.0f ); + Dali::Vector3 expectedGravity(0.0f, 0.0f, -1.0f); - DALI_TEST_EQUALS( gravity, expectedGravity, TEST_LOCATION); + DALI_TEST_EQUALS(gravity, expectedGravity, TEST_LOCATION); END_TEST; } @@ -368,9 +367,9 @@ int UtcDaliNavigationSetTransformP(void) Matrix matrix; matrix.SetIdentity(); - Quaternion q = Quaternion( Radian(Degree(-90)), Vector3(1.0, 0.0, 0.0)); - Matrix newMatrix; - matrix.Multiply( newMatrix, matrix, q); // Rotate matrix along X-axis + Quaternion q = Quaternion(Radian(Degree(-90)), Vector3(1.0, 0.0, 0.0)); + Matrix newMatrix; + matrix.Multiply(newMatrix, matrix, q); // Rotate matrix along X-axis navmesh->SetSceneTransform(newMatrix); @@ -384,12 +383,12 @@ int UtcDaliNavigationSetTransformP(void) auto gravityVector = navmesh->GetGravityVector(); // 'point' should be turned into the gravity vector after transforming into the local space - DALI_TEST_EQUALS( navMeshLocalSpace, gravityVector, TEST_LOCATION); + DALI_TEST_EQUALS(navMeshLocalSpace, gravityVector, TEST_LOCATION); navMeshParentSpace = navmesh->PointLocalToScene(gravityVector); // The gravity should be transformed back into point - DALI_TEST_EQUALS( navMeshParentSpace, point, TEST_LOCATION); + DALI_TEST_EQUALS(navMeshParentSpace, point, TEST_LOCATION); END_TEST; } @@ -403,21 +402,21 @@ int UtcDaliNavigationFindFloor0P(void) // All calculations in the navmesh local space navmesh->SetSceneTransform(Matrix(Matrix::IDENTITY)); - std::vector inPositions; - std::vector expectedPositions; - std::vector expectedFaceIndex; - std::vector expectedResult; + std::vector inPositions; + std::vector expectedPositions; + std::vector expectedFaceIndex; + std::vector expectedResult; // Lift slightly over the floor level auto upFromGravity = navmesh->GetGravityVector() * (0.05f); auto size = navmesh->GetFaceCount(); - for( auto i = 0u; i < size; ++i) + for(auto i = 0u; i < size; ++i) { const auto* face = navmesh->GetFace(i); Vector3(face->center); inPositions.emplace_back(Vector3(face->center)); - inPositions.back() -= Vector3( upFromGravity ); + inPositions.back() -= Vector3(upFromGravity); expectedResult.emplace_back(true); expectedPositions.emplace_back(face->center); @@ -428,29 +427,29 @@ int UtcDaliNavigationFindFloor0P(void) // Middle 'circle' of scene inPositions.emplace_back(Vector3(-0.048838f, 0.039285f, 0.013085f)); expectedPositions.emplace_back(); - expectedFaceIndex.emplace_back( NavigationMesh::NULL_FACE ); + expectedFaceIndex.emplace_back(NavigationMesh::NULL_FACE); expectedResult.emplace_back(false); // Triangle under stairs inPositions.emplace_back(Vector3(0.44365f, -1.787f, 0.13085f)); expectedPositions.emplace_back(); - expectedFaceIndex.emplace_back( NavigationMesh::NULL_FACE ); + expectedFaceIndex.emplace_back(NavigationMesh::NULL_FACE); expectedResult.emplace_back(false); // Outside area inPositions.emplace_back(Vector3(0.77197f, -3.8596f, 0.13085f)); expectedPositions.emplace_back(); - expectedFaceIndex.emplace_back( NavigationMesh::NULL_FACE ); + expectedFaceIndex.emplace_back(NavigationMesh::NULL_FACE); expectedResult.emplace_back(false); - for( auto i = 0u; i < inPositions.size(); ++i ) + for(auto i = 0u; i < inPositions.size(); ++i) { - Vector3 outPosition; - uint32_t faceIndex {NavigationMesh::NULL_FACE}; - auto result = navmesh->FindFloor(inPositions[i], outPosition, faceIndex); - DALI_TEST_EQUALS( bool(result), bool(expectedResult[i]), TEST_LOCATION); - DALI_TEST_EQUALS( faceIndex, expectedFaceIndex[i], TEST_LOCATION); - DALI_TEST_EQUALS( outPosition, expectedPositions[i], TEST_LOCATION); + Vector3 outPosition; + FaceIndex faceIndex{NavigationMesh::NULL_FACE}; + auto result = navmesh->FindFloor(inPositions[i], outPosition, faceIndex); + DALI_TEST_EQUALS(bool(result), bool(expectedResult[i]), TEST_LOCATION); + DALI_TEST_EQUALS(faceIndex, expectedFaceIndex[i], TEST_LOCATION); + DALI_TEST_EQUALS(outPosition, expectedPositions[i], TEST_LOCATION); } END_TEST; @@ -466,49 +465,49 @@ int UtcDaliNavigationFindFloorForFace1P(void) navmesh->SetSceneTransform(Matrix(Matrix::IDENTITY)); { - auto faceIndex = 0u; - auto position = Vector3( 0, 0, 0); + auto faceIndex = FaceIndex(0u); + auto position = Vector3(0, 0, 0); auto dontCheckNeighbours = true; - auto outPosition = Vector3(); - auto expectedPosition = Vector3(); - bool result = false; + auto outPosition = Vector3(); + auto expectedPosition = Vector3(); + bool result = false; { // test 1. position lies within selected triangle - faceIndex = 137; - position = Vector3(-6.0767f, -1.7268f, 4.287f); - expectedPosition = Vector3(-6.0767f, -1.7268f, 3.83f); + faceIndex = 137; + position = Vector3(-6.0767f, -1.7268f, 4.287f); + expectedPosition = Vector3(-6.0767f, -1.7268f, 3.83f); dontCheckNeighbours = true; - result = navmesh->FindFloorForFace(position, faceIndex, dontCheckNeighbours, outPosition); + result = navmesh->FindFloorForFace(position, faceIndex, dontCheckNeighbours, outPosition); - DALI_TEST_EQUALS( result, true, TEST_LOCATION); - DALI_TEST_EQUALS( outPosition, expectedPosition, TEST_LOCATION); + DALI_TEST_EQUALS(result, true, TEST_LOCATION); + DALI_TEST_EQUALS(outPosition, expectedPosition, TEST_LOCATION); } { // test 2. position lies outside selected triangle, not checking neighbours - faceIndex = 137; - position = Vector3(-5.3073f, -0.6023f, 4.287f); - expectedPosition = Vector3::ZERO; - outPosition = Vector3::ZERO; + faceIndex = 137; + position = Vector3(-5.3073f, -0.6023f, 4.287f); + expectedPosition = Vector3::ZERO; + outPosition = Vector3::ZERO; dontCheckNeighbours = true; - result = navmesh->FindFloorForFace(position, faceIndex, dontCheckNeighbours, outPosition); + result = navmesh->FindFloorForFace(position, faceIndex, dontCheckNeighbours, outPosition); - DALI_TEST_EQUALS( result, false, TEST_LOCATION); - DALI_TEST_EQUALS( outPosition, expectedPosition, TEST_LOCATION); + DALI_TEST_EQUALS(result, false, TEST_LOCATION); + DALI_TEST_EQUALS(outPosition, expectedPosition, TEST_LOCATION); } { // test 3. position lies outside selected triangle but this time checking neighbours - faceIndex = 137; - position = Vector3(-5.3073f, -0.6023f, 4.287f); - expectedPosition = Vector3(-5.3073, -0.6023, 3.83); - outPosition = Vector3::ZERO; + faceIndex = 137; + position = Vector3(-5.3073f, -0.6023f, 4.287f); + expectedPosition = Vector3(-5.3073, -0.6023, 3.83); + outPosition = Vector3::ZERO; dontCheckNeighbours = false; - result = navmesh->FindFloorForFace(position, faceIndex, dontCheckNeighbours, outPosition); + result = navmesh->FindFloorForFace(position, faceIndex, dontCheckNeighbours, outPosition); - DALI_TEST_EQUALS( result, true, TEST_LOCATION); - DALI_TEST_EQUALS( outPosition, expectedPosition, TEST_LOCATION); + DALI_TEST_EQUALS(result, true, TEST_LOCATION); + DALI_TEST_EQUALS(outPosition, expectedPosition, TEST_LOCATION); } } @@ -525,26 +524,25 @@ int UtcDaliNavigationFindFloorForFace2P(void) navmesh->SetSceneTransform(Matrix(Matrix::IDENTITY)); { - [[maybe_unused]] auto faceIndex = 0u; - auto position = Vector3( 0, 0, 0); - auto dontCheckNeighbours = true; - auto outPosition = Vector3(); - auto expectedPosition = Vector3(); - bool result = false; + [[maybe_unused]] auto faceIndex = 0u; + auto position = Vector3(0, 0, 0); + auto dontCheckNeighbours = true; + auto outPosition = Vector3(); + auto expectedPosition = Vector3(); + bool result = false; { // test 4. position lies within a triangle but this time full search forced, // the navmesh must have no previous searches (mCurrentFace shouldn't be set) - faceIndex = 137; - position = Vector3(-6.0767f, -1.7268f, 4.287f); - expectedPosition = Vector3(-6.0767f, -1.7268f, 3.83f); + faceIndex = 137; + position = Vector3(-6.0767f, -1.7268f, 4.287f); + expectedPosition = Vector3(-6.0767f, -1.7268f, 3.83f); dontCheckNeighbours = true; - result = navmesh->FindFloorForFace(position, NavigationMesh::NULL_FACE, dontCheckNeighbours, outPosition); + result = navmesh->FindFloorForFace(position, NavigationMesh::NULL_FACE, dontCheckNeighbours, outPosition); - DALI_TEST_EQUALS( result, true, TEST_LOCATION); - DALI_TEST_EQUALS( outPosition, expectedPosition, TEST_LOCATION); + DALI_TEST_EQUALS(result, true, TEST_LOCATION); + DALI_TEST_EQUALS(outPosition, expectedPosition, TEST_LOCATION); } - } END_TEST; diff --git a/automated-tests/src/dali-scene3d/utc-Dali-PathFinding.cpp b/automated-tests/src/dali-scene3d/utc-Dali-PathFinding.cpp index 8866f36..b0cfeb4 100644 --- a/automated-tests/src/dali-scene3d/utc-Dali-PathFinding.cpp +++ b/automated-tests/src/dali-scene3d/utc-Dali-PathFinding.cpp @@ -15,16 +15,16 @@ * */ +#include #include "dali-scene3d/public-api/algorithm/navigation-mesh.h" #include "dali-scene3d/public-api/algorithm/path-finder.h" #include "dali-scene3d/public-api/loader/navigation-mesh-factory.h" -#include using namespace Dali; using namespace Dali::Scene3D::Algorithm; using namespace Dali::Scene3D::Loader; -bool CompareResults( const std::vector& nodes, const WayPointList& waypoints ) +bool CompareResults(const std::vector& nodes, const WayPointList& waypoints) { if(nodes.size() != waypoints.size()) { @@ -41,10 +41,10 @@ bool CompareResults( const std::vector& nodes, const WayPointList& way oss << waypoint.GetNavigationMeshFaceIndex() << ", "; } oss << "]\n"; - tet_printf("%s\n",oss.str().c_str()); + tet_printf("%s\n", oss.str().c_str()); return false; } - for(auto i = 0u; i < nodes.size(); ++i ) + for(auto i = 0u; i < nodes.size(); ++i) { if(nodes[i] != waypoints[i].GetNavigationMeshFaceIndex()) { @@ -61,7 +61,7 @@ bool CompareResults( const std::vector& nodes, const WayPointList& way oss << waypoint.GetNavigationMeshFaceIndex() << ", "; } oss << "]\n"; - tet_printf("%s\n",oss.str().c_str()); + tet_printf("%s\n", oss.str().c_str()); return false; } } @@ -70,9 +70,9 @@ bool CompareResults( const std::vector& nodes, const WayPointList& way int UtcDaliPathFinderNewP(void) { - auto navmesh = NavigationMeshFactory::CreateFromFile( "resources/navmesh-test.bin"); + auto navmesh = NavigationMeshFactory::CreateFromFile("resources/navmesh-test.bin"); - auto pathfinder = PathFinder::New( *navmesh, PathFinderAlgorithm::DEFAULT ); + auto pathfinder = PathFinder::New(*navmesh, PathFinderAlgorithm::DEFAULT); DALI_TEST_CHECK(navmesh); DALI_TEST_CHECK(pathfinder); @@ -82,9 +82,9 @@ int UtcDaliPathFinderNewP(void) int UtcDaliPathFinderNewFail(void) { - auto navmesh = NavigationMeshFactory::CreateFromFile( "resources/navmesh-test.bin"); + auto navmesh = NavigationMeshFactory::CreateFromFile("resources/navmesh-test.bin"); - auto pathfinder = PathFinder::New( *navmesh, static_cast(-1) ); + auto pathfinder = PathFinder::New(*navmesh, static_cast(-1)); DALI_TEST_CHECK(navmesh); DALI_TEST_CHECK(!pathfinder); @@ -92,21 +92,21 @@ int UtcDaliPathFinderNewFail(void) END_TEST; } -void printWaypointForPython( WayPointList& waypoints) +void printWaypointForPython(WayPointList& waypoints) { - tet_printf( "size: %d\n", waypoints.size()); - tet_printf( "["); + tet_printf("size: %d\n", waypoints.size()); + tet_printf("["); for(auto& wp : waypoints) { auto index = wp.GetNavigationMeshFaceIndex(); tet_printf("%d, ", index); } - tet_printf( "]"); + tet_printf("]"); } int UtcDaliPathFinderFindShortestPath0(void) { - auto navmesh = NavigationMeshFactory::CreateFromFile( "resources/navmesh-test.bin"); + auto navmesh = NavigationMeshFactory::CreateFromFile("resources/navmesh-test.bin"); std::vector testAlgorithms = { PathFinderAlgorithm::DJIKSTRA_SHORTEST_PATH, @@ -116,7 +116,7 @@ int UtcDaliPathFinderFindShortestPath0(void) for(const auto& algorithm : testAlgorithms) { tet_printf("Test algorithm type : %d\n", static_cast(algorithm)); - auto pathfinder = PathFinder::New( *navmesh, algorithm ); + auto pathfinder = PathFinder::New(*navmesh, algorithm); DALI_TEST_CHECK(navmesh); DALI_TEST_CHECK(pathfinder); @@ -126,7 +126,7 @@ int UtcDaliPathFinderFindShortestPath0(void) DALI_TEST_NOT_EQUALS(int(waypoints.size()), 0, 0, TEST_LOCATION); // Results are verified in the Blender - std::vector expectedResults = + std::vector expectedResults = {18, 97, 106, 82, 50, 139}; DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION); @@ -142,11 +142,10 @@ int UtcDaliPathFinderFindShortestPath0(void) //printWaypointForPython(waypoints); // Results are verified in the Blender - std::vector expectedResults = + std::vector expectedResults = {18, 97, 106, 82, 50, 6, 89, 33, 157}; DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION); - } } @@ -155,9 +154,9 @@ int UtcDaliPathFinderFindShortestPath0(void) int UtcDaliPathFinderFindShortestPath1(void) { - auto navmesh = NavigationMeshFactory::CreateFromFile( "resources/navmesh-test.bin"); + auto navmesh = NavigationMeshFactory::CreateFromFile("resources/navmesh-test.bin"); // All coordinates in navmesh local space - navmesh->SetSceneTransform( Matrix(Matrix::IDENTITY)); + navmesh->SetSceneTransform(Matrix(Matrix::IDENTITY)); std::vector testAlgorithms = { PathFinderAlgorithm::DJIKSTRA_SHORTEST_PATH, @@ -168,29 +167,29 @@ int UtcDaliPathFinderFindShortestPath1(void) for(const auto& algorithm : testAlgorithms) { tet_printf("Test algorithm type : %d\n", static_cast(algorithm)); - auto pathfinder = PathFinder::New( *navmesh, algorithm ); + auto pathfinder = PathFinder::New(*navmesh, algorithm); DALI_TEST_CHECK(navmesh); DALI_TEST_CHECK(pathfinder); { Vector3 from(-6.0767, -1.7268, 0.1438); // ground floor - Vector3 to(-6.0767, -1.7268, 4.287); // first floor + Vector3 to(-6.0767, -1.7268, 4.287); // first floor auto waypoints = pathfinder->FindPath(from, to); DALI_TEST_NOT_EQUALS(int(waypoints.size()), 0, 0, TEST_LOCATION); // Results are verified in the Blender - std::vector expectedResults = - {154, 58, 85, 106, 128, 132, 137}; + std::vector expectedResults = + {154, 58, 85, 106, 128, 132, 137}; DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION); // Verify last and first points by finding floor points { - Vector3 verifyPos = Vector3::ZERO; - uint32_t verifyIndex = NavigationMesh::NULL_FACE; - auto result = navmesh->FindFloor(from, verifyPos, verifyIndex); + Vector3 verifyPos = Vector3::ZERO; + FaceIndex verifyIndex = NavigationMesh::NULL_FACE; + auto result = navmesh->FindFloor(from, verifyPos, verifyIndex); DALI_TEST_EQUALS(result, true, TEST_LOCATION); DALI_TEST_EQUALS(verifyPos, waypoints[0].GetScenePosition(), TEST_LOCATION); @@ -202,9 +201,9 @@ int UtcDaliPathFinderFindShortestPath1(void) } { - Vector3 verifyPos = Vector3::ZERO; - uint32_t verifyIndex = NavigationMesh::NULL_FACE; - auto result = navmesh->FindFloor(to, verifyPos, verifyIndex); + Vector3 verifyPos = Vector3::ZERO; + FaceIndex verifyIndex = NavigationMesh::NULL_FACE; + auto result = navmesh->FindFloor(to, verifyPos, verifyIndex); DALI_TEST_EQUALS(result, true, TEST_LOCATION); DALI_TEST_EQUALS(verifyPos, waypoints.back().GetScenePosition(), TEST_LOCATION); diff --git a/dali-scene3d/internal/algorithm/navigation-mesh-impl.cpp b/dali-scene3d/internal/algorithm/navigation-mesh-impl.cpp index 2a8d9f4..61485fb 100644 --- a/dali-scene3d/internal/algorithm/navigation-mesh-impl.cpp +++ b/dali-scene3d/internal/algorithm/navigation-mesh-impl.cpp @@ -111,7 +111,7 @@ NavigationMesh::NavigationMesh(const std::vector& buffer) return mHeader.vertexCount; } -bool NavigationMesh::FindFloorForFace(const Dali::Vector3& position, uint32_t faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition) +bool NavigationMesh::FindFloorForFace(const Dali::Vector3& position, FaceIndex faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition) { if(faceIndex == ::Dali::Scene3D::Algorithm::NavigationMesh::NULL_FACE) { @@ -130,7 +130,7 @@ bool NavigationMesh::FindFloorForFace(const Dali::Vector3& position, uint32_t fa // Ray direction matches gravity direction ray.direction = Vector3(mHeader.gravityVector); - IntersectResult result = NavigationRayFaceIntersection(ray, *GetFace(uint16_t(faceIndex))); + IntersectResult result = NavigationRayFaceIntersection(ray, *GetFace(faceIndex)); if(result.result) { outPosition = PointLocalToScene(result.point); @@ -144,35 +144,35 @@ bool NavigationMesh::FindFloorForFace(const Dali::Vector3& position, uint32_t fa } // Test neighbouring by edges - const auto& poly = GetFace(uint16_t(faceIndex)); + const auto& poly = GetFace(faceIndex); // collect faces sharing edges - std::vector faces; + std::vector neighbourFaces; - for(unsigned short i : poly->edge) + for(auto edgeIndex : poly->edge) { - const auto& edge = *GetEdge(i); + const auto& edge = *GetEdge(edgeIndex); // store faces - for(unsigned short j : edge.face) + for(auto edgeFaceIndex : edge.face) { - if(j != ::Dali::Scene3D::Algorithm::NavigationMesh::NULL_FACE && j != faceIndex) + if(edgeFaceIndex != ::Dali::Scene3D::Algorithm::NavigationMesh::NULL_FACE && edgeFaceIndex != faceIndex) { - faces.emplace_back(j); + neighbourFaces.emplace_back(edgeFaceIndex); } } } - if(faces.empty()) + if(neighbourFaces.empty()) { return false; } - for(const auto& p : faces) + for(const auto& neighbourFaceIndex : neighbourFaces) { - if(FindFloorForFace(position, p, true, outPosition)) + if(FindFloorForFace(position, neighbourFaceIndex, true, outPosition)) { - mCurrentFace = p; + mCurrentFace = neighbourFaceIndex; return true; } } @@ -188,11 +188,11 @@ bool NavigationMesh::FindFloorForFace(const Dali::Vector3& position, uint32_t fa */ bool NavigationMesh::FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition) { - uint32_t polyIndex = 0; - return FindFloor(position, outPosition, polyIndex); + FaceIndex faceIndex = 0u; + return FindFloor(position, outPosition, faceIndex); } -bool NavigationMesh::FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, uint32_t& faceIndex) +bool NavigationMesh::FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, FaceIndex& faceIndex) { [[maybe_unused]] auto newPos = PointSceneToLocal(Dali::Vector3(position)); @@ -205,12 +205,12 @@ bool NavigationMesh::FindFloor(const Dali::Vector3& position, Dali::Vector3& out const auto POLY_COUNT = GetFaceCount(); std::vector results; - for(auto i = 0u; i < POLY_COUNT; ++i) + for(auto faceIndex = 0u; faceIndex < POLY_COUNT; ++faceIndex) { - auto result = NavigationRayFaceIntersection(ray, *GetFace(i)); + auto result = NavigationRayFaceIntersection(ray, *GetFace(faceIndex)); if(result.result) { - result.faceIndex = i; + result.faceIndex = faceIndex; results.emplace_back(result); } } @@ -230,25 +230,25 @@ bool NavigationMesh::FindFloor(const Dali::Vector3& position, Dali::Vector3& out return true; } -const Poly* NavigationMesh::GetFace(int index) const +const Poly* NavigationMesh::GetFace(FaceIndex index) const { auto* basePtr = reinterpret_cast(mBuffer.data() + mHeader.dataOffset + mHeader.polyDataOffset); return &basePtr[index]; } -const Edge* NavigationMesh::GetEdge(int index) const +const Edge* NavigationMesh::GetEdge(EdgeIndex index) const { auto* basePtr = reinterpret_cast(mBuffer.data() + mHeader.dataOffset + mHeader.edgeDataOffset); return &basePtr[index]; } -const Vertex* NavigationMesh::GetVertex(int index) const +const Vertex* NavigationMesh::GetVertex(VertexIndex index) const { auto* basePtr = reinterpret_cast(mBuffer.data() + mHeader.dataOffset + mHeader.vertexDataOffset); return &basePtr[index]; } -NavigationMesh::IntersectResult NavigationMesh::NavigationRayFaceIntersection(NavigationRay& ray, const Face& face) +NavigationMesh::IntersectResult NavigationMesh::NavigationRayFaceIntersection(NavigationRay& ray, const Face& face) const { auto vi0 = *GetVertex(face.vertex[0]); auto vi1 = *GetVertex(face.vertex[1]); @@ -266,7 +266,7 @@ void NavigationMesh::SetTransform(const Dali::Matrix& transform) transform.InvertTransform(mTransformInverse); } -Dali::Vector3 NavigationMesh::PointSceneToLocal(const Dali::Vector3& point) +Dali::Vector3 NavigationMesh::PointSceneToLocal(const Dali::Vector3& point) const { // Transform point into navmesh space Dali::Vector4 invNewPos = mTransformInverse * Dali::Vector4(point.x, -point.y, point.z, 1.0f); @@ -275,7 +275,7 @@ Dali::Vector3 NavigationMesh::PointSceneToLocal(const Dali::Vector3& point) return Dali::Vector3(invNewPos.AsFloat()); } -Dali::Vector3 NavigationMesh::PointLocalToScene(const Dali::Vector3& point) +Dali::Vector3 NavigationMesh::PointLocalToScene(const Dali::Vector3& point) const { // Transform point into scene transform space Dali::Vector4 newPos = mTransform * Dali::Vector4(point.x, -point.y, point.z, 1.0f); diff --git a/dali-scene3d/internal/algorithm/navigation-mesh-impl.h b/dali-scene3d/internal/algorithm/navigation-mesh-impl.h index f61a922..f1db5db 100644 --- a/dali-scene3d/internal/algorithm/navigation-mesh-impl.h +++ b/dali-scene3d/internal/algorithm/navigation-mesh-impl.h @@ -42,6 +42,11 @@ namespace Dali::Scene3D::Internal::Algorithm { class NavigationRay; +// Make each to change each index value's type here. +using VertexIndex = Dali::Scene3D::Algorithm::VertexIndex; +using EdgeIndex = Dali::Scene3D::Algorithm::EdgeIndex; +using FaceIndex = Dali::Scene3D::Algorithm::FaceIndex; + /** * @class NavigationMesh */ @@ -73,7 +78,7 @@ public: { Dali::Vector3 point; float distance; - uint16_t faceIndex; + FaceIndex faceIndex; bool result; }; @@ -93,15 +98,9 @@ public: [[nodiscard]] uint32_t GetVertexCount() const; /** - * Looks for floor only within the face - * @param[in] position Position to be projected onto the face - * @param[in] faceIndex Face index - * @param[in] dontCheckNeighbours states whether to traverse onto neighbouring faces - * @param[out] outPosition Output position - * - * @return true if success + * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::FindFloorForFace() */ - bool FindFloorForFace(const Dali::Vector3& position, uint32_t faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition); + bool FindFloorForFace(const Dali::Vector3& position, FaceIndex faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition); /** * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::FindFloor() @@ -111,22 +110,22 @@ public: /** * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::FindFloor() */ - bool FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, uint32_t& faceIndex); + bool FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, FaceIndex& faceIndex); /** * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetFace() */ - [[nodiscard]] const Face* GetFace(int index) const; + [[nodiscard]] const Face* GetFace(FaceIndex index) const; /** * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetEdge() */ - [[nodiscard]] const Edge* GetEdge(int index) const; + [[nodiscard]] const Edge* GetEdge(EdgeIndex index) const; /** * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetVertex() */ - [[nodiscard]] const Vertex* GetVertex(int index) const; + [[nodiscard]] const Vertex* GetVertex(VertexIndex index) const; /** * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::SetSceneTransform() @@ -136,17 +135,17 @@ public: /** * Tests intersection between navigation ray and face */ - IntersectResult NavigationRayFaceIntersection(NavigationRay& ray, const Face& face); + IntersectResult NavigationRayFaceIntersection(NavigationRay& ray, const Face& face) const; /** * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::PointSceneToLocal() */ - Dali::Vector3 PointSceneToLocal(const Dali::Vector3& point); + Dali::Vector3 PointSceneToLocal(const Dali::Vector3& point) const; /** * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::PointLocalToScene() */ - Dali::Vector3 PointLocalToScene(const Dali::Vector3& point); + Dali::Vector3 PointLocalToScene(const Dali::Vector3& point) const; /** * @copydoc Dali::Scene3D::Algorithm::NavigationMesh::GetGravityVector() @@ -156,7 +155,7 @@ public: private: std::vector mBuffer; //< Data buffer NavigationMeshHeader_V10 mHeader; //< Navigation mesh header - uint16_t mCurrentFace; //< Current face (last floor position) + FaceIndex mCurrentFace; //< Current face (last floor position) Dali::Matrix mTransform; //< Transform matrix Dali::Matrix mTransformInverse; //< Inverse of the transform matrix }; diff --git a/dali-scene3d/internal/algorithm/path-finder-djikstra.cpp b/dali-scene3d/internal/algorithm/path-finder-djikstra.cpp index 186acf2..726c1fb 100644 --- a/dali-scene3d/internal/algorithm/path-finder-djikstra.cpp +++ b/dali-scene3d/internal/algorithm/path-finder-djikstra.cpp @@ -40,7 +40,7 @@ PathFinderAlgorithmDjikstra::~PathFinderAlgorithmDjikstra() = default; Scene3D::Algorithm::WayPointList PathFinderAlgorithmDjikstra::FindPath(const Dali::Vector3& positionFrom, const Dali::Vector3& positionTo) { Dali::Vector3 outPosFrom; - uint32_t polyIndexFrom; + FaceIndex polyIndexFrom; auto result = mNavigationMesh->FindFloor(positionFrom, outPosFrom, polyIndexFrom); Scene3D::Algorithm::WayPointList waypoints; @@ -48,7 +48,7 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmDjikstra::FindPath(const Dal if(result) { Dali::Vector3 outPosTo; - uint32_t polyIndexTo; + FaceIndex polyIndexTo; result = mNavigationMesh->FindFloor(positionTo, outPosTo, polyIndexTo); if(result) @@ -75,11 +75,11 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmDjikstra::FindPath(const Dal return waypoints; } -Scene3D::Algorithm::WayPointList PathFinderAlgorithmDjikstra::FindPath(uint32_t sourcePolyIndex, uint32_t targetPolyIndex) +Scene3D::Algorithm::WayPointList PathFinderAlgorithmDjikstra::FindPath(FaceIndex sourcePolyIndex, FaceIndex targetPolyIndex) { - auto nodeCount = uint32_t(mNodes.size()); - std::vector dist; - std::vector prev; + auto nodeCount = uint32_t(mNodes.size()); + std::vector dist; + std::vector prev; dist.resize(mNodes.size()); prev.resize(mNodes.size()); @@ -156,8 +156,8 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmDjikstra::FindPath(uint32_t } while(removeCount != nodeCount); // Compute distances for each node back to the source - auto u = targetPolyIndex; - std::list q; + auto u = targetPolyIndex; + std::list q; if(prev[u] != Scene3D::Algorithm::NavigationMesh::NULL_FACE || u == sourcePolyIndex) { while(u != Scene3D::Algorithm::NavigationMesh::NULL_FACE) @@ -208,6 +208,7 @@ void PathFinderAlgorithmDjikstra::PrepareData() mNodes.resize(faceCount); // for each face build the list + // TODO : Currently, we are assume that FaceNodeIndex is matched with FaceIndex 1:1. This might be changed in future. for(auto i = 0u; i < faceCount; ++i) { auto& node = mNodes[i]; diff --git a/dali-scene3d/internal/algorithm/path-finder-djikstra.h b/dali-scene3d/internal/algorithm/path-finder-djikstra.h index 21bd316..08a6cee 100644 --- a/dali-scene3d/internal/algorithm/path-finder-djikstra.h +++ b/dali-scene3d/internal/algorithm/path-finder-djikstra.h @@ -54,11 +54,11 @@ public: * @param[in] polyIndexTo Index of end polygon * @return List of waypoints for path */ - Scene3D::Algorithm::WayPointList FindPath(uint32_t sourcePolyIndex, uint32_t targetPolyIndex) override; + Scene3D::Algorithm::WayPointList FindPath(FaceIndex sourcePolyIndex, FaceIndex targetPolyIndex) override; - [[nodiscard]] inline const NavigationMesh::Face* Face(uint32_t i) const + [[nodiscard]] inline const NavigationMesh::Face* Face(FaceIndex faceIndex) const { - return mNavigationMesh->GetFace(i); + return mNavigationMesh->GetFace(faceIndex); } /** @@ -74,16 +74,15 @@ public: */ struct FaceNode { - uint32_t faceIndex; ///< Index of face which is associated with node - // neighbours - uint32_t faces[3]; ///< List of neighbouring faces (max 3 for a triangle) - uint32_t edges[3]; ///< List of edges (max 3 for a triangle) - float weight[3]; ///< List of weights (by distance) to each neighbour + FaceIndex faces[3]; ///< List of neighbouring faces (max 3 for a triangle) + EdgeIndex edges[3]; ///< List of edges (max 3 for a triangle) + float weight[3]; ///< List of weights (by distance) to each neighbour }; + using FaceNodeIndex = FaceIndex; NavigationMesh* mNavigationMesh; ///< Pointer to a valid NavigationMesh - std::vector mNodes; ///< List of nodes + std::vector mNodes; ///< List of nodes }; } // namespace Dali::Scene3D::Internal::Algorithm #endif // DALI_SCENE3D_INTERNAL_PATH_FINDER_DJIKSTRA_H diff --git a/dali-scene3d/internal/algorithm/path-finder-spfa-double-way.cpp b/dali-scene3d/internal/algorithm/path-finder-spfa-double-way.cpp index 3b5f844..0eb29a3 100644 --- a/dali-scene3d/internal/algorithm/path-finder-spfa-double-way.cpp +++ b/dali-scene3d/internal/algorithm/path-finder-spfa-double-way.cpp @@ -26,7 +26,8 @@ #include #include -using WayPointList = Dali::Scene3D::Algorithm::WayPointList; +using WayPointList = Dali::Scene3D::Algorithm::WayPointList; +using FaceNodeIndex = Dali::Scene3D::Internal::Algorithm::PathFinderAlgorithmSPFADoubleWay::FaceNodeIndex; namespace { @@ -38,16 +39,16 @@ constexpr float PRIORITY_SCALE_FACTOR = 0.7f; ///< The value of heuristic factor * * @param[in,out] components Container of components id stored. * @param[in] index index what we want to get components's id. - * @return uint32_t top-value of this components. + * @return FaceIndex top-value of this components. */ -uint32_t GetComponentId(std::vector& components, uint32_t index) +FaceNodeIndex GetComponentId(std::vector& components, FaceNodeIndex index) { if(components[index] == index) { return index; } // Get my parent's components id, and update myself. - uint32_t ret = GetComponentId(components, components[index]); + FaceNodeIndex ret = GetComponentId(components, components[index]); return components[index] = ret; } @@ -59,25 +60,25 @@ uint32_t GetComponentId(std::vector& components, uint32_t index) * @param[in] index0 index of components what we want to be combined. * @param[in] index1 index of components what we want to be combined. */ -void ComponentsCombine(std::vector& components, std::vector& componentsLevel, uint32_t index0, uint32_t index1) +void ComponentsCombine(std::vector& components, std::vector& componentsLevel, FaceNodeIndex index0, FaceNodeIndex index1) { - uint32_t p0 = GetComponentId(components, index0); - uint32_t p1 = GetComponentId(components, index1); - if(p0 == p1) + FaceNodeIndex ancestor0 = GetComponentId(components, index0); + FaceNodeIndex ancestor1 = GetComponentId(components, index1); + if(ancestor0 == ancestor1) { return; } - if(componentsLevel[p0] < componentsLevel[p1]) + if(componentsLevel[ancestor0] < componentsLevel[ancestor1]) { - components[p0] = p1; + components[ancestor0] = ancestor1; } else { - components[p1] = p0; - if(componentsLevel[p0] == componentsLevel[p1]) + components[ancestor1] = ancestor0; + if(componentsLevel[ancestor0] == componentsLevel[ancestor1]) { - ++componentsLevel[p0]; + ++componentsLevel[ancestor0]; } } } @@ -93,7 +94,7 @@ PathFinderAlgorithmSPFADoubleWay::PathFinderAlgorithmSPFADoubleWay(Dali::Scene3D PathFinderAlgorithmSPFADoubleWay::~PathFinderAlgorithmSPFADoubleWay() = default; -float PathFinderAlgorithmSPFADoubleWay::DistancePanaltyCalculate(uint32_t index) const noexcept +float PathFinderAlgorithmSPFADoubleWay::DistancePanaltyCalculate(FaceIndex index) const noexcept { return dist[index] - priority[index] * PRIORITY_SCALE_FACTOR; } @@ -101,7 +102,7 @@ float PathFinderAlgorithmSPFADoubleWay::DistancePanaltyCalculate(uint32_t index) Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(const Dali::Vector3& positionFrom, const Dali::Vector3& positionTo) { Dali::Vector3 outPosFrom; - uint32_t polyIndexFrom; + FaceIndex polyIndexFrom; auto result = mNavigationMesh->FindFloor(positionFrom, outPosFrom, polyIndexFrom); Scene3D::Algorithm::WayPointList waypoints; @@ -109,7 +110,7 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(cons if(result) { Dali::Vector3 outPosTo; - uint32_t polyIndexTo; + FaceIndex polyIndexTo; result = mNavigationMesh->FindFloor(positionTo, outPosTo, polyIndexTo); if(result) @@ -136,7 +137,7 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(cons return waypoints; } -Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(uint32_t sourcePolyIndex, uint32_t targetPolyIndex) +Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(FaceIndex sourcePolyIndex, FaceIndex targetPolyIndex) { // Fast return if source and target index is same. if(sourcePolyIndex == targetPolyIndex) @@ -160,11 +161,11 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(uint } // pair - using queueItem = std::pair; + using queueItem = std::pair; std::list nodeQueue; - std::unordered_set usedPolyIndexs[2]; + std::unordered_set usedPolyIndexs[2]; // Set distance of source and target dist[sourcePolyIndex] = 0.0f; @@ -178,9 +179,9 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(uint usedPolyIndexs[0].insert(sourcePolyIndex); usedPolyIndexs[1].insert(targetPolyIndex); - bool foundPath = false; - uint32_t forwardEndIndex = Scene3D::Algorithm::NavigationMesh::NULL_FACE; - uint32_t backwardStartIndex = Scene3D::Algorithm::NavigationMesh::NULL_FACE; + bool foundPath = false; + FaceIndex forwardEndIndex = Scene3D::Algorithm::NavigationMesh::NULL_FACE; + FaceIndex backwardStartIndex = Scene3D::Algorithm::NavigationMesh::NULL_FACE; const auto sourcePos = Dali::Vector3(Face(sourcePolyIndex)->center); const auto targetPos = Dali::Vector3(Face(targetPolyIndex)->center); @@ -265,9 +266,9 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(uint } // Build path of face index - std::list q; + std::list q; { - uint32_t u = forwardEndIndex; + FaceIndex u = forwardEndIndex; while(u != Scene3D::Algorithm::NavigationMesh::NULL_FACE) { q.push_front(u); @@ -275,7 +276,7 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(uint } } { - uint32_t u = backwardStartIndex; + FaceIndex u = backwardStartIndex; while(u != Scene3D::Algorithm::NavigationMesh::NULL_FACE) { q.push_back(u); @@ -350,10 +351,10 @@ void PathFinderAlgorithmSPFADoubleWay::PrepareData() queued.resize(faceCount); // Temperal container for components level. It will be used for Union-Find algorithm. - std::vector componentLevels(faceCount); + std::vector componentLevels(faceCount); // Initialize path informations. - for(auto i = 0u; i < faceCount; ++i) + for(FaceNodeIndex i = 0u; i < faceCount; ++i) { dist[i] = std::numeric_limits::infinity(); priority[i] = -1.0f; // Initialize by negative value, that we didn't calculate yet. @@ -366,7 +367,8 @@ void PathFinderAlgorithmSPFADoubleWay::PrepareData() } // for each face build the list - for(auto i = 0u; i < faceCount; ++i) + // TODO : Currently, we are assume that FaceNodeIndex is matched with FaceIndex 1:1. This might be changed in future. + for(FaceNodeIndex i = 0u; i < faceCount; ++i) { auto& node = mNodes[i]; const auto* face = mNavigationMesh->GetFace(i); diff --git a/dali-scene3d/internal/algorithm/path-finder-spfa-double-way.h b/dali-scene3d/internal/algorithm/path-finder-spfa-double-way.h index 32e04b9..60b7469 100644 --- a/dali-scene3d/internal/algorithm/path-finder-spfa-double-way.h +++ b/dali-scene3d/internal/algorithm/path-finder-spfa-double-way.h @@ -54,11 +54,11 @@ public: * @param[in] polyIndexTo Index of end polygon * @return List of waypoints for path */ - Scene3D::Algorithm::WayPointList FindPath(uint32_t sourcePolyIndex, uint32_t targetPolyIndex) override; + Scene3D::Algorithm::WayPointList FindPath(FaceIndex sourcePolyIndex, FaceIndex targetPolyIndex) override; - [[nodiscard]] inline const NavigationMesh::Face* Face(uint32_t i) const + [[nodiscard]] inline const NavigationMesh::Face* Face(FaceIndex faceIndex) const { - return mNavigationMesh->GetFace(i); + return mNavigationMesh->GetFace(faceIndex); } /** @@ -75,31 +75,30 @@ public: * * @param[in] index index of node what we want to calculate panantly. */ - float DistancePanaltyCalculate(uint32_t index) const noexcept; + float DistancePanaltyCalculate(FaceIndex index) const noexcept; /** * Structure describes single node of pathfinding algorithm */ struct FaceNode { - uint32_t faceIndex; ///< Index of face which is associated with node - // neighbours - uint32_t faces[3]; ///< List of neighbouring faces (max 3 for a triangle) - uint32_t edges[3]; ///< List of edges (max 3 for a triangle) - float weight[3]; ///< List of weights (by distance) to each neighbour + FaceIndex faces[3]; ///< List of neighbouring faces (max 3 for a triangle) + EdgeIndex edges[3]; ///< List of edges (max 3 for a triangle) + float weight[3]; ///< List of weights (by distance) to each neighbour }; + using FaceNodeIndex = FaceIndex; NavigationMesh* mNavigationMesh; ///< Pointer to a valid NavigationMesh std::vector mNodes; ///< List of nodes private: - std::vector dist; - std::vector priority; ///< Cached priority value. It will be calculated by source & target poly index per every queries. - std::vector prevForward; - std::vector prevBackward; - std::vector componentIds; ///< Id of connected components per each nodes. It should be one of node's index. - std::vector queued; + std::vector dist; + std::vector priority; ///< Cached priority value. It will be calculated by source & target poly index per every queries. + std::vector prevForward; + std::vector prevBackward; + std::vector componentIds; ///< Id of connected components per each nodes. It should be one of node's index. + std::vector queued; }; } // namespace Dali::Scene3D::Internal::Algorithm #endif // DALI_SCENE3D_INTERNAL_PATH_FINDER_SPFA_DOUBLE_WAY_H diff --git a/dali-scene3d/internal/algorithm/path-finder-spfa.cpp b/dali-scene3d/internal/algorithm/path-finder-spfa.cpp index aa8b3bd..c6b1d48 100644 --- a/dali-scene3d/internal/algorithm/path-finder-spfa.cpp +++ b/dali-scene3d/internal/algorithm/path-finder-spfa.cpp @@ -40,7 +40,7 @@ PathFinderAlgorithmSPFA::~PathFinderAlgorithmSPFA() = default; Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFA::FindPath(const Dali::Vector3& positionFrom, const Dali::Vector3& positionTo) { Dali::Vector3 outPosFrom; - uint32_t polyIndexFrom; + FaceIndex polyIndexFrom; auto result = mNavigationMesh->FindFloor(positionFrom, outPosFrom, polyIndexFrom); Scene3D::Algorithm::WayPointList waypoints; @@ -48,7 +48,7 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFA::FindPath(const Dali::V if(result) { Dali::Vector3 outPosTo; - uint32_t polyIndexTo; + FaceIndex polyIndexTo; result = mNavigationMesh->FindFloor(positionTo, outPosTo, polyIndexTo); if(result) @@ -75,18 +75,18 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFA::FindPath(const Dali::V return waypoints; } -Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFA::FindPath(uint32_t sourcePolyIndex, uint32_t targetPolyIndex) +Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFA::FindPath(FaceIndex sourcePolyIndex, FaceIndex targetPolyIndex) { - auto nodeCount = uint32_t(mNodes.size()); - std::vector dist; - std::vector prev; - std::vector queued; + auto nodeCount = uint32_t(mNodes.size()); + std::vector dist; + std::vector prev; + std::vector queued; dist.resize(mNodes.size()); prev.resize(mNodes.size()); queued.resize(mNodes.size()); - std::list nodeQueue; + std::list nodeQueue; [[maybe_unused]] auto sourcePos = Dali::Vector3(Face(sourcePolyIndex)->center); @@ -145,8 +145,8 @@ Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFA::FindPath(uint32_t sour } // Compute distances for each node back to the source - auto u = targetPolyIndex; - std::list q; + auto u = targetPolyIndex; + std::list q; if(prev[u] != Scene3D::Algorithm::NavigationMesh::NULL_FACE || u == sourcePolyIndex) { while(u != Scene3D::Algorithm::NavigationMesh::NULL_FACE) @@ -203,7 +203,8 @@ void PathFinderAlgorithmSPFA::PrepareData() mNodes.resize(faceCount); // for each face build the list - for(auto i = 0u; i < faceCount; ++i) + // TODO : Currently, we are assume that FaceNodeIndex is matched with FaceIndex 1:1. This might be changed in future. + for(FaceNodeIndex i = 0u; i < faceCount; ++i) { auto& node = mNodes[i]; const auto* face = mNavigationMesh->GetFace(i); diff --git a/dali-scene3d/internal/algorithm/path-finder-spfa.h b/dali-scene3d/internal/algorithm/path-finder-spfa.h index 99fc4af..8b62206 100644 --- a/dali-scene3d/internal/algorithm/path-finder-spfa.h +++ b/dali-scene3d/internal/algorithm/path-finder-spfa.h @@ -54,11 +54,11 @@ public: * @param[in] polyIndexTo Index of end polygon * @return List of waypoints for path */ - Scene3D::Algorithm::WayPointList FindPath(uint32_t sourcePolyIndex, uint32_t targetPolyIndex) override; + Scene3D::Algorithm::WayPointList FindPath(FaceIndex sourcePolyIndex, FaceIndex targetPolyIndex) override; - [[nodiscard]] inline const NavigationMesh::Face* Face(uint32_t i) const + [[nodiscard]] inline const NavigationMesh::Face* Face(FaceIndex faceIndex) const { - return mNavigationMesh->GetFace(i); + return mNavigationMesh->GetFace(faceIndex); } /** @@ -74,13 +74,12 @@ public: */ struct FaceNode { - uint32_t faceIndex; ///< Index of face which is associated with node - // neighbours - uint32_t faces[3]; ///< List of neighbouring faces (max 3 for a triangle) - uint32_t edges[3]; ///< List of edges (max 3 for a triangle) - float weight[3]; ///< List of weights (by distance) to each neighbour + FaceIndex faces[3]; ///< List of neighbouring faces (max 3 for a triangle) + EdgeIndex edges[3]; ///< List of edges (max 3 for a triangle) + float weight[3]; ///< List of weights (by distance) to each neighbour }; + using FaceNodeIndex = FaceIndex; NavigationMesh* mNavigationMesh; ///< Pointer to a valid NavigationMesh std::vector mNodes; ///< List of nodes diff --git a/dali-scene3d/public-api/algorithm/navigation-mesh.cpp b/dali-scene3d/public-api/algorithm/navigation-mesh.cpp index 45225ae..9b594d1 100644 --- a/dali-scene3d/public-api/algorithm/navigation-mesh.cpp +++ b/dali-scene3d/public-api/algorithm/navigation-mesh.cpp @@ -24,10 +24,9 @@ using Dali::Vector3; namespace Dali::Scene3D::Algorithm { - -NavigationMesh::NavigationMesh( NavigationMeshImpl* impl ) +NavigationMesh::NavigationMesh(NavigationMeshImpl* impl) { - mImpl.reset( impl ); + mImpl.reset(impl); } NavigationMesh::~NavigationMesh() = default; @@ -47,27 +46,27 @@ NavigationMesh::~NavigationMesh() = default; return mImpl->GetVertexCount(); } -bool NavigationMesh::FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, uint32_t& polyIndex) +bool NavigationMesh::FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, FaceIndex& faceIndex) { - return mImpl->FindFloor(position, outPosition, polyIndex); + return mImpl->FindFloor(position, outPosition, faceIndex); } -bool NavigationMesh::FindFloorForFace(const Dali::Vector3& position, uint32_t faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition) +bool NavigationMesh::FindFloorForFace(const Dali::Vector3& position, FaceIndex faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition) { return mImpl->FindFloorForFace(position, faceIndex, dontCheckNeighbours, outPosition); } -[[nodiscard]] const NavigationMesh::Face* NavigationMesh::GetFace(int index) const +[[nodiscard]] const NavigationMesh::Face* NavigationMesh::GetFace(FaceIndex index) const { return mImpl->GetFace(index); } -[[nodiscard]] const NavigationMesh::Edge* NavigationMesh::GetEdge(int index) const +[[nodiscard]] const NavigationMesh::Edge* NavigationMesh::GetEdge(EdgeIndex index) const { return mImpl->GetEdge(index); } -[[nodiscard]] const NavigationMesh::Vertex* NavigationMesh::GetVertex(int index) const +[[nodiscard]] const NavigationMesh::Vertex* NavigationMesh::GetVertex(VertexIndex index) const { return mImpl->GetVertex(index); } @@ -77,12 +76,12 @@ void NavigationMesh::SetSceneTransform(const Dali::Matrix& transform) mImpl->SetTransform(transform); } -Dali::Vector3 NavigationMesh::PointSceneToLocal(const Dali::Vector3& point) +Dali::Vector3 NavigationMesh::PointSceneToLocal(const Dali::Vector3& point) const { return mImpl->PointSceneToLocal(point); } -Dali::Vector3 NavigationMesh::PointLocalToScene(const Dali::Vector3& point) +Dali::Vector3 NavigationMesh::PointLocalToScene(const Dali::Vector3& point) const { return mImpl->PointLocalToScene(point); } @@ -92,4 +91,4 @@ Dali::Vector3 NavigationMesh::GetGravityVector() const return mImpl->GetGravityVector(); } -} \ No newline at end of file +} // namespace Dali::Scene3D::Algorithm \ No newline at end of file diff --git a/dali-scene3d/public-api/algorithm/navigation-mesh.h b/dali-scene3d/public-api/algorithm/navigation-mesh.h index b2a0fd9..638c26d 100644 --- a/dali-scene3d/public-api/algorithm/navigation-mesh.h +++ b/dali-scene3d/public-api/algorithm/navigation-mesh.h @@ -50,6 +50,11 @@ namespace Dali::Scene3D::Algorithm // Using PImpling but not usual DALi handles as this object isn't supposed to be refcounted using NavigationMeshImpl = Dali::Scene3D::Internal::Algorithm::NavigationMesh; +// Make each to change each index value's type here. +using VertexIndex = uint16_t; +using EdgeIndex = uint16_t; +using FaceIndex = uint16_t; + /** * @class NavigationMesh * @@ -75,14 +80,14 @@ public: /** * @struct Face * - * Describes a single polygon + * Describes a single polygon's face */ struct Face { - uint16_t vertex[NAVIGATION_MESH_MAX_VERTICES_PER_FACE]; ///< Vertices per face - uint16_t edge[NAVIGATION_MESH_MAX_EDGES_PER_FACE]; ///< Edges per face - float normal[NAVIGATION_MESH_MAX_COMPONENTS_3D]; ///< Normal vector - float center[NAVIGATION_MESH_MAX_COMPONENTS_3D]; ///< Barycentric coordinates + VertexIndex vertex[NAVIGATION_MESH_MAX_VERTICES_PER_FACE]; ///< Vertices per face + EdgeIndex edge[NAVIGATION_MESH_MAX_EDGES_PER_FACE]; ///< Edges per face + float normal[NAVIGATION_MESH_MAX_COMPONENTS_3D]; ///< Normal vector + float center[NAVIGATION_MESH_MAX_COMPONENTS_3D]; ///< Barycentric coordinates }; /** @@ -92,8 +97,8 @@ public: */ struct Edge { - uint16_t vertex[NAVIGATION_MESH_MAX_COMPONENTS_2D]; ///< Vertices making the edge - uint16_t face[NAVIGATION_MESH_MAX_COMPONENTS_2D]; ///< Faces on both sides of edge + VertexIndex vertex[NAVIGATION_MESH_MAX_COMPONENTS_2D]; ///< Vertices making the edge + FaceIndex face[NAVIGATION_MESH_MAX_COMPONENTS_2D]; ///< Faces on both sides of edge }; /** @@ -106,7 +111,7 @@ public: { union { - float co[NAVIGATION_MESH_MAX_COMPONENTS_3D]; ///< Coordinates of vertex + float coordinates[NAVIGATION_MESH_MAX_COMPONENTS_3D]; ///< Coordinates of vertex struct { float x, y, z; @@ -147,11 +152,11 @@ public: * @brief Looks for the floor under specified position * @param[in] position Position to investigate * @param[in] outPosition Position on the floor in found - * @param[in] faceIndex Index of NavigationMesh face associated with floor + * @param[out] faceIndex Index of NavigationMesh face associated with floor * * @return True if floor has been found, False otherwise */ - bool FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, uint32_t& faceIndex); + bool FindFloor(const Dali::Vector3& position, Dali::Vector3& outPosition, FaceIndex& faceIndex); /** * @brief Looks for a floor starting from specified face @@ -167,28 +172,28 @@ public: * * @return True on success, false otherwise */ - bool FindFloorForFace(const Dali::Vector3& position, uint32_t faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition); + bool FindFloorForFace(const Dali::Vector3& position, FaceIndex faceIndex, bool dontCheckNeighbours, Dali::Vector3& outPosition); /** * @brief Returns pointer to Face structure * @param[in] index Index of face to retrieve * @return Pointer to valid Face structure or nullptr */ - [[nodiscard]] const Face* GetFace(int index) const; + [[nodiscard]] const Face* GetFace(FaceIndex index) const; /** * @brief Returns edge structure * @param[in] index Index of edge to retrieve * @return Pointer to valid Edge structure or nullptr */ - [[nodiscard]] const Edge* GetEdge(int index) const; + [[nodiscard]] const Edge* GetEdge(EdgeIndex index) const; /** * @brief Returns vertex structure * @param[in] index Index of vertex to retrieve * @return Pointer to valid Vertex structure or nullptr */ - [[nodiscard]] const Vertex* GetVertex(int index) const; + [[nodiscard]] const Vertex* GetVertex(VertexIndex index) const; /** * @brief Sets static transform for the navigation mesh object @@ -225,7 +230,7 @@ public: * @param[in] point Point to transform * @return Point transformed to the local space */ - Dali::Vector3 PointSceneToLocal(const Dali::Vector3& point); + Dali::Vector3 PointSceneToLocal(const Dali::Vector3& point) const; /** * @brief Transforms point into the parent transform space @@ -235,7 +240,7 @@ public: * @param[in] point Point to transform * @return Point transformed into the parent space */ - Dali::Vector3 PointLocalToScene(const Dali::Vector3& point); + Dali::Vector3 PointLocalToScene(const Dali::Vector3& point) const; /** * @brief Returns direction of the gravity vector @@ -246,8 +251,8 @@ public: */ Dali::Vector3 GetGravityVector() const; - static constexpr uint16_t NULL_FACE{0xffff}; ///< Represents null polygon - static constexpr uint16_t NULL_EDGE{0xffff}; ///< represents null edge + static constexpr FaceIndex NULL_FACE{std::numeric_limits::max()}; ///< Represents null face + static constexpr EdgeIndex NULL_EDGE{std::numeric_limits::max()}; ///< Represents null edge public: DALI_INTERNAL explicit NavigationMesh(NavigationMeshImpl* impl); diff --git a/dali-scene3d/public-api/algorithm/path-finder.cpp b/dali-scene3d/public-api/algorithm/path-finder.cpp index 6f29448..621fc82 100644 --- a/dali-scene3d/public-api/algorithm/path-finder.cpp +++ b/dali-scene3d/public-api/algorithm/path-finder.cpp @@ -63,7 +63,7 @@ WayPointList PathFinder::FindPath(const Dali::Vector3& positionFrom, const Dali: return mImpl->FindPath(positionFrom, positionTo); } -WayPointList PathFinder::FindPath(uint32_t polyIndexFrom, uint32_t polyIndexTo) +WayPointList PathFinder::FindPath(FaceIndex polyIndexFrom, FaceIndex polyIndexTo) { return mImpl->FindPath(polyIndexFrom, polyIndexTo); } diff --git a/dali-scene3d/public-api/algorithm/path-finder.h b/dali-scene3d/public-api/algorithm/path-finder.h index ee37d5e..5d2a6f1 100644 --- a/dali-scene3d/public-api/algorithm/path-finder.h +++ b/dali-scene3d/public-api/algorithm/path-finder.h @@ -68,7 +68,7 @@ public: * @param[in] polyIndexTo Index of end polygon * @return List of waypoints for path or empty vector if no success */ - virtual WayPointList FindPath(uint32_t polyIndexFrom, uint32_t polyIndexTo) = 0; + virtual WayPointList FindPath(FaceIndex polyIndexFrom, FaceIndex polyIndexTo) = 0; }; /** @@ -119,7 +119,7 @@ public: * @param[in] faceIndexTo Target face index * @return List of waypoints for path or empty list on failure */ - WayPointList FindPath(uint32_t faceIndexFrom, uint32_t faceIndexTo); + WayPointList FindPath(FaceIndex faceIndexFrom, FaceIndex faceIndexTo); private: PathFinder() = delete;