MeasureService.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. import { dataService } from "./DataService";
  2. // import { mathUtil } from "../MathUtil.js";
  3. import { coordinate } from "../Coordinate";
  4. import Constant from "../Constant";
  5. /*
  6. 1. 所有的点都投影到上下左右四个测量区域并排序
  7. 2. 分别从起点开始向终点一段段画测量线,如果太短(按照像素来)则合并到上一条线段上。确定好当前线段的起始点后,开始画
  8. */
  9. export default class MeasureService {
  10. constructor() {
  11. this.pad = {
  12. top: 60,
  13. bottom: 60,
  14. left: 265,
  15. right: 265,
  16. };
  17. this.region = {};
  18. this.measureLines = {
  19. top: [],
  20. bottom: [],
  21. left: [],
  22. right: [],
  23. };
  24. this.minDis = null;
  25. //1 英尺=0.3048 米
  26. //this.ftUnit = 0.3048
  27. this.unit = "m";
  28. this.defalutMeasurePad = {
  29. bottom: 60,
  30. right: 265,
  31. };
  32. }
  33. /**
  34. * 设置边距
  35. * @param {*} config
  36. */
  37. padding(config = {}) {
  38. Object.assign(this.pad, config);
  39. Object.assign(this.defalutMeasurePad, config);
  40. }
  41. updatePad(pad) {
  42. Object.assign(this.pad, pad);
  43. }
  44. updateRegion(download) {
  45. this.region.top = this.pad.top;
  46. this.region.bottom = coordinate.height - this.pad.bottom;
  47. this.region.left = this.pad.left;
  48. this.region.right = coordinate.width - this.pad.right;
  49. // if (download) { //不能用下面的,因为Constant.cadImg_Height / Constant.ratio和coordinate.height不同
  50. // this.region.bottom = Constant.cadImg_Height / Constant.ratio - this.pad.bottom
  51. // this.region.right = Constant.cadImg_Width / Constant.ratio - this.pad.right
  52. // }
  53. // let leftTop = coordinate.getXYFromScreen({
  54. // x: this.region.left,
  55. // y: this.region.top,
  56. // })
  57. // let rightBottom = coordinate.getXYFromScreen({
  58. // x: this.region.right,
  59. // y: this.region.bottom,
  60. // })
  61. // this.region.top = leftTop.y
  62. // this.region.left = leftTop.x
  63. // this.region.bottom = rightBottom.y
  64. // this.region.right = rightBottom.x
  65. }
  66. //更新测量线
  67. update() {
  68. if (this.minDis == null) {
  69. this.minDis = 100 / coordinate.res;
  70. }
  71. let tops = [];
  72. let bottoms = [];
  73. let lefts = [];
  74. let rights = [];
  75. let measurePoints = [];
  76. let data = dataService.getFloorData();
  77. if (!data) {
  78. return;
  79. }
  80. const points = dataService.getPoints();
  81. for (let key in points) {
  82. const point = points[key];
  83. measurePoints.push({
  84. x: point.x,
  85. y: point.y,
  86. });
  87. }
  88. function sortNumber_topbottom(a, b) {
  89. return b.y - a.y;
  90. }
  91. function sortNumber_leftright(a, b) {
  92. return a.x - b.x;
  93. }
  94. tops = [].concat(measurePoints).sort(sortNumber_topbottom.bind(this));
  95. bottoms = [].concat(tops);
  96. bottoms.reverse();
  97. lefts = [].concat(measurePoints).sort(sortNumber_leftright.bind(this));
  98. rights = [].concat(lefts);
  99. rights.reverse();
  100. let start = null;
  101. let end = null;
  102. this.measureLines.top = [];
  103. for (let i = 0; i < tops.length; ++i) {
  104. if (i == 0) {
  105. start = tops[0].x;
  106. end = tops[0].x;
  107. this.measureLines.top.push({
  108. x: tops[0].x,
  109. y: this.region.top,
  110. });
  111. } else {
  112. if (tops[i].x >= start && tops[i].x <= end) {
  113. continue;
  114. } else {
  115. start = Math.min(start, tops[i].x);
  116. end = Math.max(end, tops[i].x);
  117. if (start != end) {
  118. this.measureLines.top.push({
  119. x: tops[i].x,
  120. y: this.region.top,
  121. });
  122. }
  123. }
  124. }
  125. }
  126. tops = this.measureLines.top.sort(sortNumber_leftright.bind(this));
  127. this.measureLines.top = [];
  128. this.measureLines.top.push(tops[0]);
  129. for (let i = 0; i < tops.length - 1; ++i) {
  130. start = tops[i];
  131. end = null;
  132. for (let j = i + 1; j < tops.length; ++j) {
  133. end = tops[j];
  134. if (Math.abs(start.x - end.x) < this.minDis) {
  135. end = null;
  136. ++i;
  137. continue;
  138. } else {
  139. break;
  140. }
  141. }
  142. if (end != null) {
  143. this.measureLines.top.push(end);
  144. } else if (i == tops.length - 1) {
  145. let len = this.measureLines.top.length;
  146. this.measureLines.top[len - 1] = tops[i];
  147. break;
  148. }
  149. }
  150. this.measureLines.bottom = [];
  151. for (let i = 0; i < bottoms.length; ++i) {
  152. if (i == 0) {
  153. start = bottoms[0].x;
  154. end = bottoms[0].x;
  155. this.measureLines.bottom.push({
  156. x: bottoms[0].x,
  157. y: this.region.bottom,
  158. });
  159. } else {
  160. if (bottoms[i].x >= start && bottoms[i].x <= end) {
  161. continue;
  162. } else {
  163. start = Math.min(start, bottoms[i].x);
  164. end = Math.max(end, bottoms[i].x);
  165. if (start != end) {
  166. this.measureLines.bottom.push({
  167. x: bottoms[i].x,
  168. y: this.region.bottom,
  169. });
  170. }
  171. }
  172. }
  173. }
  174. bottoms = this.measureLines.bottom.sort(sortNumber_leftright.bind(this));
  175. this.measureLines.bottom = [];
  176. this.measureLines.bottom.push(bottoms[0]);
  177. for (let i = 0; i < bottoms.length - 1; ++i) {
  178. start = bottoms[i];
  179. end = null;
  180. for (let j = i + 1; j < bottoms.length; ++j) {
  181. end = bottoms[j];
  182. if (Math.abs(start.x - end.x) < this.minDis) {
  183. end = null;
  184. ++i;
  185. continue;
  186. } else {
  187. break;
  188. }
  189. }
  190. if (end != null) {
  191. this.measureLines.bottom.push(end);
  192. } else if (i == bottoms.length - 1) {
  193. let len = this.measureLines.bottom.length;
  194. this.measureLines.bottom[len - 1] = bottoms[i];
  195. break;
  196. }
  197. }
  198. this.measureLines.left = [];
  199. for (let i = 0; i < lefts.length; ++i) {
  200. if (i == 0) {
  201. start = lefts[0].y;
  202. end = lefts[0].y;
  203. this.measureLines.left.push({
  204. x: this.region.left,
  205. y: lefts[0].y,
  206. });
  207. } else {
  208. if (lefts[i].y >= start && lefts[i].y <= end) {
  209. continue;
  210. } else {
  211. start = Math.min(start, lefts[i].y);
  212. end = Math.max(end, lefts[i].y);
  213. if (start != end) {
  214. this.measureLines.left.push({
  215. x: this.region.left,
  216. y: lefts[i].y,
  217. });
  218. }
  219. }
  220. }
  221. }
  222. lefts = this.measureLines.left.sort(sortNumber_topbottom.bind(this));
  223. this.measureLines.left = [];
  224. this.measureLines.left.push(lefts[0]);
  225. for (let i = 0; i < lefts.length - 1; ++i) {
  226. start = lefts[i];
  227. end = null;
  228. for (let j = i + 1; j < lefts.length; ++j) {
  229. end = lefts[j];
  230. if (Math.abs(start.y - end.y) < this.minDis) {
  231. end = null;
  232. ++i;
  233. continue;
  234. } else {
  235. break;
  236. }
  237. }
  238. if (end != null) {
  239. this.measureLines.left.push(end);
  240. } else if (i == lefts.length - 1) {
  241. let len = this.measureLines.left.length;
  242. this.measureLines.left[len - 1] = lefts[i];
  243. break;
  244. }
  245. }
  246. this.measureLines.right = [];
  247. for (let i = 0; i < rights.length; ++i) {
  248. if (i == 0) {
  249. start = rights[0].y;
  250. end = rights[0].y;
  251. this.measureLines.right.push({
  252. x: this.region.right,
  253. y: rights[0].y,
  254. });
  255. } else {
  256. if (rights[i].y >= start && rights[i].y <= end) {
  257. continue;
  258. } else {
  259. start = Math.min(start, rights[i].y);
  260. end = Math.max(end, rights[i].y);
  261. if (start != end) {
  262. this.measureLines.right.push({
  263. x: this.region.right,
  264. y: rights[i].y,
  265. });
  266. }
  267. }
  268. }
  269. }
  270. rights = this.measureLines.right.sort(sortNumber_topbottom.bind(this));
  271. this.measureLines.right = [];
  272. this.measureLines.right.push(rights[0]);
  273. for (let i = 0; i < rights.length - 1; ++i) {
  274. start = rights[i];
  275. end = null;
  276. for (let j = i + 1; j < rights.length; ++j) {
  277. end = rights[j];
  278. if (Math.abs(start.y - end.y) < this.minDis) {
  279. end = null;
  280. ++i;
  281. continue;
  282. } else {
  283. break;
  284. }
  285. }
  286. if (end != null) {
  287. this.measureLines.right.push(end);
  288. } else if (i == rights.length - 1) {
  289. let len = this.measureLines.right.length;
  290. this.measureLines.right[len - 1] = rights[i];
  291. break;
  292. }
  293. }
  294. }
  295. /*
  296. update() {
  297. let tops = []
  298. let bottoms = []
  299. let lefts = []
  300. let rights = []
  301. let minX = coordinate.center.x;
  302. let minY = coordinate.center.y;
  303. let maxX = coordinate.center.x;
  304. let maxY = coordinate.center.y;
  305. const points = dataService.getPoints()
  306. for (let key in points) {
  307. const point = points[key]
  308. if(point.y > coordinate.center.y){
  309. tops.push({
  310. x: point.x,
  311. y: this.region.top,
  312. })
  313. }
  314. else{
  315. bottoms.push({
  316. x:point.x,
  317. y:this.region.bottom
  318. })
  319. }
  320. if(point.x<coordinate.center.x){
  321. lefts.push({
  322. x: this.region.left,
  323. y: point.y,
  324. })
  325. }
  326. else{
  327. rights.push({
  328. x:this.region.right,
  329. y:point.y
  330. })
  331. }
  332. if(minX>point.x){
  333. minX = point.x
  334. }
  335. if(maxX<point.x){
  336. maxX = point.x
  337. }
  338. if(minY>point.y){
  339. minY = point.y
  340. }
  341. if(maxY<point.y){
  342. maxY = point.y
  343. }
  344. }
  345. tops.unshift({
  346. x:minX,
  347. y:this.region.top
  348. })
  349. tops.push({
  350. x:maxX,
  351. y:this.region.top
  352. })
  353. bottoms.unshift({
  354. x:minX,
  355. y:this.region.bottom
  356. })
  357. bottoms.push({
  358. x:maxX,
  359. y:this.region.bottom
  360. })
  361. lefts.unshift({
  362. x:this.region.left,
  363. y:maxY
  364. })
  365. lefts.push({
  366. x:this.region.left,
  367. y:minY
  368. })
  369. rights.unshift({
  370. x:this.region.right,
  371. y:maxY
  372. })
  373. rights.push({
  374. x:this.region.right,
  375. y:minY
  376. })
  377. function sortNumber_topbottom(a, b) {
  378. return a.x - b.x
  379. }
  380. function sortNumber_leftright(a, b) {
  381. return b.y - a.y
  382. }
  383. tops = tops.sort(sortNumber_topbottom.bind(this))
  384. bottoms = bottoms.sort(sortNumber_topbottom.bind(this))
  385. lefts = lefts.sort(sortNumber_leftright.bind(this))
  386. rights = rights.sort(sortNumber_leftright.bind(this))
  387. this.measureLines.top = []
  388. this.measureLines.top.push(tops[0])
  389. for (let i = 0; i < tops.length - 1; ++i) {
  390. let start = tops[i]
  391. let end = null
  392. for (let j = i + 1; j < tops.length; ++j) {
  393. end = tops[j]
  394. if (mathUtil.getDistance(start, end) < this.minDis) {
  395. end = null
  396. ++i
  397. continue
  398. } else {
  399. break
  400. }
  401. }
  402. if (end != null) {
  403. this.measureLines.top.push(end)
  404. } else if (i == tops.length - 1) {
  405. let len = this.measureLines.top.length
  406. this.measureLines.top[len - 1] = tops[i]
  407. break
  408. }
  409. }
  410. this.measureLines.bottom = []
  411. this.measureLines.bottom.push(bottoms[0])
  412. for (let i = 0; i < bottoms.length - 1; ++i) {
  413. let start = bottoms[i]
  414. let end = null
  415. for (let j = i + 1; j < bottoms.length; ++j) {
  416. end = bottoms[j]
  417. if (mathUtil.getDistance(start, end) < this.minDis) {
  418. end = null
  419. ++i
  420. continue
  421. } else {
  422. break
  423. }
  424. }
  425. if (end != null) {
  426. this.measureLines.bottom.push(end)
  427. } else if (i == bottoms.length - 1) {
  428. let len = this.measureLines.bottom.length
  429. this.measureLines.bottom[len - 1] = bottoms[i]
  430. break
  431. }
  432. }
  433. this.measureLines.left = []
  434. this.measureLines.left.push(lefts[0])
  435. for (let i = 0; i < lefts.length - 1; ++i) {
  436. let start = lefts[i]
  437. let end = null
  438. for (let j = i + 1; j < lefts.length; ++j) {
  439. end = lefts[j]
  440. if (mathUtil.getDistance(start, end) < this.minDis) {
  441. end = null
  442. ++i
  443. continue
  444. } else {
  445. break
  446. }
  447. }
  448. if (end != null) {
  449. this.measureLines.left.push(end)
  450. } else if (i == lefts.length - 1) {
  451. let len = this.measureLines.left.length
  452. this.measureLines.left[len - 1] = lefts[i]
  453. break
  454. }
  455. }
  456. this.measureLines.right = []
  457. this.measureLines.right.push(rights[0])
  458. for (let i = 0; i < rights.length - 1; ++i) {
  459. let start = rights[i]
  460. let end = null
  461. for (let j = i + 1; j < rights.length; ++j) {
  462. end = rights[j]
  463. if (mathUtil.getDistance(start, end) < this.minDis) {
  464. end = null
  465. ++i
  466. continue
  467. } else {
  468. break
  469. }
  470. }
  471. if (end != null) {
  472. this.measureLines.right.push(end)
  473. } else if (i == rights.length - 1) {
  474. let len = this.measureLines.right.length
  475. this.measureLines.right[len - 1] = rights[i]
  476. break
  477. }
  478. }
  479. }
  480. */
  481. }
  482. const measureService = new MeasureService();
  483. export { measureService };