]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Allow PipeRun merges when diameters are the same 00/3500/1
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 12 Nov 2019 09:31:48 +0000 (11:31 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Tue, 12 Nov 2019 09:31:48 +0000 (11:31 +0200)
gitlab #43

Change-Id: I5f85b1abc62da4eff0790f77b42999fa4274a324

org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/PipeRun.java
org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipingRules.java
org.simantics.plant3d/src/org/simantics/plant3d/utils/ComponentUtils.java

index 958228e8eefad236d03d6485f5f3a8d0b08030d1..2c334c1f2810f085f0deaad34028ea9d2a28994c 100644 (file)
@@ -4,7 +4,9 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.simantics.g3d.math.MathTools;
 import org.simantics.g3d.property.annotations.GetPropertyValue;
@@ -158,6 +160,86 @@ public class PipeRun extends P3DParentNode<IP3DNode> {
                return true;
        }
        
+       public boolean canMerge(PipeRun other) {
+           return MathTools.equals(pipeDiameter,other.pipeDiameter);
+       }
+       
+       /**
+        * Merges contents of PipeRun r2 to this PipeRun. Note: does not connect boundary components!
+        * @param r2
+        */
+       public void merge(PipeRun r2) {
+        Map<Integer, Integer> turnIndexMap = null;
+        if (!this.equalSpecs(r2)) {
+            if (!this.canMerge(r2))
+                throw new IllegalArgumentException("PipeRuns cannot be merged");
+            // Merge turn radii.
+            turnIndexMap = new HashMap<>();
+            List<Double> mergedTurnRadius = new ArrayList<>();
+            for (double t : this.getTurnRadiusArray()) {
+                mergedTurnRadius.add(t);
+            }
+            for (int i2 = 0; i2 < r2.getTurnRadiusArray().length; i2++) {
+                double t2 = r2.getTurnRadiusArray()[i2];
+                boolean found = false;
+                for (int i = 0; i < mergedTurnRadius.size(); i++) {
+                    if (MathTools.equals(mergedTurnRadius.get(i), t2)) {
+                        turnIndexMap.put(i2, i);
+                        found = true;
+                        break;
+                    }
+                }
+                if (!found) {
+                    turnIndexMap.put(i2, mergedTurnRadius.size());
+                    mergedTurnRadius.add(t2);
+                }
+            }
+            for (PipeControlPoint pcp : r2.getControlPoints()) {
+                PipelineComponent comp = pcp.getPipelineComponent();
+                if (comp instanceof TurnComponent) {
+                    
+                }
+            }
+            if (mergedTurnRadius.size() > this.getTurnRadiusArray().length) {
+                double arr[] = new double[mergedTurnRadius.size()];
+                for (int i = 0; i < mergedTurnRadius.size(); i++) {
+                    arr[i] = mergedTurnRadius.get(i);
+                }
+                this.setTurnRadiusArray(arr);
+            }
+        }
+        // Move components and control points
+        Collection<PipeControlPoint> pcps = r2.getControlPoints();
+        for (PipeControlPoint pcp : pcps) {
+            r2.deattachChild(pcp);
+            this.addChild(pcp);
+            PipelineComponent component = pcp.getPipelineComponent();
+            if (component != null) {
+                if (!(component instanceof Nozzle)) {
+                    component.deattach();
+                    this.addChild(component);
+                } else {
+                    Nozzle n = (Nozzle)component;
+                    n.setPipeRun(this);
+                }
+            }
+        }
+        // Use new turn radii indexes 
+        if (turnIndexMap != null) {
+            for (PipeControlPoint pcp : pcps) {
+                PipelineComponent component = pcp.getPipelineComponent();
+                if (component instanceof TurnComponent) {
+                    TurnComponent tc = (TurnComponent)component;
+                    if (tc.getTurnRadiusIndex() == null || tc.getTurnRadiusIndex() < 0)
+                        continue;
+                    tc.setTurnRadiusIndex(turnIndexMap.get(tc.getTurnRadiusIndex()));
+                }
+            }
+        }
+        r2.remove();
+        
+    }
+       
        private class ComponentComparator implements Comparator<PipelineComponent> {
                @Override
                public int compare(PipelineComponent o1, PipelineComponent o2) {
index de96517552f7d6774f7c0ef0cf20c04820e17f93..3094f7d20651527c8ca1728a7760a3b4187ce2d5 100644 (file)
@@ -1770,30 +1770,7 @@ public class PipingRules {
                }
        }
        
-       public static void merge(PipeRun run1, PipeRun r2) {
-               Map<PipeControlPoint, Vector3d> positions = new HashMap<PipeControlPoint, Vector3d>();
-               Map<PipeControlPoint, Quat4d> orientations = new HashMap<PipeControlPoint, Quat4d>();
-               for (PipeControlPoint pcp : r2.getControlPoints()) {
-                       positions.put(pcp, pcp.getWorldPosition());
-                       orientations.put(pcp, pcp.getWorldOrientation());
-               }
-               for (PipeControlPoint pcp : r2.getControlPoints()) {
-                       r2.deattachChild(pcp);
-                       run1.addChild(pcp);
-                       PipelineComponent component = pcp.getPipelineComponent();
-                       if (component != null) {
-                               if (!(component instanceof Nozzle)) {
-                                       component.deattach();
-                                       run1.addChild(component);
-                               } else {
-                                       Nozzle n = (Nozzle)component;
-                                       n.setPipeRun(run1);
-                               }
-                       }
-               }
-               r2.remove();
-               
-       }
+       
        
        public static void validate(PipeRun pipeRun) {
                if (pipeRun == null)
index 69bf593156e3fa448e8a335e94de60388712c54c..14dce68dc351c71c913141c27f3aeb2239280eb7 100644 (file)
@@ -538,7 +538,7 @@ public class ComponentUtils {
                                requiresReverse = true;
                        }
                        PipeRun other = endCP.getPipeRun();
-                       boolean mergeRuns = other == null ? true : pipeRun.equalSpecs(other);
+                       boolean mergeRuns = other == null ? true : pipeRun.canMerge(other);
                        
                        if (requiresReverse) {
                                // Pipe line must be traversible with next/previous relations without direction change.
@@ -546,10 +546,11 @@ public class ComponentUtils {
                                PipingRules.reverse(other);
                                
                        }
+
                        if (mergeRuns) {
                                // Runs have compatible specs and must be merged
                                if (other != null && pipeRun != other)
-                                       PipingRules.merge(pipeRun, other);
+                                       pipeRun.merge(other);
                                else if (other == null) {
                                        if (!(endTo instanceof Nozzle)) {
                                                pipeRun.addChild(endTo);