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 boolean asDirected(PipeControlPoint pcp, Direction direction) {
447 if (pcp.isDirected())
449 if (pcp.isTurn() && pcp.isFixed()) {
450 if (!pcp._getReversed())
451 return direction == Direction.NEXT;
453 return direction == Direction.PREVIOUS;
458 private static Vector3d direction(PipeControlPoint pcp, Direction direction) {
459 return pcp.getDirection(direction);
462 private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
464 if (asDirected(u.start, Direction.NEXT))
466 if (asDirected(u.end, Direction.PREVIOUS))
470 updateFreePathLeg(u, lengthChange);
473 updateDirectedPathLeg(u, lengthChange);
476 updateDualDirectedPathLeg(u, lengthChange);
482 private static void updateFreePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
484 System.out.println("PipingRules.updateFreePipeRun " + u + " " + lengthChange);
485 checkExpandPathLeg(u, lengthChange);
486 if (u.start.isInline() || u.end.isInline() || u.start.isFixed() || u.end.isFixed())
487 processPathLeg(u, true, false);
490 private static void updateInlineControlPoints(UpdateStruct2 u, boolean checkSizes) throws Exception{
492 System.out.println("PipingRules.updateInlineControlPoints() " + u);
494 Vector3d start = new Vector3d(u.startPoint);
495 Vector3d end = new Vector3d(u.endPoint);
498 // create offsets for leg ends.
499 MathTools.mad(start, u.dir, u.start.getInlineLength());
500 MathTools.mad(end, u.dir, -u.end.getInlineLength());
503 boolean recalcline = false;
507 for (PipeControlPoint icp : u.list) {
508 updateInlineControlPoint(icp, start, end, u.dir);
510 if (icp.isOffset()) {
511 // TODO : offset vector is already calculated and should be cached
512 Vector3d off = icp.getSizeChangeOffsetVector(u.dir);
513 updateOffsetPoint(icp, off);
519 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
520 pathLegPoints.add(u.start);
521 for (PipeControlPoint icp : u.list) {
522 // updateInlineControlPoint(icp, u.startPoint,
523 // u.endPoint,u.dir);
524 updateBranchControlPointBranches(icp);
525 pathLegPoints.add(icp);
527 pathLegPoints.add(u.end);
529 // TODO : values can be cached in the loop
530 for (int i = 0; i < pathLegPoints.size(); i++) {
531 PipeControlPoint icp = pathLegPoints.get(i);
533 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
534 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
536 if (icp.isVariableLength()) {
537 if (prev != null && next != null) {
539 recalcline = recalcline | updateVariableLength(icp, prev, next);
542 // this is variable length component at the end of the
544 // the problem is that we want to keep unconnected end
545 // of the component in the same
546 // place, but center of the component must be moved.
547 updateVariableLengthEnd(icp, prev != null ? prev : next);
551 } else if (prev != null && !prev.isVariableLength()) {
552 // If this and previous control point are not variable
553 // length pcps, we'll have to check if there is no empty
554 // space between them.
555 // I there is, we'll have to create new variable length
556 // component between them.
557 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
560 } else { // with offset
561 Vector3d sp = new Vector3d(start);
562 Vector3d ep = new Vector3d(end);
565 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
566 pathLegPoints.add(u.start);
568 for (PipeControlPoint icp : u.list) {
569 updateInlineControlPoint(icp, sp, ep, u.dir);
570 updateBranchControlPointBranches(icp);
571 pathLegPoints.add(icp);
572 if (icp.isOffset()) {
573 // TODO : offset vector is already calculated and should be
575 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
576 updateOffsetPoint(icp, offset);
581 pathLegPoints.add(u.end);
586 sp = new Vector3d(u.startPoint);
587 ep = new Vector3d(u.endPoint);
590 for (int i = 0; i < pathLegPoints.size(); i++) {
591 PipeControlPoint icp = pathLegPoints.get(i);
593 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
594 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
596 if (prev != null && prev.isDualInline())
597 prev = prev.getSubPoint().get(0);
600 if (icp.isVariableLength()) {
601 if (prev != null && next != null) {
602 recalcline = recalcline | updateVariableLength(icp, prev, next);
605 // this is variable length component at the end of the
607 // the problem is that we want to keep unconnected end
608 // of the component in the same
609 // place, but center of the component must be moved.
610 updateVariableLengthEnd(icp, prev != null ? prev : next);
612 } else if (prev != null && !prev.isVariableLength()) {
613 // If this and previous control point are not variable
614 // length pcps, we'll have to check if there is no empty
615 // space between them.
616 // I there is, we'll have to create new variable length
617 // component between them.
618 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
620 if (icp.isOffset()) {
621 // TODO : offset vector is already calculated and should be
623 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
631 u.start.findNextEnd(u.list);
635 private static boolean updateVariableLength(PipeControlPoint icp, PipeControlPoint prev, PipeControlPoint next) {
636 Vector3d prevPos = prev.getWorldPosition();
637 Vector3d nextPos = next.getWorldPosition();
639 Vector3d dir = new Vector3d(nextPos);
641 double l = dir.lengthSquared(); // distance between
644 double l2prev = prev.getInlineLength(); // distance
648 double l2next = next.getInlineLength();
649 double l2 = l2prev + l2next;
650 double l2s = MathTools.square(l2);
651 if (l2s < l) { // check if there is enough space for
652 // variable length component.
655 double length = Math.sqrt(l) - l2; // true length of
659 dir.scale(length * 0.5 + l2prev); // calculate
664 icp.setWorldPosition(dir);
665 icp.setLength(length);
668 // components leave no space to the component and it
671 if (icp.isDeletable()) {
673 System.out.println("PipingRules.updateVariableLength removing " + icp);
681 private static boolean possibleVaribleLengthInsert(PipeControlPoint icp, PipeControlPoint prev) throws Exception{
682 Vector3d currentPos = icp.getWorldPosition();
683 Vector3d prevPos = prev.getWorldPosition();
684 Vector3d dir = new Vector3d(currentPos);
686 double l = dir.lengthSquared();
687 double l2prev = prev.getInlineLength();
688 double l2next = icp.getInlineLength();
689 double l2 = l2prev + l2next;
690 double l2s = l2 * l2;
692 if (allowInsertRemove) {
694 double length = Math.sqrt(l) - l2; // true length of the
697 dir.scale(length * 0.5 + l2prev); // calculate center
701 PipeControlPoint scp = insertStraight(prev, icp, dir, length);
710 private static void updateVariableLengthEnd(PipeControlPoint icp, PipeControlPoint prev) {
711 Vector3d currentPos = icp.getWorldPosition();
712 Vector3d prevPos = prev.getWorldPosition();
714 Vector3d dir = new Vector3d();
715 dir.sub(currentPos, prevPos);
717 boolean simple = true;
719 double currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
720 icp.setLength(currentLength);
722 double currentLength = icp.getLength();
723 if (currentLength < MathTools.NEAR_ZERO) {
724 currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
727 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
729 Point3d endPos = new Point3d(dir);
730 endPos.scale(currentLength * 0.5);
731 endPos.add(currentPos); // this is the free end of the component
733 double offset = prev.getInlineLength();
734 Point3d beginPos = new Point3d(dir);
735 beginPos.scale(offset);
736 beginPos.add(prevPos); // this is the connected end of the component
738 double l = beginPos.distance(endPos);
741 System.out.println("Length for " + icp + " is NaN");
744 beginPos.add(dir); // center position
747 System.out.println("PipingRules.updateInlineControlPoints() setting variable length to " + l);
750 icp.setWorldPosition(new Vector3d(beginPos));
754 private static void ppNoOffset(UpdateStruct2 u) throws Exception {
756 System.out.println("PipingRules.ppNoOffset() " + u);
757 Vector3d offset = new Vector3d();
760 for (PipeControlPoint icp : u.list) {
761 if (icp.isOffset()) {
762 offset.add(icp.getSizeChangeOffsetVector(u.dir));
763 } else if (icp.isDualSub())
764 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
768 checkExpandPathLeg(u, PathLegUpdateType.NONE);
771 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 {
773 System.out.println("PipingRules.ppNoDir() " + start + " " + end + " " + iter + " " + toRemove.size());
774 // FIXME : extra loop (dir should be calculated here)
775 Vector3d dir = new Vector3d();
776 Vector3d offset = new Vector3d();
777 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
778 ppNoOffset(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, null, hasOffsets, iter, reversed, toRemove, updated));
781 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
782 checkExpandPathLeg(u, lengthChange, u.updated.isInline() && u.updated.isOffset());
785 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange, boolean updateEnds) throws Exception {
787 System.out.println("PipingRules.checkExpandPathLeg() " + u + " " + lengthChange);
788 if (lengthChange != PathLegUpdateType.NONE) {
789 // FIXME : turns cannot be checked before inline cps are updated,
790 // since their position affects calculation of turns
791 processPathLeg(u, updateEnds, false);
792 int type = checkTurns(u, lengthChange);
793 if (type == REMOVE_NONE) {
794 processPathLeg(u, updateEnds, true);
796 expandPathLeg(u, type);
799 processPathLeg(u, updateEnds, true);
803 private static void updateDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
805 System.out.println("PipingRules.updateDirectedPipeRun() " + u + " " + lengthChange);
806 PipeControlPoint dcp;
807 PipeControlPoint other;
808 boolean canMoveOther = false;
809 boolean dcpStart = false;
810 boolean inlineEnd = false;
812 if (asDirected(u.start, Direction.NEXT)) {
815 position = u.startPoint;
819 inlineEnd = u.end.isInline();
824 position = u.endPoint;
827 inlineEnd = u.start.isInline();
830 Vector3d directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
831 if (directedDirection == null) {
832 updateTurnControlPointTurn(dcp, dcp.getPrevious(), dcp.getNext());
833 directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
834 if (directedDirection == null) {
838 Point3d directedEndPoint = new Point3d(u.endPoint);
840 directedEndPoint.add(u.offset);
842 double mu[] = new double[2];
845 Vector3d t = new Vector3d();
848 closest = MathTools.closestPointOnStraight(directedEndPoint, u.startPoint, directedDirection, mu);
849 t.sub(closest, directedEndPoint);
851 closest = MathTools.closestPointOnStraight(u.startPoint, directedEndPoint, directedDirection, mu);
852 t.sub(closest, u.startPoint);
855 double distance = t.length();
856 boolean aligned = (distance < ALLOWED_OFFSET);
858 if (u.start.isInline() || u.end.isInline() || u.start.isFixed() || u.end.isFixed())
859 processPathLeg(u, true, false);
860 checkExpandPathLeg(u, lengthChange, inlineEnd);
866 PipeControlPoint nextToMoved;
868 if (u.list.size() > 0)
870 nextToMoved = u.list.get(0);
872 nextToMoved = u.list.get(u.list.size() - 1);
876 nextToMoved = u.start;
877 if (other.isVariableAngle()) {
879 // TODO calculate needed space from next run end.
882 closest.set(u.startPoint);
884 closest.set(u.endPoint);
886 Vector3d v = new Vector3d(directedDirection);
887 v.scale(spaceForTurn(other));
893 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + closest);
894 other.setWorldPosition(closest);
896 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));
897 if (u.end.getNext() != null)
898 updatePathLegNext(u.end, u.updated, PathLegUpdateType.NEXT);
900 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));
901 if (u.start.getPrevious() != null)
902 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.PREV);
905 // TODO : calculate needed space from next run end.
906 if (allowInsertRemove)
907 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
912 } else if (other.isNonDirected() && other.getParentPoint() != null) {
913 // FIXME : this code was for updating branches
914 Vector3d bintersect = new Vector3d();
915 PipeControlPoint bcp = other.getParentPoint();
916 if (bcp != null && canMoveOther) {
917 Point3d bstart = new Point3d();
918 Point3d bend = new Point3d();
919 Vector3d bdir = new Vector3d();
920 bcp.getInlineControlPointEnds(bstart, bend, bdir);
921 Vector3d nintersect = new Vector3d();
923 MathTools.intersectStraightStraight(position, directedDirection, bend, bdir, nintersect, bintersect, mu);
924 Vector3d dist = new Vector3d(nintersect);
925 dist.sub(bintersect);
926 canMoveOther = mu[1] > 0.0 && mu[1] < 1.0 && dist.lengthSquared() < 0.01;
928 // TODO : endControlPoints are undirected: calculcate
929 // correct position for it
930 throw new UnsupportedOperationException("not implemented");
934 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + bintersect);
935 // is required branch position is in possible range
936 bcp.setWorldPosition(bintersect);
938 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);
940 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);
943 // branch cannot be moved into right position, new turn
944 // / elbow must be inserted
945 if (allowInsertRemove)
946 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
951 } else { // assume that control point cannot be moved, but can
953 if (allowInsertRemove)
954 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
964 private static void updateDualDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
966 System.out.println("PipingRules.updateDualDirectedPipeRun() " + u + " " + lengthChange);
968 PipeControlPoint dcp1 = u.start;
969 PipeControlPoint dcp2 = u.end;
970 Point3d position1 = new Point3d(u.startPoint);
971 Point3d position2 = new Point3d(u.endPoint);
972 Point3d position1offset = new Point3d(position1);
973 position1offset.sub(u.offset);
974 Point3d position2offset = new Point3d(position2);
975 position2offset.add(u.offset);
976 Vector3d dir1 = direction(dcp1, Direction.NEXT);
977 Vector3d dir2 = direction(dcp2, Direction.PREVIOUS);
978 Vector3d p1 = MathTools.closestPointOnStraight(position1offset, position2, dir2);
979 Vector3d p2 = MathTools.closestPointOnStraight(position2offset, position1, dir1);
980 double d1 = position1.distance(new Point3d(p1));
981 double d2 = position2.distance(new Point3d(p2));
983 boolean aligned = (d1 < ALLOWED_OFFSET && d2 < ALLOWED_OFFSET);
989 } else if (allowInsertRemove){
990 PipeControlPoint dcp;
991 PipeControlPoint next;
994 if (u.list.size() > 0)
995 next = u.list.get(0);
1000 if (u.list.size() > 0)
1001 next = u.list.get(u.list.size() - 1);
1006 p1 = dcp.getWorldPosition();
1007 // FIXME: calculate position of the elbows properly.
1014 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position2, dir2);
1016 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position1, dir1);
1019 PipeControlPoint tcp1 = insertElbow(dcp, next, p1);
1020 PipeControlPoint tcp2 = insertElbow(tcp1, next, p2);
1023 System.out.println("PipingRules.updateDualDirectedPipeRun() created two turns " + tcp1 + " " + tcp2);
1026 Vector3d dd = new Vector3d(p2);
1029 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1030 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1032 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1034 updatePathLegPrev(tcp2, u.updated, PathLegUpdateType.NONE);
1036 Vector3d dd = new Vector3d(p1);
1039 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1040 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1042 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1044 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.NONE);
1053 private static double spaceForTurn(PipeControlPoint tcp) {
1054 // TODO : this returns now space for 90 deg turn.
1055 // The challenge: position of tcp affects the turn angle, which then affects the required space. Perhaps we need to iterate...
1056 // Additionally, if the path legs contain offset, using just positions of opposite path leg ends is not enough,
1057 return tcp.getPipeRun().getTurnRadius();
1060 private static void insertElbowUpdate(UpdateStruct2 u, PipeControlPoint dcp, PipeControlPoint next, boolean dcpStart, Vector3d position, Vector3d directedDirection) throws Exception{
1063 // Vector3d closest = new Vector3d(position);
1064 // closest.add(directedDirection);
1066 PipeControlPoint tcp = null;
1069 closest = MathTools.closestPointOnStraight(next.getWorldPosition(), position, directedDirection);
1070 tcp = insertElbow(dcp, next, closest);
1072 closest = MathTools.closestPointOnStraight(dcp.getWorldPosition(), position, directedDirection);
1073 tcp = insertElbow(next, dcp, closest);
1075 // TODO properly calculate required distance between start and inserted elbow.
1076 double d = MathTools.distance(position, closest);
1077 double s = spaceForTurn(tcp);
1080 Vector3d p = new Vector3d(directedDirection);
1090 System.out.println("PipingRules.updateDirectedPipeRun() inserted " + tcp);
1093 // update pipe run from new turn to other end
1094 ppNoDir(tcp, new Vector3d(closest), u.list, u.end, u.endPoint, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1095 // update pipe run from directed to new turn
1096 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));
1098 // update pipe run from other end to new turn
1099 ppNoDir(u.start, u.startPoint, u.list, tcp, new Vector3d(closest), u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1100 // update pipe run from new turn to directed
1101 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));
1106 * Checks if turns can be removed (turn angle near zero)
1108 private static int checkTurns(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1110 System.out.println("PipingRules.checkTurns() " + u.start + " " + u.end);
1111 boolean startRemoved = false;
1112 boolean endRemoved = false;
1113 if (u.start.isVariableAngle()) {
1114 // this won't work properly if inline control points are not updated
1115 PipeControlPoint startPrev = u.start.getPrevious();
1116 if (startPrev != null) {
1118 if (!u.hasOffsets) {
1119 a = updateTurnControlPointTurn(u.start, startPrev, u.end);
1121 Vector3d ep = new Vector3d(u.endPoint);
1123 a = updateTurnControlPointTurn(u.start, u.startPoint, startPrev.getPosition(), ep);
1126 if (a < MIN_TURN_ANGLE && u.start.isDeletable())
1127 startRemoved = true;
1128 else if (lengthChange == PathLegUpdateType.PREV || lengthChange == PathLegUpdateType.PREV_S) {
1129 PathLegUpdateType type;
1130 if (lengthChange == PathLegUpdateType.PREV_S)
1131 type = PathLegUpdateType.PREV;
1133 type = PathLegUpdateType.NONE;
1134 updatePathLegPrev(u.start, u.start, type);
1138 if (u.end.isVariableAngle()) {
1140 PipeControlPoint endNext = u.end.getNext();
1141 if (endNext != null) {
1143 if (!u.hasOffsets) {
1144 a = updateTurnControlPointTurn(u.end, u.start, endNext);
1146 Vector3d sp = new Vector3d(u.startPoint);
1148 a = updateTurnControlPointTurn(u.end, u.endPoint, sp, endNext.getPosition());
1150 if (a < MIN_TURN_ANGLE && u.end.isDeletable())
1152 else if (lengthChange == PathLegUpdateType.NEXT || lengthChange == PathLegUpdateType.NEXT_S) {
1153 PathLegUpdateType type;
1154 if (lengthChange == PathLegUpdateType.NEXT_S)
1155 type = PathLegUpdateType.NEXT;
1157 type = PathLegUpdateType.NONE;
1158 updatePathLegNext(u.end, u.end, type);
1163 System.out.println("PipingRules.checkTurns() res " + startRemoved + " " + endRemoved);
1164 if (!startRemoved && !endRemoved)
1166 if (startRemoved && endRemoved)
1169 return REMOVE_START;
1174 * Expands piperun search over turns that are going to be removed
1177 private static void expandPathLeg(UpdateStruct2 u, int type) throws Exception {
1179 System.out.println("PipingRules.expandPipeline " + u.start + " " + u.end);
1180 ArrayList<PipeControlPoint> newList = new ArrayList<PipeControlPoint>();
1183 throw new RuntimeException("Error in piping rules");
1185 u.toRemove.add(new ExpandIterInfo(u.start, REMOVE_START));
1186 u.start = u.start.findPreviousEnd();
1187 u.startPoint = u.start.getPosition();
1188 u.start.findNextEnd(newList);
1189 newList.addAll(u.list);
1193 u.toRemove.add(new ExpandIterInfo(u.end, REMOVE_END));
1194 u.end = u.end.findNextEnd(newList);
1195 u.endPoint = u.end.getPosition();
1196 u.list.addAll(newList);
1199 u.toRemove.add(new ExpandIterInfo(u.start, u.end));
1200 u.start = u.start.findPreviousEnd();
1201 u.startPoint = u.start.getPosition();
1202 u.start.findNextEnd(newList);
1203 newList.addAll(u.list);
1205 newList = new ArrayList<PipeControlPoint>();
1206 u.end = u.end.findNextEnd(newList);
1207 u.endPoint = u.end.getPosition();
1208 u.list.addAll(newList);
1211 throw new RuntimeException("Error in piping rules");
1214 u.offset = new Vector3d();
1217 for (PipeControlPoint icp : u.list) {
1218 if (icp.isOffset()) {
1219 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1220 } else if (icp.isDualSub())
1221 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1225 System.out.println("PipingRules.expandPipeline expanded " + u.start + " " + u.end);
1227 updatePathLeg(u, PathLegUpdateType.NONE);
1231 * reverts one iteration of turn removing back)
1233 private static void backIter(UpdateStruct2 u) throws Exception {
1236 System.out.println("PipingRules.backIter" + u.start + " " + u.end);
1238 throw new RuntimeException("Error in piping rules");
1239 ExpandIterInfo info = u.toRemove.get(u.toRemove.size() - 1);
1240 u.toRemove.remove(u.toRemove.size() - 1);
1241 if (info.getType() == REMOVE_START || info.getType() == REMOVE_BOTH) {
1242 while (u.list.size() > 0) {
1243 PipeControlPoint icp = u.list.get(0);
1244 if (icp.getPrevious().equals(info.getStart()))
1248 u.start = info.getStart();
1250 if (info.getType() == REMOVE_END || info.getType() == REMOVE_BOTH) {
1251 while (u.list.size() > 0) {
1252 PipeControlPoint icp = u.list.get(u.list.size() - 1);
1253 if (icp.getNext().equals(info.getEnd()))
1257 u.end = info.getEnd();
1259 u.offset = new Vector3d();
1262 for (PipeControlPoint icp : u.list) {
1263 if (icp.isOffset()) {
1264 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1265 } else if (icp.isDualSub())
1266 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1274 * Processes pipe run (removes necessary turns and updates run ends)
1276 // private static void processPathLeg(PipeControlPoint start, Point3d
1277 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1278 // end,Point3d endPoint, Vector3d dir,Vector3d offset, boolean
1279 // hasOffsets,int iter, boolean reversed, ArrayList<ExpandIterInfo>
1280 // toRemove) throws TransactionException {
1282 private static void processPathLeg(UpdateStruct2 u) throws Exception {
1284 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1285 processPathLeg(u, true, true);
1288 private static void processPathLeg(UpdateStruct2 u, boolean updateEnds, boolean updateInline) throws Exception {
1290 System.out.println("PipingRules.processPathLeg " + (updateEnds ? "ends " : "") + (updateInline ? "inline " : "") + u.start + " " + u.end);
1292 if (u.toRemove.size() > 0) {
1293 for (ExpandIterInfo info : u.toRemove) {
1294 if (info.getStart() != null) {
1296 System.out.println("PipingRules.processPathLeg removing start " + info.getStart());
1297 info.getStart()._remove();
1299 if (info.getEnd() != null) {
1301 System.out.println("PipingRules.processPathLeg removing end " + info.getEnd());
1302 info.getEnd()._remove();
1305 // ControlPointTools.removeControlPoint may remove more than one CP;
1306 // we must populate inline CP list again.
1308 u.start.findNextEnd( u.list);
1310 // FIXME : inline CPs are update twice because their positions must be
1311 // updated before and after ends.
1312 updateInlineControlPoints(u, false);
1315 if (u.start.isTurn()) {
1316 updateTurnControlPointTurn(u.start, u.start.getPrevious(), u.start.getNext());
1317 // updatePathLegPrev(u.start, u.start, PathLegUpdateType.NONE);
1318 } else if (u.start.isEnd()) {
1319 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1320 } else if (u.start.isInline()) {
1321 updateControlPointOrientation(u.start);
1323 if (u.end.isTurn()) {
1324 updateTurnControlPointTurn(u.end, u.end.getPrevious(), u.end.getNext());
1325 // updatePathLegNext(u.end, u.end, PathLegUpdateType.NONE);
1326 } else if (u.end.isEnd()) {
1327 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1328 } else if (u.end.isInline()) {
1329 updateControlPointOrientation(u.end);
1333 if (u.start.isEnd()) {
1334 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1336 if (u.end.isEnd()) {
1337 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1341 updateInlineControlPoints(u, true);
1346 * Processes pipe run and recalculates offset
1348 // private static void processPathLeg(PipeControlPoint start, Point3d
1349 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1350 // end,Point3d endPoint, Vector3d dir, boolean hasOffsets,int iter, boolean
1351 // reversed, ArrayList<ExpandIterInfo> toRemove) throws TransactionException
1353 private static void processPathLegNoOffset(UpdateStruct2 u) throws Exception {
1355 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1356 Vector3d offset = new Vector3d();
1359 for (PipeControlPoint icp : u.list) {
1360 if (icp.isOffset()) {
1361 offset.add(icp.getSizeChangeOffsetVector(u.dir));
1362 } else if (icp.isDualSub()) {
1363 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1370 private static void updateOffsetPoint(PipeControlPoint sccp, Vector3d offset) {
1371 Vector3d world = sccp.getWorldPosition();
1373 PipeControlPoint ocp = sccp.getSubPoint().iterator().next();
1374 ocp.setWorldPosition(world);
1378 * Updates InlineControlPoints position when straight pipe's end(s) have
1386 private static void updateInlineControlPoint(PipeControlPoint icp, Vector3d prev, Vector3d next, Vector3d dir) {
1388 System.out.println("PipingRules.updateInlineControlPoint() " + icp);
1390 Vector3d inlinePoint = icp.getWorldPosition();
1391 Vector3d prevPoint = new Vector3d(prev);
1392 Vector3d nextPoint = new Vector3d(next);
1393 if (!icp.isVariableLength()) {
1394 // Reserve space for fixed length components.
1395 MathTools.mad(prevPoint, dir, icp.getInlineLength());
1396 MathTools.mad(nextPoint, dir, -icp.getInlineLength());
1397 if (MathTools.distance(prevPoint, nextPoint) < ALLOWED_OFFSET) {
1402 boolean canCalc = MathTools.distance(prevPoint, nextPoint) > ALLOWED_OFFSET;
1404 System.out.print("InlineControlPoint update " + icp + " " + inlinePoint + " " + prevPoint + " " + nextPoint);
1405 Vector3d newInlinePoint = null;
1407 boolean branchUpdate = false;
1408 PipeControlPoint becp = null;
1409 for (PipeControlPoint pcp : icp.getSubPoint())
1410 if (pcp.isNonDirected()) {
1411 branchUpdate = true;
1416 if (DUMMY || !branchUpdate) {
1417 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1421 // FIXME : can only handle one branch
1422 PipeControlPoint p = null;
1423 if (becp.getNext() != null) {
1424 p = becp.findNextEnd();
1425 } else if (becp.getPrevious() != null) {
1426 p = becp.findPreviousEnd();
1429 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1430 } else if (canCalc){
1431 Vector3d branchLegEnd = p.getWorldPosition();
1432 Vector3d dir2 = new Vector3d(inlinePoint);
1433 dir2.sub(branchLegEnd);
1434 Vector3d dir1 = new Vector3d(nextPoint);
1435 dir1.sub(prevPoint);
1436 newInlinePoint = new Vector3d();
1437 double mu[] = new double[2];
1438 MathTools.intersectStraightStraight(new Vector3d(prevPoint), dir1, new Vector3d(branchLegEnd), dir2, newInlinePoint, new Vector3d(), mu);
1440 System.out.println(mu[0]);
1441 // FIXME : reserve space
1443 newInlinePoint = new Vector3d(prevPoint);
1444 } else if (mu[0] > 1.0) {
1445 newInlinePoint = new Vector3d(nextPoint);
1450 // prevPoint == nextPoint
1451 newInlinePoint = new Vector3d(prevPoint);
1454 System.out.println(" " + newInlinePoint);
1456 icp.setWorldPosition(newInlinePoint);
1457 updateControlPointOrientation(icp);
1461 * Updates InlineControlPoints position when straight pipe's end(s) have
1469 private static void updateEndComponentControlPoint(PipeControlPoint ecp, Vector3d start, Vector3d end) throws Exception {
1471 System.out.println("PipingRules.updateEndComponentControlPoint() " + ecp);
1472 // PipeControlPoint next = ecp.getNext();
1473 // PipeControlPoint prev = ecp.getPrevious();
1474 // if (next != null) {
1475 // end = G3DTools.getPoint(next.getLocalPosition());
1476 // start = G3DTools.getPoint(ecp.getLocalPosition());
1477 // } else if (prev != null) {
1478 // end = G3DTools.getPoint(ecp.getLocalPosition());
1479 // start = G3DTools.getPoint(prev.getLocalPosition());
1481 // // TODO : warning?
1484 // Vector3d dir = new Vector3d (end);
1487 // G3DTools.setTuple(ecp.getDirection(), dir);
1489 updateControlPointOrientation(ecp);
1491 for (PipeControlPoint pcp : ecp.getSubPoint()) {
1492 // TODO update position
1493 updatePathLegEndControlPoint(pcp);
1497 private static void updateControlPointOrientation(PipeControlPoint pcp) {
1498 // FIXME : hack to bypass variable length components orientation
1499 // if (pcp.getAtMostOneRelatedObject(ProcessResource.g3dResource.HasWorldOrientation) == null)
1501 // if (pcp.rotationAngle == null)
1503 Double angleO = pcp.getRotationAngle();
1507 Boolean reversedO = pcp.getReversed();
1508 boolean reversed = false;
1509 if (reversedO != null)
1510 reversed = reversedO;
1511 Quat4d q = pcp.getControlPointOrientationQuat(angle, reversed);
1512 pcp.setWorldOrientation(q);
1516 * Updates all branches when branch's position has been changed
1520 private static void updateBranchControlPointBranches(PipeControlPoint bcp) throws Exception {
1522 System.out.println("PipingRules.updateBranchControlPointBranches() " + bcp);
1523 if (bcp.isDualInline())
1525 Collection<PipeControlPoint> branches = bcp.getSubPoint();
1526 if (branches.size() == 0) {
1528 System.out.println("No Branches found");
1532 for (PipeControlPoint pcp : branches) {
1533 updatePathLegEndControlPoint(pcp);
1538 * Recalculates turn control point's internal data (turn angle and offset)
1544 private static double updateTurnControlPointTurn(PipeControlPoint tcp, PipeControlPoint prev, PipeControlPoint next) {
1546 System.out.println("PipingTools.updateTurnControlPointTurn()" + tcp);
1548 if (!tcp.isFixed()) {
1549 if (next == null || prev == null)
1550 return tcp.getTurnAngle();
1551 Vector3d middlePoint = tcp.getWorldPosition();
1552 Vector3d nextPoint = next.getWorldPosition();
1553 Vector3d prevPoint = prev.getWorldPosition();
1554 return updateTurnControlPointTurn(tcp, middlePoint, prevPoint, nextPoint);
1556 // Verify that fixed turn is properly defined.
1557 // TODO : when reversed flag is changed, rotation angle should be recalculated, otherwise the orientation will change.
1558 // TODO : should reverse flag toggle to be done already when we are removing defining side?
1559 if (prev != null && next != null) {
1561 } else if (prev == null) {
1562 if (!tcp._getReversed())
1563 tcp.setReversed(true);
1564 } else if (next == null) {
1565 if (tcp._getReversed())
1566 tcp.setReversed(false);
1570 if (!tcp._getReversed()) {
1572 return Math.PI; // FIXME : argh
1574 Vector3d middlePoint = tcp.getWorldPosition();
1575 Vector3d prevPoint = prev.getWorldPosition();
1576 dir = new Vector3d();
1577 dir.sub(middlePoint, prevPoint);
1581 return Math.PI; // FIXME : argh
1583 Vector3d middlePoint = tcp.getWorldPosition();
1584 Vector3d nextPoint = next.getWorldPosition();
1585 dir = new Vector3d();
1586 dir.sub(middlePoint, nextPoint);
1590 Quat4d q = PipeControlPoint.getControlPointOrientationQuat(dir, tcp.getRotationAngle() != null ? tcp.getRotationAngle() : 0.0);
1591 Vector3d v = new Vector3d();
1592 MathTools.rotate(q, MathTools.Y_AXIS,v);
1594 tcp.setWorldOrientation(q);
1595 return tcp.getTurnAngle();
1600 * Recalculates turn control point's internal data (turn angle and offset)
1603 * @param middlePoint
1607 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d middlePoint, Vector3d prevPoint, Vector3d nextPoint) {
1609 Vector3d dir1 = new Vector3d(middlePoint);
1610 dir1.sub(prevPoint);
1611 Vector3d dir2 = new Vector3d(nextPoint);
1612 dir2.sub(middlePoint);
1614 System.out.println("PipingTools.updateTurnControlPointTurn " + tcp + " " + prevPoint + " " + middlePoint + " " + nextPoint);
1615 return updateTurnControlPointTurn(tcp, dir1, dir2);
1618 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d dir1, Vector3d dir2) {
1619 double turnAngle = dir1.angle(dir2);
1621 double angle = Math.PI - turnAngle;
1623 Vector3d turnAxis = new Vector3d();
1624 turnAxis.cross(dir1, dir2);
1625 if (turnAxis.lengthSquared() > MathTools.NEAR_ZERO) {
1626 double elbowRadius = tcp.getPipelineComponent().getPipeRun().getTurnRadius();
1627 double R = elbowRadius / Math.tan(angle * 0.5);
1629 turnAxis.normalize();
1630 tcp.setTurnAngle(turnAngle);
1631 tcp.setLength(R);// setComponentOffsetValue(R);
1632 tcp.setTurnAxis(turnAxis);
1633 // tcp.setPosition(tcp.getPosition());
1636 tcp.setTurnAngle(0.0);
1638 tcp.setTurnAxis(new Vector3d(MathTools.Y_AXIS));
1640 updateControlPointOrientation(tcp);
1642 System.out.println("PipingTools.updateTurnControlPointTurn " + dir1 + " " + dir2 + " " + turnAngle + " " + turnAxis);
1646 public static List<PipeControlPoint> getControlPoints(PipeRun pipeRun) {
1647 List<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
1648 if (pipeRun.getControlPoints().size() == 0)
1650 PipeControlPoint pcp = pipeRun.getControlPoints().iterator().next();
1651 while (pcp.getPrevious() != null) {
1652 PipeControlPoint prev = pcp.getPrevious();
1653 if (prev.getPipeRun() != pipeRun)
1657 if (pcp.isDualSub()) {
1658 pcp = pcp.getParentPoint();
1661 while (pcp.getNext() != null) {
1662 pcp = pcp.getNext();
1663 if (pcp.getPipeRun() != pipeRun)
1670 public static void reverse(PipeRun pipeRun) {
1671 List<PipeControlPoint> list = getControlPoints(pipeRun);
1672 if (list.size() <= 1)
1673 return; // nothing to do.
1675 for (int i = 0 ; i < list.size(); i++) {
1676 boolean first = i == 0;
1677 boolean last = i == list.size() - 1;
1678 PipeControlPoint current = list.get(i);
1679 PipeControlPoint currentSub = null;
1680 if (current.isDualInline())
1681 currentSub = current.getSubPoint().get(0);
1683 PipeControlPoint next = list.get(i+1);
1684 if (next.isDualInline())
1685 next = next.getSubPoint().get(0);
1686 current.setNext(null);
1687 current.setPrevious(next);
1688 if (currentSub != null) {
1689 currentSub.setNext(null);
1690 currentSub.setPrevious(next);
1693 PipeControlPoint prev = list.get(i-1);
1695 current.setPrevious(null);
1696 current.setNext(prev);
1698 if (currentSub != null) {
1699 currentSub.setPrevious(null);
1700 currentSub.setNext(prev);
1703 PipeControlPoint prev = list.get(i-1);
1704 PipeControlPoint next = list.get(i+1);
1705 if (next.isDualInline())
1706 next = next.getSubPoint().get(0);
1709 current.setPrevious(next);
1710 current.setNext(prev);
1712 if (currentSub != null) {
1713 currentSub.setPrevious(next);
1714 currentSub.setNext(prev);
1718 if (current.isTurn() && current.isFixed()) {
1719 current.setReversed(!current._getReversed());
1721 if (current.isInline() && current.isReverse()) {
1722 current.setReversed(!current._getReversed());
1727 public static void merge(PipeRun run1, PipeRun r2) {
1728 Map<PipeControlPoint, Vector3d> positions = new HashMap<PipeControlPoint, Vector3d>();
1729 Map<PipeControlPoint, Quat4d> orientations = new HashMap<PipeControlPoint, Quat4d>();
1730 for (PipeControlPoint pcp : r2.getControlPoints()) {
1731 positions.put(pcp, pcp.getWorldPosition());
1732 orientations.put(pcp, pcp.getWorldOrientation());
1734 for (PipeControlPoint pcp : r2.getControlPoints()) {
1735 r2.deattachChild(pcp);
1737 PipelineComponent component = pcp.getPipelineComponent();
1738 if (component != null) {
1739 if (!(component instanceof Nozzle)) {
1740 component.deattach();
1741 run1.addChild(component);
1743 Nozzle n = (Nozzle)component;
1752 public static void validate(PipeRun pipeRun) {
1753 if (pipeRun == null)
1755 Collection<PipeControlPoint> pcps = pipeRun.getControlPoints();
1757 //System.out.println("Validate " + pipeRun.getName());
1758 for (PipeControlPoint pcp : pcps) {
1759 if (pcp.getParentPoint() == null || pcp.getParentPoint().getPipeRun() != pipeRun)
1762 List<PipeControlPoint> runPcps = getControlPoints(pipeRun);
1763 if (runPcps.size() != count) {
1764 System.out.println("Run " + pipeRun.getName() + " contains unconnected control points");
1766 for (PipeControlPoint pcp : pcps) {
1767 if (!pcp.isDirected() && pcp.getNext() == null && pcp.getPrevious() == null)
1768 System.out.println("Orphan undirected " + pcp);
1770 for (PipeControlPoint pcp : pcps) {
1771 if (pcp.getParentPoint() == null) {
1772 PipeControlPoint sub = null;
1773 if (pcp.isDualInline())
1774 sub = pcp.getSubPoint().get(0);
1775 PipeControlPoint next = pcp.getNext();
1776 PipeControlPoint prev = pcp.getPrevious();
1778 if (!(next.getPrevious() == pcp || next.getPrevious() == sub)) {
1779 System.out.println("Inconsistency between " + pcp + " -> " +next );
1783 PipeControlPoint prevParent = null;
1784 if (prev.isDualSub()) {
1785 prevParent = prev.getParentPoint();
1786 } else if (prev.isDualInline()) {
1787 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1789 if (!(prev.getNext() == pcp && (prevParent == null || prevParent.getNext() == pcp))) {
1790 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1797 public static void splitVariableLengthComponent(PipelineComponent newComponent, InlineComponent splittingComponent, boolean assignPos) throws Exception{
1798 assert(!splittingComponent.getControlPoint().isFixed());
1799 assert(!(newComponent instanceof InlineComponent && !newComponent.getControlPoint().isFixed()));
1800 PipeControlPoint newCP = newComponent.getControlPoint();
1801 PipeControlPoint splittingCP = splittingComponent.getControlPoint();
1802 PipeControlPoint nextCP = splittingCP.getNext();
1803 PipeControlPoint prevCP = splittingCP.getPrevious();
1805 /* there are many different cases to insert new component when
1806 it splits existing VariableLengthinlineComponent.
1808 1. VariableLengthComponet is connected from both sides:
1809 - insert new component between VariableLength component and component connected to it
1810 - insert new VariableLengthComponent between inserted component and component selected in previous step
1812 2. VariableLengthComponent is connected from one side
1813 - Use previous case or:
1814 - Insert new component to empty end
1815 - Insert new VariableLength component to inserted components empty end
1817 3. VariableLength is not connected to any component.
1818 - Should not be possible, at least in current implementation.
1819 - Could be done using second case
1823 if (nextCP == null && prevCP == null) {
1824 // this should not be possible
1825 throw new RuntimeException("VariableLengthComponent " + splittingComponent + " is not connected to anything.");
1827 double reservedLength = splittingComponent.getControlPoint().getLength();
1828 double newLength = newComponent.getControlPoint().getLength();
1831 Point3d next = new Point3d();
1832 Point3d prev = new Point3d();
1833 splittingCP.getInlineControlPointEnds(prev, next);
1835 Vector3d newPos = null;
1837 newPos = new Vector3d(prev);
1838 Vector3d dir = new Vector3d(next);
1842 newComponent.setWorldPosition(newPos);
1844 newPos = newComponent.getWorldPosition();
1849 Vector3d dir = new Vector3d(next);
1852 dir.scale(newLength * 0.5);
1853 Point3d vn = new Point3d(newPos);
1854 Point3d vp = new Point3d(newPos);
1857 double ln = vn.distance(next);
1858 double lp = vp.distance(prev);
1859 vp.interpolate(prev, 0.5);
1860 vn.interpolate(next, 0.5);
1863 PipeControlPoint newVariableLengthCP = null;//insertStraight(pcp1, pcp2, pos, length);
1864 if (nextCP == null) {
1865 newCP.insert(splittingCP, Direction.NEXT);
1866 newVariableLengthCP = insertStraight(newCP, Direction.NEXT, new Vector3d(vn), ln);
1867 splittingCP.setWorldPosition(new Vector3d(vp));
1868 // ControlPointTools.setWorldPosition(splittingCP, vp);
1869 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1870 } else if (prevCP == null) {
1871 newCP.insert(splittingCP, Direction.PREVIOUS);
1872 newVariableLengthCP = insertStraight(newCP, Direction.PREVIOUS, new Vector3d(vp), lp);
1873 splittingCP.setWorldPosition(new Vector3d(vn));
1874 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, ln);
1876 newCP.insert(splittingCP, nextCP);
1877 newVariableLengthCP = insertStraight(newCP, nextCP, new Vector3d(vn), ln);
1878 splittingCP.setWorldPosition(new Vector3d(vp));
1879 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1881 positionUpdate(newCP);
1885 public static void addSizeChange(boolean reversed, PipeRun pipeRun, PipeRun other, InlineComponent reducer, PipeControlPoint previous, PipeControlPoint next) {
1886 PipeControlPoint pcp = reducer.getControlPoint();
1887 PipeControlPoint ocp = pcp.getSubPoint().get(0);
1889 String name = pipeRun.getUniqueName("Reducer");
1890 reducer.setName(name);
1891 pipeRun.addChild(reducer);
1892 other.addChild(ocp);
1893 reducer.setAlternativePipeRun(other);
1895 previous.setNext(pcp);
1896 pcp.setPrevious(previous);
1897 ocp.setPrevious(previous);
1901 next.setPrevious(ocp);
1904 String name = other.getUniqueName("Reducer");
1905 reducer.setName(name);
1906 other.addChild(reducer);
1907 pipeRun.addChild(ocp);
1908 reducer.setAlternativePipeRun(pipeRun);
1912 pcp.setPrevious(next);
1913 ocp.setPrevious(next);
1915 pcp.setNext(previous);
1916 ocp.setNext(previous);
1917 previous.setPrevious(ocp);