GetRoute.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.fdkk.fdkkmeta.util.kesar;
  2. import cn.hutool.core.io.LineHandler;
  3. import cn.hutool.core.io.file.FileReader;
  4. import cn.hutool.core.util.ObjectUtil;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.fdkk.fdkkmeta.domain.dto.RouteDto;
  9. import com.fdkk.fdkkmeta.domain.po.RoutePO;
  10. import lombok.extern.slf4j.Slf4j;
  11. import java.util.*;
  12. @Slf4j
  13. public class GetRoute {
  14. private static final String inputFilePath = "E:\\test\\project\\age_laser\\routeMap.txt";
  15. private static final Double startX = 5.358192084229412;
  16. private static final Double startY = -7.905951689748807;
  17. private static final Double startZ = -1.3145928255248511;
  18. private static final Double endX = -2.143694831230416;
  19. private static final Double endY = -3.3754012098200965;
  20. private static final Double endZ = -1.1803865408990568;
  21. private static RoutePO minStartId = null;
  22. private static RoutePO minEndId = null;
  23. private static Node start = null;
  24. private static Node end = null;
  25. private static final AStar g_AStar = new AStar();
  26. /**
  27. * 读取文件,获取文件内容
  28. * target_freespace.json
  29. * <p>
  30. * list 是routeMap的结果集
  31. *
  32. * @return
  33. */
  34. public static JSONArray getRoute(String initPath, RouteDto dto) {
  35. try {
  36. Map<Integer, RoutePO> maps = GetRoute.readMap(initPath, dto);
  37. MapInfo info = new MapInfo(maps, GetRoute.start, GetRoute.end);
  38. List<Node> path = GetRoute.g_AStar.start(info);
  39. List<Integer> ids = new LinkedList<>();
  40. for (Node node : path) {
  41. ids.add(node.id);
  42. }
  43. GetRoute.log.info("棋盘获取路线点-所有点{}", JSON.toJSONString(ids));
  44. JSONArray jsonArray = GetRoute.convertFromPath(path, dto);
  45. List<Integer> ids2 = new LinkedList<>();
  46. for (Object o : jsonArray) {
  47. JSONObject a = JSON.parseObject(JSONObject.toJSON(o).toString());
  48. if (a.getInteger("id") != null) {
  49. ids2.add(a.getInteger("id"));
  50. }
  51. }
  52. GetRoute.log.info(JSON.toJSONString(ids));
  53. GetRoute.log.info(JSON.toJSONString(ids2));
  54. return jsonArray;
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. return null;
  59. }
  60. public static Map<Integer, RoutePO> readMap(String initPath, RouteDto dto) {
  61. Double startX = dto.getS_location().getX();
  62. Double startY = dto.getS_location().getY();
  63. Double startZ = dto.getS_location().getZ();
  64. Double endX = dto.getE_location().getX();
  65. Double endY = dto.getE_location().getY();
  66. Double endZ = dto.getE_location().getZ();
  67. Coord _start = new Coord(startX, startY, startZ);
  68. Coord _end = new Coord(endX, endY, endZ);
  69. final Double[] startDistance = {1000.0};
  70. final Double[] endDistance = {1000.0};
  71. FileReader fileReader = new FileReader(initPath);
  72. Map<Integer, RoutePO> collect = new HashMap<>();
  73. fileReader.readLines((LineHandler) s -> {
  74. RoutePO po = JSON.toJavaObject(JSON.parseObject(s), RoutePO.class);
  75. Coord coord = new Coord(Double.valueOf(po.getX()), Double.valueOf(po.getY()), Double.valueOf(po.getZ()));
  76. Double _startDistance = GetRoute.g_AStar.calcH(_start, coord);
  77. if (_startDistance <= startDistance[0]) {
  78. GetRoute.minStartId = po;
  79. startDistance[0] = _startDistance;
  80. }
  81. Double _endDistance = GetRoute.g_AStar.calcH(_end, coord);
  82. if (_endDistance <= endDistance[0]) {
  83. GetRoute.minEndId = po;
  84. endDistance[0] = _endDistance;
  85. }
  86. collect.put(po.getId(), po);
  87. });
  88. GetRoute.log.info("数量: {}", collect.size());
  89. GetRoute.log.info("棋盘获取路线点-开始点{},{}", _start.x, _start.y);
  90. GetRoute.log.info("棋盘获取路线点-结束点{},{}", _end.x, _end.y);
  91. // 开始结束点一样就直接返回
  92. if (ObjectUtil.isNull(minStartId)){
  93. GetRoute.log.info("找不到开始点");
  94. return null;
  95. }
  96. if (ObjectUtil.isNull(minEndId)){
  97. GetRoute.log.info("找不到结束点");
  98. return null;
  99. }
  100. if (minStartId.getId()==minEndId.getId()){
  101. GetRoute.log.info("超过1.1米就找不到路径startDistance {},endDistance{}", startDistance[0], endDistance[0]);
  102. GetRoute.log.info("开始最近距离{} ", startDistance[0]);
  103. GetRoute.log.info("结束最近距离{}", endDistance[0]);
  104. return null;
  105. }
  106. Coord startVirtualCoord = new Coord(GetRoute.minStartId.getX(), GetRoute.minStartId.getY(), GetRoute.minStartId.getZ());
  107. Coord endVirtualCoord = new Coord(GetRoute.minEndId.getX(), GetRoute.minEndId.getY(), GetRoute.minEndId.getZ());
  108. Double H = GetRoute.g_AStar.calcH1(startVirtualCoord, endVirtualCoord);
  109. GetRoute.start = new Node(GetRoute.minStartId.getId(), 0.0, startVirtualCoord, null, 0.0, H, GetRoute.minStartId.getIds());
  110. GetRoute.end = new Node(GetRoute.minEndId.getId(), 0.0, endVirtualCoord, null, 0.0, 0.0, GetRoute.minEndId.getIds());
  111. GetRoute.log.info("开始最近距离{}", startDistance[0]);
  112. GetRoute.log.info("结束最近距离{}", endDistance[0]);
  113. GetRoute.log.info("开始最近点{}", GetRoute.minStartId);
  114. GetRoute.log.info("结束最近点{}", GetRoute.minEndId);
  115. return collect;
  116. }
  117. private static JSONArray convertFromPath(List<Node> mappath, RouteDto dto) {
  118. double defaultdistance = 0.36;
  119. if (mappath == null || mappath.size() == 0) {
  120. GetRoute.log.info("convertFromPath,mappath.size ,{}", mappath.size());
  121. return null;
  122. }
  123. JSONArray route = new JSONArray();
  124. //起点不在path上,path的第一个点对应的是格子
  125. JSONObject start = new JSONObject();
  126. start.put("id", GetRoute.start.id);
  127. start.put("location", GetRoute.start.coord);
  128. route.add(start);
  129. Double[] virtualEndPosition = new Double[3];
  130. for (int i = mappath.size() - 1; i >= 0; i--) {
  131. Node node = mappath.get(i);
  132. if (node.id!=GetRoute.start.id&&node.id!=GetRoute.end.id) {
  133. JSONObject item = new JSONObject();
  134. item.put("location", node.coord);
  135. item.put("id", node.id);
  136. route.add(item);
  137. }
  138. }
  139. JSONObject end = new JSONObject();
  140. end.put("id", GetRoute.end.id);
  141. end.put("location", GetRoute.end.coord);
  142. route.add(end);
  143. return route;
  144. }
  145. }