AddRoad.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import { listenLayer } from "../ListenLayer";
  2. import Constant from "../Constant";
  3. // import { mathUtil } from "../MathUtil";
  4. import { dataService } from "../Service/DataService";
  5. import { pointService } from "../Service/PointService";
  6. import { elementService } from "../Service/ElementService";
  7. // import { wallService } from "../Service/WallService";
  8. import { stateService } from "../Service/StateService";
  9. import { symbolService } from "../Service/SymbolService";
  10. import VectorType from "../enum/VectorType";
  11. import { roadService } from "../Service/RoadService";
  12. export default class AddRoad {
  13. constructor() {
  14. this.startInfo = {};
  15. this.endInfo = {};
  16. this.canAdd = false;
  17. }
  18. setPointInfo(dir, pointInfo) {
  19. if (dir == "start") {
  20. this.startInfo = {
  21. position: {
  22. x: pointInfo.x,
  23. y: pointInfo.y,
  24. },
  25. linkedPointId: pointInfo.linkedPointId,
  26. linkedRoadId: pointInfo.linkedRoadId,
  27. };
  28. } else if (dir == "end") {
  29. this.endInfo = {
  30. position: {
  31. x: pointInfo.x,
  32. y: pointInfo.y,
  33. },
  34. linkedPointId: pointInfo.linkedPointId,
  35. linkedRoadId: pointInfo.linkedRoadId,
  36. };
  37. }
  38. }
  39. buildRoad() {
  40. console.log("添加路段!");
  41. let joinInfos = this.getJoinsForRoads();
  42. let splitPointIds = this.splitAllJoins(joinInfos);
  43. this.creatNewRoads(splitPointIds);
  44. this.canAdd = false;
  45. this.updateStart(
  46. this.endInfo.position,
  47. splitPointIds[splitPointIds.length - 1]
  48. );
  49. listenLayer.clear();
  50. }
  51. //开始新建公路,可能是多个
  52. creatNewRoads(splitPointIds) {
  53. for (let i = 0; i < splitPointIds.length - 1; ++i) {
  54. let pointId1 = splitPointIds[i];
  55. let pointId2 = splitPointIds[i + 1];
  56. let roadId = roadService.getRoadId(pointId1, pointId2);
  57. if (!roadId) {
  58. let road = roadService.create(pointId1, pointId2);
  59. }
  60. }
  61. }
  62. canAddRoadForEnd(endPt) {
  63. if (listenLayer.modifyPoint) {
  64. if (
  65. mathUtil.getDistance(this.startInfo.position, listenLayer.modifyPoint) <
  66. Constant.minAdsorb
  67. ) {
  68. return false;
  69. }
  70. } else {
  71. if (
  72. mathUtil.getDistance(this.startInfo.position, endPt) <
  73. Constant.minAdsorb
  74. ) {
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. getJoinsForRoads() {
  81. let result = [];
  82. const roads = dataService.getRoads();
  83. let hasComputerPointIds = [];
  84. let hasExitPointIds = [];
  85. if (this.startInfo.linkedPointId) {
  86. hasComputerPointIds.push(this.startInfo.linkedPointId);
  87. hasExitPointIds.push(this.startInfo.linkedPointId);
  88. result.push({
  89. join: { x: this.startInfo.position.x, y: this.startInfo.position.y },
  90. pointId: this.startInfo.linkedPointId,
  91. });
  92. } else if (this.startInfo.linkedRoadId) {
  93. result.push({
  94. join: { x: this.startInfo.position.x, y: this.startInfo.position.y },
  95. roadId: this.startInfo.linkedRoadId,
  96. });
  97. } else {
  98. result.push({
  99. join: { x: this.startInfo.position.x, y: this.startInfo.position.y },
  100. });
  101. }
  102. if (this.endInfo.linkedPointId) {
  103. hasComputerPointIds.push(this.endInfo.linkedPointId);
  104. hasExitPointIds.push(this.endInfo.linkedPointId);
  105. result.push({
  106. join: { x: this.endInfo.position.x, y: this.endInfo.position.y },
  107. pointId: this.endInfo.linkedPointId,
  108. });
  109. } else if (this.endInfo.linkedRoadId) {
  110. result.push({
  111. join: { x: this.endInfo.position.x, y: this.endInfo.position.y },
  112. roadId: this.endInfo.linkedRoadId,
  113. });
  114. } else {
  115. result.push({
  116. join: { x: this.endInfo.position.x, y: this.endInfo.position.y },
  117. });
  118. }
  119. const line = mathUtil.createLine1(
  120. this.startInfo.position,
  121. this.endInfo.position
  122. );
  123. for (let key in roads) {
  124. if (this.startInfo.linkedRoadId && this.startInfo.linkedRoadId == key) {
  125. continue;
  126. } else if (
  127. this.endInfo.linkedRoadId &&
  128. this.endInfo.linkedRoadId == key
  129. ) {
  130. continue;
  131. }
  132. let road = roads[key];
  133. if (
  134. hasExitPointIds.indexOf(road.startId) > -1 ||
  135. hasExitPointIds.indexOf(road.endId) > -1
  136. ) {
  137. continue;
  138. }
  139. let startPoint = dataService.getPoint(road.startId);
  140. let endPoint = dataService.getPoint(road.endId);
  141. let flag = false;
  142. if (hasComputerPointIds.indexOf(road.startId) < 0) {
  143. hasComputerPointIds.push(road.startId);
  144. const startJoin = mathUtil.getJoinLinePoint(startPoint, line);
  145. if (
  146. mathUtil.PointInSegment(
  147. startJoin,
  148. this.startInfo.position,
  149. this.endInfo.position,
  150. Constant.minAdsorb
  151. ) &&
  152. mathUtil.getDistance(startPoint, startJoin) < Constant.minAdsorb
  153. ) {
  154. result.push({
  155. join: { x: startJoin.x, y: startJoin.y },
  156. pointId: road.startId,
  157. });
  158. flag = true;
  159. hasExitPointIds.push(road.startId);
  160. }
  161. }
  162. if (hasComputerPointIds.indexOf(road.endId) < 0) {
  163. hasComputerPointIds.push(road.endId);
  164. const endJoin = mathUtil.getJoinLinePoint(endPoint, line);
  165. if (
  166. mathUtil.PointInSegment(
  167. endJoin,
  168. this.startInfo.position,
  169. this.endInfo.position,
  170. Constant.minAdsorb
  171. ) &&
  172. mathUtil.getDistance(endPoint, endJoin) < Constant.minAdsorb
  173. ) {
  174. result.push({
  175. join: { x: endJoin.x, y: endJoin.y },
  176. pointId: road.endId,
  177. });
  178. flag = true;
  179. hasExitPointIds.push(road.endId);
  180. }
  181. }
  182. if (!flag) {
  183. //不与墙key的端点相交
  184. if (
  185. hasExitPointIds.indexOf(road.startId) < 0 &&
  186. hasExitPointIds.indexOf(road.endId) < 0
  187. ) {
  188. let join = mathUtil.getIntersectionPoint3(
  189. startPoint,
  190. endPoint,
  191. this.startInfo.position,
  192. this.endInfo.position
  193. );
  194. if (join) {
  195. result.push({ join: { x: join.x, y: join.y }, roadId: key });
  196. }
  197. }
  198. }
  199. }
  200. return result;
  201. }
  202. splitAllJoins(joinInfos) {
  203. // 对joinInfos要进行排序
  204. joinInfos = joinInfos.sort(sortNumber.bind(this));
  205. function sortNumber(a, b) {
  206. return (
  207. mathUtil.getDistance(this.startInfo.position, a.join) -
  208. mathUtil.getDistance(this.startInfo.position, b.join)
  209. );
  210. }
  211. const splitPointIds = [];
  212. for (let i = 0; i < joinInfos.length; ++i) {
  213. const info = joinInfos[i];
  214. const join = info.join;
  215. const roadId = info.roadId;
  216. const pointId = info.pointId;
  217. if (pointId) {
  218. splitPointIds.push(pointId);
  219. } else if (roadId) {
  220. // 拆分roadId
  221. const splitPoint = pointService.create(join);
  222. const splitPointId = splitPoint.vectorId;
  223. //可能joinInfos的两个点都在roadId上
  224. let newRoadId = null;
  225. if (
  226. joinInfos[i + 1] &&
  227. joinInfos[i].roadId == joinInfos[i + 1].roadId
  228. ) {
  229. let road = dataService.getRoad(roadId);
  230. let startPoint = dataService.getPoint(road.startId);
  231. if (
  232. mathUtil.getDistance(startPoint, joinInfos[i].join) <
  233. mathUtil.getDistance(startPoint, joinInfos[i + 1].join)
  234. ) {
  235. newRoadId = roadService.splitRoad(roadId, splitPointId, "end");
  236. } else {
  237. newRoadId = roadService.splitRoad(roadId, splitPointId, "start");
  238. }
  239. } else {
  240. newRoadId = roadService.splitRoad(roadId, splitPointId, "start");
  241. }
  242. if (newRoadId == null) {
  243. dataService.deletePoint(splitPointId);
  244. continue;
  245. }
  246. splitPointIds.push(splitPointId);
  247. }
  248. //首尾没有吸附的情况
  249. else {
  250. const splitPoint = pointService.create(join);
  251. splitPointIds.push(splitPoint.vectorId);
  252. }
  253. }
  254. return splitPointIds;
  255. }
  256. updateStart(position, linkedPointId) {
  257. mathUtil.clonePoint(this.startInfo.position, position);
  258. this.startInfo.linkedPointId = linkedPointId;
  259. //elementService.setNewWallStartPosition(this.startInfo.position);
  260. }
  261. clear() {
  262. this.startInfo = {};
  263. this.endInfo = {};
  264. this.canAdd = false;
  265. //elementService.hideNewWall();
  266. //elementService.hideStartAddWall();
  267. }
  268. }
  269. const addRoad = new AddRoad();
  270. export { addRoad };