]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Disabling branch creation prevented connections 52/3452/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 5 Nov 2019 16:35:20 +0000 (18:35 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 5 Nov 2019 16:35:20 +0000 (18:35 +0200)
Additionally, if connected pipes are not aligned, they are connected
with turn component.

gitlab #36

Change-Id: If6040fe024187fbed464faacd93ad8b677bb6a4f

org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java
org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/TurnComponent.java

index 7cc13cd46db010e080a097e5b487c55c56eda6d5..7de27a31cde15fc90d939bd19df5e54908387ff2 100644 (file)
@@ -54,6 +54,8 @@ public class RoutePipeAction extends vtkSwtAction {
        boolean lockForced;
        private double BRANCH_SNAP_DISTANCE = 0.05;
        private double NOZZLE_SNAP_DISTANCE = 0.05;
+       private static double BRANCH_DOT_PRODUCT = 0.95; // dot product value used for prevent branch creation
+       private static double ALIGN_DOT_PRODUCT = 0.99; // dot product for creating turn when connecting pipes
 
        private double istep = 10.0;
        private int decimals = 2;
@@ -759,7 +761,7 @@ public class RoutePipeAction extends vtkSwtAction {
                                        updateRoute(o,d); 
                                }
                        } else {  
-                               if (hoverObject instanceof InlineComponent && ((InlineComponent)hoverObject).isVariableLength() && (endType = endingLockToStraight(hoverObject,mu)) != null) {
+                               if (hoverObject instanceof InlineComponent && ((InlineComponent)hoverObject).isVariableLength() && (endType = endingLockToStraight(hoverObject,mu, new Point3d(currentPosition))) != null) {
                                        endTo = (InlineComponent)hoverObject;
                                } else if (hoverObject instanceof Nozzle && endingLockToNozzle(hoverObject)) {
                                        endTo = (Nozzle)hoverObject;
@@ -938,12 +940,12 @@ public class RoutePipeAction extends vtkSwtAction {
        }
        
        private PositionType endingToStraight(INode straightNode, double mu[], Vector3d o, Vector3d d) {
-           if (!allowBranches) {
-               updateCurrentPoint();
-            return null;
-           }
+//         if (!allowBranches) {
+//             updateCurrentPoint();
+//            return null;
+//         }
                InlineComponent s = (InlineComponent)straightNode;
-               String info = "";
+               
                Point3d sStart = new Point3d();
                Point3d sEnd = new Point3d();
                s.getEnds(sStart, sEnd);
@@ -957,7 +959,7 @@ public class RoutePipeAction extends vtkSwtAction {
                        if (p != null) {
                                currentPosition = new Vector3d(p);
                                // snapping is detected, check if snapped point can create branch with straight
-                               PositionType t = endingLockToStraight(s, mu);
+                               PositionType t = endingLockToStraight(s, mu, currentPipePoint);
                                if (t != null)
                                        return t;
                                // if not, we'll have to remove highlight that was added when snapped point was detected
@@ -979,64 +981,68 @@ public class RoutePipeAction extends vtkSwtAction {
                // branch point must lie between straight's ends. If connection point is exactly
                // on straight end user may want to connect pipes to each other
                // TODO : take account sizes of inline components)
-               
-               boolean connectPrev = false;
-               boolean connectNext = false;
-               boolean branch = false;
-               if (mu[0] < 0.1) {
-                       connectPrev = true;
-               }
-               else if (mu[0] > 0.9) {
-                       connectNext = true;
-               }
+               return endingToStraight(mu, s, sStart, sEnd, currentPipePoint);
+       }
+       
+       private PositionType endingToStraight(double mu[], InlineComponent s, Point3d sStart, Point3d sEnd , Point3d currentPipePoint) {
+           String info = "";
+           
+           boolean connectPrev = false;
+        boolean connectNext = false;
+        boolean branch = false;
+        if (mu[0] < 0.1) {
+            connectPrev = true;
+        }
+        else if (mu[0] > 0.9) {
+            connectNext = true;
+        }
 
-                 
-               if (connectPrev) {
-                       PipeControlPoint pcp = s.getControlPoint();
-                       if (pcp.getPrevious() != null)
-                               connectPrev = false;
-               } else if (connectNext) {
-                       PipeControlPoint pcp = s.getControlPoint();
-                       if (pcp.getNext() != null)
-                               connectNext = false;
-               } else {
-                   Vector3d dir = s.getControlPoint().getPathLegDirection(Direction.NEXT);
-                   Vector3d currDir = getLast().getControlPoint().getPathLegDirection(Direction.NEXT);
-                   dir.normalize();
-                   currDir.normalize();
-                   double dot = dir.dot(currDir);
-                   System.out.println(dot + " " + currDir + " " + dir);
-                   if (dot > 0.95 || dot < -0.95) {
-                       // pipes are almost in the same direction, creating a branch is not feasible.
-                       branch = false;
-                   } else {
-                       branch = true;
-                   }
-               }
-               
-                       
-               if (connectNext || connectPrev)
-                       info += "Connect pipes :";
-               else if (branch)
-                       info += "Create a Branch :";
-               
-               setInfoText(info + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)));
-               if (connectNext) {
-                   currentPosition.set(sEnd);
-                   updateCurrentPoint();
-                       return PositionType.NEXT;
-               } else if (connectPrev){
-                   currentPosition.set(sStart);
-                   updateCurrentPoint();
-                       return PositionType.PREVIOUS;
-               } else if (branch) {
-                   return PositionType.SPLIT;
-               } else {
-                   currentPosition.set(currentPipePoint);
-                   updateCurrentPoint();
-                   return null;
-               }
-                       
+          
+        if (connectPrev) {
+            PipeControlPoint pcp = s.getControlPoint();
+            if (pcp.getPrevious() != null)
+                connectPrev = false;
+        } else if (connectNext) {
+            PipeControlPoint pcp = s.getControlPoint();
+            if (pcp.getNext() != null)
+                connectNext = false;
+        } else {
+            Vector3d dir = s.getControlPoint().getPathLegDirection(Direction.NEXT);
+            Vector3d currDir = getLast().getControlPoint().getPathLegDirection(Direction.NEXT);
+            dir.normalize();
+            currDir.normalize();
+            double dot = dir.dot(currDir);
+            System.out.println(dot + " " + currDir + " " + dir);
+            if (dot > BRANCH_DOT_PRODUCT || dot < -BRANCH_DOT_PRODUCT) {
+                // pipes are almost in the same direction, creating a branch is not feasible.
+                branch = false;
+            } else {
+                branch = true;
+            }
+        }
+        
+            
+        if (connectNext || connectPrev)
+            info += "Connect pipes :";
+        else if (branch)
+            info += "Create a Branch :";
+        
+        setInfoText(info + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)));
+        if (connectNext) {
+            currentPosition.set(sEnd);
+            updateCurrentPoint();
+            return PositionType.NEXT;
+        } else if (connectPrev){
+            currentPosition.set(sStart);
+            updateCurrentPoint();
+            return PositionType.PREVIOUS;
+        } else if (branch && allowBranches) {
+            return PositionType.SPLIT;
+        } else {
+            currentPosition.set(currentPipePoint);
+            updateCurrentPoint();
+            return null;
+        }
        }
        
        private PipeControlPoint endingToComponent(INode componentNode, Vector3d o, Vector3d d) {
@@ -1060,11 +1066,11 @@ public class RoutePipeAction extends vtkSwtAction {
                return null;
        }
        
-       private PositionType endingLockToStraight(INode straightNode, double mu[]) {
-           if (!allowBranches) {
-            updateCurrentPoint();
-            return null;
-        }
+       private PositionType endingLockToStraight(INode straightNode, double mu[], Point3d currentPipePoint) {
+//         if (!allowBranches) {
+//            updateCurrentPoint();
+//            return null;
+//        }
                InlineComponent s = (InlineComponent)straightNode;
                Point3d sStart = new Point3d();
                Point3d sEnd = new Point3d(); 
@@ -1083,15 +1089,21 @@ public class RoutePipeAction extends vtkSwtAction {
                // startPoint of branch must be between pipe ends
                // TODO : take account sizes of elbows (or other components)
                // branch point must be between pipe ends and intersection points must be quite close to each other
-               if (mu[0] > 0.0 && mu[0] < 1.0 && routePoint.lengthSquared() < BRANCH_SNAP_DISTANCE) {
-                       currentPosition.set(branchPoint);
-                       
-                       updateCurrentPoint();
-                       
-                       setInfoText("Create a branch (l) :" + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)) + " " + routePoint.lengthSquared());
-                       return PositionType.SPLIT;
-               }
-               return null;
+               
+               if (routePoint.lengthSquared() > BRANCH_SNAP_DISTANCE)
+                   return null;
+               
+               return endingToStraight(mu, s, sStart, sEnd, currentPipePoint);
+               
+//             if (mu[0] > 0.0 && mu[0] < 1.0 && routePoint.lengthSquared() < BRANCH_SNAP_DISTANCE) {
+//                     currentPosition.set(branchPoint);
+//                     
+//                     updateCurrentPoint();
+//                     
+//                     setInfoText("Create a branch (l) :" + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)) + " " + routePoint.lengthSquared());
+//                     return PositionType.SPLIT;
+//             }
+//             return null;
        }
        
        private boolean endingLockToNozzle(INode nozzleNode) {
@@ -1119,40 +1131,64 @@ public class RoutePipeAction extends vtkSwtAction {
                return null;
        }
        
+       protected void addTurn() throws Exception{
+           InlineComponent previous = (InlineComponent)getLast();
+        PipeControlPoint previousCP = previous.getControlPoint();
+        TurnComponent turn = ComponentUtils.createTurn(root);
+        PipeControlPoint turnCP = turn.getControlPoint();
+        turn.setName(pipeRun.getUniqueName("Elbow"));
+        pipeRun.addChild(turn);
+        added.add(turn);
+        turnCP.setDeletable(false); // mark turnCP nonDeletable so that PipingRules won't delete it immediately.
+        if (!reversed) {
+            previousCP.setNext(turnCP);
+            turnCP.setPrevious(previousCP);
+        } else {
+            previousCP.setPrevious(turnCP);
+            turnCP.setNext(previousCP);
+        }
+        
+        turnCP.setWorldPosition(currentPosition);
+        turnCP.setTurnAngle(0.0);
+        turnCP.setLength(0.0);
+        
+       }
+       
+       protected void addStraight() throws Exception{
+        TurnComponent turn = (TurnComponent)getLast();
+        PipeControlPoint turnCP = turn.getControlPoint();
+        
+        InlineComponent straight = ComponentUtils.createStraight(root);
+        
+        PipeControlPoint straightCP = straight.getControlPoint();
+        straight.setName(pipeRun.getUniqueName("Pipe"));
+        
+        
+        pipeRun.addChild(straight);
+        
+        added.add(straight);
+        
+        
+        
+        if (!reversed) {
+            turnCP.setNext(straightCP);
+            straightCP.setPrevious(turnCP);
+        } else {
+            turnCP.setPrevious(straightCP);
+            straightCP.setNext(turnCP);
+        }
+        
+        turnCP.setWorldPosition(currentPosition);
+        turnCP.setTurnAngle(0.0);
+        turnCP.setLength(0.0);
+        straightCP.setWorldPosition(currentPosition);
+        straightCP.setLength(0.0);
+           
+       }
+       
        protected void addPoint() throws Exception {
-               InlineComponent previous = (InlineComponent)getLast();
-               PipeControlPoint previousCP = previous.getControlPoint();
-               TurnComponent turn = ComponentUtils.createTurn(root);
-               InlineComponent straight = ComponentUtils.createStraight(root);
-               PipeControlPoint turnCP = turn.getControlPoint();
-               PipeControlPoint straightCP = straight.getControlPoint();
-               straight.setName(pipeRun.getUniqueName("Pipe"));
-               turn.setName(pipeRun.getUniqueName("Elbow"));
-               pipeRun.addChild(turn);
-               pipeRun.addChild(straight);
-               added.add(turn);
-               added.add(straight);
-               
-               turnCP.setDeletable(false); // mark turnCP nonDeletable so that PipingRules won't delete it immediately.
-               
-               if (!reversed) {
-                       previousCP.setNext(turnCP);
-                       turnCP.setPrevious(previousCP);
-                       turnCP.setNext(straightCP);
-                       straightCP.setPrevious(turnCP);
-               } else {
-                       previousCP.setPrevious(turnCP);
-                       turnCP.setNext(previousCP);
-                       turnCP.setPrevious(straightCP);
-                       straightCP.setNext(turnCP);
-               }
-               
-               turnCP.setWorldPosition(currentPosition);
-               turnCP.setTurnAngle(0.0);
-               turnCP.setLength(0.0);
-               straightCP.setWorldPosition(currentPosition);
-               straightCP.setLength(0.0);
-               
+               addTurn();
+               addStraight();
                setPreviousPosition(currentPosition);
                updateCurrentPoint();
                
@@ -1220,6 +1256,18 @@ public class RoutePipeAction extends vtkSwtAction {
                state = ToolState.NOT_ACTIVE;
                
                if (endTo != null) {
+                   if (endType == PositionType.NEXT || endType == PositionType.PREVIOUS && endTo instanceof InlineComponent) {
+                       Vector3d dir = endTo.getControlPoint().getPathLegDirection(Direction.NEXT);
+                   Vector3d currDir = getLast().getControlPoint().getPathLegDirection(Direction.NEXT);
+                   dir.normalize();
+                   currDir.normalize();
+                   double dot = dir.dot(currDir);
+                   System.out.println(dot + " " + currDir + " " + dir);
+                   if (dot < ALIGN_DOT_PRODUCT && dot> -ALIGN_DOT_PRODUCT) {
+                       addTurn();    
+                   }
+                       
+                   }
                        ComponentUtils.connect(getLast(), endTo, endType, currentPosition);
                }
                panel.useDefaultAction();
index 3d5fb5bf7fc27130d078414dde5da2a72bbd17c9..cd490a5d38aa9b7965e6f9b2daf3a9cf5d5f2bc3 100644 (file)
@@ -80,8 +80,12 @@ public class TurnComponent extends PipelineComponent {
                }
        }
        
-       @RelatedGetValue(Plant3D.URIs.HasTurnAngle)
        public Double getTurnAngle() {
+           return getControlPoint().getTurnAngle();
+       }
+       
+       @RelatedGetValue(Plant3D.URIs.HasTurnAngle)
+       public Double _getTurnAngle() {
            if (!getControlPoint().asFixedAngle())
                return null;
                return getControlPoint().getTurnAngle();