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-double-way.h>
21 #include <dali/public-api/common/vector-wrapper.h>
23 #include <unordered_set>
26 #include <dali-scene3d/internal/algorithm/path-finder-waypoint-data.h>
27 #include <dali-scene3d/public-api/algorithm/path-finder-waypoint.h>
29 using WayPointList = Dali::Scene3D::Algorithm::WayPointList;
30 using FaceNodeIndex = Dali::Scene3D::Internal::Algorithm::PathFinderAlgorithmSPFADoubleWay::FaceNodeIndex;
34 constexpr float PRIORITY_SCALE_FACTOR = 0.7f; ///< The value of heuristic factor that how much will you consider
35 /// direction of source --> target. If 0.0f, we will use only dist.
38 * @brief Get the Component Id object
40 * @param[in,out] components Container of components id stored.
41 * @param[in] index index what we want to get components's id.
42 * @return FaceIndex top-value of this components.
44 FaceNodeIndex GetComponentId(std::vector<FaceNodeIndex>& components, FaceNodeIndex index)
46 if(components[index] == index)
50 // Get my parent's components id, and update myself.
51 FaceNodeIndex ret = GetComponentId(components, components[index]);
52 return components[index] = ret;
56 * @brief Combine two elements by Union-Find algorithm.
58 * @param[in,out] components Container of components id stored.
59 * @param[in,out] componentsLevel Container of components level stored.
60 * @param[in] index0 index of components what we want to be combined.
61 * @param[in] index1 index of components what we want to be combined.
63 void ComponentsCombine(std::vector<FaceNodeIndex>& components, std::vector<FaceNodeIndex>& componentsLevel, FaceNodeIndex index0, FaceNodeIndex index1)
65 FaceNodeIndex ancestor0 = GetComponentId(components, index0);
66 FaceNodeIndex ancestor1 = GetComponentId(components, index1);
67 if(ancestor0 == ancestor1)
72 if(componentsLevel[ancestor0] < componentsLevel[ancestor1])
74 components[ancestor0] = ancestor1;
78 components[ancestor1] = ancestor0;
79 if(componentsLevel[ancestor0] == componentsLevel[ancestor1])
81 ++componentsLevel[ancestor0];
87 namespace Dali::Scene3D::Internal::Algorithm
89 PathFinderAlgorithmSPFADoubleWay::PathFinderAlgorithmSPFADoubleWay(Dali::Scene3D::Algorithm::NavigationMesh& navMesh)
90 : mNavigationMesh(&GetImplementation(navMesh))
95 PathFinderAlgorithmSPFADoubleWay::~PathFinderAlgorithmSPFADoubleWay() = default;
97 float PathFinderAlgorithmSPFADoubleWay::DistancePanaltyCalculate(FaceIndex index) const noexcept
99 return dist[index] - priority[index] * PRIORITY_SCALE_FACTOR;
102 Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(const Dali::Vector3& positionFrom, const Dali::Vector3& positionTo)
104 Dali::Vector3 outPosFrom;
105 FaceIndex polyIndexFrom;
106 auto result = mNavigationMesh->FindFloor(positionFrom, outPosFrom, polyIndexFrom);
108 Scene3D::Algorithm::WayPointList waypoints;
112 Dali::Vector3 outPosTo;
113 FaceIndex polyIndexTo;
114 result = mNavigationMesh->FindFloor(positionTo, outPosTo, polyIndexTo);
119 waypoints = FindPath(polyIndexFrom, polyIndexTo);
121 if(!waypoints.empty())
123 // replace first and last waypoint
124 auto& wpFrom = static_cast<WayPointData&>(waypoints[0]);
125 auto& wpTo = static_cast<WayPointData&>(waypoints.back());
127 Vector2 fromCenter(wpFrom.point3d.x, wpFrom.point3d.y);
128 wpFrom.point3d = outPosFrom;
129 wpFrom.point2d = fromCenter - Vector2(outPosFrom.x, outPosFrom.y);
131 Vector2 toCenter(wpTo.point3d.x, wpTo.point3d.y);
132 wpTo.point3d = outPosTo;
133 wpTo.point2d = toCenter - Vector2(outPosTo.x, outPosTo.y);
138 // Returns waypoints with non-zero size of empty vector in case of failure (no path to be found)
142 Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(FaceIndex sourcePolyIndex, FaceIndex targetPolyIndex)
144 // Fast return if source and target index is same.
145 if(sourcePolyIndex == targetPolyIndex)
147 WayPointList waypoints;
150 auto& wp = static_cast<WayPointData&>(waypoints[0]);
151 wp.face = mNavigationMesh->GetFace(sourcePolyIndex);
152 wp.nodeIndex = sourcePolyIndex;
155 return OptimizeWaypoints(waypoints);
158 // Fast return if source and target index is not in same components.
159 // That mean, there is no path. Return empty list.
160 if(GetComponentId(componentIds, sourcePolyIndex) != GetComponentId(componentIds, targetPolyIndex))
162 return WayPointList();
165 // pair<navimesh FaceIndex, is backward direction>
166 using queueItem = std::pair<FaceIndex, uint8_t>;
168 std::list<queueItem> nodeQueue;
170 std::unordered_set<FaceIndex> usedPolyIndexs[2];
172 // Set distance of source and target
173 dist[sourcePolyIndex] = 0.0f;
174 dist[targetPolyIndex] = 0.0f;
175 priority[sourcePolyIndex] = 0.0f;
176 priority[targetPolyIndex] = 0.0f;
177 queued[sourcePolyIndex] = true;
178 queued[targetPolyIndex] = true;
179 nodeQueue.push_back(std::make_pair(sourcePolyIndex, 0));
180 nodeQueue.push_back(std::make_pair(targetPolyIndex, 1));
181 usedPolyIndexs[0].insert(sourcePolyIndex);
182 usedPolyIndexs[1].insert(targetPolyIndex);
184 bool foundPath = false;
185 FaceIndex forwardEndIndex = Scene3D::Algorithm::NavigationMesh::NULL_FACE;
186 FaceIndex backwardStartIndex = Scene3D::Algorithm::NavigationMesh::NULL_FACE;
188 const auto sourcePos = Dali::Vector3(Face(sourcePolyIndex)->center);
189 const auto targetPos = Dali::Vector3(Face(targetPolyIndex)->center);
190 Vector3 direction = targetPos - sourcePos;
191 direction.Normalize();
193 // Note : we always success to found path since source and target is in same components.
196 // find minimum distance
197 auto minDistIndex = nodeQueue.front().first;
198 auto isBackward = nodeQueue.front().second;
199 nodeQueue.pop_front();
200 queued[minDistIndex] = false;
202 // check the neighbours
203 for(auto i = 0u; i < 3 && !foundPath; ++i)
205 auto nIndex = mNodes[minDistIndex].faces[i];
206 if(nIndex != Scene3D::Algorithm::NavigationMesh::NULL_FACE)
208 if(usedPolyIndexs[!isBackward].count(nIndex))
214 forwardEndIndex = nIndex;
215 backwardStartIndex = minDistIndex;
219 forwardEndIndex = minDistIndex;
220 backwardStartIndex = nIndex;
225 usedPolyIndexs[isBackward].insert(nIndex);
227 auto alt = dist[minDistIndex] + mNodes[minDistIndex].weight[i];
228 if(alt < dist[nIndex])
234 prevBackward[nIndex] = minDistIndex;
235 if(priority[nIndex] < 0.0f)
237 const auto currentPos = Dali::Vector3(Face(nIndex)->center);
238 Vector3 diff = currentPos - targetPos;
239 priority[nIndex] = std::max(0.0f, -direction.Dot(diff));
244 prevForward[nIndex] = minDistIndex;
245 if(priority[nIndex] < 0.0f)
247 const auto currentPos = Dali::Vector3(Face(nIndex)->center);
248 Vector3 diff = currentPos - sourcePos;
249 priority[nIndex] = std::max(0.0f, direction.Dot(diff));
255 queued[nIndex] = true;
256 if(!nodeQueue.empty() && DistancePanaltyCalculate(nIndex) < DistancePanaltyCalculate(nodeQueue.front().first))
258 nodeQueue.push_front(std::make_pair(nIndex, isBackward));
262 nodeQueue.push_back(std::make_pair(nIndex, isBackward));
270 // Build path of face index
271 std::list<FaceIndex> q;
273 FaceIndex u = forwardEndIndex;
274 while(u != Scene3D::Algorithm::NavigationMesh::NULL_FACE)
281 FaceIndex u = backwardStartIndex;
282 while(u != Scene3D::Algorithm::NavigationMesh::NULL_FACE)
289 WayPointList waypoints;
290 waypoints.resize(q.size());
296 auto& wp = static_cast<WayPointData&>(waypoints[index]);
297 wp.face = mNavigationMesh->GetFace(n);
301 // set the common edge with previous node
304 const auto& node = mNodes[prevN];
305 for(auto i = 0u; i < 3; ++i)
307 if(node.faces[i] == wp.nodeIndex)
309 wp.edge = mNavigationMesh->GetEdge(node.edges[i]);
319 // Reset informations what we used.
321 for(const auto& i : usedPolyIndexs[0])
323 dist[i] = std::numeric_limits<float>::infinity();
324 priority[i] = -1.0f; // Initialize by negative value, that we didn't calculate yet.
325 prevForward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
326 prevBackward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
330 for(const auto& i : usedPolyIndexs[1])
332 dist[i] = std::numeric_limits<float>::infinity();
333 priority[i] = -1.0f; // Initialize by negative value, that we didn't calculate yet.
334 prevForward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
335 prevBackward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
339 return OptimizeWaypoints(waypoints);
342 void PathFinderAlgorithmSPFADoubleWay::PrepareData()
344 // Build the list structure connecting the nodes
345 auto faceCount = mNavigationMesh->GetFaceCount();
347 mNodes.resize(faceCount);
348 dist.resize(faceCount);
349 priority.resize(faceCount);
350 prevForward.resize(faceCount);
351 prevBackward.resize(faceCount);
352 componentIds.resize(faceCount);
353 queued.resize(faceCount);
355 // Temperal container for components level. It will be used for Union-Find algorithm.
356 std::vector<FaceNodeIndex> componentLevels(faceCount);
358 // Initialize path informations.
359 for(FaceNodeIndex i = 0u; i < faceCount; ++i)
361 dist[i] = std::numeric_limits<float>::infinity();
362 priority[i] = -1.0f; // Initialize by negative value, that we didn't calculate yet.
363 prevForward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
364 prevBackward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
367 componentIds[i] = i; // Components id should be initialized by itself.
368 componentLevels[i] = 0u;
371 // for each face build the list
372 // TODO : Currently, we are assume that FaceNodeIndex is matched with FaceIndex 1:1. This might be changed in future.
373 for(FaceNodeIndex i = 0u; i < faceCount; ++i)
375 auto& node = mNodes[i];
376 const auto* face = mNavigationMesh->GetFace(i);
377 auto c0 = Dali::Vector3(face->center);
379 // for each edge add neighbouring face and compute distance to set the weight of node
380 for(auto edgeIndex = 0u; edgeIndex < 3; ++edgeIndex)
382 const auto* edge = mNavigationMesh->GetEdge(face->edge[edgeIndex]);
383 auto p1 = edge->face[0];
384 auto p2 = edge->face[1];
386 // One of faces is current face so ignore it
387 auto p = ((p1 != i) ? p1 : p2);
388 node.faces[edgeIndex] = p;
389 if(p != ::Dali::Scene3D::Algorithm::NavigationMesh::NULL_FACE)
391 node.edges[edgeIndex] = face->edge[edgeIndex];
392 auto c1 = Dali::Vector3(mNavigationMesh->GetFace(p)->center);
393 node.weight[edgeIndex] = (c1 - c0).Length();
395 // Connect two components
396 ComponentsCombine(componentIds, componentLevels, i, p);
402 [[maybe_unused]] static float ccw(const Dali::Vector2& A, const Dali::Vector2& B, const Dali::Vector2& C)
404 return (C.y - A.y) * (B.x - A.x) > (B.y - A.y) * (C.x - A.x);
407 [[maybe_unused]] static bool intersect(const Dali::Vector2& A, const Dali::Vector2& B, const Dali::Vector2& C, const Dali::Vector2& D)
409 return ccw(A, C, D) != ccw(B, C, D) && ccw(A, B, C) != ccw(A, B, D);
412 Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::OptimizeWaypoints(WayPointList& waypoints) const
414 WayPointList optimizedWaypoints;
415 optimizedWaypoints.emplace_back(waypoints[0]);
416 optimizedWaypoints.reserve(waypoints.size());
418 auto startIndex = 1u;
420 bool finished = false;
421 for(auto j = 0; !finished; ++j)
423 auto& startWaypoint = optimizedWaypoints.back();
424 const auto& startWaypointData = static_cast<const WayPointData&>(startWaypoint);
426 // add new-last waypoint which will be overriden as long as intersection takes place
427 optimizedWaypoints.emplace_back();
428 for(auto wpIndex = startIndex; wpIndex < waypoints.size(); ++wpIndex)
430 if(wpIndex == waypoints.size() - 1)
432 optimizedWaypoints.back() = waypoints.back();
436 // Points between centres of faces
438 const auto& wpData = static_cast<const WayPointData&>(waypoints[wpIndex]);
440 auto Pa0 = Dali::Vector2(startWaypointData.face->center[0], startWaypointData.face->center[1]);
441 auto Pa1 = Dali::Vector2(wpData.face->center[0], wpData.face->center[1]);
443 bool doesIntersect = true;
444 for(auto i = startIndex; i < wpIndex; ++i)
446 const auto& wp = static_cast<WayPointData&>(waypoints[i]);
447 // Skip starting waypoint
448 if(wp.face == startWaypointData.face)
452 auto Pb0 = mNavigationMesh->GetVertex(wp.edge->vertex[0]);
453 auto Pb1 = mNavigationMesh->GetVertex(wp.edge->vertex[1]);
454 auto vPb0 = Dali::Vector2(Pb0->x, Pb0->y);
455 auto vPb1 = Dali::Vector2(Pb1->x, Pb1->y);
457 doesIntersect = intersect(Pa0, Pa1, vPb0, vPb1);
466 optimizedWaypoints.back() = waypoints[wpIndex - 1];
467 startIndex = wpIndex - 1;
473 for(auto& wp : optimizedWaypoints)
475 auto& wpData = static_cast<WayPointData&>(wp);
476 wpData.point3d = mNavigationMesh->PointLocalToScene(Dali::Vector3(wpData.face->center));
477 wpData.point2d = Vector2::ZERO;
480 return optimizedWaypoints;
482 } // namespace Dali::Scene3D::Internal::Algorithm