1 package org.simantics.plant3d.scenegraph.controlpoint;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
8 import javax.vecmath.AxisAngle4d;
9 import javax.vecmath.Matrix3d;
10 import javax.vecmath.Point3d;
11 import javax.vecmath.Quat4d;
12 import javax.vecmath.Tuple3d;
13 import javax.vecmath.Vector3d;
15 import org.simantics.g3d.math.MathTools;
16 import org.simantics.g3d.property.annotations.GetPropertyValue;
17 import org.simantics.g3d.scenegraph.G3DNode;
18 import org.simantics.plant3d.scenegraph.IP3DNode;
19 import org.simantics.plant3d.scenegraph.Nozzle;
20 import org.simantics.plant3d.scenegraph.P3DRootNode;
21 import org.simantics.plant3d.scenegraph.PipeRun;
22 import org.simantics.plant3d.scenegraph.PipelineComponent;
24 import vtk.vtkRenderer;
27 public class PipeControlPoint extends G3DNode implements IP3DNode {
29 private static boolean DEBUG = false;
31 public enum PointType{INLINE,TURN,END};
32 public enum Direction{NEXT,PREVIOUS};
33 public enum PositionType {SPLIT,NEXT,PREVIOUS,PORT}
35 private PipelineComponent component;
37 private PointType type;
38 private boolean isFixed = true; // In-line: fixed-length Turn: fixed-angle
39 private boolean isMod = false; // Can user modify fixed value manually
40 private boolean isRotate = false; // rotates around path leg axis.
41 private boolean isReverse = false; // definition direction can be swapped
42 private boolean isDeletable = true; // can be removed by rules
43 private boolean isSizeChange = false; // changes size of the pipe. The next control point / component is on different PipeRun
44 private boolean isSub = false; // child point for offset / size change
46 private boolean disposed = false;
48 public PipeControlPoint(PipelineComponent component) {
49 this.component = component;
50 if (component.getPipeRun() != null)
51 component.getPipeRun().addChild(this);
55 public PipeControlPoint(PipelineComponent component, PipeRun piperun) {
56 this.component = component;
57 piperun.addChild(this);
61 public void update(vtkRenderer ren) {
63 PipingRules.requestUpdate(this);
64 } catch (Exception e) {
70 public PipeRun getPipeRun() {
71 return (PipeRun)getParent();
74 public PipelineComponent getPipelineComponent() {
78 public PointType getType() {
82 public void setType(PointType type) {
86 @GetPropertyValue(name="Fixed",tabId="Debug",value="fixed")
87 public boolean isFixed() {
91 public void setFixed(boolean fixed) {
95 @GetPropertyValue(name="Mod",tabId="Debug",value="mod")
96 public boolean isMod() {
100 public void setMod(boolean isMod) {
104 @GetPropertyValue(name="Rotate",tabId="Debug",value="rotate")
105 public boolean isRotate() {
109 public void setRotate(boolean rotate) {
110 this.isRotate = rotate;
113 @GetPropertyValue(name="Reverse",tabId="Debug",value="reverse")
114 public boolean isReverse() {
118 public void setReverse(boolean reverse) {
119 this.isReverse = reverse;
122 public void setSub(boolean sub) {
126 @GetPropertyValue(name="Deletable",tabId="Debug",value="deletable")
127 public boolean isDeletable() {
131 public void setDeletable(boolean deletable) {
132 this.isDeletable = deletable;
135 public boolean isPathLegEnd() {
136 return type != PointType.INLINE;
139 public boolean isEnd() {
140 return type == PointType.END;
143 public boolean isTurn() {
144 return type == PointType.TURN;
147 public boolean isInline() {
148 return type == PointType.INLINE;
151 public boolean asPathLegEnd() {
152 // Ends and Turns are path leg ends by default, but also unconnected inline are path leg ends.
153 return isPathLegEnd() || getNext() == null || getPrevious() == null;
157 * True for end components, if control point defines absolute position direction, which rules cannot modify.
158 * This is typical for nozzles.
161 public boolean isDirected() {
162 return isFixed && isEnd();
166 * True for end components, if control is opposite to directed, and rules can modify position and orientation.
167 * This is typical for caps, and other end components.
170 public boolean isNonDirected() {
171 return !isFixed && isEnd();
174 public boolean isVariableLength() {
175 return !isFixed && isInline();
179 * Fixed length in-line component is such that piping rules cannot modify the length.
182 public boolean isFixedLength() {
183 return isFixed && isInline();
186 public boolean isVariableAngle() {
187 return !isFixed && isTurn();
191 * Fixed angle turn component is such that piping rules cannot modify the angle.
194 public boolean isFixedAngle() {
195 return isFixed && isTurn();
199 * Does the turn behave like fixed angle?
200 * For variable angle turns, the turn angle is defined by connected components, and without them, we must handle the component as fixed angle.
203 public boolean asFixedAngle() {
204 return isTurn() && (isFixed || next == null || previous == null);
207 public boolean isBranchEnd() {
208 return isDeletable && isEnd();
211 public boolean isOffset() {
212 return offset != null;
215 public boolean isDualSub() {
216 return parent != null && isSub;
219 public boolean isDualInline() {
220 return children.size() == 1 && children.get(0).isDualSub();
223 public boolean isAxial() {
224 return isInline() && !isDualInline();
227 public boolean isSizeChange() {
229 // if (children.size() == 0)
231 // if (!isDualInline())
233 // return getPipeRun() != children.get(0).getPipeRun();
236 public void setSizeChange(boolean isSizeChange) {
237 this.isSizeChange = isSizeChange;
241 private PipeControlPoint next;
242 private PipeControlPoint previous;
244 public PipeControlPoint getNext() {
248 public PipeControlPoint getPrevious() {
252 public void setNext(PipeControlPoint next) {
254 getParentPoint().setNext(next);
257 if (next != null && next.isDualSub())
259 if (_setNext(next)) {
260 for (PipeControlPoint pcp : children) {
268 public void setPrevious(PipeControlPoint prev) {
270 getParentPoint().setPrevious(prev);
273 if (prev != null && prev.isDualInline())
274 prev = prev.children.get(0);
275 if (_setPrevious(prev)) {
276 for (PipeControlPoint pcp : children) {
278 pcp._setPrevious(prev);
284 protected boolean _setNext(PipeControlPoint next) {
285 if (isEnd() && previous != null && next != null)
286 throw new RuntimeException("End control points are allowed to have only one connection");
288 throw new RuntimeException("Cannot connect to self");
289 if (this.next == next)
291 if (DEBUG) System.out.println(this + " next " + next);
292 if (next == null && isVariableAngle() && previous != null && !isRemoved()) {
293 convertVariableAngleToFixed(Direction.NEXT);
296 if (component != null) {
297 if (parent == null || isSub)
298 component.setNext(next != null ? next.component : null);
300 component.setBranch0(next != null ? next.component : null);
306 protected boolean _setPrevious(PipeControlPoint previous) {
307 if (isEnd() && next != null && previous != null)
308 throw new RuntimeException("End control points are allowed to have only one connection");
309 if (previous == this)
310 throw new RuntimeException("Cannot connect to self");
311 if (this.previous == previous)
313 if (DEBUG) System.out.println(this + " previous " + previous);
314 if (previous == null && isVariableAngle() && next != null && !isRemoved()) {
315 convertVariableAngleToFixed(Direction.PREVIOUS);
317 this.previous = previous;
318 if (component != null) {
319 if (parent == null || isSub)
320 component.setPrevious(previous != null ? previous.component : null);
322 component.setBranch0(previous != null ? previous.component : null);
328 private void convertVariableAngleToFixed(Direction direction) {
329 // We are removing reference, which transforms variable angle to fixed angle.
330 // Since fixed angle is defined differently, we need to calculate fixed angle parameters based on current data
331 // We need to calculate turnAngle and rotationAngle
332 Vector3d dirOut = getPathLegDirection(direction == Direction.NEXT ? Direction.NEXT : Direction.PREVIOUS);
333 Vector3d dir = getPathLegDirection(direction == Direction.NEXT ? Direction.PREVIOUS : Direction.NEXT);
334 if (dir == null || dirOut == null)
337 double angle = dir.angle(dirOut);
338 //super._setNext(null);
339 if (direction == Direction.NEXT)
343 setRotationAngle(0.0);
344 setReversed(direction == Direction.NEXT ? false : true);
345 Vector3d dirOutN = getPathLegDirection(direction == Direction.NEXT ? Direction.NEXT : Direction.PREVIOUS);
347 AxisAngle4d aa = new AxisAngle4d();
348 if (MathTools.createRotation(dirOutN, dirOut, dir, aa)) {
349 setRotationAngle(aa.angle);
351 if (DEBUG) System.out.println("convertToFixed " + dir + " " + dirOut + " " +dirOutN + " " +angle + " "+ aa.angle);
355 public PipeControlPoint parent;
356 public List<PipeControlPoint> children = new ArrayList<PipeControlPoint>();
358 public List<PipeControlPoint> getChildPoints() {
362 public PipeControlPoint getParentPoint() {
367 private double length;
368 private Double turnAngle;
369 private Vector3d turnAxis;
371 private Double offset;
372 private Double rotationAngle;
373 private Boolean reversed;
375 @GetPropertyValue(name="Length",tabId="Debug",value="length")
376 public double getLength() {
380 public void setLength(double l) {
381 if (Double.isInfinite(l) || Double.isNaN(l)) {
384 if (Math.abs(this.length-l) < MathTools.NEAR_ZERO)
387 firePropertyChanged("length");
389 getDualSub().setLength(l);
392 @GetPropertyValue(name="Turn Angle",tabId="Debug",value="turnAngle")
393 public Double getTurnAngle() {
397 @GetPropertyValue(name="Turn Axis",tabId="Debug",value="turnAxis")
398 public Vector3d getTurnAxis() {
402 @GetPropertyValue(name="Offset",tabId="Debug",value="offset")
403 public Double getOffset() {
407 @GetPropertyValue(name="Rotation Angle",tabId="Debug",value="rotationAngle")
408 public Double getRotationAngle() {
409 if (isRotate || asFixedAngle())
410 return rotationAngle;
414 @GetPropertyValue(name="Reversed",tabId="Debug",value="reversed")
415 public Boolean getReversed() {
419 public boolean _getReversed() {
420 if (reversed == null)
425 public void setTurnAngle(Double turnAngle) {
426 if (turnAngle == null || Double.isInfinite(turnAngle) || Double.isNaN(turnAngle)) {
429 if (this.turnAngle != null && Math.abs(this.turnAngle-turnAngle) < MathTools.NEAR_ZERO)
431 this.turnAngle = turnAngle;
432 firePropertyChanged("turnAngle");
435 public void setTurnAxis(Vector3d turnAxis) {
436 if (this.turnAxis != null && MathTools.equals(turnAxis, this.turnAxis))
438 this.turnAxis = turnAxis;
439 firePropertyChanged("turnAxis");
442 public void setOffset(Double offset) {
443 if (Double.isInfinite(offset) || Double.isNaN(offset)) {
446 if (this.offset != null && Math.abs(this.offset-offset) < MathTools.NEAR_ZERO)
448 this.offset = offset;
449 firePropertyChanged("offset");
452 public void setRotationAngle(Double rotationAngle) {
453 if (Double.isInfinite(rotationAngle) || Double.isNaN(rotationAngle)) {
456 if (this.rotationAngle != null && Math.abs(this.rotationAngle-rotationAngle) < MathTools.NEAR_ZERO)
458 this.rotationAngle = rotationAngle;
459 firePropertyChanged("rotationAngle");
462 public void setReversed(Boolean reversed) {
463 this.reversed = reversed;
464 firePropertyChanged("reversed");
467 public Vector3d getSizeChangeOffsetVector(Vector3d dir) {
469 if (rotationAngle == null)
470 q = getControlPointOrientationQuat(dir, 0.0);
472 q = getControlPointOrientationQuat(dir, rotationAngle);
473 Vector3d v = new Vector3d(0.0,offset,0.0);
474 Vector3d offset = new Vector3d();
475 MathTools.rotate(q, v, offset);
479 public Vector3d getSizeChangeOffsetVector() {
481 if (rotationAngle == null)
482 q = getControlPointOrientationQuat(0.0);
484 q = getControlPointOrientationQuat(rotationAngle);
485 Vector3d v = new Vector3d(0.0,offset,0.0);
486 Vector3d offset = new Vector3d();
487 MathTools.rotate(q, v, offset);
491 @GetPropertyValue(name="Next",tabId="Debug",value="next")
492 private String getNextString() {
495 return next.toString();
498 @GetPropertyValue(name="Previous",tabId="Debug",value="previous")
499 private String getPrevString() {
500 if (previous == null)
502 return previous.toString();
505 @GetPropertyValue(name="Sub",tabId="Debug",value="sub")
506 private String getSubString() {
507 if (children.size() == 0)
509 return Arrays.toString(children.toArray());
512 @GetPropertyValue(name="Type",tabId="Debug",value="type")
513 public String getTypeString() {
517 public Quat4d getControlPointOrientationQuat(double angle) {
519 if (turnAxis == null) {
520 Vector3d dir = getPathLegDirection(Direction.NEXT);
521 return getControlPointOrientationQuat(dir, angle);
523 Vector3d dir = getPathLegDirection(Direction.PREVIOUS);
524 if (dir != null) dir.negate();
525 return getControlPointOrientationQuat(dir, turnAxis, angle);
529 public Quat4d getControlPointOrientationQuat(Vector3d dir, double angle, boolean reversed) {
530 if (turnAxis == null) {
531 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
533 Quat4d q = getControlPointOrientationQuat(dir, angle);
535 Quat4d q2 = new Quat4d();
536 q2.set(new AxisAngle4d(MathTools.Y_AXIS, Math.PI));
541 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
543 return getControlPointOrientationQuat(dir, turnAxis, angle);
547 public Quat4d getControlPointOrientationQuat(double angle, boolean reversed) {
549 if (turnAxis == null) {
550 Vector3d dir = getPathLegDirection(Direction.NEXT);
551 return getControlPointOrientationQuat(dir, angle, reversed);
553 Vector3d dir = getPathLegDirection(Direction.PREVIOUS);
555 return getControlPointOrientationQuat(dir, angle, reversed);
561 public static Quat4d getControlPointOrientationQuat(Vector3d dir, double angle) {
562 if (dir == null || dir.lengthSquared() < MathTools.NEAR_ZERO)
563 return MathTools.getIdentityQuat();
566 Vector3d up = new Vector3d(0.0, 1.0, 0.0);
567 double a = up.angle(dir);
568 if (a < 0.1 || (Math.PI - a) < 0.1) {
569 up.set(1.0, 0.0, 0.0);
573 return getControlPointOrientationQuat(dir, up, angle);
576 public static Quat4d getControlPointOrientationQuat(Vector3d dir, Vector3d up, double angle) {
577 if (dir == null || dir.lengthSquared() < MathTools.NEAR_ZERO)
578 return MathTools.getIdentityQuat();
580 final Vector3d front = new Vector3d(1.0,0.0,0.0);
582 Quat4d q1 = new Quat4d();
585 Vector3d right = new Vector3d();
587 right.cross(dir, up);
588 up.cross(right, dir);
592 Matrix3d m = new Matrix3d();
603 //q1.set(m); MathTools contains more stable conversion
604 MathTools.getQuat(m, q1);
606 // if (DEBUG) System.out.println("PipingTools.getPipeComponentOrientationQuat() " + dir+ " " + up + " " + right);
608 Quat4d q2 = new Quat4d();
609 q2.set(new AxisAngle4d(front, angle));
614 public void insert(PipeControlPoint previous, PipeControlPoint next) {
615 // inserting an offsetpoint is error,
617 throw new RuntimeException("Dual sub points cannot be inserted.");
618 // size change control point cannot be inserted this way, because it ends PipeRun
620 throw new RuntimeException("Size change points cannot be inserted.");
621 PipeRun piperun = previous.getPipeRun();
622 // and just to make sure that control point structure is not corrupted
623 if (getPipeRun() != null) {
624 if (piperun != getPipeRun() || piperun != next.getPipeRun())
625 throw new RuntimeException("All controls points must be located on the same pipe run");
627 piperun.addChild(this);
630 // insert new BranchControlPoint between straight's control points
631 PipeControlPoint previousNext = previous.getNext();
632 PipeControlPoint previousPrevious = previous.getPrevious();
634 PipeControlPoint offsetCP = null;
636 offsetCP = getDualSub();
638 if (previousNext != null && previousNext == next) {
639 if (previous.isDualInline()) {
640 throw new RuntimeException();
642 if (next.isDualSub()) {
643 throw new RuntimeException();
645 previous.setNext(this);
646 this.setPrevious(previous);
647 if (previous.isDualSub()) {
648 previous.getParentPoint().setNext(this);
652 if (offsetCP == null) {
653 next.setPrevious(this);
655 next.setPrevious(offsetCP);
656 offsetCP.setNext(next);
657 offsetCP.setPrevious(previous);
660 if (next.isDualInline()) {
661 next.getDualSub().setPrevious(this);
663 } else if (previousPrevious != null && previousPrevious == next) {
664 // control point were given in reverse order
665 if (next.isDualInline())
666 throw new RuntimeException();
667 if (previous.isDualSub())
668 throw new RuntimeException();
670 this.setNext(previous);
671 if (offsetCP == null) {
672 previous.setNext(this);
674 previous.setPrevious(offsetCP);
675 offsetCP.setNext(previous);
676 offsetCP.setPrevious(next);
678 if (previous.isDualInline()) {
679 previous.getDualSub().setPrevious(this);
681 this.setPrevious(next);
683 if (next.isDualSub()) {
684 next.getParentPoint().setNext(this);
688 throw new RuntimeException();
691 PipingRules.validate(piperun);
696 public void insert(PipeControlPoint pcp, Direction direction) {
698 throw new RuntimeException();
699 if (direction == Direction.NEXT) {
700 // if direction is next, user must have given OffsetPoint
701 if (pcp.isDualInline())
702 throw new RuntimeException();
703 // basic next/prev links
705 this.setPrevious(pcp);
706 // and last take care of sizechange / offset points
707 if (pcp.isDualSub()) {
708 pcp.getParentPoint().setNext(this);
710 if (isDualInline()) {
711 getDualSub().setPrevious(this);
714 // if direction is previous, user must have given sizechange
716 throw new RuntimeException();
717 // previous direction is more complicated, since if newCP is SizeChangeControlPoint,
718 // we must link pcp to newCP's OffsetPoint
719 PipeControlPoint nocp = null;
720 if (isDualInline()) {
725 pcp.setPrevious(this);
727 pcp.setPrevious(nocp);
730 if (pcp.isDualInline()) {
731 PipeControlPoint ocp = pcp.getDualSub();
733 ocp.setPrevious(this);
735 ocp.setPrevious(nocp);
739 PipingRules.validate(getPipeRun());
742 public Vector3d getDirectedControlPointDirection() {
743 assert (isDirected());
744 Vector3d dir = new Vector3d();
745 MathTools.rotate(getWorldOrientation(), new Vector3d(1.0, 0.0, 0.0), dir);
751 * Returns direction vector.
753 * For directed control points, always returns outwards pointing vector.
756 * @return normalized vector, or null
758 public Vector3d getDirection(Direction direction) {
760 return getDirectedControlPointDirection();
761 if (isTurn() && asFixedAngle()) {
762 if (direction == Direction.NEXT) {
763 if (previous != null) {
764 PipeControlPoint pcp = this;
765 Vector3d dir = new Vector3d();
766 dir.sub(pcp.getWorldPosition(),previous.getWorldPosition());
767 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
771 Quat4d q = getControlPointOrientationQuat(dir, pcp.getRotationAngle() != null ? pcp.getRotationAngle() : 0.0);
772 AxisAngle4d aa = new AxisAngle4d(MathTools.Y_AXIS,pcp.getTurnAngle() == null ? 0.0 : pcp.getTurnAngle());
773 Quat4d q2 = MathTools.getQuat(aa);
774 Vector3d v = new Vector3d(1.,0.,0.);
775 Vector3d offset = new Vector3d();
776 MathTools.rotate(q2, v, offset);
777 MathTools.rotate(q, offset, dir);
783 PipeControlPoint pcp = this;
784 Vector3d dir = new Vector3d();
785 dir.sub(next.getWorldPosition(),pcp.getWorldPosition());
786 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
790 Quat4d q = getControlPointOrientationQuat(dir, pcp.getRotationAngle() != null ? pcp.getRotationAngle() : 0.0);
791 AxisAngle4d aa = new AxisAngle4d(MathTools.Y_AXIS,pcp.getTurnAngle() == null ? 0.0 : pcp.getTurnAngle());
792 Quat4d q2 = MathTools.getQuat(aa);
793 Vector3d v = new Vector3d(1.,0.,0.);
794 Vector3d offset = new Vector3d();
795 MathTools.rotate(q2, v, offset);
796 MathTools.rotate(q, offset, dir);
806 * Returns path leg direction of the control point.
808 * This method differs from getDirection by also returning inward pointing vectors for directed control points.
813 public Vector3d getPathLegDirection(Direction direction) {
814 if (direction == Direction.NEXT) {
816 PipeControlPoint pcp = this;
817 if (pcp.isDualInline()) {
818 pcp = pcp.getDualSub();
820 Vector3d v = new Vector3d();
821 v.sub(next.getWorldPosition(),pcp.getWorldPosition());
822 if (v.lengthSquared() > MathTools.NEAR_ZERO)
828 if (previous == null) {
830 throw new RuntimeException("Cannot calculate path leg direction for unconnected control point " + this);
831 return getDirectedControlPointDirection();
834 if (isVariableAngle() && !asFixedAngle())
835 throw new RuntimeException("Cannot calculate path leg direction for unconnected variable angle control point " + this);
837 PipeControlPoint pcp = this;
838 if (pcp.isDualSub()) {
839 pcp = pcp.getParentPoint();
841 Vector3d v = new Vector3d();
842 v.sub(pcp.getWorldPosition(),previous.getWorldPosition());
843 if (v.lengthSquared() > MathTools.NEAR_ZERO)
848 } else if (isDirected()) {
849 return getDirectedControlPointDirection();
850 } else if (isEnd()) {
851 Vector3d v = new Vector3d();
852 v.sub(getWorldPosition(),previous.getWorldPosition());
853 if (v.lengthSquared() > MathTools.NEAR_ZERO)
858 } else if (isTurn() && asFixedAngle() && !_getReversed()) {
859 return getDirection(Direction.NEXT);
861 throw new RuntimeException("Missing implementation " + this);
865 if (previous != null) {
866 PipeControlPoint pcp = this;
868 pcp = getParentPoint();
869 Vector3d v = new Vector3d();
870 v.sub(previous.getWorldPosition(),pcp.getWorldPosition());
871 if (v.lengthSquared() > MathTools.NEAR_ZERO)
879 throw new RuntimeException("Cannot calculate path leg direction for unconnected control point " + this);
880 Vector3d v = getDirectedControlPointDirection();
884 if (isVariableAngle() && !asFixedAngle())
885 throw new RuntimeException("Cannot calculate path leg direction for unconnected variable angle control point " + this);
887 PipeControlPoint pcp = this;
888 if (pcp.isDualInline()) {
889 pcp = pcp.getDualSub();
891 Vector3d v = new Vector3d();
892 v.sub(pcp.getWorldPosition(),next.getWorldPosition());
893 if (v.lengthSquared() > MathTools.NEAR_ZERO)
898 } else if (isDirected()) {
899 Vector3d v = getDirectedControlPointDirection();
902 } else if (isEnd()) {
903 Vector3d v = new Vector3d();
904 v.sub(getWorldPosition(),next.getWorldPosition());
905 if (v.lengthSquared() > MathTools.NEAR_ZERO)
910 } else if (isTurn() && asFixedAngle() && _getReversed()) {
911 return getDirection(Direction.PREVIOUS);
913 throw new RuntimeException("Missing implementation " + this);
919 public void getInlineControlPointEnds(Tuple3d p1, Tuple3d p2) {
922 PipeControlPoint sub = isAxial() ? this : getDualSub();
923 Vector3d pos = getWorldPosition(), pos2 = sub == this ? pos : sub.getWorldPosition();
924 Vector3d dir = sub.getInlineDir();
926 dir.scale(length * 0.5);
933 public void getControlPointEnds(Tuple3d p1, Tuple3d p2) {
934 PipeControlPoint sub = isAxial() || isDirected() || isTurn() ? this : getChildPoints().get(0);
935 Vector3d pos = getWorldPosition(), pos2 = sub == this ? pos : sub.getWorldPosition();
940 dir2 = getInlineDir();
941 dir2.scale(length * 0.5);
942 dir1 = new Vector3d(dir2);
945 dir1 = getPathLegDirection(Direction.PREVIOUS);
946 dir2 = sub.getPathLegDirection(Direction.NEXT);
957 * Get both path leg directions, with (0,0,0) if no connection exists. The returned vectors are not normalized.
959 * @param v1 Set to the direction towards the previous control point on output
960 * @param v2 Set to the direction towards the next control point on output
962 public void getEndDirections(Tuple3d v1, Tuple3d v2) {
963 PipeControlPoint sub = isAxial() ? this : getDualSub();
965 Vector3d dir1 = getPathLegDirection(Direction.PREVIOUS);
966 Vector3d dir2 = sub.getPathLegDirection(Direction.NEXT);
979 public void getInlineControlPointEnds(Tuple3d p1, Tuple3d p2, Vector3d dir) {
982 Vector3d pos = getWorldPosition();
983 dir.set(getInlineDir());
985 dir.scale(length * 0.5);
992 public void getInlineControlPointEnds(Tuple3d center, Tuple3d p1, Tuple3d p2, Vector3d dir) {
995 Vector3d pos = getWorldPosition();
997 dir.set(getInlineDir());
999 dir.scale(length * 0.5);
1006 public Vector3d getInlineDir() {
1007 Vector3d dir = getPathLegDirection(Direction.NEXT);
1009 dir = getPathLegDirection(Direction.PREVIOUS);
1011 // Use reverse direction
1014 // Control point is not connected at all, use current orientation
1015 dir = new Vector3d(1,0,0);
1016 MathTools.rotate(getWorldOrientation(), dir, dir);
1022 public double getInlineLength() {
1023 if (type == PointType.TURN)
1025 else if (type == PointType.INLINE)
1026 return length * 0.5;
1031 * Return the position indicated by the argument. If the indicated direction is not connected, the
1032 * control point's wolrd position is returned instead.
1034 * @param type A selector for the position to be returned
1035 * @return The selected position
1037 public Vector3d getRealPosition(PositionType type) {
1038 Vector3d pos = getWorldPosition();
1041 Vector3d dir = getInlineDir();
1042 double length = getInlineLength();
1048 Vector3d dir = getInlineDir();
1049 double length = getInlineLength();
1055 // IEntity portDir = pcp.getSingleRelatedObject(ProcessResource.plant3Dresource.HasDirection);
1056 // TODO : how we calculated needed space for a port; does it has an offset from control point's position or not?
1066 public void getInlineMovement(Tuple3d start, Tuple3d end) {
1067 // FIXME : check type of neighbor components and allow movement on top of variable length components,
1068 // find proper range for movement (pcp's position is not)
1069 PipeControlPoint p = previous.getPrevious();
1070 PipeControlPoint n = next.getNext();
1071 start.set(p.getWorldPosition());
1072 end.set(n.getWorldPosition());
1075 public PipeControlPoint findNextEnd() {
1076 ArrayList<PipeControlPoint> t = new ArrayList<PipeControlPoint>();
1077 return findNextEnd( t);
1080 public PipeControlPoint findPreviousEnd() {
1081 ArrayList<PipeControlPoint> t = new ArrayList<PipeControlPoint>();
1082 return findPreviousEnd(t);
1085 public PipeControlPoint findNextEnd(List<PipeControlPoint> nextList) {
1087 PipeControlPoint pcp = null;
1088 PipeControlPoint p = null;
1089 if (nextList.size() == 0)
1093 p = nextList.get(nextList.size() - 1);
1098 if (nextList.size() > 0)
1099 nextList.remove(nextList.size() - 1);
1100 // if (DEBUG) System.out.println(" " + pcp.getResource() + " not full");
1104 if (pcp.isPathLegEnd()) {
1105 //if (DEBUG) System.out.println(" " + pcp.getResource());
1109 // if (DEBUG) System.out.print(" " + pcp.getResource());
1114 public PipeControlPoint findPreviousEnd(List<PipeControlPoint> prevList) {
1116 PipeControlPoint pcp = null;
1117 PipeControlPoint p = null;
1118 if (prevList.size() == 0)
1122 p = prevList.get(prevList.size() - 1);
1124 pcp = p.getPrevious();
1127 if (prevList.size() > 0)
1128 prevList.remove(prevList.size() - 1);
1129 // if (DEBUG) System.out.println(" " + pcp.getResource() + " not full");
1132 if (pcp.isPathLegEnd()) {
1133 // if (DEBUG) System.out.println(" " + pcp.getResource());
1137 // if (DEBUG)System.out.print(" " + pcp.getResource());
1142 public void _remove() {
1147 public PipeControlPoint getDualSub() {
1149 return getChildPoints().get(0);
1151 throw new IllegalStateException("Current control point is not dual inline");
1155 public void _remove(boolean renconnect) {
1159 if (DEBUG) System.out.println(this + " Remove " + renconnect);
1161 if (getParentPoint() != null) {
1162 getParentPoint()._remove(renconnect);
1165 PipeRun pipeRun = getPipeRun();
1166 // PipeRUn removal has been changed, so pipeRun may be null.
1167 // if (pipeRun == null)
1170 PipeControlPoint additionalRemove = null;
1171 if (!PipingRules.isEnabled()) {
1177 PipeControlPoint currentPrev = previous;
1178 PipeControlPoint currentNext = next;
1179 if (currentNext == null && currentPrev == null) {
1181 if (pipeRun != null) {
1182 pipeRun.remChild(this);
1183 checkRemove(pipeRun);
1187 if (currentNext != null && currentPrev != null) {
1188 boolean link = renconnect;
1189 if (currentNext.isBranchEnd()) {
1191 currentNext.remove();
1195 if (currentPrev.isBranchEnd()) {
1197 currentPrev.remove();
1202 if (currentPrev.isDirected() && currentNext.isDirected())
1204 else if (this.isDualInline()) {
1206 } else if (this.isDualSub()) {
1207 throw new RuntimeException("_remove() is called for parent point, somehow got to child point. " + this);
1210 if (currentNext == null) {
1212 } else if (currentNext.isDualInline()) {
1213 PipeControlPoint sccp = currentNext;
1214 PipeControlPoint ocp = currentNext.getDualSub();
1216 throw new RuntimeException("Removing PipeControlPoint " + this+ " structure damaged, no offset control point");
1219 sccp.setPrevious(currentPrev);
1220 //ocp.setPrevious(currentPrev);
1221 assert(ocp.getPrevious() == currentPrev);
1223 sccp.setPrevious(null);
1224 //ocp.setPrevious(null);
1225 assert(ocp.getPrevious() == null);
1228 } else if (currentNext.isDualSub()) {
1229 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, next control point is offset control point");
1230 } else if (currentNext.previous == this) {
1232 currentNext.setPrevious(currentPrev);
1234 currentNext.setPrevious(null);
1237 } else if (isDualInline()) {
1238 if (currentNext.previous != getDualSub()) {
1239 throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1242 currentNext.setPrevious(currentPrev);
1244 currentNext.setPrevious(null);
1248 throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1250 if (currentPrev == null) {
1252 } else if (currentPrev.isDualInline()) {
1253 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, previous control point is size change control point");
1254 } else if (currentPrev.isDualSub()) {
1255 PipeControlPoint ocp = currentPrev;
1256 PipeControlPoint sccp = currentPrev.getParentPoint();
1258 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, no size change control point");
1260 //ocp.setNext(currentNext);
1261 sccp.setNext(currentNext);
1262 assert(ocp.getNext() == currentNext);
1264 //ocp.setNext(null);
1266 assert(ocp.getNext() == null);
1269 } else if (currentPrev.next == this) {
1271 currentPrev.setNext(currentNext);
1273 currentPrev.setNext(null);
1277 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged");
1280 if (currentNext.isVariableLength() && currentPrev.isVariableLength()) {
1281 // we have to join them into single variable length component.
1282 additionalRemove = currentPrev;
1283 // combine lengths and set the location of remaining control point to the center.
1284 Point3d ps = new Point3d();
1285 Point3d pe = new Point3d();
1286 Point3d ns = new Point3d();
1287 Point3d ne = new Point3d();
1288 currentPrev.getInlineControlPointEnds(ps, pe);
1289 currentNext.getInlineControlPointEnds(ns, ne);
1290 double l = currentPrev.getLength() + currentNext.getLength();
1291 Vector3d cp = new Vector3d();
1294 currentNext.setLength(l);
1295 currentNext.setWorldPosition(cp);
1298 // FIXME : pipe run must be split into two parts, since the control point structure is no more continuous.
1300 } else if (currentNext != null) {
1301 if (currentNext.isDualInline()) {
1302 PipeControlPoint sccp = currentNext;
1303 PipeControlPoint ocp = currentNext.getDualSub();
1305 throw new RuntimeException("Removing PipeControlPoint " + this+ " structure damaged, no offset control point");
1307 sccp.setPrevious(null);
1308 assert(ocp.getPrevious() == null);
1309 //ocp.setPrevious(null);
1310 } else if (currentNext.isDualSub()) {
1311 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, next control point is offset control point");
1312 } else if (currentNext.previous == this) {
1313 currentNext.setPrevious(null);
1314 } else if (isDualInline()) {
1315 if (currentNext.previous != getDualSub()) {
1316 throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1318 currentNext.setPrevious(null);
1320 throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1323 } else { //(previous != null)
1324 if(currentPrev.isDualInline()) {
1325 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, previous control point is size change control point");
1326 } else if (currentPrev.isDualSub()) {
1327 PipeControlPoint ocp = currentPrev;
1328 PipeControlPoint sccp = currentPrev.getParentPoint();
1330 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, no size change control point");
1333 assert(ocp.getNext() == null);
1334 } else if (currentPrev.next == this) {
1335 currentPrev.setNext(null);
1337 throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1341 if (children.size() > 0 ) {
1343 } else if (parent!= null) {
1344 removeParentPoint();
1350 if (pipeRun != null) {
1351 pipeRun.remChild(this);
1352 checkRemove(pipeRun);
1353 if (PipingRules.isEnabled() && pipeRun.getParent() != null && pipeRun.getControlPoints().size() > 0)
1354 PipingRules.validate(pipeRun);
1356 if (additionalRemove != null)
1357 additionalRemove.remove();
1362 * Removes control point and attempts to reconnect next/prev
1364 * If this point is size change (PipeRuns are different on both sides), then reconnection cannot be made.
1366 public void remove() {
1367 PipeControlPoint currentPrev = previous;
1368 PipeControlPoint currentNext = next;
1371 if (currentNext != null)
1372 if (!currentNext.checkRemove())
1373 PipingRules.requestUpdate(currentNext);
1374 if (currentPrev != null)
1375 if (!currentPrev.checkRemove())
1376 PipingRules.requestUpdate(currentPrev);
1377 } catch (Exception e) {
1378 e.printStackTrace();
1384 * Removes control point without attempting to reconnect next/prev.
1385 * This usually leads to creation of another PipeRun for the control points after this point.
1387 public void removeAndSplit() {
1388 PipeControlPoint currentPrev = previous;
1389 PipeControlPoint currentNext = next;
1391 if (next != null && previous != null) {
1392 P3DRootNode root = (P3DRootNode)getPipelineComponent().getRootNode();
1393 PipeRun nextPipeRun = new PipeRun();
1394 nextPipeRun.setName(root.getUniqueName("PipeRun"));
1395 root.addChild(nextPipeRun);
1397 PipeRun previousRun = previous.getPipeRun();
1398 nextPipeRun.setPipeDiameter(previousRun.getPipeDiameter());
1399 nextPipeRun.setTurnRadiusArray(previousRun.getTurnRadiusArray());
1401 PipelineComponent n = next.getPipelineComponent();
1403 if (n.getPipeRun() != previousRun)
1405 if (! (n instanceof Nozzle)) {
1407 nextPipeRun.addChild(n);
1409 n.setPipeRun(nextPipeRun);
1415 if (currentNext != null)
1416 if (!currentNext.checkRemove())
1417 PipingRules.requestUpdate(currentNext);
1418 if (currentPrev != null)
1419 if (!currentPrev.checkRemove())
1420 PipingRules.requestUpdate(currentPrev);
1421 } catch (Exception e) {
1422 e.printStackTrace();
1427 * This is called when adjacent control point is removed.
1429 * This call should remove the give point, if the point cannot exist alone.
1430 * At the moment there is one such case: branch.
1434 protected boolean checkRemove() {
1435 if (getParentPoint() != null) {
1436 return getParentPoint().checkRemove();
1438 if (getPipelineComponent() == null)
1439 return true; // already removed
1440 if (getPipelineComponent().getType().equals("Plant3D.URIs.Builtin_BranchSplitComponent")) {
1441 if (getChildPoints().get(0).getNext() == null && getChildPoints().get(0).getPrevious() == null) {
1446 return checkRemove(getPipeRun());
1450 private boolean checkRemove(PipeRun pipeRun) {
1451 if (pipeRun == null)
1453 Collection<PipeControlPoint> points = pipeRun.getControlPoints();
1454 if (points.size() == 0) {
1457 } else if (points.size() == 1) {
1458 PipeControlPoint pcp = points.iterator().next();
1459 if (pcp.isDeletable() && pcp.getNext() == null && pcp.getPrevious() == null) {
1460 pcp._remove(); // This call will recursively call also this method...
1463 } else if (points.size() == 2) {
1469 private void removeSubPoints() {
1470 for (PipeControlPoint p : children) {
1474 PipeControlPoint currentNext = p.getNext();
1475 PipeControlPoint currentPrev = p.getPrevious();
1477 p._setPrevious(null);
1478 PipeRun run = p.getPipeRun();
1483 if (currentNext != null)
1484 if (!currentNext.checkRemove())
1485 PipingRules.requestUpdate(currentNext);
1486 if (currentPrev != null)
1487 if (!currentPrev.checkRemove())
1488 PipingRules.requestUpdate(currentPrev);
1494 private void removeParentPoint() {
1495 throw new RuntimeException("Child points cannot be removed directly");
1498 public boolean isRemoved() {
1499 return component == null;
1502 private void removeComponent() {
1503 if (component == null)
1505 PipelineComponent next = component.getNext();
1506 PipelineComponent prev = component.getPrevious();
1507 PipelineComponent br0 = component.getBranch0();
1508 component.setNext(null);
1509 component.setPrevious(null);
1510 component.setBranch0(null);
1512 if (next.getNext() == component)
1514 else if (next.getPrevious() == component)
1515 next.setPrevious(null);
1516 else if (next.getBranch0() == component)
1517 next.setBranch0(null);
1520 if (prev.getNext() == component)
1522 else if (prev.getPrevious() == component)
1523 prev.setPrevious(null);
1524 else if (prev.getBranch0() == component)
1525 prev.setBranch0(null);
1528 if (br0.getNext() == component)
1530 else if (br0.getPrevious() == component)
1531 br0.setPrevious(null);
1532 else if (br0.getBranch0() == component)
1533 br0.setBranch0(null);
1535 PipelineComponent comp = component;
1542 public void setOrientation(Quat4d orientation) {
1543 if (MathTools.equals(orientation, getOrientation()))
1545 if (getPipelineComponent() != null && (getPipelineComponent() instanceof Nozzle))
1546 System.out.println();
1547 super.setOrientation(orientation);
1548 if (getParentPoint() == null && component != null)
1549 component._setWorldOrientation(getWorldOrientation());
1554 public void setPosition(Vector3d position) {
1555 if (MathTools.equals(position, getPosition()))
1557 if (Double.isNaN(position.x) || Double.isNaN(position.y) || Double.isNaN(position.z))
1558 throw new IllegalArgumentException("NaN is not supported");
1559 super.setPosition(position);
1560 if (getParentPoint() == null && component != null)
1561 component._setWorldPosition(getWorldPosition());
1565 private void updateSubPoint() {
1567 if (next == null && previous == null) {
1568 for (PipeControlPoint sub : getChildPoints()) {
1569 sub.setWorldPosition(getWorldPosition());
1570 sub.setWorldOrientation(getWorldOrientation());
1574 for (PipeControlPoint sub : getChildPoints()) {
1575 Vector3d wp = getWorldPosition();
1576 wp.add(getSizeChangeOffsetVector());
1577 sub.setWorldPosition(wp);
1578 sub.setWorldOrientation(getWorldOrientation());
1581 for (PipeControlPoint sub : getChildPoints()) {
1582 sub.setWorldPosition(getWorldPosition());
1583 sub.setWorldOrientation(getWorldOrientation());
1589 public void _setWorldPosition(Vector3d position) {
1590 Vector3d localPos = getLocalPosition(position);
1591 super.setPosition(localPos);
1595 public void _setWorldOrientation(Quat4d orientation) {
1596 Quat4d localOr = getLocalOrientation(orientation);
1597 super.setOrientation(localOr);
1602 public String toString() {
1603 return getClass().getName() + "@" + Integer.toHexString(hashCode());