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.getSubPoint().get(0)) {
222 pcp1 = pcp1.getSubPoint().get(0);
223 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
224 PipeControlPoint t = pcp1;
227 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getSubPoint().get(0) && pcp2.getNext() == pcp1) {
228 PipeControlPoint t = pcp1;
229 pcp1 = pcp2.getSubPoint().get(0);
232 throw new RuntimeException();
234 TurnComponent elbow = ComponentUtils.createTurn((P3DRootNode)pcp1.getRootNode());
235 PipeControlPoint pcp = elbow.getControlPoint();
236 if (pcp1.isDualInline())
237 pcp1 = pcp1.getSubPoint().get(0);
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.getSubPoint().get(0)) {
255 pcp1 = pcp1.getSubPoint().get(0);
256 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
257 PipeControlPoint t = pcp1;
260 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getSubPoint().get(0) && pcp2.getNext() == pcp1) {
261 PipeControlPoint t = pcp1;
262 pcp1 = pcp2.getSubPoint().get(0);
265 throw new RuntimeException();
267 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp1.getRootNode());
268 PipeControlPoint scp = component.getControlPoint();
269 if (pcp1.isDualInline())
270 pcp1 = pcp1.getSubPoint().get(0);
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.getSubPoint().get(0);
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 private static boolean calculateOffset(Vector3d startPoint, Vector3d endPoint, ArrayList<PipeControlPoint> list, Vector3d dir, Vector3d offset) {
379 boolean hasOffsets = false;
380 List<PipeControlPoint> offsets = new ArrayList<PipeControlPoint>(list.size());
381 for (PipeControlPoint icp : list) {
382 if (icp.isOffset()) {
384 } else if (icp.isDualSub())
385 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
387 if (offsets.size() == 0) {
390 double l = dir.lengthSquared();
391 if (l > MathTools.NEAR_ZERO)
392 dir.scale(1.0/Math.sqrt(l));
393 offset.set(0.0, 0.0, 0.0);
396 Vector3d sp = new Vector3d(startPoint);
397 Point3d ep = new Point3d(endPoint);
400 double l = dir.lengthSquared();
401 if (l > MathTools.NEAR_ZERO)
402 dir.scale(1.0/Math.sqrt(l));
406 offset.set(0.0, 0.0, 0.0);
408 for (PipeControlPoint icp : offsets) {
409 Vector3d v = icp.getSizeChangeOffsetVector(dir);
412 Point3d nep = new Point3d(endPoint);
414 if (nep.distance(ep) < 0.0000000001) {
420 l = dir.lengthSquared();
421 if (l > MathTools.NEAR_ZERO)
422 dir.scale(1.0/Math.sqrt(l));
427 if (DEBUG && hasOffsets)
428 System.out.println("calcOffset s:"+ startPoint + " e:" + endPoint + " d:" + dir + " o:"+offset) ;
432 private static UpdateStruct2 createUS(PipeControlPoint start, Direction direction, int iter, ArrayList<ExpandIterInfo> toRemove, PipeControlPoint updated) {
433 ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
434 PipeControlPoint end = null;
435 if (direction == Direction.NEXT) {
436 end = start.findNextEnd(list);
438 ArrayList<PipeControlPoint> prevList = new ArrayList<PipeControlPoint>();
439 PipeControlPoint tend = start.findPreviousEnd(prevList);
440 for (PipeControlPoint icp : prevList) {
441 if (icp.isDualSub()) {
442 list.add(0, icp.getParentPoint());
452 boolean hasOffsets = false;
453 Vector3d offset = new Vector3d();
454 Vector3d startPoint = start.getWorldPosition();
455 Vector3d endPoint = end.getWorldPosition();
456 Vector3d dir = new Vector3d();
457 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
458 return new UpdateStruct2(start, startPoint, list, end, endPoint, dir, offset, hasOffsets, iter, direction == Direction.PREVIOUS, toRemove, updated);
461 private static boolean asDirected(PipeControlPoint pcp, Direction direction) {
462 if (pcp.isDirected())
464 if (pcp.asFixedAngle()) {
465 if (!pcp._getReversed())
466 return direction == Direction.NEXT;
468 return direction == Direction.PREVIOUS;
473 private static Vector3d direction(PipeControlPoint pcp, Direction direction) {
474 return pcp.getDirection(direction);
477 private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
479 if (asDirected(u.start, Direction.NEXT))
481 if (asDirected(u.end, Direction.PREVIOUS))
485 updateFreePathLeg(u, lengthChange);
488 updateDirectedPathLeg(u, lengthChange);
491 updateDualDirectedPathLeg(u, lengthChange);
497 private static void updateFreePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
499 System.out.println("PipingRules.updateFreePipeRun " + u + " " + lengthChange);
500 checkExpandPathLeg(u, lengthChange);
501 if (u.start.isInline() || u.end.isInline() || u.start.asFixedAngle()|| u.end.asFixedAngle())
502 processPathLeg(u, true, false);
505 private static void updateInlineControlPoints(UpdateStruct2 u, boolean checkSizes) throws Exception{
507 System.out.println("PipingRules.updateInlineControlPoints() " + u);
509 Vector3d start = new Vector3d(u.startPoint);
510 Vector3d end = new Vector3d(u.endPoint);
513 // create offsets for leg ends.
514 MathTools.mad(start, u.dir, u.start.getInlineLength());
515 MathTools.mad(end, u.dir, -u.end.getInlineLength());
518 boolean recalcline = false;
522 for (PipeControlPoint icp : u.list) {
523 updateInlineControlPoint(icp, start, end, u.dir);
525 if (icp.isOffset()) {
526 // TODO : offset vector is already calculated and should be cached
527 Vector3d off = icp.getSizeChangeOffsetVector(u.dir);
528 updateOffsetPoint(icp, off);
534 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
535 pathLegPoints.add(u.start);
536 for (PipeControlPoint icp : u.list) {
537 // updateInlineControlPoint(icp, u.startPoint,
538 // u.endPoint,u.dir);
539 updateBranchControlPointBranches(icp);
540 pathLegPoints.add(icp);
542 pathLegPoints.add(u.end);
544 // TODO : values can be cached in the loop
545 for (int i = 0; i < pathLegPoints.size(); i++) {
546 PipeControlPoint icp = pathLegPoints.get(i);
548 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
549 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
551 if (icp.isVariableLength()) {
552 if (prev != null && next != null) {
554 recalcline = recalcline | updateVariableLength(icp, prev, next);
557 // this is variable length component at the end of the
559 // the problem is that we want to keep unconnected end
560 // of the component in the same
561 // place, but center of the component must be moved.
562 updateVariableLengthEnd(icp, prev != null ? prev : next);
566 } else if (prev != null && !prev.isVariableLength()) {
567 // If this and previous control point are not variable
568 // length pcps, we'll have to check if there is no empty
569 // space between them.
570 // I there is, we'll have to create new variable length
571 // component between them.
572 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
575 } else { // with offset
576 Vector3d sp = new Vector3d(start);
577 Vector3d ep = new Vector3d(end);
580 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
581 pathLegPoints.add(u.start);
583 for (PipeControlPoint icp : u.list) {
584 updateInlineControlPoint(icp, sp, ep, u.dir);
585 updateBranchControlPointBranches(icp);
586 pathLegPoints.add(icp);
587 if (icp.isOffset()) {
588 // TODO : offset vector is already calculated and should be
590 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
591 updateOffsetPoint(icp, offset);
596 pathLegPoints.add(u.end);
601 sp = new Vector3d(u.startPoint);
602 ep = new Vector3d(u.endPoint);
605 for (int i = 0; i < pathLegPoints.size(); i++) {
606 PipeControlPoint icp = pathLegPoints.get(i);
608 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
609 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
611 if (prev != null && prev.isDualInline())
612 prev = prev.getSubPoint().get(0);
615 if (icp.isVariableLength()) {
616 if (prev != null && next != null) {
617 recalcline = recalcline | updateVariableLength(icp, prev, next);
620 // this is variable length component at the end of the
622 // the problem is that we want to keep unconnected end
623 // of the component in the same
624 // place, but center of the component must be moved.
625 updateVariableLengthEnd(icp, prev != null ? prev : next);
627 } else if (prev != null && !prev.isVariableLength()) {
628 // If this and previous control point are not variable
629 // length pcps, we'll have to check if there is no empty
630 // space between them.
631 // I there is, we'll have to create new variable length
632 // component between them.
633 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
635 if (icp.isOffset()) {
636 // TODO : offset vector is already calculated and should be
638 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
646 u.start.findNextEnd(u.list);
650 private static boolean updateVariableLength(PipeControlPoint icp, PipeControlPoint prev, PipeControlPoint next) {
651 Vector3d prevPos = prev.getWorldPosition();
652 Vector3d nextPos = next.getWorldPosition();
654 Vector3d dir = new Vector3d(nextPos);
656 double l = dir.lengthSquared(); // distance between
659 double l2prev = prev.getInlineLength(); // distance
663 double l2next = next.getInlineLength();
664 double l2 = l2prev + l2next;
665 double l2s = MathTools.square(l2);
666 if (l2s < l) { // check if there is enough space for
667 // variable length component.
670 double length = Math.sqrt(l) - l2; // true length of
674 dir.scale(length * 0.5 + l2prev); // calculate
679 icp.setWorldPosition(dir);
680 icp.setLength(length);
683 // components leave no space to the component and it
686 if (icp.isDeletable()) {
687 if (!allowInsertRemove) {
688 icp.setLength(0.0001);
693 System.out.println("PipingRules.updateVariableLength removing " + icp);
701 private static boolean possibleVaribleLengthInsert(PipeControlPoint icp, PipeControlPoint prev) throws Exception{
702 Vector3d currentPos = icp.getWorldPosition();
703 Vector3d prevPos = prev.getWorldPosition();
704 Vector3d dir = new Vector3d(currentPos);
706 double l = dir.lengthSquared();
707 double l2prev = prev.getInlineLength();
708 double l2next = icp.getInlineLength();
709 double l2 = l2prev + l2next;
710 double l2s = l2 * l2;
712 if (allowInsertRemove) {
714 double length = Math.sqrt(l) - l2; // true length of the
717 dir.scale(length * 0.5 + l2prev); // calculate center
721 PipeControlPoint scp = insertStraight(prev, icp, dir, length);
730 private static void updateVariableLengthEnd(PipeControlPoint icp, PipeControlPoint prev) {
731 Vector3d currentPos = icp.getWorldPosition();
732 Vector3d prevPos = prev.getWorldPosition();
734 Vector3d dir = new Vector3d();
735 dir.sub(currentPos, prevPos);
737 boolean simple = currentUpdates.contains(icp);
739 // Update based on position -> adjust length
740 double currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
741 icp.setLength(currentLength);
743 // Update based on neighbour movement -> adjust length and position, so that free end stays in place.
744 double currentLength = icp.getLength();
745 if (currentLength < MathTools.NEAR_ZERO) {
746 currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
749 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
751 Point3d endPos = new Point3d(dir);
752 endPos.scale(currentLength * 0.5);
753 endPos.add(currentPos); // this is the free end of the component
755 double offset = prev.getInlineLength();
756 Point3d beginPos = new Point3d(dir);
757 beginPos.scale(offset);
758 beginPos.add(prevPos); // this is the connected end of the component
760 double l = beginPos.distance(endPos);
763 System.out.println("Length for " + icp + " is NaN");
766 beginPos.add(dir); // center position
769 System.out.println("PipingRules.updateInlineControlPoints() setting variable length to " + l);
772 icp.setWorldPosition(new Vector3d(beginPos));
776 private static void ppNoOffset(UpdateStruct2 u) throws Exception {
778 System.out.println("PipingRules.ppNoOffset() " + u);
779 Vector3d offset = new Vector3d();
782 for (PipeControlPoint icp : u.list) {
783 if (icp.isOffset()) {
784 offset.add(icp.getSizeChangeOffsetVector(u.dir));
785 } else if (icp.isDualSub())
786 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
790 checkExpandPathLeg(u, PathLegUpdateType.NONE);
793 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 {
795 System.out.println("PipingRules.ppNoDir() " + start + " " + end + " " + iter + " " + toRemove.size());
796 // FIXME : extra loop (dir should be calculated here)
797 Vector3d dir = new Vector3d();
798 Vector3d offset = new Vector3d();
799 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
800 ppNoOffset(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, null, hasOffsets, iter, reversed, toRemove, updated));
803 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
804 checkExpandPathLeg(u, lengthChange, u.updated.isInline() && u.updated.isOffset());
807 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange, boolean updateEnds) throws Exception {
809 System.out.println("PipingRules.checkExpandPathLeg() " + u + " " + lengthChange);
810 if (lengthChange != PathLegUpdateType.NONE) {
811 // FIXME : turns cannot be checked before inline cps are updated,
812 // since their position affects calculation of turns
813 processPathLeg(u, updateEnds, false);
814 int type = checkTurns(u, lengthChange);
815 if (type == REMOVE_NONE) {
816 processPathLeg(u, updateEnds, true);
818 expandPathLeg(u, type);
821 processPathLeg(u, updateEnds, true);
825 private static void updateDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
827 System.out.println("PipingRules.updateDirectedPipeRun() " + u + " " + lengthChange);
828 PipeControlPoint dcp;
829 PipeControlPoint other;
830 boolean canMoveOther = false;
831 boolean dcpStart = false;
832 boolean inlineEnd = false;
834 if (asDirected(u.start, Direction.NEXT)) {
837 position = u.startPoint;
841 inlineEnd = u.end.isInline();
846 position = u.endPoint;
849 inlineEnd = u.start.isInline();
852 Vector3d directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
853 if (directedDirection == null) {
854 //updateTurnControlPointTurn(dcp, dcp.getPrevious(), dcp.getNext());
855 updateTurnControlPointTurn(dcp, null, null);
856 directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
857 if (directedDirection == null) {
861 Point3d directedEndPoint = new Point3d(u.endPoint);
863 directedEndPoint.add(u.offset);
865 double mu[] = new double[2];
868 Vector3d t = new Vector3d();
871 closest = MathTools.closestPointOnStraight(directedEndPoint, u.startPoint, directedDirection, mu);
872 t.sub(closest, directedEndPoint);
874 closest = MathTools.closestPointOnStraight(u.startPoint, directedEndPoint, directedDirection, mu);
875 t.sub(closest, u.startPoint);
878 double distance = t.length();
879 boolean aligned = (distance < ALLOWED_OFFSET);
881 if (u.start.isInline() || u.end.isInline() || u.start.asFixedAngle() || u.end.asFixedAngle())
882 processPathLeg(u, true, false);
883 checkExpandPathLeg(u, lengthChange, inlineEnd);
889 PipeControlPoint nextToMoved;
891 if (u.list.size() > 0)
893 nextToMoved = u.list.get(0);
895 nextToMoved = u.list.get(u.list.size() - 1);
899 nextToMoved = u.start;
900 if (other.isVariableAngle()) {
902 // TODO calculate needed space from next run end.
903 double space = spaceForTurn(other);
906 closest.set(u.startPoint);
908 closest.set(u.endPoint);
910 Vector3d v = new Vector3d(directedDirection);
917 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + closest);
918 other.setWorldPosition(closest);
920 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));
921 if (u.end.getNext() != null)
922 updatePathLegNext(u.end, u.updated, PathLegUpdateType.NEXT);
924 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));
925 if (u.start.getPrevious() != null)
926 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.PREV);
929 // TODO : calculate needed space from next run end.
930 if (allowInsertRemove)
931 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
936 } else if (other.isNonDirected() && other.getParentPoint() != null) {
937 // FIXME : this code was for updating branches
938 Vector3d bintersect = new Vector3d();
939 PipeControlPoint bcp = other.getParentPoint();
940 if (bcp != null && canMoveOther) {
941 Point3d bstart = new Point3d();
942 Point3d bend = new Point3d();
943 Vector3d bdir = new Vector3d();
944 bcp.getInlineControlPointEnds(bstart, bend, bdir);
945 Vector3d nintersect = new Vector3d();
947 MathTools.intersectStraightStraight(position, directedDirection, bend, bdir, nintersect, bintersect, mu);
948 Vector3d dist = new Vector3d(nintersect);
949 dist.sub(bintersect);
950 canMoveOther = mu[1] > 0.0 && mu[1] < 1.0 && dist.lengthSquared() < 0.01;
952 // TODO : endControlPoints are undirected: calculcate
953 // correct position for it
954 throw new UnsupportedOperationException("not implemented");
958 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + bintersect);
959 // is required branch position is in possible range
960 bcp.setWorldPosition(bintersect);
962 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);
964 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);
967 // branch cannot be moved into right position, new turn
968 // / elbow must be inserted
969 if (allowInsertRemove)
970 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
975 } else { // assume that control point cannot be moved, but can
977 if (allowInsertRemove)
978 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
988 private static void updateDualDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
990 System.out.println("PipingRules.updateDualDirectedPipeRun() " + u + " " + lengthChange);
992 PipeControlPoint dcp1 = u.start;
993 PipeControlPoint dcp2 = u.end;
994 Point3d position1 = new Point3d(u.startPoint);
995 Point3d position2 = new Point3d(u.endPoint);
996 Point3d position1offset = new Point3d(position1);
997 position1offset.sub(u.offset);
998 Point3d position2offset = new Point3d(position2);
999 position2offset.add(u.offset);
1000 Vector3d dir1 = direction(dcp1, Direction.NEXT);
1001 Vector3d dir2 = direction(dcp2, Direction.PREVIOUS);
1002 Vector3d p1 = MathTools.closestPointOnStraight(position1offset, position2, dir2);
1003 Vector3d p2 = MathTools.closestPointOnStraight(position2offset, position1, dir1);
1004 double d1 = position1.distance(new Point3d(p1));
1005 double d2 = position2.distance(new Point3d(p2));
1007 boolean aligned = (d1 < ALLOWED_OFFSET && d2 < ALLOWED_OFFSET);
1013 } else if (allowInsertRemove){
1014 PipeControlPoint dcp;
1015 PipeControlPoint next;
1018 if (u.list.size() > 0)
1019 next = u.list.get(0);
1024 if (u.list.size() > 0)
1025 next = u.list.get(u.list.size() - 1);
1030 p1 = dcp.getWorldPosition();
1031 Vector3d v = new Vector3d();
1037 // Reserve space for 90 deg elbow
1038 double off = dcp1.getPipeRun().getTurnRadius();
1043 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position2, dir2);
1045 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position1, dir1);
1047 // By default, the elbows are placed next to each other, by using 90 deg angles.
1048 // If the distance between elbows is not enough, we must move the other elbow (and create more shallow angle elbows)
1049 if (MathTools.distance(p1, p2) < off*2.05) {
1053 PipeControlPoint tcp1 = insertElbow(dcp, next, p1);
1054 PipeControlPoint tcp2 = insertElbow(tcp1, next, p2);
1057 System.out.println("PipingRules.updateDualDirectedPipeRun() created two turns " + tcp1 + " " + tcp2);
1060 Vector3d dd = new Vector3d(p2);
1063 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1064 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1066 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1068 updatePathLegPrev(tcp2, u.updated, PathLegUpdateType.NONE);
1070 Vector3d dd = new Vector3d(p1);
1073 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1074 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1076 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1078 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.NONE);
1087 private static double spaceForTurn(PipeControlPoint tcp) {
1088 // TODO : this returns now space for 90 deg turn.
1089 // The challenge: position of tcp affects the turn angle, which then affects the required space. Perhaps we need to iterate...
1090 // Additionally, if the path legs contain offset, using just positions of opposite path leg ends is not enough,
1091 return ((TurnComponent)tcp.getPipelineComponent()).getTurnRadius();
1094 private static void insertElbowUpdate(UpdateStruct2 u, PipeControlPoint dcp, PipeControlPoint next, boolean dcpStart, Vector3d position, Vector3d directedDirection) throws Exception{
1097 // Vector3d closest = new Vector3d(position);
1098 // closest.add(directedDirection);
1100 PipeControlPoint tcp = null;
1103 closest = MathTools.closestPointOnStraight(next.getWorldPosition(), position, directedDirection);
1104 tcp = insertElbow(dcp, next, closest);
1106 closest = MathTools.closestPointOnStraight(dcp.getWorldPosition(), position, directedDirection);
1107 tcp = insertElbow(next, dcp, closest);
1109 // TODO properly calculate required distance between start and inserted elbow.
1110 double d = MathTools.distance(position, closest);
1111 double s = spaceForTurn(tcp);
1114 Vector3d p = new Vector3d(directedDirection);
1124 System.out.println("PipingRules.updateDirectedPipeRun() inserted " + tcp);
1127 // update pipe run from new turn to other end
1128 ppNoDir(tcp, new Vector3d(closest), u.list, u.end, u.endPoint, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1129 // update pipe run from directed to new turn
1130 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));
1132 // update pipe run from other end to new turn
1133 ppNoDir(u.start, u.startPoint, u.list, tcp, new Vector3d(closest), u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1134 // update pipe run from new turn to directed
1135 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));
1140 * Checks if turns can be removed (turn angle near zero)
1142 private static int checkTurns(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1144 System.out.println("PipingRules.checkTurns() " + u.start + " " + u.end);
1145 boolean startRemoved = false;
1146 boolean endRemoved = false;
1147 if (u.start.isVariableAngle()) {
1148 // this won't work properly if inline control points are not updated
1149 PipeControlPoint startPrev = u.start.getPrevious();
1150 if (startPrev != null) {
1151 double a = updateTurnControlPointTurn(u.start, null, u.dir);
1152 if (a < MIN_TURN_ANGLE && u.start.isDeletable())
1153 startRemoved = true;
1154 else if (lengthChange == PathLegUpdateType.PREV || lengthChange == PathLegUpdateType.PREV_S) {
1155 PathLegUpdateType type;
1156 if (lengthChange == PathLegUpdateType.PREV_S)
1157 type = PathLegUpdateType.PREV;
1159 type = PathLegUpdateType.NONE;
1160 updatePathLegPrev(u.start, u.start, type);
1164 if (u.end.isVariableAngle()) {
1166 PipeControlPoint endNext = u.end.getNext();
1167 if (endNext != null) {
1168 // TODO: u.end, u.dir, null
1169 double a = updateTurnControlPointTurn(u.end, null, null);
1170 if (a < MIN_TURN_ANGLE && u.end.isDeletable())
1172 else if (lengthChange == PathLegUpdateType.NEXT || lengthChange == PathLegUpdateType.NEXT_S) {
1173 PathLegUpdateType type;
1174 if (lengthChange == PathLegUpdateType.NEXT_S)
1175 type = PathLegUpdateType.NEXT;
1177 type = PathLegUpdateType.NONE;
1178 updatePathLegNext(u.end, u.end, type);
1183 System.out.println("PipingRules.checkTurns() res " + startRemoved + " " + endRemoved);
1184 if (!startRemoved && !endRemoved)
1186 if (startRemoved && endRemoved)
1189 return REMOVE_START;
1194 * Expands piperun search over turns that are going to be removed
1197 private static void expandPathLeg(UpdateStruct2 u, int type) throws Exception {
1199 System.out.println("PipingRules.expandPipeline " + u.start + " " + u.end);
1200 ArrayList<PipeControlPoint> newList = new ArrayList<PipeControlPoint>();
1203 throw new RuntimeException("Error in piping rules");
1205 u.toRemove.add(new ExpandIterInfo(u.start, REMOVE_START));
1206 u.start = u.start.findPreviousEnd();
1207 u.startPoint = u.start.getPosition();
1208 u.start.findNextEnd(newList);
1209 newList.addAll(u.list);
1213 u.toRemove.add(new ExpandIterInfo(u.end, REMOVE_END));
1214 u.end = u.end.findNextEnd(newList);
1215 u.endPoint = u.end.getPosition();
1216 u.list.addAll(newList);
1219 u.toRemove.add(new ExpandIterInfo(u.start, u.end));
1220 u.start = u.start.findPreviousEnd();
1221 u.startPoint = u.start.getPosition();
1222 u.start.findNextEnd(newList);
1223 newList.addAll(u.list);
1225 newList = new ArrayList<PipeControlPoint>();
1226 u.end = u.end.findNextEnd(newList);
1227 u.endPoint = u.end.getPosition();
1228 u.list.addAll(newList);
1231 throw new RuntimeException("Error in piping rules");
1234 u.offset = new Vector3d();
1237 for (PipeControlPoint icp : u.list) {
1238 if (icp.isOffset()) {
1239 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1240 } else if (icp.isDualSub())
1241 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1245 System.out.println("PipingRules.expandPipeline expanded " + u.start + " " + u.end);
1247 updatePathLeg(u, PathLegUpdateType.NONE);
1251 * reverts one iteration of turn removing back)
1253 private static void backIter(UpdateStruct2 u) throws Exception {
1256 System.out.println("PipingRules.backIter" + u.start + " " + u.end);
1258 throw new RuntimeException("Error in piping rules");
1259 ExpandIterInfo info = u.toRemove.get(u.toRemove.size() - 1);
1260 u.toRemove.remove(u.toRemove.size() - 1);
1261 if (info.getType() == REMOVE_START || info.getType() == REMOVE_BOTH) {
1262 while (u.list.size() > 0) {
1263 PipeControlPoint icp = u.list.get(0);
1264 if (icp.getPrevious().equals(info.getStart()))
1268 u.start = info.getStart();
1270 if (info.getType() == REMOVE_END || info.getType() == REMOVE_BOTH) {
1271 while (u.list.size() > 0) {
1272 PipeControlPoint icp = u.list.get(u.list.size() - 1);
1273 if (icp.getNext().equals(info.getEnd()))
1277 u.end = info.getEnd();
1279 u.offset = new Vector3d();
1282 for (PipeControlPoint icp : u.list) {
1283 if (icp.isOffset()) {
1284 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1285 } else if (icp.isDualSub())
1286 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1294 * Processes pipe run (removes necessary turns and updates run ends)
1296 // private static void processPathLeg(PipeControlPoint start, Point3d
1297 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1298 // end,Point3d endPoint, Vector3d dir,Vector3d offset, boolean
1299 // hasOffsets,int iter, boolean reversed, ArrayList<ExpandIterInfo>
1300 // toRemove) throws TransactionException {
1302 private static void processPathLeg(UpdateStruct2 u) throws Exception {
1304 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1305 processPathLeg(u, true, true);
1308 private static void processPathLeg(UpdateStruct2 u, boolean updateEnds, boolean updateInline) throws Exception {
1310 System.out.println("PipingRules.processPathLeg " + (updateEnds ? "ends " : "") + (updateInline ? "inline " : "") + u.start + " " + u.end);
1312 if (u.toRemove.size() > 0) {
1313 for (ExpandIterInfo info : u.toRemove) {
1314 if (info.getStart() != null) {
1316 System.out.println("PipingRules.processPathLeg removing start " + info.getStart());
1317 info.getStart()._remove();
1319 if (info.getEnd() != null) {
1321 System.out.println("PipingRules.processPathLeg removing end " + info.getEnd());
1322 info.getEnd()._remove();
1325 // ControlPointTools.removeControlPoint may remove more than one CP;
1326 // we must populate inline CP list again.
1328 u.start.findNextEnd( u.list);
1330 // FIXME : inline CPs are update twice because their positions must be
1331 // updated before and after ends.
1332 updateInlineControlPoints(u, false);
1335 if (u.start.isTurn()) {
1336 //updateTurnControlPointTurn(u.start, u.start.getPrevious(), u.start.getNext());
1337 updateTurnControlPointTurn(u.start, null, null);
1338 // updatePathLegPrev(u.start, u.start, PathLegUpdateType.NONE);
1339 } else if (u.start.isEnd()) {
1340 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1341 } else if (u.start.isInline()) {
1342 updateControlPointOrientation(u.start);
1344 if (u.end.isTurn()) {
1345 //updateTurnControlPointTurn(u.end, u.end.getPrevious(), u.end.getNext());
1346 updateTurnControlPointTurn(u.end, null, null);
1347 // updatePathLegNext(u.end, u.end, PathLegUpdateType.NONE);
1348 } else if (u.end.isEnd()) {
1349 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1350 } else if (u.end.isInline()) {
1351 updateControlPointOrientation(u.end);
1355 if (u.start.isEnd()) {
1356 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1358 if (u.end.isEnd()) {
1359 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1363 updateInlineControlPoints(u, true);
1368 * Processes pipe run and recalculates offset
1370 // private static void processPathLeg(PipeControlPoint start, Point3d
1371 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1372 // end,Point3d endPoint, Vector3d dir, boolean hasOffsets,int iter, boolean
1373 // reversed, ArrayList<ExpandIterInfo> toRemove) throws TransactionException
1375 private static void processPathLegNoOffset(UpdateStruct2 u) throws Exception {
1377 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1378 Vector3d offset = new Vector3d();
1381 for (PipeControlPoint icp : u.list) {
1382 if (icp.isOffset()) {
1383 offset.add(icp.getSizeChangeOffsetVector(u.dir));
1384 } else if (icp.isDualSub()) {
1385 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1392 private static void updateOffsetPoint(PipeControlPoint sccp, Vector3d offset) {
1393 Vector3d world = sccp.getWorldPosition();
1395 PipeControlPoint ocp = sccp.getSubPoint().iterator().next();
1396 ocp.setWorldPosition(world);
1400 * Updates InlineControlPoints position when straight pipe's end(s) have
1408 private static void updateInlineControlPoint(PipeControlPoint icp, Vector3d prev, Vector3d next, Vector3d dir) {
1410 System.out.println("PipingRules.updateInlineControlPoint() " + icp);
1412 Vector3d inlinePoint = icp.getWorldPosition();
1413 Vector3d prevPoint = new Vector3d(prev);
1414 Vector3d nextPoint = new Vector3d(next);
1415 if (!icp.isVariableLength()) {
1416 // Reserve space for fixed length components.
1417 MathTools.mad(prevPoint, dir, icp.getInlineLength());
1418 MathTools.mad(nextPoint, dir, -icp.getInlineLength());
1419 if (MathTools.distance(prevPoint, nextPoint) < ALLOWED_OFFSET) {
1424 boolean canCalc = MathTools.distance(prevPoint, nextPoint) > ALLOWED_OFFSET;
1426 System.out.print("InlineControlPoint update " + icp + " " + inlinePoint + " " + prevPoint + " " + nextPoint);
1427 Vector3d newInlinePoint = null;
1429 boolean branchUpdate = false;
1430 PipeControlPoint becp = null;
1431 for (PipeControlPoint pcp : icp.getSubPoint())
1432 if (pcp.isNonDirected()) {
1433 branchUpdate = true;
1438 if (DUMMY || !branchUpdate) {
1439 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1443 // FIXME : can only handle one branch
1444 PipeControlPoint p = null;
1445 if (becp.getNext() != null) {
1446 p = becp.findNextEnd();
1447 } else if (becp.getPrevious() != null) {
1448 p = becp.findPreviousEnd();
1451 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1452 } else if (canCalc){
1453 Vector3d branchLegEnd = p.getWorldPosition();
1454 Vector3d dir2 = new Vector3d(inlinePoint);
1455 dir2.sub(branchLegEnd);
1456 Vector3d dir1 = new Vector3d(nextPoint);
1457 dir1.sub(prevPoint);
1458 newInlinePoint = new Vector3d();
1459 double mu[] = new double[2];
1460 MathTools.intersectStraightStraight(new Vector3d(prevPoint), dir1, new Vector3d(branchLegEnd), dir2, newInlinePoint, new Vector3d(), mu);
1462 System.out.println(mu[0]);
1463 // FIXME : reserve space
1465 newInlinePoint = new Vector3d(prevPoint);
1466 } else if (mu[0] > 1.0) {
1467 newInlinePoint = new Vector3d(nextPoint);
1472 // prevPoint == nextPoint
1473 newInlinePoint = new Vector3d(prevPoint);
1476 System.out.println(" " + newInlinePoint);
1478 icp.setWorldPosition(newInlinePoint);
1479 updateControlPointOrientation(icp);
1483 * Updates InlineControlPoints position when straight pipe's end(s) have
1491 private static void updateEndComponentControlPoint(PipeControlPoint ecp, Vector3d start, Vector3d end) throws Exception {
1493 System.out.println("PipingRules.updateEndComponentControlPoint() " + ecp);
1494 //FIXME : end control point cannot be fixed!
1495 //if (!ecp.isFixed())
1496 updateControlPointOrientation(ecp);
1498 for (PipeControlPoint pcp : ecp.getSubPoint()) {
1499 // TODO update position
1500 updatePathLegEndControlPoint(pcp);
1504 private static void updateControlPointOrientation(PipeControlPoint pcp) {
1505 // FIXME : hack to bypass variable length components orientation
1506 // if (pcp.getAtMostOneRelatedObject(ProcessResource.g3dResource.HasWorldOrientation) == null)
1508 // if (pcp.rotationAngle == null)
1510 Double angleO = pcp.getRotationAngle();
1514 Boolean reversedO = pcp.getReversed();
1515 boolean reversed = false;
1516 if (reversedO != null)
1517 reversed = reversedO;
1518 Quat4d q = pcp.getControlPointOrientationQuat(angle, reversed);
1519 pcp.setWorldOrientation(q);
1523 * Updates all branches when branch's position has been changed
1527 private static void updateBranchControlPointBranches(PipeControlPoint bcp) throws Exception {
1529 System.out.println("PipingRules.updateBranchControlPointBranches() " + bcp);
1530 if (bcp.isDualInline())
1532 Collection<PipeControlPoint> branches = bcp.getSubPoint();
1533 if (branches.size() == 0) {
1535 System.out.println("No Branches found");
1539 for (PipeControlPoint pcp : branches) {
1540 updatePathLegEndControlPoint(pcp);
1544 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d prev, Vector3d next) {
1546 UpdateStruct2 us = createUS(tcp, Direction.NEXT, 0, new ArrayList<PipingRules.ExpandIterInfo>(), tcp);
1551 UpdateStruct2 us = createUS(tcp, Direction.PREVIOUS, 0, new ArrayList<PipingRules.ExpandIterInfo>(), tcp);
1557 if (!tcp.asFixedAngle()) {
1560 if (next == null || prev == null) {
1561 if (tcp.getTurnAngle() != null)
1562 return tcp.getTurnAngle();
1563 return Math.PI; // FIXME : argh
1565 double turnAngle = prev.angle(next);
1567 double angle = Math.PI - turnAngle;
1569 Vector3d turnAxis = new Vector3d();
1570 turnAxis.cross(prev, next);
1571 if (turnAxis.lengthSquared() > MathTools.NEAR_ZERO) {
1572 double elbowRadius = ((TurnComponent)tcp.getPipelineComponent()).getTurnRadius();
1573 double R = elbowRadius / Math.tan(angle * 0.5);
1575 turnAxis.normalize();
1576 tcp.setTurnAngle(turnAngle);
1577 tcp.setLength(R);// setComponentOffsetValue(R);
1578 tcp.setTurnAxis(turnAxis);
1579 // tcp.setPosition(tcp.getPosition());
1582 tcp.setTurnAngle(0.0);
1584 tcp.setTurnAxis(new Vector3d(MathTools.Y_AXIS));
1587 updateControlPointOrientation(tcp);
1590 System.out.println("PipingTools.updateTurnControlPointTurn " + prev + " " + next + " " + turnAngle + " " + turnAxis);
1594 if (prev != null && next != null) {
1596 } else if (prev == null) {
1597 if (!tcp._getReversed())
1598 tcp.setReversed(true);
1599 } else if (next == null) {
1600 if (tcp._getReversed())
1601 tcp.setReversed(false);
1604 Vector3d dir = null;
1605 if (!tcp._getReversed()) {
1612 return Math.PI; // FIXME : argh
1615 Quat4d q = PipeControlPoint.getControlPointOrientationQuat(dir, tcp.getRotationAngle() != null ? tcp.getRotationAngle() : 0.0);
1616 Vector3d v = new Vector3d();
1617 MathTools.rotate(q, MathTools.Y_AXIS,v);
1619 tcp.setWorldOrientation(q);
1620 if (tcp.getTurnAngle() != null)
1621 return tcp.getTurnAngle();
1622 return Math.PI; // FIXME : argh
1628 public static List<PipeControlPoint> getControlPoints(PipeRun pipeRun) {
1629 List<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
1630 if (pipeRun.getControlPoints().size() == 0)
1632 PipeControlPoint pcp = pipeRun.getControlPoints().iterator().next();
1633 while (pcp.getPrevious() != null) {
1634 PipeControlPoint prev = pcp.getPrevious();
1635 if (prev.getPipeRun() != pipeRun && prev.getPipeRun() != null) { // bypass possible corruption
1640 if (pcp.isDualSub()) {
1641 pcp = pcp.getParentPoint();
1644 while (pcp.getNext() != null) {
1645 pcp = pcp.getNext();
1646 if (pcp.getPipeRun() != pipeRun)
1653 public static void reverse(PipeRun pipeRun) {
1656 List<PipeControlPoint> points = getControlPoints(pipeRun);
1657 PipeControlPoint pcp = points.get(0);
1658 if (pcp.isSizeChange() && pcp.getSubPoint().size() > 0) {
1659 pipeRun = pcp.getPipeRun();
1664 List<PipeRun> all = new ArrayList<PipeRun>();
1665 List<List<PipeControlPoint>> pcps = new ArrayList<List<PipeControlPoint>>();
1668 List<PipeControlPoint> points = getControlPoints(pipeRun);
1670 PipeControlPoint pcp = points.get(points.size()-1);
1671 if (pcp.getSubPoint().size() > 0) {
1672 pipeRun = pcp.getSubPoint().get(0).getPipeRun();
1677 for (int i = 0 ; i < all.size(); i++) {
1678 List<PipeControlPoint> list = pcps.get(i);
1681 for (int i = 0 ; i < all.size(); i++) {
1682 boolean last = i == all.size() - 1;
1683 List<PipeControlPoint> list = pcps.get(i);
1686 List<PipeControlPoint> list2 = pcps.get(i+1);
1687 PipeControlPoint prev = list.get(list.size()-1);
1688 PipeControlPoint next = list2.get(0);
1689 System.out.println();
1691 // Reverse the component on the boundary.
1692 InlineComponent ic = (InlineComponent)prev.getPipelineComponent();
1693 PipeRun r1 = ic.getPipeRun();
1694 PipeRun r2 = ic.getAlternativePipeRun();
1695 if (r1 == null || r2 == null)
1696 throw new RuntimeException("Components on PipeRun changes should refer to bot PipeRuns");
1700 ic.setAlternativePipeRun(r1);
1702 throw new RuntimeException("PipeRun changes should contain shared control points");
1710 private static void _reverse(List<PipeControlPoint> list) {
1711 if (list.size() <= 1)
1712 return; // nothing to do.
1714 for (int i = 0 ; i < list.size(); i++) {
1715 boolean first = i == 0;
1716 boolean last = i == list.size() - 1;
1717 PipeControlPoint current = list.get(i);
1718 PipeControlPoint currentSub = null;
1719 if (current.isDualInline())
1720 currentSub = current.getSubPoint().get(0);
1722 PipeControlPoint next = list.get(i+1);
1723 if (next.isDualInline())
1724 next = next.getSubPoint().get(0);
1725 if (current.getNext() == next)
1726 current.setNext(null);
1727 current.setPrevious(next);
1728 if (currentSub != null) {
1729 if (currentSub.getNext() == next)
1730 currentSub.setNext(null);
1731 currentSub.setPrevious(next);
1734 PipeControlPoint prev = list.get(i-1);
1736 if (current.getPrevious() == prev)
1737 current.setPrevious(null);
1738 current.setNext(prev);
1740 if (currentSub != null) {
1741 if (currentSub.getPrevious() == prev)
1742 currentSub.setPrevious(null);
1743 currentSub.setNext(prev);
1746 PipeControlPoint prev = list.get(i-1);
1747 PipeControlPoint next = list.get(i+1);
1748 if (next.isDualInline())
1749 next = next.getSubPoint().get(0);
1752 current.setPrevious(next);
1753 current.setNext(prev);
1755 if (currentSub != null) {
1756 currentSub.setPrevious(next);
1757 currentSub.setNext(prev);
1761 //if (current.isTurn() && current.isFixed()) {
1762 if (current.asFixedAngle()) {
1763 current.setReversed(!current._getReversed());
1765 if (current.isInline() && current.isReverse()) {
1766 current.setReversed(!current._getReversed());
1771 public static void merge(PipeRun run1, PipeRun r2) {
1772 Map<PipeControlPoint, Vector3d> positions = new HashMap<PipeControlPoint, Vector3d>();
1773 Map<PipeControlPoint, Quat4d> orientations = new HashMap<PipeControlPoint, Quat4d>();
1774 for (PipeControlPoint pcp : r2.getControlPoints()) {
1775 positions.put(pcp, pcp.getWorldPosition());
1776 orientations.put(pcp, pcp.getWorldOrientation());
1778 for (PipeControlPoint pcp : r2.getControlPoints()) {
1779 r2.deattachChild(pcp);
1781 PipelineComponent component = pcp.getPipelineComponent();
1782 if (component != null) {
1783 if (!(component instanceof Nozzle)) {
1784 component.deattach();
1785 run1.addChild(component);
1787 Nozzle n = (Nozzle)component;
1796 public static void validate(PipeRun pipeRun) {
1797 if (pipeRun == null)
1799 Collection<PipeControlPoint> pcps = pipeRun.getControlPoints();
1801 //System.out.println("Validate " + pipeRun.getName());
1802 for (PipeControlPoint pcp : pcps) {
1803 if (pcp.getParentPoint() == null || pcp.getParentPoint().getPipeRun() != pipeRun)
1806 List<PipeControlPoint> runPcps = getControlPoints(pipeRun);
1807 if (runPcps.size() != count) {
1808 System.out.println("Run " + pipeRun.getName() + " contains unconnected control points, found " + runPcps.size() + " connected, " + pcps.size() + " total.");
1809 for (PipeControlPoint pcp : pcps) {
1810 if (!runPcps.contains(pcp)) {
1811 System.out.println("Unconnected " + pcp + " " + pcp.getPipelineComponent());
1815 for (PipeControlPoint pcp : pcps) {
1816 if (pcp.getPipeRun() == null) {
1817 System.out.println("PipeRun ref missing " + pcp + " " + pcp.getPipelineComponent());
1819 if (!pcp.isDirected() && pcp.getNext() == null && pcp.getPrevious() == null)
1820 System.out.println("Orphan undirected " + pcp + " " + pcp.getPipelineComponent());
1822 for (PipeControlPoint pcp : pcps) {
1823 if (pcp.getParentPoint() == null) {
1824 PipeControlPoint sub = null;
1825 if (pcp.isDualInline())
1826 sub = pcp.getSubPoint().get(0);
1827 PipeControlPoint next = pcp.getNext();
1828 PipeControlPoint prev = pcp.getPrevious();
1830 if (!(next.getPrevious() == pcp || next.getPrevious() == sub)) {
1831 System.out.println("Inconsistency between " + pcp + " -> " +next );
1835 PipeControlPoint prevParent = null;
1836 if (prev.isDualSub()) {
1837 prevParent = prev.getParentPoint();
1838 } else if (prev.isDualInline()) {
1839 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1841 if (!(prev.getNext() == pcp && (prevParent == null || prevParent.getNext() == pcp))) {
1842 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1849 public static void splitVariableLengthComponent(PipelineComponent newComponent, InlineComponent splittingComponent, boolean assignPos) throws Exception{
1850 assert(!splittingComponent.getControlPoint().isFixedLength());
1851 assert(!(newComponent instanceof InlineComponent && !newComponent.getControlPoint().isFixedLength()));
1852 PipeControlPoint newCP = newComponent.getControlPoint();
1853 PipeControlPoint splittingCP = splittingComponent.getControlPoint();
1854 PipeControlPoint nextCP = splittingCP.getNext();
1855 PipeControlPoint prevCP = splittingCP.getPrevious();
1857 /* there are many different cases to insert new component when
1858 it splits existing VariableLengthinlineComponent.
1860 1. VariableLengthComponet is connected from both sides:
1861 - insert new component between VariableLength component and component connected to it
1862 - insert new VariableLengthComponent between inserted component and component selected in previous step
1864 2. VariableLengthComponent is connected from one side
1865 - Use previous case or:
1866 - Insert new component to empty end
1867 - Insert new VariableLength component to inserted components empty end
1869 3. VariableLength is not connected to any component.
1870 - Should not be possible, at least in current implementation.
1871 - Could be done using second case
1875 if (nextCP == null && prevCP == null) {
1876 // this should not be possible
1877 throw new RuntimeException("VariableLengthComponent " + splittingComponent + " is not connected to anything.");
1879 double reservedLength = splittingComponent.getControlPoint().getLength();
1880 double newLength = newComponent.getControlPoint().getLength();
1883 Point3d next = new Point3d();
1884 Point3d prev = new Point3d();
1885 splittingCP.getInlineControlPointEnds(prev, next);
1887 Vector3d newPos = null;
1889 newPos = new Vector3d(prev);
1890 Vector3d dir = new Vector3d(next);
1894 newComponent.setWorldPosition(newPos);
1896 newPos = newComponent.getWorldPosition();
1901 Vector3d dir = new Vector3d(next);
1904 dir.scale(newLength * 0.5);
1905 Point3d vn = new Point3d(newPos);
1906 Point3d vp = new Point3d(newPos);
1909 double ln = vn.distance(next);
1910 double lp = vp.distance(prev);
1911 vp.interpolate(prev, 0.5);
1912 vn.interpolate(next, 0.5);
1915 PipeControlPoint newVariableLengthCP = null;//insertStraight(pcp1, pcp2, pos, length);
1916 if (nextCP == null) {
1917 newCP.insert(splittingCP, Direction.NEXT);
1918 newVariableLengthCP = insertStraight(newCP, Direction.NEXT, new Vector3d(vn), ln);
1919 splittingCP.setWorldPosition(new Vector3d(vp));
1920 // ControlPointTools.setWorldPosition(splittingCP, vp);
1921 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1922 } else if (prevCP == null) {
1923 newCP.insert(splittingCP, Direction.PREVIOUS);
1924 newVariableLengthCP = insertStraight(newCP, Direction.PREVIOUS, new Vector3d(vp), lp);
1925 splittingCP.setWorldPosition(new Vector3d(vn));
1926 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, ln);
1928 newCP.insert(splittingCP, nextCP);
1929 newVariableLengthCP = insertStraight(newCP, nextCP, new Vector3d(vn), ln);
1930 splittingCP.setWorldPosition(new Vector3d(vp));
1931 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1933 positionUpdate(newCP);
1937 public static void addSizeChange(boolean reversed, PipeRun pipeRun, PipeRun other, InlineComponent reducer, PipeControlPoint previous, PipeControlPoint next) {
1938 PipeControlPoint pcp = reducer.getControlPoint();
1939 PipeControlPoint ocp = pcp.getSubPoint().get(0);
1941 String name = pipeRun.getUniqueName("Reducer");
1942 reducer.setName(name);
1943 pipeRun.addChild(reducer);
1944 other.addChild(ocp);
1945 reducer.setAlternativePipeRun(other);
1947 previous.setNext(pcp);
1948 pcp.setPrevious(previous);
1949 ocp.setPrevious(previous);
1953 next.setPrevious(ocp);
1956 String name = other.getUniqueName("Reducer");
1957 reducer.setName(name);
1958 other.addChild(reducer);
1959 pipeRun.addChild(ocp);
1960 reducer.setAlternativePipeRun(pipeRun);
1964 pcp.setPrevious(next);
1965 ocp.setPrevious(next);
1967 pcp.setNext(previous);
1968 ocp.setNext(previous);
1969 previous.setPrevious(ocp);