useFloorConnectFinder.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import type {Vector3} from "three";
  2. import type {WeightedGraph} from "./WeightedGraph";
  3. export const useFloorConnectFinder = (map_conn_points: Map<string, Map<string, Vector3>>, graph: WeightedGraph) => {
  4. // 找到起始地图到终点地图的所有路线,并过滤出最短路径
  5. const findNavPath = (start_floor: string, end_floor: string, start_pos: Vector3) => {
  6. if (!map_conn_points.has(start_floor) || !map_conn_points.has(end_floor)) return null;
  7. // const vertices = graph.getVertices();
  8. const paths: string[][] = [];
  9. let weight: Record<string, number> = {};
  10. for (const start_point of map_conn_points.get(start_floor)!.keys()) {
  11. for (const end_point of map_conn_points.get(end_floor)!.keys()) {
  12. const {shortest_paths, shortest_paths_weight} = graph.dijkstraAllShortestPaths(start_point, end_point);
  13. if (shortest_paths.length) {
  14. paths.push(...shortest_paths);
  15. weight = {
  16. ...weight,
  17. ...shortest_paths_weight
  18. };
  19. }
  20. }
  21. }
  22. // 找到总权重最小的路径
  23. let min_path_weight = Infinity;
  24. for (const path in weight) {
  25. if (weight[path] < min_path_weight) {
  26. min_path_weight = weight[path];
  27. }
  28. }
  29. const filter_shortest_paths = paths.filter(path => {
  30. return weight[path.join("_")] === min_path_weight;
  31. });
  32. // 找出距离起始坐标位置最近的连通点路径(优化体验)
  33. // let point_distance_to_start_pos = Infinity;
  34. // let distance_to_start_pos_path_key = "";
  35. // filter_shortest_paths.forEach(path => {
  36. // const distance = start_pos.distanceTo(vertices.get(path[0]) as Vector3);
  37. // if (distance < point_distance_to_start_pos) {
  38. // point_distance_to_start_pos = distance;
  39. // distance_to_start_pos_path_key = path.join("_");
  40. // }
  41. // });
  42. return filter_shortest_paths.map(path => ({
  43. path,
  44. weight: min_path_weight,
  45. // nearest_to_current: path.join("_") === distance_to_start_pos_path_key
  46. }));
  47. };
  48. const getPathPointPosition = (path: string[]) => {
  49. const vertices = graph.getVertices();
  50. return path.map(key => {
  51. return {
  52. key,
  53. pos: vertices.get(key) as Vector3
  54. };
  55. });
  56. };
  57. // 根据连通点Key找到所属楼层
  58. const findFloorByKey = (key: string) => {
  59. for (const [floor_key, floor] of map_conn_points.entries()) {
  60. if (floor.has(key)) {
  61. return floor_key;
  62. }
  63. }
  64. };
  65. // 全部连通点Key所属楼层映射
  66. const getConnectPointKeyMap = () => {
  67. const map: Record<string, string> = {};
  68. for (const point_key of graph.getVertices().keys()) {
  69. // @ts-ignore
  70. map[point_key] = findFloorByKey(point_key);
  71. }
  72. return map;
  73. };
  74. return {
  75. findNavPath,
  76. getPathPointPosition,
  77. getConnectPointKeyMap
  78. };
  79. };