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.g3d.scenegraph.base.INode;
19 import org.simantics.plant3d.scenegraph.IP3DNode;
20 import org.simantics.plant3d.scenegraph.Nozzle;
21 import org.simantics.plant3d.scenegraph.P3DRootNode;
22 import org.simantics.plant3d.scenegraph.PipeRun;
23 import org.simantics.plant3d.scenegraph.PipelineComponent;
25 import vtk.vtkRenderer;
28 public class PipeControlPoint extends G3DNode implements IP3DNode {
30 private static boolean DEBUG = false;
32 public enum PointType{INLINE,TURN,END};
33 public enum Direction{NEXT,PREVIOUS};
34 public enum PositionType {SPLIT,NEXT,PREVIOUS,PORT}
36 private PipelineComponent component;
38 private PointType type;
39 private boolean isFixed = true; // In-line: fixed-length Turn: fixed-angle
40 private boolean isMod = false; // Can user modify fixed value manually
41 private boolean isRotate = false; // rotates around path leg axis.
42 private boolean isReverse = false; // definition direction can be swapped
43 private boolean isDeletable = true; // can be removed by rules
44 private boolean isSizeChange = false; // changes size of the pipe. The next control point / component is on different PipeRun
45 private boolean isSub = false; // child point for offset / size change
47 private boolean disposed = false;
49 public PipeControlPoint(PipelineComponent component) {
50 this.component = component;
51 if (component.getPipeRun() != null)
52 component.getPipeRun().addChild(this);
56 public PipeControlPoint(PipelineComponent component, PipeRun piperun) {
57 this.component = component;
58 piperun.addChild(this);
62 public void update(vtkRenderer ren) {
64 PipingRules.requestUpdate(this);
65 } catch (Exception e) {
71 public PipeRun getPipeRun() {
72 return (PipeRun)getParent();
75 public PipelineComponent getPipelineComponent() {
79 public PointType getType() {
83 public void setType(PointType type) {
87 @GetPropertyValue(name="Fixed",tabId="Debug",value="fixed")
88 public boolean isFixed() {
92 public void setFixed(boolean fixed) {
96 @GetPropertyValue(name="Mod",tabId="Debug",value="mod")
97 public boolean isMod() {
101 public void setMod(boolean isMod) {
105 @GetPropertyValue(name="Rotate",tabId="Debug",value="rotate")
106 public boolean isRotate() {
110 public void setRotate(boolean rotate) {
111 this.isRotate = rotate;
114 @GetPropertyValue(name="Reverse",tabId="Debug",value="reverse")
115 public boolean isReverse() {
119 public void setReverse(boolean reverse) {
120 this.isReverse = reverse;
123 public void setSub(boolean sub) {
127 @GetPropertyValue(name="Deletable",tabId="Debug",value="deletable")
128 public boolean isDeletable() {
132 public void setDeletable(boolean deletable) {
133 this.isDeletable = deletable;
136 public boolean isPathLegEnd() {
137 return type != PointType.INLINE;
140 public boolean isEnd() {
141 return type == PointType.END;
144 public boolean isTurn() {
145 return type == PointType.TURN;
148 public boolean isInline() {
149 return type == PointType.INLINE;
152 public boolean asPathLegEnd() {
153 // Ends and Turns are path leg ends by default, but also unconnected inline are path leg ends.
154 return isPathLegEnd() || getNext() == null || getPrevious() == null;
158 * True for end components, if control point defines absolute position direction, which rules cannot modify.
159 * This is typical for nozzles.
162 public boolean isDirected() {
163 return isFixed && isEnd();
167 * True for end components, if control is opposite to directed, and rules can modify position and orientation.
168 * This is typical for caps, and other end components.
171 public boolean isNonDirected() {
172 return !isFixed && isEnd();
175 public boolean isVariableLength() {
176 return !isFixed && isInline();
180 * Fixed length in-line component is such that piping rules cannot modify the length.
183 public boolean isFixedLength() {
184 return isFixed && isInline();
187 public boolean isVariableAngle() {
188 return !isFixed && isTurn();
192 * Fixed angle turn component is such that piping rules cannot modify the angle.
195 public boolean isFixedAngle() {
196 return isFixed && isTurn();
200 * Does the turn behave like fixed angle?
201 * For variable angle turns, the turn angle is defined by connected components, and without them, we must handle the component as fixed angle.
204 public boolean asFixedAngle() {
205 return isTurn() && (isFixed || next == null || previous == null);
208 public boolean isBranchEnd() {
209 return isDeletable && isEnd();
212 public boolean isOffset() {
213 return offset != null;
216 public boolean isDualSub() {
217 return parent != null && isSub;
220 public boolean isDualInline() {
221 return children.size() == 1 && children.get(0).isDualSub();
224 public boolean isAxial() {
225 return isInline() && !isDualInline();
228 public boolean isSizeChange() {
230 // if (children.size() == 0)
232 // if (!isDualInline())
234 // return getPipeRun() != children.get(0).getPipeRun();
237 public void setSizeChange(boolean isSizeChange) {
238 this.isSizeChange = isSizeChange;
242 private PipeControlPoint next;
243 private PipeControlPoint previous;
245 public PipeControlPoint getNext() {
249 public PipeControlPoint getPrevious() {
253 public void setNext(PipeControlPoint next) {
255 getParentPoint().setNext(next);
258 if (next != null && next.isDualSub())
260 if (_setNext(next)) {
261 for (PipeControlPoint pcp : children) {
269 public void setPrevious(PipeControlPoint prev) {
271 getParentPoint().setPrevious(prev);
274 if (prev != null && prev.isDualInline())
275 prev = prev.children.get(0);
276 if (_setPrevious(prev)) {
277 for (PipeControlPoint pcp : children) {
279 pcp._setPrevious(prev);
285 protected boolean _setNext(PipeControlPoint next) {
286 if (isEnd() && previous != null && next != null)
287 throw new RuntimeException("End control points are allowed to have only one connection");
289 throw new RuntimeException("Cannot connect to self");
290 if (this.next == next)
292 if (DEBUG) System.out.println(this + " next " + next);
293 if (next == null && isVariableAngle() && previous != null && !isRemoved()) {
294 convertVariableAngleToFixed(Direction.NEXT);
297 if (component != null) {
298 if (parent == null || isSub)
299 component.setNext(next != null ? next.component : null);
301 component.setBranch0(next != null ? next.component : null);
307 protected boolean _setPrevious(PipeControlPoint previous) {
308 if (isEnd() && next != null && previous != null)
309 throw new RuntimeException("End control points are allowed to have only one connection");
310 if (previous == this)
311 throw new RuntimeException("Cannot connect to self");
312 if (this.previous == previous)
314 if (DEBUG) System.out.println(this + " previous " + previous);
315 if (previous == null && isVariableAngle() && next != null && !isRemoved()) {
316 convertVariableAngleToFixed(Direction.PREVIOUS);
318 this.previous = previous;
319 if (component != null) {
320 if (parent == null || isSub)
321 component.setPrevious(previous != null ? previous.component : null);
323 component.setBranch0(previous != null ? previous.component : null);
329 private void convertVariableAngleToFixed(Direction direction) {
330 // We are removing reference, which transforms variable angle to fixed angle.
331 // Since fixed angle is defined differently, we need to calculate fixed angle parameters based on current data
332 // We need to calculate turnAngle and rotationAngle
333 Vector3d dirOut = getPathLegDirection(direction == Direction.NEXT ? Direction.NEXT : Direction.PREVIOUS);
334 Vector3d dir = getPathLegDirection(direction == Direction.NEXT ? Direction.PREVIOUS : Direction.NEXT);
335 if (dir == null || dirOut == null)
338 double angle = dir.angle(dirOut);
339 //super._setNext(null);
340 if (direction == Direction.NEXT)
344 setRotationAngle(0.0);
345 setReversed(direction == Direction.NEXT ? false : true);
346 Vector3d dirOutN = getPathLegDirection(direction == Direction.NEXT ? Direction.NEXT : Direction.PREVIOUS);
348 AxisAngle4d aa = new AxisAngle4d();
349 if (MathTools.createRotation(dirOutN, dirOut, dir, aa)) {
350 setRotationAngle(aa.angle);
352 if (DEBUG) System.out.println("convertToFixed " + dir + " " + dirOut + " " +dirOutN + " " +angle + " "+ aa.angle);
356 public PipeControlPoint parent;
357 public List<PipeControlPoint> children = new ArrayList<PipeControlPoint>();
359 public List<PipeControlPoint> getChildPoints() {
363 public PipeControlPoint getParentPoint() {
368 private double length;
369 private Double turnAngle;
370 private Vector3d turnAxis;
372 private Double offset;
373 private Double rotationAngle;
374 private Boolean reversed;
376 @GetPropertyValue(name="Length",tabId="Debug",value="length")
377 public double getLength() {
381 public void setLength(double l) {
382 if (Double.isInfinite(l) || Double.isNaN(l)) {
385 if (Math.abs(this.length-l) < MathTools.NEAR_ZERO)
388 firePropertyChanged("length");
390 getDualSub().setLength(l);
393 @GetPropertyValue(name="Turn Angle",tabId="Debug",value="turnAngle")
394 public Double getTurnAngle() {
398 @GetPropertyValue(name="Turn Axis",tabId="Debug",value="turnAxis")
399 public Vector3d getTurnAxis() {
403 @GetPropertyValue(name="Offset",tabId="Debug",value="offset")
404 public Double getOffset() {
408 @GetPropertyValue(name="Rotation Angle",tabId="Debug",value="rotationAngle")
409 public Double getRotationAngle() {
410 if (isRotate || asFixedAngle())
411 return rotationAngle;
415 @GetPropertyValue(name="Reversed",tabId="Debug",value="reversed")
416 public Boolean getReversed() {
420 public boolean _getReversed() {
421 if (reversed == null)
426 public void setTurnAngle(Double turnAngle) {
427 if (turnAngle == null || Double.isInfinite(turnAngle) || Double.isNaN(turnAngle)) {
430 if (this.turnAngle != null && Math.abs(this.turnAngle-turnAngle) < MathTools.NEAR_ZERO)
432 this.turnAngle = turnAngle;
433 firePropertyChanged("turnAngle");
436 public void setTurnAxis(Vector3d turnAxis) {
437 if (this.turnAxis != null && MathTools.equals(turnAxis, this.turnAxis))
439 this.turnAxis = turnAxis;
440 firePropertyChanged("turnAxis");
443 public void setOffset(Double offset) {
444 if (Double.isInfinite(offset) || Double.isNaN(offset)) {
447 if (this.offset != null && Math.abs(this.offset-offset) < MathTools.NEAR_ZERO)
449 this.offset = offset;
450 firePropertyChanged("offset");
453 public void setRotationAngle(Double rotationAngle) {
454 if (Double.isInfinite(rotationAngle) || Double.isNaN(rotationAngle)) {
457 if (this.rotationAngle != null && Math.abs(this.rotationAngle-rotationAngle) < MathTools.NEAR_ZERO)
459 this.rotationAngle = rotationAngle;
460 firePropertyChanged("rotationAngle");
463 public void setReversed(Boolean reversed) {
464 this.reversed = reversed;
465 firePropertyChanged("reversed");
468 public Vector3d getSizeChangeOffsetVector(Vector3d dir) {
470 if (rotationAngle == null)
471 q = getControlPointOrientationQuat(dir, 0.0);
473 q = getControlPointOrientationQuat(dir, rotationAngle);
474 Vector3d v = new Vector3d(0.0,offset,0.0);
475 Vector3d offset = new Vector3d();
476 MathTools.rotate(q, v, offset);
480 public Vector3d getSizeChangeOffsetVector() {
482 if (rotationAngle == null)
483 q = getControlPointOrientationQuat(0.0);
485 q = getControlPointOrientationQuat(rotationAngle);
486 Vector3d v = new Vector3d(0.0,offset,0.0);
487 Vector3d offset = new Vector3d();
488 MathTools.rotate(q, v, offset);
492 @GetPropertyValue(name="Next",tabId="Debug",value="next")
493 private String getNextString() {
496 return next.toString();
499 @GetPropertyValue(name="Previous",tabId="Debug",value="previous")
500 private String getPrevString() {
501 if (previous == null)
503 return previous.toString();
506 @GetPropertyValue(name="Sub",tabId="Debug",value="sub")
507 private String getSubString() {
508 if (children.size() == 0)
510 return Arrays.toString(children.toArray());
513 @GetPropertyValue(name="Type",tabId="Debug",value="type")
514 public String getTypeString() {
518 public Vector3d getPathLegEndpointVector() {
519 PipeControlPoint a = findPreviousEnd();
520 PipeControlPoint b = findNextEnd();
522 if (a == null || b == null) {
523 return getPathLegDirection();
526 Vector3d p1 = a.getWorldPosition();
527 Vector3d p2 = b.getWorldPosition();
529 double l = p2.length();
535 return getPathLegDirection();
539 public Vector3d getPathLegDirection() {
540 if (turnAxis == null) {
541 return getPathLegDirection(Direction.NEXT);
543 Vector3d dir = getPathLegDirection(Direction.PREVIOUS);
544 if (dir != null) dir.negate();
549 public Quat4d getControlPointOrientationQuat(double angle) {
550 Vector3d dir = getPathLegDirection();
551 if (turnAxis == null) {
552 return getControlPointOrientationQuat(dir, angle);
554 return getControlPointOrientationQuat(dir, turnAxis, angle);
558 public Quat4d getControlPointOrientationQuat(Vector3d dir, double angle, boolean reversed) {
559 if (turnAxis == null) {
560 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
562 Quat4d q = getControlPointOrientationQuat(dir, angle);
564 Quat4d q2 = new Quat4d();
565 q2.set(new AxisAngle4d(MathTools.Y_AXIS, Math.PI));
570 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
572 return getControlPointOrientationQuat(dir, turnAxis, angle);
576 public Quat4d getControlPointOrientationQuat(double angle, boolean reversed) {
577 Vector3d dir = getPathLegDirection();
578 return getControlPointOrientationQuat(dir, angle, reversed);
581 public Quat4d getControlPointOrientationQuat(Vector3d dir, double angle) {
582 if (dir == null || dir.lengthSquared() < MathTools.NEAR_ZERO)
583 return MathTools.getIdentityQuat();
585 final P3DRootNode root = getRoot();
586 Vector3d up = root != null ? new Vector3d(root.getUpVector()) : new Vector3d(0.0, 1.0, 0.0);
587 double a = up.angle(getPathLegEndpointVector());
588 if (a < 0.1 || (Math.PI - a) < 0.1) {
590 up.set(up.getY(), up.getZ(), up.getX());
593 return getControlPointOrientationQuat(dir, up, angle);
596 public P3DRootNode getRoot() {
597 INode n = getParent();
598 while (n != null && !(n instanceof P3DRootNode))
600 return (P3DRootNode) n;
603 public static Quat4d getControlPointOrientationQuat(Vector3d dir, Vector3d up, double angle) {
604 if (dir == null || dir.lengthSquared() < MathTools.NEAR_ZERO)
605 return MathTools.getIdentityQuat();
607 final Vector3d front = new Vector3d(1.0,0.0,0.0);
609 Quat4d q1 = new Quat4d();
612 Vector3d right = new Vector3d();
614 up = new Vector3d(up);
615 right.cross(dir, up);
616 up.cross(right, dir);
620 Matrix3d m = new Matrix3d();
631 //q1.set(m); MathTools contains more stable conversion
632 MathTools.getQuat(m, q1);
634 // if (DEBUG) System.out.println("PipingTools.getPipeComponentOrientationQuat() " + dir+ " " + up + " " + right);
636 Quat4d q2 = new Quat4d();
637 q2.set(new AxisAngle4d(front, angle));
642 public void insert(PipeControlPoint previous, PipeControlPoint next) {
643 // inserting an offsetpoint is error,
645 throw new RuntimeException("Dual sub points cannot be inserted.");
646 // size change control point cannot be inserted this way, because it ends PipeRun
647 // if (isSizeChange())
648 // throw new RuntimeException("Size change points cannot be inserted.");
649 PipeRun piperun = previous.getPipeRun();
650 // and just to make sure that control point structure is not corrupted
651 if (getPipeRun() != null) {
652 if (piperun != getPipeRun() || piperun != next.getPipeRun())
653 throw new RuntimeException("All controls points must be located on the same pipe run");
655 piperun.addChild(this);
658 // insert new BranchControlPoint between straight's control points
659 PipeControlPoint previousNext = previous.getNext();
660 PipeControlPoint previousPrevious = previous.getPrevious();
662 PipeControlPoint offsetCP = null;
664 offsetCP = getDualSub();
666 if (previousNext != null && previousNext == next) {
667 if (previous.isDualInline()) {
668 throw new RuntimeException();
670 if (next.isDualSub()) {
671 throw new RuntimeException();
673 previous.setNext(this);
674 this.setPrevious(previous);
675 if (previous.isDualSub()) {
676 previous.getParentPoint().setNext(this);
680 if (offsetCP == null) {
681 next.setPrevious(this);
683 next.setPrevious(offsetCP);
684 offsetCP.setNext(next);
685 offsetCP.setPrevious(previous);
688 if (next.isDualInline()) {
689 next.getDualSub().setPrevious(this);
691 } else if (previousPrevious != null && previousPrevious == next) {
692 // control point were given in reverse order
693 if (next.isDualInline())
694 throw new RuntimeException();
695 if (previous.isDualSub())
696 throw new RuntimeException();
698 this.setNext(previous);
699 if (offsetCP == null) {
700 previous.setNext(this);
702 previous.setPrevious(offsetCP);
703 offsetCP.setNext(previous);
704 offsetCP.setPrevious(next);
706 if (previous.isDualInline()) {
707 previous.getDualSub().setPrevious(this);
709 this.setPrevious(next);
711 if (next.isDualSub()) {
712 next.getParentPoint().setNext(this);
716 throw new RuntimeException();
719 PipingRules.validate(piperun);
724 public void insert(PipeControlPoint pcp, Direction direction) {
726 throw new RuntimeException();
727 if (direction == Direction.NEXT) {
728 // if direction is next, user must have given OffsetPoint
729 if (pcp.isDualInline())
730 throw new RuntimeException();
731 // basic next/prev links
733 this.setPrevious(pcp);
734 // and last take care of sizechange / offset points
735 if (pcp.isDualSub()) {
736 pcp.getParentPoint().setNext(this);
738 if (isDualInline()) {
739 getDualSub().setPrevious(this);
742 // if direction is previous, user must have given sizechange
744 throw new RuntimeException();
745 // previous direction is more complicated, since if newCP is SizeChangeControlPoint,
746 // we must link pcp to newCP's OffsetPoint
747 PipeControlPoint nocp = null;
748 if (isDualInline()) {
753 pcp.setPrevious(this);
755 pcp.setPrevious(nocp);
758 if (pcp.isDualInline()) {
759 PipeControlPoint ocp = pcp.getDualSub();
761 ocp.setPrevious(this);
763 ocp.setPrevious(nocp);
767 PipingRules.validate(getPipeRun());
770 public Vector3d getDirectedControlPointDirection() {
771 assert (isDirected());
772 Vector3d dir = new Vector3d();
773 MathTools.rotate(getWorldOrientation(), new Vector3d(1.0, 0.0, 0.0), dir);
779 * Returns direction vector.
781 * For directed control points, always returns outwards pointing vector.
784 * @return normalized vector, or null
786 public Vector3d getDirection(Direction direction) {
788 return getDirectedControlPointDirection();
789 if (isTurn() && asFixedAngle()) {
790 if (direction == Direction.NEXT) {
791 if (previous != null) {
792 PipeControlPoint pcp = this;
793 Vector3d dir = new Vector3d();
794 dir.sub(pcp.getWorldPosition(),previous.getWorldPosition());
795 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
799 Quat4d q = getControlPointOrientationQuat(dir, pcp.getRotationAngle() != null ? pcp.getRotationAngle() : 0.0);
800 AxisAngle4d aa = new AxisAngle4d(MathTools.Y_AXIS,pcp.getTurnAngle() == null ? 0.0 : pcp.getTurnAngle());
801 Quat4d q2 = MathTools.getQuat(aa);
802 Vector3d v = new Vector3d(1.,0.,0.);
803 Vector3d offset = new Vector3d();
804 MathTools.rotate(q2, v, offset);
805 MathTools.rotate(q, offset, dir);
811 PipeControlPoint pcp = this;
812 Vector3d dir = new Vector3d();
813 dir.sub(next.getWorldPosition(),pcp.getWorldPosition());
814 if (dir.lengthSquared() > MathTools.NEAR_ZERO)
818 Quat4d q = getControlPointOrientationQuat(dir, pcp.getRotationAngle() != null ? pcp.getRotationAngle() : 0.0);
819 AxisAngle4d aa = new AxisAngle4d(MathTools.Y_AXIS,pcp.getTurnAngle() == null ? 0.0 : pcp.getTurnAngle());
820 Quat4d q2 = MathTools.getQuat(aa);
821 Vector3d v = new Vector3d(1.,0.,0.);
822 Vector3d offset = new Vector3d();
823 MathTools.rotate(q2, v, offset);
824 MathTools.rotate(q, offset, dir);
834 * Returns path leg direction of the control point.
836 * This method differs from getDirection by also returning inward pointing vectors for directed control points.
841 public Vector3d getPathLegDirection(Direction direction) {
842 if (direction == Direction.NEXT) {
844 PipeControlPoint pcp = this;
845 if (pcp.isDualInline()) {
846 pcp = pcp.getDualSub();
848 Vector3d v = new Vector3d();
849 v.sub(next.getWorldPosition(),pcp.getWorldPosition());
850 if (v.lengthSquared() > MathTools.NEAR_ZERO)
856 if (previous == null) {
858 throw new RuntimeException("Cannot calculate path leg direction for unconnected control point " + this);
859 return getDirectedControlPointDirection();
862 if (isVariableAngle() && !asFixedAngle())
863 throw new RuntimeException("Cannot calculate path leg direction for unconnected variable angle control point " + this);
865 PipeControlPoint pcp = this;
866 if (pcp.isDualSub()) {
867 pcp = pcp.getParentPoint();
869 Vector3d v = new Vector3d();
870 v.sub(pcp.getWorldPosition(),previous.getWorldPosition());
871 if (v.lengthSquared() > MathTools.NEAR_ZERO)
876 } else if (isDirected()) {
877 return getDirectedControlPointDirection();
878 } else if (isEnd()) {
879 Vector3d v = new Vector3d();
880 v.sub(getWorldPosition(),previous.getWorldPosition());
881 if (v.lengthSquared() > MathTools.NEAR_ZERO)
886 } else if (isTurn() && asFixedAngle() && !_getReversed()) {
887 return getDirection(Direction.NEXT);
889 throw new RuntimeException("Missing implementation " + this);
893 if (previous != null) {
894 PipeControlPoint pcp = this;
896 pcp = getParentPoint();
897 Vector3d v = new Vector3d();
898 v.sub(previous.getWorldPosition(),pcp.getWorldPosition());
899 if (v.lengthSquared() > MathTools.NEAR_ZERO)
907 throw new RuntimeException("Cannot calculate path leg direction for unconnected control point " + this);
908 Vector3d v = getDirectedControlPointDirection();
912 if (isVariableAngle() && !asFixedAngle())
913 throw new RuntimeException("Cannot calculate path leg direction for unconnected variable angle control point " + this);
915 PipeControlPoint pcp = this;
916 if (pcp.isDualInline()) {
917 pcp = pcp.getDualSub();
919 Vector3d v = new Vector3d();
920 v.sub(pcp.getWorldPosition(),next.getWorldPosition());
921 if (v.lengthSquared() > MathTools.NEAR_ZERO)
926 } else if (isDirected()) {
927 Vector3d v = getDirectedControlPointDirection();
930 } else if (isEnd()) {
931 Vector3d v = new Vector3d();
932 v.sub(getWorldPosition(),next.getWorldPosition());
933 if (v.lengthSquared() > MathTools.NEAR_ZERO)
938 } else if (isTurn() && asFixedAngle() && _getReversed()) {
939 return getDirection(Direction.PREVIOUS);
941 throw new RuntimeException("Missing implementation " + this);
947 public void getInlineControlPointEnds(Tuple3d p1, Tuple3d p2) {
950 PipeControlPoint sub = isAxial() ? this : getDualSub();
951 Vector3d pos = getWorldPosition(), pos2 = sub == this ? pos : sub.getWorldPosition();
952 Vector3d dir = sub.getInlineDir();
954 dir.scale(length * 0.5);
961 public void getControlPointEnds(Tuple3d p1, Tuple3d p2) {
962 PipeControlPoint sub = isAxial() || isDirected() || isTurn() ? this : getChildPoints().get(0);
963 Vector3d pos = getWorldPosition(), pos2 = sub == this ? pos : sub.getWorldPosition();
968 dir2 = getInlineDir();
969 dir2.scale(length * 0.5);
970 dir1 = new Vector3d(dir2);
973 dir1 = getPathLegDirection(Direction.PREVIOUS);
974 dir2 = sub.getPathLegDirection(Direction.NEXT);
985 * Get both path leg directions, with (0,0,0) if no connection exists. The returned vectors are not normalized.
987 * @param v1 Set to the direction towards the previous control point on output
988 * @param v2 Set to the direction towards the next control point on output
990 public void getEndDirections(Tuple3d v1, Tuple3d v2) {
991 PipeControlPoint sub = isAxial() ? this : getDualSub();
993 Vector3d dir1 = getPathLegDirection(Direction.PREVIOUS);
994 Vector3d dir2 = sub.getPathLegDirection(Direction.NEXT);
1007 public void getInlineControlPointEnds(Tuple3d p1, Tuple3d p2, Vector3d dir) {
1008 assert (isInline());
1010 Vector3d pos = getWorldPosition();
1011 dir.set(getInlineDir());
1013 dir.scale(length * 0.5);
1020 public void getInlineControlPointEnds(Tuple3d center, Tuple3d p1, Tuple3d p2, Vector3d dir) {
1021 assert (isInline());
1023 Vector3d pos = getWorldPosition();
1025 dir.set(getInlineDir());
1027 dir.scale(length * 0.5);
1034 public Vector3d getInlineDir() {
1035 Vector3d dir = getPathLegDirection(Direction.NEXT);
1037 dir = getPathLegDirection(Direction.PREVIOUS);
1039 // Use reverse direction
1042 // Control point is not connected at all, use current orientation
1043 dir = new Vector3d(1,0,0);
1044 MathTools.rotate(getWorldOrientation(), dir, dir);
1050 public double getInlineLength() {
1051 if (type == PointType.TURN)
1053 else if (type == PointType.INLINE)
1054 return length * 0.5;
1059 * Return the position indicated by the argument. If the indicated direction is not connected, the
1060 * control point's wolrd position is returned instead.
1062 * @param type A selector for the position to be returned
1063 * @return The selected position
1065 public Vector3d getRealPosition(PositionType type) {
1066 Vector3d pos = getWorldPosition();
1069 double length = getInlineLength();
1072 dir = getInlineDir();
1074 dir = getPathLegDirection(Direction.NEXT);
1081 double length = getInlineLength();
1084 dir = getInlineDir();
1087 dir = getPathLegDirection(Direction.PREVIOUS);
1094 // IEntity portDir = pcp.getSingleRelatedObject(ProcessResource.plant3Dresource.HasDirection);
1095 // TODO : how we calculated needed space for a port; does it has an offset from control point's position or not?
1105 public void getInlineMovement(Tuple3d start, Tuple3d end) {
1106 // FIXME : check type of neighbor components and allow movement on top of variable length components,
1107 // find proper range for movement (pcp's position is not)
1108 PipeControlPoint p = previous.getPrevious();
1109 PipeControlPoint n = next.getNext();
1110 start.set(p.getWorldPosition());
1111 end.set(n.getWorldPosition());
1114 public PipeControlPoint findNextEnd() {
1115 ArrayList<PipeControlPoint> t = new ArrayList<PipeControlPoint>();
1116 return findNextEnd( t);
1119 public PipeControlPoint findPreviousEnd() {
1120 ArrayList<PipeControlPoint> t = new ArrayList<PipeControlPoint>();
1121 return findPreviousEnd(t);
1124 public PipeControlPoint findNextEnd(List<PipeControlPoint> nextList) {
1126 PipeControlPoint pcp = null;
1127 PipeControlPoint p = null;
1128 if (nextList.size() == 0)
1132 p = nextList.get(nextList.size() - 1);
1137 if (nextList.size() > 0)
1138 nextList.remove(nextList.size() - 1);
1139 // if (DEBUG) System.out.println(" " + pcp.getResource() + " not full");
1143 if (pcp.isPathLegEnd()) {
1144 //if (DEBUG) System.out.println(" " + pcp.getResource());
1148 // if (DEBUG) System.out.print(" " + pcp.getResource());
1153 public PipeControlPoint findPreviousEnd(List<PipeControlPoint> prevList) {
1155 PipeControlPoint pcp = null;
1156 PipeControlPoint p = null;
1157 if (prevList.size() == 0)
1161 p = prevList.get(prevList.size() - 1);
1163 pcp = p.getPrevious();
1166 if (prevList.size() > 0)
1167 prevList.remove(prevList.size() - 1);
1168 // if (DEBUG) System.out.println(" " + pcp.getResource() + " not full");
1171 if (pcp.isPathLegEnd()) {
1172 // if (DEBUG) System.out.println(" " + pcp.getResource());
1176 // if (DEBUG)System.out.print(" " + pcp.getResource());
1181 public void _remove() {
1186 public PipeControlPoint getDualSub() {
1188 return getChildPoints().get(0);
1190 throw new IllegalStateException("Current control point is not dual inline");
1194 public void _remove(boolean renconnect) {
1198 if (DEBUG) System.out.println(this + " Remove " + renconnect);
1200 if (getParentPoint() != null) {
1201 getParentPoint()._remove(renconnect);
1204 PipeRun pipeRun = getPipeRun();
1205 // PipeRUn removal has been changed, so pipeRun may be null.
1206 // if (pipeRun == null)
1209 PipeControlPoint additionalRemove = null;
1210 if (!PipingRules.isEnabled()) {
1216 PipeControlPoint currentPrev = previous;
1217 PipeControlPoint currentNext = next;
1218 if (currentNext == null && currentPrev == null) {
1220 if (pipeRun != null) {
1221 pipeRun.remChild(this);
1222 checkRemove(pipeRun);
1226 if (currentNext != null && currentPrev != null) {
1227 boolean link = renconnect;
1228 if (currentNext.isBranchEnd()) {
1230 currentNext.remove();
1234 if (currentPrev.isBranchEnd()) {
1236 currentPrev.remove();
1241 if (currentPrev.isDirected() && currentNext.isDirected())
1243 else if (this.isDualSub()) {
1244 throw new RuntimeException("_remove() is called for parent point, somehow got to child point. " + this);
1247 if (currentNext == null) {
1249 } else if (currentNext.isDualInline()) {
1250 PipeControlPoint sccp = currentNext;
1251 PipeControlPoint ocp = currentNext.getDualSub();
1253 throw new RuntimeException("Removing PipeControlPoint " + this+ " structure damaged, no offset control point");
1256 sccp.setPrevious(currentPrev);
1257 //ocp.setPrevious(currentPrev);
1258 assert(ocp.getPrevious() == currentPrev);
1260 sccp.setPrevious(null);
1261 //ocp.setPrevious(null);
1262 assert(ocp.getPrevious() == null);
1265 } else if (currentNext.isDualSub()) {
1266 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, next control point is offset control point");
1267 } else if (currentNext.previous == this) {
1269 currentNext.setPrevious(currentPrev);
1271 currentNext.setPrevious(null);
1274 } else if (isDualInline()) {
1275 if (currentNext.previous != getDualSub()) {
1276 throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1279 currentNext.setPrevious(currentPrev);
1281 currentNext.setPrevious(null);
1285 throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1287 if (currentPrev == null) {
1289 } else if (currentPrev.isDualInline()) {
1290 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, previous control point is size change control point");
1291 } else if (currentPrev.isDualSub()) {
1292 PipeControlPoint ocp = currentPrev;
1293 PipeControlPoint sccp = currentPrev.getParentPoint();
1295 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, no size change control point");
1297 //ocp.setNext(currentNext);
1298 sccp.setNext(currentNext);
1299 assert(ocp.getNext() == currentNext);
1301 //ocp.setNext(null);
1303 assert(ocp.getNext() == null);
1306 } else if (currentPrev.next == this) {
1308 currentPrev.setNext(currentNext);
1310 currentPrev.setNext(null);
1314 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged");
1317 if (currentNext.isVariableLength() && currentPrev.isVariableLength()) {
1318 // we have to join them into single variable length component.
1319 additionalRemove = currentPrev;
1320 // combine lengths and set the location of remaining control point to the center.
1321 Point3d ps = new Point3d();
1322 Point3d pe = new Point3d();
1323 Point3d ns = new Point3d();
1324 Point3d ne = new Point3d();
1325 currentPrev.getInlineControlPointEnds(ps, pe);
1326 currentNext.getInlineControlPointEnds(ns, ne);
1327 double l = currentPrev.getLength() + currentNext.getLength();
1328 Vector3d cp = new Vector3d();
1331 currentNext.setLength(l);
1332 currentNext.setWorldPosition(cp);
1335 // FIXME : pipe run must be split into two parts, since the control point structure is no more continuous.
1337 } else if (currentNext != null) {
1338 if (currentNext.isDualInline()) {
1339 PipeControlPoint sccp = currentNext;
1340 PipeControlPoint ocp = currentNext.getDualSub();
1342 throw new RuntimeException("Removing PipeControlPoint " + this+ " structure damaged, no offset control point");
1344 sccp.setPrevious(null);
1345 assert(ocp.getPrevious() == null);
1346 //ocp.setPrevious(null);
1347 } else if (currentNext.isDualSub()) {
1348 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, next control point is offset control point");
1349 } else if (currentNext.previous == this) {
1350 currentNext.setPrevious(null);
1351 } else if (isDualInline()) {
1352 if (currentNext.previous != getDualSub()) {
1353 throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1355 currentNext.setPrevious(null);
1357 throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1360 } else { //(previous != null)
1361 if(currentPrev.isDualInline()) {
1362 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, previous control point is size change control point");
1363 } else if (currentPrev.isDualSub()) {
1364 PipeControlPoint ocp = currentPrev;
1365 PipeControlPoint sccp = currentPrev.getParentPoint();
1367 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, no size change control point");
1370 assert(ocp.getNext() == null);
1371 } else if (currentPrev.next == this) {
1372 currentPrev.setNext(null);
1374 throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1378 if (children.size() > 0 ) {
1380 } else if (parent!= null) {
1381 removeParentPoint();
1387 if (pipeRun != null) {
1388 pipeRun.remChild(this);
1389 checkRemove(pipeRun);
1390 if (PipingRules.isEnabled() && pipeRun.getParent() != null && pipeRun.getControlPoints().size() > 0)
1391 PipingRules.validate(pipeRun);
1393 if (additionalRemove != null)
1394 additionalRemove.remove();
1399 * Removes control point and attempts to reconnect next/prev
1401 * If this point is size change (PipeRuns are different on both sides), then reconnection cannot be made.
1403 public void remove() {
1404 PipeControlPoint currentPrev = previous;
1405 PipeControlPoint currentNext = next;
1408 if (currentNext != null)
1409 if (!currentNext.checkRemove())
1410 PipingRules.requestUpdate(currentNext);
1411 if (currentPrev != null)
1412 if (!currentPrev.checkRemove())
1413 PipingRules.requestUpdate(currentPrev);
1414 } catch (Exception e) {
1415 e.printStackTrace();
1421 * Removes control point without attempting to reconnect next/prev.
1422 * This usually leads to creation of another PipeRun for the control points after this point.
1424 public void removeAndSplit() {
1425 PipeControlPoint currentPrev = previous;
1426 PipeControlPoint currentNext = next;
1428 if (next != null && previous != null) {
1429 P3DRootNode root = (P3DRootNode)getPipelineComponent().getRootNode();
1430 PipeRun nextPipeRun = new PipeRun();
1431 nextPipeRun.setName(root.getUniqueName("PipeRun"));
1432 root.addChild(nextPipeRun);
1434 PipeRun previousRun = previous.getPipeRun();
1435 nextPipeRun.setPipeDiameter(previousRun.getPipeDiameter());
1436 nextPipeRun.setTurnRadiusArray(previousRun.getTurnRadiusArray());
1438 PipelineComponent n = next.getPipelineComponent();
1440 if (n.getPipeRun() != previousRun)
1442 if (! (n instanceof Nozzle)) {
1444 nextPipeRun.addChild(n);
1446 n.setPipeRun(nextPipeRun);
1452 if (currentNext != null)
1453 if (!currentNext.checkRemove())
1454 PipingRules.requestUpdate(currentNext);
1455 if (currentPrev != null)
1456 if (!currentPrev.checkRemove())
1457 PipingRules.requestUpdate(currentPrev);
1458 } catch (Exception e) {
1459 e.printStackTrace();
1464 * This is called when adjacent control point is removed.
1466 * This call should remove the give point, if the point cannot exist alone.
1467 * At the moment there is one such case: branch.
1471 protected boolean checkRemove() {
1472 if (getParentPoint() != null) {
1473 return getParentPoint().checkRemove();
1475 if (getPipelineComponent() == null)
1476 return true; // already removed
1477 if (getPipelineComponent().getType().equals("Plant3D.URIs.Builtin_BranchSplitComponent")) {
1478 if (getChildPoints().get(0).getNext() == null && getChildPoints().get(0).getPrevious() == null) {
1483 return checkRemove(getPipeRun());
1487 private boolean checkRemove(PipeRun pipeRun) {
1488 if (pipeRun == null)
1490 Collection<PipeControlPoint> points = pipeRun.getControlPoints();
1491 if (points.size() == 0) {
1494 } else if (points.size() == 1) {
1495 PipeControlPoint pcp = points.iterator().next();
1496 if (pcp.isDeletable() && pcp.getNext() == null && pcp.getPrevious() == null) {
1497 pcp._remove(); // This call will recursively call also this method...
1500 } else if (points.size() == 2) {
1506 private void removeSubPoints() {
1507 for (PipeControlPoint p : children) {
1511 PipeControlPoint currentNext = p.getNext();
1512 PipeControlPoint currentPrev = p.getPrevious();
1514 p._setPrevious(null);
1515 PipeRun run = p.getPipeRun();
1520 if (currentNext != null)
1521 if (!currentNext.checkRemove())
1522 PipingRules.requestUpdate(currentNext);
1523 if (currentPrev != null)
1524 if (!currentPrev.checkRemove())
1525 PipingRules.requestUpdate(currentPrev);
1531 private void removeParentPoint() {
1532 throw new RuntimeException("Child points cannot be removed directly");
1535 public boolean isRemoved() {
1536 return component == null;
1539 private void removeComponent() {
1540 if (component == null)
1542 PipelineComponent next = component.getNext();
1543 PipelineComponent prev = component.getPrevious();
1544 PipelineComponent br0 = component.getBranch0();
1545 component.setNext(null);
1546 component.setPrevious(null);
1547 component.setBranch0(null);
1549 if (next.getNext() == component)
1551 else if (next.getPrevious() == component)
1552 next.setPrevious(null);
1553 else if (next.getBranch0() == component)
1554 next.setBranch0(null);
1557 if (prev.getNext() == component)
1559 else if (prev.getPrevious() == component)
1560 prev.setPrevious(null);
1561 else if (prev.getBranch0() == component)
1562 prev.setBranch0(null);
1565 if (br0.getNext() == component)
1567 else if (br0.getPrevious() == component)
1568 br0.setPrevious(null);
1569 else if (br0.getBranch0() == component)
1570 br0.setBranch0(null);
1572 PipelineComponent comp = component;
1579 public void setOrientation(Quat4d orientation) {
1580 if (MathTools.equals(orientation, getOrientation()))
1582 if (getPipelineComponent() != null && (getPipelineComponent() instanceof Nozzle))
1583 System.out.println();
1584 super.setOrientation(orientation);
1585 if (getParentPoint() == null && component != null)
1586 component._setWorldOrientation(getWorldOrientation());
1591 public void setPosition(Vector3d position) {
1592 if (MathTools.equals(position, getPosition()))
1594 if (Double.isNaN(position.x) || Double.isNaN(position.y) || Double.isNaN(position.z))
1595 throw new IllegalArgumentException("NaN is not supported");
1596 super.setPosition(position);
1597 if (getParentPoint() == null && component != null)
1598 component._setWorldPosition(getWorldPosition());
1602 private void updateSubPoint() {
1604 if (next == null && previous == null) {
1605 for (PipeControlPoint sub : getChildPoints()) {
1606 sub.setWorldPosition(getWorldPosition());
1607 sub.setWorldOrientation(getWorldOrientation());
1611 for (PipeControlPoint sub : getChildPoints()) {
1612 Vector3d wp = getWorldPosition();
1613 wp.add(getSizeChangeOffsetVector());
1614 sub.setWorldPosition(wp);
1615 sub.setWorldOrientation(getWorldOrientation());
1618 for (PipeControlPoint sub : getChildPoints()) {
1619 sub.setWorldPosition(getWorldPosition());
1620 sub.setWorldOrientation(getWorldOrientation());
1626 public void _setWorldPosition(Vector3d position) {
1627 Vector3d localPos = getLocalPosition(position);
1628 super.setPosition(localPos);
1632 public void _setWorldOrientation(Quat4d orientation) {
1633 Quat4d localOr = getLocalOrientation(orientation);
1634 super.setOrientation(localOr);
1639 public String toString() {
1640 return getClass().getName() + "@" + Integer.toHexString(hashCode());