1 package org.simantics.plant3d.scenegraph.controlpoint;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashMap;
9 import javax.vecmath.Point3d;
10 import javax.vecmath.Quat4d;
11 import javax.vecmath.Vector3d;
13 import org.simantics.g3d.math.MathTools;
14 import org.simantics.plant3d.scenegraph.InlineComponent;
15 import org.simantics.plant3d.scenegraph.Nozzle;
16 import org.simantics.plant3d.scenegraph.P3DRootNode;
17 import org.simantics.plant3d.scenegraph.PipeRun;
18 import org.simantics.plant3d.scenegraph.PipelineComponent;
19 import org.simantics.plant3d.scenegraph.TurnComponent;
20 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
21 import org.simantics.plant3d.utils.ComponentUtils;
22 import org.simantics.utils.ui.ErrorLogger;
24 public class PipingRules {
25 private static final boolean DEBUG = false;
26 private static final boolean DUMMY = false;
28 private static double MIN_TURN_ANGLE = 0.001; // Threshold for removing turn components.
29 private static double ALLOWED_OFFSET = 0.001; // Allowed offset for directed path legs
31 private static final int REMOVE_NONE = 0;
32 private static final int REMOVE_START = 1;
33 private static final int REMOVE_END = 2;
34 private static final int REMOVE_BOTH = 3;
37 private enum PathLegUpdateType {
38 NONE, PREV, NEXT, PREV_S, NEXT_S
41 private static boolean enabled = true;
42 private static boolean updating = false;
43 private static boolean allowInsertRemove = true;
44 private static boolean triedIR = false;
47 private static List<PipeControlPoint> requestUpdates = new ArrayList<PipeControlPoint>();
48 private static List<PipeControlPoint> currentUpdates = new ArrayList<PipeControlPoint>();
50 private static Object updateMutex = new Object();
51 private static Object ruleMutex = new Object();
53 public static void requestUpdate(PipeControlPoint pcp) {
54 if (DEBUG) System.out.println("PipingRules request " + pcp);
55 synchronized (updateMutex) {
56 if (!requestUpdates.contains(pcp))
57 requestUpdates.add(pcp);
61 public static boolean update() throws Exception {
62 if (requestUpdates.size() == 0)
65 List<PipeControlPoint> temp = new ArrayList<PipeControlPoint>(requestUpdates.size());
66 synchronized(updateMutex) {
67 temp.addAll(requestUpdates);
68 requestUpdates.clear();
70 synchronized (ruleMutex) {
71 currentUpdates.clear();
72 currentUpdates.addAll(temp);
73 // TODO : we should remove already processed control points from currentUpdates after each _positionUpdate call.
74 for (PipeControlPoint pcp : currentUpdates)
75 _positionUpdate(pcp, true);
76 currentUpdates.clear();
78 synchronized(updateMutex) {
79 requestUpdates.removeAll(temp);
85 public static boolean positionUpdate(PipeControlPoint pcp) throws Exception {
87 return positionUpdate(pcp, true);
90 public static boolean positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception {
91 synchronized (ruleMutex) {
92 currentUpdates.add(pcp);
93 boolean b = _positionUpdate(pcp, allowIR);
94 currentUpdates.clear();
100 private static boolean _positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception {
101 if (updating || !enabled)
103 if (pcp.getPipeRun() == null)
106 if (DEBUG) System.out.println("PipingRules " + pcp);
108 allowInsertRemove = allowIR;
110 validate(pcp.getPipeRun());
111 if (pcp.isPathLegEnd()) {
112 updatePathLegEndControlPoint(pcp); // FIXME: Rules won't work properly, if they are not run twice.
113 updatePathLegEndControlPoint(pcp);
115 updateInlineControlPoint(pcp);
116 updateInlineControlPoint(pcp);
118 validate(pcp.getPipeRun());
119 if (!allowInsertRemove)
124 // System.out.println("PipingRules done " + pcp);
128 public static void setEnabled(boolean enabled) {
129 PipingRules.enabled = enabled;
131 currentUpdates.clear();
134 public static boolean isEnabled() {
138 // private void commit() {
139 // root.getNodeMap().commit();
142 public static class ExpandIterInfo {
143 // these two are turn control points
144 private PipeControlPoint start;
145 private PipeControlPoint end;
148 public ExpandIterInfo() {
152 public ExpandIterInfo(PipeControlPoint tcp, int type) {
153 if (type == REMOVE_START)
160 public ExpandIterInfo(PipeControlPoint start, PipeControlPoint end) {
163 this.type = REMOVE_BOTH;
166 public PipeControlPoint getEnd() {
170 public void setEnd(PipeControlPoint end) {
174 public PipeControlPoint getStart() {
178 public void setStart(PipeControlPoint start) {
182 public int getType() {
186 public void setType(int type) {
192 private static void updatePathLegEndControlPoint(PipeControlPoint pcp) throws Exception {
194 System.out.println("PipingRules.updatePathLegEndControlPoint() " + pcp);
195 if (pcp.getNext() != null) {
196 updatePathLegNext(pcp, pcp, PathLegUpdateType.NEXT_S);
198 if (pcp.getPrevious() != null) {
199 updatePathLegPrev(pcp, pcp, PathLegUpdateType.PREV_S);
204 private static void updateInlineControlPoint(PipeControlPoint pcp) throws Exception {
206 System.out.println("PipingRules.updateInlineControlPoint() " + pcp);
207 PipeControlPoint start = pcp.findPreviousEnd();
208 updatePathLegNext(start, pcp, PathLegUpdateType.NONE);
211 private static PipeControlPoint insertElbow(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos) throws Exception{
213 System.out.println("PipingRules.insertElbow() " + pcp1 + " " + pcp2 + " " + pos);
214 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
216 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getSubPoint().get(0)) {
217 pcp1 = pcp1.getSubPoint().get(0);
218 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
219 PipeControlPoint t = pcp1;
222 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getSubPoint().get(0) && pcp2.getNext() == pcp1) {
223 PipeControlPoint t = pcp1;
224 pcp1 = pcp2.getSubPoint().get(0);
227 throw new RuntimeException();
229 TurnComponent elbow = ComponentUtils.createTurn((P3DRootNode)pcp1.getRootNode());
230 PipeControlPoint pcp = elbow.getControlPoint();
231 if (pcp1.isDualInline())
232 pcp1 = pcp1.getSubPoint().get(0);
233 String name = pcp1.getPipeRun().getUniqueName("Elbow");
235 pcp1.getPipeRun().addChild(elbow);
237 pcp.insert(pcp1, pcp2);
239 pcp.setWorldPosition(pos);
240 validate(pcp.getPipeRun());
244 private static PipeControlPoint insertStraight(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos, double length) throws Exception {
246 System.out.println("PipingRules.insertStraight() " + pcp1 + " " + pcp2 + " " + pos);
247 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
249 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getSubPoint().get(0)) {
250 pcp1 = pcp1.getSubPoint().get(0);
251 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
252 PipeControlPoint t = pcp1;
255 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getSubPoint().get(0) && pcp2.getNext() == pcp1) {
256 PipeControlPoint t = pcp1;
257 pcp1 = pcp2.getSubPoint().get(0);
260 throw new RuntimeException();
262 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp1.getRootNode());
263 PipeControlPoint scp = component.getControlPoint();
264 if (pcp1.isDualInline())
265 pcp1 = pcp1.getSubPoint().get(0);
266 String name = pcp1.getPipeRun().getUniqueName("Pipe");
267 component.setName(name);
268 pcp1.getPipeRun().addChild(component);
270 scp.insert(pcp1, pcp2);
272 scp.setWorldPosition(pos);
273 scp.setLength(length);
274 validate(scp.getPipeRun());
278 private static PipeControlPoint insertStraight(PipeControlPoint pcp, Direction direction , Vector3d pos, double length) throws Exception {
280 System.out.println("PipingRules.insertStraight() " + pcp + " " + direction + " " + pos);
282 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp.getRootNode());
283 PipeControlPoint scp = component.getControlPoint();
284 if (pcp.isDualInline() && direction == Direction.NEXT)
285 pcp = pcp.getSubPoint().get(0);
286 String name = pcp.getPipeRun().getUniqueName("Pipe");
287 component.setName(name);
288 pcp.getPipeRun().addChild(component);
290 scp.insert(pcp,direction);
292 scp.setWorldPosition(pos);
293 scp.setLength(length);
294 validate(scp.getPipeRun());
298 private static void updatePathLegNext(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
300 UpdateStruct2 us = createUS(start, Direction.NEXT, 0, new ArrayList<ExpandIterInfo>(), updated);
301 if (lengthChange == PathLegUpdateType.NONE) {
302 if (start.equals(updated))
303 lengthChange = PathLegUpdateType.NEXT;
304 else if (us.end.equals(updated))
305 lengthChange = PathLegUpdateType.PREV;
307 updatePathLeg(us, lengthChange);
310 private static void updatePathLegPrev(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
311 // TODO: this method is not symmetric with updatePathLegNext, which may alter lengthChange parameter?
312 UpdateStruct2 us = createUS(start, Direction.PREVIOUS, 0, new ArrayList<ExpandIterInfo>(), updated);
313 // if (lengthChange == PathLegUpdateType.NONE) {
314 // if (start.equals(updated))
315 // lengthChange = PathLegUpdateType.NEXT;
316 // else if (us.end.equals(updated))
317 // lengthChange = PathLegUpdateType.PREV;
319 updatePathLeg(us, lengthChange);
322 private static class UpdateStruct2 {
323 public PipeControlPoint start;
324 public Vector3d startPoint;
325 public ArrayList<PipeControlPoint> list;
326 public PipeControlPoint end;
327 public Vector3d endPoint;
329 public Vector3d offset;
330 public boolean hasOffsets;
332 public boolean reversed;
333 public ArrayList<ExpandIterInfo> toRemove;
334 public PipeControlPoint updated;
336 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) {
337 if (start == null || end == null)
338 throw new NullPointerException();
340 this.startPoint = startPoint;
343 this.endPoint = endPoint;
345 this.offset = offset;
346 this.hasOffsets = hasOffsets;
348 this.reversed = reversed;
349 this.toRemove = toRemove;
350 this.updated = updated;
352 if (!MathTools.isValid(startPoint) ||
353 !MathTools.isValid(endPoint) ||
354 !MathTools.isValid(dir)) {
355 throw new RuntimeException();
359 public String toString() {
360 return start + " " + end+ " " + dir + " " + hasOffsets + " " + offset + " " + iter + " " + toRemove.size();
365 private static boolean calculateOffset(Vector3d startPoint, Vector3d endPoint, ArrayList<PipeControlPoint> list, Vector3d dir, Vector3d offset) {
366 boolean hasOffsets = false;
367 List<PipeControlPoint> offsets = new ArrayList<PipeControlPoint>(list.size());
368 for (PipeControlPoint icp : list) {
369 if (icp.isOffset()) {
371 } else if (icp.isDualSub())
372 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
374 if (offsets.size() == 0) {
377 double l = dir.lengthSquared();
378 if (l > MathTools.NEAR_ZERO)
379 dir.scale(1.0/Math.sqrt(l));
380 offset.set(0.0, 0.0, 0.0);
383 Vector3d sp = new Vector3d(startPoint);
384 Point3d ep = new Point3d(endPoint);
387 double l = dir.lengthSquared();
388 if (l > MathTools.NEAR_ZERO)
389 dir.scale(1.0/Math.sqrt(l));
393 offset.set(0.0, 0.0, 0.0);
395 for (PipeControlPoint icp : offsets) {
396 Vector3d v = icp.getSizeChangeOffsetVector(dir);
399 Point3d nep = new Point3d(endPoint);
401 if (nep.distance(ep) < 0.0000000001) {
407 l = dir.lengthSquared();
408 if (l > MathTools.NEAR_ZERO)
409 dir.scale(1.0/Math.sqrt(l));
414 if (DEBUG && hasOffsets)
415 System.out.println("calcOffset s:"+ startPoint + " e:" + endPoint + " d:" + dir + " o:"+offset) ;
419 private static UpdateStruct2 createUS(PipeControlPoint start, Direction direction, int iter, ArrayList<ExpandIterInfo> toRemove, PipeControlPoint updated) {
420 ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
421 PipeControlPoint end = null;
422 if (direction == Direction.NEXT) {
423 end = start.findNextEnd(list);
425 ArrayList<PipeControlPoint> prevList = new ArrayList<PipeControlPoint>();
426 PipeControlPoint tend = start.findPreviousEnd(prevList);
427 for (PipeControlPoint icp : prevList) {
428 if (icp.isDualSub()) {
429 list.add(0, icp.getParentPoint());
439 boolean hasOffsets = false;
440 Vector3d offset = new Vector3d();
441 Vector3d startPoint = start.getWorldPosition();
442 Vector3d endPoint = end.getWorldPosition();
443 Vector3d dir = new Vector3d();
444 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
445 return new UpdateStruct2(start, startPoint, list, end, endPoint, dir, offset, hasOffsets, iter, direction == Direction.PREVIOUS, toRemove, updated);
448 private static boolean asDirected(PipeControlPoint pcp, Direction direction) {
449 if (pcp.isDirected())
451 if (pcp.isTurn() && pcp.isFixed()) {
452 if (!pcp._getReversed())
453 return direction == Direction.NEXT;
455 return direction == Direction.PREVIOUS;
460 private static Vector3d direction(PipeControlPoint pcp, Direction direction) {
461 return pcp.getDirection(direction);
464 private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
466 if (asDirected(u.start, Direction.NEXT))
468 if (asDirected(u.end, Direction.PREVIOUS))
472 updateFreePathLeg(u, lengthChange);
475 updateDirectedPathLeg(u, lengthChange);
478 updateDualDirectedPathLeg(u, lengthChange);
484 private static void updateFreePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
486 System.out.println("PipingRules.updateFreePipeRun " + u + " " + lengthChange);
487 checkExpandPathLeg(u, lengthChange);
488 if (u.start.isInline() || u.end.isInline() || u.start.isFixed() || u.end.isFixed())
489 processPathLeg(u, true, false);
492 private static void updateInlineControlPoints(UpdateStruct2 u, boolean checkSizes) throws Exception{
494 System.out.println("PipingRules.updateInlineControlPoints() " + u);
496 Vector3d start = new Vector3d(u.startPoint);
497 Vector3d end = new Vector3d(u.endPoint);
500 // create offsets for leg ends.
501 MathTools.mad(start, u.dir, u.start.getInlineLength());
502 MathTools.mad(end, u.dir, -u.end.getInlineLength());
505 boolean recalcline = false;
509 for (PipeControlPoint icp : u.list) {
510 updateInlineControlPoint(icp, start, end, u.dir);
512 if (icp.isOffset()) {
513 // TODO : offset vector is already calculated and should be cached
514 Vector3d off = icp.getSizeChangeOffsetVector(u.dir);
515 updateOffsetPoint(icp, off);
521 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
522 pathLegPoints.add(u.start);
523 for (PipeControlPoint icp : u.list) {
524 // updateInlineControlPoint(icp, u.startPoint,
525 // u.endPoint,u.dir);
526 updateBranchControlPointBranches(icp);
527 pathLegPoints.add(icp);
529 pathLegPoints.add(u.end);
531 // TODO : values can be cached in the loop
532 for (int i = 0; i < pathLegPoints.size(); i++) {
533 PipeControlPoint icp = pathLegPoints.get(i);
535 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
536 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
538 if (icp.isVariableLength()) {
539 if (prev != null && next != null) {
541 recalcline = recalcline | updateVariableLength(icp, prev, next);
544 // this is variable length component at the end of the
546 // the problem is that we want to keep unconnected end
547 // of the component in the same
548 // place, but center of the component must be moved.
549 updateVariableLengthEnd(icp, prev != null ? prev : next);
553 } else if (prev != null && !prev.isVariableLength()) {
554 // If this and previous control point are not variable
555 // length pcps, we'll have to check if there is no empty
556 // space between them.
557 // I there is, we'll have to create new variable length
558 // component between them.
559 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
562 } else { // with offset
563 Vector3d sp = new Vector3d(start);
564 Vector3d ep = new Vector3d(end);
567 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<PipeControlPoint>();
568 pathLegPoints.add(u.start);
570 for (PipeControlPoint icp : u.list) {
571 updateInlineControlPoint(icp, sp, ep, u.dir);
572 updateBranchControlPointBranches(icp);
573 pathLegPoints.add(icp);
574 if (icp.isOffset()) {
575 // TODO : offset vector is already calculated and should be
577 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
578 updateOffsetPoint(icp, offset);
583 pathLegPoints.add(u.end);
588 sp = new Vector3d(u.startPoint);
589 ep = new Vector3d(u.endPoint);
592 for (int i = 0; i < pathLegPoints.size(); i++) {
593 PipeControlPoint icp = pathLegPoints.get(i);
595 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
596 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
598 if (prev != null && prev.isDualInline())
599 prev = prev.getSubPoint().get(0);
602 if (icp.isVariableLength()) {
603 if (prev != null && next != null) {
604 recalcline = recalcline | updateVariableLength(icp, prev, next);
607 // this is variable length component at the end of the
609 // the problem is that we want to keep unconnected end
610 // of the component in the same
611 // place, but center of the component must be moved.
612 updateVariableLengthEnd(icp, prev != null ? prev : next);
614 } else if (prev != null && !prev.isVariableLength()) {
615 // If this and previous control point are not variable
616 // length pcps, we'll have to check if there is no empty
617 // space between them.
618 // I there is, we'll have to create new variable length
619 // component between them.
620 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
622 if (icp.isOffset()) {
623 // TODO : offset vector is already calculated and should be
625 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
633 u.start.findNextEnd(u.list);
637 private static boolean updateVariableLength(PipeControlPoint icp, PipeControlPoint prev, PipeControlPoint next) {
638 Vector3d prevPos = prev.getWorldPosition();
639 Vector3d nextPos = next.getWorldPosition();
641 Vector3d dir = new Vector3d(nextPos);
643 double l = dir.lengthSquared(); // distance between
646 double l2prev = prev.getInlineLength(); // distance
650 double l2next = next.getInlineLength();
651 double l2 = l2prev + l2next;
652 double l2s = MathTools.square(l2);
653 if (l2s < l) { // check if there is enough space for
654 // variable length component.
657 double length = Math.sqrt(l) - l2; // true length of
661 dir.scale(length * 0.5 + l2prev); // calculate
666 icp.setWorldPosition(dir);
667 icp.setLength(length);
670 // components leave no space to the component and it
673 if (icp.isDeletable()) {
675 System.out.println("PipingRules.updateVariableLength removing " + icp);
683 private static boolean possibleVaribleLengthInsert(PipeControlPoint icp, PipeControlPoint prev) throws Exception{
684 Vector3d currentPos = icp.getWorldPosition();
685 Vector3d prevPos = prev.getWorldPosition();
686 Vector3d dir = new Vector3d(currentPos);
688 double l = dir.lengthSquared();
689 double l2prev = prev.getInlineLength();
690 double l2next = icp.getInlineLength();
691 double l2 = l2prev + l2next;
692 double l2s = l2 * l2;
694 if (allowInsertRemove) {
696 double length = Math.sqrt(l) - l2; // true length of the
699 dir.scale(length * 0.5 + l2prev); // calculate center
703 PipeControlPoint scp = insertStraight(prev, icp, dir, length);
712 private static void updateVariableLengthEnd(PipeControlPoint icp, PipeControlPoint prev) {
713 Vector3d currentPos = icp.getWorldPosition();
714 Vector3d prevPos = prev.getWorldPosition();
716 Vector3d dir = new Vector3d();
717 dir.sub(currentPos, prevPos);
719 boolean simple = currentUpdates.contains(icp);
721 // Update based on position -> adjust length
722 double currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
723 icp.setLength(currentLength);
725 // Update based on neighbour movement -> adjust length and position, so that free end stays in place.
726 double currentLength = icp.getLength();
727 if (currentLength < MathTools.NEAR_ZERO) {
728 currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
731 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
733 Point3d endPos = new Point3d(dir);
734 endPos.scale(currentLength * 0.5);
735 endPos.add(currentPos); // this is the free end of the component
737 double offset = prev.getInlineLength();
738 Point3d beginPos = new Point3d(dir);
739 beginPos.scale(offset);
740 beginPos.add(prevPos); // this is the connected end of the component
742 double l = beginPos.distance(endPos);
745 System.out.println("Length for " + icp + " is NaN");
748 beginPos.add(dir); // center position
751 System.out.println("PipingRules.updateInlineControlPoints() setting variable length to " + l);
754 icp.setWorldPosition(new Vector3d(beginPos));
758 private static void ppNoOffset(UpdateStruct2 u) throws Exception {
760 System.out.println("PipingRules.ppNoOffset() " + u);
761 Vector3d offset = new Vector3d();
764 for (PipeControlPoint icp : u.list) {
765 if (icp.isOffset()) {
766 offset.add(icp.getSizeChangeOffsetVector(u.dir));
767 } else if (icp.isDualSub())
768 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
772 checkExpandPathLeg(u, PathLegUpdateType.NONE);
775 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 {
777 System.out.println("PipingRules.ppNoDir() " + start + " " + end + " " + iter + " " + toRemove.size());
778 // FIXME : extra loop (dir should be calculated here)
779 Vector3d dir = new Vector3d();
780 Vector3d offset = new Vector3d();
781 hasOffsets = calculateOffset(startPoint, endPoint, list, dir, offset);
782 ppNoOffset(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, null, hasOffsets, iter, reversed, toRemove, updated));
785 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
786 checkExpandPathLeg(u, lengthChange, u.updated.isInline() && u.updated.isOffset());
789 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange, boolean updateEnds) throws Exception {
791 System.out.println("PipingRules.checkExpandPathLeg() " + u + " " + lengthChange);
792 if (lengthChange != PathLegUpdateType.NONE) {
793 // FIXME : turns cannot be checked before inline cps are updated,
794 // since their position affects calculation of turns
795 processPathLeg(u, updateEnds, false);
796 int type = checkTurns(u, lengthChange);
797 if (type == REMOVE_NONE) {
798 processPathLeg(u, updateEnds, true);
800 expandPathLeg(u, type);
803 processPathLeg(u, updateEnds, true);
807 private static void updateDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
809 System.out.println("PipingRules.updateDirectedPipeRun() " + u + " " + lengthChange);
810 PipeControlPoint dcp;
811 PipeControlPoint other;
812 boolean canMoveOther = false;
813 boolean dcpStart = false;
814 boolean inlineEnd = false;
816 if (asDirected(u.start, Direction.NEXT)) {
819 position = u.startPoint;
823 inlineEnd = u.end.isInline();
828 position = u.endPoint;
831 inlineEnd = u.start.isInline();
834 Vector3d directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
835 if (directedDirection == null) {
836 //updateTurnControlPointTurn(dcp, dcp.getPrevious(), dcp.getNext());
837 updateTurnControlPointTurn(dcp, null, null);
838 directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
839 if (directedDirection == null) {
843 Point3d directedEndPoint = new Point3d(u.endPoint);
845 directedEndPoint.add(u.offset);
847 double mu[] = new double[2];
850 Vector3d t = new Vector3d();
853 closest = MathTools.closestPointOnStraight(directedEndPoint, u.startPoint, directedDirection, mu);
854 t.sub(closest, directedEndPoint);
856 closest = MathTools.closestPointOnStraight(u.startPoint, directedEndPoint, directedDirection, mu);
857 t.sub(closest, u.startPoint);
860 double distance = t.length();
861 boolean aligned = (distance < ALLOWED_OFFSET);
863 if (u.start.isInline() || u.end.isInline() || u.start.isFixed() || u.end.isFixed())
864 processPathLeg(u, true, false);
865 checkExpandPathLeg(u, lengthChange, inlineEnd);
871 PipeControlPoint nextToMoved;
873 if (u.list.size() > 0)
875 nextToMoved = u.list.get(0);
877 nextToMoved = u.list.get(u.list.size() - 1);
881 nextToMoved = u.start;
882 if (other.isVariableAngle()) {
884 // TODO calculate needed space from next run end.
885 double space = spaceForTurn(other);
888 closest.set(u.startPoint);
890 closest.set(u.endPoint);
892 Vector3d v = new Vector3d(directedDirection);
899 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + closest);
900 other.setWorldPosition(closest);
902 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));
903 if (u.end.getNext() != null)
904 updatePathLegNext(u.end, u.updated, PathLegUpdateType.NEXT);
906 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));
907 if (u.start.getPrevious() != null)
908 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.PREV);
911 // TODO : calculate needed space from next run end.
912 if (allowInsertRemove)
913 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
918 } else if (other.isNonDirected() && other.getParentPoint() != null) {
919 // FIXME : this code was for updating branches
920 Vector3d bintersect = new Vector3d();
921 PipeControlPoint bcp = other.getParentPoint();
922 if (bcp != null && canMoveOther) {
923 Point3d bstart = new Point3d();
924 Point3d bend = new Point3d();
925 Vector3d bdir = new Vector3d();
926 bcp.getInlineControlPointEnds(bstart, bend, bdir);
927 Vector3d nintersect = new Vector3d();
929 MathTools.intersectStraightStraight(position, directedDirection, bend, bdir, nintersect, bintersect, mu);
930 Vector3d dist = new Vector3d(nintersect);
931 dist.sub(bintersect);
932 canMoveOther = mu[1] > 0.0 && mu[1] < 1.0 && dist.lengthSquared() < 0.01;
934 // TODO : endControlPoints are undirected: calculcate
935 // correct position for it
936 throw new UnsupportedOperationException("not implemented");
940 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + bintersect);
941 // is required branch position is in possible range
942 bcp.setWorldPosition(bintersect);
944 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);
946 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);
949 // branch cannot be moved into right position, new turn
950 // / elbow must be inserted
951 if (allowInsertRemove)
952 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
957 } else { // assume that control point cannot be moved, but can
959 if (allowInsertRemove)
960 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
970 private static void updateDualDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
972 System.out.println("PipingRules.updateDualDirectedPipeRun() " + u + " " + lengthChange);
974 PipeControlPoint dcp1 = u.start;
975 PipeControlPoint dcp2 = u.end;
976 Point3d position1 = new Point3d(u.startPoint);
977 Point3d position2 = new Point3d(u.endPoint);
978 Point3d position1offset = new Point3d(position1);
979 position1offset.sub(u.offset);
980 Point3d position2offset = new Point3d(position2);
981 position2offset.add(u.offset);
982 Vector3d dir1 = direction(dcp1, Direction.NEXT);
983 Vector3d dir2 = direction(dcp2, Direction.PREVIOUS);
984 Vector3d p1 = MathTools.closestPointOnStraight(position1offset, position2, dir2);
985 Vector3d p2 = MathTools.closestPointOnStraight(position2offset, position1, dir1);
986 double d1 = position1.distance(new Point3d(p1));
987 double d2 = position2.distance(new Point3d(p2));
989 boolean aligned = (d1 < ALLOWED_OFFSET && d2 < ALLOWED_OFFSET);
995 } else if (allowInsertRemove){
996 PipeControlPoint dcp;
997 PipeControlPoint next;
1000 if (u.list.size() > 0)
1001 next = u.list.get(0);
1006 if (u.list.size() > 0)
1007 next = u.list.get(u.list.size() - 1);
1012 p1 = dcp.getWorldPosition();
1013 Vector3d v = new Vector3d();
1019 // Reserve space for 90 deg elbow
1020 double off = dcp1.getPipeRun().getTurnRadius();
1025 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position2, dir2);
1027 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position1, dir1);
1029 // By default, the elbows are placed next to each other, by using 90 deg angles.
1030 // If the distance between elbows is not enough, we must move the other elbow (and create more shallow angle elbows)
1031 if (MathTools.distance(p1, p2) < off*2.05) {
1035 PipeControlPoint tcp1 = insertElbow(dcp, next, p1);
1036 PipeControlPoint tcp2 = insertElbow(tcp1, next, p2);
1039 System.out.println("PipingRules.updateDualDirectedPipeRun() created two turns " + tcp1 + " " + tcp2);
1042 Vector3d dd = new Vector3d(p2);
1045 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1046 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1048 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1050 updatePathLegPrev(tcp2, u.updated, PathLegUpdateType.NONE);
1052 Vector3d dd = new Vector3d(p1);
1055 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1056 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1058 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1060 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.NONE);
1069 private static double spaceForTurn(PipeControlPoint tcp) {
1070 // TODO : this returns now space for 90 deg turn.
1071 // The challenge: position of tcp affects the turn angle, which then affects the required space. Perhaps we need to iterate...
1072 // Additionally, if the path legs contain offset, using just positions of opposite path leg ends is not enough,
1073 return tcp.getPipeRun().getTurnRadius();
1076 private static void insertElbowUpdate(UpdateStruct2 u, PipeControlPoint dcp, PipeControlPoint next, boolean dcpStart, Vector3d position, Vector3d directedDirection) throws Exception{
1079 // Vector3d closest = new Vector3d(position);
1080 // closest.add(directedDirection);
1082 PipeControlPoint tcp = null;
1085 closest = MathTools.closestPointOnStraight(next.getWorldPosition(), position, directedDirection);
1086 tcp = insertElbow(dcp, next, closest);
1088 closest = MathTools.closestPointOnStraight(dcp.getWorldPosition(), position, directedDirection);
1089 tcp = insertElbow(next, dcp, closest);
1091 // TODO properly calculate required distance between start and inserted elbow.
1092 double d = MathTools.distance(position, closest);
1093 double s = spaceForTurn(tcp);
1096 Vector3d p = new Vector3d(directedDirection);
1106 System.out.println("PipingRules.updateDirectedPipeRun() inserted " + tcp);
1109 // update pipe run from new turn to other end
1110 ppNoDir(tcp, new Vector3d(closest), u.list, u.end, u.endPoint, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1111 // update pipe run from directed to new turn
1112 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));
1114 // update pipe run from other end to new turn
1115 ppNoDir(u.start, u.startPoint, u.list, tcp, new Vector3d(closest), u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1116 // update pipe run from new turn to directed
1117 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));
1122 * Checks if turns can be removed (turn angle near zero)
1124 private static int checkTurns(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1126 System.out.println("PipingRules.checkTurns() " + u.start + " " + u.end);
1127 boolean startRemoved = false;
1128 boolean endRemoved = false;
1129 if (u.start.isVariableAngle()) {
1130 // this won't work properly if inline control points are not updated
1131 PipeControlPoint startPrev = u.start.getPrevious();
1132 if (startPrev != null) {
1133 double a = updateTurnControlPointTurn(u.start, null, u.dir);
1134 if (a < MIN_TURN_ANGLE && u.start.isDeletable())
1135 startRemoved = true;
1136 else if (lengthChange == PathLegUpdateType.PREV || lengthChange == PathLegUpdateType.PREV_S) {
1137 PathLegUpdateType type;
1138 if (lengthChange == PathLegUpdateType.PREV_S)
1139 type = PathLegUpdateType.PREV;
1141 type = PathLegUpdateType.NONE;
1142 updatePathLegPrev(u.start, u.start, type);
1146 if (u.end.isVariableAngle()) {
1148 PipeControlPoint endNext = u.end.getNext();
1149 if (endNext != null) {
1150 // TODO: u.end, u.dir, null
1151 double a = updateTurnControlPointTurn(u.end, null, null);
1152 if (a < MIN_TURN_ANGLE && u.end.isDeletable())
1154 else if (lengthChange == PathLegUpdateType.NEXT || lengthChange == PathLegUpdateType.NEXT_S) {
1155 PathLegUpdateType type;
1156 if (lengthChange == PathLegUpdateType.NEXT_S)
1157 type = PathLegUpdateType.NEXT;
1159 type = PathLegUpdateType.NONE;
1160 updatePathLegNext(u.end, u.end, type);
1165 System.out.println("PipingRules.checkTurns() res " + startRemoved + " " + endRemoved);
1166 if (!startRemoved && !endRemoved)
1168 if (startRemoved && endRemoved)
1171 return REMOVE_START;
1176 * Expands piperun search over turns that are going to be removed
1179 private static void expandPathLeg(UpdateStruct2 u, int type) throws Exception {
1181 System.out.println("PipingRules.expandPipeline " + u.start + " " + u.end);
1182 ArrayList<PipeControlPoint> newList = new ArrayList<PipeControlPoint>();
1185 throw new RuntimeException("Error in piping rules");
1187 u.toRemove.add(new ExpandIterInfo(u.start, REMOVE_START));
1188 u.start = u.start.findPreviousEnd();
1189 u.startPoint = u.start.getPosition();
1190 u.start.findNextEnd(newList);
1191 newList.addAll(u.list);
1195 u.toRemove.add(new ExpandIterInfo(u.end, REMOVE_END));
1196 u.end = u.end.findNextEnd(newList);
1197 u.endPoint = u.end.getPosition();
1198 u.list.addAll(newList);
1201 u.toRemove.add(new ExpandIterInfo(u.start, u.end));
1202 u.start = u.start.findPreviousEnd();
1203 u.startPoint = u.start.getPosition();
1204 u.start.findNextEnd(newList);
1205 newList.addAll(u.list);
1207 newList = new ArrayList<PipeControlPoint>();
1208 u.end = u.end.findNextEnd(newList);
1209 u.endPoint = u.end.getPosition();
1210 u.list.addAll(newList);
1213 throw new RuntimeException("Error in piping rules");
1216 u.offset = new Vector3d();
1219 for (PipeControlPoint icp : u.list) {
1220 if (icp.isOffset()) {
1221 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1222 } else if (icp.isDualSub())
1223 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1227 System.out.println("PipingRules.expandPipeline expanded " + u.start + " " + u.end);
1229 updatePathLeg(u, PathLegUpdateType.NONE);
1233 * reverts one iteration of turn removing back)
1235 private static void backIter(UpdateStruct2 u) throws Exception {
1238 System.out.println("PipingRules.backIter" + u.start + " " + u.end);
1240 throw new RuntimeException("Error in piping rules");
1241 ExpandIterInfo info = u.toRemove.get(u.toRemove.size() - 1);
1242 u.toRemove.remove(u.toRemove.size() - 1);
1243 if (info.getType() == REMOVE_START || info.getType() == REMOVE_BOTH) {
1244 while (u.list.size() > 0) {
1245 PipeControlPoint icp = u.list.get(0);
1246 if (icp.getPrevious().equals(info.getStart()))
1250 u.start = info.getStart();
1252 if (info.getType() == REMOVE_END || info.getType() == REMOVE_BOTH) {
1253 while (u.list.size() > 0) {
1254 PipeControlPoint icp = u.list.get(u.list.size() - 1);
1255 if (icp.getNext().equals(info.getEnd()))
1259 u.end = info.getEnd();
1261 u.offset = new Vector3d();
1264 for (PipeControlPoint icp : u.list) {
1265 if (icp.isOffset()) {
1266 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1267 } else if (icp.isDualSub())
1268 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1276 * Processes pipe run (removes necessary turns and updates run ends)
1278 // private static void processPathLeg(PipeControlPoint start, Point3d
1279 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1280 // end,Point3d endPoint, Vector3d dir,Vector3d offset, boolean
1281 // hasOffsets,int iter, boolean reversed, ArrayList<ExpandIterInfo>
1282 // toRemove) throws TransactionException {
1284 private static void processPathLeg(UpdateStruct2 u) throws Exception {
1286 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1287 processPathLeg(u, true, true);
1290 private static void processPathLeg(UpdateStruct2 u, boolean updateEnds, boolean updateInline) throws Exception {
1292 System.out.println("PipingRules.processPathLeg " + (updateEnds ? "ends " : "") + (updateInline ? "inline " : "") + u.start + " " + u.end);
1294 if (u.toRemove.size() > 0) {
1295 for (ExpandIterInfo info : u.toRemove) {
1296 if (info.getStart() != null) {
1298 System.out.println("PipingRules.processPathLeg removing start " + info.getStart());
1299 info.getStart()._remove();
1301 if (info.getEnd() != null) {
1303 System.out.println("PipingRules.processPathLeg removing end " + info.getEnd());
1304 info.getEnd()._remove();
1307 // ControlPointTools.removeControlPoint may remove more than one CP;
1308 // we must populate inline CP list again.
1310 u.start.findNextEnd( u.list);
1312 // FIXME : inline CPs are update twice because their positions must be
1313 // updated before and after ends.
1314 updateInlineControlPoints(u, false);
1317 if (u.start.isTurn()) {
1318 //updateTurnControlPointTurn(u.start, u.start.getPrevious(), u.start.getNext());
1319 updateTurnControlPointTurn(u.start, null, null);
1320 // updatePathLegPrev(u.start, u.start, PathLegUpdateType.NONE);
1321 } else if (u.start.isEnd()) {
1322 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1323 } else if (u.start.isInline()) {
1324 updateControlPointOrientation(u.start);
1326 if (u.end.isTurn()) {
1327 //updateTurnControlPointTurn(u.end, u.end.getPrevious(), u.end.getNext());
1328 updateTurnControlPointTurn(u.end, null, null);
1329 // updatePathLegNext(u.end, u.end, PathLegUpdateType.NONE);
1330 } else if (u.end.isEnd()) {
1331 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1332 } else if (u.end.isInline()) {
1333 updateControlPointOrientation(u.end);
1337 if (u.start.isEnd()) {
1338 updateEndComponentControlPoint(u.start, u.startPoint, u.endPoint);
1340 if (u.end.isEnd()) {
1341 updateEndComponentControlPoint(u.end, u.startPoint, u.endPoint);
1345 updateInlineControlPoints(u, true);
1350 * Processes pipe run and recalculates offset
1352 // private static void processPathLeg(PipeControlPoint start, Point3d
1353 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1354 // end,Point3d endPoint, Vector3d dir, boolean hasOffsets,int iter, boolean
1355 // reversed, ArrayList<ExpandIterInfo> toRemove) throws TransactionException
1357 private static void processPathLegNoOffset(UpdateStruct2 u) throws Exception {
1359 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1360 Vector3d offset = new Vector3d();
1363 for (PipeControlPoint icp : u.list) {
1364 if (icp.isOffset()) {
1365 offset.add(icp.getSizeChangeOffsetVector(u.dir));
1366 } else if (icp.isDualSub()) {
1367 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1374 private static void updateOffsetPoint(PipeControlPoint sccp, Vector3d offset) {
1375 Vector3d world = sccp.getWorldPosition();
1377 PipeControlPoint ocp = sccp.getSubPoint().iterator().next();
1378 ocp.setWorldPosition(world);
1382 * Updates InlineControlPoints position when straight pipe's end(s) have
1390 private static void updateInlineControlPoint(PipeControlPoint icp, Vector3d prev, Vector3d next, Vector3d dir) {
1392 System.out.println("PipingRules.updateInlineControlPoint() " + icp);
1394 Vector3d inlinePoint = icp.getWorldPosition();
1395 Vector3d prevPoint = new Vector3d(prev);
1396 Vector3d nextPoint = new Vector3d(next);
1397 if (!icp.isVariableLength()) {
1398 // Reserve space for fixed length components.
1399 MathTools.mad(prevPoint, dir, icp.getInlineLength());
1400 MathTools.mad(nextPoint, dir, -icp.getInlineLength());
1401 if (MathTools.distance(prevPoint, nextPoint) < ALLOWED_OFFSET) {
1406 boolean canCalc = MathTools.distance(prevPoint, nextPoint) > ALLOWED_OFFSET;
1408 System.out.print("InlineControlPoint update " + icp + " " + inlinePoint + " " + prevPoint + " " + nextPoint);
1409 Vector3d newInlinePoint = null;
1411 boolean branchUpdate = false;
1412 PipeControlPoint becp = null;
1413 for (PipeControlPoint pcp : icp.getSubPoint())
1414 if (pcp.isNonDirected()) {
1415 branchUpdate = true;
1420 if (DUMMY || !branchUpdate) {
1421 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1425 // FIXME : can only handle one branch
1426 PipeControlPoint p = null;
1427 if (becp.getNext() != null) {
1428 p = becp.findNextEnd();
1429 } else if (becp.getPrevious() != null) {
1430 p = becp.findPreviousEnd();
1433 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1434 } else if (canCalc){
1435 Vector3d branchLegEnd = p.getWorldPosition();
1436 Vector3d dir2 = new Vector3d(inlinePoint);
1437 dir2.sub(branchLegEnd);
1438 Vector3d dir1 = new Vector3d(nextPoint);
1439 dir1.sub(prevPoint);
1440 newInlinePoint = new Vector3d();
1441 double mu[] = new double[2];
1442 MathTools.intersectStraightStraight(new Vector3d(prevPoint), dir1, new Vector3d(branchLegEnd), dir2, newInlinePoint, new Vector3d(), mu);
1444 System.out.println(mu[0]);
1445 // FIXME : reserve space
1447 newInlinePoint = new Vector3d(prevPoint);
1448 } else if (mu[0] > 1.0) {
1449 newInlinePoint = new Vector3d(nextPoint);
1454 // prevPoint == nextPoint
1455 newInlinePoint = new Vector3d(prevPoint);
1458 System.out.println(" " + newInlinePoint);
1460 icp.setWorldPosition(newInlinePoint);
1461 updateControlPointOrientation(icp);
1465 * Updates InlineControlPoints position when straight pipe's end(s) have
1473 private static void updateEndComponentControlPoint(PipeControlPoint ecp, Vector3d start, Vector3d end) throws Exception {
1475 System.out.println("PipingRules.updateEndComponentControlPoint() " + ecp);
1476 // PipeControlPoint next = ecp.getNext();
1477 // PipeControlPoint prev = ecp.getPrevious();
1478 // if (next != null) {
1479 // end = G3DTools.getPoint(next.getLocalPosition());
1480 // start = G3DTools.getPoint(ecp.getLocalPosition());
1481 // } else if (prev != null) {
1482 // end = G3DTools.getPoint(ecp.getLocalPosition());
1483 // start = G3DTools.getPoint(prev.getLocalPosition());
1485 // // TODO : warning?
1488 // Vector3d dir = new Vector3d (end);
1491 // G3DTools.setTuple(ecp.getDirection(), dir);
1493 updateControlPointOrientation(ecp);
1495 for (PipeControlPoint pcp : ecp.getSubPoint()) {
1496 // TODO update position
1497 updatePathLegEndControlPoint(pcp);
1501 private static void updateControlPointOrientation(PipeControlPoint pcp) {
1502 // FIXME : hack to bypass variable length components orientation
1503 // if (pcp.getAtMostOneRelatedObject(ProcessResource.g3dResource.HasWorldOrientation) == null)
1505 // if (pcp.rotationAngle == null)
1507 Double angleO = pcp.getRotationAngle();
1511 Boolean reversedO = pcp.getReversed();
1512 boolean reversed = false;
1513 if (reversedO != null)
1514 reversed = reversedO;
1515 Quat4d q = pcp.getControlPointOrientationQuat(angle, reversed);
1516 pcp.setWorldOrientation(q);
1520 * Updates all branches when branch's position has been changed
1524 private static void updateBranchControlPointBranches(PipeControlPoint bcp) throws Exception {
1526 System.out.println("PipingRules.updateBranchControlPointBranches() " + bcp);
1527 if (bcp.isDualInline())
1529 Collection<PipeControlPoint> branches = bcp.getSubPoint();
1530 if (branches.size() == 0) {
1532 System.out.println("No Branches found");
1536 for (PipeControlPoint pcp : branches) {
1537 updatePathLegEndControlPoint(pcp);
1541 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d prev, Vector3d next) {
1543 UpdateStruct2 us = createUS(tcp, Direction.NEXT, 0, new ArrayList<PipingRules.ExpandIterInfo>(), tcp);
1548 UpdateStruct2 us = createUS(tcp, Direction.PREVIOUS, 0, new ArrayList<PipingRules.ExpandIterInfo>(), tcp);
1554 if (!tcp.isFixed()) {
1557 if (next == null || prev == null) {
1558 if (tcp.getTurnAngle() != null)
1559 return tcp.getTurnAngle();
1560 return Math.PI; // FIXME : argh
1562 double turnAngle = prev.angle(next);
1564 double angle = Math.PI - turnAngle;
1566 Vector3d turnAxis = new Vector3d();
1567 turnAxis.cross(prev, next);
1568 if (turnAxis.lengthSquared() > MathTools.NEAR_ZERO) {
1569 double elbowRadius = tcp.getPipelineComponent().getPipeRun().getTurnRadius();
1570 double R = elbowRadius / Math.tan(angle * 0.5);
1572 turnAxis.normalize();
1573 tcp.setTurnAngle(turnAngle);
1574 tcp.setLength(R);// setComponentOffsetValue(R);
1575 tcp.setTurnAxis(turnAxis);
1576 // tcp.setPosition(tcp.getPosition());
1579 tcp.setTurnAngle(0.0);
1581 tcp.setTurnAxis(new Vector3d(MathTools.Y_AXIS));
1584 updateControlPointOrientation(tcp);
1587 System.out.println("PipingTools.updateTurnControlPointTurn " + prev + " " + next + " " + turnAngle + " " + turnAxis);
1591 if (prev != null && next != null) {
1593 } else if (prev == null) {
1594 if (!tcp._getReversed())
1595 tcp.setReversed(true);
1596 } else if (next == null) {
1597 if (tcp._getReversed())
1598 tcp.setReversed(false);
1601 Vector3d dir = null;
1602 if (!tcp._getReversed()) {
1609 return Math.PI; // FIXME : argh
1612 Quat4d q = PipeControlPoint.getControlPointOrientationQuat(dir, tcp.getRotationAngle() != null ? tcp.getRotationAngle() : 0.0);
1613 Vector3d v = new Vector3d();
1614 MathTools.rotate(q, MathTools.Y_AXIS,v);
1616 tcp.setWorldOrientation(q);
1617 if (tcp.getTurnAngle() != null)
1618 return tcp.getTurnAngle();
1619 return Math.PI; // FIXME : argh
1625 public static List<PipeControlPoint> getControlPoints(PipeRun pipeRun) {
1626 List<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
1627 if (pipeRun.getControlPoints().size() == 0)
1629 PipeControlPoint pcp = pipeRun.getControlPoints().iterator().next();
1630 while (pcp.getPrevious() != null) {
1631 PipeControlPoint prev = pcp.getPrevious();
1632 if (prev.getPipeRun() != pipeRun)
1636 if (pcp.isDualSub()) {
1637 pcp = pcp.getParentPoint();
1640 while (pcp.getNext() != null) {
1641 pcp = pcp.getNext();
1642 if (pcp.getPipeRun() != pipeRun)
1649 public static void reverse(PipeRun pipeRun) {
1652 List<PipeControlPoint> points = getControlPoints(pipeRun);
1653 PipeControlPoint pcp = points.get(0);
1654 if (pcp.isSizeChange() && pcp.getSubPoint().size() > 0) {
1655 pipeRun = pcp.getPipeRun();
1660 List<PipeRun> all = new ArrayList<PipeRun>();
1661 List<List<PipeControlPoint>> pcps = new ArrayList<List<PipeControlPoint>>();
1664 List<PipeControlPoint> points = getControlPoints(pipeRun);
1666 PipeControlPoint pcp = points.get(points.size()-1);
1667 if (pcp.getSubPoint().size() > 0) {
1668 pipeRun = pcp.getSubPoint().get(0).getPipeRun();
1673 for (int i = 0 ; i < all.size(); i++) {
1674 List<PipeControlPoint> list = pcps.get(i);
1677 for (int i = 0 ; i < all.size(); i++) {
1678 boolean last = i == all.size() - 1;
1679 List<PipeControlPoint> list = pcps.get(i);
1682 List<PipeControlPoint> list2 = pcps.get(i+1);
1683 PipeControlPoint prev = list.get(list.size()-1);
1684 PipeControlPoint next = list2.get(0);
1685 System.out.println();
1687 // Reverse the component on the boundary.
1688 InlineComponent ic = (InlineComponent)prev.getPipelineComponent();
1689 PipeRun r1 = ic.getPipeRun();
1690 PipeRun r2 = ic.getAlternativePipeRun();
1691 if (r1 == null || r2 == null)
1692 throw new RuntimeException("Components on PipeRun changes should refer to bot PipeRuns");
1696 ic.setAlternativePipeRun(r1);
1698 throw new RuntimeException("PipeRun changes should contain shared control points");
1706 private static void _reverse(List<PipeControlPoint> list) {
1707 if (list.size() <= 1)
1708 return; // nothing to do.
1710 for (int i = 0 ; i < list.size(); i++) {
1711 boolean first = i == 0;
1712 boolean last = i == list.size() - 1;
1713 PipeControlPoint current = list.get(i);
1714 PipeControlPoint currentSub = null;
1715 if (current.isDualInline())
1716 currentSub = current.getSubPoint().get(0);
1718 PipeControlPoint next = list.get(i+1);
1719 if (next.isDualInline())
1720 next = next.getSubPoint().get(0);
1721 if (current.getNext() == next)
1722 current.setNext(null);
1723 current.setPrevious(next);
1724 if (currentSub != null) {
1725 if (currentSub.getNext() == next)
1726 currentSub.setNext(null);
1727 currentSub.setPrevious(next);
1730 PipeControlPoint prev = list.get(i-1);
1732 if (current.getPrevious() == prev)
1733 current.setPrevious(null);
1734 current.setNext(prev);
1736 if (currentSub != null) {
1737 if (currentSub.getPrevious() == prev)
1738 currentSub.setPrevious(null);
1739 currentSub.setNext(prev);
1742 PipeControlPoint prev = list.get(i-1);
1743 PipeControlPoint next = list.get(i+1);
1744 if (next.isDualInline())
1745 next = next.getSubPoint().get(0);
1748 current.setPrevious(next);
1749 current.setNext(prev);
1751 if (currentSub != null) {
1752 currentSub.setPrevious(next);
1753 currentSub.setNext(prev);
1757 if (current.isTurn() && current.isFixed()) {
1758 current.setReversed(!current._getReversed());
1760 if (current.isInline() && current.isReverse()) {
1761 current.setReversed(!current._getReversed());
1766 public static void merge(PipeRun run1, PipeRun r2) {
1767 Map<PipeControlPoint, Vector3d> positions = new HashMap<PipeControlPoint, Vector3d>();
1768 Map<PipeControlPoint, Quat4d> orientations = new HashMap<PipeControlPoint, Quat4d>();
1769 for (PipeControlPoint pcp : r2.getControlPoints()) {
1770 positions.put(pcp, pcp.getWorldPosition());
1771 orientations.put(pcp, pcp.getWorldOrientation());
1773 for (PipeControlPoint pcp : r2.getControlPoints()) {
1774 r2.deattachChild(pcp);
1776 PipelineComponent component = pcp.getPipelineComponent();
1777 if (component != null) {
1778 if (!(component instanceof Nozzle)) {
1779 component.deattach();
1780 run1.addChild(component);
1782 Nozzle n = (Nozzle)component;
1791 public static void validate(PipeRun pipeRun) {
1792 if (pipeRun == null)
1794 Collection<PipeControlPoint> pcps = pipeRun.getControlPoints();
1796 //System.out.println("Validate " + pipeRun.getName());
1797 for (PipeControlPoint pcp : pcps) {
1798 if (pcp.getParentPoint() == null || pcp.getParentPoint().getPipeRun() != pipeRun)
1801 List<PipeControlPoint> runPcps = getControlPoints(pipeRun);
1802 if (runPcps.size() != count) {
1803 System.out.println("Run " + pipeRun.getName() + " contains unconnected control points");
1805 for (PipeControlPoint pcp : pcps) {
1806 if (!pcp.isDirected() && pcp.getNext() == null && pcp.getPrevious() == null)
1807 System.out.println("Orphan undirected " + pcp);
1809 for (PipeControlPoint pcp : pcps) {
1810 if (pcp.getParentPoint() == null) {
1811 PipeControlPoint sub = null;
1812 if (pcp.isDualInline())
1813 sub = pcp.getSubPoint().get(0);
1814 PipeControlPoint next = pcp.getNext();
1815 PipeControlPoint prev = pcp.getPrevious();
1817 if (!(next.getPrevious() == pcp || next.getPrevious() == sub)) {
1818 System.out.println("Inconsistency between " + pcp + " -> " +next );
1822 PipeControlPoint prevParent = null;
1823 if (prev.isDualSub()) {
1824 prevParent = prev.getParentPoint();
1825 } else if (prev.isDualInline()) {
1826 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1828 if (!(prev.getNext() == pcp && (prevParent == null || prevParent.getNext() == pcp))) {
1829 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
1836 public static void splitVariableLengthComponent(PipelineComponent newComponent, InlineComponent splittingComponent, boolean assignPos) throws Exception{
1837 assert(!splittingComponent.getControlPoint().isFixed());
1838 assert(!(newComponent instanceof InlineComponent && !newComponent.getControlPoint().isFixed()));
1839 PipeControlPoint newCP = newComponent.getControlPoint();
1840 PipeControlPoint splittingCP = splittingComponent.getControlPoint();
1841 PipeControlPoint nextCP = splittingCP.getNext();
1842 PipeControlPoint prevCP = splittingCP.getPrevious();
1844 /* there are many different cases to insert new component when
1845 it splits existing VariableLengthinlineComponent.
1847 1. VariableLengthComponet is connected from both sides:
1848 - insert new component between VariableLength component and component connected to it
1849 - insert new VariableLengthComponent between inserted component and component selected in previous step
1851 2. VariableLengthComponent is connected from one side
1852 - Use previous case or:
1853 - Insert new component to empty end
1854 - Insert new VariableLength component to inserted components empty end
1856 3. VariableLength is not connected to any component.
1857 - Should not be possible, at least in current implementation.
1858 - Could be done using second case
1862 if (nextCP == null && prevCP == null) {
1863 // this should not be possible
1864 throw new RuntimeException("VariableLengthComponent " + splittingComponent + " is not connected to anything.");
1866 double reservedLength = splittingComponent.getControlPoint().getLength();
1867 double newLength = newComponent.getControlPoint().getLength();
1870 Point3d next = new Point3d();
1871 Point3d prev = new Point3d();
1872 splittingCP.getInlineControlPointEnds(prev, next);
1874 Vector3d newPos = null;
1876 newPos = new Vector3d(prev);
1877 Vector3d dir = new Vector3d(next);
1881 newComponent.setWorldPosition(newPos);
1883 newPos = newComponent.getWorldPosition();
1888 Vector3d dir = new Vector3d(next);
1891 dir.scale(newLength * 0.5);
1892 Point3d vn = new Point3d(newPos);
1893 Point3d vp = new Point3d(newPos);
1896 double ln = vn.distance(next);
1897 double lp = vp.distance(prev);
1898 vp.interpolate(prev, 0.5);
1899 vn.interpolate(next, 0.5);
1902 PipeControlPoint newVariableLengthCP = null;//insertStraight(pcp1, pcp2, pos, length);
1903 if (nextCP == null) {
1904 newCP.insert(splittingCP, Direction.NEXT);
1905 newVariableLengthCP = insertStraight(newCP, Direction.NEXT, new Vector3d(vn), ln);
1906 splittingCP.setWorldPosition(new Vector3d(vp));
1907 // ControlPointTools.setWorldPosition(splittingCP, vp);
1908 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1909 } else if (prevCP == null) {
1910 newCP.insert(splittingCP, Direction.PREVIOUS);
1911 newVariableLengthCP = insertStraight(newCP, Direction.PREVIOUS, new Vector3d(vp), lp);
1912 splittingCP.setWorldPosition(new Vector3d(vn));
1913 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, ln);
1915 newCP.insert(splittingCP, nextCP);
1916 newVariableLengthCP = insertStraight(newCP, nextCP, new Vector3d(vn), ln);
1917 splittingCP.setWorldPosition(new Vector3d(vp));
1918 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
1920 positionUpdate(newCP);
1924 public static void addSizeChange(boolean reversed, PipeRun pipeRun, PipeRun other, InlineComponent reducer, PipeControlPoint previous, PipeControlPoint next) {
1925 PipeControlPoint pcp = reducer.getControlPoint();
1926 PipeControlPoint ocp = pcp.getSubPoint().get(0);
1928 String name = pipeRun.getUniqueName("Reducer");
1929 reducer.setName(name);
1930 pipeRun.addChild(reducer);
1931 other.addChild(ocp);
1932 reducer.setAlternativePipeRun(other);
1934 previous.setNext(pcp);
1935 pcp.setPrevious(previous);
1936 ocp.setPrevious(previous);
1940 next.setPrevious(ocp);
1943 String name = other.getUniqueName("Reducer");
1944 reducer.setName(name);
1945 other.addChild(reducer);
1946 pipeRun.addChild(ocp);
1947 reducer.setAlternativePipeRun(pipeRun);
1951 pcp.setPrevious(next);
1952 ocp.setPrevious(next);
1954 pcp.setNext(previous);
1955 ocp.setNext(previous);
1956 previous.setPrevious(ocp);