]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java
Data corruption on editor startup.
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / actions / RoutePipeAction.java
index 9773a9eae8c77a5c3c3a1bf350ffadaee6a0dc39..5c787037daed939790c0d8030942b9e1e5ea3aa9 100644 (file)
@@ -18,15 +18,14 @@ import org.simantics.g3d.math.Ray;
 import org.simantics.g3d.scenegraph.NodeMap;
 import org.simantics.g3d.scenegraph.base.INode;
 import org.simantics.g3d.tools.ConstraintDetector;
-import org.simantics.g3d.tools.DummyConstraintDetector;
 import org.simantics.g3d.vtk.gizmo.TranslateAxisGizmo;
 import org.simantics.g3d.vtk.swt.InteractiveVtkComposite;
 import org.simantics.g3d.vtk.swt.vtkSwtAction;
 import org.simantics.g3d.vtk.utils.vtkUtil;
 import org.simantics.plant3d.Activator;
+import org.simantics.plant3d.gizmo.ConstraintPointGizmo;
 import org.simantics.plant3d.gizmo.SplitPointSelectionGizmo;
 import org.simantics.plant3d.gizmo.TerminalSelectionGizmo;
-import org.simantics.plant3d.ontology.Plant3D;
 import org.simantics.plant3d.scenegraph.EndComponent;
 import org.simantics.plant3d.scenegraph.InlineComponent;
 import org.simantics.plant3d.scenegraph.Nozzle;
