]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/PipeRun.java
Allow PipeRun merges when diameters are the same
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / PipeRun.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) {