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;
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) {
}
}
- 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)