1 package org.simantics.plant3d.scenegraph.controlpoint;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
9 import javax.vecmath.Point3d;
10 import javax.vecmath.Quat4d;
11 import javax.vecmath.Vector3d;
13 import org.simantics.g3d.math.MathTools;
14 import org.simantics.plant3d.scenegraph.InlineComponent;
15 import org.simantics.plant3d.scenegraph.Nozzle;
16 import org.simantics.plant3d.scenegraph.P3DRootNode;
17 import org.simantics.plant3d.scenegraph.PipeRun;
18 import org.simantics.plant3d.scenegraph.PipelineComponent;
19 import org.simantics.plant3d.scenegraph.TurnComponent;
20 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
21 import org.simantics.plant3d.utils.ComponentUtils;
22 import org.simantics.utils.ui.ErrorLogger;
24 public class PipingRules {
25 private static final boolean DEBUG = false;
26 private static final boolean DUMMY = false;
28 private static final double MIN_TURN_ANGLE = 0.01;
30 private static final int REMOVE_NONE = 0;
31 private static final int REMOVE_START = 1;
32 private static final int REMOVE_END = 2;
33 private static final int REMOVE_BOTH = 3;
35 // private P3DRootNode root;
37 // public PipingRules(P3DRootNode root) {
41 private enum PathLegUpdateType {
42 NONE, PREV, NEXT, PREV_S, NEXT_S
45 private static boolean enabled = true;
46 private static boolean updating = false;
47 private static boolean allowInsertRemove = true;
48 private static boolean triedIR = false;
51 private static List<PipeControlPoint> updates = new ArrayList<PipeControlPoint>();
53 private static Object mutex = new Object();
55 public static void requestUpdate(PipeControlPoint pcp) {
56 if (DEBUG) System.out.println("PipingRules request " + pcp);
57 synchronized (mutex) {
58 if (!updates.contains(pcp))
63 public static synchronized boolean update() throws Exception {
64 if (updates.size() == 0)
66 List<PipeControlPoint> temp = new ArrayList<PipeControlPoint>(updates.size());
72 for (PipeControlPoint pcp : temp)
77 public static boolean positionUpdate(PipeControlPoint pcp) throws Exception {
79 return positionUpdate(pcp, true);
82 public static boolean positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception {
83 if (updating || !enabled)
85 if (pcp.getPipeRun() == null)
88 if (DEBUG) System.out.println("PipingRules " + pcp);
90 allowInsertRemove = allowIR;
92 validate(pcp.getPipeRun());
93 if (pcp.isPathLegEnd()) {
94 updatePathLegEndControlPoint(pcp); // FXIME: Rules won't work properly, if they are not run twice.
95 updatePathLegEndControlPoint(pcp);
97 updateInlineControlPoint(pcp);
98 updateInlineControlPoint(pcp);
100 validate(pcp.getPipeRun());
101 if (!allowInsertRemove)
106 // System.out.println("PipingRules done " + pcp);
110 public static void setEnabled(boolean enabled) {
111 PipingRules.enabled = enabled;
116 public static boolean isEnabled() {
120 // private void commit() {
121 // root.getNodeMap().commit();
124 public static class ExpandIterInfo {
125 // these two are turn control points
126 private PipeControlPoint start;
127 private PipeControlPoint end;
130 public ExpandIterInfo() {
134 public ExpandIterInfo(PipeControlPoint tcp, int type) {
135 if (type == REMOVE_START)
142 public ExpandIterInfo(PipeControlPoint start, PipeControlPoint end) {
145 this.type = REMOVE_BOTH;
148 public PipeControlPoint getEnd() {
152 public void setEnd(PipeControlPoint end) {
156 public PipeControlPoint getStart() {
160 public void setStart(PipeControlPoint start) {
164 public int getType() {
168 public void setType(int type) {
174 private static void updatePathLegEndControlPoint(PipeControlPoint pcp) throws Exception {
176 System.out.println("PipingRules.updatePathLegEndControlPoint() " + pcp);
177 if (pcp.getNext() != null) {
178 updatePathLegNext(pcp, pcp, PathLegUpdateType.NEXT_S);
180 if (pcp.getPrevious() != null) {
181 updatePathLegPrev(pcp, pcp, PathLegUpdateType.PREV_S);
186 private static void updateInlineControlPoint(PipeControlPoint pcp) throws Exception {
188 System.out.println("PipingRules.updateInlineControlPoint() " + pcp);
189 PipeControlPoint start = pcp.findPreviousEnd();
190 updatePathLegNext(start, pcp, PathLegUpdateType.NONE);
193 private static PipeControlPoint insertElbow(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos) throws Exception{
195 System.out.println("PipingRules.insertElbow() " + pcp1 + " " + pcp2 + " " + pos);
196 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
198 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getSubPoint().get(0)) {
199 pcp1 = pcp1.getSubPoint().get(0);
200 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
201 PipeControlPoint t = pcp1;
204 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getSubPoint().get(0) && pcp2.getNext() == pcp1) {
205 PipeControlPoint t = pcp1;
206 pcp1 = pcp2.getSubPoint().get(0);
209 throw new RuntimeException();
211 TurnComponent elbow = ComponentUtils.createTurn((P3DRootNode)pcp1.getRootNode());
212 PipeControlPoint pcp = elbow.getControlPoint();
213 if (pcp1.isDualInline())
214 pcp1 = pcp1.getSubPoint().get(0);
215 String name = pcp1.getPipeRun().getUniqueName("Elbow");
217 pcp1.getPipeRun().addChild(elbow);
219 pcp.insert(pcp1, pcp2);
221 pcp.setWorldPosition(pos);
222 validate(pcp.getPipeRun());
226 private static PipeControlPoint insertStraight(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos, double length) throws Exception {
228 System.out.println("PipingRules.insertStraight() " + pcp1 + " " + pcp2 + " " + pos);
229 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
231 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getSubPoint().get(0)) {
232 pcp1 = pcp1.getSubPoint().get(0);
233 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
234 PipeControlPoint t = pcp1;
237 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getSubPoint().get(0) && pcp2.getNext() == pcp1) {
238 PipeControlPoint t = pcp1;
239 pcp1 = pcp2.getSubPoint().get(0);
242 throw new RuntimeException();
244 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp1.getRootNode());
245 PipeControlPoint scp = component.getControlPoint();
246 if (pcp1.isDualInline())
247 pcp1 = pcp1.getSubPoint().get(0);
248 String name = pcp1.getPipeRun().getUniqueName("Pipe");
249 component.setName(name);
250 pcp1.getPipeRun().addChild(component);
252 scp.insert(pcp1, pcp2);
254 scp.setWorldPosition(pos);
255 scp.setLength(length);
256 validate(scp.getPipeRun());
260 private static PipeControlPoint insertStraight(PipeControlPoint pcp, Direction direction , Vector3d pos, double length) throws Exception {
262 System.out.println("PipingRules.insertStraight() " + pcp + " " + direction + " " + pos);
264 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp.getRootNode());
265 PipeControlPoint scp = component.getControlPoint();
266 if (pcp.isDualInline() && direction == Direction.NEXT)
267 pcp = pcp.getSubPoint().get(0);
268 String name = pcp.getPipeRun().getUniqueName("Pipe");
269 component.setName(name);
270 pcp.getPipeRun().addChild(component);
272 scp.insert(pcp,direction);
274 scp.setWorldPosition(pos);
275 scp.setLength(length);
276 validate(scp.getPipeRun());
280 private static void updatePathLegNext(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
281 ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
282 PipeControlPoint end = start.findNextEnd(list);
283 // this is for inline cp that is also path leg end
284 if (start.equals(updated))
285 lengthChange = PathLegUpdateType.NEXT;
286 else if (end.equals(updated))
287 lengthChange = PathLegUpdateType.PREV;
288 updatePathLegNext(start, list, end, updated, lengthChange);
291 private static void updatePathLegNext(PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
292 updatePathLeg(start, list, end, false, 0, new ArrayList<ExpandIterInfo>(), updated, lengthChange);
295 private static class UpdateStruct2 {
296 public PipeControlPoint start;
297 public Vector3d startPoint;
298 public ArrayList<PipeControlPoint> list;
299 public PipeControlPoint end;
300 public Vector3d endPoint;
302 public Vector3d offset;
303 public boolean hasOffsets;
305 public boolean reversed;
306 public ArrayList<ExpandIterInfo> toRemove;
307 public PipeControlPoint updated;
309 public UpdateStruct2(PipeControlPoint start, Vector3d startPoint, ArrayList<PipeControlPoint> list, PipeControlPoint end, Vector3d endPoint, Vector3d dir, Vector3d offset, boolean hasOffsets, int iter, boolean reversed, ArrayList<ExpandIterInfo> toRemove, PipeControlPoint updated) {
310 if (start == null || end == null)
311 throw new NullPointerException();
313 this.startPoint = startPoint;
316 this.endPoint = endPoint;
318 this.offset = offset;
319 this.hasOffsets = hasOffsets;
321 this.reversed = reversed;
322 this.toRemove = toRemove;
323 this.updated = updated;
325 if (!MathTools.isValid(startPoint) ||
326 !MathTools.isValid(endPoint) ||
327 !MathTools.isValid(dir)) {
328 throw new RuntimeException();
332 public String toString() {
333 return start + " " + end+ " " + dir + " " + hasOffsets + " " + offset + " " + iter + " " + toRemove.size();
338 private static boolean calculateOffset(Vector3d startPoint, Vector3d endPoint, ArrayList<PipeControlPoint> list, Vector3d dir, Vector3d offset) {
339 boolean hasOffsets = false;
342 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
344 offset.set(0.0, 0.0, 0.0);
345 for (PipeControlPoint icp : list) {
346 if (icp.isOffset()) {
348 offset.add(icp.getSizeChangeOffsetVector(dir));
349 } else if (icp.isDualSub())
350 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
357 * starting point of the pipe run
359 * list of inline control points in the pipe run
361 * ending point of the pipe run
363 * boolean flag indicating wether start or end control point was
364 * modified (if true then end point was modified)
365 * @throws TransactionException
367 private static void updatePathLeg(PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, boolean reversed, int iter, ArrayList<ExpandIterInfo> toRemove, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
370 // FIXME: direction is calculated wrong way!
371 boolean hasOffsets = false;
372 Vector3d offset = new Vector3d();
373 Vector3d startPoint = start.getWorldPosition();
374 Vector3d endPoint = end.getWorldPosition();
375 Vector3d dir = new Vector3d();
376 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
377 updatePathLeg(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, offset, hasOffsets, iter, reversed, toRemove, updated), lengthChange);
381 private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
383 if (u.start.isDirected())
385 if (u.end.isDirected())
389 updateFreePathLeg(u, lengthChange);
392 updateDirectedPathLeg(u, lengthChange);
395 updateDualDirectedPathLeg(u, lengthChange);
401 private static void updateFreePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
403 System.out.println("PipingRules.updateFreePipeRun " + u + " " + lengthChange);
404 checkExpandPathLeg(u, lengthChange);
405 if (u.start.isInline() || u.end.isInline())
406 processPathLeg(u, true, false);
409 private static void updateInlineControlPoints(UpdateStruct2 u, boolean checkSizes) throws Exception{
411 System.out.println("PipingRules.updateInlineControlPoints() " + u);
414 // FIXME : cache positions
416 Vector3d start = new Vector3d(u.startPoint);
417 Vector3d end = new Vector3d(u.endPoint);
419 MathTools.mad(start, u.dir, 0.1);
420 MathTools.mad(end, u.dir, -0.1);
421 for (PipeControlPoint icp : u.list) {
422 updateInlineControlPoint(icp, start, end, u.dir);
427 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
428 pathLegPoints.add(u.start);
429 for (PipeControlPoint icp : u.list) {
430 // updateInlineControlPoint(icp, u.startPoint,
431 // u.endPoint,u.dir);
432 updateBranchControlPointBranches(icp);
433 pathLegPoints.add(icp);
435 pathLegPoints.add(u.end);
437 // TODO : values can be cached in the loop
438 for (int i = 1; i < pathLegPoints.size(); i++) {
439 PipeControlPoint icp = pathLegPoints.get(i);
441 PipeControlPoint prev;
443 prev = pathLegPoints.get(i - 1);
444 prevPos = prev.getWorldPosition();
445 Vector3d currentPos = icp.getWorldPosition();
447 if (icp.isVariableLength()) {
448 if (i != pathLegPoints.size() - 1) {
449 PipeControlPoint next;
451 next = pathLegPoints.get(i + 1);
452 nextPos = next.getWorldPosition();
453 Vector3d dir = new Vector3d(nextPos);
455 double l = dir.lengthSquared(); // distance between
458 double l2prev = prev.getInlineLength(); // distance
462 double l2next = next.getInlineLength();
463 double l2 = l2prev + l2next;
464 double l2s = MathTools.square(l2);
465 if (l2s < l) { // check if there is enough space for
466 // variable length component.
469 double length = Math.sqrt(l) - l2; // true length of
473 dir.scale(length * 0.5 + l2prev); // calculate
478 icp.setWorldPosition(dir);
479 icp.setLength(length);
481 // components leave no space to the component and it
483 if (icp.isDeletable())
488 // this is variable length component at the end of the
490 // the problem is that we want to keep unconnected end
491 // of the component in the same
492 // place, but center of the component must be moved.
493 double currentLength = icp.getLength();
495 Vector3d dir = new Vector3d();
496 dir.sub(currentPos, prevPos);
498 if (currentLength < MathTools.NEAR_ZERO) {
499 currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
502 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
504 Point3d endPos = new Point3d(dir);
505 endPos.scale(currentLength * 0.5);
506 endPos.add(currentPos); // this is the free end of the
509 double offset = prev.getInlineLength();
510 Point3d beginPos = new Point3d(dir);
511 beginPos.scale(offset);
512 beginPos.add(prevPos); // this is the connected end of
515 double l = beginPos.distance(endPos);
518 System.out.println();
521 beginPos.add(dir); // center position
524 System.out.println("PipingRules.updateInlineControlPoints() setting variable length to " + l);
527 icp.setWorldPosition(new Vector3d(beginPos));
531 } else if (!prev.isVariableLength()) {
532 // If this and previous control point are not variable
533 // length pcps, we'll have to check if there is no empty
534 // space between them.
535 // I there is, we'll have to create new variable length
536 // component between them.
537 Vector3d dir = new Vector3d(currentPos);
539 double l = dir.lengthSquared();
540 double l2prev = prev.getInlineLength();
541 double l2next = icp.getInlineLength();
542 double l2 = l2prev + l2next;
543 double l2s = l2 * l2;
545 if (allowInsertRemove) {
547 double length = Math.sqrt(l) - l2; // true length of the
550 dir.scale(length * 0.5 + l2prev); // calculate center
554 PipeControlPoint scp = insertStraight(prev, icp, dir, length);
562 u.endPoint.sub(u.offset);
564 for (PipeControlPoint icp : u.list) {
565 updateInlineControlPoint(icp, u.startPoint, u.endPoint, u.dir);
566 updateBranchControlPointBranches(icp);
568 if (icp.isOffset()) {
569 // TODO : offset vector is already calculated and should be
571 u.offset = icp.getSizeChangeOffsetVector(u.dir);
572 updateOffsetPoint(icp, u.offset);
573 u.startPoint.add(u.offset);
574 u.endPoint.add(u.offset);
580 private static void ppNoOffset(UpdateStruct2 u) throws Exception {
582 System.out.println("PipingRules.ppNoOffset() " + u);
583 Vector3d offset = new Vector3d();
586 for (PipeControlPoint icp : u.list) {
587 if (icp.isOffset()) {
588 offset.add(icp.getSizeChangeOffsetVector(u.dir));
589 } else if (icp.isDualSub())
590 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
594 checkExpandPathLeg(u, PathLegUpdateType.NONE);
597 private static void ppNoDir(PipeControlPoint start, Vector3d startPoint, ArrayList<PipeControlPoint> list, PipeControlPoint end, Vector3d endPoint, boolean hasOffsets, int iter, boolean reversed, ArrayList<ExpandIterInfo> toRemove, PipeControlPoint updated) throws Exception {
599 System.out.println("PipingRules.ppNoDir() " + start + " " + end + " " + iter + " " + toRemove.size());
600 // FIXME : extra loop (dir should be calculated here)
601 Vector3d dir = new Vector3d();
602 Vector3d offset = new Vector3d();
603 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
604 ppNoOffset(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, null, hasOffsets, iter, reversed, toRemove, updated));
607 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
608 checkExpandPathLeg(u, lengthChange, false);
611 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange, boolean forceUpdate) throws Exception {
613 System.out.println("PipingRules.checkExpandPathLeg() " + u + " " + lengthChange);
614 if (lengthChange != PathLegUpdateType.NONE) {
615 // FIXME : turns cannot be checked before inline cps are updated,
616 // since their position affects calculation of turns
617 processPathLeg(u, forceUpdate, false);
618 int type = checkTurns(u, lengthChange);
619 if (type == REMOVE_NONE) {
620 processPathLeg(u, forceUpdate, true);
622 expandPathLeg(u, type);
625 processPathLeg(u, forceUpdate, true);
629 private static void updateDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
631 System.out.println("PipingRules.updateDirectedPipeRun() " + u + " " + lengthChange);
632 PipeControlPoint dcp;
633 PipeControlPoint other;
634 boolean canMoveOther = false;
635 boolean dcpStart = false;
636 boolean inlineEnd = false;
638 if (u.start.isDirected()) {
641 position = u.startPoint;
645 inlineEnd = u.end.isInline();
650 position = u.endPoint;
653 inlineEnd = u.start.isInline();
656 Vector3d directedDirection = dcp.getDirection();
657 Point3d directedEndPoint = new Point3d(u.endPoint);
659 directedEndPoint.add(u.offset);
661 double mu[] = new double[2];
664 Vector3d t = new Vector3d();
667 closest = MathTools.closestPointOnStraight(directedEndPoint, u.startPoint, directedDirection, mu);
668 t.sub(closest, directedEndPoint);
670 closest = MathTools.closestPointOnStraight(u.startPoint, directedEndPoint, directedDirection, mu);
671 t.sub(closest, u.startPoint);
674 double distance = t.lengthSquared();
675 boolean aligned = (distance < 0.002);
677 checkExpandPathLeg(u, lengthChange, inlineEnd);
683 PipeControlPoint nextToMoved;
685 if (u.list.size() > 0)
687 nextToMoved = u.list.get(0);
689 nextToMoved = u.list.get(u.list.size() - 1);
693 nextToMoved = u.start;
694 if (other.isVariableAngle()) {
696 // TODO calculate needed space from next run end.
699 closest.set(u.startPoint);
701 closest.set(u.endPoint);
703 closest.add(directedDirection);
708 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + closest);
709 other.setWorldPosition(closest);
711 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));
712 if (u.end.getNext() != null)
713 updatePathLegNext(u.end, u.updated, PathLegUpdateType.NEXT);
715 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));
716 if (u.start.getPrevious() != null)
717 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.PREV);
720 // TODO : calculate needed space from next run end.
721 if (allowInsertRemove)
722 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
727 } else if (other.isNonDirected() && other.getParentPoint() != null) {
728 // FIXME : this code was for updating branches
729 Vector3d bintersect = new Vector3d();
730 PipeControlPoint bcp = other.getParentPoint();
731 if (bcp != null && canMoveOther) {
732 Point3d bstart = new Point3d();
733 Point3d bend = new Point3d();
734 Vector3d bdir = new Vector3d();
735 bcp.getInlineControlPointEnds(bstart, bend, bdir);
736 Vector3d nintersect = new Vector3d();
738 MathTools.intersectStraightStraight(position, directedDirection, bend, bdir, nintersect, bintersect, mu);
739 Vector3d dist = new Vector3d(nintersect);
740 dist.sub(bintersect);
741 canMoveOther = mu[1] > 0.0 && mu[1] < 1.0 && dist.lengthSquared() < 0.01;
743 // TODO : endControlPoints are undirected: calculcate
744 // correct position for it
745 throw new UnsupportedOperationException("not implemented");
749 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + bintersect);
750 // is required branch position is in possible range
751 bcp.setWorldPosition(bintersect);
753 checkExpandPathLeg(new UpdateStruct2(u.start, u.startPoint, u.list, u.end, new Vector3d(bintersect), directedDirection, u.offset, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated), lengthChange);
755 checkExpandPathLeg(new UpdateStruct2(u.start, new Vector3d(bintersect), u.list, u.end, u.endPoint, directedDirection, u.offset, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated), lengthChange);
758 // branch cannot be moved into right position, new turn
759 // / elbow must be inserted
760 if (allowInsertRemove)
761 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
766 } else { // assume that control point cannot be moved, but can
768 if (allowInsertRemove)
769 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
779 private static void updateDualDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
781 System.out.println("PipingRules.updateDualDirectedPipeRun() " + u + " " + lengthChange);
783 PipeControlPoint dcp1 = u.start;
784 PipeControlPoint dcp2 = u.end;
785 Point3d position1 = new Point3d(u.startPoint);
786 Point3d position2 = new Point3d(u.endPoint);
787 Point3d position1offset = new Point3d(position1);
788 position1offset.sub(u.offset);
789 Point3d position2offset = new Point3d(position2);
790 position2offset.add(u.offset);
791 Vector3d dir1 = dcp1.getDirection();
792 Vector3d dir2 = dcp2.getDirection();
793 Vector3d p1 = MathTools.closestPointOnStraight(position1offset, position2, dir2);
794 Vector3d p2 = MathTools.closestPointOnStraight(position2offset, position1, dir1);
795 double d1 = position1.distance(new Point3d(p1));
796 double d2 = position2.distance(new Point3d(p2));
798 boolean aligned = (d1 < 0.01 && d2 < 0.01);
804 } else if (allowInsertRemove){
805 PipeControlPoint dcp;
806 PipeControlPoint next;
809 if (u.list.size() > 0)
810 next = u.list.get(0);
815 if (u.list.size() > 0)
816 next = u.list.get(u.list.size() - 1);
821 p1 = dcp.getWorldPosition();
822 // FIXME: calculate position of the elbows properly.
829 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position2, dir2);
831 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position1, dir1);
834 PipeControlPoint tcp1 = insertElbow(dcp, next, p1);
835 PipeControlPoint tcp2 = insertElbow(tcp1, next, p2);
838 System.out.println("PipingRules.updateDualDirectedPipeRun() created two turns " + tcp1 + " " + tcp2);
841 Vector3d dd = new Vector3d(p2);
844 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
845 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
847 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
849 updatePathLegPrev(tcp2, u.updated, PathLegUpdateType.NONE);
851 Vector3d dd = new Vector3d(p1);
854 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
855 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
857 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
859 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.NONE);
868 private static void insertElbowUpdate(UpdateStruct2 u, PipeControlPoint dcp, PipeControlPoint next, boolean dcpStart, Vector3d position, Vector3d directedDirection) throws Exception{
870 Vector3d closest = new Vector3d(position);
871 closest.add(directedDirection);
872 PipeControlPoint tcp = null;
874 tcp = insertElbow(dcp, next, new Vector3d(closest));
876 tcp = insertElbow(next, dcp, new Vector3d(closest));
879 System.out.println("PipingRules.updateDirectedPipeRun() inserted " + tcp);
882 // update pipe run from new turn to other end
883 ppNoDir(tcp, new Vector3d(closest), u.list, u.end, u.endPoint, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
884 // update pipe run from directed to new turn
885 processPathLeg(new UpdateStruct2(u.start, u.startPoint, new ArrayList<PipeControlPoint>(), tcp, new Vector3d(closest), directedDirection, new Vector3d(), false, 0, false, new ArrayList<ExpandIterInfo>(), u.updated));
887 // update pipe run from other end to new turn
888 ppNoDir(u.start, u.startPoint, u.list, tcp, new Vector3d(closest), u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
889 // update pipe run from new turn to directed
890 processPathLeg(new UpdateStruct2(tcp, new Vector3d(closest), new ArrayList<PipeControlPoint>(), u.end, u.endPoint, directedDirection, new Vector3d(), false, 0, false, new ArrayList<ExpandIterInfo>(), u.updated));
895 * Checks if turns can be removed (turn angle near zero)
897 private static int checkTurns(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
899 System.out.println("PipingRules.checkTurns() " + u.start + " " + u.end);
900 boolean startRemoved = false;
901 boolean endRemoved = false;
902 if (u.start.isVariableAngle()) {
903 // this won't work properly if inline control points are not updated
904 PipeControlPoint startPrev = u.start.getPrevious();
905 if (startPrev != null) {
908 a = updateTurnControlPointTurn(u.start, startPrev, u.end);
910 Vector3d ep = new Vector3d(u.endPoint);
912 a = updateTurnControlPointTurn(u.start, u.startPoint, startPrev.getPosition(), ep);
915 if (a < MIN_TURN_ANGLE && u.start.isDeletable())
917 else if (lengthChange == PathLegUpdateType.PREV || lengthChange == PathLegUpdateType.PREV_S) {
918 PathLegUpdateType type;
919 if (lengthChange == PathLegUpdateType.PREV_S)
920 type = PathLegUpdateType.PREV;
922 type = PathLegUpdateType.NONE;
923 updatePathLegPrev(u.start, u.start, type);
927 if (u.end.isVariableAngle()) {
929 PipeControlPoint endNext = u.end.getNext();
930 if (endNext != null) {
933 a = updateTurnControlPointTurn(u.end, u.start, endNext);
935 Vector3d sp = new Vector3d(u.startPoint);
937 a = updateTurnControlPointTurn(u.end, u.endPoint, sp, endNext.getPosition());
939 if (a < MIN_TURN_ANGLE && u.end.isDeletable())
941 else if (lengthChange == PathLegUpdateType.NEXT || lengthChange == PathLegUpdateType.NEXT_S) {
942 PathLegUpdateType type;
943 if (lengthChange == PathLegUpdateType.NEXT_S)
944 type = PathLegUpdateType.NEXT;
946 type = PathLegUpdateType.NONE;
947 updatePathLegNext(u.end, u.end, type);
952 System.out.println("PipingRules.checkTurns() res " + startRemoved + " " + endRemoved);
953 if (!startRemoved && !endRemoved)
955 if (startRemoved && endRemoved)
963 * Expands piperun search over turns that are going to be removed
966 private static void expandPathLeg(UpdateStruct2 u, int type) throws Exception {
968 System.out.println("PipingRules.expandPipeline " + u.start + " " + u.end);
969 ArrayList<PipeControlPoint> newList = new ArrayList<PipeControlPoint>();
972 throw new RuntimeException("Error in piping rules");
974 u.toRemove.add(new ExpandIterInfo(u.start, REMOVE_START));
975 u.start = u.start.findPreviousEnd();
976 u.startPoint = u.start.getPosition();
977 u.start.findNextEnd(newList);
978 newList.addAll(u.list);
982 u.toRemove.add(new ExpandIterInfo(u.end, REMOVE_END));
983 u.end = u.end.findNextEnd(newList);
984 u.endPoint = u.end.getPosition();
985 u.list.addAll(newList);
988 u.toRemove.add(new ExpandIterInfo(u.start, u.end));
989 u.start = u.start.findPreviousEnd();
990 u.startPoint = u.start.getPosition();
991 u.start.findNextEnd(newList);
992 newList.addAll(u.list);
994 newList = new ArrayList<PipeControlPoint>();
995 u.end = u.end.findNextEnd(newList);
996 u.endPoint = u.end.getPosition();
997 u.list.addAll(newList);
1000 throw new RuntimeException("Error in piping rules");
1003 u.offset = new Vector3d();
1006 for (PipeControlPoint icp : u.list) {
1007 if (icp.isOffset()) {
1008 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1009 } else if (icp.isDualSub())
1010 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1014 System.out.println("PipingRules.expandPipeline expanded " + u.start + " " + u.end);
1016 updatePathLeg(u, PathLegUpdateType.NONE);
1020 * reverts one iteration of turn removing back)
1022 private static void backIter(UpdateStruct2 u) throws Exception {
1025 System.out.println("PipingRules.backIter" + u.start + " " + u.end);
1027 throw new RuntimeException("Error in piping rules");
1028 ExpandIterInfo info = u.toRemove.get(u.toRemove.size() - 1);
1029 u.toRemove.remove(u.toRemove.size() - 1);
1030 if (info.getType() == REMOVE_START || info.getType() == REMOVE_BOTH) {
1031 while (u.list.size() > 0) {
1032 PipeControlPoint icp = u.list.get(0);
1033 if (icp.getPrevious().equals(info.getStart()))
1037 u.start = info.getStart();
1039 if (info.getType() == REMOVE_END || info.getType() == REMOVE_BOTH) {
1040 while (u.list.size() > 0) {
1041 PipeControlPoint icp = u.list.get(u.list.size() - 1);
1042 if (icp.getNext().equals(info.getEnd()))
1046 u.end = info.getEnd();
1048 u.offset = new Vector3d();
1051 for (PipeControlPoint icp : u.list) {
1052 if (icp.isOffset()) {
1053 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1054 } else if (icp.isDualSub())
1055 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1063 * Processes pipe run (removes necessary turns and updates run ends)
1065 // private static void processPathLeg(PipeControlPoint start, Point3d
1066 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1067 // end,Point3d endPoint, Vector3d dir,Vector3d offset, boolean
1068 // hasOffsets,int iter, boolean reversed, ArrayList<ExpandIterInfo>
1069 // toRemove) throws TransactionException {
1071 private static void processPathLeg(UpdateStruct2 u) throws Exception {
1073 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1074 processPathLeg(u, true, true);
1077 private static void processPathLeg(UpdateStruct2 u, boolean updateEnds, boolean updateInline) throws Exception {
1079 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1081 if (u.toRemove.size() > 0) {
1082 for (ExpandIterInfo info : u.toRemove) {
1083 if (info.getStart() != null) {
1084 info.getStart()._remove();
1086 if (info.getEnd() != null) {
1087 info.getEnd()._remove();
1090 // ControlPointTools.removeControlPoint may remove mo0re than one
1092 // we must populate inline CP list again.
1094 u.start.findNextEnd( u.list);
1096 // FIXME : inline CPs are update twice because their positions must be
1097 // updated before and after ends.
1098 updateInlineControlPoints(u, false);
1101 if (u.start.isTurn()) {
1102 updateTurnControlPointTurn(u.start, u.start.getPrevious(), u.start.getNext());
1103 // updatePathLegPrev(u.start, u.start, PathLegUpdateType.NONE);
1104 } else if (u.start.isEnd()) {
1105 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1106 } else if (u.start.isInline()) {
1107 updateControlPointOrientation(u.start);
1109 if (u.end.isTurn()) {
1110 updateTurnControlPointTurn(u.end, u.end.getPrevious(), u.end.getNext());
1111 // updatePathLegNext(u.end, u.end, PathLegUpdateType.NONE);
1112 } else if (u.end.isEnd()) {
1113 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1114 } else if (u.end.isInline()) {
1115 updateControlPointOrientation(u.end);
1119 if (u.start.isEnd()) {
1120 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1122 if (u.end.isEnd()) {
1123 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1127 updateInlineControlPoints(u, true);
1132 * Processes pipe run and recalculates offset
1134 // private static void processPathLeg(PipeControlPoint start, Point3d
1135 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1136 // end,Point3d endPoint, Vector3d dir, boolean hasOffsets,int iter, boolean
1137 // reversed, ArrayList<ExpandIterInfo> toRemove) throws TransactionException
1139 private static void processPathLegNoOffset(UpdateStruct2 u) throws Exception {
1141 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1142 Vector3d offset = new Vector3d();
1145 for (PipeControlPoint icp : u.list) {
1146 if (icp.isOffset()) {
1147 offset.add(icp.getSizeChangeOffsetVector(u.dir));
1148 } else if (icp.isDualSub()) {
1149 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1156 private static void updateOffsetPoint(PipeControlPoint sccp, Vector3d offset) {
1157 Vector3d world = sccp.getWorldPosition();
1159 PipeControlPoint ocp = sccp.getSubPoint().iterator().next();
1160 ocp.setWorldPosition(world);
1163 private static void updatePathLegPrev(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
1164 ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
1165 PipeControlPoint end = start.findPreviousEnd(list);
1166 updatePathLegPrev(start, list, end, updated, lengthChange);
1169 private static void updatePathLegPrev(PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
1170 // reverses the list
1171 ArrayList<PipeControlPoint> nextList = new ArrayList<PipeControlPoint>();
1172 for (PipeControlPoint icp : list) {
1173 if (icp.isDualSub()) {
1174 nextList.add(0, icp.getParentPoint());
1176 nextList.add(0, icp);
1180 updatePathLeg(end, nextList, start, true, 0, new ArrayList<ExpandIterInfo>(), updated, lengthChange);
1185 * Updates InlineControlPoints position when straight pipe's end(s) have
1193 private static void updateInlineControlPoint(PipeControlPoint icp, Vector3d nextPoint, Vector3d prevPoint, Vector3d dir) {
1195 System.out.println("PipingRules.updateInlineControlPoint() " + icp);
1197 Vector3d inlinePoint = icp.getWorldPosition();
1199 System.out.print("InlineControlPoint update " + icp + " " + inlinePoint + " " + prevPoint + " " + nextPoint);
1200 Vector3d newInlinePoint = null;
1201 boolean branchUpdate = false;
1202 PipeControlPoint becp = null;
1203 for (PipeControlPoint pcp : icp.getSubPoint())
1204 if (pcp.isNonDirected()) {
1205 branchUpdate = true;
1210 if (DUMMY || !branchUpdate) {
1211 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), new Vector3d(nextPoint), new Vector3d(prevPoint));
1214 // FIXME : can only handle one branch
1215 PipeControlPoint p = null;
1216 if (becp.getNext() != null) {
1217 p = becp.findNextEnd();
1218 } else if (becp.getPrevious() != null) {
1219 p = becp.findPreviousEnd();
1222 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), new Vector3d(nextPoint), new Vector3d(prevPoint));
1224 Vector3d branchLegEnd = p.getWorldPosition();
1225 Vector3d dir2 = new Vector3d(inlinePoint);
1226 dir2.sub(branchLegEnd);
1227 Vector3d dir1 = new Vector3d(nextPoint);
1228 dir1.sub(prevPoint);
1229 newInlinePoint = new Vector3d();
1230 double mu[] = new double[2];
1231 MathTools.intersectStraightStraight(new Vector3d(prevPoint), dir1, new Vector3d(branchLegEnd), dir2, newInlinePoint, new Vector3d(), mu);
1233 System.out.println(mu[0]);
1234 // FIXME : reserve space
1236 newInlinePoint = new Vector3d(prevPoint);
1237 } else if (mu[0] > 1.0) {
1238 newInlinePoint = new Vector3d(nextPoint);
1243 System.out.println(" " + newInlinePoint);
1245 icp.setWorldPosition(newInlinePoint);
1246 updateControlPointOrientation(icp);
1250 * Updates InlineControlPoints position when straight pipe's end(s) have
1258 private static void updateEndComponentControlPoint(PipeControlPoint ecp, Vector3d start, Vector3d end) throws Exception {
1260 System.out.println("PipingRules.updateEndComponentControlPoint() " + ecp);
1261 // PipeControlPoint next = ecp.getNext();
1262 // PipeControlPoint prev = ecp.getPrevious();
1263 // if (next != null) {
1264 // end = G3DTools.getPoint(next.getLocalPosition());
1265 // start = G3DTools.getPoint(ecp.getLocalPosition());
1266 // } else if (prev != null) {
1267 // end = G3DTools.getPoint(ecp.getLocalPosition());
1268 // start = G3DTools.getPoint(prev.getLocalPosition());
1270 // // TODO : warning?
1273 // Vector3d dir = new Vector3d (end);
1276 // G3DTools.setTuple(ecp.getDirection(), dir);
1278 updateControlPointOrientation(ecp);
1280 for (PipeControlPoint pcp : ecp.getSubPoint()) {
1281 // TODO update position
1282 updatePathLegEndControlPoint(pcp);
1286 private static void updateControlPointOrientation(PipeControlPoint pcp) {
1287 // FIXME : hack to bypass variable length components orientation
1288 // if (pcp.getAtMostOneRelatedObject(ProcessResource.g3dResource.HasWorldOrientation) == null)
1290 // if (pcp.rotationAngle == null)
1292 Double angleO = pcp.getRotationAngle();
1297 Quat4d q = pcp.getControlPointOrientationQuat(angle);
1298 pcp.setWorldOrientation(q);
1302 * Updates all branches when branch's position has been changed
1306 private static void updateBranchControlPointBranches(PipeControlPoint bcp) throws Exception {
1308 System.out.println("PipingRules.updateBranchControlPointBranches() " + bcp);
1309 if (bcp.isDualInline())
1311 Collection<PipeControlPoint> branches = bcp.getSubPoint();
1312 if (branches.size() == 0) {
1314 System.out.println("No Branches found");
1318 for (PipeControlPoint pcp : branches) {
1319 updatePathLegEndControlPoint(pcp);
1324 * Recalculates turn control point's internal data (turn angle and offset)
1330 private static double updateTurnControlPointTurn(PipeControlPoint tcp, PipeControlPoint prev, PipeControlPoint next) {
1332 System.out.println("PipingTools.updateTurnControlPointTurn()" + tcp);
1333 if (next == null || prev == null)
1334 return Math.PI; // FIXME : argh
1335 Vector3d middlePoint = tcp.getWorldPosition();
1336 Vector3d nextPoint = next.getWorldPosition();
1337 Vector3d prevPoint = prev.getWorldPosition();
1338 return updateTurnControlPointTurn(tcp, middlePoint, prevPoint, nextPoint);
1342 * Recalculates turn control point's internal data (turn angle and offset)
1345 * @param middlePoint
1349 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d middlePoint, Vector3d prevPoint, Vector3d nextPoint) {
1351 Vector3d dir1 = new Vector3d(middlePoint);
1352 dir1.sub(prevPoint);
1353 Vector3d dir2 = new Vector3d(nextPoint);
1354 dir2.sub(middlePoint);
1356 System.out.println("PipingTools.updateTurnControlPointTurn " + tcp + " " + prevPoint + " " + middlePoint + " " + nextPoint);
1357 return updateTurnControlPointTurn(tcp, dir1, dir2);
1360 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d dir1, Vector3d dir2) {
1361 double turnAngle = dir1.angle(dir2);
1363 double angle = Math.PI - turnAngle;
1365 Vector3d turnAxis = new Vector3d();
1366 turnAxis.cross(dir1, dir2);
1367 if (turnAxis.lengthSquared() > MathTools.NEAR_ZERO) {
1368 double elbowRadius = tcp.getPipelineComponent().getPipeRun().getTurnRadius();
1369 double R = elbowRadius / Math.tan(angle * 0.5);
1371 turnAxis.normalize();
1372 tcp.setTurnAngle(turnAngle);
1373 tcp.setLength(R);// setComponentOffsetValue(R);
1374 tcp.setTurnAxis(turnAxis);
1375 // tcp.setPosition(tcp.getPosition());
1378 tcp.setTurnAngle(0.0);
1380 tcp.setTurnAxis(MathTools.Y_AXIS);
1382 updateControlPointOrientation(tcp);
1384 System.out.println("PipingTools.updateTurnControlPointTurn " + dir1 + " " + dir2 + " " + turnAngle + " " + turnAxis);
1388 public static List<PipeControlPoint> getControlPoints(PipeRun pipeRun) {
1389 List<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
1390 if (pipeRun.getControlPoints().size() == 0)
1392 PipeControlPoint pcp = pipeRun.getControlPoints().iterator().next();
1393 while (pcp.getPrevious() != null) {
1394 PipeControlPoint prev = pcp.getPrevious();
1395 if (prev.getPipeRun() != pipeRun)
1399 if (pcp.isDualSub()) {
1400 pcp = pcp.getParentPoint();
1403 while (pcp.getNext() != null) {
1404 pcp = pcp.getNext();
1405 if (pcp.getPipeRun() != pipeRun)
1412 public static void reverse(PipeRun pipeRun) {
1413 List<PipeControlPoint> list = getControlPoints(pipeRun);
1414 if (list.size() <= 1)
1415 return; // nothing to do.
1417 for (int i = 0 ; i < list.size(); i++) {
1418 boolean first = i == 0;
1419 boolean last = i == list.size() - 1;
1420 PipeControlPoint current = list.get(i);
1421 PipeControlPoint currentSub = null;
1422 if (current.isDualInline())
1423 currentSub = current.getSubPoint().get(0);
1425 PipeControlPoint next = list.get(i+1);
1426 if (next.isDualInline())
1427 next = next.getSubPoint().get(0);
1428 current.setNext(null);
1429 current.setPrevious(next);
1430 if (currentSub != null) {
1431 currentSub.setNext(null);
1432 currentSub.setPrevious(next);
1435 PipeControlPoint prev = list.get(i-1);
1437 current.setPrevious(null);
1438 current.setNext(prev);
1440 if (currentSub != null) {
1441 currentSub.setPrevious(null);
1442 currentSub.setNext(prev);
1445 PipeControlPoint prev = list.get(i-1);
1446 PipeControlPoint next = list.get(i+1);
1447 if (next.isDualInline())
1448 next = next.getSubPoint().get(0);
1451 current.setPrevious(next);
1452 current.setNext(prev);
1454 if (currentSub != null) {
1455 currentSub.setPrevious(next);
1456 currentSub.setNext(prev);
1463 public static void merge(PipeRun run1, PipeRun r2) {
1464 Map<PipeControlPoint, Vector3d> positions = new HashMap<PipeControlPoint, Vector3d>();
1465 Map<PipeControlPoint, Quat4d> orientations = new HashMap<PipeControlPoint, Quat4d>();
1466 for (PipeControlPoint pcp : r2.getControlPoints()) {
1467 positions.put(pcp, pcp.getWorldPosition());
1468 orientations.put(pcp, pcp.getWorldOrientation());
1470 for (PipeControlPoint pcp : r2.getControlPoints()) {
1471 r2.deattachChild(pcp);
1473 PipelineComponent component = pcp.getPipelineComponent();
1474 if (component != null) {
1475 if (!(component instanceof Nozzle)) {
1476 component.deattach();
1477 run1.addChild(component);
1479 Nozzle n = (Nozzle)component;
1488 public static void validate(PipeRun pipeRun) {
1489 if (pipeRun == null)
1491 Collection<PipeControlPoint> pcps = pipeRun.getControlPoints();
1493 for (PipeControlPoint pcp : pcps) {
1494 if (pcp.getParentPoint() == null || pcp.getParentPoint().getPipeRun() != pipeRun)
1497 List<PipeControlPoint> runPcps = getControlPoints(pipeRun);
1498 if (runPcps.size() != count) {
1499 System.out.println("Run is not connected");
1501 for (PipeControlPoint pcp : pcps) {
1502 if (pcp.getParentPoint() == null) {
1503 PipeControlPoint sub = null;
1504 if (pcp.isDualInline())
1505 sub = pcp.getSubPoint().get(0);
1506 PipeControlPoint next = pcp.getNext();
1507 PipeControlPoint prev = pcp.getPrevious();
1509 if (!(next.getPrevious() == pcp || next.getPrevious() == sub)) {
1510 System.out.println("Inconsistency between " + pcp + " -> " +next );
1514 PipeControlPoint prevParent = null;
1515 if (prev.isDualSub()) {
1516 prevParent = prev.getParentPoint();
1517 } else if (prev.isDualInline()) {
1518 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1520 if (!(prev.getNext() == pcp && (prevParent == null || prevParent.getNext() == pcp))) {
1521 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1528 public static void splitVariableLengthComponent(PipelineComponent newComponent, InlineComponent splittingComponent, boolean assignPos) throws Exception{
1529 assert(!splittingComponent.getControlPoint().isFixed());
1530 assert(!(newComponent instanceof InlineComponent && !newComponent.getControlPoint().isFixed()));
1531 PipeControlPoint newCP = newComponent.getControlPoint();
1532 PipeControlPoint splittingCP = splittingComponent.getControlPoint();
1533 PipeControlPoint nextCP = splittingCP.getNext();
1534 PipeControlPoint prevCP = splittingCP.getPrevious();
1536 /* there are many different cases to insert new component when
1537 it splits existing VariableLengthinlineComponent.
1539 1. VariableLengthComponet is connected from both sides:
1540 - insert new component between VariableLength component and component connected to it
1541 - insert new VariableLengthComponent between inserted component and component selected in previous step
1543 2. VariableLengthComponent is connected from one side
1544 - Use previous case or:
1545 - Insert new component to empty end
1546 - Insert new VariableLength component to inserted components empty end
1548 3. VariableLength is not connected to any component.
1549 - Should not be possible, at least in current implementation.
1550 - Could be done using second case
1554 if (nextCP == null && prevCP == null) {
1555 // this should not be possible
1556 throw new RuntimeException("VariableLengthComponent " + splittingComponent + " is not connected to anything.");
1558 double reservedLength = splittingComponent.getControlPoint().getLength();
1559 double newLength = newComponent.getControlPoint().getLength();
1562 Point3d next = new Point3d();
1563 Point3d prev = new Point3d();
1564 splittingCP.getInlineControlPointEnds(prev, next);
1566 Vector3d newPos = null;
1568 newPos = new Vector3d(prev);
1569 Vector3d dir = new Vector3d(next);
1573 newComponent.setWorldPosition(newPos);
1575 newPos = newComponent.getWorldPosition();
1580 Vector3d dir = new Vector3d(next);
1583 dir.scale(newLength * 0.5);
1584 Point3d vn = new Point3d(newPos);
1585 Point3d vp = new Point3d(newPos);
1588 double ln = vn.distance(next);
1589 double lp = vp.distance(prev);
1590 vp.interpolate(prev, 0.5);
1591 vn.interpolate(next, 0.5);
1594 PipeControlPoint newVariableLengthCP = null;//insertStraight(pcp1, pcp2, pos, length);
1595 if (nextCP == null) {
1596 newCP.insert(splittingCP, Direction.NEXT);
1597 newVariableLengthCP = insertStraight(newCP, Direction.NEXT, new Vector3d(vn), ln);
1598 splittingCP.setWorldPosition(new Vector3d(vp));
1599 // ControlPointTools.setWorldPosition(splittingCP, vp);
1600 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1601 } else if (prevCP == null) {
1602 newCP.insert(splittingCP, Direction.PREVIOUS);
1603 newVariableLengthCP = insertStraight(newCP, Direction.PREVIOUS, new Vector3d(vp), lp);
1604 splittingCP.setWorldPosition(new Vector3d(vn));
1605 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, ln);
1607 newCP.insert(splittingCP, nextCP);
1608 newVariableLengthCP = insertStraight(newCP, nextCP, new Vector3d(vn), ln);
1609 splittingCP.setWorldPosition(new Vector3d(vp));
1610 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1612 positionUpdate(newCP);
1616 public static void addSizeChange(boolean reversed, PipeRun pipeRun, PipeRun other, InlineComponent reducer, PipeControlPoint previous, PipeControlPoint next) {
1617 PipeControlPoint pcp = reducer.getControlPoint();
1618 PipeControlPoint ocp = pcp.getSubPoint().get(0);
1620 String name = pipeRun.getUniqueName("Reducer");
1621 reducer.setName(name);
1622 pipeRun.addChild(reducer);
1623 other.addChild(ocp);
1624 reducer.setAlternativePipeRun(other);
1626 previous.setNext(pcp);
1627 pcp.setPrevious(previous);
1628 ocp.setPrevious(previous);
1632 next.setPrevious(ocp);
1635 String name = other.getUniqueName("Reducer");
1636 reducer.setName(name);
1637 other.addChild(reducer);
1638 pipeRun.addChild(ocp);
1639 reducer.setAlternativePipeRun(pipeRun);
1643 pcp.setPrevious(next);
1644 ocp.setPrevious(next);
1646 pcp.setNext(previous);
1647 ocp.setNext(previous);
1648 previous.setPrevious(ocp);