[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d / utc-Dali-PathFinding.cpp
index c75074a..d924c3a 100644 (file)
  *
  */
 
+#include <dali-test-suite-utils.h>
 #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 <dali-test-suite-utils.h>
 
 using namespace Dali;
 using namespace Dali::Scene3D::Algorithm;
 using namespace Dali::Scene3D::Loader;
 
-bool CompareResults( const std::vector<uint32_t>& nodes, const WayPointList& waypoints )
+bool CompareResults(const std::vector<FaceIndex>& nodes, const WayPointList& waypoints)
 {
   if(nodes.size() != waypoints.size())
   {
+    std::ostringstream oss;
+    oss << "expect indexs : [";
+    for(const auto& index : nodes)
+    {
+      oss << index << ", ";
+    }
+    oss << "]\n";
+    oss << "your indexs : [";
+    for(const auto& waypoint : waypoints)
+    {
+      oss << waypoint.GetNavigationMeshFaceIndex() << ", ";
+    }
+    oss << "]\n";
+    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())
     {
+      std::ostringstream oss;
+      oss << "expect indexs : [";
+      for(const auto& index : nodes)
+      {
+        oss << index << ", ";
+      }
+      oss << "]\n";
+      oss << "your indexs : [";
+      for(const auto& waypoint : waypoints)
+      {
+        oss << waypoint.GetNavigationMeshFaceIndex() << ", ";
+      }
+      oss << "]\n";
+      tet_printf("%s\n", oss.str().c_str());
       return false;
     }
   }
@@ -42,9 +70,9 @@ bool CompareResults( const std::vector<uint32_t>& 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);
@@ -54,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<PathFinderAlgorithm>(-1) );
+  auto pathfinder = PathFinder::New(*navmesh, static_cast<PathFinderAlgorithm>(-1));
 
   DALI_TEST_CHECK(navmesh);
   DALI_TEST_CHECK(!pathfinder);
@@ -64,109 +92,127 @@ 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 UtcDaliPathFinderDjikstraFindPath0(void)
+int UtcDaliPathFinderFindShortestPath0(void)
 {
-  auto navmesh = NavigationMeshFactory::CreateFromFile( "resources/navmesh-test.bin");
+  auto navmesh = NavigationMeshFactory::CreateFromFile("resources/navmesh-test.bin");
 
-  auto pathfinder = PathFinder::New( *navmesh, PathFinderAlgorithm::DJIKSTRA_SHORTEST_PATH );
-
-  DALI_TEST_CHECK(navmesh);
-  DALI_TEST_CHECK(pathfinder);
+  std::vector<PathFinderAlgorithm> testAlgorithms = {
+    PathFinderAlgorithm::DIJKSTRA_SHORTEST_PATH,
+    PathFinderAlgorithm::SPFA,
+  };
 
+  for(const auto& algorithm : testAlgorithms)
   {
-    auto waypoints = pathfinder->FindPath(18, 139);
-    DALI_TEST_NOT_EQUALS(int(waypoints.size()), 0, 0, TEST_LOCATION);
-
-    // Results are verified in the Blender
-    std::vector<uint32_t> expectedResults =
-      {18, 97, 106, 82, 50, 139};
+    tet_printf("Test algorithm type : %d\n", static_cast<int>(algorithm));
+    auto pathfinder = PathFinder::New(*navmesh, algorithm);
 
-    DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION);
-  }
-  //printWaypointForPython(waypoints);
+    DALI_TEST_CHECK(navmesh);
+    DALI_TEST_CHECK(pathfinder);
 
-  {
-    // Top floor middle to the tree
+    {
+      auto waypoints = pathfinder->FindPath(18, 139);
+      DALI_TEST_NOT_EQUALS(int(waypoints.size()), 0, 0, TEST_LOCATION);
 
-    auto waypoints = pathfinder->FindPath(18, 157);
-    DALI_TEST_NOT_EQUALS(int(waypoints.size()), 0, 0, TEST_LOCATION);
+      // Results are verified in the Blender
+      std::vector<FaceIndex> expectedResults =
+        {18, 97, 106, 82, 50, 139};
 
+      DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION);
+    }
     //printWaypointForPython(waypoints);
 
