]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Fix issue in directed path updates with offsets 93/3693/3
authorReino Ruusu <reino.ruusu@semantum.fi>
Tue, 10 Dec 2019 16:09:22 +0000 (18:09 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Tue, 10 Dec 2019 18:35:01 +0000 (20:35 +0200)
gitlab #67

Change-Id: I9547e20b1364b16c7a3e83eaba2e968a1a6d73e7

org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java

index e8e39c081d7c1461e297777bd08336335a9641e9..d27de99b2fd4142be90cfc34a458f50d0228ed36 100644 (file)
@@ -363,26 +363,18 @@ public class PipingRules {
 
        }
 
-       @SuppressWarnings("unused")
+       private static boolean calculateDirectedOffset(Vector3d startPoint, Vector3d endPoint, PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, Vector3d dir, Vector3d offset) {
+               return calculateOffset(startPoint, endPoint, start, list, end, dir, offset, true);
+       }
+
        private static boolean calculateOffset(Vector3d startPoint, Vector3d endPoint, PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, Vector3d dir, Vector3d offset) {
-               boolean hasOffsets = false;
-               List<PipeControlPoint> offsets = new ArrayList<PipeControlPoint>(list.size());
-               // Only start offset affects the calculation
-               if (start.isOffset())
-                   offsets.add(start);
-               for (PipeControlPoint icp : list) {
-                       if (icp.isOffset()) {
-                               offsets.add(icp);
-                       } else if (icp.isDualSub())
-                               ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
-               }
+               return calculateOffset(startPoint, endPoint, start, list, end, dir, offset, false);
+       }
+       
+       private static boolean calculateOffset(Vector3d startPoint, Vector3d endPoint, PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, Vector3d dir, Vector3d offset, boolean directed) {
+               List<PipeControlPoint> offsets = getOffsetPoints(start, list);
                if (offsets.size() == 0) {
-                       dir.set(endPoint);
-                       dir.sub(startPoint);
-                       double l = dir.lengthSquared(); 
-                       if (l > MathTools.NEAR_ZERO)
-                               dir.scale(1.0/Math.sqrt(l));
-                       offset.set(0.0, 0.0, 0.0);
+                       setZeroOffset(startPoint, endPoint, dir, offset);
                        return false;
                } else {
                        Vector3d sp = new Vector3d(startPoint);
@@ -392,6 +384,7 @@ public class PipingRules {
                        double l = dir.lengthSquared(); 
                        if (l > MathTools.NEAR_ZERO)
                                dir.scale(1.0/Math.sqrt(l));
+                       
                        int iter = 100;
                        while (iter >= 0) {
                                iter--;
@@ -401,11 +394,16 @@ public class PipingRules {
                                        Vector3d v = icp.getSizeChangeOffsetVector(dir);
                                        offset.add(v);
                                }
+                               
+                               if (directed)
+                                       break;
+                               
                                Point3d nep = new Point3d(endPoint);
                                nep.sub(offset);
                                if (nep.distance(ep) < 0.0000000001) {
                                        break;
-                               } 
+                               }
+                               
                                ep = nep;
                                dir.set(ep);
                                dir.sub(sp);
@@ -413,14 +411,37 @@ public class PipingRules {
                                if (l > MathTools.NEAR_ZERO)
                                        dir.scale(1.0/Math.sqrt(l));
                        }
-                       hasOffsets = true;
+                       
+                       if (DEBUG)
+                               System.out.println("calcOffset s:"+ startPoint + " e:" + endPoint + " d:" + dir + " o:"+offset) ;
+                       
+                       return true;
                }
-               
-               if (DEBUG && hasOffsets)
-                       System.out.println("calcOffset s:"+ startPoint + " e:" + endPoint + " d:" + dir + " o:"+offset) ;
-               return hasOffsets;
        }
        
+       public static void setZeroOffset(Vector3d startPoint, Vector3d endPoint, Vector3d dir, Vector3d offset) {
+               dir.set(endPoint);
+               dir.sub(startPoint);
+               double l = dir.lengthSquared(); 
+               if (l > MathTools.NEAR_ZERO)
+                       dir.scale(1.0/Math.sqrt(l));
+               offset.set(0.0, 0.0, 0.0);
+       }
+
+       public static List<PipeControlPoint> getOffsetPoints(PipeControlPoint start, ArrayList<PipeControlPoint> list) {
+               List<PipeControlPoint> offsets = new ArrayList<PipeControlPoint>(list.size());
+               // Only start offset affects the calculation
+               if (start.isOffset())
+                   offsets.add(start);
+               for (PipeControlPoint icp : list) {
+                       if (icp.isOffset()) {
+                               offsets.add(icp);
+                       } else if (icp.isDualSub())
+                               ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
+               }
+               return offsets;
+       }
+
        private static UpdateStruct2 createUS(PipeControlPoint start, Direction direction, int iter, ArrayList<ExpandIterInfo> toRemove, PipeControlPoint updated) {
                ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
                PipeControlPoint end = null;
@@ -1049,7 +1070,6 @@ public class PipingRules {
                        //if (u.start.isInline() || u.end.isInline() || u.start.asFixedAngle() || u.end.asFixedAngle())
                    //    processPathLeg(u, true, false);
                        checkExpandPathLeg(u, lengthChange, inlineEnd || u.start.isInline() || u.end.isInline() || u.start.asFixedAngle() || u.end.asFixedAngle());
-                       
                } else {
                        if (u.iter > 0) {
                                backIter(u);
@@ -1082,13 +1102,26 @@ public class PipingRules {
                                        if (canMoveOther) {
                                                if (DEBUG)
                                                        System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + closest);
+                                               
+                                               // Not aligned - we need to recalculate the offset to reflect new end points.
+                                               Vector3d offset;
+                                               if (u.hasOffsets) {
+                                                       offset = new Vector3d();
+                                                       Vector3d newDir = new Vector3d();
+                                                       calculateDirectedOffset(position, closest, u.start, u.list, u.end, newDir, offset);
+                                                       closest.add(offset);
+                                               } else {
+                                                       offset = new Vector3d();
+                                               }
+                                               
                                                other.setWorldPosition(closest);
+                                               
                                                if (dcpStart) {
-                                                       ppNoOffset(new UpdateStruct2(u.start, u.startPoint, u.list, u.end, new Vector3d(closest), directedDirection, null, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated),true);
+                                                       checkExpandPathLeg(new UpdateStruct2(u.start, u.startPoint, u.list, u.end, new Vector3d(closest), directedDirection, offset, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated), PathLegUpdateType.NONE, true);
                                                        if (u.end.getNext() != null)
                                                                updatePathLegNext(u.end, u.updated, PathLegUpdateType.NEXT);
                                                } else {
-                                                       ppNoOffset(new UpdateStruct2(u.start, new Vector3d(closest), u.list, u.end, u.endPoint, directedDirection, null, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated),true);
+                                                       checkExpandPathLeg(new UpdateStruct2(u.start, new Vector3d(closest), u.list, u.end, u.endPoint, directedDirection, offset, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated), PathLegUpdateType.NONE, true);
                                                        if (u.start.getPrevious() != null)
                                                                updatePathLegPrev(u.start, u.updated, PathLegUpdateType.PREV);
                                                }