ListenLayer.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. import { mathUtil } from "./Util/MathUtil";
  2. import { dataService } from "./Service/DataService.js";
  3. import { stateService } from "./Service/StateService.js";
  4. import { roadService } from "./Service/RoadService.js";
  5. import Constant from "./Constant.js";
  6. import VectorType from "./enum/VectorType.js";
  7. import SelectState from "./enum/SelectState.js";
  8. import bezierUtil from "./Util/bezierUtil.js";
  9. import { elementService } from "./Service/ElementService";
  10. import { coordinate } from "./Coordinate";
  11. import { draw } from "./Renderer/Draw.js";
  12. export default class ListenLayer {
  13. constructor() {
  14. this.roadInfo = {
  15. roadId: null,
  16. type: null,
  17. state: null,
  18. };
  19. this.curveRoadInfo = {
  20. curveRoadId: null,
  21. type: null,
  22. state: null,
  23. };
  24. this.pointInfo = {
  25. pointId: null,
  26. type: null,
  27. state: null,
  28. };
  29. this.curvePointInfo = {
  30. curvePointId: null,
  31. type: null,
  32. state: null,
  33. };
  34. this.tagInfo = {
  35. tagId: null,
  36. state: null,
  37. };
  38. this.modifyPoint = null;
  39. // this.testStart = null;
  40. // this.testEnd = null;
  41. // this.testHit = false;
  42. }
  43. //开始监听,exceptPointId表示不考虑的点,exceptRoadIds表示不考虑的墙
  44. start(
  45. position,
  46. exceptPointId,
  47. exceptRoadIds,
  48. exceptCurvePointId,
  49. exceptCurveRoadId
  50. ) {
  51. let isSame = false;
  52. this.clear();
  53. let curveRoadInfo = this.isSelectCurveRoad(position, exceptCurveRoadId);
  54. let roadInfo = this.isSelectRoad(position, exceptRoadIds);
  55. let curvePointInfo = this.isSelectCurvePoint(position, exceptCurvePointId);
  56. let pointInfo = this.isSelectPoint(position, exceptPointId);
  57. this.setModifyPoint(
  58. position,
  59. pointInfo,
  60. curvePointInfo,
  61. roadInfo,
  62. curveRoadInfo
  63. );
  64. // let flag1 = this.equalAndClone(this.curveRoadInfo, curveRoadInfo);
  65. // let flag2 = this.equalAndClone(this.roadInfo, roadInfo);
  66. // let flag3 = this.equalAndClone(this.curvePointInfo, curvePointInfo);
  67. // let flag4 = this.equalAndClone(this.pointInfo, pointInfo);
  68. // isSame = this.updateSelectItem(flag1, flag2, flag3, flag4);
  69. isSame = this.updateSelectItem();
  70. return !isSame;
  71. }
  72. isSelectPoint(position, exceptPointId) {
  73. let pointInfo = {
  74. pointId: null,
  75. type: null,
  76. distance: null,
  77. };
  78. let seqInfo = {};
  79. const points = dataService.getPoints();
  80. for (const pointId in points) {
  81. if (pointId == exceptPointId) {
  82. continue;
  83. }
  84. const point = dataService.getPoint(pointId);
  85. const distance = mathUtil.getDistance(position, point);
  86. if (distance < Constant.minAdsorbPix) {
  87. if (pointInfo.pointId == null) {
  88. pointInfo = {
  89. pointId: pointId,
  90. type: VectorType.Point,
  91. distance: distance,
  92. };
  93. } else if (pointInfo.pointId != null) {
  94. if (distance < pointInfo.distance) {
  95. pointInfo = {
  96. pointId: pointId,
  97. type: VectorType.Point,
  98. distance: distance,
  99. };
  100. }
  101. }
  102. } else {
  103. if (Math.abs(position.x - point.x) < Constant.minAdsorbPix) {
  104. seqInfo.linkedPointIdX = pointId;
  105. seqInfo.x = point.x;
  106. } else if (Math.abs(position.y - point.y) < Constant.minAdsorbPix) {
  107. seqInfo.linkedPointIdY = pointId;
  108. seqInfo.y = point.y;
  109. }
  110. }
  111. }
  112. if (pointInfo.pointId) {
  113. const linkedPoint = dataService.getPoint(pointInfo.pointId);
  114. pointInfo.x = linkedPoint.x;
  115. pointInfo.y = linkedPoint.y;
  116. }
  117. //因为这种纠正的权限最低
  118. else {
  119. if (seqInfo.hasOwnProperty("linkedPointIdX")) {
  120. pointInfo.linkedPointIdX = seqInfo.linkedPointIdX;
  121. pointInfo.x = seqInfo.x;
  122. }
  123. if (seqInfo.hasOwnProperty("linkedPointIdY")) {
  124. pointInfo.linkedPointIdY = seqInfo.linkedPointIdY;
  125. pointInfo.y = seqInfo.y;
  126. }
  127. if (pointInfo.hasOwnProperty("y") && !pointInfo.hasOwnProperty("x")) {
  128. pointInfo.x = position.x;
  129. }
  130. if (pointInfo.hasOwnProperty("x") && !pointInfo.hasOwnProperty("y")) {
  131. pointInfo.y = position.y;
  132. }
  133. }
  134. return pointInfo;
  135. }
  136. isSelectCurvePoint(position, exceptCurvePointId) {
  137. let curvePointInfo = {
  138. curvePointId: null,
  139. type: null,
  140. distance: null,
  141. };
  142. let seqInfo = {};
  143. const curvePoints = dataService.getCurvePoints();
  144. for (const curvePointId in curvePoints) {
  145. if (curvePointId == exceptCurvePointId) {
  146. continue;
  147. }
  148. const curvePoint = dataService.getCurvePoint(curvePointId);
  149. const distance = mathUtil.getDistance(position, curvePoint);
  150. if (distance < Constant.minAdsorbPix) {
  151. if (curvePointInfo.curvePointId == null) {
  152. curvePointInfo = {
  153. curvePointId: curvePointId,
  154. type: VectorType.CurvePoint,
  155. distance: distance,
  156. };
  157. } else if (curvePointInfo.pointId != null) {
  158. if (distance < curvePointInfo.distance) {
  159. curvePointInfo = {
  160. curvePointId: curvePointId,
  161. type: VectorType.CurvePoint,
  162. distance: distance,
  163. };
  164. }
  165. }
  166. } else {
  167. if (Math.abs(position.x - curvePoint.x) < Constant.minAdsorbPix) {
  168. seqInfo.linkedPointIdX = curvePointId;
  169. seqInfo.x = curvePoint.x;
  170. } else if (
  171. Math.abs(position.y - curvePoint.y) < Constant.minAdsorbPix
  172. ) {
  173. seqInfo.linkedPointIdY = curvePointId;
  174. seqInfo.y = curvePoint.y;
  175. }
  176. }
  177. }
  178. if (curvePointInfo.curvePointId) {
  179. curvePointInfo.linkedCurvePointId = curvePointInfo.curvePointId;
  180. const linkedCurvePoint = dataService.getCurvePoint(
  181. curvePointInfo.curvePointId
  182. );
  183. curvePointInfo.x = linkedCurvePoint.x;
  184. curvePointInfo.y = linkedCurvePoint.y;
  185. } else {
  186. if (seqInfo.hasOwnProperty("linkedPointIdX")) {
  187. curvePointInfo.linkedPointIdX = seqInfo.linkedPointIdX;
  188. curvePointInfo.x = seqInfo.x;
  189. } else if (seqInfo.hasOwnProperty("linkedPointIdY")) {
  190. curvePointInfo.linkedPointIdY = seqInfo.linkedPointIdY;
  191. curvePointInfo.y = seqInfo.y;
  192. }
  193. if (
  194. curvePointInfo.hasOwnProperty("y") &&
  195. !curvePointInfo.hasOwnProperty("x")
  196. ) {
  197. curvePointInfo.x = position.x;
  198. }
  199. if (
  200. curvePointInfo.hasOwnProperty("x") &&
  201. !curvePointInfo.hasOwnProperty("y")
  202. ) {
  203. curvePointInfo.y = position.y;
  204. }
  205. }
  206. return curvePointInfo;
  207. }
  208. isSelectRoad(position, exceptRoadIds) {
  209. let roadInfo = {
  210. roadId: null,
  211. type: null,
  212. distance: null,
  213. };
  214. const roads = dataService.getRoads();
  215. for (const roadId in roads) {
  216. if (exceptRoadIds != null && exceptRoadIds.hasOwnProperty(roadId)) {
  217. continue;
  218. }
  219. const road = dataService.getRoad(roadId);
  220. let startPoint = dataService.getPoint(road.startId);
  221. let endPoint = dataService.getPoint(road.endId);
  222. const roadLine = roadService.getMidLine(road);
  223. const join = mathUtil.getJoinLinePoint(position, roadLine);
  224. const distance = mathUtil.getDistance(position, join);
  225. if (
  226. distance < road.width / 2 &&
  227. mathUtil.isContainForSegment(join, startPoint, endPoint)
  228. ) {
  229. if (roadInfo.roadId == null) {
  230. roadInfo = {
  231. roadId: roadId,
  232. type: VectorType.Road,
  233. distance: distance,
  234. };
  235. } else if (roadInfo.roadId != null) {
  236. if (distance < roadInfo.distance) {
  237. roadInfo = {
  238. roadId: roadId,
  239. type: VectorType.Road,
  240. distance: mathUtil.getDisForPoinLine(position, roadLine),
  241. };
  242. }
  243. }
  244. }
  245. }
  246. if (roadInfo.roadId) {
  247. const linkedRoad = dataService.getRoad(roadInfo.roadId);
  248. const linkedRoadLine = roadService.getMidLine(linkedRoad);
  249. const linkedPosition = mathUtil.getJoinLinePoint(
  250. position,
  251. linkedRoadLine
  252. );
  253. roadInfo.x = linkedPosition.x;
  254. roadInfo.y = linkedPosition.y;
  255. }
  256. return roadInfo;
  257. }
  258. isSelectCurveRoad(position, exceptCurveRoadId) {
  259. let curveRoadInfo = {
  260. curveRoadId: null,
  261. type: null,
  262. distance: null,
  263. };
  264. const curveRoads = dataService.getCurveRoads();
  265. for (const curveRoadId in curveRoads) {
  266. if (curveRoadId == exceptCurveRoadId) {
  267. continue;
  268. }
  269. const curveRoad = dataService.getCurveRoad(curveRoadId);
  270. let joinInfo = this.distanceForBezier(position, curveRoad.curves);
  271. if (joinInfo.distance < Constant.minAdsorbPix) {
  272. curveRoadInfo = {
  273. curveRoadId: curveRoadId,
  274. type: VectorType.CurveRoad,
  275. distance: joinInfo.distance,
  276. x: joinInfo.position.x,
  277. y: joinInfo.position.y,
  278. };
  279. }
  280. }
  281. return curveRoadInfo;
  282. }
  283. setModifyPoint(position, pointInfo, curvePointInfo, roadInfo, curveRoadInfo) {
  284. //优先级最高
  285. if (pointInfo.pointId || curvePointInfo.curvePointId) {
  286. this.modifyPoint = {};
  287. if (pointInfo.pointId && curvePointInfo.curvePointId) {
  288. if (pointInfo.distance < curvePointInfo.distance) {
  289. this.modifyPoint.linkedPointId = pointInfo.pointId;
  290. this.modifyPoint.x = pointInfo.x;
  291. this.modifyPoint.y = pointInfo.y;
  292. } else {
  293. this.modifyPoint.linkedCurvePointId = curvePointInfo.curvePointId;
  294. this.modifyPoint.x = curvePointInfo.x;
  295. this.modifyPoint.y = curvePointInfo.y;
  296. }
  297. } else if (pointInfo.pointId) {
  298. this.modifyPoint.linkedPointId = pointInfo.pointId;
  299. this.modifyPoint.x = pointInfo.x;
  300. this.modifyPoint.y = pointInfo.y;
  301. } else if (curvePointInfo.curvePointId) {
  302. this.modifyPoint.linkedCurvePointId = curvePointInfo.curvePointId;
  303. this.modifyPoint.x = curvePointInfo.x;
  304. this.modifyPoint.y = curvePointInfo.y;
  305. }
  306. } else if (roadInfo.roadId || curveRoadInfo.curveRoadId) {
  307. this.modifyPoint = {};
  308. if (roadInfo.roadId && curveRoadInfo.curveRoadId) {
  309. if (roadInfo.distance < curveRoadInfo.distance) {
  310. this.modifyPoint.linkedRoadId = roadInfo.roadId;
  311. this.modifyPoint.x = roadInfo.x;
  312. this.modifyPoint.y = roadInfo.y;
  313. } else {
  314. this.modifyPoint.linkedCurveRoadId = curveRoadInfo.curveRoadId;
  315. this.modifyPoint.x = curveRoadInfo.x;
  316. this.modifyPoint.y = curveRoadInfo.y;
  317. }
  318. } else if (roadInfo.roadId) {
  319. this.modifyPoint.linkedRoadId = roadInfo.roadId;
  320. this.modifyPoint.x = roadInfo.x;
  321. this.modifyPoint.y = roadInfo.y;
  322. } else if (curveRoadInfo.curveRoadId) {
  323. this.modifyPoint.linkedCurveRoadId = curveRoadInfo.curveRoadId;
  324. this.modifyPoint.x = curveRoadInfo.x;
  325. this.modifyPoint.y = curveRoadInfo.y;
  326. }
  327. } else if (pointInfo.linkedPointIdX) {
  328. this.modifyPoint = {};
  329. this.modifyPoint.linkedPointIdX = pointInfo.linkedPointIdX;
  330. this.modifyPoint.x = pointInfo.x;
  331. this.modifyPoint.y = position.y;
  332. } else if (pointInfo.linkedPointIdY) {
  333. this.modifyPoint = {};
  334. this.modifyPoint.linkedPointIdY = pointInfo.linkedPointIdY;
  335. this.modifyPoint.y = pointInfo.y;
  336. this.modifyPoint.x = position.x;
  337. } else if (curvePointInfo.linkedPointIdX) {
  338. this.modifyPoint = {};
  339. this.modifyPoint.linkedPointIdX = curvePointInfo.linkedPointIdX;
  340. this.modifyPoint.x = curvePointInfo.x;
  341. this.modifyPoint.y = position.y;
  342. } else if (curvePointInfo.linkedPointIdY) {
  343. this.modifyPoint = {};
  344. this.modifyPoint.linkedPointIdY = curvePointInfo.linkedPointIdY;
  345. this.modifyPoint.y = curvePointInfo.y;
  346. this.modifyPoint.x = position.x;
  347. } else {
  348. this.modifyPoint = null;
  349. }
  350. }
  351. updateSelectItem() {
  352. let selectItem = stateService.getSelectItem();
  353. if (this.modifyPoint == null) {
  354. if (selectItem != null) {
  355. stateService.clearSelectItem();
  356. return true;
  357. } else {
  358. return false;
  359. }
  360. } else if (this.modifyPoint.linkedPointId) {
  361. stateService.setSelectItem(
  362. this.modifyPoint.linkedPointId,
  363. VectorType.Point,
  364. SelectState.Select
  365. );
  366. } else if (this.modifyPoint.linkedCurvePointId) {
  367. stateService.setSelectItem(
  368. this.modifyPoint.linkedCurvePointId,
  369. VectorType.CurvePoint,
  370. SelectState.Select
  371. );
  372. } else if (this.modifyPoint.linkedRoadId) {
  373. stateService.setSelectItem(
  374. this.modifyPoint.linkedRoadId,
  375. VectorType.Road,
  376. SelectState.Select
  377. );
  378. } else if (this.modifyPoint.linkedCurveRoadId) {
  379. stateService.setSelectItem(
  380. this.modifyPoint.linkedCurveRoadId,
  381. VectorType.CurveRoad,
  382. SelectState.Select
  383. );
  384. }
  385. let newSelectItem = stateService.getSelectItem();
  386. if (selectItem == null && newSelectItem == null) {
  387. return false;
  388. } else if (selectItem == null && newSelectItem != null) {
  389. return true;
  390. } else if (selectItem != null && newSelectItem == null) {
  391. return true;
  392. } else if (selectItem.vectorId == newSelectItem.vectorId) {
  393. return false;
  394. } else {
  395. return true;
  396. }
  397. // if (!flag1) {
  398. // stateService.setSelectItem(
  399. // this.curveRoadInfo.curveRoadId,
  400. // this.curveRoadInfo.type,
  401. // this.curveRoadInfo.state
  402. // );
  403. // }
  404. // if (!flag2) {
  405. // stateService.setSelectItem(
  406. // this.roadInfo.roadId,
  407. // this.roadInfo.type,
  408. // this.roadInfo.state
  409. // );
  410. // }
  411. // if (!flag3) {
  412. // stateService.setSelectItem(
  413. // this.curvePointInfo.curvePointId,
  414. // this.curvePointInfo.type,
  415. // this.curvePointInfo.state
  416. // );
  417. // }
  418. // if (!flag4) {
  419. // stateService.setSelectItem(
  420. // this.pointInfo.pointId,
  421. // this.pointInfo.type,
  422. // this.pointInfo.state
  423. // );
  424. // }
  425. // let selectItem = stateService.getSelectItem();
  426. // if (selectItem != null) {
  427. // console.log("监听:" + JSON.stringify(selectItem));
  428. // }
  429. // return flag1 && flag2 && flag3 && flag4;
  430. }
  431. distanceForBezier(position, curves) {
  432. let joinInfo = {
  433. position: null,
  434. distance: null,
  435. };
  436. for (let i = 0; i < curves.length; ++i) {
  437. const curve = curves[i];
  438. let bezierData = [];
  439. bezierData.push(curve.start.x);
  440. bezierData.push(curve.start.y);
  441. bezierData.push(curve.control.x);
  442. bezierData.push(curve.control.y);
  443. bezierData.push(curve.end.x);
  444. bezierData.push(curve.end.y);
  445. const { isHit, getInfo } = bezierUtil.measureBezier(...bezierData);
  446. const { point } = getInfo(position);
  447. if (
  448. joinInfo.distance == null ||
  449. mathUtil.getDistance(position, {
  450. x: point[0],
  451. y: point[1],
  452. }) < joinInfo.distance
  453. ) {
  454. joinInfo.distance = mathUtil.getDistance(position, {
  455. x: point[0],
  456. y: point[1],
  457. });
  458. joinInfo.position = {
  459. x: point[0],
  460. y: point[1],
  461. };
  462. }
  463. }
  464. return joinInfo;
  465. }
  466. equalAndClone(info1, info2) {
  467. let flag = true;
  468. for (let key in info1) {
  469. if (info1[key] != info2[key]) {
  470. flag = false;
  471. }
  472. info1[key] = info2[key];
  473. }
  474. return flag;
  475. }
  476. clear() {
  477. this.roadInfo = {
  478. roadId: null,
  479. type: null,
  480. state: null,
  481. };
  482. this.curveRoadInfo = {
  483. curveRoadId: null,
  484. type: null,
  485. state: null,
  486. };
  487. this.pointInfo = {
  488. pointId: null,
  489. type: null,
  490. state: null,
  491. };
  492. this.curvePointInfo = {
  493. curvePointId: null,
  494. type: null,
  495. state: null,
  496. };
  497. this.tagInfo = {
  498. tagId: null,
  499. state: null,
  500. };
  501. this.modifyPoint = null;
  502. }
  503. }
  504. const listenLayer = new ListenLayer();
  505. export { listenLayer };