-    // Results are verified in the Blender
-    std::vector<uint32_t> expectedResults =
-      {18, 97, 106, 82, 50, 6, 89, 33, 157};
+    {
+      // Top floor middle to the tree
+
+      auto waypoints = pathfinder->FindPath(18, 157);
+      DALI_TEST_NOT_EQUALS(int(waypoints.size()), 0, 0, TEST_LOCATION);
+
+      //printWaypointForPython(waypoints);
 
-    DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION);
+      // Results are verified in the Blender
+      std::vector<FaceIndex> expectedResults =
+        {18, 97, 106, 82, 50, 6, 89, 33, 157};
 
+      DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION);
+    }
   }
 
   END_TEST;
 }
 
-int UtcDaliPathFinderDjikstraFindPath1(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));
-
-  auto pathfinder = PathFinder::New( *navmesh, PathFinderAlgorithm::DJIKSTRA_SHORTEST_PATH );
+  navmesh->SetSceneTransform(Matrix(Matrix::IDENTITY));
 
-  DALI_TEST_CHECK(navmesh);
-  DALI_TEST_CHECK(pathfinder);
+  std::vector<PathFinderAlgorithm> testAlgorithms = {
+    PathFinderAlgorithm::DIJKSTRA_SHORTEST_PATH,
+    PathFinderAlgorithm::SPFA,
+    PathFinderAlgorithm::SPFA_DOUBLE_WAY, /* Note : Even this algorithm doesn't found shortest path, UTC will pass. */
+  };
 
+  for(const auto& algorithm : testAlgorithms)
   {
-    Vector3 from(-6.0767, -1.7268, 0.1438); // ground 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<uint32_t> expectedResults =
-     {154, 58, 85, 106, 128, 132, 137};
+    tet_printf("Test algorithm type : %d\n", static_cast<int>(algorithm));
+    auto pathfinder = PathFinder::New(*navmesh, algorithm);
 
-    DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION);
+    DALI_TEST_CHECK(navmesh);
+    DALI_TEST_CHECK(pathfinder);
 
-    // 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);
-
-      DALI_TEST_EQUALS(result, true, TEST_LOCATION);
-      DALI_TEST_EQUALS(verifyPos, waypoints[0].GetScenePosition(), TEST_LOCATION);
-      DALI_TEST_EQUALS(verifyIndex, waypoints[0].GetNavigationMeshFaceIndex(), TEST_LOCATION);
-
-      // Verified with Blender
-      Vector2 local(1.064201f, -0.273200f);
-      DALI_TEST_EQUALS(local, waypoints[0].GetFaceLocalSpacePosition(), TEST_LOCATION);
-    }
-
-    {
-      Vector3  verifyPos = Vector3::ZERO;
-      uint32_t 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);
-      DALI_TEST_EQUALS(verifyIndex, waypoints.back().GetNavigationMeshFaceIndex(), TEST_LOCATION);
-
-      // Verified with Blender
-      Vector2 local(0.165907f, 0.142597f);
-      DALI_TEST_EQUALS(local, waypoints.back().GetFaceLocalSpacePosition(), TEST_LOCATION);
+      Vector3 from(-6.0767, -1.7268, 0.1438); // ground 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<FaceIndex> 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;
+        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);
+        DALI_TEST_EQUALS(verifyIndex, waypoints[0].GetNavigationMeshFaceIndex(), TEST_LOCATION);
+
+        // Verified with Blender
+        Vector2 local(1.064201f, -0.273200f);
+        DALI_TEST_EQUALS(local, waypoints[0].GetFaceLocalSpacePosition(), TEST_LOCATION);
+      }
+
+      {
+        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);
+        DALI_TEST_EQUALS(verifyIndex, waypoints.back().GetNavigationMeshFaceIndex(), TEST_LOCATION);
+
+        // Verified with Blender
+        Vector2 local(0.165907f, 0.142597f);
+        DALI_TEST_EQUALS(local, waypoints.back().GetFaceLocalSpacePosition(), TEST_LOCATION);
+      }
     }
   }