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> updates = new ArrayList<PipeControlPoint>();
49 private static Object mutex = new Object();
51 public static void requestUpdate(PipeControlPoint pcp) {
52 if (DEBUG) System.out.println("PipingRules request " + pcp);
53 synchronized (mutex) {
54 if (!updates.contains(pcp))
59 public static synchronized boolean update() throws Exception {
60 if (updates.size() == 0)
62 List<PipeControlPoint> temp = new ArrayList<PipeControlPoint>(updates.size());
68 for (PipeControlPoint pcp : temp)
73 public static boolean positionUpdate(PipeControlPoint pcp) throws Exception {
75 return positionUpdate(pcp, true);
78 public static boolean positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception {
79 if (updating || !enabled)
81 if (pcp.getPipeRun() == null)
84 if (DEBUG) System.out.println("PipingRules " + pcp);
86 allowInsertRemove = allowIR;
88 validate(pcp.getPipeRun());
89 if (pcp.isPathLegEnd()) {
90 updatePathLegEndControlPoint(pcp); // FIXME: Rules won't work properly, if they are not run twice.
91 updatePathLegEndControlPoint(pcp);
93 updateInlineControlPoint(pcp);
94 updateInlineControlPoint(pcp);
96 validate(pcp.getPipeRun());
97 if (!allowInsertRemove)
102 // System.out.println("PipingRules done " + pcp);
106 public static void setEnabled(boolean enabled) {
107 PipingRules.enabled = enabled;
112 public static boolean isEnabled() {
116 // private void commit() {
117 // root.getNodeMap().commit();
120 public static class ExpandIterInfo {
121 // these two are turn control points
122 private PipeControlPoint start;
123 private PipeControlPoint end;
126 public ExpandIterInfo() {
130 public ExpandIterInfo(PipeControlPoint tcp, int type) {
131 if (type == REMOVE_START)
138 public ExpandIterInfo(PipeControlPoint start, PipeControlPoint end) {
141 this.type = REMOVE_BOTH;
144 public PipeControlPoint getEnd() {
148 public void setEnd(PipeControlPoint end) {
152 public PipeControlPoint getStart() {
156 public void setStart(PipeControlPoint start) {
160 public int getType() {
164 public void setType(int type) {
170 private static void updatePathLegEndControlPoint(PipeControlPoint pcp) throws Exception {
172 System.out.println("PipingRules.updatePathLegEndControlPoint() " + pcp);
173 if (pcp.getNext() != null) {
174 updatePathLegNext(pcp, pcp, PathLegUpdateType.NEXT_S);
176 if (pcp.getPrevious() != null) {
177 updatePathLegPrev(pcp, pcp, PathLegUpdateType.PREV_S);
182 private static void updateInlineControlPoint(PipeControlPoint pcp) throws Exception {
184 System.out.println("PipingRules.updateInlineControlPoint() " + pcp);
185 PipeControlPoint start = pcp.findPreviousEnd();
186 updatePathLegNext(start, pcp, PathLegUpdateType.NONE);
189 private static PipeControlPoint insertElbow(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos) throws Exception{
191 System.out.println("PipingRules.insertElbow() " + pcp1 + " " + pcp2 + " " + pos);
192 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
194 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getSubPoint().get(0)) {
195 pcp1 = pcp1.getSubPoint().get(0);
196 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
197 PipeControlPoint t = pcp1;
200 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getSubPoint().get(0) && pcp2.getNext() == pcp1) {
201 PipeControlPoint t = pcp1;
202 pcp1 = pcp2.getSubPoint().get(0);
205 throw new RuntimeException();
207 TurnComponent elbow = ComponentUtils.createTurn((P3DRootNode)pcp1.getRootNode());
208 PipeControlPoint pcp = elbow.getControlPoint();
209 if (pcp1.isDualInline())
210 pcp1 = pcp1.getSubPoint().get(0);
211 String name = pcp1.getPipeRun().getUniqueName("Elbow");
213 pcp1.getPipeRun().addChild(elbow);
215 pcp.insert(pcp1, pcp2);
217 pcp.setWorldPosition(pos);
218 validate(pcp.getPipeRun());
222 private static PipeControlPoint insertStraight(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos, double length) throws Exception {
224 System.out.println("PipingRules.insertStraight() " + pcp1 + " " + pcp2 + " " + pos);
225 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
227 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getSubPoint().get(0)) {
228 pcp1 = pcp1.getSubPoint().get(0);
229 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
230 PipeControlPoint t = pcp1;
233 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getSubPoint().get(0) && pcp2.getNext() == pcp1) {
234 PipeControlPoint t = pcp1;
235 pcp1 = pcp2.getSubPoint().get(0);
238 throw new RuntimeException();
240 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp1.getRootNode());
241 PipeControlPoint scp = component.getControlPoint();
242 if (pcp1.isDualInline())
243 pcp1 = pcp1.getSubPoint().get(0);
244 String name = pcp1.getPipeRun().getUniqueName("Pipe");
245 component.setName(name);
246 pcp1.getPipeRun().addChild(component);
248 scp.insert(pcp1, pcp2);
250 scp.setWorldPosition(pos);
251 scp.setLength(length);
252 validate(scp.getPipeRun());
256 private static PipeControlPoint insertStraight(PipeControlPoint pcp, Direction direction , Vector3d pos, double length) throws Exception {
258 System.out.println("PipingRules.insertStraight() " + pcp + " " + direction + " " + pos);
260 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp.getRootNode());
261 PipeControlPoint scp = component.getControlPoint();
262 if (pcp.isDualInline() && direction == Direction.NEXT)
263 pcp = pcp.getSubPoint().get(0);
264 String name = pcp.getPipeRun().getUniqueName("Pipe");
265 component.setName(name);
266 pcp.getPipeRun().addChild(component);
268 scp.insert(pcp,direction);
270 scp.setWorldPosition(pos);
271 scp.setLength(length);
272 validate(scp.getPipeRun());
276 private static void updatePathLegNext(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
277 ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
278 PipeControlPoint end = start.findNextEnd(list);
279 // this is for inline cp that is also path leg end
280 if (lengthChange == PathLegUpdateType.NONE) {
281 if (start.equals(updated))
282 lengthChange = PathLegUpdateType.NEXT;
283 else if (end.equals(updated))
284 lengthChange = PathLegUpdateType.PREV;
286 updatePathLegNext(start, list, end, updated, lengthChange);
289 private static void updatePathLegNext(PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
290 updatePathLeg(start, list, end, false, 0, new ArrayList<ExpandIterInfo>(), updated, lengthChange);
293 private static void updatePathLegPrev(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
294 ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
295 PipeControlPoint end = start.findPreviousEnd(list);
296 // TODO: this method is not symmetric with updatePathLegNext, which may alter lengthChange parameter?
297 updatePathLegPrev(start, list, end, updated, lengthChange);
300 private static void updatePathLegPrev(PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
302 ArrayList<PipeControlPoint> nextList = new ArrayList<PipeControlPoint>();
303 for (PipeControlPoint icp : list) {
304 if (icp.isDualSub()) {
305 nextList.add(0, icp.getParentPoint());
307 nextList.add(0, icp);
310 updatePathLeg(end, nextList, start, true, 0, new ArrayList<ExpandIterInfo>(), updated, lengthChange);
314 private static class UpdateStruct2 {
315 public PipeControlPoint start;
316 public Vector3d startPoint;
317 public ArrayList<PipeControlPoint> list;
318 public PipeControlPoint end;
319 public Vector3d endPoint;
321 public Vector3d offset;
322 public boolean hasOffsets;
324 public boolean reversed;
325 public ArrayList<ExpandIterInfo> toRemove;
326 public PipeControlPoint updated;
328 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) {
329 if (start == null || end == null)
330 throw new NullPointerException();
332 this.startPoint = startPoint;
335 this.endPoint = endPoint;
337 this.offset = offset;
338 this.hasOffsets = hasOffsets;
340 this.reversed = reversed;
341 this.toRemove = toRemove;
342 this.updated = updated;
344 if (!MathTools.isValid(startPoint) ||
345 !MathTools.isValid(endPoint) ||
346 !MathTools.isValid(dir)) {
347 throw new RuntimeException();
351 public String toString() {
352 return start + " " + end+ " " + dir + " " + hasOffsets + " " + offset + " " + iter + " " + toRemove.size();
357 private static boolean calculateOffset(Vector3d startPoint, Vector3d endPoint, ArrayList<PipeControlPoint> list, Vector3d dir, Vector3d offset) {
358 boolean hasOffsets = false;
359 List<PipeControlPoint> offsets = new ArrayList<PipeControlPoint>(list.size());
360 for (PipeControlPoint icp : list) {
361 if (icp.isOffset()) {
363 } else if (icp.isDualSub())
364 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
366 if (offsets.size() == 0) {
369 double l = dir.lengthSquared();
370 if (l > MathTools.NEAR_ZERO)
371 dir.scale(1.0/Math.sqrt(l));
372 offset.set(0.0, 0.0, 0.0);
375 Vector3d sp = new Vector3d(startPoint);
376 Point3d ep = new Point3d(endPoint);
379 double l = dir.lengthSquared();
380 if (l > MathTools.NEAR_ZERO)
381 dir.scale(1.0/Math.sqrt(l));
385 offset.set(0.0, 0.0, 0.0);
387 for (PipeControlPoint icp : offsets) {
388 Vector3d v = icp.getSizeChangeOffsetVector(dir);
391 Point3d nep = new Point3d(endPoint);
393 if (nep.distance(ep) < 0.0000000001) {
399 l = dir.lengthSquared();
400 if (l > MathTools.NEAR_ZERO)
401 dir.scale(1.0/Math.sqrt(l));
406 // for (PipeControlPoint icp : list) {
407 // if (icp.isOffset()) {
408 // icp.setOffset(((InlineComponent)icp.getPipelineComponent()).getOffset());
409 // hasOffsets = true;
410 // Vector3d v = icp.getSizeChangeOffsetVector(dir);
412 // } else if (icp.isDualSub())
413 // ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
415 if (DEBUG && hasOffsets)
416 System.out.println("calcOffset s:"+ startPoint + " e:" + endPoint + " d:" + dir + " o:"+offset) ;
422 * starting point of the pipe run
424 * list of inline control points in the pipe run
426 * ending point of the pipe run
428 * boolean flag indicating wether start or end control point was
429 * modified (if true then end point was modified)
430 * @throws TransactionException
432 private static void updatePathLeg(PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, boolean reversed, int iter, ArrayList<ExpandIterInfo> toRemove, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
435 // FIXME: direction is calculated wrong way!
436 boolean hasOffsets = false;
437 Vector3d offset = new Vector3d();
438 Vector3d startPoint = start.getWorldPosition();
439 Vector3d endPoint = end.getWorldPosition();
440 Vector3d dir = new Vector3d();
441 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
442 updatePathLeg(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, offset, hasOffsets, iter, reversed, toRemove, updated), lengthChange);
446 private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
448 if (u.start.isDirected())
450 if (u.end.isDirected())
454 updateFreePathLeg(u, lengthChange);
457 updateDirectedPathLeg(u, lengthChange);
460 updateDualDirectedPathLeg(u, lengthChange);
466 private static void updateFreePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
468 System.out.println("PipingRules.updateFreePipeRun " + u + " " + lengthChange);
469 checkExpandPathLeg(u, lengthChange);
470 if (u.start.isInline() || u.end.isInline())
471 processPathLeg(u, true, false);
474 private static void updateInlineControlPoints(UpdateStruct2 u, boolean checkSizes) throws Exception{
476 System.out.println("PipingRules.updateInlineControlPoints() " + u);
478 Vector3d start = new Vector3d(u.startPoint);
479 Vector3d end = new Vector3d(u.endPoint);
482 // create offsets for leg ends.
483 MathTools.mad(start, u.dir, u.start.getInlineLength());
484 MathTools.mad(end, u.dir, -u.end.getInlineLength());
487 boolean recalcline = false;
491 for (PipeControlPoint icp : u.list) {
492 updateInlineControlPoint(icp, start, end, u.dir);
494 if (icp.isOffset()) {
495 // TODO : offset vector is already calculated and should be cached
496 Vector3d off = icp.getSizeChangeOffsetVector(u.dir);
497 updateOffsetPoint(icp, off);
503 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
504 pathLegPoints.add(u.start);
505 for (PipeControlPoint icp : u.list) {
506 // updateInlineControlPoint(icp, u.startPoint,
507 // u.endPoint,u.dir);
508 updateBranchControlPointBranches(icp);
509 pathLegPoints.add(icp);
511 pathLegPoints.add(u.end);
513 // TODO : values can be cached in the loop
514 for (int i = 1; i < pathLegPoints.size(); i++) {
515 PipeControlPoint icp = pathLegPoints.get(i);
517 PipeControlPoint prev = pathLegPoints.get(i - 1);
520 if (icp.isVariableLength()) {
521 if (i != pathLegPoints.size() - 1) {
522 PipeControlPoint next = pathLegPoints.get(i + 1);
523 recalcline = recalcline | updateVariableLength(icp, prev, next);
526 // this is variable length component at the end of the
528 // the problem is that we want to keep unconnected end
529 // of the component in the same
530 // place, but center of the component must be moved.
531 updateVariableLengthEnd(icp, prev);
535 } else if (!prev.isVariableLength()) {
536 // If this and previous control point are not variable
537 // length pcps, we'll have to check if there is no empty
538 // space between them.
539 // I there is, we'll have to create new variable length
540 // component between them.
541 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
544 } else { // with offset
545 Vector3d sp = new Vector3d(start);
546 Vector3d ep = new Vector3d(end);
549 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
550 pathLegPoints.add(u.start);
552 for (PipeControlPoint icp : u.list) {
553 updateInlineControlPoint(icp, sp, ep, u.dir);
554 updateBranchControlPointBranches(icp);
555 pathLegPoints.add(icp);
556 if (icp.isOffset()) {
557 // TODO : offset vector is already calculated and should be
559 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
560 updateOffsetPoint(icp, offset);
565 pathLegPoints.add(u.end);
570 sp = new Vector3d(u.startPoint);
571 ep = new Vector3d(u.endPoint);
574 for (int i = 1; i < pathLegPoints.size(); i++) {
575 PipeControlPoint icp = pathLegPoints.get(i);
577 PipeControlPoint prev = pathLegPoints.get(i - 1);
578 if (prev.isDualInline())
579 prev = prev.getSubPoint().get(0);
582 if (icp.isVariableLength()) {
583 if (i != pathLegPoints.size() - 1) {
584 PipeControlPoint next;
585 next = pathLegPoints.get(i + 1);
586 recalcline = recalcline | updateVariableLength(icp, prev, next);
589 // this is variable length component at the end of the
591 // the problem is that we want to keep unconnected end
592 // of the component in the same
593 // place, but center of the component must be moved.
594 updateVariableLengthEnd(icp, prev);
596 } else if (!prev.isVariableLength()) {
597 // If this and previous control point are not variable
598 // length pcps, we'll have to check if there is no empty
599 // space between them.
600 // I there is, we'll have to create new variable length
601 // component between them.
602 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
604 if (icp.isOffset()) {
605 // TODO : offset vector is already calculated and should be
607 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
615 u.start.findNextEnd(u.list);
619 private static boolean updateVariableLength(PipeControlPoint icp, PipeControlPoint prev, PipeControlPoint next) {
620 Vector3d prevPos = prev.getWorldPosition();
621 Vector3d nextPos = next.getWorldPosition();
623 Vector3d dir = new Vector3d(nextPos);
625 double l = dir.lengthSquared(); // distance between
628 double l2prev = prev.getInlineLength(); // distance
632 double l2next = next.getInlineLength();
633 double l2 = l2prev + l2next;
634 double l2s = MathTools.square(l2);
635 if (l2s < l) { // check if there is enough space for
636 // variable length component.
639 double length = Math.sqrt(l) - l2; // true length of
643 dir.scale(length * 0.5 + l2prev); // calculate
648 icp.setWorldPosition(dir);
649 icp.setLength(length);
652 // components leave no space to the component and it
655 if (icp.isDeletable()) {
657 System.out.println("PipingRules.updateVariableLength removing " + icp);
665 private static boolean possibleVaribleLengthInsert(PipeControlPoint icp, PipeControlPoint prev) throws Exception{
666 Vector3d currentPos = icp.getWorldPosition();
667 Vector3d prevPos = prev.getWorldPosition();
668 Vector3d dir = new Vector3d(currentPos);
670 double l = dir.lengthSquared();
671 double l2prev = prev.getInlineLength();
672 double l2next = icp.getInlineLength();
673 double l2 = l2prev + l2next;
674 double l2s = l2 * l2;
676 if (allowInsertRemove) {
678 double length = Math.sqrt(l) - l2; // true length of the
681 dir.scale(length * 0.5 + l2prev); // calculate center
685 PipeControlPoint scp = insertStraight(prev, icp, dir, length);
694 private static void updateVariableLengthEnd(PipeControlPoint icp, PipeControlPoint prev) {
695 double currentLength = icp.getLength();
696 Vector3d currentPos = icp.getWorldPosition();
697 Vector3d prevPos = prev.getWorldPosition();
699 Vector3d dir = new Vector3d();
700 dir.sub(currentPos, prevPos);
702 if (currentLength < MathTools.NEAR_ZERO) {
703 currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
706 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
708 Point3d endPos = new Point3d(dir);
709 endPos.scale(currentLength * 0.5);
710 endPos.add(currentPos); // this is the free end of the
713 double offset = prev.getInlineLength();
714 Point3d beginPos = new Point3d(dir);
715 beginPos.scale(offset);
716 beginPos.add(prevPos); // this is the connected end of
719 double l = beginPos.distance(endPos);
722 System.out.println("Length for " + icp + " is NaN");
725 beginPos.add(dir); // center position
728 System.out.println("PipingRules.updateInlineControlPoints() setting variable length to " + l);
731 icp.setWorldPosition(new Vector3d(beginPos));
734 private static void ppNoOffset(UpdateStruct2 u) throws Exception {
736 System.out.println("PipingRules.ppNoOffset() " + u);
737 Vector3d offset = new Vector3d();
740 for (PipeControlPoint icp : u.list) {
741 if (icp.isOffset()) {
742 offset.add(icp.getSizeChangeOffsetVector(u.dir));
743 } else if (icp.isDualSub())
744 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
748 checkExpandPathLeg(u, PathLegUpdateType.NONE);
751 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 {
753 System.out.println("PipingRules.ppNoDir() " + start + " " + end + " " + iter + " " + toRemove.size());
754 // FIXME : extra loop (dir should be calculated here)
755 Vector3d dir = new Vector3d();
756 Vector3d offset = new Vector3d();
757 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
758 ppNoOffset(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, null, hasOffsets, iter, reversed, toRemove, updated));
761 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
762 checkExpandPathLeg(u, lengthChange, u.updated.isInline() && u.updated.isOffset());
765 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange, boolean updateEnds) throws Exception {
767 System.out.println("PipingRules.checkExpandPathLeg() " + u + " " + lengthChange);
768 if (lengthChange != PathLegUpdateType.NONE) {
769 // FIXME : turns cannot be checked before inline cps are updated,
770 // since their position affects calculation of turns
771 processPathLeg(u, updateEnds, false);
772 int type = checkTurns(u, lengthChange);
773 if (type == REMOVE_NONE) {
774 processPathLeg(u, updateEnds, true);
776 expandPathLeg(u, type);
779 processPathLeg(u, updateEnds, true);
783 private static void updateDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
785 System.out.println("PipingRules.updateDirectedPipeRun() " + u + " " + lengthChange);
786 PipeControlPoint dcp;
787 PipeControlPoint other;
788 boolean canMoveOther = false;
789 boolean dcpStart = false;
790 boolean inlineEnd = false;
792 if (u.start.isDirected()) {
795 position = u.startPoint;
799 inlineEnd = u.end.isInline();
804 position = u.endPoint;
807 inlineEnd = u.start.isInline();
810 Vector3d directedDirection = dcp.getDirection();
811 Point3d directedEndPoint = new Point3d(u.endPoint);
813 directedEndPoint.add(u.offset);
815 double mu[] = new double[2];
818 Vector3d t = new Vector3d();
821 closest = MathTools.closestPointOnStraight(directedEndPoint, u.startPoint, directedDirection, mu);
822 t.sub(closest, directedEndPoint);
824 closest = MathTools.closestPointOnStraight(u.startPoint, directedEndPoint, directedDirection, mu);
825 t.sub(closest, u.startPoint);
828 double distance = t.length();
829 boolean aligned = (distance < ALLOWED_OFFSET);
831 checkExpandPathLeg(u, lengthChange, inlineEnd);
837 PipeControlPoint nextToMoved;
839 if (u.list.size() > 0)
841 nextToMoved = u.list.get(0);
843 nextToMoved = u.list.get(u.list.size() - 1);
847 nextToMoved = u.start;
848 if (other.isVariableAngle()) {
850 // TODO calculate needed space from next run end.
853 closest.set(u.startPoint);
855 closest.set(u.endPoint);
857 Vector3d v = new Vector3d(directedDirection);
858 v.scale(spaceForTurn(other));
864 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + closest);
865 other.setWorldPosition(closest);
867 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));
868 if (u.end.getNext() != null)
869 updatePathLegNext(u.end, u.updated, PathLegUpdateType.NEXT);
871 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));
872 if (u.start.getPrevious() != null)
873 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.PREV);
876 // TODO : calculate needed space from next run end.
877 if (allowInsertRemove)
878 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
883 } else if (other.isNonDirected() && other.getParentPoint() != null) {
884 // FIXME : this code was for updating branches
885 Vector3d bintersect = new Vector3d();
886 PipeControlPoint bcp = other.getParentPoint();
887 if (bcp != null && canMoveOther) {
888 Point3d bstart = new Point3d();
889 Point3d bend = new Point3d();
890 Vector3d bdir = new Vector3d();
891 bcp.getInlineControlPointEnds(bstart, bend, bdir);
892 Vector3d nintersect = new Vector3d();
894 MathTools.intersectStraightStraight(position, directedDirection, bend, bdir, nintersect, bintersect, mu);
895 Vector3d dist = new Vector3d(nintersect);
896 dist.sub(bintersect);
897 canMoveOther = mu[1] > 0.0 && mu[1] < 1.0 && dist.lengthSquared() < 0.01;
899 // TODO : endControlPoints are undirected: calculcate
900 // correct position for it
901 throw new UnsupportedOperationException("not implemented");
905 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + bintersect);
906 // is required branch position is in possible range
907 bcp.setWorldPosition(bintersect);
909 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);
911 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);
914 // branch cannot be moved into right position, new turn
915 // / elbow must be inserted
916 if (allowInsertRemove)
917 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
922 } else { // assume that control point cannot be moved, but can
924 if (allowInsertRemove)
925 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
935 private static void updateDualDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
937 System.out.println("PipingRules.updateDualDirectedPipeRun() " + u + " " + lengthChange);
939 PipeControlPoint dcp1 = u.start;
940 PipeControlPoint dcp2 = u.end;
941 Point3d position1 = new Point3d(u.startPoint);
942 Point3d position2 = new Point3d(u.endPoint);
943 Point3d position1offset = new Point3d(position1);
944 position1offset.sub(u.offset);
945 Point3d position2offset = new Point3d(position2);
946 position2offset.add(u.offset);
947 Vector3d dir1 = dcp1.getDirection();
948 Vector3d dir2 = dcp2.getDirection();
949 Vector3d p1 = MathTools.closestPointOnStraight(position1offset, position2, dir2);
950 Vector3d p2 = MathTools.closestPointOnStraight(position2offset, position1, dir1);
951 double d1 = position1.distance(new Point3d(p1));
952 double d2 = position2.distance(new Point3d(p2));
954 boolean aligned = (d1 < ALLOWED_OFFSET && d2 < ALLOWED_OFFSET);
960 } else if (allowInsertRemove){
961 PipeControlPoint dcp;
962 PipeControlPoint next;
965 if (u.list.size() > 0)
966 next = u.list.get(0);
971 if (u.list.size() > 0)
972 next = u.list.get(u.list.size() - 1);
977 p1 = dcp.getWorldPosition();
978 // FIXME: calculate position of the elbows properly.
985 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position2, dir2);
987 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position1, dir1);
990 PipeControlPoint tcp1 = insertElbow(dcp, next, p1);
991 PipeControlPoint tcp2 = insertElbow(tcp1, next, p2);
994 System.out.println("PipingRules.updateDualDirectedPipeRun() created two turns " + tcp1 + " " + tcp2);
997 Vector3d dd = new Vector3d(p2);
1000 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1001 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1003 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1005 updatePathLegPrev(tcp2, u.updated, PathLegUpdateType.NONE);
1007 Vector3d dd = new Vector3d(p1);
1010 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1011 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1013 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1015 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.NONE);
1024 private static double spaceForTurn(PipeControlPoint tcp) {
1025 // TODO : this returns now space for 90 deg turn.
1026 // The challenge: position of tcp affects the turn angle, which then affects the required space. Perhaps we need to iterate...
1027 // Additionally, if the path legs contain offset, using just positions of opposite path leg ends is not enough,
1028 return tcp.getPipeRun().getTurnRadius();
1031 private static void insertElbowUpdate(UpdateStruct2 u, PipeControlPoint dcp, PipeControlPoint next, boolean dcpStart, Vector3d position, Vector3d directedDirection) throws Exception{
1034 // Vector3d closest = new Vector3d(position);
1035 // closest.add(directedDirection);
1037 PipeControlPoint tcp = null;
1040 closest = MathTools.closestPointOnStraight(next.getWorldPosition(), position, directedDirection);
1041 tcp = insertElbow(dcp, next, closest);
1043 closest = MathTools.closestPointOnStraight(dcp.getWorldPosition(), position, directedDirection);
1044 tcp = insertElbow(next, dcp, closest);
1046 // TODO properly calculate required distance between start and inserted elbow.
1047 double d = MathTools.distance(position, closest);
1048 double s = spaceForTurn(tcp);
1051 Vector3d p = new Vector3d(directedDirection);
1061 System.out.println("PipingRules.updateDirectedPipeRun() inserted " + tcp);
1064 // update pipe run from new turn to other end
1065 ppNoDir(tcp, new Vector3d(closest), u.list, u.end, u.endPoint, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1066 // update pipe run from directed to new turn
1067 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));
1069 // update pipe run from other end to new turn
1070 ppNoDir(u.start, u.startPoint, u.list, tcp, new Vector3d(closest), u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1071 // update pipe run from new turn to directed
1072 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));
1077 * Checks if turns can be removed (turn angle near zero)
1079 private static int checkTurns(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1081 System.out.println("PipingRules.checkTurns() " + u.start + " " + u.end);
1082 boolean startRemoved = false;
1083 boolean endRemoved = false;
1084 if (u.start.isVariableAngle()) {
1085 // this won't work properly if inline control points are not updated
1086 PipeControlPoint startPrev = u.start.getPrevious();
1087 if (startPrev != null) {
1089 if (!u.hasOffsets) {
1090 a = updateTurnControlPointTurn(u.start, startPrev, u.end);
1092 Vector3d ep = new Vector3d(u.endPoint);
1094 a = updateTurnControlPointTurn(u.start, u.startPoint, startPrev.getPosition(), ep);
1097 if (a < MIN_TURN_ANGLE && u.start.isDeletable())
1098 startRemoved = true;
1099 else if (lengthChange == PathLegUpdateType.PREV || lengthChange == PathLegUpdateType.PREV_S) {
1100 PathLegUpdateType type;
1101 if (lengthChange == PathLegUpdateType.PREV_S)
1102 type = PathLegUpdateType.PREV;
1104 type = PathLegUpdateType.NONE;
1105 updatePathLegPrev(u.start, u.start, type);
1109 if (u.end.isVariableAngle()) {
1111 PipeControlPoint endNext = u.end.getNext();
1112 if (endNext != null) {
1114 if (!u.hasOffsets) {
1115 a = updateTurnControlPointTurn(u.end, u.start, endNext);
1117 Vector3d sp = new Vector3d(u.startPoint);
1119 a = updateTurnControlPointTurn(u.end, u.endPoint, sp, endNext.getPosition());
1121 if (a < MIN_TURN_ANGLE && u.end.isDeletable())
1123 else if (lengthChange == PathLegUpdateType.NEXT || lengthChange == PathLegUpdateType.NEXT_S) {
1124 PathLegUpdateType type;
1125 if (lengthChange == PathLegUpdateType.NEXT_S)
1126 type = PathLegUpdateType.NEXT;
1128 type = PathLegUpdateType.NONE;
1129 updatePathLegNext(u.end, u.end, type);
1134 System.out.println("PipingRules.checkTurns() res " + startRemoved + " " + endRemoved);
1135 if (!startRemoved && !endRemoved)
1137 if (startRemoved && endRemoved)
1140 return REMOVE_START;
1145 * Expands piperun search over turns that are going to be removed
1148 private static void expandPathLeg(UpdateStruct2 u, int type) throws Exception {
1150 System.out.println("PipingRules.expandPipeline " + u.start + " " + u.end);
1151 ArrayList<PipeControlPoint> newList = new ArrayList<PipeControlPoint>();
1154 throw new RuntimeException("Error in piping rules");
1156 u.toRemove.add(new ExpandIterInfo(u.start, REMOVE_START));
1157 u.start = u.start.findPreviousEnd();
1158 u.startPoint = u.start.getPosition();
1159 u.start.findNextEnd(newList);
1160 newList.addAll(u.list);
1164 u.toRemove.add(new ExpandIterInfo(u.end, REMOVE_END));
1165 u.end = u.end.findNextEnd(newList);
1166 u.endPoint = u.end.getPosition();
1167 u.list.addAll(newList);
1170 u.toRemove.add(new ExpandIterInfo(u.start, u.end));
1171 u.start = u.start.findPreviousEnd();
1172 u.startPoint = u.start.getPosition();
1173 u.start.findNextEnd(newList);
1174 newList.addAll(u.list);
1176 newList = new ArrayList<PipeControlPoint>();
1177 u.end = u.end.findNextEnd(newList);
1178 u.endPoint = u.end.getPosition();
1179 u.list.addAll(newList);
1182 throw new RuntimeException("Error in piping rules");
1185 u.offset = new Vector3d();
1188 for (PipeControlPoint icp : u.list) {
1189 if (icp.isOffset()) {
1190 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1191 } else if (icp.isDualSub())
1192 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1196 System.out.println("PipingRules.expandPipeline expanded " + u.start + " " + u.end);
1198 updatePathLeg(u, PathLegUpdateType.NONE);
1202 * reverts one iteration of turn removing back)
1204 private static void backIter(UpdateStruct2 u) throws Exception {
1207 System.out.println("PipingRules.backIter" + u.start + " " + u.end);
1209 throw new RuntimeException("Error in piping rules");
1210 ExpandIterInfo info = u.toRemove.get(u.toRemove.size() - 1);
1211 u.toRemove.remove(u.toRemove.size() - 1);
1212 if (info.getType() == REMOVE_START || info.getType() == REMOVE_BOTH) {
1213 while (u.list.size() > 0) {
1214 PipeControlPoint icp = u.list.get(0);
1215 if (icp.getPrevious().equals(info.getStart()))
1219 u.start = info.getStart();
1221 if (info.getType() == REMOVE_END || info.getType() == REMOVE_BOTH) {
1222 while (u.list.size() > 0) {
1223 PipeControlPoint icp = u.list.get(u.list.size() - 1);
1224 if (icp.getNext().equals(info.getEnd()))
1228 u.end = info.getEnd();
1230 u.offset = new Vector3d();
1233 for (PipeControlPoint icp : u.list) {
1234 if (icp.isOffset()) {
1235 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1236 } else if (icp.isDualSub())
1237 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1245 * Processes pipe run (removes necessary turns and updates run ends)
1247 // private static void processPathLeg(PipeControlPoint start, Point3d
1248 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1249 // end,Point3d endPoint, Vector3d dir,Vector3d offset, boolean
1250 // hasOffsets,int iter, boolean reversed, ArrayList<ExpandIterInfo>
1251 // toRemove) throws TransactionException {
1253 private static void processPathLeg(UpdateStruct2 u) throws Exception {
1255 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1256 processPathLeg(u, true, true);
1259 private static void processPathLeg(UpdateStruct2 u, boolean updateEnds, boolean updateInline) throws Exception {
1261 System.out.println("PipingRules.processPathLeg " + (updateEnds ? "ends " : "") + (updateInline ? "inline " : "") + u.start + " " + u.end);
1263 if (u.toRemove.size() > 0) {
1264 for (ExpandIterInfo info : u.toRemove) {
1265 if (info.getStart() != null) {
1267 System.out.println("PipingRules.processPathLeg removing start " + info.getStart());
1268 info.getStart()._remove();
1270 if (info.getEnd() != null) {
1272 System.out.println("PipingRules.processPathLeg removing end " + info.getEnd());
1273 info.getEnd()._remove();
1276 // ControlPointTools.removeControlPoint may remove mo0re than one
1278 // we must populate inline CP list again.
1280 u.start.findNextEnd( u.list);
1282 // FIXME : inline CPs are update twice because their positions must be
1283 // updated before and after ends.
1284 updateInlineControlPoints(u, false);
1287 if (u.start.isTurn()) {
1288 updateTurnControlPointTurn(u.start, u.start.getPrevious(), u.start.getNext());
1289 // updatePathLegPrev(u.start, u.start, PathLegUpdateType.NONE);
1290 } else if (u.start.isEnd()) {
1291 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1292 } else if (u.start.isInline()) {
1293 updateControlPointOrientation(u.start);
1295 if (u.end.isTurn()) {
1296 updateTurnControlPointTurn(u.end, u.end.getPrevious(), u.end.getNext());
1297 // updatePathLegNext(u.end, u.end, PathLegUpdateType.NONE);
1298 } else if (u.end.isEnd()) {
1299 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1300 } else if (u.end.isInline()) {
1301 updateControlPointOrientation(u.end);
1305 if (u.start.isEnd()) {
1306 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1308 if (u.end.isEnd()) {
1309 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1313 updateInlineControlPoints(u, true);
1318 * Processes pipe run and recalculates offset
1320 // private static void processPathLeg(PipeControlPoint start, Point3d
1321 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1322 // end,Point3d endPoint, Vector3d dir, boolean hasOffsets,int iter, boolean
1323 // reversed, ArrayList<ExpandIterInfo> toRemove) throws TransactionException
1325 private static void processPathLegNoOffset(UpdateStruct2 u) throws Exception {
1327 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1328 Vector3d offset = new Vector3d();
1331 for (PipeControlPoint icp : u.list) {
1332 if (icp.isOffset()) {
1333 offset.add(icp.getSizeChangeOffsetVector(u.dir));
1334 } else if (icp.isDualSub()) {
1335 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1342 private static void updateOffsetPoint(PipeControlPoint sccp, Vector3d offset) {
1343 Vector3d world = sccp.getWorldPosition();
1345 PipeControlPoint ocp = sccp.getSubPoint().iterator().next();
1346 ocp.setWorldPosition(world);
1350 * Updates InlineControlPoints position when straight pipe's end(s) have
1358 private static void updateInlineControlPoint(PipeControlPoint icp, Vector3d prev, Vector3d next, Vector3d dir) {
1360 System.out.println("PipingRules.updateInlineControlPoint() " + icp);
1362 Vector3d inlinePoint = icp.getWorldPosition();
1363 Vector3d prevPoint = new Vector3d(prev);
1364 Vector3d nextPoint = new Vector3d(next);
1365 if (!icp.isVariableLength()) {
1366 // Reserve space for fixed length components.
1367 MathTools.mad(prevPoint, dir, icp.getInlineLength());
1368 MathTools.mad(nextPoint, dir, -icp.getInlineLength());
1369 if (MathTools.distance(prevPoint, nextPoint) < ALLOWED_OFFSET) {
1374 boolean canCalc = MathTools.distance(prevPoint, nextPoint) > ALLOWED_OFFSET;
1376 System.out.print("InlineControlPoint update " + icp + " " + inlinePoint + " " + prevPoint + " " + nextPoint);
1377 Vector3d newInlinePoint = null;
1379 boolean branchUpdate = false;
1380 PipeControlPoint becp = null;
1381 for (PipeControlPoint pcp : icp.getSubPoint())
1382 if (pcp.isNonDirected()) {
1383 branchUpdate = true;
1388 if (DUMMY || !branchUpdate) {
1389 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1393 // FIXME : can only handle one branch
1394 PipeControlPoint p = null;
1395 if (becp.getNext() != null) {
1396 p = becp.findNextEnd();
1397 } else if (becp.getPrevious() != null) {
1398 p = becp.findPreviousEnd();
1401 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1402 } else if (canCalc){
1403 Vector3d branchLegEnd = p.getWorldPosition();
1404 Vector3d dir2 = new Vector3d(inlinePoint);
1405 dir2.sub(branchLegEnd);
1406 Vector3d dir1 = new Vector3d(nextPoint);
1407 dir1.sub(prevPoint);
1408 newInlinePoint = new Vector3d();
1409 double mu[] = new double[2];
1410 MathTools.intersectStraightStraight(new Vector3d(prevPoint), dir1, new Vector3d(branchLegEnd), dir2, newInlinePoint, new Vector3d(), mu);
1412 System.out.println(mu[0]);
1413 // FIXME : reserve space
1415 newInlinePoint = new Vector3d(prevPoint);
1416 } else if (mu[0] > 1.0) {
1417 newInlinePoint = new Vector3d(nextPoint);
1422 // prevPoint == nextPoint
1423 newInlinePoint = new Vector3d(prevPoint);
1426 System.out.println(" " + newInlinePoint);
1428 icp.setWorldPosition(newInlinePoint);
1429 updateControlPointOrientation(icp);
1433 * Updates InlineControlPoints position when straight pipe's end(s) have
1441 private static void updateEndComponentControlPoint(PipeControlPoint ecp, Vector3d start, Vector3d end) throws Exception {
1443 System.out.println("PipingRules.updateEndComponentControlPoint() " + ecp);
1444 // PipeControlPoint next = ecp.getNext();
1445 // PipeControlPoint prev = ecp.getPrevious();
1446 // if (next != null) {
1447 // end = G3DTools.getPoint(next.getLocalPosition());
1448 // start = G3DTools.getPoint(ecp.getLocalPosition());
1449 // } else if (prev != null) {
1450 // end = G3DTools.getPoint(ecp.getLocalPosition());
1451 // start = G3DTools.getPoint(prev.getLocalPosition());
1453 // // TODO : warning?
1456 // Vector3d dir = new Vector3d (end);
1459 // G3DTools.setTuple(ecp.getDirection(), dir);
1461 updateControlPointOrientation(ecp);
1463 for (PipeControlPoint pcp : ecp.getSubPoint()) {
1464 // TODO update position
1465 updatePathLegEndControlPoint(pcp);
1469 private static void updateControlPointOrientation(PipeControlPoint pcp) {
1470 // FIXME : hack to bypass variable length components orientation
1471 // if (pcp.getAtMostOneRelatedObject(ProcessResource.g3dResource.HasWorldOrientation) == null)
1473 // if (pcp.rotationAngle == null)
1475 Double angleO = pcp.getRotationAngle();
1479 Boolean reversedO = pcp.getReversed();
1480 boolean reversed = false;
1481 if (reversedO != null)
1482 reversed = reversedO;
1483 Quat4d q = pcp.getControlPointOrientationQuat(angle, reversed);
1484 pcp.setWorldOrientation(q);
1488 * Updates all branches when branch's position has been changed
1492 private static void updateBranchControlPointBranches(PipeControlPoint bcp) throws Exception {
1494 System.out.println("PipingRules.updateBranchControlPointBranches() " + bcp);
1495 if (bcp.isDualInline())
1497 Collection<PipeControlPoint> branches = bcp.getSubPoint();
1498 if (branches.size() == 0) {
1500 System.out.println("No Branches found");
1504 for (PipeControlPoint pcp : branches) {
1505 updatePathLegEndControlPoint(pcp);
1510 * Recalculates turn control point's internal data (turn angle and offset)
1516 private static double updateTurnControlPointTurn(PipeControlPoint tcp, PipeControlPoint prev, PipeControlPoint next) {
1518 System.out.println("PipingTools.updateTurnControlPointTurn()" + tcp);
1519 if (next == null || prev == null)
1520 return Math.PI; // FIXME : argh
1521 Vector3d middlePoint = tcp.getWorldPosition();
1522 Vector3d nextPoint = next.getWorldPosition();
1523 Vector3d prevPoint = prev.getWorldPosition();
1524 return updateTurnControlPointTurn(tcp, middlePoint, prevPoint, nextPoint);
1528 * Recalculates turn control point's internal data (turn angle and offset)
1531 * @param middlePoint
1535 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d middlePoint, Vector3d prevPoint, Vector3d nextPoint) {
1537 Vector3d dir1 = new Vector3d(middlePoint);
1538 dir1.sub(prevPoint);
1539 Vector3d dir2 = new Vector3d(nextPoint);
1540 dir2.sub(middlePoint);
1542 System.out.println("PipingTools.updateTurnControlPointTurn " + tcp + " " + prevPoint + " " + middlePoint + " " + nextPoint);
1543 return updateTurnControlPointTurn(tcp, dir1, dir2);
1546 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d dir1, Vector3d dir2) {
1547 double turnAngle = dir1.angle(dir2);
1549 double angle = Math.PI - turnAngle;
1551 Vector3d turnAxis = new Vector3d();
1552 turnAxis.cross(dir1, dir2);
1553 if (turnAxis.lengthSquared() > MathTools.NEAR_ZERO) {
1554 double elbowRadius = tcp.getPipelineComponent().getPipeRun().getTurnRadius();
1555 double R = elbowRadius / Math.tan(angle * 0.5);
1557 turnAxis.normalize();
1558 tcp.setTurnAngle(turnAngle);
1559 tcp.setLength(R);// setComponentOffsetValue(R);
1560 tcp.setTurnAxis(turnAxis);
1561 // tcp.setPosition(tcp.getPosition());
1564 tcp.setTurnAngle(0.0);
1566 tcp.setTurnAxis(MathTools.Y_AXIS);
1568 updateControlPointOrientation(tcp);
1570 System.out.println("PipingTools.updateTurnControlPointTurn " + dir1 + " " + dir2 + " " + turnAngle + " " + turnAxis);
1574 public static List<PipeControlPoint> getControlPoints(PipeRun pipeRun) {
1575 List<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
1576 if (pipeRun.getControlPoints().size() == 0)
1578 PipeControlPoint pcp = pipeRun.getControlPoints().iterator().next();
1579 while (pcp.getPrevious() != null) {
1580 PipeControlPoint prev = pcp.getPrevious();
1581 if (prev.getPipeRun() != pipeRun)
1585 if (pcp.isDualSub()) {
1586 pcp = pcp.getParentPoint();
1589 while (pcp.getNext() != null) {
1590 pcp = pcp.getNext();
1591 if (pcp.getPipeRun() != pipeRun)
1598 public static void reverse(PipeRun pipeRun) {
1599 List<PipeControlPoint> list = getControlPoints(pipeRun);
1600 if (list.size() <= 1)
1601 return; // nothing to do.
1603 for (int i = 0 ; i < list.size(); i++) {
1604 boolean first = i == 0;
1605 boolean last = i == list.size() - 1;
1606 PipeControlPoint current = list.get(i);
1607 PipeControlPoint currentSub = null;
1608 if (current.isDualInline())
1609 currentSub = current.getSubPoint().get(0);
1611 PipeControlPoint next = list.get(i+1);
1612 if (next.isDualInline())
1613 next = next.getSubPoint().get(0);
1614 current.setNext(null);
1615 current.setPrevious(next);
1616 if (currentSub != null) {
1617 currentSub.setNext(null);
1618 currentSub.setPrevious(next);
1621 PipeControlPoint prev = list.get(i-1);
1623 current.setPrevious(null);
1624 current.setNext(prev);
1626 if (currentSub != null) {
1627 currentSub.setPrevious(null);
1628 currentSub.setNext(prev);
1631 PipeControlPoint prev = list.get(i-1);
1632 PipeControlPoint next = list.get(i+1);
1633 if (next.isDualInline())
1634 next = next.getSubPoint().get(0);
1637 current.setPrevious(next);
1638 current.setNext(prev);
1640 if (currentSub != null) {
1641 currentSub.setPrevious(next);
1642 currentSub.setNext(prev);
1649 public static void merge(PipeRun run1, PipeRun r2) {
1650 Map<PipeControlPoint, Vector3d> positions = new HashMap<PipeControlPoint, Vector3d>();
1651 Map<PipeControlPoint, Quat4d> orientations = new HashMap<PipeControlPoint, Quat4d>();
1652 for (PipeControlPoint pcp : r2.getControlPoints()) {
1653 positions.put(pcp, pcp.getWorldPosition());
1654 orientations.put(pcp, pcp.getWorldOrientation());
1656 for (PipeControlPoint pcp : r2.getControlPoints()) {
1657 r2.deattachChild(pcp);
1659 PipelineComponent component = pcp.getPipelineComponent();
1660 if (component != null) {
1661 if (!(component instanceof Nozzle)) {
1662 component.deattach();
1663 run1.addChild(component);
1665 Nozzle n = (Nozzle)component;
1674 public static void validate(PipeRun pipeRun) {
1675 if (pipeRun == null)
1677 Collection<PipeControlPoint> pcps = pipeRun.getControlPoints();
1679 for (PipeControlPoint pcp : pcps) {
1680 if (pcp.getParentPoint() == null || pcp.getParentPoint().getPipeRun() != pipeRun)
1683 List<PipeControlPoint> runPcps = getControlPoints(pipeRun);
1684 if (runPcps.size() != count) {
1685 System.out.println("Run is not connected");
1687 for (PipeControlPoint pcp : pcps) {
1688 if (!pcp.isDirected() && pcp.getNext() == null && pcp.getPrevious() == null)
1689 System.out.println("Orphan undirected " + pcp);
1691 for (PipeControlPoint pcp : pcps) {
1692 if (pcp.getParentPoint() == null) {
1693 PipeControlPoint sub = null;
1694 if (pcp.isDualInline())
1695 sub = pcp.getSubPoint().get(0);
1696 PipeControlPoint next = pcp.getNext();
1697 PipeControlPoint prev = pcp.getPrevious();
1699 if (!(next.getPrevious() == pcp || next.getPrevious() == sub)) {
1700 System.out.println("Inconsistency between " + pcp + " -> " +next );
1704 PipeControlPoint prevParent = null;
1705 if (prev.isDualSub()) {
1706 prevParent = prev.getParentPoint();
1707 } else if (prev.isDualInline()) {
1708 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1710 if (!(prev.getNext() == pcp && (prevParent == null || prevParent.getNext() == pcp))) {
1711 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1718 public static void splitVariableLengthComponent(PipelineComponent newComponent, InlineComponent splittingComponent, boolean assignPos) throws Exception{
1719 assert(!splittingComponent.getControlPoint().isFixed());
1720 assert(!(newComponent instanceof InlineComponent && !newComponent.getControlPoint().isFixed()));
1721 PipeControlPoint newCP = newComponent.getControlPoint();
1722 PipeControlPoint splittingCP = splittingComponent.getControlPoint();
1723 PipeControlPoint nextCP = splittingCP.getNext();
1724 PipeControlPoint prevCP = splittingCP.getPrevious();
1726 /* there are many different cases to insert new component when
1727 it splits existing VariableLengthinlineComponent.
1729 1. VariableLengthComponet is connected from both sides:
1730 - insert new component between VariableLength component and component connected to it
1731 - insert new VariableLengthComponent between inserted component and component selected in previous step
1733 2. VariableLengthComponent is connected from one side
1734 - Use previous case or:
1735 - Insert new component to empty end
1736 - Insert new VariableLength component to inserted components empty end
1738 3. VariableLength is not connected to any component.
1739 - Should not be possible, at least in current implementation.
1740 - Could be done using second case
1744 if (nextCP == null && prevCP == null) {
1745 // this should not be possible
1746 throw new RuntimeException("VariableLengthComponent " + splittingComponent + " is not connected to anything.");
1748 double reservedLength = splittingComponent.getControlPoint().getLength();
1749 double newLength = newComponent.getControlPoint().getLength();
1752 Point3d next = new Point3d();
1753 Point3d prev = new Point3d();
1754 splittingCP.getInlineControlPointEnds(prev, next);
1756 Vector3d newPos = null;
1758 newPos = new Vector3d(prev);
1759 Vector3d dir = new Vector3d(next);
1763 newComponent.setWorldPosition(newPos);
1765 newPos = newComponent.getWorldPosition();
1770 Vector3d dir = new Vector3d(next);
1773 dir.scale(newLength * 0.5);
1774 Point3d vn = new Point3d(newPos);
1775 Point3d vp = new Point3d(newPos);
1778 double ln = vn.distance(next);
1779 double lp = vp.distance(prev);
1780 vp.interpolate(prev, 0.5);
1781 vn.interpolate(next, 0.5);
1784 PipeControlPoint newVariableLengthCP = null;//insertStraight(pcp1, pcp2, pos, length);
1785 if (nextCP == null) {
1786 newCP.insert(splittingCP, Direction.NEXT);
1787 newVariableLengthCP = insertStraight(newCP, Direction.NEXT, new Vector3d(vn), ln);
1788 splittingCP.setWorldPosition(new Vector3d(vp));
1789 // ControlPointTools.setWorldPosition(splittingCP, vp);
1790 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1791 } else if (prevCP == null) {
1792 newCP.insert(splittingCP, Direction.PREVIOUS);
1793 newVariableLengthCP = insertStraight(newCP, Direction.PREVIOUS, new Vector3d(vp), lp);
1794 splittingCP.setWorldPosition(new Vector3d(vn));
1795 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, ln);
1797 newCP.insert(splittingCP, nextCP);
1798 newVariableLengthCP = insertStraight(newCP, nextCP, new Vector3d(vn), ln);
1799 splittingCP.setWorldPosition(new Vector3d(vp));
1800 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1802 positionUpdate(newCP);
1806 public static void addSizeChange(boolean reversed, PipeRun pipeRun, PipeRun other, InlineComponent reducer, PipeControlPoint previous, PipeControlPoint next) {
1807 PipeControlPoint pcp = reducer.getControlPoint();
1808 PipeControlPoint ocp = pcp.getSubPoint().get(0);
1810 String name = pipeRun.getUniqueName("Reducer");
1811 reducer.setName(name);
1812 pipeRun.addChild(reducer);
1813 other.addChild(ocp);
1814 reducer.setAlternativePipeRun(other);
1816 previous.setNext(pcp);
1817 pcp.setPrevious(previous);
1818 ocp.setPrevious(previous);
1822 next.setPrevious(ocp);
1825 String name = other.getUniqueName("Reducer");
1826 reducer.setName(name);
1827 other.addChild(reducer);
1828 pipeRun.addChild(ocp);
1829 reducer.setAlternativePipeRun(pipeRun);
1833 pcp.setPrevious(next);
1834 ocp.setPrevious(next);
1836 pcp.setNext(previous);
1837 ocp.setNext(previous);
1838 previous.setPrevious(ocp);