]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java
Simplified free ended variable length component update
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / controlpoint / PipingRules.java
index 5ec468e27993ab8531290f18537d245225773178..b096266f7af72901d2718812b9dd77d3570d3689 100644 (file)
@@ -442,12 +442,28 @@ public class PipingRules {
                updatePathLeg(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, offset, hasOffsets, iter, reversed, toRemove, updated), lengthChange);
 
        }
-
+       
+       private static boolean asDirected(PipeControlPoint pcp, Direction direction) {
+           if (pcp.isDirected())
+               return true;
+           if (pcp.isTurn() && pcp.isFixed()) {
+              if (!pcp._getReversed())
+                  return direction == Direction.NEXT;
+              else
+                  return direction == Direction.PREVIOUS;
+           }
+           return false;
+       }
+       
+       private static Vector3d direction(PipeControlPoint pcp, Direction direction) {
+           return pcp.getDirection(direction);
+       }
        private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
                int directed = 0;
-               if (u.start.isDirected())
+               if (asDirected(u.start, Direction.NEXT))
                        directed++;
-               if (u.end.isDirected())
+               if (asDirected(u.end, Direction.PREVIOUS))
                        directed++;
                switch (directed) {
                case 0:
@@ -467,7 +483,7 @@ public class PipingRules {
                if (DEBUG)
                        System.out.println("PipingRules.updateFreePipeRun " + u + " " + lengthChange);
                checkExpandPathLeg(u, lengthChange);
-               if (u.start.isInline() || u.end.isInline())
+               if (u.start.isInline() || u.end.isInline() || u.start.isFixed() || u.end.isFixed())
                        processPathLeg(u, true, false);
        }
 
@@ -475,13 +491,19 @@ public class PipingRules {
                if (DEBUG)
                        System.out.println("PipingRules.updateInlineControlPoints() " + u);
 
-               if (!u.hasOffsets) {
-                       
-                       Vector3d start = new Vector3d(u.startPoint);
-                       Vector3d end = new Vector3d(u.endPoint);
-                       // create offsets.
+               Vector3d start = new Vector3d(u.startPoint);
+               Vector3d end = new Vector3d(u.endPoint);
+               
+               if (checkSizes) {
+                       // create offsets for leg ends.
                        MathTools.mad(start, u.dir, u.start.getInlineLength());
                        MathTools.mad(end, u.dir, -u.end.getInlineLength());
+               }
+               
+               boolean recalcline = false;
+               if (!u.hasOffsets) {
+                       
+                       
                        for (PipeControlPoint icp : u.list) {
                                updateInlineControlPoint(icp, start, end, u.dir);
                                
@@ -514,7 +536,7 @@ public class PipingRules {
                                if (icp.isVariableLength()) {
                                        if (i != pathLegPoints.size() - 1) {
                                                PipeControlPoint next = pathLegPoints.get(i + 1);
-                                               updateVariableLength(icp,  prev, next);
+                                               recalcline = recalcline | updateVariableLength(icp,  prev, next);
 
                                        } else {
                                                // this is variable length component at the end of the
@@ -532,35 +554,12 @@ public class PipingRules {
                                        // space between them.
                                        // I there is, we'll have to create new variable length
                                        // component between them.
-                                       Vector3d currentPos = icp.getWorldPosition();
-                                       Vector3d prevPos = prev.getWorldPosition();
-                                       Vector3d dir = new Vector3d(currentPos);
-                                       dir.sub(prevPos);
-                                       double l = dir.lengthSquared();
-                                       double l2prev = prev.getInlineLength();
-                                       double l2next = icp.getInlineLength();
-                                       double l2 = l2prev + l2next;
-                                       double l2s = l2 * l2;
-                                       if (l > l2s) {
-                                               if (allowInsertRemove) {
-                                                       dir.normalize();
-                                                       double length = Math.sqrt(l) - l2; // true length of the
-                                                                                                                               // variable length
-                                                                                                                               // component
-                                                       dir.scale(length * 0.5 + l2prev); // calculate center
-                                                                                                                               // position of the
-                                                                                                                               // component
-                                                       dir.add(prevPos);
-                                                       PipeControlPoint scp = insertStraight(prev, icp, dir, length);
-                                               } else {
-                                                       triedIR = true;
-                                               }
-                                       }
+                                       recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
                                }
                        }
                } else { // with offset
-                       Vector3d sp = new Vector3d(u.startPoint);
-                       Vector3d ep = new Vector3d(u.endPoint);
+                       Vector3d sp = new Vector3d(start);
+                       Vector3d ep = new Vector3d(end);
                        ep.sub(u.offset);
                        
                        ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
@@ -581,6 +580,9 @@ public class PipingRules {
                        }
                        pathLegPoints.add(u.end);
                        
+                       if (!checkSizes)
+                               return; 
+                       
                        sp = new Vector3d(u.startPoint);
                        ep = new Vector3d(u.endPoint);
                        ep.sub(u.offset);
@@ -597,7 +599,7 @@ public class PipingRules {
                                        if (i != pathLegPoints.size() - 1) {
                                                PipeControlPoint next;
                                                next = pathLegPoints.get(i + 1);
-                                               updateVariableLength(icp,  prev, next);
+                                               recalcline = recalcline | updateVariableLength(icp,  prev, next);
 
                                        } else {
                                                // this is variable length component at the end of the
@@ -607,7 +609,15 @@ public class PipingRules {
                                                // place, but center of the component must be moved.
                                                updateVariableLengthEnd(icp, prev);
                                        }
-                               } else if (icp.isOffset()) {
+                               } else if (!prev.isVariableLength()) {
+                                       // If this and previous control point are not variable
+                                       // length pcps, we'll have to check if there is no empty
+                                       // space between them.
+                                       // I there is, we'll have to create new variable length
+                                       // component between them.
+                                       recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
+                               }
+                               if (icp.isOffset()) {
                                        // TODO : offset vector is already calculated and should be
                                        // cached
                                        Vector3d  offset = icp.getSizeChangeOffsetVector(u.dir);
@@ -616,9 +626,13 @@ public class PipingRules {
                                }
                        }
                }
+               if (recalcline) {
+                       u.list.clear();
+                       u.start.findNextEnd(u.list);
+               }
        }
        
-       private static void updateVariableLength(PipeControlPoint icp, PipeControlPoint prev,  PipeControlPoint next) {
+       private static boolean updateVariableLength(PipeControlPoint icp, PipeControlPoint prev,  PipeControlPoint next) {
                Vector3d prevPos = prev.getWorldPosition();
                Vector3d nextPos = next.getWorldPosition();
                
@@ -649,6 +663,7 @@ public class PipingRules {
                        dir.add(prevPos);
                        icp.setWorldPosition(dir);
                        icp.setLength(length);
+                       return false;
                } else {
                        // components leave no space to the component and it
                        // must be removed
@@ -657,48 +672,83 @@ public class PipingRules {
                                if (DEBUG)
                                        System.out.println("PipingRules.updateVariableLength removing " + icp);
                                icp._remove();
+                               return true;
                        }
+                       return false;
                }
        }
        
-       private static void updateVariableLengthEnd(PipeControlPoint icp,  PipeControlPoint prev) {
-               double currentLength = icp.getLength();
+       private static boolean possibleVaribleLengthInsert(PipeControlPoint icp, PipeControlPoint prev) throws Exception{
                Vector3d currentPos = icp.getWorldPosition();
                Vector3d prevPos = prev.getWorldPosition();
-               
-               Vector3d dir = new Vector3d();
-               dir.sub(currentPos, prevPos);
-               
-               if (currentLength < MathTools.NEAR_ZERO) {
-                       currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
+               Vector3d dir = new Vector3d(currentPos);
+               dir.sub(prevPos);
+               double l = dir.lengthSquared();
+               double l2prev = prev.getInlineLength();
+               double l2next = icp.getInlineLength();
+               double l2 = l2prev + l2next;
+               double l2s = l2 * l2;
+               if (l > l2s) {
+                       if (allowInsertRemove) {
+                               dir.normalize();
+                               double length = Math.sqrt(l) - l2; // true length of the
+                                                                                                       // variable length
+                                                                                                       // component
+                               dir.scale(length * 0.5 + l2prev); // calculate center
+                                                                                                       // position of the
+                                                                                                       // component
+                               dir.add(prevPos);
+                               PipeControlPoint scp = insertStraight(prev, icp, dir, length);
+                               return true;
+                       } else {
+                               triedIR = true;
+                       }
                }
-               
-               if (dir.lengthSquared() > MathTools.NEAR_ZERO)
-                       dir.normalize();
-               Point3d endPos = new Point3d(dir);
-               endPos.scale(currentLength * 0.5);
-               endPos.add(currentPos); // this is the free end of the
-                                                               // component
-
-               double offset = prev.getInlineLength();
-               Point3d beginPos = new Point3d(dir);
-               beginPos.scale(offset);
-               beginPos.add(prevPos); // this is the connected end of
-                                                               // the component
-
-               double l = beginPos.distance(endPos);
-               
-               if (Double.isNaN(l))
-                       System.out.println("Length for " + icp + " is NaN");
-
-               dir.scale(l * 0.5);
-               beginPos.add(dir); // center position
-
-               if (DEBUG)
-                       System.out.println("PipingRules.updateInlineControlPoints() setting variable length to " + l);
-               icp.setLength(l);
-
-               icp.setWorldPosition(new Vector3d(beginPos));
+               return false;
+       }
+       
+       private static void updateVariableLengthEnd(PipeControlPoint icp,  PipeControlPoint prev) {
+           Vector3d currentPos = icp.getWorldPosition();
+        Vector3d prevPos = prev.getWorldPosition();
+        
+        Vector3d dir = new Vector3d();
+        dir.sub(currentPos, prevPos);
+        
+           boolean simple = true;
+           if (simple) {
+               double currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
+               icp.setLength(currentLength);
+           } else {
+               double currentLength = icp.getLength();
+               if (currentLength < MathTools.NEAR_ZERO) {
+                       currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
+               }
+               
+               if (dir.lengthSquared() > MathTools.NEAR_ZERO)
+                       dir.normalize();
+               Point3d endPos = new Point3d(dir);
+               endPos.scale(currentLength * 0.5);
+               endPos.add(currentPos); // this is the free end of the component
+    
+               double offset = prev.getInlineLength();
+               Point3d beginPos = new Point3d(dir);
+               beginPos.scale(offset);
+               beginPos.add(prevPos); // this is the connected end of the component
+    
+               double l = beginPos.distance(endPos);
+               
+               if (Double.isNaN(l))
+                       System.out.println("Length for " + icp + " is NaN");
+    
+               dir.scale(l * 0.5);
+               beginPos.add(dir); // center position
+    
+               if (DEBUG)
+                       System.out.println("PipingRules.updateInlineControlPoints() setting variable length to " + l);
+               icp.setLength(l);
+    
+               icp.setWorldPosition(new Vector3d(beginPos));
+           }
        }
 
        private static void ppNoOffset(UpdateStruct2 u) throws Exception {
@@ -759,7 +809,7 @@ public class PipingRules {
                boolean dcpStart = false;
                boolean inlineEnd = false;
                Vector3d position;
-               if (u.start.isDirected()) {
+               if (asDirected(u.start, Direction.NEXT)) {
                        dcp = u.start;
                        other = u.end;
                        position = u.startPoint;
@@ -777,7 +827,7 @@ public class PipingRules {
                        inlineEnd = u.start.isInline();
                }
 
-               Vector3d directedDirection = dcp.getDirection();
+               Vector3d directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
                Point3d directedEndPoint = new Point3d(u.endPoint);
                if (u.hasOffsets)
                        directedEndPoint.add(u.offset);
@@ -798,6 +848,8 @@ public class PipingRules {
                double distance = t.length();
                boolean aligned = (distance < ALLOWED_OFFSET);
                if (aligned) {
+                   if (u.start.isInline() || u.end.isInline() || u.start.isFixed() || u.end.isFixed())
+                   processPathLeg(u, true, false);
                        checkExpandPathLeg(u, lengthChange, inlineEnd);
                        
                } else {
@@ -914,8 +966,8 @@ public class PipingRules {
                position1offset.sub(u.offset);
                Point3d position2offset = new Point3d(position2);
                position2offset.add(u.offset);
-               Vector3d dir1 = dcp1.getDirection();
-               Vector3d dir2 = dcp2.getDirection();
+               Vector3d dir1 = direction(dcp1, Direction.NEXT);
+               Vector3d dir2 = direction(dcp2, Direction.PREVIOUS);
                Vector3d p1 = MathTools.closestPointOnStraight(position1offset, position2, dir2);
                Vector3d p2 = MathTools.closestPointOnStraight(position2offset, position1, dir1);
                double d1 = position1.distance(new Point3d(p1));
@@ -1243,8 +1295,7 @@ public class PipingRules {
                                        info.getEnd()._remove();
                                }
                        }
-                       // ControlPointTools.removeControlPoint may remove mo0re than one
-                       // CP;
+                       // ControlPointTools.removeControlPoint may remove more than one CP;
                        // we must populate inline CP list again.
                        u.list.clear();
                        u.start.findNextEnd( u.list);
@@ -1446,8 +1497,11 @@ public class PipingRules {
                double angle = 0.0;
                if (angleO != null)
                        angle = angleO;
-
-               Quat4d q = pcp.getControlPointOrientationQuat(angle);
+               Boolean reversedO = pcp.getReversed();
+               boolean reversed = false;
+               if (reversedO != null)
+                       reversed = reversedO;
+               Quat4d q = pcp.getControlPointOrientationQuat(angle, reversed);
                pcp.setWorldOrientation(q);
        }
 
@@ -1483,12 +1537,43 @@ public class PipingRules {
        private static double updateTurnControlPointTurn(PipeControlPoint tcp, PipeControlPoint prev, PipeControlPoint next) {
                if (DEBUG)
                        System.out.println("PipingTools.updateTurnControlPointTurn()" + tcp);
-               if (next == null || prev == null)
-                       return Math.PI; // FIXME : argh
-               Vector3d middlePoint = tcp.getWorldPosition();
-               Vector3d nextPoint = next.getWorldPosition();
-               Vector3d prevPoint = prev.getWorldPosition();
-               return updateTurnControlPointTurn(tcp, middlePoint, prevPoint, nextPoint);
+               
+               if (!tcp.isFixed()) {
+                   if (next == null || prev == null)
+                   return Math.PI; // FIXME : argh
+               Vector3d middlePoint = tcp.getWorldPosition();
+               Vector3d nextPoint = next.getWorldPosition();
+               Vector3d prevPoint = prev.getWorldPosition();
+               return updateTurnControlPointTurn(tcp, middlePoint, prevPoint, nextPoint);
+               } else {
+                   Vector3d dir;
+                   if (!tcp._getReversed()) {
+                   if (prev == null)
+                   return Math.PI; // FIXME : argh
+                   
+                   Vector3d middlePoint = tcp.getWorldPosition();
+                   Vector3d prevPoint = prev.getWorldPosition();
+                   dir = new Vector3d();
+                   dir.sub(middlePoint, prevPoint);        
+                  
+                   } else {
+                       if (next == null)
+                    return Math.PI; // FIXME : argh
+                
+                Vector3d middlePoint = tcp.getWorldPosition();
+                Vector3d nextPoint = next.getWorldPosition();
+                dir = new Vector3d();
+                dir.sub(nextPoint,middlePoint);
+                   }
+                   dir.normalize();
+            
+            Quat4d q = PipeControlPoint.getControlPointOrientationQuat(dir, tcp.getRotationAngle() != null ? tcp.getRotationAngle() : 0.0);
+            Vector3d v = new Vector3d();
+            MathTools.rotate(q, MathTools.Y_AXIS,v);
+            tcp.setTurnAxis(v);
+            tcp.setWorldOrientation(q);
+            return tcp.getTurnAngle();
+               }
        }
 
        /**
@@ -1530,7 +1615,7 @@ public class PipingRules {
                        turnAngle = 0.0;
                        tcp.setTurnAngle(0.0);
                        tcp.setLength(0.0);
-                       tcp.setTurnAxis(MathTools.Y_AXIS);
+                       tcp.setTurnAxis(new Vector3d(MathTools.Y_AXIS));
                }
                updateControlPointOrientation(tcp);
                if (DEBUG)
@@ -1643,18 +1728,18 @@ public class PipingRules {
                        return;
                Collection<PipeControlPoint> pcps = pipeRun.getControlPoints();
                int count = 0;
+               //System.out.println("Validate " + pipeRun.getName());
                for (PipeControlPoint pcp : pcps) {
                        if (pcp.getParentPoint() == null || pcp.getParentPoint().getPipeRun() != pipeRun)
                                count++;
                }
                List<PipeControlPoint> runPcps = getControlPoints(pipeRun);
                if (runPcps.size() != count) {
-                       System.out.println("Run is not connected");
+                       System.out.println("Run " + pipeRun.getName() + " contains unconnected control points");
                }
                for (PipeControlPoint pcp : pcps) {
-                       if (!pcp.isDirected())
-                               if (pcp.getNext() == null && pcp.getPrevious() == null)
-                                       System.out.println("Orphan " + pcp);
+                       if (!pcp.isDirected() && pcp.getNext() == null && pcp.getPrevious() == null)
+                               System.out.println("Orphan undirected " + pcp);
                }
                for (PipeControlPoint pcp : pcps) {
                        if (pcp.getParentPoint() == null) {