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 double MIN_TURN_ANGLE = 0.001; // Threshold for removing turn components.
29 private static double ALLOWED_OFFSET = 0.001; // Allowed offset for directed path legs
31 private static final int REMOVE_NONE = 0;
32 private static final int REMOVE_START = 1;
33 private static final int REMOVE_END = 2;
34 private static final int REMOVE_BOTH = 3;
37 private enum PathLegUpdateType {
38 NONE, PREV, NEXT, PREV_S, NEXT_S
41 private static boolean enabled = true;
42 private static boolean updating = false;
43 private static boolean allowInsertRemove = true;
44 private static boolean triedIR = false;
47 private static List<PipeControlPoint> requestUpdates = new ArrayList<PipeControlPoint>();
48 private static List<PipeControlPoint> currentUpdates = new ArrayList<PipeControlPoint>();
50 private static Object updateMutex = new Object();
51 private static Object ruleMutex = new Object();
53 public static void requestUpdate(PipeControlPoint pcp) {
54 if (!PipingRules.enabled)
56 if (DEBUG) System.out.println("PipingRules request " + pcp);
57 synchronized (updateMutex) {
58 if (!requestUpdates.contains(pcp))
59 requestUpdates.add(pcp);
63 public static boolean update() throws Exception {
64 if (!PipingRules.enabled)
67 if (requestUpdates.size() == 0)
70 List<PipeControlPoint> temp = new ArrayList<PipeControlPoint>(requestUpdates.size());
71 synchronized(updateMutex) {
72 temp.addAll(requestUpdates);
73 requestUpdates.clear();
75 synchronized (ruleMutex) {
76 currentUpdates.clear();
77 currentUpdates.addAll(temp);
78 // TODO : we should remove already processed control points from currentUpdates after each _positionUpdate call.
79 for (PipeControlPoint pcp : currentUpdates)
80 _positionUpdate(pcp, true);
81 currentUpdates.clear();
83 synchronized(updateMutex) {
84 requestUpdates.removeAll(temp);
90 public static boolean positionUpdate(PipeControlPoint pcp) throws Exception {
92 return positionUpdate(pcp, true);
95 public static boolean positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception {
96 synchronized (ruleMutex) {
97 currentUpdates.add(pcp);
98 boolean b = _positionUpdate(pcp, allowIR);
99 currentUpdates.clear();
105 private static boolean _positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception {
106 if (updating || !enabled)
108 if (pcp.getPipeRun() == null)
111 if (DEBUG) System.out.println("PipingRules " + pcp);
113 allowInsertRemove = allowIR;
115 validate(pcp.getPipeRun());
116 if (pcp.isPathLegEnd()) {
117 updatePathLegEndControlPoint(pcp); // FIXME: Rules won't work properly, if they are not run twice.
118 updatePathLegEndControlPoint(pcp);
120 updateInlineControlPoint(pcp);
121 updateInlineControlPoint(pcp);
123 validate(pcp.getPipeRun());
124 if (!allowInsertRemove)
129 // System.out.println("PipingRules done " + pcp);
133 public static void setEnabled(boolean enabled) {
134 PipingRules.enabled = enabled;
136 currentUpdates.clear();
139 public static boolean isEnabled() {
143 // private void commit() {
144 // root.getNodeMap().commit();
147 public static class ExpandIterInfo {
148 // these two are turn control points
149 private PipeControlPoint start;
150 private PipeControlPoint end;
153 public ExpandIterInfo() {
157 public ExpandIterInfo(PipeControlPoint tcp, int type) {
158 if (type == REMOVE_START)
165 public ExpandIterInfo(PipeControlPoint start, PipeControlPoint end) {
168 this.type = REMOVE_BOTH;
171 public PipeControlPoint getEnd() {
175 public void setEnd(PipeControlPoint end) {
179 public PipeControlPoint getStart() {
183 public void setStart(PipeControlPoint start) {
187 public int getType() {
191 public void setType(int type) {
197 private static void updatePathLegEndControlPoint(PipeControlPoint pcp) throws Exception {
199 System.out.println("PipingRules.updatePathLegEndControlPoint() " + pcp);
200 if (pcp.getNext() != null) {
201 updatePathLegNext(pcp, pcp, PathLegUpdateType.NEXT_S);
203 if (pcp.getPrevious() != null) {
204 updatePathLegPrev(pcp, pcp, PathLegUpdateType.PREV_S);
209 private static void updateInlineControlPoint(PipeControlPoint pcp) throws Exception {
211 System.out.println("PipingRules.updateInlineControlPoint() " + pcp);
212 PipeControlPoint start = pcp.findPreviousEnd();
213 updatePathLegNext(start, pcp, PathLegUpdateType.NONE);
216 private static PipeControlPoint insertElbow(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos) throws Exception{
218 System.out.println("PipingRules.insertElbow() " + pcp1 + " " + pcp2 + " " + pos);
219 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
221 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getDualSub()) {
222 pcp1 = pcp1.getDualSub();
223 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
224 PipeControlPoint t = pcp1;
227 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getDualSub() && pcp2.getNext() == pcp1) {
228 PipeControlPoint t = pcp1;
229 pcp1 = pcp2.getDualSub();
232 throw new RuntimeException();
234 TurnComponent elbow = ComponentUtils.createTurn((P3DRootNode)pcp1.getRootNode());
235 PipeControlPoint pcp = elbow.getControlPoint();
236 if (pcp1.isDualInline())
237 pcp1 = pcp1.getDualSub();
238 String name = pcp1.getPipeRun().getUniqueName("Elbow");
240 pcp1.getPipeRun().addChild(elbow);
242 pcp.insert(pcp1, pcp2);
244 pcp.setWorldPosition(pos);
245 validate(pcp.getPipeRun());
249 private static PipeControlPoint insertStraight(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos, double length) throws Exception {
251 System.out.println("PipingRules.insertStraight() " + pcp1 + " " + pcp2 + " " + pos);
252 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
254 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getDualSub()) {
255 pcp1 = pcp1.getDualSub();
256 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
257 PipeControlPoint t = pcp1;
260 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getDualSub() && pcp2.getNext() == pcp1) {
261 PipeControlPoint t = pcp1;
262 pcp1 = pcp2.getDualSub();
265 throw new RuntimeException();
267 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp1.getRootNode());
268 PipeControlPoint scp = component.getControlPoint();
269 if (pcp1.isDualInline())
270 pcp1 = pcp1.getDualSub();
271 String name = pcp1.getPipeRun().getUniqueName("Pipe");
272 component.setName(name);
273 pcp1.getPipeRun().addChild(component);
275 scp.insert(pcp1, pcp2);
277 scp.setWorldPosition(pos);
278 scp.setLength(length);
279 validate(scp.getPipeRun());
283 private static PipeControlPoint insertStraight(PipeControlPoint pcp, Direction direction , Vector3d pos, double length) throws Exception {
285 System.out.println("PipingRules.insertStraight() " + pcp + " " + direction + " " + pos);
287 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp.getRootNode());
288 PipeControlPoint scp = component.getControlPoint();
289 if (pcp.isDualInline() && direction == Direction.NEXT)
290 pcp = pcp.getDualSub();
291 String name = pcp.getPipeRun().getUniqueName("Pipe");
292 component.setName(name);
293 pcp.getPipeRun().addChild(component);
295 scp.insert(pcp,direction);
297 scp.setWorldPosition(pos);
298 scp.setLength(length);
299 validate(scp.getPipeRun());
303 private static void updatePathLegNext(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
305 UpdateStruct2 us = createUS(start, Direction.NEXT, 0, new ArrayList<ExpandIterInfo>(), updated);
306 if (lengthChange == PathLegUpdateType.NONE) {
307 if (start.equals(updated))
308 lengthChange = PathLegUpdateType.NEXT;
309 else if (us.end.equals(updated))
310 lengthChange = PathLegUpdateType.PREV;
313 System.out.println("Null update struct " + start);
316 updatePathLeg(us, lengthChange);
319 private static void updatePathLegPrev(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
320 // TODO: this method is not symmetric with updatePathLegNext, which may alter lengthChange parameter?
321 UpdateStruct2 us = createUS(start, Direction.PREVIOUS, 0, new ArrayList<ExpandIterInfo>(), updated);
322 // if (lengthChange == PathLegUpdateType.NONE) {
323 // if (start.equals(updated))
324 // lengthChange = PathLegUpdateType.NEXT;
325 // else if (us.end.equals(updated))
326 // lengthChange = PathLegUpdateType.PREV;
329 System.out.println("Null update struct " + start);
332 updatePathLeg(us, lengthChange);
335 private static class UpdateStruct2 {
336 public PipeControlPoint start;
337 public Vector3d startPoint;
338 public ArrayList<PipeControlPoint> list;
339 public PipeControlPoint end;
340 public Vector3d endPoint;
342 public Vector3d offset;
343 public boolean hasOffsets;
345 public boolean reversed;
346 public ArrayList<ExpandIterInfo> toRemove;
347 public PipeControlPoint updated;
349 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) {
350 if (start == null || end == null)
351 throw new NullPointerException();
353 this.startPoint = startPoint;
356 this.endPoint = endPoint;
358 this.offset = offset;
359 this.hasOffsets = hasOffsets;
361 this.reversed = reversed;
362 this.toRemove = toRemove;
363 this.updated = updated;
365 if (!MathTools.isValid(startPoint) ||
366 !MathTools.isValid(endPoint) ||
367 !MathTools.isValid(dir)) {
368 throw new RuntimeException();
372 public String toString() {
373 return start + " " + end+ " " + dir + " " + hasOffsets + " " + offset + " " + iter + " " + toRemove.size();
378 @SuppressWarnings("unused")
379 private static boolean calculateOffset(Vector3d startPoint, Vector3d endPoint, ArrayList<PipeControlPoint> list, Vector3d dir, Vector3d offset) {
380 boolean hasOffsets = false;
381 List<PipeControlPoint> offsets = new ArrayList<PipeControlPoint>(list.size());
382 for (PipeControlPoint icp : list) {
383 if (icp.isOffset()) {
385 } else if (icp.isDualSub())
386 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
388 if (offsets.size() == 0) {
391 double l = dir.lengthSquared();
392 if (l > MathTools.NEAR_ZERO)
393 dir.scale(1.0/Math.sqrt(l));
394 offset.set(0.0, 0.0, 0.0);
397 Vector3d sp = new Vector3d(startPoint);
398 Point3d ep = new Point3d(endPoint);
401 double l = dir.lengthSquared();
402 if (l > MathTools.NEAR_ZERO)
403 dir.scale(1.0/Math.sqrt(l));
407 offset.set(0.0, 0.0, 0.0);
409 for (PipeControlPoint icp : offsets) {
410 Vector3d v = icp.getSizeChangeOffsetVector(dir);
413 Point3d nep = new Point3d(endPoint);
415 if (nep.distance(ep) < 0.0000000001) {
421 l = dir.lengthSquared();
422 if (l > MathTools.NEAR_ZERO)
423 dir.scale(1.0/Math.sqrt(l));
428 if (DEBUG && hasOffsets)
429 System.out.println("calcOffset s:"+ startPoint + " e:" + endPoint + " d:" + dir + " o:"+offset) ;
433 private static UpdateStruct2 createUS(PipeControlPoint start, Direction direction, int iter, ArrayList<ExpandIterInfo> toRemove, PipeControlPoint updated) {
434 ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
435 PipeControlPoint end = null;
436 if (direction == Direction.NEXT) {
437 end = start.findNextEnd(list);
439 ArrayList<PipeControlPoint> prevList = new ArrayList<PipeControlPoint>();
440 PipeControlPoint tend = start.findPreviousEnd(prevList);
441 for (PipeControlPoint icp : prevList) {
442 if (icp.isDualSub()) {
443 list.add(0, icp.getParentPoint());
453 boolean hasOffsets = false;
454 Vector3d offset = new Vector3d();
455 Vector3d startPoint = start.getWorldPosition();
456 Vector3d endPoint = end.getWorldPosition();
457 Vector3d dir = new Vector3d();
458 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
459 return new UpdateStruct2(start, startPoint, list, end, endPoint, dir, offset, hasOffsets, iter, direction == Direction.PREVIOUS, toRemove, updated);
462 private static boolean asDirected(PipeControlPoint pcp, Direction direction) {
463 if (pcp.isDirected())
465 if (pcp.asFixedAngle()) {
466 if (!pcp._getReversed())
467 return direction == Direction.NEXT;
469 return direction == Direction.PREVIOUS;
474 private static Vector3d direction(PipeControlPoint pcp, Direction direction) {
475 return pcp.getDirection(direction);
478 private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
480 if (asDirected(u.start, Direction.NEXT))
482 if (asDirected(u.end, Direction.PREVIOUS))
486 updateFreePathLeg(u, lengthChange);
489 updateDirectedPathLeg(u, lengthChange);
492 updateDualDirectedPathLeg(u, lengthChange);
498 private static void updateFreePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
500 System.out.println("PipingRules.updateFreePipeRun " + u + " " + lengthChange);
501 checkExpandPathLeg(u, lengthChange);
502 if (u.start.isInline() || u.end.isInline() || u.start.asFixedAngle()|| u.end.asFixedAngle())
503 processPathLeg(u, true, false);
506 private static void updateInlineControlPoints(UpdateStruct2 u, boolean checkSizes) throws Exception{
508 System.out.println("PipingRules.updateInlineControlPoints() " + u);
510 Vector3d start = new Vector3d(u.startPoint);
511 Vector3d end = new Vector3d(u.endPoint);
514 // create offsets for leg ends.
515 MathTools.mad(start, u.dir, u.start.getInlineLength());
516 MathTools.mad(end, u.dir, -u.end.getInlineLength());
519 boolean recalcline = false;
523 for (PipeControlPoint icp : u.list) {
524 updateInlineControlPoint(icp, start, end, u.dir);
526 if (icp.isOffset()) {
527 // TODO : offset vector is already calculated and should be cached
528 Vector3d off = icp.getSizeChangeOffsetVector(u.dir);
529 updateOffsetPoint(icp, off);
535 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
536 pathLegPoints.add(u.start);
537 for (PipeControlPoint icp : u.list) {
538 // updateInlineControlPoint(icp, u.startPoint,
539 // u.endPoint,u.dir);
540 updateBranchControlPointBranches(icp);
541 pathLegPoints.add(icp);
543 pathLegPoints.add(u.end);
545 // TODO : values can be cached in the loop
546 for (int i = 0; i < pathLegPoints.size(); i++) {
547 PipeControlPoint icp = pathLegPoints.get(i);
549 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
550 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
552 if (icp.isVariableLength()) {
553 if (prev != null && next != null) {
555 recalcline = recalcline | updateVariableLength(icp, prev, next);
558 // this is variable length component at the end of the
560 // the problem is that we want to keep unconnected end
561 // of the component in the same
562 // place, but center of the component must be moved.
563 updateVariableLengthEnd(icp, prev != null ? prev : next);
567 } else if (prev != null && !prev.isVariableLength()) {
568 // If this and previous control point are not variable
569 // length pcps, we'll have to check if there is no empty
570 // space between them.
571 // I there is, we'll have to create new variable length
572 // component between them.
573 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
576 } else { // with offset
577 Vector3d sp = new Vector3d(start);
578 Vector3d ep = new Vector3d(end);
581 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
582 pathLegPoints.add(u.start);
584 for (PipeControlPoint icp : u.list) {
585 updateInlineControlPoint(icp, sp, ep, u.dir);
586 updateBranchControlPointBranches(icp);
587 pathLegPoints.add(icp);
588 if (icp.isOffset()) {
589 // TODO : offset vector is already calculated and should be
591 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
592 updateOffsetPoint(icp, offset);
597 pathLegPoints.add(u.end);
602 sp = new Vector3d(u.startPoint);
603 ep = new Vector3d(u.endPoint);
606 for (int i = 0; i < pathLegPoints.size(); i++) {
607 PipeControlPoint icp = pathLegPoints.get(i);
609 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
610 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
612 if (prev != null && prev.isDualInline())
613 prev = prev.getDualSub();
616 if (icp.isVariableLength()) {
617 if (prev != null && next != null) {
618 recalcline = recalcline | updateVariableLength(icp, prev, next);
621 // this is variable length component at the end of the
623 // the problem is that we want to keep unconnected end
624 // of the component in the same
625 // place, but center of the component must be moved.
626 updateVariableLengthEnd(icp, prev != null ? prev : next);
628 } else if (prev != null && !prev.isVariableLength()) {
629 // If this and previous control point are not variable
630 // length pcps, we'll have to check if there is no empty
631 // space between them.
632 // I there is, we'll have to create new variable length
633 // component between them.
634 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
636 if (icp.isOffset()) {
637 // TODO : offset vector is already calculated and should be
639 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
647 u.start.findNextEnd(u.list);
651 private static boolean updateVariableLength(PipeControlPoint icp, PipeControlPoint prev, PipeControlPoint next) {
652 Vector3d prevPos = prev.getWorldPosition();
653 Vector3d nextPos = next.getWorldPosition();
655 Vector3d dir = new Vector3d(nextPos);
657 double l = dir.lengthSquared(); // distance between
660 double l2prev = prev.getInlineLength(); // distance
664 double l2next = next.getInlineLength();
665 double l2 = l2prev + l2next;
666 double l2s = MathTools.square(l2);
667 if (l2s < l) { // check if there is enough space for
668 // variable length component.
671 double length = Math.sqrt(l) - l2; // true length of
675 dir.scale(length * 0.5 + l2prev); // calculate
680 icp.setWorldPosition(dir);
681 icp.setLength(length);
684 // components leave no space to the component and it
687 if (icp.isDeletable()) {
688 if (!allowInsertRemove) {
689 icp.setLength(0.0001);
694 System.out.println("PipingRules.updateVariableLength removing " + icp);
702 private static boolean possibleVaribleLengthInsert(PipeControlPoint icp, PipeControlPoint prev) throws Exception{
703 Vector3d currentPos = icp.getWorldPosition();
704 Vector3d prevPos = prev.getWorldPosition();
705 Vector3d dir = new Vector3d(currentPos);
707 double l = dir.lengthSquared();
708 double l2prev = prev.getInlineLength();
709 double l2next = icp.getInlineLength();
710 double l2 = l2prev + l2next;
711 double l2s = l2 * l2;
713 if (allowInsertRemove) {
715 double length = Math.sqrt(l) - l2; // true length of the
718 dir.scale(length * 0.5 + l2prev); // calculate center
722 insertStraight(prev, icp, dir, length);
731 private static void updateVariableLengthEnd(PipeControlPoint icp, PipeControlPoint prev) {
732 Vector3d currentPos = icp.getWorldPosition();
733 Vector3d prevPos = prev.getWorldPosition();
735 Vector3d dir = new Vector3d();
736 dir.sub(currentPos, prevPos);
738 boolean simple = currentUpdates.contains(icp);
740 // Update based on position -> adjust length
741 double currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
742 icp.setLength(currentLength);
744 // Update based on neighbour movement -> adjust length and position, so that free end stays in place.
745 double currentLength = icp.getLength();
746 if (currentLength < MathTools.NEAR_ZERO) {
747 currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
750 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
752 Point3d endPos = new Point3d(dir);
753 endPos.scale(currentLength * 0.5);
754 endPos.add(currentPos); // this is the free end of the component
756 double offset = prev.getInlineLength();
757 Point3d beginPos = new Point3d(dir);
758 beginPos.scale(offset);
759 beginPos.add(prevPos); // this is the connected end of the component
761 double l = beginPos.distance(endPos);
764 System.out.println("Length for " + icp + " is NaN");
767 beginPos.add(dir); // center position
770 System.out.println("PipingRules.updateInlineControlPoints() setting variable length to " + l);
773 icp.setWorldPosition(new Vector3d(beginPos));
777 private static void ppNoOffset(UpdateStruct2 u) throws Exception {
779 System.out.println("PipingRules.ppNoOffset() " + u);
780 Vector3d offset = new Vector3d();
783 for (PipeControlPoint icp : u.list) {
784 if (icp.isOffset()) {
785 offset.add(icp.getSizeChangeOffsetVector(u.dir));
786 } else if (icp.isDualSub())
787 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
791 checkExpandPathLeg(u, PathLegUpdateType.NONE);
794 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 {
796 System.out.println("PipingRules.ppNoDir() " + start + " " + end + " " + iter + " " + toRemove.size());
797 // FIXME : extra loop (dir should be calculated here)
798 Vector3d dir = new Vector3d();
799 Vector3d offset = new Vector3d();
800 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
801 ppNoOffset(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, null, hasOffsets, iter, reversed, toRemove, updated));
804 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
805 checkExpandPathLeg(u, lengthChange, u.updated.isInline() && u.updated.isOffset());
808 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange, boolean updateEnds) throws Exception {
810 System.out.println("PipingRules.checkExpandPathLeg() " + u + " " + lengthChange);
811 if (lengthChange != PathLegUpdateType.NONE) {
812 // FIXME : turns cannot be checked before inline cps are updated,
813 // since their position affects calculation of turns
814 processPathLeg(u, updateEnds, false);
815 int type = checkTurns(u, lengthChange);
816 if (type == REMOVE_NONE) {
817 processPathLeg(u, updateEnds, true);
819 expandPathLeg(u, type);
822 processPathLeg(u, updateEnds, true);
826 private static void updateDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
828 System.out.println("PipingRules.updateDirectedPipeRun() " + u + " " + lengthChange);
829 PipeControlPoint dcp;
830 PipeControlPoint other;
831 boolean canMoveOther = false;
832 boolean dcpStart = false;
833 boolean inlineEnd = false;
835 if (asDirected(u.start, Direction.NEXT)) {
838 position = u.startPoint;
842 inlineEnd = u.end.isInline();
847 position = u.endPoint;
850 inlineEnd = u.start.isInline();
853 Vector3d directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
854 if (directedDirection == null) {
855 //updateTurnControlPointTurn(dcp, dcp.getPrevious(), dcp.getNext());
856 updateTurnControlPointTurn(dcp, null, null);
857 directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
858 if (directedDirection == null) {
862 Point3d directedEndPoint = new Point3d(u.endPoint);
864 directedEndPoint.add(u.offset);
866 double mu[] = new double[2];
869 Vector3d t = new Vector3d();
872 closest = MathTools.closestPointOnStraight(directedEndPoint, u.startPoint, directedDirection, mu);
873 t.sub(closest, directedEndPoint);
875 closest = MathTools.closestPointOnStraight(u.startPoint, directedEndPoint, directedDirection, mu);
876 t.sub(closest, u.startPoint);
879 double distance = t.length();
880 boolean aligned = (distance < ALLOWED_OFFSET);
882 if (u.start.isInline() || u.end.isInline() || u.start.asFixedAngle() || u.end.asFixedAngle())
883 processPathLeg(u, true, false);
884 checkExpandPathLeg(u, lengthChange, inlineEnd);
890 PipeControlPoint nextToMoved;
892 if (u.list.size() > 0)
894 nextToMoved = u.list.get(0);
896 nextToMoved = u.list.get(u.list.size() - 1);
900 nextToMoved = u.start;
901 if (other.isVariableAngle()) {
903 // TODO calculate needed space from next run end.
904 double space = spaceForTurn(other);
907 closest.set(u.startPoint);
909 closest.set(u.endPoint);
911 Vector3d v = new Vector3d(directedDirection);
918 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + closest);
919 other.setWorldPosition(closest);
921 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));
922 if (u.end.getNext() != null)
923 updatePathLegNext(u.end, u.updated, PathLegUpdateType.NEXT);
925 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));
926 if (u.start.getPrevious() != null)
927 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.PREV);
930 // TODO : calculate needed space from next run end.
931 if (allowInsertRemove)
932 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
937 } else if (other.isNonDirected() && other.getParentPoint() != null) {
938 // FIXME : this code was for updating branches
939 Vector3d bintersect = new Vector3d();
940 PipeControlPoint bcp = other.getParentPoint();
941 if (bcp != null && canMoveOther) {
942 Point3d bstart = new Point3d();
943 Point3d bend = new Point3d();
944 Vector3d bdir = new Vector3d();
945 bcp.getInlineControlPointEnds(bstart, bend, bdir);
946 Vector3d nintersect = new Vector3d();
948 MathTools.intersectStraightStraight(position, directedDirection, bend, bdir, nintersect, bintersect, mu);
949 Vector3d dist = new Vector3d(nintersect);
950 dist.sub(bintersect);
951 canMoveOther = mu[1] > 0.0 && mu[1] < 1.0 && dist.lengthSquared() < 0.01;
953 // TODO : endControlPoints are undirected: calculcate
954 // correct position for it
955 throw new UnsupportedOperationException("not implemented");
959 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + bintersect);
960 // is required branch position is in possible range
961 bcp.setWorldPosition(bintersect);
963 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);
965 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);
968 // branch cannot be moved into right position, new turn
969 // / elbow must be inserted
970 if (allowInsertRemove)
971 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
976 } else { // assume that control point cannot be moved, but can
978 if (allowInsertRemove)
979 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
989 private static void updateDualDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
991 System.out.println("PipingRules.updateDualDirectedPipeRun() " + u + " " + lengthChange);
993 PipeControlPoint dcp1 = u.start;
994 PipeControlPoint dcp2 = u.end;
995 Point3d position1 = new Point3d(u.startPoint);
996 Point3d position2 = new Point3d(u.endPoint);
997 Point3d position1offset = new Point3d(position1);
998 position1offset.sub(u.offset);
999 Point3d position2offset = new Point3d(position2);
1000 position2offset.add(u.offset);
1001 Vector3d dir1 = direction(dcp1, Direction.NEXT);
1002 Vector3d dir2 = direction(dcp2, Direction.PREVIOUS);
1003 Vector3d p1 = MathTools.closestPointOnStraight(position1offset, position2, dir2);
1004 Vector3d p2 = MathTools.closestPointOnStraight(position2offset, position1, dir1);
1005 double d1 = position1.distance(new Point3d(p1));
1006 double d2 = position2.distance(new Point3d(p2));
1008 boolean aligned = (d1 < ALLOWED_OFFSET && d2 < ALLOWED_OFFSET);
1014 } else if (allowInsertRemove){
1015 PipeControlPoint dcp;
1016 PipeControlPoint next;
1019 if (u.list.size() > 0)
1020 next = u.list.get(0);
1025 if (u.list.size() > 0)
1026 next = u.list.get(u.list.size() - 1);
1031 p1 = dcp.getWorldPosition();
1032 Vector3d v = new Vector3d();
1038 // Reserve space for 90 deg elbow
1039 double off = dcp1.getPipeRun().getTurnRadius();
1044 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position2, dir2);
1046 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position1, dir1);
1048 // By default, the elbows are placed next to each other, by using 90 deg angles.
1049 // If the distance between elbows is not enough, we must move the other elbow (and create more shallow angle elbows)
1050 if (MathTools.distance(p1, p2) < off*2.05) {
1054 PipeControlPoint tcp1 = insertElbow(dcp, next, p1);
1055 PipeControlPoint tcp2 = insertElbow(tcp1, next, p2);
1058 System.out.println("PipingRules.updateDualDirectedPipeRun() created two turns " + tcp1 + " " + tcp2);
1061 Vector3d dd = new Vector3d(p2);
1064 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1065 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1067 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1069 updatePathLegPrev(tcp2, u.updated, PathLegUpdateType.NONE);
1071 Vector3d dd = new Vector3d(p1);
1074 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1075 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1077 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1079 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.NONE);
1088 private static double spaceForTurn(PipeControlPoint tcp) {
1089 // TODO : this returns now space for 90 deg turn.
1090 // The challenge: position of tcp affects the turn angle, which then affects the required space. Perhaps we need to iterate...
1091 // Additionally, if the path legs contain offset, using just positions of opposite path leg ends is not enough,
1092 return ((TurnComponent)tcp.getPipelineComponent()).getTurnRadius();
1095 private static void insertElbowUpdate(UpdateStruct2 u, PipeControlPoint dcp, PipeControlPoint next, boolean dcpStart, Vector3d position, Vector3d directedDirection) throws Exception{
1098 // Vector3d closest = new Vector3d(position);
1099 // closest.add(directedDirection);
1101 PipeControlPoint tcp = null;
1104 closest = MathTools.closestPointOnStraight(next.getWorldPosition(), position, directedDirection);
1105 tcp = insertElbow(dcp, next, closest);
1107 closest = MathTools.closestPointOnStraight(dcp.getWorldPosition(), position, directedDirection);
1108 tcp = insertElbow(next, dcp, closest);
1110 // TODO properly calculate required distance between start and inserted elbow.
1111 double d = MathTools.distance(position, closest);
1112 double s = spaceForTurn(tcp);
1115 Vector3d p = new Vector3d(directedDirection);
1125 System.out.println("PipingRules.updateDirectedPipeRun() inserted " + tcp);
1128 // update pipe run from new turn to other end
1129 ppNoDir(tcp, new Vector3d(closest), u.list, u.end, u.endPoint, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1130 // update pipe run from directed to new turn
1131 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));
1133 // update pipe run from other end to new turn
1134 ppNoDir(u.start, u.startPoint, u.list, tcp, new Vector3d(closest), u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1135 // update pipe run from new turn to directed
1136 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));
1141 * Checks if turns can be removed (turn angle near zero)
1143 private static int checkTurns(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1145 System.out.println("PipingRules.checkTurns() " + u.start + " " + u.end);
1146 boolean startRemoved = false;
1147 boolean endRemoved = false;
1148 if (u.start.isVariableAngle()) {
1149 // this won't work properly if inline control points are not updated
1150 PipeControlPoint startPrev = u.start.getPrevious();
1151 if (startPrev != null) {
1152 double a = updateTurnControlPointTurn(u.start, null, u.dir);
1153 if (a < MIN_TURN_ANGLE && u.start.isDeletable())
1154 startRemoved = true;
1155 else if (lengthChange == PathLegUpdateType.PREV || lengthChange == PathLegUpdateType.PREV_S) {
1156 PathLegUpdateType type;
1157 if (lengthChange == PathLegUpdateType.PREV_S)
1158 type = PathLegUpdateType.PREV;
1160 type = PathLegUpdateType.NONE;
1161 updatePathLegPrev(u.start, u.start, type);
1165 if (u.end.isVariableAngle()) {
1167 PipeControlPoint endNext = u.end.getNext();
1168 if (endNext != null) {
1169 // TODO: u.end, u.dir, null
1170 double a = updateTurnControlPointTurn(u.end, null, null);
1171 if (a < MIN_TURN_ANGLE && u.end.isDeletable())
1173 else if (lengthChange == PathLegUpdateType.NEXT || lengthChange == PathLegUpdateType.NEXT_S) {
1174 PathLegUpdateType type;
1175 if (lengthChange == PathLegUpdateType.NEXT_S)
1176 type = PathLegUpdateType.NEXT;
1178 type = PathLegUpdateType.NONE;
1179 updatePathLegNext(u.end, u.end, type);
1184 System.out.println("PipingRules.checkTurns() res " + startRemoved + " " + endRemoved);
1185 if (!startRemoved && !endRemoved)
1187 if (startRemoved && endRemoved)
1190 return REMOVE_START;
1195 * Expands piperun search over turns that are going to be removed
1198 private static void expandPathLeg(UpdateStruct2 u, int type) throws Exception {
1200 System.out.println("PipingRules.expandPipeline " + u.start + " " + u.end);
1201 ArrayList<PipeControlPoint> newList = new ArrayList<PipeControlPoint>();
1204 throw new RuntimeException("Error in piping rules");
1206 u.toRemove.add(new ExpandIterInfo(u.start, REMOVE_START));
1207 u.start = u.start.findPreviousEnd();
1208 u.startPoint = u.start.getPosition();
1209 u.start.findNextEnd(newList);
1210 newList.addAll(u.list);
1214 u.toRemove.add(new ExpandIterInfo(u.end, REMOVE_END));
1215 u.end = u.end.findNextEnd(newList);
1216 u.endPoint = u.end.getPosition();
1217 u.list.addAll(newList);
1220 u.toRemove.add(new ExpandIterInfo(u.start, u.end));
1221 u.start = u.start.findPreviousEnd();
1222 u.startPoint = u.start.getPosition();
1223 u.start.findNextEnd(newList);
1224 newList.addAll(u.list);
1226 newList = new ArrayList<PipeControlPoint>();
1227 u.end = u.end.findNextEnd(newList);
1228 u.endPoint = u.end.getPosition();
1229 u.list.addAll(newList);
1232 throw new RuntimeException("Error in piping rules");
1235 u.offset = new Vector3d();
1238 for (PipeControlPoint icp : u.list) {
1239 if (icp.isOffset()) {
1240 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1241 } else if (icp.isDualSub())
1242 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1246 System.out.println("PipingRules.expandPipeline expanded " + u.start + " " + u.end);
1248 updatePathLeg(u, PathLegUpdateType.NONE);
1252 * reverts one iteration of turn removing back)
1254 private static void backIter(UpdateStruct2 u) throws Exception {
1257 System.out.println("PipingRules.backIter" + u.start + " " + u.end);
1259 throw new RuntimeException("Error in piping rules");
1260 ExpandIterInfo info = u.toRemove.get(u.toRemove.size() - 1);
1261 u.toRemove.remove(u.toRemove.size() - 1);
1262 if (info.getType() == REMOVE_START || info.getType() == REMOVE_BOTH) {
1263 while (u.list.size() > 0) {
1264 PipeControlPoint icp = u.list.get(0);
1265 if (icp.getPrevious().equals(info.getStart()))
1269 u.start = info.getStart();
1271 if (info.getType() == REMOVE_END || info.getType() == REMOVE_BOTH) {
1272 while (u.list.size() > 0) {
1273 PipeControlPoint icp = u.list.get(u.list.size() - 1);
1274 if (icp.getNext().equals(info.getEnd()))
1278 u.end = info.getEnd();
1280 u.offset = new Vector3d();
1283 for (PipeControlPoint icp : u.list) {
1284 if (icp.isOffset()) {
1285 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1286 } else if (icp.isDualSub())
1287 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1295 * Processes pipe run (removes necessary turns and updates run ends)
1297 // private static void processPathLeg(PipeControlPoint start, Point3d
1298 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1299 // end,Point3d endPoint, Vector3d dir,Vector3d offset, boolean
1300 // hasOffsets,int iter, boolean reversed, ArrayList<ExpandIterInfo>
1301 // toRemove) throws TransactionException {
1303 private static void processPathLeg(UpdateStruct2 u) throws Exception {
1305 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1306 processPathLeg(u, true, true);
1309 private static void processPathLeg(UpdateStruct2 u, boolean updateEnds, boolean updateInline) throws Exception {
1311 System.out.println("PipingRules.processPathLeg " + (updateEnds ? "ends " : "") + (updateInline ? "inline " : "") + u.start + " " + u.end);
1313 if (u.toRemove.size() > 0) {
1314 for (ExpandIterInfo info : u.toRemove) {
1315 if (info.getStart() != null) {
1317 System.out.println("PipingRules.processPathLeg removing start " + info.getStart());
1318 info.getStart()._remove();
1320 if (info.getEnd() != null) {
1322 System.out.println("PipingRules.processPathLeg removing end " + info.getEnd());
1323 info.getEnd()._remove();
1326 // ControlPointTools.removeControlPoint may remove more than one CP;
1327 // we must populate inline CP list again.
1329 u.start.findNextEnd( u.list);
1331 // FIXME : inline CPs are update twice because their positions must be
1332 // updated before and after ends.
1333 updateInlineControlPoints(u, false);
1336 if (u.start.isTurn()) {
1337 //updateTurnControlPointTurn(u.start, u.start.getPrevious(), u.start.getNext());
1338 updateTurnControlPointTurn(u.start, null, null);
1339 // updatePathLegPrev(u.start, u.start, PathLegUpdateType.NONE);
1340 } else if (u.start.isEnd()) {
1341 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1342 } else if (u.start.isInline()) {
1343 updateControlPointOrientation(u.start);
1345 if (u.end.isTurn()) {
1346 //updateTurnControlPointTurn(u.end, u.end.getPrevious(), u.end.getNext());
1347 updateTurnControlPointTurn(u.end, null, null);
1348 // updatePathLegNext(u.end, u.end, PathLegUpdateType.NONE);
1349 } else if (u.end.isEnd()) {
1350 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1351 } else if (u.end.isInline()) {
1352 updateControlPointOrientation(u.end);
1356 if (u.start.isEnd()) {
1357 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1359 if (u.end.isEnd()) {
1360 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1364 updateInlineControlPoints(u, true);
1369 * Processes pipe run and recalculates offset
1371 // private static void processPathLeg(PipeControlPoint start, Point3d
1372 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1373 // end,Point3d endPoint, Vector3d dir, boolean hasOffsets,int iter, boolean
1374 // reversed, ArrayList<ExpandIterInfo> toRemove) throws TransactionException
1376 @SuppressWarnings("unused")
1377 private static void processPathLegNoOffset(UpdateStruct2 u) throws Exception {
1379 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1380 Vector3d offset = new Vector3d();
1383 for (PipeControlPoint icp : u.list) {
1384 if (icp.isOffset()) {
1385 offset.add(icp.getSizeChangeOffsetVector(u.dir));
1386 } else if (icp.isDualSub()) {
1387 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1394 private static void updateOffsetPoint(PipeControlPoint sccp, Vector3d offset) {
1395 Vector3d world = sccp.getWorldPosition();
1397 PipeControlPoint ocp = sccp.getDualSub();
1398 ocp.setWorldPosition(world);
1402 * Updates InlineControlPoints position when straight pipe's end(s) have
1410 private static void updateInlineControlPoint(PipeControlPoint icp, Vector3d prev, Vector3d next, Vector3d dir) {
1412 System.out.println("PipingRules.updateInlineControlPoint() " + icp);
1414 Vector3d inlinePoint = icp.getWorldPosition();
1415 Vector3d prevPoint = new Vector3d(prev);
1416 Vector3d nextPoint = new Vector3d(next);
1417 if (!icp.isVariableLength()) {
1418 // Reserve space for fixed length components.
1419 MathTools.mad(prevPoint, dir, icp.getInlineLength());
1420 MathTools.mad(nextPoint, dir, -icp.getInlineLength());
1421 if (MathTools.distance(prevPoint, nextPoint) < ALLOWED_OFFSET) {
1426 boolean canCalc = MathTools.distance(prevPoint, nextPoint) > ALLOWED_OFFSET;
1428 System.out.print("InlineControlPoint update " + icp + " " + inlinePoint + " " + prevPoint + " " + nextPoint);
1429 Vector3d newInlinePoint = null;
1431 boolean branchUpdate = false;
1432 PipeControlPoint becp = null;
1433 for (PipeControlPoint pcp : icp.getChildPoints())
1434 if (pcp.isNonDirected()) {
1435 branchUpdate = true;
1440 if (DUMMY || !branchUpdate) {
1441 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1445 // FIXME : can only handle one branch
1446 PipeControlPoint p = null;
1447 if (becp.getNext() != null) {
1448 p = becp.findNextEnd();
1449 } else if (becp.getPrevious() != null) {
1450 p = becp.findPreviousEnd();
1453 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1454 } else if (canCalc){
1455 Vector3d branchLegEnd = p.getWorldPosition();
1456 Vector3d dir2 = new Vector3d(inlinePoint);
1457 dir2.sub(branchLegEnd);
1458 Vector3d dir1 = new Vector3d(nextPoint);
1459 dir1.sub(prevPoint);
1460 newInlinePoint = new Vector3d();
1461 double mu[] = new double[2];
1462 MathTools.intersectStraightStraight(new Vector3d(prevPoint), dir1, new Vector3d(branchLegEnd), dir2, newInlinePoint, new Vector3d(), mu);
1464 System.out.println(mu[0]);
1465 // FIXME : reserve space
1467 newInlinePoint = new Vector3d(prevPoint);
1468 } else if (mu[0] > 1.0) {
1469 newInlinePoint = new Vector3d(nextPoint);
1474 // prevPoint == nextPoint
1475 newInlinePoint = new Vector3d(prevPoint);
1478 System.out.println(" " + newInlinePoint);
1480 icp.setWorldPosition(newInlinePoint);
1481 updateControlPointOrientation(icp);
1485 * Updates InlineControlPoints position when straight pipe's end(s) have
1493 private static void updateEndComponentControlPoint(PipeControlPoint ecp, Vector3d start, Vector3d end) throws Exception {
1495 System.out.println("PipingRules.updateEndComponentControlPoint() " + ecp);
1496 //FIXME : end control point cannot be fixed!
1497 //if (!ecp.isFixed())
1498 updateControlPointOrientation(ecp);
1500 for (PipeControlPoint pcp : ecp.getChildPoints()) {
1501 // TODO update position
1502 updatePathLegEndControlPoint(pcp);
1506 private static void updateControlPointOrientation(PipeControlPoint pcp) {
1507 // FIXME : hack to bypass variable length components orientation
1508 // if (pcp.getAtMostOneRelatedObject(ProcessResource.g3dResource.HasWorldOrientation) == null)
1510 // if (pcp.rotationAngle == null)
1512 Double angleO = pcp.getRotationAngle();
1516 Boolean reversedO = pcp.getReversed();
1517 boolean reversed = false;
1518 if (reversedO != null)
1519 reversed = reversedO;
1520 Quat4d q = pcp.getControlPointOrientationQuat(angle, reversed);
1521 pcp.setWorldOrientation(q);
1525 * Updates all branches when branch's position has been changed
1529 private static void updateBranchControlPointBranches(PipeControlPoint bcp) throws Exception {
1531 System.out.println("PipingRules.updateBranchControlPointBranches() " + bcp);
1532 if (bcp.isDualInline())
1534 Collection<PipeControlPoint> branches = bcp.getChildPoints();
1535 if (branches.size() == 0) {
1537 System.out.println("No Branches found");
1541 for (PipeControlPoint pcp : branches) {
1542 updatePathLegEndControlPoint(pcp);
1546 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d prev, Vector3d next) {
1548 UpdateStruct2 us = createUS(tcp, Direction.NEXT, 0, new ArrayList<PipingRules.ExpandIterInfo>(), tcp);
1553 UpdateStruct2 us = createUS(tcp, Direction.PREVIOUS, 0, new ArrayList<PipingRules.ExpandIterInfo>(), tcp);
1559 if (!tcp.asFixedAngle()) {
1562 if (next == null || prev == null) {
1563 if (tcp.getTurnAngle() != null)
1564 return tcp.getTurnAngle();
1565 return Math.PI; // FIXME : argh
1567 double turnAngle = prev.angle(next);
1569 double angle = Math.PI - turnAngle;
1571 Vector3d turnAxis = new Vector3d();
1572 turnAxis.cross(prev, next);
1573 if (turnAxis.lengthSquared() > MathTools.NEAR_ZERO) {
1574 double elbowRadius = ((TurnComponent)tcp.getPipelineComponent()).getTurnRadius();
1575 double R = elbowRadius / Math.tan(angle * 0.5);
1577 turnAxis.normalize();
1578 tcp.setTurnAngle(turnAngle);
1579 tcp.setLength(R);// setComponentOffsetValue(R);
1580 tcp.setTurnAxis(turnAxis);
1581 // tcp.setPosition(tcp.getPosition());
1584 tcp.setTurnAngle(0.0);
1586 tcp.setTurnAxis(new Vector3d(MathTools.Y_AXIS));
1589 updateControlPointOrientation(tcp);
1592 System.out.println("PipingTools.updateTurnControlPointTurn " + prev + " " + next + " " + turnAngle + " " + turnAxis);
1596 if (prev != null && next != null) {
1598 } else if (prev == null) {
1599 if (!tcp._getReversed())
1600 tcp.setReversed(true);
1601 } else if (next == null) {
1602 if (tcp._getReversed())
1603 tcp.setReversed(false);
1606 Vector3d dir = null;
1607 if (!tcp._getReversed()) {
1614 return Math.PI; // FIXME : argh
1617 Quat4d q = PipeControlPoint.getControlPointOrientationQuat(dir, tcp.getRotationAngle() != null ? tcp.getRotationAngle() : 0.0);
1618 Vector3d v = new Vector3d();
1619 MathTools.rotate(q, MathTools.Y_AXIS,v);
1621 tcp.setWorldOrientation(q);
1622 if (tcp.getTurnAngle() != null)
1623 return tcp.getTurnAngle();
1624 return Math.PI; // FIXME : argh
1630 public static List<PipeControlPoint> getControlPoints(PipeRun pipeRun) {
1631 List<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
1632 if (pipeRun.getControlPoints().size() == 0)
1634 PipeControlPoint pcp = pipeRun.getControlPoints().iterator().next();
1635 while (pcp.getPrevious() != null) {
1636 PipeControlPoint prev = pcp.getPrevious();
1637 if (prev.getPipeRun() != pipeRun && prev.getPipeRun() != null) { // bypass possible corruption
1642 if (pcp.isDualSub()) {
1643 pcp = pcp.getParentPoint();
1646 while (pcp.getNext() != null) {
1647 pcp = pcp.getNext();
1648 if (pcp.getPipeRun() != pipeRun)
1655 public static void reverse(PipeRun pipeRun) {
1658 List<PipeControlPoint> points = getControlPoints(pipeRun);
1659 PipeControlPoint pcp = points.get(0);
1660 if (pcp.isSizeChange() && pcp.getChildPoints().size() > 0) {
1661 pipeRun = pcp.getPipeRun();
1666 List<PipeRun> all = new ArrayList<PipeRun>();
1667 List<List<PipeControlPoint>> pcps = new ArrayList<List<PipeControlPoint>>();
1670 List<PipeControlPoint> points = getControlPoints(pipeRun);
1672 PipeControlPoint pcp = points.get(points.size()-1);
1673 if (pcp.getChildPoints().size() > 0) {
1674 PipeRun pipeRun2 = pcp.getChildPoints().get(0).getPipeRun();
1675 if (pipeRun == pipeRun2)
1683 for (int i = 0 ; i < all.size(); i++) {
1684 List<PipeControlPoint> list = pcps.get(i);
1687 for (int i = 0 ; i < all.size(); i++) {
1688 boolean last = i == all.size() - 1;
1689 List<PipeControlPoint> list = pcps.get(i);
1692 List<PipeControlPoint> list2 = pcps.get(i+1);
1693 PipeControlPoint prev = list.get(list.size()-1);
1694 PipeControlPoint next = list2.get(0);
1695 System.out.println();
1697 // Reverse the component on the boundary.
1698 InlineComponent ic = (InlineComponent)prev.getPipelineComponent();
1699 PipeRun r1 = ic.getPipeRun();
1700 PipeRun r2 = ic.getAlternativePipeRun();
1701 if (r1 == null || r2 == null)
1702 throw new RuntimeException("Components on PipeRun changes should refer to bot PipeRuns");
1706 ic.setAlternativePipeRun(r1);
1708 throw new RuntimeException("PipeRun changes should contain shared control points");
1716 private static void _reverse(List<PipeControlPoint> list) {
1717 if (list.size() <= 1)
1718 return; // nothing to do.
1720 for (int i = 0 ; i < list.size(); i++) {
1721 boolean first = i == 0;
1722 boolean last = i == list.size() - 1;
1723 PipeControlPoint current = list.get(i);
1724 PipeControlPoint currentSub = null;
1725 if (current.isDualInline())
1726 currentSub = current.getDualSub();
1728 PipeControlPoint next = list.get(i+1);
1729 if (next.isDualInline())
1730 next = next.getDualSub();
1731 if (current.getNext() == next)
1732 current.setNext(null);
1733 current.setPrevious(next);
1734 if (currentSub != null) {
1735 if (currentSub.getNext() == next)
1736 currentSub.setNext(null);
1737 currentSub.setPrevious(next);
1740 PipeControlPoint prev = list.get(i-1);
1742 if (current.getPrevious() == prev)
1743 current.setPrevious(null);
1744 current.setNext(prev);
1746 if (currentSub != null) {
1747 if (currentSub.getPrevious() == prev)
1748 currentSub.setPrevious(null);
1749 currentSub.setNext(prev);
1752 PipeControlPoint prev = list.get(i-1);
1753 PipeControlPoint next = list.get(i+1);
1754 if (next.isDualInline())
1755 next = next.getDualSub();
1758 current.setPrevious(next);
1759 current.setNext(prev);
1761 if (currentSub != null) {
1762 currentSub.setPrevious(next);
1763 currentSub.setNext(prev);
1767 //if (current.isTurn() && current.isFixed()) {
1768 if (current.asFixedAngle()) {
1769 current.setReversed(!current._getReversed());
1771 if (current.isInline() && current.isReverse()) {
1772 current.setReversed(!current._getReversed());
1779 public static void validate(PipeRun pipeRun) {
1780 if (pipeRun == null)
1782 Collection<PipeControlPoint> pcps = pipeRun.getControlPoints();
1784 //System.out.println("Validate " + pipeRun.getName());
1785 for (PipeControlPoint pcp : pcps) {
1786 if (pcp.getParentPoint() == null || pcp.getParentPoint().getPipeRun() != pipeRun)
1789 List<PipeControlPoint> runPcps = getControlPoints(pipeRun);
1790 if (runPcps.size() != count) {
1791 System.out.println("Run " + pipeRun.getName() + " contains unconnected control points, found " + runPcps.size() + " connected, " + pcps.size() + " total.");
1792 for (PipeControlPoint pcp : pcps) {
1793 if (!runPcps.contains(pcp)) {
1794 System.out.println("Unconnected " + pcp + " " + pcp.getPipelineComponent());
1798 for (PipeControlPoint pcp : pcps) {
1799 if (pcp.getPipeRun() == null) {
1800 System.out.println("PipeRun ref missing " + pcp + " " + pcp.getPipelineComponent());
1802 if (!pcp.isDirected() && pcp.getNext() == null && pcp.getPrevious() == null)
1803 System.out.println("Orphan undirected " + pcp + " " + pcp.getPipelineComponent());
1805 for (PipeControlPoint pcp : pcps) {
1806 if (pcp.getParentPoint() == null) {
1807 PipeControlPoint sub = null;
1808 if (pcp.isDualInline())
1809 sub = pcp.getDualSub();
1810 PipeControlPoint next = pcp.getNext();
1811 PipeControlPoint prev = pcp.getPrevious();
1813 if (!(next.getPrevious() == pcp || next.getPrevious() == sub)) {
1814 System.out.println("Inconsistency between " + pcp + " -> " +next );
1818 PipeControlPoint prevParent = null;
1819 if (prev.isDualSub()) {
1820 prevParent = prev.getParentPoint();
1821 } else if (prev.isDualInline()) {
1822 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1824 if (!(prev.getNext() == pcp && (prevParent == null || prevParent.getNext() == pcp))) {
1825 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1832 public static void splitVariableLengthComponent(PipelineComponent newComponent, InlineComponent splittingComponent, boolean assignPos) throws Exception{
1833 assert(!splittingComponent.getControlPoint().isFixedLength());
1834 assert(!(newComponent instanceof InlineComponent && !newComponent.getControlPoint().isFixedLength()));
1835 PipeControlPoint newCP = newComponent.getControlPoint();
1836 PipeControlPoint splittingCP = splittingComponent.getControlPoint();
1837 PipeControlPoint nextCP = splittingCP.getNext();
1838 PipeControlPoint prevCP = splittingCP.getPrevious();
1840 /* there are many different cases to insert new component when
1841 it splits existing VariableLengthinlineComponent.
1843 1. VariableLengthComponet is connected from both sides:
1844 - insert new component between VariableLength component and component connected to it
1845 - insert new VariableLengthComponent between inserted component and component selected in previous step
1847 2. VariableLengthComponent is connected from one side
1848 - Use previous case or:
1849 - Insert new component to empty end
1850 - Insert new VariableLength component to inserted components empty end
1852 3. VariableLength is not connected to any component.
1853 - Should not be possible, at least in current implementation.
1854 - Could be done using second case
1858 if (nextCP == null && prevCP == null) {
1859 // this should not be possible
1860 throw new RuntimeException("VariableLengthComponent " + splittingComponent + " is not connected to anything.");
1862 double newLength = newComponent.getControlPoint().getLength();
1865 Point3d next = new Point3d();
1866 Point3d prev = new Point3d();
1867 splittingCP.getInlineControlPointEnds(prev, next);
1869 Vector3d newPos = null;
1871 newPos = new Vector3d(prev);
1872 Vector3d dir = new Vector3d(next);
1876 newComponent.setWorldPosition(newPos);
1878 newPos = newComponent.getWorldPosition();
1883 Vector3d dir = new Vector3d(next);
1886 dir.scale(newLength * 0.5);
1887 Point3d vn = new Point3d(newPos);
1888 Point3d vp = new Point3d(newPos);
1891 double ln = vn.distance(next);
1892 double lp = vp.distance(prev);
1893 vp.interpolate(prev, 0.5);
1894 vn.interpolate(next, 0.5);
1897 if (nextCP == null) {
1898 newCP.insert(splittingCP, Direction.NEXT);
1899 insertStraight(newCP, Direction.NEXT, new Vector3d(vn), ln);
1900 splittingCP.setWorldPosition(new Vector3d(vp));
1901 // ControlPointTools.setWorldPosition(splittingCP, vp);
1902 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1903 } else if (prevCP == null) {
1904 newCP.insert(splittingCP, Direction.PREVIOUS);
1905 insertStraight(newCP, Direction.PREVIOUS, new Vector3d(vp), lp);
1906 splittingCP.setWorldPosition(new Vector3d(vn));
1907 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, ln);
1909 newCP.insert(splittingCP, nextCP);
1910 insertStraight(newCP, nextCP, new Vector3d(vn), ln);
1911 splittingCP.setWorldPosition(new Vector3d(vp));
1912 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1914 positionUpdate(newCP);
1918 public static void addSizeChange(boolean reversed, PipeRun pipeRun, PipeRun other, InlineComponent reducer, PipeControlPoint previous, PipeControlPoint next) {
1919 PipeControlPoint pcp = reducer.getControlPoint();
1920 PipeControlPoint ocp = pcp.getDualSub();
1922 String name = pipeRun.getUniqueName("Reducer");
1923 reducer.setName(name);
1924 pipeRun.addChild(reducer);
1925 other.addChild(ocp);
1926 reducer.setAlternativePipeRun(other);
1928 previous.setNext(pcp);
1929 pcp.setPrevious(previous);
1930 ocp.setPrevious(previous);
1934 next.setPrevious(ocp);
1937 String name = other.getUniqueName("Reducer");
1938 reducer.setName(name);
1939 other.addChild(reducer);
1940 pipeRun.addChild(ocp);
1941 reducer.setAlternativePipeRun(pipeRun);
1945 pcp.setPrevious(next);
1946 ocp.setPrevious(next);
1948 pcp.setNext(previous);
1949 ocp.setNext(previous);
1950 previous.setPrevious(ocp);