Browse Source

Merge branch 'master' of http://192.168.0.115:3000/bill/traffic-laser

bill 2 years ago
parent
commit
aff1915c15

+ 4 - 4
src/graphic/Renderer/Render.js

@@ -91,10 +91,10 @@ export default class Render {
       this.drawGeometry(roads[key]);
     }
 
-    let points = dataService.getPoints();
-    for (let key in points) {
-      this.drawGeometry(points[key]);
-    }
+    // let points = dataService.getPoints();
+    // for (let key in points) {
+    //   this.drawGeometry(points[key]);
+    // }
 
     let controlPoints = dataService.getControlPoints();
     for (let key in controlPoints) {

+ 93 - 12
src/graphic/Service/ControlPointService.js

@@ -2,6 +2,7 @@ import ControlPoint from "../Geometry/ControlPoint.js";
 import { dataService } from "./DataService.js";
 import { mathUtil } from "../Util/MathUtil";
 import { edgeService } from "./EdgeService.js";
+import { roadService } from "./RoadService.js";
 
 export default class ControlPointService {
   constructor() {}
@@ -23,24 +24,24 @@ export default class ControlPointService {
     //更新edge的坐标
     let position1 = mathUtil.getJoinLinePoint(position, line1);
     let position2 = mathUtil.getJoinLinePoint(position, line2);
+
     //可能position1或者position2不在对应的edge上,这时候需要调换顺序
-    if (
-      !mathUtil.isContainForSegment(position1, edge1.start, edge1.end) ||
-      !mathUtil.isContainForSegment(position2, edge2.start, edge2.end)
-    ) {
-      position = mathUtil.getPositionForExtendedLine(point2, point1);
-      position1 = mathUtil.getJoinLinePoint(position, line1);
-      position2 = mathUtil.getJoinLinePoint(position, line2);
+    if (!this.isPointOnEdgeRay(edge1, dir1, position)) {
+      let _position = mathUtil.getPositionForExtendedLine(point2, point1);
+      position1 = mathUtil.getJoinLinePoint(_position, line1);
+    }
+
+    if (!this.isPointOnEdgeRay(edge2, dir2, position)) {
+      let _position = mathUtil.getPositionForExtendedLine(point2, point1);
+      position2 = mathUtil.getJoinLinePoint(_position, line2);
     }
 
-    // const realRosition = mathUtil.getPositionForExtendedLine(point2, point1);
-    // const realRosition = {
-    //   x: (point1.x + point2.x) / 2,
-    //   y: (point1.y + point2.y) / 2,
-    // };
     if (controlPoint == null) {
       //新建控制点
       controlPoint = this.create(realRosition);
+      //需要删除之前的控制点
+      dataService.deleteControlPointForEdge(edge1.vectorId);
+      dataService.deleteControlPointForEdge(edge2.vectorId);
       //设置控制点的信息
       controlPoint.setEdgeInfo(edge1.vectorId, dir1, edge2.vectorId, dir2);
       //添加到数据集里
@@ -53,6 +54,86 @@ export default class ControlPointService {
     edge1.setPosition(position1, dir1);
     edge2.setPosition(position2, dir2);
   }
+
+  //position在直线edge.start,edge.end上
+  // isPointOnEdgeRay(edge, dir, position) {
+  //   let point1, point2;
+  //   if (dir == "start") {
+  //     point1 = edge.start;
+  //     point2 = edge.end;
+  //   } else if (dir == "end") {
+  //     point1 = edge.end;
+  //     point2 = edge.start;
+  //   }
+
+  //   let v1 = {
+  //     x: position.x - point1.x,
+  //     y: position.y - point1.y,
+  //   };
+
+  //   let v2 = {
+  //     x: point2.x - point1.x,
+  //     y: point2.y - point1.y,
+  //   };
+
+  //   let v = {
+  //     x: v1.x + v2.x,
+  //     y: v1.y + v2.y,
+  //   };
+
+  //   let d = mathUtil.getDistance(v, { x: 0, y: 0 });
+  //   let d1 = mathUtil.getDistance(position, point1);
+  //   let d2 = mathUtil.getDistance(point2, point1);
+
+  //   if (Math.abs(d - d1 - d2) < 0.01) {
+  //     return true;
+  //   } else {
+  //     return false;
+  //   }
+  // }
+
+  //角度小,road宽的时候,可能edge顺序会倒,但是road的顺序不会
+  isPointOnEdgeRay(edge, dir, position) {
+    let road = dataService.getRoad(edge.parent);
+    let point1, point2;
+    if (dir == "start") {
+      point1 = dataService.getPoint(road.startId);
+      point2 = dataService.getPoint(road.endId);
+    } else if (dir == "end") {
+      point1 = dataService.getPoint(road.endId);
+      point2 = dataService.getPoint(road.startId);
+    }
+
+    position = mathUtil.getJoinLinePoint(
+      position,
+      roadService.getMidLine(road)
+    );
+
+    let v1 = {
+      x: position.x - point1.x,
+      y: position.y - point1.y,
+    };
+
+    let v2 = {
+      x: point2.x - point1.x,
+      y: point2.y - point1.y,
+    };
+
+    let v = {
+      x: v1.x + v2.x,
+      y: v1.y + v2.y,
+    };
+
+    let d = mathUtil.getDistance(v, { x: 0, y: 0 });
+    let d1 = mathUtil.getDistance(position, point1);
+    let d2 = mathUtil.getDistance(point2, point1);
+
+    if (Math.abs(d - d1 - d2) < 0.2) {
+      return true;
+    } else {
+      return false;
+    }
+  }
 }
 
 const controlPointService = new ControlPointService();

+ 17 - 0
src/graphic/Service/DataService.js

@@ -139,6 +139,23 @@ export class DataService {
     }
   }
 
+  deleteControlPointForEdge(edgeId) {
+    let dControlPointIds = [];
+    for (let key in this.vectorData.controlPoints) {
+      const controlPoint = this.vectorData.controlPoints[key];
+      if (
+        controlPoint.edgeInfo1.id == edgeId ||
+        controlPoint.edgeInfo2.id == edgeId
+      ) {
+        dControlPointIds.push(key);
+      }
+    }
+
+    for (let i = 0; i < dControlPointIds.length; ++i) {
+      delete this.vectorData.controlPoints[dControlPointIds[i]];
+    }
+  }
+
   /**
    * 对公路边缘的操作
    */

+ 22 - 0
src/graphic/Service/EdgeService.js

@@ -492,6 +492,8 @@ export default class EdgeService {
         console.log("情况1");
         mathUtil.clonePoint(leftEdge1.start, newEdgePoint1);
         mathUtil.clonePoint(rightEdge2.start, newEdgePoint1);
+        //要删除控制点
+        dataService.deleteControlPoint(rightEdge1.vectorId, leftEdge2.vectorId);
         if (angle > Constant.maxAngle) {
           //要删除控制点
           dataService.deleteControlPoint(
@@ -509,11 +511,15 @@ export default class EdgeService {
             "start",
             "start"
           );
+
+          //需要删除之前的控制点,包含:leftEdge1.vectorId和rightEdge2.vectorId
         }
       } else {
         console.log("情况2");
         mathUtil.clonePoint(rightEdge1.start, newEdgePoint2);
         mathUtil.clonePoint(leftEdge2.start, newEdgePoint2);
+        //要删除控制点
+        dataService.deleteControlPoint(leftEdge1.vectorId, rightEdge2.vectorId);
         if (angle > Constant.maxAngle) {
           //要删除控制点
           dataService.deleteControlPoint(
@@ -558,6 +564,11 @@ export default class EdgeService {
         console.log("情况3");
         mathUtil.clonePoint(leftEdge1.start, newEdgePoint1);
         mathUtil.clonePoint(leftEdge2.end, newEdgePoint1);
+        //要删除控制点
+        dataService.deleteControlPoint(
+          rightEdge1.vectorId,
+          rightEdge2.vectorId
+        );
         if (angle > Constant.maxAngle) {
           //要删除控制点
           dataService.deleteControlPoint(
@@ -580,6 +591,8 @@ export default class EdgeService {
         console.log("情况4");
         mathUtil.clonePoint(rightEdge1.start, newEdgePoint2);
         mathUtil.clonePoint(rightEdge2.end, newEdgePoint2);
+        //要删除控制点
+        dataService.deleteControlPoint(leftEdge1.vectorId, leftEdge2.vectorId);
         if (angle > Constant.maxAngle) {
           //要删除控制点
           dataService.deleteControlPoint(
@@ -624,6 +637,11 @@ export default class EdgeService {
         console.log("情况5");
         mathUtil.clonePoint(leftEdge1.end, newEdgePoint1);
         mathUtil.clonePoint(leftEdge2.start, newEdgePoint1);
+        //要删除控制点
+        dataService.deleteControlPoint(
+          rightEdge1.vectorId,
+          rightEdge2.vectorId
+        );
         if (angle > Constant.maxAngle) {
           //要删除控制点
           dataService.deleteControlPoint(
@@ -646,6 +664,8 @@ export default class EdgeService {
         console.log("情况6");
         mathUtil.clonePoint(rightEdge1.end, newEdgePoint2);
         mathUtil.clonePoint(rightEdge2.start, newEdgePoint2);
+        //要删除控制点
+        dataService.deleteControlPoint(leftEdge1.vectorId, leftEdge2.vectorId);
         if (angle > Constant.maxAngle) {
           //要删除控制点
           dataService.deleteControlPoint(
@@ -690,6 +710,8 @@ export default class EdgeService {
         console.log("情况7");
         mathUtil.clonePoint(leftEdge1.end, newEdgePoint1);
         mathUtil.clonePoint(rightEdge2.end, newEdgePoint1);
+        //要删除控制点
+        dataService.deleteControlPoint(leftEdge2.vectorId, rightEdge1.vectorId);
         if (angle > Constant.maxAngle) {
           //要删除控制点
           dataService.deleteControlPoint(

+ 7 - 1
src/graphic/Util/MathUtil.js

@@ -574,7 +574,7 @@ export default class MathUtil {
     return (
       dot_product_AP >= 0 &&
       dot_product_BP <= 0 &&
-      Math.abs(AP.x * BP.y - AP.y * BP.x) <= 0.001
+      Math.abs(AP.x * BP.y - AP.y * BP.x) <= 0.01
     );
   }
 
@@ -992,6 +992,12 @@ export default class MathUtil {
     };
     return point;
   }
+
+  isOnRay(start, dir, position) {
+    const v1 = { x: dir.x - start.x, y: dir.y - start.y };
+    const v2 = { x: position.x - start.x, y: position.y - start.y };
+    return v1.x * v2.y - v1.y * v2.x;
+  }
 }
 
 const mathUtil = new MathUtil();