1 package org.simantics.plant3d.scenegraph.controlpoint;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import javax.vecmath.Point3d;
8 import javax.vecmath.Quat4d;
9 import javax.vecmath.Vector3d;
11 import org.simantics.g3d.math.MathTools;
12 import org.simantics.plant3d.scenegraph.InlineComponent;
13 import org.simantics.plant3d.scenegraph.P3DRootNode;
14 import org.simantics.plant3d.scenegraph.PipeRun;
15 import org.simantics.plant3d.scenegraph.PipelineComponent;
16 import org.simantics.plant3d.scenegraph.TurnComponent;
17 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
18 import org.simantics.plant3d.utils.ComponentUtils;
19 import org.simantics.utils.ui.ErrorLogger;
21 public class PipingRules {
22 private static final boolean DEBUG = false;
23 private static final boolean DUMMY = false;
25 private static double MIN_TURN_ANGLE = 0.001; // Threshold for removing turn components.
26 private static double ALLOWED_OFFSET = 0.001; // Allowed offset for directed path legs
27 private static double MIN_INLINE_LENGTH = 0.001; // Minimum length of inline components, when component removal is not allowed.
29 private static final int REMOVE_NONE = 0;
30 private static final int REMOVE_START = 1;
31 private static final int REMOVE_END = 2;
32 private static final int REMOVE_BOTH = 3;
35 // PathLeg iteration indicator. NEXT_S > NEXT > NONE PREV_S > PREV > NONE
36 private enum PathLegUpdateType {
37 NONE, // Only current path leg needs to be updated (for example, inline comp was moved)
38 PREV, // Current and previous path leg need to be updated
39 NEXT, // Current and next path leg need to be updated
40 PREV_S, // Current and previous two path legs need to be updated (turn was moved, which affect other path leg end turns, and thus following path legs
41 NEXT_S // Current and next two path legs need to be updated
44 private static boolean enabled = true; //
45 private static boolean updating = false;
46 private static boolean allowInsertRemove = true;
47 private static boolean triedIR = false;
50 private static List<PipeControlPoint> requestUpdates = new ArrayList<PipeControlPoint>();
51 private static List<PipeControlPoint> currentUpdates = new ArrayList<PipeControlPoint>();
53 private static Object updateMutex = new Object();
54 private static Object ruleMutex = new Object();
56 public static void requestUpdate(PipeControlPoint pcp) {
57 if (!PipingRules.enabled)
59 if (DEBUG) System.out.println("PipingRules request " + pcp);
60 synchronized (updateMutex) {
61 if (!requestUpdates.contains(pcp))
62 requestUpdates.add(pcp);
66 public static boolean update() throws Exception {
67 if (!PipingRules.enabled)
70 if (requestUpdates.size() == 0)
73 List<PipeControlPoint> temp = new ArrayList<PipeControlPoint>(requestUpdates.size());
74 synchronized(updateMutex) {
75 temp.addAll(requestUpdates);
76 requestUpdates.clear();
78 synchronized (ruleMutex) {
79 currentUpdates.clear();
80 currentUpdates.addAll(temp);
81 // TODO : we should remove already processed control points from currentUpdates after each _positionUpdate call.
82 for (PipeControlPoint pcp : currentUpdates)
83 _positionUpdate(pcp, true);
84 currentUpdates.clear();
86 synchronized(updateMutex) {
87 requestUpdates.removeAll(temp);
93 public static boolean positionUpdate(PipeControlPoint pcp) throws Exception {
95 return positionUpdate(pcp, true);
98 public static boolean positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception {
99 synchronized (ruleMutex) {
100 currentUpdates.add(pcp);
101 boolean b = _positionUpdate(pcp, allowIR);
102 currentUpdates.clear();
108 private static boolean _positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception {
109 if (updating || !enabled)
111 if (pcp.getPipeRun() == null)
114 if (DEBUG) System.out.println("PipingRules " + pcp);
116 allowInsertRemove = allowIR;
118 validate(pcp.getPipeRun());
119 if (pcp.asPathLegEnd()) {
120 updatePathLegEndControlPoint(pcp); // FIXME: Rules won't work properly, if they are not run twice.
121 //updatePathLegEndControlPoint(pcp);
123 updateInlineControlPoint(pcp);
124 //updateInlineControlPoint(pcp);
126 validate(pcp.getPipeRun());
127 if (!allowInsertRemove)
132 // System.out.println("PipingRules done " + pcp);
136 public static void setEnabled(boolean enabled) {
137 PipingRules.enabled = enabled;
139 currentUpdates.clear();
142 public static boolean isEnabled() {
146 public static class ExpandIterInfo {
147 // these two are turn control points
148 private PipeControlPoint start;
149 private PipeControlPoint end;
152 public ExpandIterInfo() {
156 public ExpandIterInfo(PipeControlPoint tcp, int type) {
157 if (type == REMOVE_START)
164 public ExpandIterInfo(PipeControlPoint start, PipeControlPoint end) {
167 this.type = REMOVE_BOTH;
170 public PipeControlPoint getEnd() {
174 public void setEnd(PipeControlPoint end) {
178 public PipeControlPoint getStart() {
182 public void setStart(PipeControlPoint start) {
186 public int getType() {
190 public void setType(int type) {
196 private static void updatePathLegEndControlPoint(PipeControlPoint pcp) throws Exception {
198 System.out.println("PipingRules.updatePathLegEndControlPoint() " + pcp);
199 if (pcp.getNext() != null) {
200 updatePathLegNext(pcp, pcp, PathLegUpdateType.NEXT_S);
202 if (pcp.getPrevious() != null) {
203 updatePathLegPrev(pcp, pcp, PathLegUpdateType.PREV_S);
208 private static void updateInlineControlPoint(PipeControlPoint pcp) throws Exception {
210 System.out.println("PipingRules.updateInlineControlPoint() " + pcp);
211 PipeControlPoint start = pcp.findPreviousEnd();
212 updatePathLegNext(start, pcp, PathLegUpdateType.NONE);
215 private static PipeControlPoint insertElbow(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos) throws Exception{
217 System.out.println("PipingRules.insertElbow() " + pcp1 + " " + pcp2 + " " + pos);
218 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
220 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getDualSub()) {
221 pcp1 = pcp1.getDualSub();
222 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
223 PipeControlPoint t = pcp1;
226 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getDualSub() && pcp2.getNext() == pcp1) {
227 PipeControlPoint t = pcp1;
228 pcp1 = pcp2.getDualSub();
231 throw new RuntimeException();
233 TurnComponent elbow = ComponentUtils.createTurn((P3DRootNode)pcp1.getRootNode());
234 PipeControlPoint pcp = elbow.getControlPoint();
235 if (pcp1.isDualInline())
236 pcp1 = pcp1.getDualSub();
237 String name = pcp1.getPipeRun().getUniqueName("Elbow");
239 pcp1.getPipeRun().addChild(elbow);
241 pcp.insert(pcp1, pcp2);
243 pcp.setWorldPosition(pos);
244 validate(pcp.getPipeRun());
248 private static PipeControlPoint insertStraight(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos, double length) throws Exception {
250 System.out.println("PipingRules.insertStraight() " + pcp1 + " " + pcp2 + " " + pos);
251 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
253 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getDualSub()) {
254 pcp1 = pcp1.getDualSub();
255 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
256 PipeControlPoint t = pcp1;
259 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getDualSub() && pcp2.getNext() == pcp1) {
260 PipeControlPoint t = pcp1;
261 pcp1 = pcp2.getDualSub();
264 throw new RuntimeException();
266 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp1.getRootNode());
267 PipeControlPoint scp = component.getControlPoint();
268 if (pcp1.isDualInline())
269 pcp1 = pcp1.getDualSub();
270 String name = pcp1.getPipeRun().getUniqueName("Pipe");
271 component.setName(name);
272 pcp1.getPipeRun().addChild(component);
274 scp.insert(pcp1, pcp2);
276 scp.setWorldPosition(pos);
277 scp.setLength(length);
278 validate(scp.getPipeRun());
282 private static PipeControlPoint insertStraight(PipeControlPoint pcp, Direction direction , Vector3d pos, double length) throws Exception {
284 System.out.println("PipingRules.insertStraight() " + pcp + " " + direction + " " + pos);
286 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp.getRootNode());
287 PipeControlPoint scp = component.getControlPoint();
288 if (pcp.isDualInline() && direction == Direction.NEXT)
289 pcp = pcp.getDualSub();
290 String name = pcp.getPipeRun().getUniqueName("Pipe");
291 component.setName(name);
292 pcp.getPipeRun().addChild(component);
294 scp.insert(pcp,direction);
296 scp.setWorldPosition(pos);
297 scp.setLength(length);
298 validate(scp.getPipeRun());
302 private static void updatePathLegNext(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
303 UpdateStruct2 us = createUS(start, Direction.NEXT, 0, new ArrayList<ExpandIterInfo>(), updated);
305 System.out.println("Null update struct " + start);
308 updatePathLeg(us, lengthChange);
311 private static void updatePathLegPrev(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
312 UpdateStruct2 us = createUS(start, Direction.PREVIOUS, 0, new ArrayList<ExpandIterInfo>(), updated);
314 System.out.println("Null update struct " + start);
317 updatePathLeg(us, lengthChange);
320 private static class UpdateStruct2 {
321 public PipeControlPoint start;
322 public Vector3d startPoint;
323 public ArrayList<PipeControlPoint> list;
324 public PipeControlPoint end;
325 public Vector3d endPoint;
327 public Vector3d offset;
328 public boolean hasOffsets;
330 public boolean reversed;
331 public ArrayList<ExpandIterInfo> toRemove;
332 public PipeControlPoint updated;
334 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) {
335 if (start == null || end == null)
336 throw new NullPointerException();
338 this.startPoint = startPoint;
341 this.endPoint = endPoint;
343 this.offset = offset;
344 this.hasOffsets = hasOffsets;
346 this.reversed = reversed;
347 this.toRemove = toRemove;
348 this.updated = updated;
350 if (!MathTools.isValid(startPoint) ||
351 !MathTools.isValid(endPoint) ||
352 !MathTools.isValid(dir)) {
353 throw new RuntimeException();
357 public String toString() {
358 return start + " " + end+ " " + dir + " " + hasOffsets + " " + offset + " " + iter + " " + toRemove.size();
363 @SuppressWarnings("unused")
364 private static boolean calculateOffset(Vector3d startPoint, Vector3d endPoint, ArrayList<PipeControlPoint> list, Vector3d dir, Vector3d offset) {
365 boolean hasOffsets = false;
366 List<PipeControlPoint> offsets = new ArrayList<PipeControlPoint>(list.size());
367 for (PipeControlPoint icp : list) {
368 if (icp.isOffset()) {
370 } else if (icp.isDualSub())
371 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
373 if (offsets.size() == 0) {
376 double l = dir.lengthSquared();
377 if (l > MathTools.NEAR_ZERO)
378 dir.scale(1.0/Math.sqrt(l));
379 offset.set(0.0, 0.0, 0.0);
382 Vector3d sp = new Vector3d(startPoint);
383 Point3d ep = new Point3d(endPoint);
386 double l = dir.lengthSquared();
387 if (l > MathTools.NEAR_ZERO)
388 dir.scale(1.0/Math.sqrt(l));
392 offset.set(0.0, 0.0, 0.0);
394 for (PipeControlPoint icp : offsets) {
395 Vector3d v = icp.getSizeChangeOffsetVector(dir);
398 Point3d nep = new Point3d(endPoint);
400 if (nep.distance(ep) < 0.0000000001) {
406 l = dir.lengthSquared();
407 if (l > MathTools.NEAR_ZERO)
408 dir.scale(1.0/Math.sqrt(l));
413 if (DEBUG && hasOffsets)
414 System.out.println("calcOffset s:"+ startPoint + " e:" + endPoint + " d:" + dir + " o:"+offset) ;
418 private static UpdateStruct2 createUS(PipeControlPoint start, Direction direction, int iter, ArrayList<ExpandIterInfo> toRemove, PipeControlPoint updated) {
419 ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
420 PipeControlPoint end = null;
421 if (direction == Direction.NEXT) {
422 end = start.findNextEnd(list);
424 ArrayList<PipeControlPoint> prevList = new ArrayList<PipeControlPoint>();
425 PipeControlPoint tend = start.findPreviousEnd(prevList);
426 for (PipeControlPoint icp : prevList) {
427 if (icp.isDualSub()) {
428 list.add(0, icp.getParentPoint());
438 boolean hasOffsets = false;
439 Vector3d offset = new Vector3d();
440 Vector3d startPoint = start.getWorldPosition();
441 Vector3d endPoint = end.getWorldPosition();
442 Vector3d dir = new Vector3d();
443 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
444 return new UpdateStruct2(start, startPoint, list, end, endPoint, dir, offset, hasOffsets, iter, direction == Direction.PREVIOUS, toRemove, updated);
447 private static boolean asDirected(PipeControlPoint pcp, Direction direction) {
448 if (pcp.isDirected())
450 if (pcp.asFixedAngle()) {
451 if (!pcp._getReversed())
452 return direction == Direction.NEXT;
454 return direction == Direction.PREVIOUS;
459 private static Vector3d direction(PipeControlPoint pcp, Direction direction) {
460 return pcp.getDirection(direction);
463 private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
466 if (lengthChange == PathLegUpdateType.NONE) {
470 updatePathLeg(u, lengthChange, rs, re);
473 private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange, boolean rs, boolean re) throws Exception {
475 if (asDirected(u.start, Direction.NEXT))
477 if (asDirected(u.end, Direction.PREVIOUS))
480 u.start.getPipelineComponent().setError(null);
482 u.end.getPipelineComponent().setError(null);
483 for (PipeControlPoint pcp : u.list)
484 pcp.getPipelineComponent().setError(null);
487 updateFreePathLeg(u, lengthChange);
490 updateDirectedPathLeg(u, lengthChange);
493 updateDualDirectedPathLeg(u, lengthChange);
499 private static void updateFreePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
501 System.out.println("PipingRules.updateFreePipeRun " + u + " " + lengthChange);
502 checkExpandPathLeg(u, lengthChange);
503 if (u.start.isInline() || u.end.isInline() || u.start.asFixedAngle()|| u.end.asFixedAngle())
504 processPathLeg(u, true, false);
507 private static void updateInlineControlPoints(UpdateStruct2 u, boolean checkSizes) throws Exception{
509 System.out.println("PipingRules.updateInlineControlPoints() " + u);
511 Vector3d start = new Vector3d(u.startPoint);
512 Vector3d end = new Vector3d(u.endPoint);
515 // create offsets for leg ends.
516 if (u.start.isTurn())
517 MathTools.mad(start, u.dir, u.start.getInlineLength());
519 MathTools.mad(end, u.dir, -u.end.getInlineLength());
522 boolean recalcline = false;
526 for (PipeControlPoint icp : u.list) {
527 updateInlineControlPoint(icp, start, end, u.dir);
529 if (icp.isOffset()) {
530 // TODO : offset vector is already calculated and should be cached
531 Vector3d off = icp.getSizeChangeOffsetVector(u.dir);
532 updateOffsetPoint(icp, off);
538 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
539 ArrayList<PipeControlPoint> fixedLengthPoints = new ArrayList<PipeControlPoint>();
540 pathLegPoints.add(u.start);
542 for (PipeControlPoint icp : u.list) {
543 // updateInlineControlPoint(icp, u.startPoint,
544 // u.endPoint,u.dir);
545 updateBranchControlPointBranches(icp);
546 pathLegPoints.add(icp);
547 if (!icp.isVariableLength())
548 fixedLengthPoints.add(icp);
550 pathLegPoints.add(u.end);
552 // updateInlineControlPoint keeps components between path leg ends, but does not ensure that fixed length components do no overlap each other
554 for (int i = 0; i < fixedLengthPoints.size(); i++) {
555 PipeControlPoint prev = i == 0 ? null : fixedLengthPoints.get(i-1);
556 PipeControlPoint curr = fixedLengthPoints.get(i);
557 PipeControlPoint next = i == fixedLengthPoints.size() -1 ? null : fixedLengthPoints.get(i+1);
558 updateFixedLength(curr, prev, next, start,end, u.dir);
561 for (int i = 0; i < pathLegPoints.size(); i++) {
562 PipeControlPoint icp = pathLegPoints.get(i);
564 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
565 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
567 if (icp.isVariableLength()) {
568 if (prev != null && next != null) {
570 recalcline = recalcline | updateVariableLength(icp, prev, next);
573 // this is variable length component at the end of the
575 // the problem is that we want to keep unconnected end
576 // of the component in the same
577 // place, but center of the component must be moved.
578 updateVariableLengthEnd(icp, prev != null ? prev : next);
582 } else if (prev != null && !prev.isVariableLength()) {
583 // If this and previous control point are not variable
584 // length pcps, we'll have to check if there is no empty
585 // space between them.
586 // I there is, we'll have to create new variable length
587 // component between them.
588 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
591 } else { // with offset
592 Vector3d sp = new Vector3d(start);
593 Vector3d ep = new Vector3d(end);
596 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
597 pathLegPoints.add(u.start);
599 for (PipeControlPoint icp : u.list) {
600 updateInlineControlPoint(icp, sp, ep, u.dir);
601 updateBranchControlPointBranches(icp);
602 pathLegPoints.add(icp);
603 if (icp.isOffset()) {
604 // TODO : offset vector is already calculated and should be
606 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
607 updateOffsetPoint(icp, offset);
612 pathLegPoints.add(u.end);
617 sp = new Vector3d(u.startPoint);
618 ep = new Vector3d(u.endPoint);
621 for (int i = 0; i < pathLegPoints.size(); i++) {
622 PipeControlPoint icp = pathLegPoints.get(i);
624 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
625 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
627 if (prev != null && prev.isDualInline())
628 prev = prev.getDualSub();
631 if (icp.isVariableLength()) {
632 if (prev != null && next != null) {
633 recalcline = recalcline | updateVariableLength(icp, prev, next);
636 // this is variable length component at the end of the
638 // the problem is that we want to keep unconnected end
639 // of the component in the same
640 // place, but center of the component must be moved.
641 updateVariableLengthEnd(icp, prev != null ? prev : next);
643 } else if (prev != null && !prev.isVariableLength()) {
644 // If this and previous control point are not variable
645 // length pcps, we'll have to check if there is no empty
646 // space between them.
647 // I there is, we'll have to create new variable length
648 // component between them.
649 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
651 if (icp.isOffset()) {
652 // TODO : offset vector is already calculated and should be
654 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
662 u.start.findNextEnd(u.list);
665 double pathLegLength = MathTools.distance(u.startPoint, u.endPoint);
666 double availableLength = pathLegLength;
667 if (u.start.isTurn())
668 availableLength -= u.start.getInlineLength();
670 availableLength -= u.end.getInlineLength();
671 for (PipeControlPoint pcp : u.list) {
672 if (!pcp.isVariableLength())
673 availableLength-= pcp.getLength();
675 if (availableLength < 0.0) {
676 u.start.getPipelineComponent().setError("Not enough available space");
677 u.end.getPipelineComponent().setError("Not enough available space");
678 for (PipeControlPoint pcp : u.list)
679 pcp.getPipelineComponent().setError("Not enough available space");
681 // System.out.println(u.start.getPipelineComponent().toString() + " " + pathLegLength + " " + availableLength + " " + u.end.getPipelineComponent().toString() + " " + u.start.getInlineLength() + " " + u.end.getInlineLength());
685 private static void updateFixedLength(PipeControlPoint icp, PipeControlPoint prev, PipeControlPoint next, Vector3d s, Vector3d e, Vector3d dir) {
687 checkOverlap(icp, prev);
690 checkOverlap(icp, next);
693 private static void checkOverlap(PipeControlPoint icp, PipeControlPoint prev) {
694 double d = MathTools.distance(prev.getWorldPosition(), icp.getWorldPosition());
695 double r = icp.getInlineLength() + prev.getInlineLength();
697 if (icp.getPipelineComponent().getError() == null)
698 icp.getPipelineComponent().setError("Overlapping");
699 if (prev.getPipelineComponent().getError() == null)
700 prev.getPipelineComponent().setError("Overlapping");
704 private static boolean updateVariableLength(PipeControlPoint icp, PipeControlPoint prev, PipeControlPoint next) {
705 Vector3d prevPos = prev.getWorldPosition();
706 Vector3d nextPos = next.getWorldPosition();
708 Vector3d dir = new Vector3d(nextPos);
710 double l = dir.length(); // distance between control points
711 double l2prev = prev.getInlineLength(); // distance taken by components
712 double l2next = next.getInlineLength();
713 double l2 = l2prev + l2next;
714 double length = l - l2; // true length of the variable length component
715 if (length >= MIN_INLINE_LENGTH) { // check if there is enough space for variable length component.
718 dir.scale(length * 0.5 + l2prev); // calculate center position of the component
720 icp.setWorldPosition(dir);
721 icp.setLength(length);
724 // components leave no space to the component and it must be removed
725 if (icp.isDeletable()) {
726 if (!allowInsertRemove) {
727 icp.setLength(MIN_INLINE_LENGTH);
728 icp.getPipelineComponent().setError("Not enough available space");
733 System.out.println("PipingRules.updateVariableLength removing " + icp);
737 icp.setLength(MIN_INLINE_LENGTH);
738 icp.getPipelineComponent().setError("Not enough available space");
744 private static boolean possibleVaribleLengthInsert(PipeControlPoint icp, PipeControlPoint prev) throws Exception{
745 Vector3d currentPos = icp.getWorldPosition();
746 Vector3d prevPos = prev.getWorldPosition();
747 Vector3d dir = new Vector3d(currentPos);
749 double l = dir.lengthSquared();
750 double l2prev = prev.getInlineLength();
751 double l2next = icp.getInlineLength();
752 double l2 = l2prev + l2next;
753 double l2s = l2 * l2;
755 if (allowInsertRemove) {
757 double length = Math.sqrt(l) - l2; // true length of the variable length component
758 dir.scale(length * 0.5 + l2prev); // calculate center position of the component
760 insertStraight(prev, icp, dir, length);
769 private static void updateVariableLengthEnd(PipeControlPoint icp, PipeControlPoint prev) {
770 Vector3d currentPos = icp.getWorldPosition();
771 Vector3d prevPos = prev.getWorldPosition();
773 Vector3d dir = new Vector3d();
774 dir.sub(currentPos, prevPos);
776 boolean simple = currentUpdates.contains(icp);
778 // Update based on position -> adjust length
779 double currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
780 icp.setLength(currentLength);
782 // Update based on neighbour movement -> adjust length and position, so that free end stays in place.
783 double currentLength = icp.getLength();
784 if (currentLength < MathTools.NEAR_ZERO) {
785 currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
788 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
790 Point3d endPos = new Point3d(dir);
791 endPos.scale(currentLength * 0.5);
792 endPos.add(currentPos); // this is the free end of the component
794 double offset = prev.getInlineLength();
795 Point3d beginPos = new Point3d(dir);
796 beginPos.scale(offset);
797 beginPos.add(prevPos); // this is the connected end of the component
799 double l = beginPos.distance(endPos);
802 System.out.println("Length for " + icp + " is NaN");
805 beginPos.add(dir); // center position
808 System.out.println("PipingRules.updateInlineControlPoints() setting variable length to " + l);
811 icp.setWorldPosition(new Vector3d(beginPos));
815 private static void ppNoOffset(UpdateStruct2 u) throws Exception {
817 System.out.println("PipingRules.ppNoOffset() " + u);
818 Vector3d offset = new Vector3d();
821 for (PipeControlPoint icp : u.list) {
822 if (icp.isOffset()) {
823 offset.add(icp.getSizeChangeOffsetVector(u.dir));
824 } else if (icp.isDualSub())
825 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
829 checkExpandPathLeg(u, PathLegUpdateType.NONE);
832 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 {
834 System.out.println("PipingRules.ppNoDir() " + start + " " + end + " " + iter + " " + toRemove.size());
835 // FIXME : extra loop (dir should be calculated here)
836 Vector3d dir = new Vector3d();
837 Vector3d offset = new Vector3d();
838 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
839 ppNoOffset(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, null, hasOffsets, iter, reversed, toRemove, updated));
842 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
843 checkExpandPathLeg(u, lengthChange, u.updated.isInline() && u.updated.isOffset());
846 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange, boolean updateEnds) throws Exception {
848 System.out.println("PipingRules.checkExpandPathLeg() " + u + " " + lengthChange);
849 if (lengthChange != PathLegUpdateType.NONE) {
850 // FIXME : turns cannot be checked before inline cps are updated,
851 // since their position affects calculation of turns
852 processPathLeg(u, updateEnds, false);
853 int type = checkTurns(u, lengthChange);
854 if (type == REMOVE_NONE) {
855 processPathLeg(u, updateEnds, true);
857 expandPathLeg(u, type);
860 processPathLeg(u, updateEnds, true);
864 private static void updateDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
866 System.out.println("PipingRules.updateDirectedPipeRun() " + u + " " + lengthChange);
867 PipeControlPoint dcp;
868 PipeControlPoint other;
869 boolean canMoveOther = false;
870 boolean dcpStart = false;
871 boolean inlineEnd = false;
873 if (asDirected(u.start, Direction.NEXT)) {
876 position = u.startPoint;
880 inlineEnd = u.end.isInline();
885 position = u.endPoint;
888 inlineEnd = u.start.isInline();
891 Vector3d directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
892 if (directedDirection == null) {
893 //updateTurnControlPointTurn(dcp, dcp.getPrevious(), dcp.getNext());
894 updateTurnControlPointTurn(dcp, null, null);
895 directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
896 if (directedDirection == null) {
900 Point3d directedEndPoint = new Point3d(u.endPoint);
902 directedEndPoint.add(u.offset);
904 double mu[] = new double[2];
907 Vector3d t = new Vector3d();
910 closest = MathTools.closestPointOnStraight(directedEndPoint, u.startPoint, directedDirection, mu);
911 t.sub(closest, directedEndPoint);
913 closest = MathTools.closestPointOnStraight(u.startPoint, directedEndPoint, directedDirection, mu);
914 t.sub(closest, u.startPoint);
917 double distance = t.length();
918 boolean aligned = (distance < ALLOWED_OFFSET);
920 if (u.start.isInline() || u.end.isInline() || u.start.asFixedAngle() || u.end.asFixedAngle())
921 processPathLeg(u, true, false);
922 checkExpandPathLeg(u, lengthChange, inlineEnd);
928 PipeControlPoint nextToMoved;
930 if (u.list.size() > 0)
932 nextToMoved = u.list.get(0);
934 nextToMoved = u.list.get(u.list.size() - 1);
938 nextToMoved = u.start;
939 if (other.isVariableAngle()) {
941 // TODO calculate needed space from next run end.
942 double space = spaceForTurn(other);
945 closest.set(u.startPoint);
947 closest.set(u.endPoint);
949 Vector3d v = new Vector3d(directedDirection);
956 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + closest);
957 other.setWorldPosition(closest);
959 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));
960 if (u.end.getNext() != null)
961 updatePathLegNext(u.end, u.updated, PathLegUpdateType.NEXT);
963 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));
964 if (u.start.getPrevious() != null)
965 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.PREV);
968 // TODO : calculate needed space from next run end.
969 if (allowInsertRemove)
970 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
975 } else if (other.isNonDirected() && other.getParentPoint() != null) {
976 // FIXME : this code was for updating branches
977 Vector3d bintersect = new Vector3d();
978 PipeControlPoint bcp = other.getParentPoint();
979 if (bcp != null && canMoveOther) {
980 Point3d bstart = new Point3d();
981 Point3d bend = new Point3d();
982 Vector3d bdir = new Vector3d();
983 bcp.getInlineControlPointEnds(bstart, bend, bdir);
984 Vector3d nintersect = new Vector3d();
986 MathTools.intersectStraightStraight(position, directedDirection, bend, bdir, nintersect, bintersect, mu);
987 Vector3d dist = new Vector3d(nintersect);
988 dist.sub(bintersect);
989 canMoveOther = mu[1] > 0.0 && mu[1] < 1.0 && dist.lengthSquared() < 0.01;
991 // TODO : endControlPoints are undirected: calculcate
992 // correct position for it
993 throw new UnsupportedOperationException("not implemented");
997 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + bintersect);
998 // is required branch position is in possible range
999 bcp.setWorldPosition(bintersect);
1001 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);
1003 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);
1006 // branch cannot be moved into right position, new turn
1007 // / elbow must be inserted
1008 if (allowInsertRemove)
1009 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
1014 } else { // assume that control point cannot be moved, but can
1016 if (allowInsertRemove)
1017 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
1027 private static void updateDualDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1029 System.out.println("PipingRules.updateDualDirectedPipeRun() " + u + " " + lengthChange);
1031 PipeControlPoint dcp1 = u.start;
1032 PipeControlPoint dcp2 = u.end;
1033 Point3d position1 = new Point3d(u.startPoint);
1034 Point3d position2 = new Point3d(u.endPoint);
1035 Point3d position1offset = new Point3d(position1);
1036 position1offset.sub(u.offset);
1037 Point3d position2offset = new Point3d(position2);
1038 position2offset.add(u.offset);
1039 Vector3d dir1 = direction(dcp1, Direction.NEXT);
1040 Vector3d dir2 = direction(dcp2, Direction.PREVIOUS);
1041 Vector3d p1 = MathTools.closestPointOnStraight(position1offset, position2, dir2);
1042 Vector3d p2 = MathTools.closestPointOnStraight(position2offset, position1, dir1);
1043 double d1 = position1.distance(new Point3d(p1));
1044 double d2 = position2.distance(new Point3d(p2));
1046 boolean aligned = (d1 < ALLOWED_OFFSET && d2 < ALLOWED_OFFSET);
1052 } else if (allowInsertRemove){
1053 PipeControlPoint dcp;
1054 PipeControlPoint next;
1057 if (u.list.size() > 0)
1058 next = u.list.get(0);
1063 if (u.list.size() > 0)
1064 next = u.list.get(u.list.size() - 1);
1069 p1 = dcp.getWorldPosition();
1070 Vector3d v = new Vector3d();
1076 // Reserve space for 90 deg elbow
1077 double off = dcp1.getPipeRun().getTurnRadius();
1082 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position2, dir2);
1084 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position1, dir1);
1086 // By default, the elbows are placed next to each other, by using 90 deg angles.
1087 // If the distance between elbows is not enough, we must move the other elbow (and create more shallow angle elbows)
1088 if (MathTools.distance(p1, p2) < off*2.05) {
1092 PipeControlPoint tcp1 = insertElbow(dcp, next, p1);
1093 PipeControlPoint tcp2 = insertElbow(tcp1, next, p2);
1096 System.out.println("PipingRules.updateDualDirectedPipeRun() created two turns " + tcp1 + " " + tcp2);
1099 Vector3d dd = new Vector3d(p2);
1102 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1103 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1105 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1107 updatePathLegPrev(tcp2, u.updated, PathLegUpdateType.NONE);
1109 Vector3d dd = new Vector3d(p1);
1112 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1113 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1115 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1117 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.NONE);
1126 private static double spaceForTurn(PipeControlPoint tcp) {
1127 // TODO : this returns now space for 90 deg turn.
1128 // The challenge: position of tcp affects the turn angle, which then affects the required space. Perhaps we need to iterate...
1129 // Additionally, if the path legs contain offset, using just positions of opposite path leg ends is not enough,
1130 return ((TurnComponent)tcp.getPipelineComponent()).getTurnRadius();
1133 private static void insertElbowUpdate(UpdateStruct2 u, PipeControlPoint dcp, PipeControlPoint next, boolean dcpStart, Vector3d position, Vector3d directedDirection) throws Exception{
1136 // Vector3d closest = new Vector3d(position);
1137 // closest.add(directedDirection);
1139 PipeControlPoint tcp = null;
1142 closest = MathTools.closestPointOnStraight(next.getWorldPosition(), position, directedDirection);
1143 tcp = insertElbow(dcp, next, closest);
1145 closest = MathTools.closestPointOnStraight(dcp.getWorldPosition(), position, directedDirection);
1146 tcp = insertElbow(next, dcp, closest);
1148 // TODO properly calculate required distance between start and inserted elbow.
1149 double d = MathTools.distance(position, closest);
1150 double s = spaceForTurn(tcp);
1153 Vector3d p = new Vector3d(directedDirection);
1163 System.out.println("PipingRules.updateDirectedPipeRun() inserted " + tcp);
1166 // update pipe run from new turn to other end
1167 ppNoDir(tcp, new Vector3d(closest), u.list, u.end, u.endPoint, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1168 // update pipe run from directed to new turn
1169 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));
1171 // update pipe run from other end to new turn
1172 ppNoDir(u.start, u.startPoint, u.list, tcp, new Vector3d(closest), u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1173 // update pipe run from new turn to directed
1174 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));
1179 * Checks if turns can be removed (turn angle near zero)
1181 private static int checkTurns(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1183 System.out.println("PipingRules.checkTurns() " + u.start + " " + u.end);
1184 boolean startRemoved = false;
1185 boolean endRemoved = false;
1186 if (u.start.isVariableAngle()) {
1187 // this won't work properly if inline control points are not updated
1188 PipeControlPoint startPrev = u.start.getPrevious();
1189 if (startPrev != null) {
1190 double a = updateTurnControlPointTurn(u.start, null, u.dir);
1191 if (a < MIN_TURN_ANGLE && u.start.isDeletable())
1192 startRemoved = true;
1193 else if (lengthChange == PathLegUpdateType.PREV || lengthChange == PathLegUpdateType.PREV_S) {
1194 PathLegUpdateType type;
1195 if (lengthChange == PathLegUpdateType.PREV_S)
1196 type = PathLegUpdateType.PREV;
1198 type = PathLegUpdateType.NONE;
1199 updatePathLegPrev(u.start, u.start, type);
1203 if (u.end.isVariableAngle()) {
1205 PipeControlPoint endNext = u.end.getNext();
1206 if (endNext != null) {
1207 // TODO: u.end, u.dir, null
1208 double a = updateTurnControlPointTurn(u.end, null, null);
1209 if (a < MIN_TURN_ANGLE && u.end.isDeletable())
1211 else if (lengthChange == PathLegUpdateType.NEXT || lengthChange == PathLegUpdateType.NEXT_S) {
1212 PathLegUpdateType type;
1213 if (lengthChange == PathLegUpdateType.NEXT_S)
1214 type = PathLegUpdateType.NEXT;
1216 type = PathLegUpdateType.NONE;
1217 updatePathLegNext(u.end, u.end, type);
1222 System.out.println("PipingRules.checkTurns() res " + startRemoved + " " + endRemoved);
1223 if (!startRemoved && !endRemoved)
1225 if (startRemoved && endRemoved)
1228 return REMOVE_START;
1233 * Expands piperun search over turns that are going to be removed
1236 private static void expandPathLeg(UpdateStruct2 u, int type) throws Exception {
1238 System.out.println("PipingRules.expandPipeline " + u.start + " " + u.end);
1239 ArrayList<PipeControlPoint> newList = new ArrayList<PipeControlPoint>();
1242 throw new RuntimeException("Error in piping rules");
1244 u.toRemove.add(new ExpandIterInfo(u.start, REMOVE_START));
1245 u.start = u.start.findPreviousEnd();
1246 u.startPoint = u.start.getPosition();
1247 u.start.findNextEnd(newList);
1248 newList.addAll(u.list);
1252 u.toRemove.add(new ExpandIterInfo(u.end, REMOVE_END));
1253 u.end = u.end.findNextEnd(newList);
1254 u.endPoint = u.end.getPosition();
1255 u.list.addAll(newList);
1258 u.toRemove.add(new ExpandIterInfo(u.start, u.end));
1259 u.start = u.start.findPreviousEnd();
1260 u.startPoint = u.start.getPosition();
1261 u.start.findNextEnd(newList);
1262 newList.addAll(u.list);
1264 newList = new ArrayList<PipeControlPoint>();
1265 u.end = u.end.findNextEnd(newList);
1266 u.endPoint = u.end.getPosition();
1267 u.list.addAll(newList);
1270 throw new RuntimeException("Error in piping rules");
1273 u.offset = new Vector3d();
1276 for (PipeControlPoint icp : u.list) {
1277 if (icp.isOffset()) {
1278 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1279 } else if (icp.isDualSub())
1280 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1284 System.out.println("PipingRules.expandPipeline expanded " + u.start + " " + u.end);
1286 updatePathLeg(u, PathLegUpdateType.NONE);
1290 * reverts one iteration of turn removing back)
1292 private static void backIter(UpdateStruct2 u) throws Exception {
1295 System.out.println("PipingRules.backIter" + u.start + " " + u.end);
1297 throw new RuntimeException("Error in piping rules");
1298 ExpandIterInfo info = u.toRemove.get(u.toRemove.size() - 1);
1299 u.toRemove.remove(u.toRemove.size() - 1);
1300 if (info.getType() == REMOVE_START || info.getType() == REMOVE_BOTH) {
1301 while (u.list.size() > 0) {
1302 PipeControlPoint icp = u.list.get(0);
1303 if (icp.getPrevious().equals(info.getStart()))
1307 u.start = info.getStart();
1309 if (info.getType() == REMOVE_END || info.getType() == REMOVE_BOTH) {
1310 while (u.list.size() > 0) {
1311 PipeControlPoint icp = u.list.get(u.list.size() - 1);
1312 if (icp.getNext().equals(info.getEnd()))
1316 u.end = info.getEnd();
1318 u.offset = new Vector3d();
1321 for (PipeControlPoint icp : u.list) {
1322 if (icp.isOffset()) {
1323 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1324 } else if (icp.isDualSub())
1325 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1333 * Processes pipe run (removes necessary turns and updates run ends)
1335 // private static void processPathLeg(PipeControlPoint start, Point3d
1336 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1337 // end,Point3d endPoint, Vector3d dir,Vector3d offset, boolean
1338 // hasOffsets,int iter, boolean reversed, ArrayList<ExpandIterInfo>
1339 // toRemove) throws TransactionException {
1341 private static void processPathLeg(UpdateStruct2 u) throws Exception {
1343 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1344 processPathLeg(u, true, true);
1347 private static void processPathLeg(UpdateStruct2 u, boolean updateEnds, boolean updateInline) throws Exception {
1349 System.out.println("PipingRules.processPathLeg " + (updateEnds ? "ends " : "") + (updateInline ? "inline " : "") + u.start + " " + u.end);
1351 if (u.toRemove.size() > 0) {
1352 for (ExpandIterInfo info : u.toRemove) {
1353 if (info.getStart() != null) {
1355 System.out.println("PipingRules.processPathLeg removing start " + info.getStart());
1356 info.getStart()._remove();
1358 if (info.getEnd() != null) {
1360 System.out.println("PipingRules.processPathLeg removing end " + info.getEnd());
1361 info.getEnd()._remove();
1364 // ControlPointTools.removeControlPoint may remove more than one CP;
1365 // we must populate inline CP list again.
1367 u.start.findNextEnd( u.list);
1369 // FIXME : inline CPs are update twice because their positions must be
1370 // updated before and after ends.
1371 updateInlineControlPoints(u, false);
1374 if (u.start.isTurn()) {
1375 //updateTurnControlPointTurn(u.start, u.start.getPrevious(), u.start.getNext());
1376 updateTurnControlPointTurn(u.start, null, null);
1377 // updatePathLegPrev(u.start, u.start, PathLegUpdateType.NONE);
1378 } else if (u.start.isEnd()) {
1379 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1380 } else if (u.start.isInline()) {
1381 updateControlPointOrientation(u.start);
1383 if (u.end.isTurn()) {
1384 //updateTurnControlPointTurn(u.end, u.end.getPrevious(), u.end.getNext());
1385 updateTurnControlPointTurn(u.end, null, null);
1386 // updatePathLegNext(u.end, u.end, PathLegUpdateType.NONE);
1387 } else if (u.end.isEnd()) {
1388 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1389 } else if (u.end.isInline()) {
1390 updateControlPointOrientation(u.end);
1394 if (u.start.isEnd()) {
1395 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1397 if (u.end.isEnd()) {
1398 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1402 updateInlineControlPoints(u, true);
1407 * Processes pipe run and recalculates offset
1409 // private static void processPathLeg(PipeControlPoint start, Point3d
1410 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1411 // end,Point3d endPoint, Vector3d dir, boolean hasOffsets,int iter, boolean
1412 // reversed, ArrayList<ExpandIterInfo> toRemove) throws TransactionException
1414 @SuppressWarnings("unused")
1415 private static void processPathLegNoOffset(UpdateStruct2 u) throws Exception {
1417 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1418 Vector3d offset = new Vector3d();
1421 for (PipeControlPoint icp : u.list) {
1422 if (icp.isOffset()) {
1423 offset.add(icp.getSizeChangeOffsetVector(u.dir));
1424 } else if (icp.isDualSub()) {
1425 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1432 private static void updateOffsetPoint(PipeControlPoint sccp, Vector3d offset) {
1433 Vector3d world = sccp.getWorldPosition();
1435 PipeControlPoint ocp = sccp.getDualSub();
1436 ocp.setWorldPosition(world);
1440 * Updates InlineControlPoints position when straight pipe's end(s) have
1448 private static void updateInlineControlPoint(PipeControlPoint icp, Vector3d prev, Vector3d next, Vector3d dir) {
1450 System.out.println("PipingRules.updateInlineControlPoint() " + icp);
1452 Vector3d inlinePoint = icp.getWorldPosition();
1453 Vector3d prevPoint = new Vector3d(prev);
1454 Vector3d nextPoint = new Vector3d(next);
1455 if (!icp.isVariableLength()) {
1456 // Reserve space for fixed length components.
1457 MathTools.mad(prevPoint, dir, icp.getInlineLength());
1458 MathTools.mad(nextPoint, dir, -icp.getInlineLength());
1459 if (MathTools.distance(prevPoint, nextPoint) < ALLOWED_OFFSET) {
1464 boolean canCalc = MathTools.distance(prevPoint, nextPoint) > ALLOWED_OFFSET;
1466 System.out.print("InlineControlPoint update " + icp + " " + inlinePoint + " " + prevPoint + " " + nextPoint);
1467 Vector3d newInlinePoint = null;
1469 boolean branchUpdate = false;
1470 PipeControlPoint becp = null;
1471 for (PipeControlPoint pcp : icp.getChildPoints())
1472 if (pcp.isNonDirected()) {
1473 branchUpdate = true;
1478 if (DUMMY || !branchUpdate) {
1479 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1483 // FIXME : can only handle one branch
1484 PipeControlPoint p = null;
1485 if (becp.getNext() != null) {
1486 p = becp.findNextEnd();
1487 } else if (becp.getPrevious() != null) {
1488 p = becp.findPreviousEnd();
1491 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1492 } else if (canCalc){
1493 Vector3d branchLegEnd = p.getWorldPosition();
1494 Vector3d dir2 = new Vector3d(inlinePoint);
1495 dir2.sub(branchLegEnd);
1496 Vector3d dir1 = new Vector3d(nextPoint);
1497 dir1.sub(prevPoint);
1498 newInlinePoint = new Vector3d();
1499 double mu[] = new double[2];
1500 MathTools.intersectStraightStraight(new Vector3d(prevPoint), dir1, new Vector3d(branchLegEnd), dir2, newInlinePoint, new Vector3d(), mu);
1502 System.out.println(mu[0]);
1503 // FIXME : reserve space
1505 newInlinePoint = new Vector3d(prevPoint);
1506 } else if (mu[0] > 1.0) {
1507 newInlinePoint = new Vector3d(nextPoint);
1512 // prevPoint == nextPoint
1513 newInlinePoint = new Vector3d(prevPoint);
1516 System.out.println(" " + newInlinePoint);
1518 icp.setWorldPosition(newInlinePoint);
1519 updateControlPointOrientation(icp);
1523 * Updates InlineControlPoints position when straight pipe's end(s) have
1531 private static void updateEndComponentControlPoint(PipeControlPoint ecp, Vector3d start, Vector3d end) throws Exception {
1533 System.out.println("PipingRules.updateEndComponentControlPoint() " + ecp);
1534 //FIXME : end control point cannot be fixed!
1535 //if (!ecp.isFixed())
1536 updateControlPointOrientation(ecp);
1538 for (PipeControlPoint pcp : ecp.getChildPoints()) {
1539 // TODO update position
1540 updatePathLegEndControlPoint(pcp);
1544 private static void updateControlPointOrientation(PipeControlPoint pcp) {
1545 // FIXME : hack to bypass variable length components orientation
1546 // if (pcp.getAtMostOneRelatedObject(ProcessResource.g3dResource.HasWorldOrientation) == null)
1548 // if (pcp.rotationAngle == null)
1550 Double angleO = pcp.getRotationAngle();
1554 Boolean reversedO = pcp.getReversed();
1555 boolean reversed = false;
1556 if (reversedO != null)
1557 reversed = reversedO;
1558 Quat4d q = pcp.getControlPointOrientationQuat(angle, reversed);
1559 pcp.setWorldOrientation(q);
1563 * Updates all branches when branch's position has been changed
1567 private static void updateBranchControlPointBranches(PipeControlPoint bcp) throws Exception {
1569 System.out.println("PipingRules.updateBranchControlPointBranches() " + bcp);
1570 if (bcp.isDualInline())
1572 Collection<PipeControlPoint> branches = bcp.getChildPoints();
1573 if (branches.size() == 0) {
1575 System.out.println("No Branches found");
1579 for (PipeControlPoint pcp : branches) {
1580 updatePathLegEndControlPoint(pcp);
1584 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d prev, Vector3d next) {
1586 UpdateStruct2 us = createUS(tcp, Direction.NEXT, 0, new ArrayList<PipingRules.ExpandIterInfo>(), tcp);
1591 UpdateStruct2 us = createUS(tcp, Direction.PREVIOUS, 0, new ArrayList<PipingRules.ExpandIterInfo>(), tcp);
1597 if (!tcp.asFixedAngle()) {
1600 if (next == null || prev == null) {
1601 if (tcp.getTurnAngle() != null)
1602 return tcp.getTurnAngle();
1603 return Math.PI; // FIXME : argh
1605 double turnAngle = prev.angle(next);
1607 double angle = Math.PI - turnAngle;
1609 Vector3d turnAxis = new Vector3d();
1610 turnAxis.cross(prev, next);
1611 if (turnAxis.lengthSquared() > MathTools.NEAR_ZERO) {
1612 double elbowRadius = ((TurnComponent)tcp.getPipelineComponent()).getTurnRadius();
1613 double R = elbowRadius / Math.tan(angle * 0.5);
1615 turnAxis.normalize();
1616 tcp.setTurnAngle(turnAngle);
1617 tcp.setLength(R);// setComponentOffsetValue(R);
1618 tcp.setTurnAxis(turnAxis);
1619 // tcp.setPosition(tcp.getPosition());
1622 tcp.setTurnAngle(0.0);
1624 tcp.setTurnAxis(new Vector3d(MathTools.Y_AXIS));
1627 updateControlPointOrientation(tcp);
1630 System.out.println("PipingTools.updateTurnControlPointTurn " + prev + " " + next + " " + turnAngle + " " + turnAxis);
1634 if (prev != null && next != null) {
1636 } else if (prev == null) {
1637 if (!tcp._getReversed())
1638 tcp.setReversed(true);
1639 } else if (next == null) {
1640 if (tcp._getReversed())
1641 tcp.setReversed(false);
1644 Vector3d dir = null;
1645 if (!tcp._getReversed()) {
1652 return Math.PI; // FIXME : argh
1655 Quat4d q = PipeControlPoint.getControlPointOrientationQuat(dir, tcp.getRotationAngle() != null ? tcp.getRotationAngle() : 0.0);
1656 Vector3d v = new Vector3d();
1657 MathTools.rotate(q, MathTools.Y_AXIS,v);
1659 tcp.setWorldOrientation(q);
1660 if (tcp.getTurnAngle() != null)
1661 return tcp.getTurnAngle();
1662 return Math.PI; // FIXME : argh
1668 public static List<PipeControlPoint> getControlPoints(PipeRun pipeRun) {
1669 List<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
1670 if (pipeRun.getControlPoints().size() == 0)
1672 PipeControlPoint pcp = pipeRun.getControlPoints().iterator().next();
1673 while (pcp.getPrevious() != null) {
1674 PipeControlPoint prev = pcp.getPrevious();
1675 if (prev.getPipeRun() != pipeRun && prev.getPipeRun() != null) { // bypass possible corruption
1680 if (pcp.isDualSub()) {
1681 pcp = pcp.getParentPoint();
1684 while (pcp.getNext() != null) {
1685 pcp = pcp.getNext();
1686 if (pcp.getPipeRun() != pipeRun)
1693 public static void reverse(PipeRun pipeRun) {
1696 List<PipeControlPoint> points = getControlPoints(pipeRun);
1697 PipeControlPoint pcp = points.get(0);
1698 if (pcp.isSizeChange() && pcp.getChildPoints().size() > 0) {
1699 pipeRun = pcp.getPipeRun();
1704 List<PipeRun> all = new ArrayList<PipeRun>();
1705 List<List<PipeControlPoint>> pcps = new ArrayList<List<PipeControlPoint>>();
1708 List<PipeControlPoint> points = getControlPoints(pipeRun);
1710 PipeControlPoint pcp = points.get(points.size()-1);
1711 if (pcp.getChildPoints().size() > 0) {
1712 PipeRun pipeRun2 = pcp.getChildPoints().get(0).getPipeRun();
1713 if (pipeRun == pipeRun2)
1721 for (int i = 0 ; i < all.size(); i++) {
1722 List<PipeControlPoint> list = pcps.get(i);
1725 for (int i = 0 ; i < all.size(); i++) {
1726 boolean last = i == all.size() - 1;
1727 List<PipeControlPoint> list = pcps.get(i);
1730 List<PipeControlPoint> list2 = pcps.get(i+1);
1731 PipeControlPoint prev = list.get(list.size()-1);
1732 PipeControlPoint next = list2.get(0);
1733 System.out.println();
1735 // Reverse the component on the boundary.
1736 InlineComponent ic = (InlineComponent)prev.getPipelineComponent();
1737 PipeRun r1 = ic.getPipeRun();
1738 PipeRun r2 = ic.getAlternativePipeRun();
1739 if (r1 == null || r2 == null)
1740 throw new RuntimeException("Components on PipeRun changes should refer to bot PipeRuns");
1744 ic.setAlternativePipeRun(r1);
1746 throw new RuntimeException("PipeRun changes should contain shared control points");
1754 private static void _reverse(List<PipeControlPoint> list) {
1755 if (list.size() <= 1)
1756 return; // nothing to do.
1758 for (int i = 0 ; i < list.size(); i++) {
1759 boolean first = i == 0;
1760 boolean last = i == list.size() - 1;
1761 PipeControlPoint current = list.get(i);
1762 PipeControlPoint currentSub = null;
1763 if (current.isDualInline())
1764 currentSub = current.getDualSub();
1766 PipeControlPoint next = list.get(i+1);
1767 if (next.isDualInline())
1768 next = next.getDualSub();
1769 if (current.getNext() == next)
1770 current.setNext(null);
1771 current.setPrevious(next);
1772 if (currentSub != null) {
1773 if (currentSub.getNext() == next)
1774 currentSub.setNext(null);
1775 currentSub.setPrevious(next);
1778 PipeControlPoint prev = list.get(i-1);
1780 if (current.getPrevious() == prev)
1781 current.setPrevious(null);
1782 current.setNext(prev);
1784 if (currentSub != null) {
1785 if (currentSub.getPrevious() == prev)
1786 currentSub.setPrevious(null);
1787 currentSub.setNext(prev);
1790 PipeControlPoint prev = list.get(i-1);
1791 PipeControlPoint next = list.get(i+1);
1792 if (next.isDualInline())
1793 next = next.getDualSub();
1796 current.setPrevious(next);
1797 current.setNext(prev);
1799 if (currentSub != null) {
1800 currentSub.setPrevious(next);
1801 currentSub.setNext(prev);
1805 //if (current.isTurn() && current.isFixed()) {
1806 if (current.asFixedAngle()) {
1807 current.setReversed(!current._getReversed());
1809 if (current.isInline() && current.isReverse()) {
1810 current.setReversed(!current._getReversed());
1817 public static void validate(PipeRun pipeRun) {
1818 if (pipeRun == null)
1820 Collection<PipeControlPoint> pcps = pipeRun.getControlPoints();
1822 //System.out.println("Validate " + pipeRun.getName());
1823 for (PipeControlPoint pcp : pcps) {
1824 if (pcp.getParentPoint() == null || pcp.getParentPoint().getPipeRun() != pipeRun)
1827 List<PipeControlPoint> runPcps = getControlPoints(pipeRun);
1828 if (runPcps.size() != count) {
1829 System.out.println("Run " + pipeRun.getName() + " contains unconnected control points, found " + runPcps.size() + " connected, " + pcps.size() + " total.");
1830 for (PipeControlPoint pcp : pcps) {
1831 if (!runPcps.contains(pcp)) {
1832 System.out.println("Unconnected " + pcp + " " + pcp.getPipelineComponent());
1836 for (PipeControlPoint pcp : pcps) {
1837 if (pcp.getPipeRun() == null) {
1838 System.out.println("PipeRun ref missing " + pcp + " " + pcp.getPipelineComponent());
1840 if (!pcp.isDirected() && pcp.getNext() == null && pcp.getPrevious() == null)
1841 System.out.println("Orphan undirected " + pcp + " " + pcp.getPipelineComponent());
1843 for (PipeControlPoint pcp : pcps) {
1844 if (pcp.getParentPoint() == null) {
1845 PipeControlPoint sub = null;
1846 if (pcp.isDualInline())
1847 sub = pcp.getDualSub();
1848 PipeControlPoint next = pcp.getNext();
1849 PipeControlPoint prev = pcp.getPrevious();
1851 if (!(next.getPrevious() == pcp || next.getPrevious() == sub)) {
1852 System.out.println("Inconsistency between " + pcp + " -> " +next );
1856 PipeControlPoint prevParent = null;
1857 if (prev.isDualSub()) {
1858 prevParent = prev.getParentPoint();
1859 } else if (prev.isDualInline()) {
1860 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1862 if (!(prev.getNext() == pcp && (prevParent == null || prevParent.getNext() == pcp))) {
1863 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1870 public static void splitVariableLengthComponent(PipelineComponent newComponent, InlineComponent splittingComponent, boolean assignPos) throws Exception{
1871 assert(!splittingComponent.getControlPoint().isFixedLength());
1872 assert(!(newComponent instanceof InlineComponent && !newComponent.getControlPoint().isFixedLength()));
1873 PipeControlPoint newCP = newComponent.getControlPoint();
1874 PipeControlPoint splittingCP = splittingComponent.getControlPoint();
1875 PipeControlPoint nextCP = splittingCP.getNext();
1876 PipeControlPoint prevCP = splittingCP.getPrevious();
1878 /* there are many different cases to insert new component when
1879 it splits existing VariableLengthinlineComponent.
1881 1. VariableLengthComponet is connected from both sides:
1882 - insert new component between VariableLength component and component connected to it
1883 - insert new VariableLengthComponent between inserted component and component selected in previous step
1885 2. VariableLengthComponent is connected from one side
1886 - Use previous case or:
1887 - Insert new component to empty end
1888 - Insert new VariableLength component to inserted components empty end
1890 3. VariableLength is not connected to any component.
1891 - Should not be possible, at least in current implementation.
1892 - Could be done using second case
1896 if (nextCP == null && prevCP == null) {
1897 // this should not be possible
1898 throw new RuntimeException("VariableLengthComponent " + splittingComponent + " is not connected to anything.");
1900 double newLength = newComponent.getControlPoint().getLength();
1903 Point3d next = new Point3d();
1904 Point3d prev = new Point3d();
1905 splittingCP.getInlineControlPointEnds(prev, next);
1907 Vector3d newPos = null;
1909 newPos = new Vector3d(prev);
1910 Vector3d dir = new Vector3d(next);
1914 newComponent.setWorldPosition(newPos);
1916 newPos = newComponent.getWorldPosition();
1921 Vector3d dir = new Vector3d(next);
1924 dir.scale(newLength * 0.5);
1925 Point3d vn = new Point3d(newPos);
1926 Point3d vp = new Point3d(newPos);
1929 double ln = vn.distance(next);
1930 double lp = vp.distance(prev);
1931 vp.interpolate(prev, 0.5);
1932 vn.interpolate(next, 0.5);
1935 if (nextCP == null) {
1936 newCP.insert(splittingCP, Direction.NEXT);
1937 insertStraight(newCP, Direction.NEXT, new Vector3d(vn), ln);
1938 splittingCP.setWorldPosition(new Vector3d(vp));
1939 // ControlPointTools.setWorldPosition(splittingCP, vp);
1940 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1941 } else if (prevCP == null) {
1942 newCP.insert(splittingCP, Direction.PREVIOUS);
1943 insertStraight(newCP, Direction.PREVIOUS, new Vector3d(vp), lp);
1944 splittingCP.setWorldPosition(new Vector3d(vn));
1945 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, ln);
1947 newCP.insert(splittingCP, nextCP);
1948 insertStraight(newCP, nextCP, new Vector3d(vn), ln);
1949 splittingCP.setWorldPosition(new Vector3d(vp));
1950 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1952 positionUpdate(newCP);
1956 public static void addSizeChange(boolean reversed, PipeRun pipeRun, PipeRun other, InlineComponent reducer, PipeControlPoint previous, PipeControlPoint next) {
1957 PipeControlPoint pcp = reducer.getControlPoint();
1958 PipeControlPoint ocp = pcp.getDualSub();
1960 String name = pipeRun.getUniqueName("Reducer");
1961 reducer.setName(name);
1962 pipeRun.addChild(reducer);
1963 other.addChild(ocp);
1964 reducer.setAlternativePipeRun(other);
1966 previous.setNext(pcp);
1967 pcp.setPrevious(previous);
1968 ocp.setPrevious(previous);
1972 next.setPrevious(ocp);
1975 String name = other.getUniqueName("Reducer");
1976 reducer.setName(name);
1977 other.addChild(reducer);
1978 pipeRun.addChild(ocp);
1979 reducer.setAlternativePipeRun(pipeRun);
1983 pcp.setPrevious(next);
1984 ocp.setPrevious(next);
1986 pcp.setNext(previous);
1987 ocp.setNext(previous);
1988 previous.setPrevious(ocp);