1 package org.simantics.plant3d.scenegraph.controlpoint;
3 import java.util.ArrayList;
4 import java.util.Collection;
7 import javax.vecmath.Point3d;
8 import javax.vecmath.Quat4d;
9 import javax.vecmath.Vector3d;
11 import org.simantics.g3d.math.MathTools;
12 import org.simantics.plant3d.scenegraph.InlineComponent;
13 import org.simantics.plant3d.scenegraph.P3DRootNode;
14 import org.simantics.plant3d.scenegraph.PipeRun;
15 import org.simantics.plant3d.scenegraph.PipelineComponent;
16 import org.simantics.plant3d.scenegraph.TurnComponent;
17 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
18 import org.simantics.plant3d.utils.ComponentUtils;
19 import org.simantics.utils.datastructures.Pair;
20 import org.simantics.utils.ui.ErrorLogger;
22 public class PipingRules {
23 private static final boolean DEBUG = false;
24 private static final boolean DUMMY = false;
26 private static double MIN_TURN_ANGLE = 0.001; // Threshold for removing turn components.
27 private static double ALLOWED_OFFSET = 0.001; // Allowed offset for directed path legs
28 private static double MIN_INLINE_LENGTH = 0.0005; // Minimum length of inline components, when component removal is not allowed.
30 private static final int REMOVE_NONE = 0;
31 private static final int REMOVE_START = 1;
32 private static final int REMOVE_END = 2;
33 private static final int REMOVE_BOTH = 3;
36 // PathLeg iteration indicator. NEXT_S > NEXT > NONE PREV_S > PREV > NONE
37 private enum PathLegUpdateType {
38 NONE, // Only current path leg needs to be updated (for example, inline comp was moved)
39 PREV, // Current and previous path leg need to be updated
40 NEXT, // Current and next path leg need to be updated
41 PREV_S, // Current and previous two path legs need to be updated (turn was moved, which affect other path leg end turns, and thus following path legs
42 NEXT_S // Current and next two path legs need to be updated
45 private static boolean enabled = true; //
46 private static boolean updating = false;
47 private static boolean allowInsertRemove = true;
48 private static boolean triedIR = false;
51 private static List<PipeControlPoint> requestUpdates = new ArrayList<PipeControlPoint>();
52 private static List<PipeControlPoint> currentUpdates = new ArrayList<PipeControlPoint>();
54 private static Object updateMutex = new Object();
55 private static Object ruleMutex = new Object();
57 public static void requestUpdate(PipeControlPoint pcp) {
58 if (!PipingRules.enabled)
60 if (DEBUG) System.out.println("PipingRules request " + pcp);
61 synchronized (updateMutex) {
62 if (!requestUpdates.contains(pcp))
63 requestUpdates.add(pcp);
67 public static boolean update() throws Exception {
68 if (!PipingRules.enabled)
71 List<PipeControlPoint> temp;
72 synchronized(updateMutex) {
73 if (requestUpdates.size() == 0)
76 temp = new ArrayList<PipeControlPoint>(requestUpdates.size());
77 temp.addAll(requestUpdates);
78 requestUpdates.clear();
80 synchronized (ruleMutex) {
81 currentUpdates.clear();
82 currentUpdates.addAll(temp);
83 // TODO : we should remove already processed control points from currentUpdates after each _positionUpdate call.
84 for (PipeControlPoint pcp : currentUpdates)
85 _positionUpdate(pcp, true);
86 currentUpdates.clear();
88 synchronized(updateMutex) {
89 requestUpdates.removeAll(temp);
95 public static boolean positionUpdate(PipeControlPoint pcp) throws Exception {
97 return positionUpdate(pcp, true);
100 public static boolean positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception {
101 synchronized (ruleMutex) {
102 currentUpdates.add(pcp);
103 boolean b = _positionUpdate(pcp, allowIR);
104 currentUpdates.clear();
110 private static boolean _positionUpdate(PipeControlPoint pcp, boolean allowIR) throws Exception {
111 if (updating || !enabled)
113 if (pcp.getPipeRun() == null)
116 if (DEBUG) System.out.println("PipingRules " + pcp);
118 allowInsertRemove = allowIR;
120 validate(pcp.getPipeRun());
121 if (pcp.getParentPoint() != null)
122 pcp = pcp.getParentPoint();
123 if (pcp.asPathLegEnd()) {
124 updatePathLegEndControlPoint(pcp); // FIXME: Rules won't work properly, if they are not run twice.
125 //updatePathLegEndControlPoint(pcp);
127 updateInlineControlPoint(pcp);
128 //updateInlineControlPoint(pcp);
130 validate(pcp.getPipeRun());
131 if (!allowInsertRemove)
136 // System.out.println("PipingRules done " + pcp);
140 public static void setEnabled(boolean enabled) {
141 PipingRules.enabled = enabled;
143 synchronized (ruleMutex) {
144 currentUpdates.clear();
149 public static boolean isEnabled() {
153 public static class ExpandIterInfo {
154 // these two are turn control points
155 private PipeControlPoint start;
156 private PipeControlPoint end;
159 public ExpandIterInfo() {
163 public ExpandIterInfo(PipeControlPoint tcp, int type) {
164 if (type == REMOVE_START)
171 public ExpandIterInfo(PipeControlPoint start, PipeControlPoint end) {
174 this.type = REMOVE_BOTH;
177 public PipeControlPoint getEnd() {
181 public void setEnd(PipeControlPoint end) {
185 public PipeControlPoint getStart() {
189 public void setStart(PipeControlPoint start) {
193 public int getType() {
197 public void setType(int type) {
203 private static void updatePathLegEndControlPoint(PipeControlPoint pcp) throws Exception {
205 System.out.println("PipingRules.updatePathLegEndControlPoint() " + pcp);
206 if (pcp.getNext() != null) {
207 updatePathLegNext(pcp, pcp, PathLegUpdateType.NEXT_S);
209 if (pcp.getPrevious() != null) {
210 updatePathLegPrev(pcp, pcp, PathLegUpdateType.PREV_S);
215 private static void updateInlineControlPoint(PipeControlPoint pcp) throws Exception {
217 System.out.println("PipingRules.updateInlineControlPoint() " + pcp);
218 PipeControlPoint start = pcp.findPreviousEnd();
219 updatePathLegNext(start, pcp, PathLegUpdateType.NONE);
221 if (pcp.isOffset()) {
222 // Adjusting the rotation angle of an offset component may change variable angle turns
223 PipeControlPoint end = pcp.findNextEnd();
224 if (end.isVariableAngle()) {
225 updatePathLegNext(end, end, PathLegUpdateType.NONE);
227 if (start.isVariableAngle()) {
228 updatePathLegPrev(start, start, PathLegUpdateType.NONE);
233 private static PipeControlPoint insertElbow(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos) throws Exception{
235 System.out.println("PipingRules.insertElbow() " + pcp1 + " " + pcp2 + " " + pos);
236 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
238 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getDualSub()) {
239 pcp1 = pcp1.getDualSub();
240 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
241 PipeControlPoint t = pcp1;
244 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getDualSub() && pcp2.getNext() == pcp1) {
245 PipeControlPoint t = pcp1;
246 pcp1 = pcp2.getDualSub();
249 throw new RuntimeException();
251 TurnComponent elbow = ComponentUtils.createTurn((P3DRootNode)pcp1.getRootNode());
252 PipeControlPoint pcp = elbow.getControlPoint();
253 if (pcp1.isDualInline())
254 pcp1 = pcp1.getDualSub();
255 String name = pcp1.getPipeRun().getUniqueName("Elbow");
257 pcp1.getPipeRun().addChild(elbow);
259 pcp.insert(pcp1, pcp2);
261 pcp.setWorldPosition(pos);
262 validate(pcp.getPipeRun());
266 private static PipeControlPoint insertStraight(PipeControlPoint pcp1, PipeControlPoint pcp2, Vector3d pos, double length) throws Exception {
268 System.out.println("PipingRules.insertStraight() " + pcp1 + " " + pcp2 + " " + pos);
269 if (pcp1.getNext() == pcp2 && pcp2.getPrevious() == pcp1) {
271 } else if (pcp1.getNext() == pcp2 && pcp1.isDualInline() && pcp2.getPrevious() == pcp1.getDualSub()) {
272 pcp1 = pcp1.getDualSub();
273 } else if (pcp1.getPrevious() == pcp2 && pcp2.getNext() == pcp1) {
274 PipeControlPoint t = pcp1;
277 } else if (pcp2.isDualInline() && pcp1.getPrevious() == pcp2.getDualSub() && pcp2.getNext() == pcp1) {
278 PipeControlPoint t = pcp1;
279 pcp1 = pcp2.getDualSub();
282 throw new RuntimeException();
284 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp1.getRootNode());
285 PipeControlPoint scp = component.getControlPoint();
286 if (pcp1.isDualInline())
287 pcp1 = pcp1.getDualSub();
288 String name = pcp1.getPipeRun().getUniqueName("Pipe");
289 component.setName(name);
290 pcp1.getPipeRun().addChild(component);
292 scp.insert(pcp1, pcp2);
294 scp.setWorldPosition(pos);
295 Vector3d dir = new Vector3d();
296 dir.sub(pcp2.getWorldPosition(), pcp1.getWorldPosition());
297 scp.orientToDirection(dir);
298 scp.setLength(length);
299 validate(scp.getPipeRun());
303 private static PipeControlPoint insertStraight(PipeControlPoint pcp, Direction direction , Vector3d pos, double length) throws Exception {
305 System.out.println("PipingRules.insertStraight() " + pcp + " " + direction + " " + pos);
307 InlineComponent component = ComponentUtils.createStraight((P3DRootNode)pcp.getRootNode());
308 PipeControlPoint scp = component.getControlPoint();
309 if (pcp.isDualInline() && direction == Direction.NEXT)
310 pcp = pcp.getDualSub();
311 String name = pcp.getPipeRun().getUniqueName("Pipe");
312 component.setName(name);
313 pcp.getPipeRun().addChild(component);
315 scp.insert(pcp,direction);
317 scp.setWorldPosition(pos);
318 scp.setLength(length);
319 validate(scp.getPipeRun());
323 private static void updatePathLegNext(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
324 UpdateStruct2 us = createUS(start, Direction.NEXT, 0, new ArrayList<ExpandIterInfo>(), updated);
326 System.out.println("Null update struct " + start);
329 updatePathLeg(us, lengthChange);
332 private static void updatePathLegPrev(PipeControlPoint start, PipeControlPoint updated, PathLegUpdateType lengthChange) throws Exception {
333 UpdateStruct2 us = createUS(start, Direction.PREVIOUS, 0, new ArrayList<ExpandIterInfo>(), updated);
335 System.out.println("Null update struct " + start);
338 updatePathLeg(us, lengthChange);
341 private static class UpdateStruct2 {
342 public PipeControlPoint start;
343 public Vector3d startPoint;
344 public ArrayList<PipeControlPoint> list;
345 public PipeControlPoint end;
346 public Vector3d endPoint;
348 public Vector3d offset;
349 public boolean hasOffsets;
351 public boolean reversed;
352 public ArrayList<ExpandIterInfo> toRemove;
353 public PipeControlPoint updated;
355 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) {
356 if (start == null || end == null)
357 throw new NullPointerException();
359 this.startPoint = startPoint;
362 this.endPoint = endPoint;
364 this.offset = offset;
365 this.hasOffsets = hasOffsets;
367 this.reversed = reversed;
368 this.toRemove = toRemove;
369 this.updated = updated;
371 if (!MathTools.isValid(startPoint) ||
372 !MathTools.isValid(endPoint) ||
373 !MathTools.isValid(dir)) {
374 throw new RuntimeException();
378 public String toString() {
379 return start + " " + end+ " " + dir + " " + hasOffsets + " " + offset + " " + iter + " " + toRemove.size();
385 * Calculate offset based on a given fixed component direction.
387 * The desired component direction is provided as an input to this method,
388 * unlike the direction vector that is calculated by calculateOffset.
390 * The returned offset vector is always perpendicular to the given direction
393 * @param startPoint Start point of leg
394 * @param endPoint End point of leg
395 * @param start Starting component of leg
396 * @param list Inline components between start and end
397 * @param end Ending component of leg
398 * @param dir Direction at which the offset is calculated
399 * @param offset A vector object to receive the offset vector values
400 * @return True if offsets are present
402 public static boolean calculateDirectedOffset(Vector3d startPoint, Vector3d endPoint, PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, Vector3d dir, Vector3d offset) {
403 return calculateOffset(startPoint, endPoint, start, list, end, dir, offset, true);
407 * Calculate offset and direction vectors for a path leg so that the given chain
408 * of components starts and ends at the given coordinates
410 * The returned direction and offset vectors are perpendicular to each other.
412 * @param startPoint Start point of the leg
413 * @param endPoint End point of the leg
414 * @param start Starting component of the leg
415 * @param list Inline components between start and end
416 * @param end Ending component of the leg
417 * @param dir A vector object to receive the component direction vector
418 * @param offset A vector object to receive the offset vector
419 * @return True if offsets are present
421 public static boolean calculateOffset(Vector3d startPoint, Vector3d endPoint, PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, Vector3d dir, Vector3d offset) {
422 return calculateOffset(startPoint, endPoint, start, list, end, dir, offset, false);
425 private static boolean calculateOffset(Vector3d startPoint, Vector3d endPoint, PipeControlPoint start, ArrayList<PipeControlPoint> list, PipeControlPoint end, Vector3d dir, Vector3d offset, boolean directed) {
426 List<PipeControlPoint> offsets = getOffsetPoints(start, list);
427 if (offsets.size() == 0) {
428 setZeroOffset(startPoint, endPoint, dir, offset);
431 Vector3d sp = new Vector3d(startPoint);
432 Point3d ep = new Point3d(endPoint);
438 double l = dir.lengthSquared();
439 if (l > MathTools.NEAR_ZERO)
440 dir.scale(1.0/Math.sqrt(l));
445 offset.set(0.0, 0.0, 0.0);
447 for (PipeControlPoint icp : offsets) {
448 Vector3d v = icp.getSizeChangeOffsetVector(dir);
455 Point3d nep = new Point3d(endPoint);
457 if (nep.distance(ep) < 0.0000000001 || iter <= 0) {
464 l = dir.lengthSquared();
465 if (l > MathTools.NEAR_ZERO)
466 dir.scale(1.0/Math.sqrt(l));
470 System.out.println("calcOffset s:"+ startPoint + " e:" + endPoint + " d:" + dir + " o:"+offset) ;
476 public static void setZeroOffset(Vector3d startPoint, Vector3d endPoint, Vector3d dir, Vector3d offset) {
479 double l = dir.lengthSquared();
480 if (l > MathTools.NEAR_ZERO)
481 dir.scale(1.0/Math.sqrt(l));
482 offset.set(0.0, 0.0, 0.0);
485 public static List<PipeControlPoint> getOffsetPoints(PipeControlPoint start, ArrayList<PipeControlPoint> list) {
486 List<PipeControlPoint> offsets = new ArrayList<PipeControlPoint>(list.size());
487 // Only start offset affects the calculation
488 if (start.isOffset())
490 for (PipeControlPoint icp : list) {
491 if (icp.isOffset()) {
493 } else if (icp.isDualSub())
494 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
499 private static UpdateStruct2 createUS(PipeControlPoint start, Direction direction, int iter, ArrayList<ExpandIterInfo> toRemove, PipeControlPoint updated) {
500 ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
501 PipeControlPoint end = null;
502 if (direction == Direction.NEXT) {
503 end = start.findNextEnd(list);
505 ArrayList<PipeControlPoint> prevList = new ArrayList<PipeControlPoint>();
506 PipeControlPoint tend = start.findPreviousEnd(prevList);
507 for (PipeControlPoint icp : prevList) {
508 if (icp.isDualSub()) {
509 list.add(0, icp.getParentPoint());
519 boolean hasOffsets = false;
520 Vector3d offset = new Vector3d();
521 Vector3d startPoint = start.getWorldPosition();
522 Vector3d endPoint = end.getWorldPosition();
523 Vector3d dir = new Vector3d();
524 hasOffsets = calculateOffset(startPoint, endPoint, start, list, end, dir, offset);
525 return new UpdateStruct2(start, startPoint, list, end, endPoint, dir, offset, hasOffsets, iter, direction == Direction.PREVIOUS, toRemove, updated);
528 private static Vector3d pathLegDirection(PipeControlPoint start) {
529 ArrayList<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
530 PipeControlPoint end = start.findNextEnd(list);
532 return start.getDirection(Direction.NEXT);
535 Vector3d offset = new Vector3d();
536 Vector3d startPoint = start.getWorldPosition();
537 Vector3d endPoint = end.getWorldPosition();
538 Vector3d dir = new Vector3d();
539 calculateOffset(startPoint, endPoint, start, list, end, dir, offset);
543 private static boolean asDirected(PipeControlPoint pcp, Direction direction) {
544 if (pcp.isDirected())
546 if (pcp.asFixedAngle()) {
547 if (!pcp._getReversed())
548 return direction == Direction.NEXT;
550 return direction == Direction.PREVIOUS;
555 private static Vector3d direction(PipeControlPoint pcp, Direction direction) {
556 return pcp.getDirection(direction);
559 private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
562 if (lengthChange == PathLegUpdateType.NONE) {
566 updatePathLeg(u, lengthChange, rs, re);
569 private static void updatePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange, boolean rs, boolean re) throws Exception {
571 if (asDirected(u.start, Direction.NEXT))
573 if (asDirected(u.end, Direction.PREVIOUS))
576 setErrorForce(u.start, null);
578 setErrorForce(u.end, null);
579 for (PipeControlPoint pcp : u.list)
580 setErrorForce(pcp, null);
583 updateFreePathLeg(u, lengthChange);
586 updateDirectedPathLeg(u, lengthChange);
589 updateDualDirectedPathLeg(u, lengthChange);
595 private static void updateFreePathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
597 System.out.println("PipingRules.updateFreePipeRun " + u + " " + lengthChange);
598 checkExpandPathLeg(u, lengthChange);
599 if (u.start.isInline() || u.end.isInline() || u.start.asFixedAngle()|| u.end.asFixedAngle())
600 processPathLeg(u, true, false);
603 private static void updateInlineControlPoints(UpdateStruct2 u, boolean checkSizes) throws Exception{
605 System.out.println("PipingRules.updateInlineControlPoints() " + u);
607 Vector3d start = new Vector3d(u.startPoint);
608 Vector3d end = new Vector3d(u.endPoint);
611 // create offsets for leg ends.
612 if (u.start.isTurn())
613 MathTools.mad(start, u.dir, u.start.getInlineLength());
615 MathTools.mad(end, u.dir, -u.end.getInlineLength());
618 boolean recalcline = false;
620 Vector3d sp = new Vector3d(start);
621 Vector3d ep = new Vector3d(end);
624 if (u.start.isOffset()) {
625 Vector3d offset = u.start.getSizeChangeOffsetVector(u.dir);
626 updateOffsetPoint(u.start, offset);
631 for (PipeControlPoint icp : u.list) {
632 updateInlineControlPoint(icp, sp, ep, u.dir);
633 if (icp.isOffset()) {
634 // TODO : offset vector is already calculated and should be cached
635 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
636 updateOffsetPoint(icp, offset);
645 // Collect all path leg points for updating variable length components. This list will also contain leg ends (usually turns)
646 ArrayList<PipeControlPoint> pathLegPoints = new ArrayList<>();
647 // Collect all fixed length components with their offsets.
648 ArrayList<Pair<PipeControlPoint,Vector3d>> fixedLengthPoints = new ArrayList<>();
650 pathLegPoints.add(u.start);
651 fixedLengthPoints.add(new Pair<PipeControlPoint, Vector3d>(u.start, new Vector3d()));
652 Vector3d off = new Vector3d();
653 for (PipeControlPoint icp : u.list) {
654 pathLegPoints.add(icp);
655 updateBranchControlPointBranches(icp);
656 if (icp.isOffset()) {
657 fixedLengthPoints.add(new Pair<PipeControlPoint, Vector3d>(icp, new Vector3d(off)));
658 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
660 } else if (!icp.isVariableLength()) {
661 fixedLengthPoints.add(new Pair<PipeControlPoint, Vector3d>(icp, new Vector3d(off)));
664 pathLegPoints.add(u.end);
665 fixedLengthPoints.add(new Pair<PipeControlPoint, Vector3d>(u.end, new Vector3d(off)));
667 sp = new Vector3d(start);
668 ep = new Vector3d(end);
671 updateFixedLengths(fixedLengthPoints, sp, ep, u.dir);
673 for (int i = 0; i < pathLegPoints.size(); i++) {
674 PipeControlPoint icp = pathLegPoints.get(i);
676 PipeControlPoint prev = i > 0 ? pathLegPoints.get(i - 1) : null;
677 PipeControlPoint next = i < pathLegPoints.size() - 1 ? pathLegPoints.get(i + 1) : null;
679 if (prev != null && prev.isDualInline())
680 prev = prev.getDualSub();
682 if (icp.isVariableLength()) {
683 if (prev != null && next != null) {
684 recalcline = recalcline | updateVariableLength(icp, prev, next);
687 // this is variable length component at the end of the piperun.
688 // the problem is that we want to keep unconnected end of the component in the same
689 // place, but center of the component must be moved.
690 updateVariableLengthEnd(icp, prev != null ? prev : next);
692 } else if (prev != null && !prev.isVariableLength()) {
693 // If this and previous control point are not variable length pcps,
694 // we'll have to check if there is no empty space between them.
695 // I there is, we'll have to create new variable length component between them.
696 recalcline = recalcline | possibleVaribleLengthInsert(icp, prev);
698 if (icp.isOffset()) {
699 // TODO : offset vector is already calculated and should be cached
700 Vector3d offset = icp.getSizeChangeOffsetVector(u.dir);
708 u.start.findNextEnd(u.list);
711 sp = new Vector3d(u.startPoint);
712 ep = new Vector3d(u.endPoint);
714 double pathLegLength = MathTools.distance(sp, ep);
715 double availableLength = pathLegLength;
716 if (u.start.isTurn())
717 availableLength -= u.start.getInlineLength();
719 availableLength -= u.end.getInlineLength();
720 for (PipeControlPoint pcp : u.list) {
721 if (!pcp.isVariableLength())
722 availableLength-= pcp.getLength();
724 if (availableLength < 0.0) {
725 setError(u.start, "Not enough available space");
726 setError(u.end, "Not enough available space");
727 for (PipeControlPoint pcp : u.list)
728 setError(pcp, "Not enough available space");
730 // System.out.println(u.start.getPipelineComponent().toString() + " " + pathLegLength + " " + availableLength + " " + u.end.getPipelineComponent().toString() + " " + u.start.getInlineLength() + " " + u.end.getInlineLength());
734 private enum Gap{ATTACHED,OVERLAP,SPACE};
736 private static class GapObj {
740 Pair<PipeControlPoint,Vector3d> pcp1;
741 Pair<PipeControlPoint,Vector3d> pcp2;
744 private static void updateFixedLengths(List<Pair<PipeControlPoint,Vector3d>> fixedLengthPoints, Vector3d s, Vector3d e, Vector3d dir) {
745 double totalLength = MathTools.distance(s, e);
746 double reservedLength = 0.0;
747 List<Double> distances = new ArrayList<>(fixedLengthPoints.size());
749 for (int i = 1; i < fixedLengthPoints.size()-1; i++) {
750 Pair<PipeControlPoint,Vector3d> pcp = fixedLengthPoints.get(i);
751 reservedLength += pcp.first.getLength();
752 Vector3d p = pcp.first.getWorldPosition();
754 double d= MathTools.distance(s, p);
757 distances.add(totalLength);
759 if (totalLength >= reservedLength) {
760 // There is enough space for all fixed length components.
761 List<GapObj> gaps = new ArrayList<>(fixedLengthPoints.size()-1);
763 // Analyze gaps between components
764 for (int i = 0; i < fixedLengthPoints.size()-1; i++) {
765 Pair<PipeControlPoint,Vector3d> pcp1 = fixedLengthPoints.get(i);
766 Pair<PipeControlPoint,Vector3d> pcp2 = fixedLengthPoints.get(i+1);
767 double d1 = distances.get(i);
768 double d2 = distances.get(i+1);
769 double ld1 = i == 0 ? 0.0 :pcp1.first.getInlineLength();
770 double ld2 = i == fixedLengthPoints.size()-2 ? 0.0 : pcp2.first.getInlineLength();
772 double e1 = d1 + ld1; // End of comp1
773 double s2 = d2 - ld2; // Start of comp2
774 double diff =s2 - e1;
775 GapObj obj = new GapObj();
779 if (diff < -MIN_INLINE_LENGTH) {
780 obj.gap = Gap.OVERLAP;
782 } else if (diff > MIN_INLINE_LENGTH) {
785 obj.gap = Gap.ATTACHED;
789 // If there are no overlaps, there is nothing to do.
792 // Get rid of overlapping components by using closest available free spaces.
793 for (int i = 0; i < gaps.size(); i++) {
794 GapObj gapObj = gaps.get(i);
795 if (gapObj.gap != Gap.OVERLAP)
797 double curr = gapObj.d;
799 while (d < gaps.size() && curr < -MIN_INLINE_LENGTH) {
800 GapObj next = i+d < gaps.size() ? gaps.get(i+d) : null;
801 GapObj prev = i-d >= 0 ? gaps.get(i-d) : null;
802 if (next != null && next.gap == Gap.SPACE) {
803 double move = Math.min(-curr, next.d);
806 if (next.d < MIN_INLINE_LENGTH)
807 next.gap = Gap.ATTACHED;
808 Vector3d mv = new Vector3d(dir);
811 for (int j = i ; j < i+d; j++) {
812 Pair<PipeControlPoint,Vector3d> pcp = gaps.get(j).pcp2;
813 Vector3d p = new Vector3d(pcp.first.getWorldPosition());
815 pcp.first.setWorldPosition(p);
818 else if (prev != null && prev.gap == Gap.SPACE) {
819 double move = Math.min(-curr, prev.d);
822 if (prev.d < MIN_INLINE_LENGTH)
823 prev.gap = Gap.ATTACHED;
824 Vector3d mv = new Vector3d(dir);
827 for (int j = i ; j > i-d; j--) {
828 Pair<PipeControlPoint,Vector3d> pcp = gaps.get(j).pcp1;
829 Vector3d p = new Vector3d(pcp.first.getWorldPosition());
831 pcp.first.setWorldPosition(p);
840 for (int i = 1; i < fixedLengthPoints.size()-1; i++) {
841 Pair<PipeControlPoint,Vector3d> prev = i == 0 ? null : fixedLengthPoints.get(i-1);
842 Pair<PipeControlPoint,Vector3d> curr = fixedLengthPoints.get(i);
843 Pair<PipeControlPoint,Vector3d> next = i == fixedLengthPoints.size() -1 ? null : fixedLengthPoints.get(i+1);
844 updateFixedLength(curr, prev, next, s,e, dir);
849 private static void updateFixedLength(Pair<PipeControlPoint,Vector3d> icp, Pair<PipeControlPoint,Vector3d> prev, Pair<PipeControlPoint,Vector3d> next, Vector3d s, Vector3d e, Vector3d dir) {
851 checkOverlap(prev, icp, dir,true);
854 checkOverlap(icp, next, dir,true);
857 private static boolean checkOverlap(Pair<PipeControlPoint,Vector3d> icp, Pair<PipeControlPoint,Vector3d> icp2, Vector3d dir, boolean se) {
858 Vector3d p1 = icp.first.getWorldPosition();
859 Vector3d p2 = icp2.first.getWorldPosition();
862 double u[] = new double[1];
863 MathTools.closestPointOnStraight(p2, p1, dir, u);
868 MathTools.mad(p2, dir, MIN_INLINE_LENGTH);
869 icp2.first.setWorldPosition(p2);
871 double d = MathTools.distance(p1, p2);
872 double r = icp.first.getInlineLength() + icp2.first.getInlineLength();
874 if ((d-r) < - MIN_INLINE_LENGTH) {
876 setError(icp.first, "Overlapping");
877 setError(icp2.first, "Overlapping");
885 * Overrides current error of a component
889 private static void setErrorForce(PipeControlPoint pcp, String error) {
890 PipelineComponent comp = pcp.getPipelineComponent();
893 comp.setError(error);
897 * Sets error for a component, if there is no existing error.
901 private static void setError(PipeControlPoint pcp, String error) {
902 PipelineComponent comp = pcp.getPipelineComponent();
905 if (comp.getError() != null)
907 comp.setError(error);
910 private static boolean updateVariableLength(PipeControlPoint icp, PipeControlPoint prev, PipeControlPoint next) {
911 Vector3d prevPos = prev.getWorldPosition();
912 Vector3d nextPos = next.getWorldPosition();
914 Vector3d dir = new Vector3d(nextPos);
916 double l = dir.length(); // distance between control points
917 double l2prev = prev.getInlineLength(); // distance taken by components
918 double l2next = next.getInlineLength();
919 double l2 = l2prev + l2next;
920 double length = l - l2; // true length of the variable length component
921 if (length >= MIN_INLINE_LENGTH) { // check if there is enough space for variable length component.
924 dir.scale(length * 0.5 + l2prev); // calculate center position of the component
926 icp.setWorldPosition(dir);
927 icp.setLength(length);
930 // components leave no space to the component and it must be removed
931 if (icp.isDeletable()) {
932 if (!allowInsertRemove) {
933 icp.setLength(MIN_INLINE_LENGTH);
934 setError(icp, "Not enough available space");
939 System.out.println("PipingRules.updateVariableLength removing " + icp);
943 icp.setLength(MIN_INLINE_LENGTH);
944 icp.getPipelineComponent().setError("Not enough available space");
950 private static boolean possibleVaribleLengthInsert(PipeControlPoint icp, PipeControlPoint prev) throws Exception{
951 Vector3d currentPos = icp.getWorldPosition();
952 Vector3d prevPos = prev.getWorldPosition();
953 Vector3d dir = new Vector3d(currentPos);
955 double l = dir.lengthSquared();
956 double l2prev = prev.getInlineLength();
957 double l2next = icp.getInlineLength();
958 double l2 = l2prev + l2next;
959 double l2s = l2 * l2;
960 double diff = l - l2s;
961 if (diff >= MIN_INLINE_LENGTH) {
962 if (allowInsertRemove) {
964 double length = Math.sqrt(l) - l2; // true length of the variable length component
965 dir.scale(length * 0.5 + l2prev); // calculate center position of the component
967 insertStraight(prev, icp, dir, length);
976 private static void updateVariableLengthEnd(PipeControlPoint icp, PipeControlPoint prev) {
977 Vector3d currentPos = icp.getWorldPosition();
978 Vector3d prevPos = prev.getWorldPosition();
980 Vector3d dir = new Vector3d();
981 dir.sub(currentPos, prevPos);
984 synchronized (ruleMutex) {
985 simple = currentUpdates.contains(icp);
989 // Update based on position -> adjust length
990 double currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
991 icp.setLength(currentLength);
993 // Update based on neighbour movement -> adjust length and position, so that free end stays in place.
994 double currentLength = icp.getLength();
995 if (currentLength < MathTools.NEAR_ZERO) {
996 currentLength = (dir.length() - prev.getInlineLength()) * 2.0;
999 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
1001 Point3d endPos = new Point3d(dir);
1002 endPos.scale(currentLength * 0.5);
1003 endPos.add(currentPos); // this is the free end of the component
1005 double offset = prev.getInlineLength();
1006 Point3d beginPos = new Point3d(dir);
1007 beginPos.scale(offset);
1008 beginPos.add(prevPos); // this is the connected end of the component
1010 double l = beginPos.distance(endPos);
1012 if (Double.isNaN(l))
1013 System.out.println("Length for " + icp + " is NaN");
1016 beginPos.add(dir); // center position
1019 System.out.println("PipingRules.updateInlineControlPoints() setting variable length to " + l);
1022 icp.setWorldPosition(new Vector3d(beginPos));
1027 * Recalculates offset vector based on current direction, and calls checkExpandPathLeg
1032 private static void ppNoOffset(UpdateStruct2 u, boolean updateEnds) throws Exception {
1034 System.out.println("PipingRules.ppNoOffset() " + u);
1035 Vector3d offset = new Vector3d();
1037 for (PipeControlPoint icp : u.list) {
1038 if (icp.isOffset()) {
1039 offset.add(icp.getSizeChangeOffsetVector(u.dir));
1040 } else if (icp.isDualSub())
1041 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1045 checkExpandPathLeg(u, PathLegUpdateType.NONE, updateEnds);
1048 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 {
1050 System.out.println("PipingRules.ppNoDir() " + start + " " + end + " " + iter + " " + toRemove.size());
1051 // FIXME : extra loop (dir should be calculated here)
1052 Vector3d dir = new Vector3d();
1053 Vector3d offset = new Vector3d();
1054 hasOffsets = calculateOffset(startPoint, endPoint, start, list, end, dir, offset);
1055 ppNoOffset(new UpdateStruct2(start, startPoint, list, end, endPoint, dir, null, hasOffsets, iter, reversed, toRemove, updated),true);
1058 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1059 checkExpandPathLeg(u, lengthChange, u.updated.isInline() && u.updated.isOffset());
1062 private static void checkExpandPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange, boolean updateEnds) throws Exception {
1064 System.out.println("PipingRules.checkExpandPathLeg() " + u + " " + lengthChange);
1065 if (lengthChange != PathLegUpdateType.NONE) {
1066 // FIXME : turns cannot be checked before inline cps are updated,
1067 // since their position affects calculation of turns
1068 processPathLeg(u, updateEnds, false);
1069 int type = checkTurns(u, lengthChange);
1070 if (type == REMOVE_NONE) {
1071 processPathLeg(u, updateEnds, true);
1073 expandPathLeg(u, type);
1076 processPathLeg(u, updateEnds, true);
1080 private static void updateDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1082 System.out.println("PipingRules.updateDirectedPipeRun() " + u + " " + lengthChange);
1083 PipeControlPoint dcp;
1084 PipeControlPoint other;
1085 boolean canMoveOther = false;
1086 boolean dcpStart = false;
1087 boolean inlineEnd = false;
1089 if (asDirected(u.start, Direction.NEXT)) {
1092 position = u.startPoint;
1095 canMoveOther = true;
1096 inlineEnd = u.end.isInline();
1101 position = u.endPoint;
1103 canMoveOther = true;
1104 inlineEnd = u.start.isInline();
1107 Vector3d directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
1108 if (directedDirection == null) {
1109 //updateTurnControlPointTurn(dcp, dcp.getPrevious(), dcp.getNext());
1110 updateTurnControlPointTurn(dcp, null, null);
1111 directedDirection = direction(dcp, dcpStart ? Direction.NEXT : Direction.PREVIOUS);
1112 if (directedDirection == null) {
1117 Point3d otherPosition = new Point3d(dcpStart ? u.endPoint : u.startPoint);
1119 Vector3d dir = dcp.getDirection(dcpStart ? Direction.NEXT : Direction.PREVIOUS);
1123 Vector3d offset = new Vector3d();
1124 calculateDirectedOffset(u.startPoint, u.endPoint, u.start, u.list, u.end, dir, offset);
1129 otherPosition.sub(offset);
1131 otherPosition.add(offset);
1134 double mu[] = new double[2];
1137 Vector3d t = new Vector3d();
1139 closest = MathTools.closestPointOnStraight(otherPosition, position, directedDirection, mu);
1140 t.sub(closest, otherPosition);
1142 double distance = t.length();
1143 boolean aligned = (distance < ALLOWED_OFFSET);
1144 double requiredSpace = 0.0;
1145 if (other.isVariableAngle()) {
1146 requiredSpace = spaceForTurn(other, dcp);
1148 if (mu[0] < requiredSpace) {
1149 // At the moment, if next component is directly behind the nozzle, we must force moving the other component.
1150 // Trying to solve the situation by adding new turn creates infinite loop...
1152 canMoveOther = true;
1155 //if (u.start.isInline() || u.end.isInline() || u.start.asFixedAngle() || u.end.asFixedAngle())
1156 // processPathLeg(u, true, false);
1157 checkExpandPathLeg(u, lengthChange, inlineEnd || u.start.isInline() || u.end.isInline() || u.start.asFixedAngle() || u.end.asFixedAngle());
1162 PipeControlPoint nextToMoved;
1164 if (u.list.size() > 0)
1166 nextToMoved = u.list.get(0);
1168 nextToMoved = u.list.get(u.list.size() - 1);
1170 nextToMoved = u.end;
1172 nextToMoved = u.start;
1173 if (other.isVariableAngle()) {
1175 // TODO calculate needed space from next run end.
1176 if (mu[0] < requiredSpace) {
1178 closest.set(u.startPoint);
1180 closest.set(u.endPoint);
1182 Vector3d v = new Vector3d(directedDirection);
1183 v.scale(requiredSpace);
1189 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + closest);
1191 // Not aligned - we need to recalculate the offset to reflect new end points.
1194 offset = new Vector3d();
1195 Vector3d newDir = new Vector3d();
1196 calculateDirectedOffset(position, closest, u.start, u.list, u.end, newDir, offset);
1197 closest.add(offset);
1199 offset = new Vector3d();
1202 other.setWorldPosition(closest);
1205 checkExpandPathLeg(new UpdateStruct2(u.start, u.startPoint, u.list, u.end, new Vector3d(closest), directedDirection, offset, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated), PathLegUpdateType.NONE, true);
1206 if (u.end.getNext() != null)
1207 updatePathLegNext(u.end, u.updated, PathLegUpdateType.NEXT);
1209 checkExpandPathLeg(new UpdateStruct2(u.start, new Vector3d(closest), u.list, u.end, u.endPoint, directedDirection, offset, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated), PathLegUpdateType.NONE, true);
1210 if (u.start.getPrevious() != null)
1211 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.PREV);
1214 // TODO : calculate needed space from next run end.
1215 if (allowInsertRemove)
1216 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
1220 } else if (other.isNonDirected() && other.getParentPoint() != null) {
1221 // FIXME : this code was for updating branches
1222 Vector3d bintersect = new Vector3d();
1223 PipeControlPoint bcp = other.getParentPoint();
1224 if (bcp != null && canMoveOther) {
1225 Point3d bstart = new Point3d();
1226 Point3d bend = new Point3d();
1227 Vector3d bdir = new Vector3d();
1228 bcp.getInlineControlPointEnds(bstart, bend, bdir);
1229 Vector3d nintersect = new Vector3d();
1231 MathTools.intersectStraightStraight(position, directedDirection, bend, bdir, nintersect, bintersect, mu);
1232 Vector3d dist = new Vector3d(nintersect);
1233 dist.sub(bintersect);
1234 canMoveOther = mu[1] > 0.0 && mu[1] < 1.0 && dist.lengthSquared() < 0.01;
1236 // TODO : endControlPoints are undirected: calculcate
1237 // correct position for it
1238 throw new UnsupportedOperationException("not implemented");
1242 System.out.println("PipingRules.updateDirectedPipeRun() moved end " + other + " to " + bintersect);
1243 // is required branch position is in possible range
1244 bcp.setWorldPosition(bintersect);
1246 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);
1248 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);
1251 // branch cannot be moved into right position, new turn
1252 // / elbow must be inserted
1253 if (allowInsertRemove)
1254 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
1259 } else { // assume that control point cannot be moved, but can
1261 if (allowInsertRemove)
1262 insertElbowUpdate(u, dcp, nextToMoved, dcpStart, position, directedDirection);
1270 private static void updateDualDirectedPathLeg(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1272 System.out.println("PipingRules.updateDualDirectedPipeRun() " + u + " " + lengthChange);
1274 PipeControlPoint dcp1 = u.start;
1275 PipeControlPoint dcp2 = u.end;
1276 Point3d position1 = new Point3d(u.startPoint);
1277 Point3d position2 = new Point3d(u.endPoint);
1279 Vector3d dir = u.start.getDirection(Direction.NEXT), offset = new Vector3d();
1280 calculateDirectedOffset(new Vector3d(position1), new Vector3d(position2), u.start, u.list, u.end, dir, offset);
1282 Point3d position1offset = new Point3d(position1);
1283 position1offset.add(offset);
1284 Point3d position2offset = new Point3d(position2);
1285 position2offset.sub(offset);
1286 Vector3d dir1 = direction(dcp1, Direction.NEXT);
1287 Vector3d dir2 = direction(dcp2, Direction.PREVIOUS);
1289 Vector3d p1 = MathTools.closestPointOnStraight(position1, position2offset, dir2);
1290 Vector3d p2 = MathTools.closestPointOnStraight(position2, position1offset, dir1);
1291 double d1 = position1.distance(new Point3d(p1));
1292 double d2 = position2.distance(new Point3d(p2));
1294 boolean aligned = (d1 < ALLOWED_OFFSET && d2 < ALLOWED_OFFSET);
1300 } else if (allowInsertRemove){
1301 PipeControlPoint dcp;
1302 PipeControlPoint next;
1305 if (u.list.size() > 0)
1306 next = u.list.get(0);
1311 if (u.list.size() > 0)
1312 next = u.list.get(u.list.size() - 1);
1317 p1 = dcp.getWorldPosition();
1318 Vector3d v = new Vector3d();
1324 // Reserve space for 90 deg elbow
1325 double off = dcp1.getPipeRun().getTurnRadius();
1330 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position2offset, dir2);
1332 p2 = MathTools.closestPointOnStraight(new Point3d(p1), position1offset, dir1);
1334 // By default, the elbows are placed next to each other, by using 90 deg angles.
1335 // If the distance between elbows is not enough, we must move the other elbow (and create more shallow angle elbows)
1336 if (MathTools.distance(p1, p2) < off*2.05) {
1340 PipeControlPoint tcp1 = insertElbow(dcp, next, p1);
1341 PipeControlPoint tcp2 = insertElbow(tcp1, next, p2);
1344 System.out.println("PipingRules.updateDualDirectedPipeRun() created two turns " + tcp1 + " " + tcp2);
1347 Vector3d dd = new Vector3d(p2);
1350 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1351 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1353 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1355 updatePathLegPrev(tcp2, u.updated, PathLegUpdateType.NONE);
1357 Vector3d dd = new Vector3d(p1);
1360 updatePathLegNext(tcp1, u.updated, PathLegUpdateType.NONE);
1361 updatePathLegNext(tcp2, u.updated, PathLegUpdateType.NONE);
1363 updatePathLegNext(u.start, u.updated, PathLegUpdateType.NONE);
1365 updatePathLegPrev(u.start, u.updated, PathLegUpdateType.NONE);
1374 private static double spaceForTurn(PipeControlPoint tcp, PipeControlPoint dcp) {
1375 // TODO : if the path legs contain offset, using just positions of opposite path leg ends is not enough.
1376 // TODO : current iterative way for calculating required space may return longer length that is required.
1377 double tr = ((TurnComponent)tcp.getPipelineComponent()).getTurnRadius();
1379 return tr; // space for 90 deg
1380 PipeControlPoint ne = tcp.findNextEnd();
1381 PipeControlPoint pe = tcp.findPreviousEnd();
1382 PipeControlPoint other = null;
1388 return tr; // space for 90 deg
1390 return tr; // space for 90 deg
1391 Vector3d dir = dcp.getDirectedControlPointDirection();
1394 dir2 = pathLegDirection(tcp);
1396 dir2 = pathLegDirection(pe);
1400 double d = dir.dot(dir2);
1402 return 0.0; // point following turn is directly in the front of the nozzle.
1403 else if (d < -0.9999)
1404 return tr*2.0; // point following turn is directly behind the nozzle, in theory, we should return Double.Inf...
1408 Vector3d tp0 = tcp.getPosition();
1410 Vector3d dp = dcp.getWorldPosition();
1412 Vector3d tp = new Vector3d(dir);
1413 tp.scaleAdd(curr, dp);
1414 tcp._setPosition(tp); // no firing of listeners here
1416 dir2 = pathLegDirection(tcp);
1418 dir2 = pathLegDirection(pe);
1422 double a = dir.angle(dir2);
1424 // other is directly between dcp and tcp, a zero angle turn should do
1425 if (Math.PI - a <= MathTools.NEAR_ZERO)
1428 double R = tr * Math.tan(a * 0.5);
1436 tcp._setPosition(tp0); // return the original value
1441 private static void insertElbowUpdate(UpdateStruct2 u, PipeControlPoint dcp, PipeControlPoint next, boolean dcpStart, Vector3d position, Vector3d directedDirection) throws Exception{
1444 // Vector3d closest = new Vector3d(position);
1445 // closest.add(directedDirection);
1447 PipeControlPoint tcp = null;
1448 Vector3d closest = new Vector3d(directedDirection);
1449 closest.scaleAdd(dcp.getPipeRun().getTurnRadius(), position);
1451 tcp = insertElbow(dcp, next, closest);
1453 tcp = insertElbow(next, dcp, closest);
1456 double s = spaceForTurn(tcp,dcp);
1457 Vector3d p = new Vector3d(directedDirection);
1458 p.scaleAdd(s, position);
1463 System.out.println("PipingRules.updateDirectedPipeRun() inserted " + tcp);
1466 // update pipe run from new turn to other end
1467 ppNoDir(tcp, new Vector3d(closest), u.list, u.end, u.endPoint, u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1468 // update pipe run from directed to new turn
1469 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));
1471 // update pipe run from other end to new turn
1472 ppNoDir(u.start, u.startPoint, u.list, tcp, new Vector3d(closest), u.hasOffsets, u.iter, u.reversed, u.toRemove, u.updated);
1473 // update pipe run from new turn to directed
1474 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));
1479 * Checks if turns can be removed (turn angle near zero)
1481 private static int checkTurns(UpdateStruct2 u, PathLegUpdateType lengthChange) throws Exception {
1483 System.out.println("PipingRules.checkTurns() " + u.start + " " + u.end);
1484 boolean startRemoved = false;
1485 boolean endRemoved = false;
1486 if (u.start.isVariableAngle()) {
1487 // this won't work properly if inline control points are not updated
1488 PipeControlPoint startPrev = u.start.getPrevious();
1489 if (startPrev != null) {
1490 double a = updateTurnControlPointTurn(u.start, null, u.dir);
1491 if (a < MIN_TURN_ANGLE && u.start.isDeletable())
1492 startRemoved = true;
1493 else if (lengthChange == PathLegUpdateType.PREV || lengthChange == PathLegUpdateType.PREV_S) {
1494 PathLegUpdateType type;
1495 if (lengthChange == PathLegUpdateType.PREV_S)
1496 type = PathLegUpdateType.PREV;
1498 type = PathLegUpdateType.NONE;
1499 updatePathLegPrev(u.start, u.start, type);
1503 if (u.end.isVariableAngle()) {
1505 PipeControlPoint endNext = u.end.getNext();
1506 if (endNext != null) {
1507 // TODO: u.end, u.dir, null
1508 double a = updateTurnControlPointTurn(u.end, null, null);
1509 if (a < MIN_TURN_ANGLE && u.end.isDeletable())
1511 else if (lengthChange == PathLegUpdateType.NEXT || lengthChange == PathLegUpdateType.NEXT_S) {
1512 PathLegUpdateType type;
1513 if (lengthChange == PathLegUpdateType.NEXT_S)
1514 type = PathLegUpdateType.NEXT;
1516 type = PathLegUpdateType.NONE;
1517 updatePathLegNext(u.end, u.end, type);
1522 System.out.println("PipingRules.checkTurns() res " + startRemoved + " " + endRemoved);
1523 if (!startRemoved && !endRemoved)
1525 if (startRemoved && endRemoved)
1528 return REMOVE_START;
1533 * Expands piperun search over turns that are going to be removed
1536 private static void expandPathLeg(UpdateStruct2 u, int type) throws Exception {
1538 System.out.println("PipingRules.expandPipeline " + u.start + " " + u.end);
1539 ArrayList<PipeControlPoint> newList = new ArrayList<PipeControlPoint>();
1542 throw new RuntimeException("Error in piping rules");
1544 u.toRemove.add(new ExpandIterInfo(u.start, REMOVE_START));
1545 u.start = u.start.findPreviousEnd();
1546 u.startPoint = u.start.getPosition();
1547 u.start.findNextEnd(newList);
1548 newList.addAll(u.list);
1552 u.toRemove.add(new ExpandIterInfo(u.end, REMOVE_END));
1553 u.end = u.end.findNextEnd(newList);
1554 u.endPoint = u.end.getPosition();
1555 u.list.addAll(newList);
1558 u.toRemove.add(new ExpandIterInfo(u.start, u.end));
1559 u.start = u.start.findPreviousEnd();
1560 u.startPoint = u.start.getPosition();
1561 u.start.findNextEnd(newList);
1562 newList.addAll(u.list);
1564 newList = new ArrayList<PipeControlPoint>();
1565 u.end = u.end.findNextEnd(newList);
1566 u.endPoint = u.end.getPosition();
1567 u.list.addAll(newList);
1570 throw new RuntimeException("Error in piping rules");
1573 u.offset = new Vector3d();
1576 for (PipeControlPoint icp : u.list) {
1577 if (icp.isOffset()) {
1578 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1579 } else if (icp.isDualSub())
1580 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1584 System.out.println("PipingRules.expandPipeline expanded " + u.start + " " + u.end);
1586 updatePathLeg(u, PathLegUpdateType.NONE);
1590 * reverts one iteration of turn removing back)
1592 private static void backIter(UpdateStruct2 u) throws Exception {
1595 System.out.println("PipingRules.backIter" + u.start + " " + u.end);
1597 throw new RuntimeException("Error in piping rules");
1598 ExpandIterInfo info = u.toRemove.get(u.toRemove.size() - 1);
1599 u.toRemove.remove(u.toRemove.size() - 1);
1600 if (info.getType() == REMOVE_START || info.getType() == REMOVE_BOTH) {
1601 while (u.list.size() > 0) {
1602 PipeControlPoint icp = u.list.get(0);
1603 if (icp.getPrevious().equals(info.getStart()))
1607 u.start = info.getStart();
1609 if (info.getType() == REMOVE_END || info.getType() == REMOVE_BOTH) {
1610 while (u.list.size() > 0) {
1611 PipeControlPoint icp = u.list.get(u.list.size() - 1);
1612 if (icp.getNext().equals(info.getEnd()))
1616 u.end = info.getEnd();
1618 u.offset = new Vector3d();
1621 for (PipeControlPoint icp : u.list) {
1622 if (icp.isOffset()) {
1623 u.offset.add(icp.getSizeChangeOffsetVector(u.dir));
1624 } else if (icp.isDualSub())
1625 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1633 * Processes pipe run (removes necessary turns and updates run ends)
1635 // private static void processPathLeg(PipeControlPoint start, Point3d
1636 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1637 // end,Point3d endPoint, Vector3d dir,Vector3d offset, boolean
1638 // hasOffsets,int iter, boolean reversed, ArrayList<ExpandIterInfo>
1639 // toRemove) throws TransactionException {
1641 private static void processPathLeg(UpdateStruct2 u) throws Exception {
1643 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1644 processPathLeg(u, true, true);
1647 private static void processPathLeg(UpdateStruct2 u, boolean updateEnds, boolean updateInline) throws Exception {
1649 System.out.println("PipingRules.processPathLeg " + (updateEnds ? "ends " : "") + (updateInline ? "inline " : "") + u.start + " " + u.end);
1651 if (u.toRemove.size() > 0) {
1652 for (ExpandIterInfo info : u.toRemove) {
1653 if (info.getStart() != null) {
1655 System.out.println("PipingRules.processPathLeg removing start " + info.getStart());
1656 info.getStart()._remove();
1658 if (info.getEnd() != null) {
1660 System.out.println("PipingRules.processPathLeg removing end " + info.getEnd());
1661 info.getEnd()._remove();
1664 // ControlPointTools.removeControlPoint may remove more than one CP;
1665 // we must populate inline CP list again.
1667 u.start.findNextEnd( u.list);
1669 // FIXME : inline CPs are update twice because their positions must be
1670 // updated before and after ends.
1671 updateInlineControlPoints(u, false);
1674 if (u.start.isTurn()) {
1675 //updateTurnControlPointTurn(u.start, u.start.getPrevious(), u.start.getNext());
1676 updateTurnControlPointTurn(u.start, null, null);
1677 // updatePathLegPrev(u.start, u.start, PathLegUpdateType.NONE);
1678 } else if (u.start.isEnd()) {
1679 updateEndComponentControlPoint(u.start, u.dir);
1680 } else if (u.start.isInline()) {
1681 u.start.orientToDirection(u.dir);
1683 if (u.end.isTurn()) {
1684 //updateTurnControlPointTurn(u.end, u.end.getPrevious(), u.end.getNext());
1685 updateTurnControlPointTurn(u.end, null, null);
1686 // updatePathLegNext(u.end, u.end, PathLegUpdateType.NONE);
1687 } else if (u.end.isEnd()) {
1688 updateEndComponentControlPoint(u.end, u.dir);
1689 } else if (u.end.isInline()) {
1690 u.end.orientToDirection(u.dir);
1694 if (u.start.isEnd()) {
1695 updateEndComponentControlPoint(u.start, u.dir);
1697 if (u.end.isEnd()) {
1698 updateEndComponentControlPoint(u.end, u.dir);
1702 updateInlineControlPoints(u, true);
1707 * Processes pipe run and recalculates offset
1709 // private static void processPathLeg(PipeControlPoint start, Point3d
1710 // startPoint,ArrayList<InlineControlPoint> list, PipeControlPoint
1711 // end,Point3d endPoint, Vector3d dir, boolean hasOffsets,int iter, boolean
1712 // reversed, ArrayList<ExpandIterInfo> toRemove) throws TransactionException
1714 @SuppressWarnings("unused")
1715 private static void processPathLegNoOffset(UpdateStruct2 u) throws Exception {
1717 System.out.println("PipingRules.processPathLeg " + u.start + " " + u.end);
1718 Vector3d offset = new Vector3d();
1721 for (PipeControlPoint icp : u.list) {
1722 if (icp.isOffset()) {
1723 offset.add(icp.getSizeChangeOffsetVector(u.dir));
1724 } else if (icp.isDualSub()) {
1725 ErrorLogger.defaultLogError("Updating pipe run, found offset controlpoint " + icp, new Exception("ASSERT!"));
1732 private static void updateOffsetPoint(PipeControlPoint sccp, Vector3d offset) {
1733 Vector3d world = sccp.getWorldPosition();
1735 PipeControlPoint ocp = sccp.getDualSub();
1736 ocp.setWorldPosition(world);
1740 * Updates InlineControlPoints position when straight pipe's end(s) have
1748 private static void updateInlineControlPoint(PipeControlPoint icp, Vector3d prev, Vector3d next, Vector3d dir) {
1750 System.out.println("PipingRules.updateInlineControlPoint() " + icp);
1752 Vector3d inlinePoint = icp.getWorldPosition();
1753 Vector3d prevPoint = new Vector3d(prev);
1754 Vector3d nextPoint = new Vector3d(next);
1755 if (!icp.isVariableLength()) {
1756 // Reserve space for fixed length components.
1757 MathTools.mad(prevPoint, dir, icp.getInlineLength());
1758 MathTools.mad(nextPoint, dir, -icp.getInlineLength());
1759 if (MathTools.distance(prevPoint, nextPoint) < ALLOWED_OFFSET) {
1764 boolean canCalc = MathTools.distance(prevPoint, nextPoint) > ALLOWED_OFFSET;
1766 System.out.print("InlineControlPoint update " + icp + " " + inlinePoint + " " + prevPoint + " " + nextPoint);
1767 Vector3d newInlinePoint = null;
1769 boolean branchUpdate = false;
1770 PipeControlPoint becp = null;
1771 for (PipeControlPoint pcp : icp.getChildPoints())
1772 if (pcp.isNonDirected()) {
1773 branchUpdate = true;
1778 if (DUMMY || !branchUpdate) {
1779 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1783 // FIXME : can only handle one branch
1784 PipeControlPoint p = null;
1785 if (becp.getNext() != null) {
1786 p = becp.findNextEnd();
1787 } else if (becp.getPrevious() != null) {
1788 p = becp.findPreviousEnd();
1791 newInlinePoint = MathTools.closestPointOnEdge(new Vector3d(inlinePoint), prevPoint, nextPoint);
1792 } else if (canCalc){
1793 Vector3d branchLegEnd = p.getWorldPosition();
1794 Vector3d dir2 = new Vector3d(inlinePoint);
1795 dir2.sub(branchLegEnd);
1796 Vector3d dir1 = new Vector3d(nextPoint);
1797 dir1.sub(prevPoint);
1798 newInlinePoint = new Vector3d();
1799 double mu[] = new double[2];
1800 MathTools.intersectStraightStraight(new Vector3d(prevPoint), dir1, new Vector3d(branchLegEnd), dir2, newInlinePoint, new Vector3d(), mu);
1802 System.out.println(mu[0]);
1803 // FIXME : reserve space
1805 newInlinePoint = new Vector3d(prevPoint);
1806 } else if (mu[0] > 1.0) {
1807 newInlinePoint = new Vector3d(nextPoint);
1812 // prevPoint == nextPoint
1813 newInlinePoint = new Vector3d(prevPoint);
1816 System.out.println(" " + newInlinePoint);
1818 icp.setWorldPosition(newInlinePoint);
1819 icp.orientToDirection(dir);
1823 * Updates InlineControlPoints position when straight pipe's end(s) have
1831 private static void updateEndComponentControlPoint(PipeControlPoint ecp, Vector3d dir) throws Exception {
1833 System.out.println("PipingRules.updateEndComponentControlPoint() " + ecp);
1835 if (!ecp.isFixed()) // prevent overriding nozzle orientations..
1836 ecp.orientToDirection(dir);
1838 for (PipeControlPoint pcp : ecp.getChildPoints()) {
1839 // TODO update position
1840 updatePathLegEndControlPoint(pcp);
1845 * Updates all branches when branch's position has been changed
1849 private static void updateBranchControlPointBranches(PipeControlPoint bcp) throws Exception {
1851 System.out.println("PipingRules.updateBranchControlPointBranches() " + bcp);
1852 if (bcp.isDualInline())
1854 Collection<PipeControlPoint> branches = bcp.getChildPoints();
1855 if (branches.size() == 0) {
1857 System.out.println("No Branches found");
1861 for (PipeControlPoint pcp : branches) {
1862 updatePathLegEndControlPoint(pcp);
1866 private static double updateTurnControlPointTurn(PipeControlPoint tcp, Vector3d prev, Vector3d next) {
1868 UpdateStruct2 us = createUS(tcp, Direction.NEXT, 0, new ArrayList<PipingRules.ExpandIterInfo>(), tcp);
1873 UpdateStruct2 us = createUS(tcp, Direction.PREVIOUS, 0, new ArrayList<PipingRules.ExpandIterInfo>(), tcp);
1879 if (!tcp.asFixedAngle()) {
1882 if (next == null || prev == null) {
1883 if (tcp.getTurnAngle() != null)
1884 return tcp.getTurnAngle();
1885 return Math.PI; // FIXME : argh
1888 final boolean isDegenerate = prev.lengthSquared() < MathTools.NEAR_ZERO || next.lengthSquared() < MathTools.NEAR_ZERO;
1889 double turnAngle = isDegenerate ? 0.0 : prev.angle(next);
1891 Vector3d turnAxis = new Vector3d();
1892 turnAxis.cross(prev, next);
1893 if (turnAxis.lengthSquared() > MathTools.NEAR_ZERO) {
1894 double elbowRadius = ((TurnComponent)tcp.getPipelineComponent()).getTurnRadius();
1895 double R = elbowRadius * Math.tan(turnAngle * 0.5);
1897 turnAxis.normalize();
1898 tcp.setTurnAngle(turnAngle);
1899 tcp.setLength(R);// setComponentOffsetValue(R);
1900 tcp.setTurnAxis(turnAxis);
1901 // tcp.setPosition(tcp.getPosition());
1904 tcp.setTurnAngle(0.0);
1906 tcp.setTurnAxis(new Vector3d(MathTools.Y_AXIS));
1909 tcp.orientToDirection(prev);
1912 System.out.println("PipingTools.updateTurnControlPointTurn " + prev + " " + next + " " + turnAngle + " " + turnAxis);
1916 if (prev != null && next != null) {
1918 } else if (prev == null) {
1919 if (!tcp._getReversed())
1920 tcp.setReversed(true);
1921 } else if (next == null) {
1922 if (tcp._getReversed())
1923 tcp.setReversed(false);
1926 Vector3d dir = null;
1927 if (!tcp._getReversed()) {
1934 return Math.PI; // FIXME : argh
1937 Quat4d q = tcp.getControlPointOrientationQuat(dir, tcp.getRotationAngle() != null ? tcp.getRotationAngle() : 0.0);
1938 Vector3d v = new Vector3d();
1939 MathTools.rotate(q, MathTools.Y_AXIS,v);
1941 tcp.setWorldOrientation(q);
1942 if (tcp.getTurnAngle() != null)
1943 return tcp.getTurnAngle();
1944 return Math.PI; // FIXME : argh
1950 public static List<PipeControlPoint> getControlPoints(PipeRun pipeRun) {
1951 List<PipeControlPoint> list = new ArrayList<PipeControlPoint>();
1952 if (pipeRun.getControlPoints().size() == 0)
1954 PipeControlPoint pcp = pipeRun.getControlPoints().iterator().next();
1955 while (pcp.getPrevious() != null) {
1956 PipeControlPoint prev = pcp.getPrevious();
1957 if (prev.getPipeRun() != pipeRun && prev.getPipeRun() != null) { // bypass possible corruption
1962 if (pcp.isDualSub()) {
1963 pcp = pcp.getParentPoint();
1966 while (pcp.getNext() != null) {
1967 pcp = pcp.getNext();
1968 if (pcp.getPipeRun() != pipeRun)
1975 public static void reverse(PipeRun pipeRun) {
1978 List<PipeControlPoint> points = getControlPoints(pipeRun);
1979 PipeControlPoint pcp = points.get(0);
1980 if (pcp.isSizeChange() && pcp.getChildPoints().size() > 0) {
1981 PipeRun pr = pcp.getPipeRun();
1989 List<PipeRun> all = new ArrayList<PipeRun>();
1990 List<List<PipeControlPoint>> pcps = new ArrayList<List<PipeControlPoint>>();
1993 List<PipeControlPoint> points = getControlPoints(pipeRun);
1995 PipeControlPoint pcp = points.get(points.size()-1);
1996 if (pcp.getChildPoints().size() > 0) {
1997 PipeRun pipeRun2 = pcp.getChildPoints().get(0).getPipeRun();
1998 if (pipeRun == pipeRun2)
2006 for (int i = 0 ; i < all.size(); i++) {
2007 List<PipeControlPoint> list = pcps.get(i);
2010 for (int i = 0 ; i < all.size(); i++) {
2011 boolean last = i == all.size() - 1;
2012 List<PipeControlPoint> list = pcps.get(i);
2015 List<PipeControlPoint> list2 = pcps.get(i+1);
2016 PipeControlPoint prev = list.get(list.size()-1);
2017 PipeControlPoint next = list2.get(0);
2019 // Reverse the component on the boundary.
2020 InlineComponent ic = (InlineComponent)prev.getPipelineComponent();
2021 PipeRun r1 = ic.getPipeRun();
2022 PipeRun r2 = ic.getAlternativePipeRun();
2023 if (r1 == null || r2 == null)
2024 throw new RuntimeException("Components on PipeRun changes should refer to bot PipeRuns");
2028 ic.setAlternativePipeRun(r1);
2030 throw new RuntimeException("PipeRun changes should contain shared control points");
2038 private static void _reverse(List<PipeControlPoint> list) {
2039 if (list.size() <= 1)
2040 return; // nothing to do.
2042 for (int i = 0 ; i < list.size(); i++) {
2043 boolean first = i == 0;
2044 boolean last = i == list.size() - 1;
2045 PipeControlPoint current = list.get(i);
2046 PipeControlPoint currentSub = null;
2047 if (current.isDualInline())
2048 currentSub = current.getDualSub();
2050 PipeControlPoint next = list.get(i+1);
2051 if (next.isDualInline())
2052 next = next.getDualSub();
2053 if (current.getNext() == next)
2054 current.setNext(null);
2055 current.setPrevious(next);
2056 if (currentSub != null) {
2057 if (currentSub.getNext() == next)
2058 currentSub.setNext(null);
2059 currentSub.setPrevious(next);
2062 PipeControlPoint prev = list.get(i-1);
2064 if (current.getPrevious() == prev)
2065 current.setPrevious(null);
2066 current.setNext(prev);
2068 if (currentSub != null) {
2069 if (currentSub.getPrevious() == prev)
2070 currentSub.setPrevious(null);
2071 currentSub.setNext(prev);
2074 PipeControlPoint prev = list.get(i-1);
2075 PipeControlPoint next = list.get(i+1);
2076 if (next.isDualInline())
2077 next = next.getDualSub();
2080 current.setPrevious(next);
2081 current.setNext(prev);
2083 if (currentSub != null) {
2084 currentSub.setPrevious(next);
2085 currentSub.setNext(prev);
2089 //if (current.isTurn() && current.isFixed()) {
2090 if (current.asFixedAngle()) {
2091 current.setReversed(!current._getReversed());
2093 if (current.isInline() && current.isReverse()) {
2094 current.setReversed(!current._getReversed());
2101 public static void validate(PipeRun pipeRun) {
2102 if (pipeRun == null)
2104 synchronized (ruleMutex) {
2105 Collection<PipeControlPoint> pcps = pipeRun.getControlPoints();
2107 //System.out.println("Validate " + pipeRun.getName());
2108 for (PipeControlPoint pcp : pcps) {
2109 if (pcp.getParentPoint() == null || pcp.getParentPoint().getPipeRun() != pipeRun)
2112 List<PipeControlPoint> runPcps = getControlPoints(pipeRun);
2113 if (runPcps.size() != count) {
2114 System.out.println("Run " + pipeRun.getName() + " contains unconnected control points, found " + runPcps.size() + " connected, " + pcps.size() + " total.");
2115 for (PipeControlPoint pcp : pcps) {
2116 if (!runPcps.contains(pcp)) {
2117 System.out.println("Unconnected " + pcp + " " + pcp.getPipelineComponent());
2121 for (PipeControlPoint pcp : pcps) {
2122 if (pcp.getPipeRun() == null) {
2123 System.out.println("PipeRun ref missing " + pcp + " " + pcp.getPipelineComponent());
2125 if (!pcp.isDirected() && pcp.getNext() == null && pcp.getPrevious() == null)
2126 System.out.println("Orphan undirected " + pcp + " " + pcp.getPipelineComponent());
2128 for (PipeControlPoint pcp : pcps) {
2129 if (pcp.getParentPoint() == null) {
2130 PipeControlPoint sub = null;
2131 if (pcp.isDualInline())
2132 sub = pcp.getDualSub();
2133 PipeControlPoint next = pcp.getNext();
2134 PipeControlPoint prev = pcp.getPrevious();
2136 if (!(next.getPrevious() == pcp || next.getPrevious() == sub)) {
2137 System.out.println("Inconsistency between " + pcp + " -> " +next );
2141 PipeControlPoint prevParent = null;
2142 if (prev.isDualSub()) {
2143 prevParent = prev.getParentPoint();
2144 } else if (prev.isDualInline()) {
2145 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
2147 if (!(prev.getNext() == pcp && (prevParent == null || prevParent.getNext() == pcp))) {
2148 System.out.println("Inconsistency between " + pcp + " <-- " +prev );
2156 public static void splitVariableLengthComponent(PipelineComponent newComponent, InlineComponent splittingComponent, boolean assignPos) throws Exception{
2157 assert(!splittingComponent.getControlPoint().isFixedLength());
2158 assert(!(newComponent instanceof InlineComponent && !newComponent.getControlPoint().isFixedLength()));
2159 PipeControlPoint newCP = newComponent.getControlPoint();
2160 PipeControlPoint splittingCP = splittingComponent.getControlPoint();
2161 PipeControlPoint nextCP = splittingCP.getNext();
2162 PipeControlPoint prevCP = splittingCP.getPrevious();
2164 /* there are many different cases to insert new component when
2165 it splits existing VariableLengthinlineComponent.
2167 1. VariableLengthComponet is connected from both sides:
2168 - insert new component between VariableLength component and component connected to it
2169 - insert new VariableLengthComponent between inserted component and component selected in previous step
2171 2. VariableLengthComponent is connected from one side
2172 - Use previous case or:
2173 - Insert new component to empty end
2174 - Insert new VariableLength component to inserted components empty end
2176 3. VariableLength is not connected to any component.
2177 - Should not be possible, at least in current implementation.
2178 - Could be done using second case
2182 if (nextCP == null && prevCP == null) {
2183 // this should not be possible
2184 throw new RuntimeException("VariableLengthComponent " + splittingComponent + " is not connected to anything.");
2186 double newLength = newComponent.getControlPoint().getLength();
2189 Point3d next = new Point3d();
2190 Point3d prev = new Point3d();
2191 splittingCP.getInlineControlPointEnds(prev, next);
2193 Vector3d newPos = null;
2195 newPos = new Vector3d(prev);
2196 Vector3d dir = new Vector3d(next);
2200 newComponent.setWorldPosition(newPos);
2202 newPos = newComponent.getWorldPosition();
2207 Vector3d dir = new Vector3d(next);
2210 dir.scale(newLength * 0.5);
2211 Point3d vn = new Point3d(newPos);
2212 Point3d vp = new Point3d(newPos);
2215 double ln = vn.distance(next);
2216 double lp = vp.distance(prev);
2217 vp.interpolate(prev, 0.5);
2218 vn.interpolate(next, 0.5);
2221 if (nextCP == null) {
2222 newCP.insert(splittingCP, Direction.NEXT);
2223 insertStraight(newCP, Direction.NEXT, new Vector3d(vn), ln);
2224 splittingCP.setWorldPosition(new Vector3d(vp));
2225 // ControlPointTools.setWorldPosition(splittingCP, vp);
2226 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
2227 } else if (prevCP == null) {
2228 newCP.insert(splittingCP, Direction.PREVIOUS);
2229 insertStraight(newCP, Direction.PREVIOUS, new Vector3d(vp), lp);
2230 splittingCP.setWorldPosition(new Vector3d(vn));
2231 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, ln);
2233 newCP.insert(splittingCP, nextCP);
2234 insertStraight(newCP, nextCP, new Vector3d(vn), ln);
2235 splittingCP.setWorldPosition(new Vector3d(vp));
2236 // splittingCP.setRelatedScalarDouble(ProcessResource.plant3Dresource.HasLength, lp);
2238 positionUpdate(newCP);
2242 public static void addSizeChange(boolean reversed, PipeRun pipeRun, PipeRun other, InlineComponent reducer, PipeControlPoint previous, PipeControlPoint next) {
2243 PipeControlPoint pcp = reducer.getControlPoint();
2244 PipeControlPoint ocp = pcp.getDualSub();
2246 String name = pipeRun.getUniqueName("Reducer");
2247 reducer.setName(name);
2248 pipeRun.addChild(reducer);
2249 other.addChild(ocp);
2250 reducer.setAlternativePipeRun(other);
2252 previous.setNext(pcp);
2253 pcp.setPrevious(previous);
2254 ocp.setPrevious(previous);
2258 next.setPrevious(ocp);
2261 String name = other.getUniqueName("Reducer");
2262 reducer.setName(name);
2263 other.addChild(reducer);
2264 pipeRun.addChild(ocp);
2265 reducer.setAlternativePipeRun(pipeRun);
2269 pcp.setPrevious(next);
2270 ocp.setPrevious(next);
2272 pcp.setNext(previous);
2273 ocp.setNext(previous);
2274 previous.setPrevious(ocp);