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>
19 #include <dali-scene3d/internal/algorithm/path-finder-waypoint-data.h>
20 #include <dali-scene3d/public-api/algorithm/path-finder-waypoint.h>
24 #include <unordered_set>
27 using WayPointList = Dali::Scene3D::Algorithm::WayPointList;
31 constexpr float PRIORITY_SCALE_FACTOR = 0.7f; ///< The value of heuristic factor that how much will you consider
32 /// direction of source --> target. If 0.0f, we will use only dist.
35 * @brief Get the Component Id object
37 * @param[in,out] components Container of components id stored.
38 * @param[in] index index what we want to get components's id.
39 * @return uint32_t top-value of this components.
41 uint32_t GetComponentId(std::vector<uint32_t>& components, uint32_t index)
43 if(components[index] == index)
47 // Get my parent's components id, and update myself.
48 uint32_t ret = GetComponentId(components, components[index]);
49 return components[index] = ret;
53 * @brief Combine two elements by Union-Find algorithm.
55 * @param[in,out] components Container of components id stored.
56 * @param[in,out] componentsLevel Container of components level stored.
57 * @param[in] index0 index of components what we want to be combined.
58 * @param[in] index1 index of components what we want to be combined.
60 void ComponentsCombine(std::vector<uint32_t>& components, std::vector<uint32_t>& componentsLevel, uint32_t index0, uint32_t index1)
62 uint32_t p0 = GetComponentId(components, index0);
63 uint32_t p1 = GetComponentId(components, index1);
69 if(componentsLevel[p0] < componentsLevel[p1])
76 if(componentsLevel[p0] == componentsLevel[p1])
78 ++componentsLevel[p0];
84 namespace Dali::Scene3D::Internal::Algorithm
86 PathFinderAlgorithmSPFADoubleWay::PathFinderAlgorithmSPFADoubleWay(Dali::Scene3D::Algorithm::NavigationMesh& navMesh)
87 : mNavigationMesh(&GetImplementation(navMesh))
92 PathFinderAlgorithmSPFADoubleWay::~PathFinderAlgorithmSPFADoubleWay() = default;
94 float PathFinderAlgorithmSPFADoubleWay::DistancePanaltyCalculate(uint32_t index) const noexcept
96 return dist[index] - priority[index] * PRIORITY_SCALE_FACTOR;
99 Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(const Dali::Vector3& positionFrom, const Dali::Vector3& positionTo)
101 Dali::Vector3 outPosFrom;
102 uint32_t polyIndexFrom;
103 auto result = mNavigationMesh->FindFloor(positionFrom, outPosFrom, polyIndexFrom);
105 Scene3D::Algorithm::WayPointList waypoints;
109 Dali::Vector3 outPosTo;
110 uint32_t polyIndexTo;
111 result = mNavigationMesh->FindFloor(positionTo, outPosTo, polyIndexTo);
116 waypoints = FindPath(polyIndexFrom, polyIndexTo);
118 // replace first and last waypoint
119 auto& wpFrom = static_cast<WayPointData&>(waypoints[0]);
120 auto& wpTo = static_cast<WayPointData&>(waypoints.back());
122 Vector2 fromCenter(wpFrom.point3d.x, wpFrom.point3d.y);
123 wpFrom.point3d = outPosFrom;
124 wpFrom.point2d = fromCenter - Vector2(outPosFrom.x, outPosFrom.y);
126 Vector2 toCenter(wpTo.point3d.x, wpTo.point3d.y);
127 wpTo.point3d = outPosTo;
128 wpTo.point2d = toCenter - Vector2(outPosTo.x, outPosTo.y);
129 wpTo.point3d = outPosTo;
133 // Returns waypoints with non-zero size of empty vector in case of failure (no path to be found)
137 Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::FindPath(uint32_t sourcePolyIndex, uint32_t targetPolyIndex)
139 // Fast return if source and target index is same.
140 if(sourcePolyIndex == targetPolyIndex)
142 WayPointList waypoints;
145 auto& wp = static_cast<WayPointData&>(waypoints[0]);
146 wp.face = mNavigationMesh->GetFace(sourcePolyIndex);
147 wp.nodeIndex = sourcePolyIndex;
150 return OptimizeWaypoints(waypoints);
153 // Fast return if source and target index is not in same components.
154 // That mean, there is no path. Return empty list.
155 if(GetComponentId(componentIds, sourcePolyIndex) != GetComponentId(componentIds, targetPolyIndex))
157 return WayPointList();
160 // pair<navimesh FaceIndex, is backward direction>
161 using queueItem = std::pair<uint32_t, uint8_t>;
163 std::list<queueItem> nodeQueue;
165 std::unordered_set<uint32_t> usedPolyIndexs[2];
167 // Set distance of source and target
168 dist[sourcePolyIndex] = 0.0f;
169 dist[targetPolyIndex] = 0.0f;
170 priority[sourcePolyIndex] = 0.0f;
171 priority[targetPolyIndex] = 0.0f;
172 queued[sourcePolyIndex] = true;
173 queued[targetPolyIndex] = true;
174 nodeQueue.push_back(std::make_pair(sourcePolyIndex, 0));
175 nodeQueue.push_back(std::make_pair(targetPolyIndex, 1));
176 usedPolyIndexs[0].insert(sourcePolyIndex);
177 usedPolyIndexs[1].insert(targetPolyIndex);
179 bool foundPath = false;
180 uint32_t forwardEndIndex = Scene3D::Algorithm::NavigationMesh::NULL_FACE;
181 uint32_t backwardStartIndex = Scene3D::Algorithm::NavigationMesh::NULL_FACE;
183 const auto sourcePos = Dali::Vector3(Face(sourcePolyIndex)->center);
184 const auto targetPos = Dali::Vector3(Face(targetPolyIndex)->center);
185 Vector3 direction = targetPos - sourcePos;
186 direction.Normalize();
188 // Note : we always success to found path since source and target is in same components.
191 // find minimum distance
192 auto minDistIndex = nodeQueue.front().first;
193 auto isBackward = nodeQueue.front().second;
194 nodeQueue.pop_front();
195 queued[minDistIndex] = false;
197 // check the neighbours
198 for(auto i = 0u; i < 3 && !foundPath; ++i)
200 auto nIndex = mNodes[minDistIndex].faces[i];
201 if(nIndex != Scene3D::Algorithm::NavigationMesh::NULL_FACE)
203 if(usedPolyIndexs[!isBackward].count(nIndex))
209 forwardEndIndex = nIndex;
210 backwardStartIndex = minDistIndex;
214 forwardEndIndex = minDistIndex;
215 backwardStartIndex = nIndex;
220 usedPolyIndexs[isBackward].insert(nIndex);
222 auto alt = dist[minDistIndex] + mNodes[minDistIndex].weight[i];
223 if(alt < dist[nIndex])
229 prevBackward[nIndex] = minDistIndex;
230 if(priority[nIndex] < 0.0f)
232 const auto currentPos = Dali::Vector3(Face(nIndex)->center);
233 Vector3 diff = currentPos - targetPos;
234 priority[nIndex] = std::max(0.0f, -direction.Dot(diff));
239 prevForward[nIndex] = minDistIndex;
240 if(priority[nIndex] < 0.0f)
242 const auto currentPos = Dali::Vector3(Face(nIndex)->center);
243 Vector3 diff = currentPos - sourcePos;
244 priority[nIndex] = std::max(0.0f, direction.Dot(diff));
250 queued[nIndex] = true;
251 if(!nodeQueue.empty() && DistancePanaltyCalculate(nIndex) < DistancePanaltyCalculate(nodeQueue.front().first))
253 nodeQueue.push_front(std::make_pair(nIndex, isBackward));
257 nodeQueue.push_back(std::make_pair(nIndex, isBackward));
265 // Build path of face index
266 std::list<uint32_t> q;
268 uint32_t u = forwardEndIndex;
269 while(u != Scene3D::Algorithm::NavigationMesh::NULL_FACE)
276 uint32_t u = backwardStartIndex;
277 while(u != Scene3D::Algorithm::NavigationMesh::NULL_FACE)
284 WayPointList waypoints;
285 waypoints.resize(q.size());
291 auto& wp = static_cast<WayPointData&>(waypoints[index]);
292 wp.face = mNavigationMesh->GetFace(n);
296 // set the common edge with previous node
299 const auto& node = mNodes[prevN];
300 for(auto i = 0u; i < 3; ++i)
302 if(node.faces[i] == wp.nodeIndex)
304 wp.edge = mNavigationMesh->GetEdge(node.edges[i]);
314 // Reset informations what we used.
316 for(const auto& i : usedPolyIndexs[0])
318 dist[i] = std::numeric_limits<float>::infinity();
319 priority[i] = -1.0f; // Initialize by negative value, that we didn't calculate yet.
320 prevForward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
321 prevBackward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
325 for(const auto& i : usedPolyIndexs[1])
327 dist[i] = std::numeric_limits<float>::infinity();
328 priority[i] = -1.0f; // Initialize by negative value, that we didn't calculate yet.
329 prevForward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
330 prevBackward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
334 return OptimizeWaypoints(waypoints);
337 void PathFinderAlgorithmSPFADoubleWay::PrepareData()
339 // Build the list structure connecting the nodes
340 auto faceCount = mNavigationMesh->GetFaceCount();
342 mNodes.resize(faceCount);
343 dist.resize(faceCount);
344 priority.resize(faceCount);
345 prevForward.resize(faceCount);
346 prevBackward.resize(faceCount);
347 componentIds.resize(faceCount);
348 queued.resize(faceCount);
350 // Temperal container for components level. It will be used for Union-Find algorithm.
351 std::vector<uint32_t> componentLevels(faceCount);
353 // Initialize path informations.
354 for(auto i = 0u; i < faceCount; ++i)
356 dist[i] = std::numeric_limits<float>::infinity();
357 priority[i] = -1.0f; // Initialize by negative value, that we didn't calculate yet.
358 prevForward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
359 prevBackward[i] = Scene3D::Algorithm::NavigationMesh::NULL_FACE; // set prev to null polygon
362 componentIds[i] = i; // Components id should be initialized by itself.
363 componentLevels[i] = 0u;
366 // for each face build the list
367 for(auto i = 0u; i < faceCount; ++i)
369 auto& node = mNodes[i];
370 const auto* face = mNavigationMesh->GetFace(i);
371 auto c0 = Dali::Vector3(face->center);
373 // for each edge add neighbouring face and compute distance to set the weight of node
374 for(auto edgeIndex = 0u; edgeIndex < 3; ++edgeIndex)
376 const auto* edge = mNavigationMesh->GetEdge(face->edge[edgeIndex]);
377 auto p1 = edge->face[0];
378 auto p2 = edge->face[1];
380 // One of faces is current face so ignore it
381 auto p = ((p1 != i) ? p1 : p2);
382 node.faces[edgeIndex] = p;
383 if(p != ::Dali::Scene3D::Algorithm::NavigationMesh::NULL_FACE)
385 node.edges[edgeIndex] = face->edge[edgeIndex];
386 auto c1 = Dali::Vector3(mNavigationMesh->GetFace(p)->center);
387 node.weight[edgeIndex] = (c1 - c0).Length();
389 // Connect two components
390 ComponentsCombine(componentIds, componentLevels, i, p);
396 [[maybe_unused]] static float ccw(const Dali::Vector2& A, const Dali::Vector2& B, const Dali::Vector2& C)
398 return (C.y - A.y) * (B.x - A.x) > (B.y - A.y) * (C.x - A.x);
401 [[maybe_unused]] static bool intersect(const Dali::Vector2& A, const Dali::Vector2& B, const Dali::Vector2& C, const Dali::Vector2& D)
403 return ccw(A, C, D) != ccw(B, C, D) && ccw(A, B, C) != ccw(A, B, D);
406 Scene3D::Algorithm::WayPointList PathFinderAlgorithmSPFADoubleWay::OptimizeWaypoints(WayPointList& waypoints) const
408 WayPointList optimizedWaypoints;
409 optimizedWaypoints.emplace_back(waypoints[0]);
410 optimizedWaypoints.reserve(waypoints.size());
412 auto startIndex = 1u;
414 bool finished = false;
415 for(auto j = 0; !finished; ++j)
417 auto& startWaypoint = optimizedWaypoints.back();
418 const auto& startWaypointData = static_cast<const WayPointData&>(startWaypoint);
420 // add new-last waypoint which will be overriden as long as intersection takes place
421 optimizedWaypoints.emplace_back();
422 for(auto wpIndex = startIndex; wpIndex < waypoints.size(); ++wpIndex)
424 if(wpIndex == waypoints.size() - 1)
426 optimizedWaypoints.back() = waypoints.back();
430 // Points between centres of faces
432 const auto& wpData = static_cast<const WayPointData&>(waypoints[wpIndex]);
434 auto Pa0 = Dali::Vector2(startWaypointData.face->center[0], startWaypointData.face->center[1]);
435 auto Pa1 = Dali::Vector2(wpData.face->center[0], wpData.face->center[1]);
437 bool doesIntersect = true;
438 for(auto i = startIndex; i < wpIndex; ++i)
440 const auto& wp = static_cast<WayPointData&>(waypoints[i]);
441 // Skip starting waypoint
442 if(wp.face == startWaypointData.face)
446 auto Pb0 = mNavigationMesh->GetVertex(wp.edge->vertex[0]);
447 auto Pb1 = mNavigationMesh->GetVertex(wp.edge->vertex[1]);
448 auto vPb0 = Dali::Vector2(Pb0->x, Pb0->y);
449 auto vPb1 = Dali::Vector2(Pb1->x, Pb1->y);
451 doesIntersect = intersect(Pa0, Pa1, vPb0, vPb1);
460 optimizedWaypoints.back() = waypoints[wpIndex - 1];
461 startIndex = wpIndex - 1;
467 for(auto& wp : optimizedWaypoints)
469 auto& wpData = static_cast<WayPointData&>(wp);
470 wpData.point3d = mNavigationMesh->PointLocalToScene(Dali::Vector3(wpData.face->center));
471 wpData.point2d = Vector2::ZERO;
474 return optimizedWaypoints;
476 } // namespace Dali::Scene3D::Internal::Algorithm