DataService.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. import Line from "../Geometry/Line.js";
  2. import CurveLine from "../Geometry/CurveLine.js";
  3. export class DataService {
  4. constructor() {
  5. this.grid = {
  6. startX: 0,
  7. startY: 0,
  8. step1: 50,
  9. step2: 250,
  10. defalutstep1: 50,
  11. defalutstep2: 250,
  12. display: true,
  13. };
  14. this.vectorData = {};
  15. this.currentId = 0; // 当前可用id
  16. }
  17. setCurrentId(id) {
  18. this.currentId = id;
  19. }
  20. getCurrentId() {
  21. return this.currentId;
  22. }
  23. updateCurrentId() {
  24. ++this.currentId;
  25. }
  26. getVectorData() {
  27. return this.vectorData;
  28. }
  29. initVectorData() {
  30. this.vectorData.backgroundImg = null;
  31. //直路
  32. this.vectorData.roadPoints = {};
  33. this.vectorData.roads = {};
  34. this.vectorData.roadEdges = {};
  35. //弯路
  36. this.vectorData.curveRoadPoints = {};
  37. this.vectorData.curveRoads = {};
  38. this.vectorData.curveRoadEdges = {};
  39. //直路交叉的时候,产生的曲线控制点
  40. this.vectorData.controlPoints = {};
  41. //直线,起点和终点都是point的id
  42. this.vectorData.lines = {};
  43. //弯曲线条
  44. this.vectorData.curvelines = {};
  45. //线段(完全或者直线)上的端点
  46. this.vectorData.points = {};
  47. this.vectorData.circles = {};
  48. //基准点
  49. this.vectorData.basePointIds = [];
  50. this.vectorData.texts = {};
  51. }
  52. //网格
  53. getGrid() {
  54. return this.grid;
  55. }
  56. setGridForPan(dx, dy) {
  57. this.grid.startX += dx;
  58. this.grid.startY += dy;
  59. }
  60. setGridForZoom(w, h, ratio) {
  61. console.log("setGridForZoom:" + ratio);
  62. this.grid.startX = w / 2 - (ratio * w) / 2;
  63. this.grid.startY = w / 2 - (ratio * h) / 2;
  64. this.grid.step1 = this.grid.defalutstep1 * ratio;
  65. this.grid.step2 = this.grid.defalutstep2 * ratio;
  66. while (this.grid.startX > 0) {
  67. this.grid.startX -= this.grid.step1;
  68. }
  69. while (this.grid.startY > 0) {
  70. this.grid.startY -= this.grid.step2;
  71. }
  72. }
  73. setGridDisplay(value) {
  74. this.grid.display = value;
  75. }
  76. getGridDisplay() {
  77. return this.grid.display;
  78. }
  79. //背景图片
  80. addBackgroundImg(img) {
  81. this.vectorData.backgroundImg = img;
  82. }
  83. getBackgroundImg() {
  84. return this.vectorData.backgroundImg;
  85. }
  86. setAngle(angle) {
  87. this.vectorData.backgroundImg.angle = angle;
  88. }
  89. //直线
  90. addLine(line) {
  91. this.vectorData.lines[line.vectorId] = line;
  92. }
  93. getLines() {
  94. return this.vectorData.lines;
  95. }
  96. getLine(lineId) {
  97. return this.vectorData.lines[lineId];
  98. }
  99. deleteLine(lineId) {
  100. delete this.vectorData.lines[lineId];
  101. }
  102. //直线的端点
  103. getPoints() {
  104. return this.vectorData.points;
  105. }
  106. getPoint(pointId) {
  107. return this.vectorData.points[pointId];
  108. }
  109. addPoint(point) {
  110. this.vectorData.points[point.vectorId] = point;
  111. }
  112. // deletePoint(pointId) {
  113. // delete this.vectorData.points[pointId];
  114. // }
  115. deletePoint(pointId, lineId) {
  116. let point = this.getPoint(pointId);
  117. //有可能先删除墙,导致点没了
  118. if (point) {
  119. if (Object.keys(point.parent).length == 0) {
  120. point = null;
  121. delete this.vectorData.points[pointId];
  122. } else if (Object.keys(point.parent).length == 1 && !lineId) {
  123. delete this.vectorData.points[pointId];
  124. } else if (
  125. Object.keys(point.parent).length == 1 &&
  126. point.parent[lineId]
  127. ) {
  128. delete this.vectorData.points[pointId];
  129. } else if (
  130. Object.keys(point.parent).length == 1 &&
  131. !point.parent[lineId]
  132. ) {
  133. return;
  134. } else {
  135. delete point.parent[lineId];
  136. }
  137. }
  138. }
  139. deletePoint(pointId) {
  140. const point = dataService.getRoadPoint(pointId);
  141. const parent = point.parent;
  142. for (const key in parent) {
  143. dataService.deleteRoadPoint(pointId, key);
  144. }
  145. }
  146. //圆圈
  147. getCircles() {
  148. return this.vectorData.circles;
  149. }
  150. getCircle(circleId) {
  151. return this.vectorData.circles[circleId];
  152. }
  153. addCircle(circle) {
  154. this.vectorData.circles[circle.vectorId] = circle;
  155. }
  156. deleteCircle(circleId) {
  157. delete this.vectorData.circles[circleId];
  158. }
  159. //弯曲线条
  160. addCurveLine(curveLine) {
  161. this.vectorData.curvelines[curveLine.vectorId] = curveLine;
  162. }
  163. addCurvePoint(curvePoint) {
  164. this.vectorData.curvePoints[curvePoint.vectorId] = curvePoint;
  165. }
  166. getCurvePoints() {
  167. return this.vectorData.curvePoints;
  168. }
  169. getCurvePoint(curvePointId) {
  170. if (curvePointId) {
  171. return this.vectorData.curvePoints[curvePointId];
  172. } else {
  173. return null;
  174. }
  175. }
  176. deleteCurvePoint(curvePointId) {
  177. delete this.vectorData.curvePoints[curvePointId];
  178. }
  179. getCurveLines() {
  180. return this.vectorData.curvelines;
  181. }
  182. getCurveLine(curveLineId) {
  183. if (curveLineId) {
  184. return this.vectorData.curvelines[curveLineId];
  185. } else {
  186. return null;
  187. }
  188. }
  189. /**
  190. * 对弯路的操作
  191. */
  192. addCurveRoadPoint(curveRoadPoint) {
  193. this.vectorData.curveRoadPoints[curveRoadPoint.vectorId] = curveRoadPoint;
  194. }
  195. getCurveRoadPoints() {
  196. return this.vectorData.curveRoadPoints;
  197. }
  198. getCurveRoadPoint(curveRoadPointId) {
  199. if (curveRoadPointId) {
  200. return this.vectorData.curveRoadPoints[curveRoadPointId];
  201. } else {
  202. return null;
  203. }
  204. }
  205. deleteCurveRoadPoint(curveRoadPointId) {
  206. delete this.vectorData.curveRoadPoints[curveRoadPointId];
  207. }
  208. addCurveRoad(curveRoad) {
  209. this.vectorData.curveRoads[curveRoad.vectorId] = curveRoad;
  210. }
  211. getCurveRoads() {
  212. return this.vectorData.curveRoads;
  213. }
  214. getCurveRoad(curveRoadId) {
  215. if (curveRoadId) {
  216. return this.vectorData.curveRoads[curveRoadId];
  217. } else {
  218. return null;
  219. }
  220. }
  221. deleteCurveRoad(curveRoadId) {
  222. let curveRoad = this.getCurveRoad(curveRoadId);
  223. for (let i = 0; i < curveRoad.points.length; ++i) {
  224. this.deleteCurveRoadPoint(curveRoad.points[i].vectorId);
  225. }
  226. this.deleteCurveRoadEdge(curveRoad.leftEdgeId);
  227. this.deleteCurveRoadEdge(curveRoad.rightEdgeId);
  228. delete this.vectorData.curveRoads[curveRoadId];
  229. }
  230. getCurveRoadEdge(curveRoadEdgeId) {
  231. return this.vectorData.curveRoadEdges[curveRoadEdgeId];
  232. }
  233. addCurveRoadEdge(curveRoadEdge) {
  234. this.vectorData.curveRoadEdges[curveRoadEdge.vectorId] = curveRoadEdge;
  235. }
  236. deleteCurveRoadEdge(curveRoadEdgeId) {
  237. delete this.vectorData.curveRoadEdges[curveRoadEdgeId];
  238. }
  239. //直线
  240. getRoads() {
  241. return this.vectorData.roads;
  242. }
  243. getRoad(roadId) {
  244. if (roadId) {
  245. return this.vectorData.roads[roadId];
  246. } else {
  247. return null;
  248. }
  249. }
  250. addRoad(road) {
  251. this.vectorData.roads[road.vectorId] = road;
  252. }
  253. deleteRoad(roadId) {
  254. let road = this.getRoad(roadId);
  255. this.deleteRoadPoint(road.startId, roadId);
  256. this.deleteRoadPoint(road.endId, roadId);
  257. this.deleteRoadEdge(road.leftEdgeId);
  258. this.deleteRoadEdge(road.rightEdgeId);
  259. delete this.vectorData.roads[roadId];
  260. }
  261. deleteRoadEdge(edgeId) {
  262. delete this.vectorData.roadEdges[edgeId];
  263. }
  264. /**
  265. * 对端点的操作
  266. */
  267. getRoadPoints() {
  268. return this.vectorData.roadPoints;
  269. }
  270. getRoadPoint(roadPointId) {
  271. if (roadPointId) {
  272. return this.vectorData.roadPoints[roadPointId];
  273. } else {
  274. return null;
  275. }
  276. }
  277. deleteRoadPoint(roadPointId, roadId) {
  278. let roadPoint = this.getRoadPoint(roadPointId);
  279. //有可能先删除墙,导致点没了
  280. if (roadPoint) {
  281. if (Object.keys(roadPoint.parent).length == 0) {
  282. roadPoint = null;
  283. delete this.vectorData.roadPoints[roadPointId];
  284. } else if (Object.keys(roadPoint.parent).length == 1 && !roadId) {
  285. delete this.vectorData.roadPoints[roadPointId];
  286. } else if (
  287. Object.keys(roadPoint.parent).length == 1 &&
  288. roadPoint.parent[roadId]
  289. ) {
  290. delete this.vectorData.roadPoints[roadPointId];
  291. } else if (
  292. Object.keys(roadPoint.parent).length == 1 &&
  293. !roadPoint.parent[roadId]
  294. ) {
  295. return;
  296. } else {
  297. delete roadPoint.parent[roadId];
  298. }
  299. }
  300. }
  301. addRoadPoint(roadPoint) {
  302. this.vectorData.roadPoints[roadPoint.vectorId] = roadPoint;
  303. }
  304. getRoadEdge(roadEdgeId) {
  305. return this.vectorData.roadEdges[roadEdgeId];
  306. }
  307. addRoadEdge(roadEdge) {
  308. this.vectorData.roadEdges[roadEdge.vectorId] = roadEdge;
  309. }
  310. /**
  311. * 对控制点的操作
  312. */
  313. getControlPoints() {
  314. return this.vectorData.controlPoints;
  315. }
  316. getControlPoint(edgeId1, edgeId2) {
  317. if (this.vectorData.controlPoints[edgeId1 + "-" + edgeId2]) {
  318. return this.vectorData.controlPoints[edgeId1 + "-" + edgeId2];
  319. } else if (this.vectorData.controlPoints[edgeId2 + "-" + edgeId1]) {
  320. return this.vectorData.controlPoints[edgeId2 + "-" + edgeId1];
  321. } else {
  322. return null;
  323. }
  324. }
  325. getControlPoint2(key) {
  326. return this.vectorData.controlPoints[key];
  327. }
  328. getControlPointForEdgeId(edgeId, dir) {
  329. for (let key in this.vectorData.controlPoints) {
  330. const controlPoint = this.vectorData.controlPoints[key];
  331. if (
  332. controlPoint.edgeInfo1.id == edgeId &&
  333. controlPoint.edgeInfo1.dir == dir
  334. ) {
  335. return this.vectorData.controlPoints[key];
  336. } else if (
  337. controlPoint.edgeInfo2.id == edgeId &&
  338. controlPoint.edgeInfo2.dir == dir
  339. ) {
  340. return this.vectorData.controlPoints[key];
  341. }
  342. }
  343. }
  344. addControlPoint(controlPoint) {
  345. this.vectorData.controlPoints[
  346. controlPoint.edgeInfo1.id + "-" + controlPoint.edgeInfo2.id
  347. ] = controlPoint;
  348. }
  349. deleteControlPoint(edgeId1, edgeId2) {
  350. let key = edgeId1 + "-" + edgeId2;
  351. if (!this.vectorData.controlPoints[key]) {
  352. key = edgeId2 + "-" + edgeId1;
  353. }
  354. if (this.vectorData.controlPoints[key]) {
  355. delete this.vectorData.controlPoints[key];
  356. }
  357. }
  358. deleteControlPointForEdge(edgeId, dir) {
  359. let dControlPointIds = [];
  360. for (let key in this.vectorData.controlPoints) {
  361. const controlPoint = this.vectorData.controlPoints[key];
  362. if (
  363. controlPoint.edgeInfo1.id == edgeId ||
  364. controlPoint.edgeInfo2.id == edgeId
  365. ) {
  366. dControlPointIds.push(key);
  367. }
  368. }
  369. for (let i = 0; i < dControlPointIds.length; ++i) {
  370. const controlPoint = this.vectorData.controlPoints[dControlPointIds[i]];
  371. if (
  372. controlPoint.edgeInfo1.id == edgeId &&
  373. controlPoint.edgeInfo1.dir == dir
  374. ) {
  375. delete this.vectorData.controlPoints[dControlPointIds[i]];
  376. } else if (
  377. controlPoint.edgeInfo2.id == edgeId &&
  378. controlPoint.edgeInfo2.dir == dir
  379. ) {
  380. delete this.vectorData.controlPoints[dControlPointIds[i]];
  381. }
  382. }
  383. }
  384. /**
  385. * 对标注的操作
  386. */
  387. addText(text) {
  388. this.vectorData.texts[text.vectorId] = text;
  389. }
  390. getText(textId) {
  391. return this.vectorData.texts[textId];
  392. }
  393. deleteText(textId) {
  394. delete this.vectorData.texts[textId];
  395. }
  396. getTexts() {
  397. return this.vectorData.texts;
  398. }
  399. clear() {
  400. this.vectorData = {};
  401. }
  402. }
  403. const dataService = new DataService();
  404. export { dataService };