123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- import { dataService } from "./DataService";
- // import { mathUtil } from "../MathUtil.js";
- import { coordinate } from "../Coordinate";
- import Constant from "../Constant";
- /*
- 1. 所有的点都投影到上下左右四个测量区域并排序
- 2. 分别从起点开始向终点一段段画测量线,如果太短(按照像素来)则合并到上一条线段上。确定好当前线段的起始点后,开始画
- */
- export default class MeasureService {
- constructor() {
- this.pad = {
- top: 60,
- bottom: 60,
- left: 265,
- right: 265,
- };
- this.region = {};
- this.measureLines = {
- top: [],
- bottom: [],
- left: [],
- right: [],
- };
- this.minDis = null;
- //1 英尺=0.3048 米
- //this.ftUnit = 0.3048
- this.unit = "m";
- this.defalutMeasurePad = {
- bottom: 60,
- right: 265,
- };
- }
- /**
- * 设置边距
- * @param {*} config
- */
- padding(config = {}) {
- Object.assign(this.pad, config);
- Object.assign(this.defalutMeasurePad, config);
- }
- updatePad(pad) {
- Object.assign(this.pad, pad);
- }
- updateRegion(download) {
- this.region.top = this.pad.top;
- this.region.bottom = coordinate.height - this.pad.bottom;
- this.region.left = this.pad.left;
- this.region.right = coordinate.width - this.pad.right;
- // if (download) { //不能用下面的,因为Constant.cadImg_Height / Constant.ratio和coordinate.height不同
- // this.region.bottom = Constant.cadImg_Height / Constant.ratio - this.pad.bottom
- // this.region.right = Constant.cadImg_Width / Constant.ratio - this.pad.right
- // }
- // let leftTop = coordinate.getXYFromScreen({
- // x: this.region.left,
- // y: this.region.top,
- // })
- // let rightBottom = coordinate.getXYFromScreen({
- // x: this.region.right,
- // y: this.region.bottom,
- // })
- // this.region.top = leftTop.y
- // this.region.left = leftTop.x
- // this.region.bottom = rightBottom.y
- // this.region.right = rightBottom.x
- }
- //更新测量线
- update() {
- if (this.minDis == null) {
- this.minDis = 100 / coordinate.res;
- }
- let tops = [];
- let bottoms = [];
- let lefts = [];
- let rights = [];
- let measurePoints = [];
- let data = dataService.getFloorData();
- if (!data) {
- return;
- }
- const points = dataService.getPoints();
- for (let key in points) {
- const point = points[key];
- measurePoints.push({
- x: point.x,
- y: point.y,
- });
- }
- function sortNumber_topbottom(a, b) {
- return b.y - a.y;
- }
- function sortNumber_leftright(a, b) {
- return a.x - b.x;
- }
- tops = [].concat(measurePoints).sort(sortNumber_topbottom.bind(this));
- bottoms = [].concat(tops);
- bottoms.reverse();
- lefts = [].concat(measurePoints).sort(sortNumber_leftright.bind(this));
- rights = [].concat(lefts);
- rights.reverse();
- let start = null;
- let end = null;
- this.measureLines.top = [];
- for (let i = 0; i < tops.length; ++i) {
- if (i == 0) {
- start = tops[0].x;
- end = tops[0].x;
- this.measureLines.top.push({
- x: tops[0].x,
- y: this.region.top,
- });
- } else {
- if (tops[i].x >= start && tops[i].x <= end) {
- continue;
- } else {
- start = Math.min(start, tops[i].x);
- end = Math.max(end, tops[i].x);
- if (start != end) {
- this.measureLines.top.push({
- x: tops[i].x,
- y: this.region.top,
- });
- }
- }
- }
- }
- tops = this.measureLines.top.sort(sortNumber_leftright.bind(this));
- this.measureLines.top = [];
- this.measureLines.top.push(tops[0]);
- for (let i = 0; i < tops.length - 1; ++i) {
- start = tops[i];
- end = null;
- for (let j = i + 1; j < tops.length; ++j) {
- end = tops[j];
- if (Math.abs(start.x - end.x) < this.minDis) {
- end = null;
- ++i;
- continue;
- } else {
- break;
- }
- }
- if (end != null) {
- this.measureLines.top.push(end);
- } else if (i == tops.length - 1) {
- let len = this.measureLines.top.length;
- this.measureLines.top[len - 1] = tops[i];
- break;
- }
- }
- this.measureLines.bottom = [];
- for (let i = 0; i < bottoms.length; ++i) {
- if (i == 0) {
- start = bottoms[0].x;
- end = bottoms[0].x;
- this.measureLines.bottom.push({
- x: bottoms[0].x,
- y: this.region.bottom,
- });
- } else {
- if (bottoms[i].x >= start && bottoms[i].x <= end) {
- continue;
- } else {
- start = Math.min(start, bottoms[i].x);
- end = Math.max(end, bottoms[i].x);
- if (start != end) {
- this.measureLines.bottom.push({
- x: bottoms[i].x,
- y: this.region.bottom,
- });
- }
- }
- }
- }
- bottoms = this.measureLines.bottom.sort(sortNumber_leftright.bind(this));
- this.measureLines.bottom = [];
- this.measureLines.bottom.push(bottoms[0]);
- for (let i = 0; i < bottoms.length - 1; ++i) {
- start = bottoms[i];
- end = null;
- for (let j = i + 1; j < bottoms.length; ++j) {
- end = bottoms[j];
- if (Math.abs(start.x - end.x) < this.minDis) {
- end = null;
- ++i;
- continue;
- } else {
- break;
- }
- }
- if (end != null) {
- this.measureLines.bottom.push(end);
- } else if (i == bottoms.length - 1) {
- let len = this.measureLines.bottom.length;
- this.measureLines.bottom[len - 1] = bottoms[i];
- break;
- }
- }
- this.measureLines.left = [];
- for (let i = 0; i < lefts.length; ++i) {
- if (i == 0) {
- start = lefts[0].y;
- end = lefts[0].y;
- this.measureLines.left.push({
- x: this.region.left,
- y: lefts[0].y,
- });
- } else {
- if (lefts[i].y >= start && lefts[i].y <= end) {
- continue;
- } else {
- start = Math.min(start, lefts[i].y);
- end = Math.max(end, lefts[i].y);
- if (start != end) {
- this.measureLines.left.push({
- x: this.region.left,
- y: lefts[i].y,
- });
- }
- }
- }
- }
- lefts = this.measureLines.left.sort(sortNumber_topbottom.bind(this));
- this.measureLines.left = [];
- this.measureLines.left.push(lefts[0]);
- for (let i = 0; i < lefts.length - 1; ++i) {
- start = lefts[i];
- end = null;
- for (let j = i + 1; j < lefts.length; ++j) {
- end = lefts[j];
- if (Math.abs(start.y - end.y) < this.minDis) {
- end = null;
- ++i;
- continue;
- } else {
- break;
- }
- }
- if (end != null) {
- this.measureLines.left.push(end);
- } else if (i == lefts.length - 1) {
- let len = this.measureLines.left.length;
- this.measureLines.left[len - 1] = lefts[i];
- break;
- }
- }
- this.measureLines.right = [];
- for (let i = 0; i < rights.length; ++i) {
- if (i == 0) {
- start = rights[0].y;
- end = rights[0].y;
- this.measureLines.right.push({
- x: this.region.right,
- y: rights[0].y,
- });
- } else {
- if (rights[i].y >= start && rights[i].y <= end) {
- continue;
- } else {
- start = Math.min(start, rights[i].y);
- end = Math.max(end, rights[i].y);
- if (start != end) {
- this.measureLines.right.push({
- x: this.region.right,
- y: rights[i].y,
- });
- }
- }
- }
- }
- rights = this.measureLines.right.sort(sortNumber_topbottom.bind(this));
- this.measureLines.right = [];
- this.measureLines.right.push(rights[0]);
- for (let i = 0; i < rights.length - 1; ++i) {
- start = rights[i];
- end = null;
- for (let j = i + 1; j < rights.length; ++j) {
- end = rights[j];
- if (Math.abs(start.y - end.y) < this.minDis) {
- end = null;
- ++i;
- continue;
- } else {
- break;
- }
- }
- if (end != null) {
- this.measureLines.right.push(end);
- } else if (i == rights.length - 1) {
- let len = this.measureLines.right.length;
- this.measureLines.right[len - 1] = rights[i];
- break;
- }
- }
- }
- /*
- update() {
- let tops = []
- let bottoms = []
- let lefts = []
- let rights = []
- let minX = coordinate.center.x;
- let minY = coordinate.center.y;
- let maxX = coordinate.center.x;
- let maxY = coordinate.center.y;
- const points = dataService.getPoints()
- for (let key in points) {
- const point = points[key]
- if(point.y > coordinate.center.y){
- tops.push({
- x: point.x,
- y: this.region.top,
- })
- }
- else{
- bottoms.push({
- x:point.x,
- y:this.region.bottom
- })
- }
- if(point.x<coordinate.center.x){
- lefts.push({
- x: this.region.left,
- y: point.y,
- })
- }
- else{
- rights.push({
- x:this.region.right,
- y:point.y
- })
- }
- if(minX>point.x){
- minX = point.x
- }
- if(maxX<point.x){
- maxX = point.x
- }
- if(minY>point.y){
- minY = point.y
- }
- if(maxY<point.y){
- maxY = point.y
- }
- }
- tops.unshift({
- x:minX,
- y:this.region.top
- })
- tops.push({
- x:maxX,
- y:this.region.top
- })
- bottoms.unshift({
- x:minX,
- y:this.region.bottom
- })
- bottoms.push({
- x:maxX,
- y:this.region.bottom
- })
- lefts.unshift({
- x:this.region.left,
- y:maxY
- })
- lefts.push({
- x:this.region.left,
- y:minY
- })
- rights.unshift({
- x:this.region.right,
- y:maxY
- })
- rights.push({
- x:this.region.right,
- y:minY
- })
- function sortNumber_topbottom(a, b) {
- return a.x - b.x
- }
- function sortNumber_leftright(a, b) {
- return b.y - a.y
- }
- tops = tops.sort(sortNumber_topbottom.bind(this))
- bottoms = bottoms.sort(sortNumber_topbottom.bind(this))
- lefts = lefts.sort(sortNumber_leftright.bind(this))
- rights = rights.sort(sortNumber_leftright.bind(this))
- this.measureLines.top = []
- this.measureLines.top.push(tops[0])
- for (let i = 0; i < tops.length - 1; ++i) {
- let start = tops[i]
- let end = null
- for (let j = i + 1; j < tops.length; ++j) {
- end = tops[j]
- if (mathUtil.getDistance(start, end) < this.minDis) {
- end = null
- ++i
- continue
- } else {
- break
- }
- }
- if (end != null) {
- this.measureLines.top.push(end)
- } else if (i == tops.length - 1) {
- let len = this.measureLines.top.length
- this.measureLines.top[len - 1] = tops[i]
- break
- }
- }
- this.measureLines.bottom = []
- this.measureLines.bottom.push(bottoms[0])
- for (let i = 0; i < bottoms.length - 1; ++i) {
- let start = bottoms[i]
- let end = null
- for (let j = i + 1; j < bottoms.length; ++j) {
- end = bottoms[j]
- if (mathUtil.getDistance(start, end) < this.minDis) {
- end = null
- ++i
- continue
- } else {
- break
- }
- }
- if (end != null) {
- this.measureLines.bottom.push(end)
- } else if (i == bottoms.length - 1) {
- let len = this.measureLines.bottom.length
- this.measureLines.bottom[len - 1] = bottoms[i]
- break
- }
- }
- this.measureLines.left = []
- this.measureLines.left.push(lefts[0])
- for (let i = 0; i < lefts.length - 1; ++i) {
- let start = lefts[i]
- let end = null
- for (let j = i + 1; j < lefts.length; ++j) {
- end = lefts[j]
- if (mathUtil.getDistance(start, end) < this.minDis) {
- end = null
- ++i
- continue
- } else {
- break
- }
- }
- if (end != null) {
- this.measureLines.left.push(end)
- } else if (i == lefts.length - 1) {
- let len = this.measureLines.left.length
- this.measureLines.left[len - 1] = lefts[i]
- break
- }
- }
- this.measureLines.right = []
- this.measureLines.right.push(rights[0])
- for (let i = 0; i < rights.length - 1; ++i) {
- let start = rights[i]
- let end = null
- for (let j = i + 1; j < rights.length; ++j) {
- end = rights[j]
- if (mathUtil.getDistance(start, end) < this.minDis) {
- end = null
- ++i
- continue
- } else {
- break
- }
- }
- if (end != null) {
- this.measureLines.right.push(end)
- } else if (i == rights.length - 1) {
- let len = this.measureLines.right.length
- this.measureLines.right[len - 1] = rights[i]
- break
- }
- }
- }
- */
- }
- const measureService = new MeasureService();
- export { measureService };
|