DataService.js 12 KB

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