@@ -63,6 +62,7 @@ public class RoutePipeAction extends vtkSwtAction {
 
        private TranslateAxisGizmo translateAxisGizmo = new TranslateAxisGizmo();
        private SplitPointSelectionGizmo splitPointSelectionGizmo;
+       private ConstraintPointGizmo constraintPointGizmo;
        private TerminalSelectionGizmo terminalSelectionGizmo;
        private NodeMap<vtkProp,INode> nodeMap;
        
@@ -95,6 +95,7 @@ public class RoutePipeAction extends vtkSwtAction {
                nodeMap = root.getNodeMap();
                splitPointSelectionGizmo = new SplitPointSelectionGizmo(panel);
                terminalSelectionGizmo = new TerminalSelectionGizmo(panel);
+               constraintPointGizmo = new ConstraintPointGizmo(panel);
                detector = new org.simantics.g3d.vtk.swt.ConstraintDetector(panel);
        }
        
@@ -153,6 +154,8 @@ public class RoutePipeAction extends vtkSwtAction {
                        splitPointSelectionGizmo.deattach();
                if (terminalSelectionGizmo.isAttached())
                        terminalSelectionGizmo.deattach();
+               if (constraintPointGizmo.isAttached())
+                   constraintPointGizmo.deattach();
                if (infoActor != null) {
                        panel.getRenderer().RemoveActor(infoActor);
                        infoActor.Delete();
@@ -440,8 +443,7 @@ public class RoutePipeAction extends vtkSwtAction {
                startRemovable = false;
                detector.clearConstraintHighlights();
                state = ToolState.NOT_ACTIVE;
-               setEnabled(false);
-               
+               setEnabled(false);              
 
        }
        
@@ -598,7 +600,10 @@ public class RoutePipeAction extends vtkSwtAction {
        
        private void updateConstraints() {
            detector.clearConstraints();
+           constraintPointGizmo.clearPositions();
            if (hoverObject == null) {
+               if (constraintPointGizmo.isAttached())
+                   constraintPointGizmo.deattach();
                return;
            }
            if (hoverObject instanceof Nozzle) {
@@ -611,10 +616,19 @@ public class RoutePipeAction extends vtkSwtAction {
                c.getEnds(p1, p2);
                detector.addContraintPoint(p1);
                detector.addContraintPoint(p2);
+               detector.addContraintPoint(new Point3d(c.getWorldPosition()));
            } else if (hoverObject instanceof TurnComponent) {
                TurnComponent n = (TurnComponent)hoverObject;
             detector.addContraintPoint(new Point3d(n.getWorldPosition()));
            }
+           if (detector.getConstraintPoints().size() > 0) {
+               for (Point3d p : detector.getConstraintPoints()) {
+                   constraintPointGizmo.addPosition(new Vector3d(p));
+               }
+               if (constraintPointGizmo.isAttached())
+                   constraintPointGizmo.deattach();
+               constraintPointGizmo.attach(panel);
+           }
        }
        
        @Override
@@ -714,6 +728,8 @@ public class RoutePipeAction extends vtkSwtAction {
                                } else if (hoverObject instanceof InlineComponent && ((InlineComponent)hoverObject).isVariableLength()) {
                                        endTo = (InlineComponent)hoverObject;
                                        endType = endingToStraight(endTo,mu,o,d);     
+                                       if (endType == null)
+                                           endTo = null;
                                } else if (hoverObject instanceof PipelineComponent && (endPort = endingToComponent(hoverObject,o,d)) != null) {
                                        endTo = (PipelineComponent)hoverObject;
                                } else {
@@ -907,6 +923,7 @@ public class RoutePipeAction extends vtkSwtAction {
                //detector.clearConstraintHighlights();
                
                Point3d previousPipePoint = new Point3d(previousPosition);
+               Point3d currentPipePoint = new Point3d(currentPosition);
                //String st = "";
                if (lock == LockType.NONE) {
                        Point3d p = detector.getSnappedPoint(o, d, new Vector3d(previousPipePoint));
@@ -917,10 +934,9 @@ public class RoutePipeAction extends vtkSwtAction {
                                if (t != null)
                                        return t;
                                // if not, we'll have to remove highlight that was added when snapped point was detected
-                               detector.clearConstraintHighlights();
-                       } 
+                               detector.clearConstraintHighlights();   
+                       }
                        
-
                        Vector3d sDir = new Vector3d(sEnd);
                        sDir.sub(sStart);
                        MathTools.intersectStraightStraight(sStart, sDir, o, d, currentPosition, new Point3d(), mu);
@@ -936,46 +952,63 @@ 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)
-               // TODO : actually make connection if its detected
+               
                boolean connectPrev = false;
                boolean connectNext = false;
-               
-               if (mu[0] < 0.0) {
-                       currentPosition.set(sStart);
+               boolean branch = false;
+               if (mu[0] < 0.1) {
                        connectPrev = true;
                }
-               else if (mu[0] > 1.0) {
-                       currentPosition.set(sEnd);
+               else if (mu[0] > 0.9) {
                        connectNext = true;
                }
-               boolean connect = false;
+
+                 
                if (connectPrev) {
                        PipeControlPoint pcp = s.getControlPoint();
-                       if (pcp.getPrevious() == null)
-                               connect = true;
+                       if (pcp.getPrevious() != null)
+                               connectPrev = false;
                } else if (connectNext) {
                        PipeControlPoint pcp = s.getControlPoint();
-                       if (pcp.getNext() == null)
-                               connect = true;
+                       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;
+                   }
                }
                
-               updateCurrentPoint();
-               
-               if (connect)
+                       
+               if (connectNext || connectPrev)
                        info += "Connect pipes :";
-               else
-                       info += "Make Branch :";
+               else if (branch)
+                       info += "Create a Branch :";
                
                setInfoText(info + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)));
-               if (connect) {
-                       if (connectNext) {
-                               return PositionType.NEXT;
-                       } else {
-                               return PositionType.PREVIOUS;
-                       }
-                               
+               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;
                }
-               return PositionType.SPLIT;
                        
        }
        
@@ -1024,7 +1057,7 @@ public class RoutePipeAction extends vtkSwtAction {
                        
                        updateCurrentPoint();
                        
-                       setInfoText("Make branch (l) :" + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)) + " " + routePoint.lengthSquared());
+                       setInfoText("Create a branch (l) :" + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)) + " " + routePoint.lengthSquared());
                        return PositionType.SPLIT;
                }
                return null;