c75074a67d02bef6285840f5aef9470bc51dd149
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d / utc-Dali-PathFinding.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "dali-scene3d/public-api/algorithm/navigation-mesh.h"
19 #include "dali-scene3d/public-api/algorithm/path-finder.h"
20 #include "dali-scene3d/public-api/loader/navigation-mesh-factory.h"
21 #include <dali-test-suite-utils.h>
22
23 using namespace Dali;
24 using namespace Dali::Scene3D::Algorithm;
25 using namespace Dali::Scene3D::Loader;
26
27 bool CompareResults( const std::vector<uint32_t>& nodes, const WayPointList& waypoints )
28 {
29   if(nodes.size() != waypoints.size())
30   {
31     return false;
32   }
33   for(auto i = 0u; i < nodes.size(); ++i )
34   {
35     if(nodes[i] != waypoints[i].GetNavigationMeshFaceIndex())
36     {
37       return false;
38     }
39   }
40   return true;
41 }
42
43 int UtcDaliPathFinderNewP(void)
44 {
45   auto navmesh = NavigationMeshFactory::CreateFromFile( "resources/navmesh-test.bin");
46
47   auto pathfinder = PathFinder::New( *navmesh, PathFinderAlgorithm::DEFAULT );
48
49   DALI_TEST_CHECK(navmesh);
50   DALI_TEST_CHECK(pathfinder);
51
52   END_TEST;
53 }
54
55 int UtcDaliPathFinderNewFail(void)
56 {
57   auto navmesh = NavigationMeshFactory::CreateFromFile( "resources/navmesh-test.bin");
58
59   auto pathfinder = PathFinder::New( *navmesh, static_cast<PathFinderAlgorithm>(-1) );
60
61   DALI_TEST_CHECK(navmesh);
62   DALI_TEST_CHECK(!pathfinder);
63
64   END_TEST;
65 }
66
67 void printWaypointForPython( WayPointList& waypoints)
68 {
69   tet_printf( "size: %d\n", waypoints.size());
70   tet_printf( "[");
71   for(auto& wp : waypoints)
72   {
73     auto index = wp.GetNavigationMeshFaceIndex();
74     tet_printf("%d, ", index);
75   }
76   tet_printf( "]");
77 }
78
79 int UtcDaliPathFinderDjikstraFindPath0(void)
80 {
81   auto navmesh = NavigationMeshFactory::CreateFromFile( "resources/navmesh-test.bin");
82
83   auto pathfinder = PathFinder::New( *navmesh, PathFinderAlgorithm::DJIKSTRA_SHORTEST_PATH );
84
85   DALI_TEST_CHECK(navmesh);
86   DALI_TEST_CHECK(pathfinder);
87
88   {
89     auto waypoints = pathfinder->FindPath(18, 139);
90     DALI_TEST_NOT_EQUALS(int(waypoints.size()), 0, 0, TEST_LOCATION);
91
92     // Results are verified in the Blender
93     std::vector<uint32_t> expectedResults =
94       {18, 97, 106, 82, 50, 139};
95
96     DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION);
97   }
98   //printWaypointForPython(waypoints);
99
100   {
101     // Top floor middle to the tree
102
103     auto waypoints = pathfinder->FindPath(18, 157);
104     DALI_TEST_NOT_EQUALS(int(waypoints.size()), 0, 0, TEST_LOCATION);
105
106     //printWaypointForPython(waypoints);
107
108     // Results are verified in the Blender
109     std::vector<uint32_t> expectedResults =
110       {18, 97, 106, 82, 50, 6, 89, 33, 157};
111
112     DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION);
113
114   }
115
116   END_TEST;
117 }
118
119 int UtcDaliPathFinderDjikstraFindPath1(void)
120 {
121   auto navmesh = NavigationMeshFactory::CreateFromFile( "resources/navmesh-test.bin");
122   // All coordinates in navmesh local space
123   navmesh->SetSceneTransform( Matrix(Matrix::IDENTITY));
124
125   auto pathfinder = PathFinder::New( *navmesh, PathFinderAlgorithm::DJIKSTRA_SHORTEST_PATH );
126
127   DALI_TEST_CHECK(navmesh);
128   DALI_TEST_CHECK(pathfinder);
129
130   {
131     Vector3 from(-6.0767, -1.7268, 0.1438); // ground floor
132     Vector3 to(-6.0767, -1.7268, 4.287); // first floor
133
134     auto waypoints = pathfinder->FindPath(from, to);
135     DALI_TEST_NOT_EQUALS(int(waypoints.size()), 0, 0, TEST_LOCATION);
136
137     // Results are verified in the Blender
138     std::vector<uint32_t> expectedResults =
139      {154, 58, 85, 106, 128, 132, 137};
140
141     DALI_TEST_EQUALS(CompareResults(expectedResults, waypoints), true, TEST_LOCATION);
142
143     // Verify last and first points by finding floor points
144     {
145       Vector3  verifyPos = Vector3::ZERO;
146       uint32_t verifyIndex  = NavigationMesh::NULL_FACE;
147       auto     result = navmesh->FindFloor(from, verifyPos, verifyIndex);
148
149       DALI_TEST_EQUALS(result, true, TEST_LOCATION);
150       DALI_TEST_EQUALS(verifyPos, waypoints[0].GetScenePosition(), TEST_LOCATION);
151       DALI_TEST_EQUALS(verifyIndex, waypoints[0].GetNavigationMeshFaceIndex(), TEST_LOCATION);
152
153       // Verified with Blender
154       Vector2 local(1.064201f, -0.273200f);
155       DALI_TEST_EQUALS(local, waypoints[0].GetFaceLocalSpacePosition(), TEST_LOCATION);
156     }
157
158     {
159       Vector3  verifyPos = Vector3::ZERO;
160       uint32_t verifyIndex  = NavigationMesh::NULL_FACE;
161       auto     result = navmesh->FindFloor(to, verifyPos, verifyIndex);
162
163       DALI_TEST_EQUALS(result, true, TEST_LOCATION);
164       DALI_TEST_EQUALS(verifyPos, waypoints.back().GetScenePosition(), TEST_LOCATION);
165       DALI_TEST_EQUALS(verifyIndex, waypoints.back().GetNavigationMeshFaceIndex(), TEST_LOCATION);
166
167       // Verified with Blender
168       Vector2 local(0.165907f, 0.142597f);
169       DALI_TEST_EQUALS(local, waypoints.back().GetFaceLocalSpacePosition(), TEST_LOCATION);
170     }
171   }
172
173   END_TEST;
174 }