2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-scene3d/internal/algorithm/path-finder-spfa.h>
19 #include <dali-scene3d/internal/algorithm/path-finder-waypoint-data.h>
20 #include <dali-scene3d/public-api/algorithm/path-finder-waypoint.h>
26 using WayPointList = Dali::Scene3D::Algorithm::WayPointList;
28 namespace Dali::Scene3D::Internal::Algorithm
30 PathFinderAlgorithmSPFA::PathFinderAlgorithmSPFA(Dali::Scene3D::Algorithm::NavigationMesh& navMesh)
31 : mNavigationMesh(&GetImplementation(navMesh))
36 PathFinderAlgorithmSPFA::~PathFinderAlgorithmSPFA() = default;
38 Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFA::FindPath(const Dali::Vector3& positionFrom, const Dali::Vector3& positionTo)
40 Dali::Vector3 outPosFrom;
41 uint32_t polyIndexFrom;
42 auto result = mNavigationMesh->FindFloor(positionFrom, outPosFrom, polyIndexFrom);
44 Scene3D::Algorithm::WayPointList waypoints;
48 Dali::Vector3 outPosTo;
50 result = mNavigationMesh->FindFloor(positionTo, outPosTo, polyIndexTo);
55 waypoints = FindPath(polyIndexFrom, polyIndexTo);
57 // replace first and last waypoint
58 auto& wpFrom = static_cast<WayPointData&>(waypoints[0]);
59 auto& wpTo = static_cast<WayPointData&>(waypoints.back());
61 Vector2 fromCenter(wpFrom.point3d.x, wpFrom.point3d.y);
62 wpFrom.point3d = outPosFrom;
63 wpFrom.point2d = fromCenter - Vector2(outPosFrom.x, outPosFrom.y);
65 Vector2 toCenter(wpTo.point3d.x, wpTo.point3d.y);
66 wpTo.point3d = outPosTo;
67 wpTo.point2d = toCenter - Vector2(outPosTo.x, outPosTo.y);
68 wpTo.point3d = outPosTo;
72 // Returns waypoints with non-zero size of empty vector in case of failure (no path to be found)
76 Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFA::FindPath(uint32_t sourcePolyIndex, uint32_t targetPolyIndex)
78 auto nodeCount = uint32_t(mNodes.size());
79 std::vector<float> dist;
80 std::vector<uint32_t> prev;
81 std::vector<bool> queued;
83 dist.resize(mNodes.size());
84 prev.resize(mNodes.size());
85 queued.resize(mNodes.size());
87 std::list<uint32_t> nodeQueue;
89 [[maybe_unused]] auto sourcePos = Dali::Vector3(Face(sourcePolyIndex)->center);
91 for(auto i = 0u; i < nodeCount; ++i)
93 dist[i] = std::numeric_limits<float>::infinity();
94 prev[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
98 // Set distance of source
99 dist[sourcePolyIndex] = 0.0f;
100 queued[sourcePolyIndex] = true;
101 nodeQueue.push_back(sourcePolyIndex);
103 while(!nodeQueue.empty())
105 // find minimum distance
106 auto minDistIndex = nodeQueue.front();
107 nodeQueue.pop_front();
108 queued[minDistIndex] = false;
110 // Skip operator when minDistIndex is target
111 if(minDistIndex == targetPolyIndex)
116 // check the neighbours
117 for(auto i = 0u; i < 3; ++i)
119 auto nIndex = mNodes[minDistIndex].faces[i];
120 if(nIndex != Scene3D::Algorithm::NavigationMesh::NULL_FACE)
122 auto alt = dist[minDistIndex] + mNodes[minDistIndex].weight[i];
123 if(alt < dist[nIndex])
126 prev[nIndex] = minDistIndex;
130 queued[nIndex] = true;
131 if(!nodeQueue.empty() && dist[nIndex] < dist[nodeQueue.front()])
133 nodeQueue.push_front(nIndex);
137 nodeQueue.push_back(nIndex);
145 // Compute distances for each node back to the source
146 auto u = targetPolyIndex;
147 std::list<uint32_t> q;
148 if(prev[u] != Scene3D::Algorithm::NavigationMesh::NULL_FACE || u == sourcePolyIndex)
150 while(u != Scene3D::Algorithm::NavigationMesh::NULL_FACE)
157 WayPointList waypoints;
158 waypoints.resize(q.size());
162 // Fail to found path. Return empty list.
170 auto& wp = static_cast<WayPointData&>(waypoints[index]);
171 wp.face = mNavigationMesh->GetFace(n);
175 // set the common edge with previous node
178 const auto& node = mNodes[prevN];
179 for(auto i = 0u; i < 3; ++i)
181 if(node.faces[i] == wp.nodeIndex)
183 wp.edge = mNavigationMesh->GetEdge(node.edges[i]);
193 return OptimizeWaypoints(waypoints);
196 void PathFinderAlgorithmSPFA::PrepareData()
198 // Build the list structure connecting the nodes
199 auto faceCount = mNavigationMesh->GetFaceCount();
201 mNodes.resize(faceCount);
203 // for each face build the list
204 for(auto i = 0u; i < faceCount; ++i)
206 auto& node = mNodes[i];
207 const auto* face = mNavigationMesh->GetFace(i);
208 auto c0 = Dali::Vector3(face->center);
210 // for each edge add neighbouring face and compute distance to set the weight of node
211 for(auto edgeIndex = 0u; edgeIndex < 3; ++edgeIndex)
213 const auto* edge = mNavigationMesh->GetEdge(face->edge[edgeIndex]);
214 auto p1 = edge->face[0];
215 auto p2 = edge->face[1];
217 // One of faces is current face so ignore it
218 auto p = ((p1 != i) ? p1 : p2);
219 node.faces[edgeIndex] = p;
220 if(p != ::Dali::Scene3D::Algorithm::NavigationMesh::NULL_FACE)
222 node.edges[edgeIndex] = face->edge[edgeIndex];
223 auto c1 = Dali::Vector3(mNavigationMesh->GetFace(p)->center);
224 node.weight[edgeIndex] = (c1 - c0).Length();
230 [[maybe_unused]] static float ccw(const Dali::Vector2& A, const Dali::Vector2& B, const Dali::Vector2& C)
232 return (C.y - A.y) * (B.x - A.x) > (B.y - A.y) * (C.x - A.x);
235 [[maybe_unused]] static bool intersect(const Dali::Vector2& A, const Dali::Vector2& B, const Dali::Vector2& C, const Dali::Vector2& D)
237 return ccw(A, C, D) != ccw(B, C, D) && ccw(A, B, C) != ccw(A, B, D);
240 Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFA::OptimizeWaypoints(WayPointList& waypoints) const
242 WayPointList optimizedWaypoints;
243 optimizedWaypoints.emplace_back(waypoints[0]);
244 optimizedWaypoints.reserve(waypoints.size());
246 auto startIndex = 1u;
248 bool finished = false;
249 for(auto j = 0; !finished; ++j)
251 auto& startWaypoint = optimizedWaypoints.back();
252 const auto& startWaypointData = static_cast<const WayPointData&>(startWaypoint);
254 // add new-last waypoint which will be overriden as long as intersection takes place
255 optimizedWaypoints.emplace_back();
256 for(auto wpIndex = startIndex; wpIndex < waypoints.size(); ++wpIndex)
258 if(wpIndex == waypoints.size() - 1)
260 optimizedWaypoints.back() = waypoints.back();
264 // Points between centres of faces
266 const auto& wpData = static_cast<const WayPointData&>(waypoints[wpIndex]);
268 auto Pa0 = Dali::Vector2(startWaypointData.face->center[0], startWaypointData.face->center[1]);
269 auto Pa1 = Dali::Vector2(wpData.face->center[0], wpData.face->center[1]);
271 bool doesIntersect = true;
272 for(auto i = startIndex; i < wpIndex; ++i)
274 const auto& wp = static_cast<WayPointData&>(waypoints[i]);
275 // Skip starting waypoint
276 if(wp.face == startWaypointData.face)
280 auto Pb0 = mNavigationMesh->GetVertex(wp.edge->vertex[0]);
281 auto Pb1 = mNavigationMesh->GetVertex(wp.edge->vertex[1]);
282 auto vPb0 = Dali::Vector2(Pb0->x, Pb0->y);
283 auto vPb1 = Dali::Vector2(Pb1->x, Pb1->y);
285 doesIntersect = intersect(Pa0, Pa1, vPb0, vPb1);
294 optimizedWaypoints.back() = waypoints[wpIndex - 1];
295 startIndex = wpIndex - 1;
301 for(auto& wp : optimizedWaypoints)
303 auto& wpData = static_cast<WayPointData&>(wp);
304 wpData.point3d = mNavigationMesh->PointLocalToScene(Dali::Vector3(wpData.face->center));
305 wpData.point2d = Vector2::ZERO;
308 return optimizedWaypoints;
310 } // namespace Dali::Scene3D::Internal::Algorithm