]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipeControlPoint.java
Allow unsplitting removal of reducers by joining pipe runs
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / controlpoint / PipeControlPoint.java
1 package org.simantics.plant3d.scenegraph.controlpoint;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.List;
7
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;
14
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;
23
24 import vtk.vtkRenderer;
25
26
27 public class PipeControlPoint extends G3DNode implements IP3DNode {
28
29         private static boolean DEBUG = false;
30
31         public enum PointType{INLINE,TURN,END};
32         public enum Direction{NEXT,PREVIOUS};
33         public enum PositionType {SPLIT,NEXT,PREVIOUS,PORT}
34
35         private PipelineComponent component;
36
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
45
46         private boolean disposed = false;
47         
48         public PipeControlPoint(PipelineComponent component) {
49                 this.component = component;
50                 if (component.getPipeRun() != null)
51                         component.getPipeRun().addChild(this);
52
53         }
54
55         public PipeControlPoint(PipelineComponent component, PipeRun piperun) {
56                 this.component = component;
57                 piperun.addChild(this);
58         }
59
60         @Override
61         public void update(vtkRenderer ren) {
62                 try {
63                         PipingRules.requestUpdate(this);
64                 } catch (Exception e) {
65                         e.printStackTrace();
66                 }
67
68         }
69
70         public PipeRun getPipeRun() {
71                 return (PipeRun)getParent();
72         }
73
74         public PipelineComponent getPipelineComponent() {
75                 return component;
76         }
77
78         public PointType getType() {
79                 return type;
80         }
81
82         public void setType(PointType type) {
83                 this.type = type;
84         }
85
86         @GetPropertyValue(name="Fixed",tabId="Debug",value="fixed")
87         public boolean isFixed() {
88                 return isFixed;
89         }
90
91         public void setFixed(boolean fixed) {
92                 this.isFixed = fixed;
93         }
94         
95         @GetPropertyValue(name="Mod",tabId="Debug",value="mod")
96         public boolean isMod() {
97         return isMod;
98     }
99     
100     public void setMod(boolean isMod) {
101         this.isMod = isMod;
102     }
103
104         @GetPropertyValue(name="Rotate",tabId="Debug",value="rotate")
105         public boolean isRotate() {
106                 return isRotate;
107         }
108
109         public void setRotate(boolean rotate) {
110                 this.isRotate = rotate;
111         }
112
113         @GetPropertyValue(name="Reverse",tabId="Debug",value="reverse")
114         public boolean isReverse() {
115                 return isReverse;
116         }
117
118         public void setReverse(boolean reverse) {
119                 this.isReverse = reverse;
120         }
121
122         public void setSub(boolean sub) {
123                 this.isSub = sub;
124         }
125
126         @GetPropertyValue(name="Deletable",tabId="Debug",value="deletable")
127         public boolean isDeletable() {
128                 return isDeletable;
129         }
130
131         public void setDeletable(boolean deletable) {
132                 this.isDeletable = deletable;
133         }
134
135         public boolean isPathLegEnd() {
136                 return type != PointType.INLINE;
137         }
138
139         public boolean isEnd() {
140                 return type == PointType.END;
141         }
142
143         public boolean isTurn() {
144                 return type == PointType.TURN;
145         }
146
147         public boolean isInline() {
148                 return type == PointType.INLINE;
149         }
150         
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;
154         }
155
156         /**
157          * True for end components, if control point defines absolute position direction, which rules cannot modify. 
158          * This is typical for nozzles.
159          * @return
160          */
161         public boolean isDirected() {
162                 return isFixed && isEnd();
163         }
164
165         /**
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.
168      * @return
169      */
170         public boolean isNonDirected() {
171                 return !isFixed && isEnd();
172         }
173
174         public boolean isVariableLength() {
175                 return !isFixed && isInline();
176         }
177         
178         /**
179          * Fixed length in-line component is such that piping rules cannot modify the length.
180          * @return
181          */
182         public boolean isFixedLength() {
183         return isFixed && isInline();
184     }
185
186         public boolean isVariableAngle() {
187                 return !isFixed && isTurn();
188         }
189         
190         /**
191          * Fixed angle turn component is such that piping rules cannot modify the angle.
192          * @return
193          */
194         public boolean isFixedAngle() {
195         return isFixed && isTurn();
196     }
197         
198         /**
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. 
201          * @return
202          */
203         public boolean asFixedAngle() {
204         return isTurn() && (isFixed || next == null || previous == null);
205     }
206
207         public boolean isBranchEnd() {
208                 return isDeletable && isEnd();
209         }
210
211         public boolean isOffset() {
212                 return offset != null;
213         }
214
215         public boolean isDualSub() {
216                 return parent != null && isSub;
217         }
218
219         public boolean isDualInline() {
220                 return children.size() == 1 && children.get(0).isDualSub();
221         }
222
223         public boolean isAxial() {
224                 return isInline() && !isDualInline();
225         }
226
227         public boolean isSizeChange() {
228                 return isSizeChange;
229                 //              if (children.size() == 0)
230                 //                      return false;
231                 //              if (!isDualInline())
232                 //                      return false;
233                 //              return getPipeRun() != children.get(0).getPipeRun();
234         }
235
236         public void setSizeChange(boolean isSizeChange) {
237                 this.isSizeChange = isSizeChange;
238         }
239
240
241         private PipeControlPoint next;
242         private PipeControlPoint previous;
243
244         public PipeControlPoint getNext() {
245                 return next;
246         }
247
248         public PipeControlPoint getPrevious() {
249                 return previous;
250         }
251
252         public void setNext(PipeControlPoint next) {
253             if (isSub) {
254                 getParentPoint().setNext(next);
255                 return;
256             }
257             if (next != null && next.isDualSub())
258                 next = next.parent;
259             if (_setNext(next)) {
260                 for (PipeControlPoint pcp : children) {
261                 if (pcp.isSub)
262                     pcp._setNext(next);
263             }
264                 updateSubPoint();
265             }
266         }
267         
268         public void setPrevious(PipeControlPoint prev) {
269         if (isSub) {
270             getParentPoint().setPrevious(prev);
271             return;
272         }
273         if (prev != null && prev.isDualInline())
274             prev = prev.children.get(0);
275         if (_setPrevious(prev)) {
276             for (PipeControlPoint pcp : children) {
277                 if (pcp.isSub)
278                     pcp._setPrevious(prev);
279             }
280             updateSubPoint();
281         }
282     }
283         
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");
287                 if (next == this)
288                         throw new RuntimeException("Cannot connect to self");
289                 if (this.next == next)
290                         return false;
291                 if (DEBUG) System.out.println(this + " next " + next);
292                 if (next == null && isVariableAngle() && previous != null && !isRemoved()) {
293                     convertVariableAngleToFixed(Direction.NEXT);
294                 }
295                 this.next = next;
296                 if (component != null) {
297                         if (parent == null || isSub)
298                                 component.setNext(next != null ? next.component : null);
299                         else
300                                 component.setBranch0(next != null ? next.component : null);
301                         
302                 }
303                 return true;
304         }
305
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)
312                         return false;
313                 if (DEBUG) System.out.println(this + " previous " + previous);
314                 if (previous == null && isVariableAngle() && next != null && !isRemoved()) {
315             convertVariableAngleToFixed(Direction.PREVIOUS);
316         }
317                 this.previous = previous;
318                 if (component != null) {
319                         if (parent == null || isSub)
320                                 component.setPrevious(previous != null ? previous.component : null);
321                         else
322                                 component.setBranch0(previous != null ? previous.component : null);
323                         updateSubPoint();
324                 }
325                 return true;
326         }
327         
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)
335             return;
336         dir.negate();
337         double angle = dir.angle(dirOut);
338         //super._setNext(null);
339         if (direction == Direction.NEXT)
340             next = null;
341         else
342             previous = null;
343         setRotationAngle(0.0);
344         setReversed(direction == Direction.NEXT ? false : true);
345         Vector3d dirOutN = getPathLegDirection(direction == Direction.NEXT ? Direction.NEXT : Direction.PREVIOUS);
346         dirOutN.normalize();
347         AxisAngle4d aa = new AxisAngle4d();
348         if (MathTools.createRotation(dirOutN, dirOut, dir, aa)) {
349             setRotationAngle(aa.angle);
350             setTurnAngle(angle);
351             if (DEBUG) System.out.println("convertToFixed " + dir + " " + dirOut + " " +dirOutN + " " +angle + " "+ aa.angle);
352         }
353         }
354
355         public PipeControlPoint parent;
356         public List<PipeControlPoint> children = new ArrayList<PipeControlPoint>();
357
358         public List<PipeControlPoint> getChildPoints() {
359                 return children;
360         }
361
362         public PipeControlPoint getParentPoint() {
363                 return parent;
364         }
365
366
367         private double length;
368         private Double turnAngle;
369         private Vector3d turnAxis;
370
371         private Double offset;
372         private Double rotationAngle;
373         private Boolean reversed;
374
375         @GetPropertyValue(name="Length",tabId="Debug",value="length")
376         public double getLength() {
377                 return length;
378         }
379
380         public void setLength(double l) {
381                 if (Double.isInfinite(l) || Double.isNaN(l)) {
382                         return;
383                 }
384                 if (Math.abs(this.length-l) < MathTools.NEAR_ZERO)
385                         return;
386                 this.length = l;
387                 firePropertyChanged("length");
388                 if (isDualInline())
389                     getDualSub().setLength(l);
390         }
391
392         @GetPropertyValue(name="Turn Angle",tabId="Debug",value="turnAngle")
393         public Double getTurnAngle() {
394                 return turnAngle;
395         }
396
397         @GetPropertyValue(name="Turn Axis",tabId="Debug",value="turnAxis")
398         public Vector3d getTurnAxis() {
399                 return turnAxis;
400         }
401
402         @GetPropertyValue(name="Offset",tabId="Debug",value="offset")
403         public Double getOffset() {
404                 return offset;
405         }
406
407         @GetPropertyValue(name="Rotation Angle",tabId="Debug",value="rotationAngle")
408         public Double getRotationAngle() {
409             if (isRotate || asFixedAngle())
410                 return rotationAngle;
411             return null;
412         }
413
414         @GetPropertyValue(name="Reversed",tabId="Debug",value="reversed")
415         public Boolean getReversed() {
416                 return reversed;
417         }
418
419         public boolean _getReversed() {
420                 if (reversed == null)
421                         return false;
422                 return reversed;
423         }
424
425         public void setTurnAngle(Double turnAngle) {
426                 if (turnAngle == null || Double.isInfinite(turnAngle) || Double.isNaN(turnAngle)) {
427                         return;
428                 }
429                 if (this.turnAngle != null && Math.abs(this.turnAngle-turnAngle) < MathTools.NEAR_ZERO)
430                         return;
431                 this.turnAngle = turnAngle;
432                 firePropertyChanged("turnAngle");
433         }
434
435         public void setTurnAxis(Vector3d turnAxis) {
436                 if (this.turnAxis != null && MathTools.equals(turnAxis, this.turnAxis))
437                         return;
438                 this.turnAxis = turnAxis;
439                 firePropertyChanged("turnAxis");
440         }
441
442         public void setOffset(Double offset) {
443                 if (Double.isInfinite(offset) || Double.isNaN(offset)) {
444                         return;
445                 }
446                 if (this.offset != null && Math.abs(this.offset-offset) < MathTools.NEAR_ZERO)
447                         return;
448                 this.offset = offset;
449                 firePropertyChanged("offset");
450         }
451
452         public void setRotationAngle(Double rotationAngle) {
453                 if (Double.isInfinite(rotationAngle) || Double.isNaN(rotationAngle)) {
454                         return;
455                 }
456                 if (this.rotationAngle != null && Math.abs(this.rotationAngle-rotationAngle) < MathTools.NEAR_ZERO)
457                         return;
458                 this.rotationAngle = rotationAngle;
459                 firePropertyChanged("rotationAngle");
460         }
461
462         public void setReversed(Boolean reversed) {
463                 this.reversed = reversed;
464                 firePropertyChanged("reversed");
465         }
466
467         public Vector3d getSizeChangeOffsetVector(Vector3d dir) {
468                 Quat4d q;
469                 if (rotationAngle == null)
470                         q = getControlPointOrientationQuat(dir, 0.0);
471                 else
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);
476                 return offset;
477         }
478
479         public Vector3d getSizeChangeOffsetVector() {
480                 Quat4d q;
481                 if (rotationAngle == null)
482                         q = getControlPointOrientationQuat(0.0);
483                 else
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);
488                 return offset;
489         }
490
491         @GetPropertyValue(name="Next",tabId="Debug",value="next")
492         private String getNextString() {
493                 if (next == null)
494                         return null;
495                 return next.toString();
496         }
497
498         @GetPropertyValue(name="Previous",tabId="Debug",value="previous")
499         private String getPrevString() {
500                 if (previous == null)
501                         return null;
502                 return previous.toString();
503         }
504
505         @GetPropertyValue(name="Sub",tabId="Debug",value="sub")
506         private String getSubString() {
507                 if (children.size() == 0)
508                         return "";
509                 return Arrays.toString(children.toArray());
510         }
511
512         @GetPropertyValue(name="Type",tabId="Debug",value="type")
513         public String getTypeString() {
514                 return type.name();
515         }
516
517         public Quat4d getControlPointOrientationQuat(double angle) {
518
519                 if (turnAxis == null) {
520                         Vector3d dir = getPathLegDirection(Direction.NEXT);
521                         return getControlPointOrientationQuat(dir, angle);
522                 } else {
523                         Vector3d dir = getPathLegDirection(Direction.PREVIOUS);
524                         if (dir != null) dir.negate();
525                         return getControlPointOrientationQuat(dir, turnAxis, angle);
526                 }
527         }
528         
529         public Quat4d getControlPointOrientationQuat(Vector3d dir, double angle, boolean reversed) {
530             if (turnAxis == null) {
531             if (dir.lengthSquared() > MathTools.NEAR_ZERO)
532                 dir.normalize();
533             Quat4d q =  getControlPointOrientationQuat(dir, angle);
534             if (reversed) {
535                 Quat4d q2 = new Quat4d();
536                 q2.set(new AxisAngle4d(MathTools.Y_AXIS, Math.PI));
537                 q.mulInverse(q2);
538             }
539             return q;
540         } else {
541             if (dir.lengthSquared() > MathTools.NEAR_ZERO)
542                 dir.normalize();
543             return getControlPointOrientationQuat(dir, turnAxis, angle);
544         }
545         }
546
547         public Quat4d getControlPointOrientationQuat(double angle, boolean reversed) {
548
549                 if (turnAxis == null) {
550                         Vector3d dir = getPathLegDirection(Direction.NEXT);
551                         return getControlPointOrientationQuat(dir, angle, reversed);
552                 } else {
553                         Vector3d dir = getPathLegDirection(Direction.PREVIOUS);
554                         dir.negate();
555                         return getControlPointOrientationQuat(dir, angle, reversed);
556                 }
557         }
558
559
560
561         public static Quat4d getControlPointOrientationQuat(Vector3d dir, double angle) {
562                 if (dir == null || dir.lengthSquared() < MathTools.NEAR_ZERO)
563                         return MathTools.getIdentityQuat();
564
565
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);
570                 }
571
572
573                 return getControlPointOrientationQuat(dir, up, angle);
574         }
575
576         public static Quat4d getControlPointOrientationQuat(Vector3d dir, Vector3d up,  double angle) {
577                 if (dir == null || dir.lengthSquared() < MathTools.NEAR_ZERO)
578                         return MathTools.getIdentityQuat();
579
580                 final Vector3d front = new Vector3d(1.0,0.0,0.0);
581
582                 Quat4d q1 = new Quat4d();
583
584
585                 Vector3d right = new Vector3d();
586
587                 right.cross(dir, up);
588                 up.cross(right, dir);
589                 right.normalize();
590                 up.normalize();
591
592                 Matrix3d m = new Matrix3d();
593                 m.m00 = dir.x;
594                 m.m10 = dir.y;
595                 m.m20 = dir.z;
596                 m.m01 = up.x;
597                 m.m11 = up.y;
598                 m.m21 = up.z;
599                 m.m02 = right.x;
600                 m.m12 = right.y;
601                 m.m22 = right.z;
602
603                 //q1.set(m); MathTools contains more stable conversion
604                 MathTools.getQuat(m, q1);
605
606                 //                      if (DEBUG) System.out.println("PipingTools.getPipeComponentOrientationQuat() " + dir+ " " + up + " " + right);
607
608                 Quat4d q2 = new Quat4d();
609                 q2.set(new AxisAngle4d(front, angle));
610                 q1.mul(q2);
611                 return q1;
612         }
613
614         public void insert(PipeControlPoint previous, PipeControlPoint next) {
615                 // inserting an offsetpoint is error, 
616                 if (isDualSub())
617                         throw new RuntimeException("Dual sub points cannot be inserted.");
618                 // size change control point cannot be inserted this way, because it ends PipeRun
619 //              if (isSizeChange())
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");
626                 } else {
627                         piperun.addChild(this);
628                 }
629
630                 // insert new BranchControlPoint between straight's control points
631                 PipeControlPoint previousNext = previous.getNext();
632                 PipeControlPoint previousPrevious = previous.getPrevious();
633
634                 PipeControlPoint offsetCP = null;
635                 if (isOffset()) {
636                         offsetCP = getDualSub();
637                 }
638                 if (previousNext != null && previousNext == next) {
639                         if (previous.isDualInline()) {
640                                 throw new RuntimeException();
641                         }
642                         if (next.isDualSub()) {
643                                 throw new RuntimeException();
644                         }
645                         previous.setNext(this);
646                         this.setPrevious(previous);
647                         if (previous.isDualSub()) {
648                                 previous.getParentPoint().setNext(this);
649                         }
650                         this.setNext(next);
651
652                         if (offsetCP == null) {
653                                 next.setPrevious(this);
654                         } else {
655                                 next.setPrevious(offsetCP);
656                                 offsetCP.setNext(next);
657                                 offsetCP.setPrevious(previous);
658                         }
659
660                         if (next.isDualInline()) {
661                                 next.getDualSub().setPrevious(this);
662                         }
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();
669
670                         this.setNext(previous);
671                         if (offsetCP == null) {
672                                 previous.setNext(this);
673                         } else {
674                                 previous.setPrevious(offsetCP);
675                                 offsetCP.setNext(previous);
676                                 offsetCP.setPrevious(next);
677                         }
678                         if (previous.isDualInline()) {
679                                 previous.getDualSub().setPrevious(this);
680                         }
681                         this.setPrevious(next);
682                         next.setNext(this);
683                         if (next.isDualSub()) {
684                                 next.getParentPoint().setNext(this);
685                         }
686
687                 } else {
688                         throw new RuntimeException();
689                 }       
690
691                 PipingRules.validate(piperun);
692         }
693
694
695
696         public void insert(PipeControlPoint pcp, Direction direction) {
697                 if (isDualSub())
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
704                         pcp.setNext(this);
705                         this.setPrevious(pcp);
706                         // and last take care of sizechange / offset points
707                         if (pcp.isDualSub()) {
708                                 pcp.getParentPoint().setNext(this);
709                         }
710                         if (isDualInline()) {
711                             getDualSub().setPrevious(this);
712                         }
713                 } else {
714                         // if direction is previous, user must have given sizechange
715                         if (pcp.isDualSub())
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()) {
721                                 nocp = getDualSub();
722                                 nocp.setNext(pcp);
723                         }
724                         if (nocp == null) {
725                                 pcp.setPrevious(this);
726                         } else {
727                                 pcp.setPrevious(nocp);
728                         }
729                         this.setNext(pcp);
730                         if (pcp.isDualInline()) {
731                                 PipeControlPoint ocp = pcp.getDualSub();
732                                 if (nocp == null)
733                                         ocp.setPrevious(this);
734                                 else
735                                         ocp.setPrevious(nocp);
736                         }
737
738                 }
739                 PipingRules.validate(getPipeRun());
740         }
741
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);
746                 dir.normalize();
747                 return dir;
748         }
749         
750         /**
751          * Returns direction vector. 
752          * 
753          * For directed control points, always returns outwards pointing vector.
754          * 
755          * @param direction
756          * @return normalized vector, or null
757          */
758         public Vector3d getDirection(Direction direction) {
759         if (isDirected())
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)
768                         dir.normalize();
769                     else
770                         return null;
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);
778                     dir.normalize();
779                     return dir;
780                 }
781             } else {
782                 if (next != null) {
783                     PipeControlPoint pcp = this;
784                     Vector3d dir = new Vector3d();
785                     dir.sub(next.getWorldPosition(),pcp.getWorldPosition());
786                     if (dir.lengthSquared() > MathTools.NEAR_ZERO)
787                         dir.normalize();
788                     else
789                         return null;
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);
797                     dir.normalize();
798                     return dir;
799                 }
800             }
801         }
802         return null;
803     }
804
805         /**
806          * Returns path leg direction of the control point.
807          * 
808          * This method differs from getDirection by also returning inward pointing vectors for directed control points.
809          * 
810          * @param direction
811          * @return
812          */
813         public Vector3d getPathLegDirection(Direction direction) {
814                 if (direction == Direction.NEXT) {
815                         if (next != null) {
816                                 PipeControlPoint pcp = this;
817                                 if (pcp.isDualInline()) {
818                                         pcp = pcp.getDualSub();
819                                 }
820                                 Vector3d v = new Vector3d();
821                                 v.sub(next.getWorldPosition(),pcp.getWorldPosition());
822                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
823                     v.normalize();
824                 else
825                     return null;
826                                 return v;
827                         } else {
828                                 if (previous == null) {
829                                         if (!isDirected())
830                                                 throw new RuntimeException("Cannot calculate path leg direction for unconnected control point " + this);
831                                         return getDirectedControlPointDirection();
832
833                                 } else {
834                                         if (isVariableAngle() && !asFixedAngle())
835                                                 throw new RuntimeException("Cannot calculate path leg direction for unconnected variable angle control point " + this);
836                                         if (isInline()) {
837                                                 PipeControlPoint pcp = this;
838                                                 if (pcp.isDualSub()) {
839                                                         pcp = pcp.getParentPoint();
840                                                 }
841                                                 Vector3d v = new Vector3d();
842                                                 v.sub(pcp.getWorldPosition(),previous.getWorldPosition());
843                                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
844                                 v.normalize();
845                                                 else
846                                                     return null;
847                                                 return v;
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)
854                             v.normalize();
855                         else
856                             return null;
857                                                 return v;
858                                         } else if (isTurn() && asFixedAngle() && !_getReversed()) {
859                                                 return getDirection(Direction.NEXT);
860                                         }
861                                         throw new RuntimeException("Missing implementation " + this);
862                                 }
863                         }
864                 } else {
865                         if (previous != null) {
866                                 PipeControlPoint pcp = this;
867                                 if (isDualSub()) 
868                                         pcp = getParentPoint();
869                                 Vector3d v = new Vector3d();
870                                 v.sub(previous.getWorldPosition(),pcp.getWorldPosition());
871                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
872                     v.normalize();
873                 else
874                     return null;
875                                 return v;
876                         } else {
877                                 if (next == null)  {
878                                         if (!isDirected())
879                                                 throw new RuntimeException("Cannot calculate path leg direction for unconnected control point " + this);
880                                         Vector3d v = getDirectedControlPointDirection();
881                                         v.negate();
882                                         return v;
883                                 } else {
884                                         if (isVariableAngle() && !asFixedAngle())
885                                                 throw new RuntimeException("Cannot calculate path leg direction for unconnected variable angle control point " + this);
886                                         if (isInline()) {
887                                                 PipeControlPoint pcp = this;
888                                                 if (pcp.isDualInline()) {
889                                                         pcp = pcp.getDualSub();
890                                                 }
891                                                 Vector3d v = new Vector3d();
892                                                 v.sub(pcp.getWorldPosition(),next.getWorldPosition());
893                                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
894                             v.normalize();
895                         else
896                             return null;
897                                                 return v;
898                                         } else if (isDirected()) {
899                                                 Vector3d v = getDirectedControlPointDirection();
900                                                 v.negate();
901                                                 return v;
902                                         } else if (isEnd()) {
903                                                 Vector3d v = new Vector3d();
904                                                 v.sub(getWorldPosition(),next.getWorldPosition());
905                                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
906                             v.normalize();
907                         else
908                             return null;
909                                                 return v;
910                                         } else if (isTurn() && asFixedAngle() && _getReversed()) {
911                                                 return getDirection(Direction.PREVIOUS);
912                                         }
913                                         throw new RuntimeException("Missing implementation " + this);
914                                 }
915                         }
916                 }
917         }
918
919         public void getInlineControlPointEnds(Tuple3d p1, Tuple3d p2) {
920                 assert (isInline());
921
922                 PipeControlPoint sub = isAxial() ? this : getDualSub();
923                 Vector3d pos = getWorldPosition(), pos2 = sub == this ? pos : sub.getWorldPosition();
924                 Vector3d dir = sub.getInlineDir();
925                 
926                 dir.scale(length * 0.5);
927                 p1.set(pos);
928                 p2.set(pos2);
929                 p1.sub(dir);
930                 p2.add(dir);
931         }
932
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();
936                 
937                 Vector3d dir1;
938                 Vector3d dir2;
939                 if (isInline()) {
940                         dir2 = getInlineDir();
941                         dir2.scale(length * 0.5);
942                         dir1 = new Vector3d(dir2);
943                         dir1.negate();
944                 } else {
945                         dir1 = getPathLegDirection(Direction.PREVIOUS);
946                         dir2 = sub.getPathLegDirection(Direction.NEXT);
947                         dir1.scale(length);
948                         dir2.scale(length);
949                 }
950                 p1.set(pos);
951                 p2.set(pos2);
952                 p1.add(dir1);
953                 p2.add(dir2);
954         }
955
956         /**
957          * Get both path leg directions, with (0,0,0) if no connection exists. The returned vectors are not normalized.
958          * 
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
961          */
962         public void getEndDirections(Tuple3d v1, Tuple3d v2) {
963                 PipeControlPoint sub = isAxial() ? this : getDualSub();
964                 
965                 Vector3d dir1 = getPathLegDirection(Direction.PREVIOUS);
966                 Vector3d dir2 = sub.getPathLegDirection(Direction.NEXT);
967                 
968                 if (dir1 != null)
969                         v1.set(dir1);
970                 else
971                         v1.set(0,0,0);
972                 
973                 if (dir2 != null)
974                         v2.set(dir2);
975                 else
976                         v2.set(0,0,0);
977         }
978
979         public void getInlineControlPointEnds(Tuple3d p1, Tuple3d p2, Vector3d dir) {
980                 assert (isInline());
981
982                 Vector3d pos = getWorldPosition();
983                 dir.set(getInlineDir());
984                 dir.normalize();
985                 dir.scale(length * 0.5);
986                 p1.set(pos);
987                 p2.set(pos);
988                 p1.sub(dir);
989                 p2.add(dir);
990         }
991
992         public void getInlineControlPointEnds(Tuple3d center, Tuple3d p1, Tuple3d p2, Vector3d dir) {
993                 assert (isInline());
994
995                 Vector3d pos = getWorldPosition();
996                 center.set(pos);
997                 dir.set(getInlineDir());
998                 dir.normalize();
999                 dir.scale(length * 0.5);
1000                 p1.set(pos);
1001                 p2.set(pos);
1002                 p1.sub(dir);
1003                 p2.add(dir);
1004         }
1005
1006         public Vector3d getInlineDir() {
1007                 Vector3d dir = getPathLegDirection(Direction.NEXT);
1008                 if (dir == null) {
1009                         dir = getPathLegDirection(Direction.PREVIOUS);
1010                         if (dir != null) {
1011                                 // Use reverse direction
1012                                 dir.scale(-1.0);
1013                         } else {
1014                                 // Control point is not connected at all, use current orientation
1015                                 dir = new Vector3d(1,0,0);
1016                                 MathTools.rotate(getWorldOrientation(), dir, dir);
1017                         }
1018                 }
1019                 return dir;
1020         }
1021
1022         public double getInlineLength() {
1023                 if (type == PointType.TURN)
1024                         return length;
1025                 else if (type == PointType.INLINE)
1026                         return length * 0.5;
1027                 return 0;
1028         }
1029
1030         /**
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.
1033          * 
1034          * @param type  A selector for the position to be returned
1035          * @return  The selected position
1036          */
1037         public Vector3d getRealPosition(PositionType type) {
1038                 Vector3d pos = getWorldPosition();
1039                 switch (type) {
1040                 case NEXT: {
1041                         double length = getInlineLength();
1042                         Vector3d dir;
1043                         if (isInline()) {
1044                                 dir = getInlineDir();
1045                         } else {
1046                                 dir = getPathLegDirection(Direction.NEXT);
1047                         }
1048                         dir.scale(length);
1049                         pos.add(dir);
1050                         break;
1051                 }
1052                 case PREVIOUS: {
1053                         double length = getInlineLength();
1054                         Vector3d dir;
1055                         if (isInline()) {
1056                                 dir = getInlineDir();
1057                                 dir.negate();
1058                         } else {
1059                                 dir = getPathLegDirection(Direction.PREVIOUS);
1060                         }
1061                         dir.scale(length);
1062                         pos.add(dir);
1063                         break;
1064                 }
1065                 case PORT:
1066                         // IEntity portDir = pcp.getSingleRelatedObject(ProcessResource.plant3Dresource.HasDirection);
1067                         // TODO : how we calculated needed space for a port; does it has an offset from control point's position or not?
1068                         break;
1069                 case SPLIT:
1070                         // do nothing
1071                         break;
1072
1073                 }
1074                 return pos;
1075         }
1076
1077         public void getInlineMovement(Tuple3d start, Tuple3d end) {
1078                 // FIXME : check type of neighbor components and allow movement on top of variable length components,
1079                 //         find proper range for movement (pcp's position is not)
1080                 PipeControlPoint p = previous.getPrevious();
1081                 PipeControlPoint n = next.getNext();
1082                 start.set(p.getWorldPosition());
1083                 end.set(n.getWorldPosition());
1084         }
1085
1086         public PipeControlPoint findNextEnd() {
1087                 ArrayList<PipeControlPoint> t = new ArrayList<PipeControlPoint>();
1088                 return findNextEnd( t);
1089         }
1090
1091         public PipeControlPoint findPreviousEnd() {
1092                 ArrayList<PipeControlPoint> t = new ArrayList<PipeControlPoint>();
1093                 return findPreviousEnd(t);
1094         }
1095
1096         public PipeControlPoint findNextEnd(List<PipeControlPoint> nextList) {
1097                 while (true) {
1098                         PipeControlPoint pcp = null;
1099                         PipeControlPoint p = null;
1100                         if (nextList.size() == 0)
1101                                 p = this;
1102
1103                         else
1104                                 p = nextList.get(nextList.size() - 1);
1105
1106                         pcp = p.getNext();
1107                         if (pcp == null) {
1108                                 pcp = p;
1109                                 if (nextList.size() > 0)
1110                                         nextList.remove(nextList.size() - 1);
1111                                 //              if (DEBUG) System.out.println(" " + pcp.getResource() + " not full");
1112                                 return pcp;
1113                                 //break;
1114                         }
1115                         if (pcp.isPathLegEnd()) {
1116                                 //if (DEBUG) System.out.println(" " + pcp.getResource());
1117                                 return pcp;
1118                         } else {
1119                                 nextList.add(pcp);
1120                                 // if (DEBUG) System.out.print(" " + pcp.getResource());
1121                         }
1122                 }
1123         }
1124
1125         public PipeControlPoint findPreviousEnd(List<PipeControlPoint> prevList) {
1126                 while (true) {
1127                         PipeControlPoint pcp = null;
1128                         PipeControlPoint p = null;
1129                         if (prevList.size() == 0)
1130                                 p = this;
1131
1132                         else
1133                                 p = prevList.get(prevList.size() - 1);
1134
1135                         pcp = p.getPrevious();
1136                         if (pcp == null) {
1137                                 pcp = p;
1138                                 if (prevList.size() > 0)
1139                                         prevList.remove(prevList.size() - 1);
1140                                 //                              if (DEBUG) System.out.println(" " + pcp.getResource() + " not full");
1141                                 return pcp;
1142                         }
1143                         if (pcp.isPathLegEnd()) {
1144                                 //                              if (DEBUG)      System.out.println(" " + pcp.getResource());
1145                                 return pcp;
1146                         } else {
1147                                 prevList.add(pcp);
1148                                 //                              if (DEBUG)System.out.print(" " + pcp.getResource());
1149                         }
1150                 }
1151         }
1152         
1153         public void _remove() {
1154             _remove(true);
1155         }
1156         
1157         
1158         public PipeControlPoint getDualSub() {
1159             if (isDualInline())
1160                 return getChildPoints().get(0);
1161             else
1162                 throw new IllegalStateException("Current control point is not dual inline");
1163         }
1164         
1165
1166         public void _remove(boolean renconnect) {
1167             if (disposed)
1168                 return;
1169                 
1170             if (DEBUG) System.out.println(this + " Remove " + renconnect);
1171
1172                 if (getParentPoint() != null) {
1173                     getParentPoint()._remove(renconnect);
1174                     return;
1175                 }
1176                 PipeRun pipeRun = getPipeRun();
1177 //              PipeRUn removal has been changed, so pipeRun may be null.
1178 //              if (pipeRun == null)
1179 //                      return;
1180
1181                 PipeControlPoint additionalRemove = null;
1182                 if (!PipingRules.isEnabled()) {
1183                     component = null;
1184                         setPrevious(null);
1185                         setNext(null);
1186                 } else {
1187
1188                         PipeControlPoint currentPrev = previous;
1189                         PipeControlPoint currentNext = next;
1190                         if (currentNext == null && currentPrev == null) {
1191                                 removeComponent();
1192                                 if (pipeRun != null) {
1193                                     pipeRun.remChild(this);
1194                                     checkRemove(pipeRun);
1195                                 }
1196                                 return;
1197                         }
1198                         if (currentNext != null && currentPrev != null) {
1199                                 boolean link = renconnect;
1200                                 if (currentNext.isBranchEnd()) {
1201                                         link = false;
1202                                         currentNext.remove();
1203                                         currentNext = null;
1204                                         setNext(null);
1205                                 }
1206                                 if (currentPrev.isBranchEnd()) {
1207                                         link = false;
1208                                         currentPrev.remove();
1209                                         currentPrev = null;
1210                                         setPrevious(null);
1211                                 }
1212                                 if (link) {
1213                                     if (currentPrev.isDirected() && currentNext.isDirected())
1214                                         link = false;
1215                                     else if (this.isDualSub()) {
1216                                         throw new RuntimeException("_remove() is called for parent point, somehow got to child point. " + this);
1217                                     }
1218                                 }
1219                                 if (currentNext == null) {
1220                                         // Nothing to do
1221                                 } else if (currentNext.isDualInline()) {
1222                                         PipeControlPoint sccp = currentNext;
1223                                         PipeControlPoint ocp = currentNext.getDualSub();
1224                                         if (ocp == null) {
1225                                                 throw new RuntimeException("Removing PipeControlPoint " + this+ " structure damaged, no offset control point");
1226                                         }
1227                                         if (link) {
1228                                                 sccp.setPrevious(currentPrev);
1229                                                 //ocp.setPrevious(currentPrev);
1230                                                 assert(ocp.getPrevious() == currentPrev);
1231                                         } else {
1232                                                 sccp.setPrevious(null);
1233                                                 //ocp.setPrevious(null);
1234                                                 assert(ocp.getPrevious() == null);
1235                                         }
1236                                         setNext(null);
1237                                 } else if (currentNext.isDualSub()) {
1238                                         throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, next control point is offset control point");
1239                                 } else if (currentNext.previous == this) {
1240                                         if (link) {
1241                                                 currentNext.setPrevious(currentPrev);
1242                                         } else {
1243                                                 currentNext.setPrevious(null);
1244                                         }
1245                                         setNext(null);
1246                                 } else if (isDualInline()) {
1247                                     if (currentNext.previous != getDualSub()) {
1248                                         throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1249                                     }
1250                                     if (link) {
1251                         currentNext.setPrevious(currentPrev);
1252                     } else {
1253                         currentNext.setPrevious(null);
1254                     }
1255                     setNext(null);
1256                                 } else {
1257                                         throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1258                                 }
1259                                 if (currentPrev == null) {
1260                                         // Nothing to do
1261                                 } else if (currentPrev.isDualInline()) {
1262                                         throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, previous control point is size change control point");
1263                                 } else if (currentPrev.isDualSub()) {
1264                                         PipeControlPoint ocp = currentPrev;
1265                                         PipeControlPoint sccp = currentPrev.getParentPoint();
1266                                         if (sccp == null)
1267                                                 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, no size change control point");
1268                                         if (link) {
1269                                                 //ocp.setNext(currentNext);
1270                                                 sccp.setNext(currentNext);
1271                                                 assert(ocp.getNext() == currentNext);
1272                                         } else {
1273                                                 //ocp.setNext(null);
1274                                                 sccp.setNext(null);
1275                                                 assert(ocp.getNext() == null);
1276                                         }
1277                                         setPrevious(null);
1278                                 } else if (currentPrev.next == this) {
1279                                         if (link)
1280                                                 currentPrev.setNext(currentNext);
1281                                         else
1282                                                 currentPrev.setNext(null);
1283
1284                                         setPrevious(null);
1285                                 } else {
1286                                         throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged");
1287                                 }
1288                                 if (link) {
1289                                         if (currentNext.isVariableLength() && currentPrev.isVariableLength()) {
1290                                                 // we have to join them into single variable length component.
1291                                                 additionalRemove = currentPrev;
1292                                                 // combine lengths and set the location of remaining control point to the center.
1293                                                 Point3d ps = new Point3d();
1294                                                 Point3d pe = new Point3d();
1295                                                 Point3d ns = new Point3d();
1296                                                 Point3d ne = new Point3d();
1297                                                 currentPrev.getInlineControlPointEnds(ps, pe);
1298                                                 currentNext.getInlineControlPointEnds(ns, ne);
1299                                                 double l = currentPrev.getLength() + currentNext.getLength();
1300                                                 Vector3d cp = new Vector3d();
1301                                                 cp.add(ps, ne);
1302                                                 cp.scale(0.5);
1303                                                 currentNext.setLength(l);
1304                                                 currentNext.setWorldPosition(cp);
1305                                         }
1306                                 } else {
1307                                         // FIXME : pipe run must be split into two parts, since the control point structure is no more continuous. 
1308                                 }
1309                         } else if (currentNext != null) {
1310                                 if (currentNext.isDualInline()) {
1311                                         PipeControlPoint sccp = currentNext;
1312                                         PipeControlPoint ocp = currentNext.getDualSub();
1313                                         if (ocp == null) {
1314                                                 throw new RuntimeException("Removing PipeControlPoint " + this+ " structure damaged, no offset control point");
1315                                         }
1316                                         sccp.setPrevious(null);
1317                                         assert(ocp.getPrevious() == null);
1318                                         //ocp.setPrevious(null);
1319                                 } else if (currentNext.isDualSub()) {
1320                                         throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, next control point is offset control point");
1321                                 } else if (currentNext.previous == this) {
1322                                     currentNext.setPrevious(null);
1323                                 }  else if (isDualInline()) {
1324                     if (currentNext.previous != getDualSub()) {
1325                         throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1326                     }
1327                     currentNext.setPrevious(null);
1328                                 } else {
1329                                         throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1330                                 }
1331                                 setNext(null);
1332                         } else {  //(previous != null)
1333                                 if(currentPrev.isDualInline()) {
1334                                         throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, previous control point is size change control point");
1335                                 } else if (currentPrev.isDualSub()) {
1336                                         PipeControlPoint ocp = currentPrev;
1337                                         PipeControlPoint sccp = currentPrev.getParentPoint();
1338                                         if (sccp == null) {
1339                                                 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, no size change control point");
1340                                         }
1341                                         sccp.setNext(null);
1342                                         assert(ocp.getNext() == null);
1343                                 } else if (currentPrev.next == this) {
1344                                     currentPrev.setNext(null);
1345                                 } else {
1346                                         throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1347                                 }
1348                                 setPrevious(null);
1349                         }
1350                         if (children.size() > 0 ) {
1351                                 removeSubPoints();
1352                         } else if (parent!= null) {
1353                                 removeParentPoint();
1354                         }
1355
1356                 }
1357
1358                 removeComponent();
1359                 if (pipeRun != null) {
1360                 pipeRun.remChild(this);
1361                 checkRemove(pipeRun);
1362                 if (PipingRules.isEnabled() && pipeRun.getParent() != null && pipeRun.getControlPoints().size() > 0)
1363                         PipingRules.validate(pipeRun);
1364                 }
1365                 if (additionalRemove != null)
1366                         additionalRemove.remove();
1367                 disposed = true;
1368         }
1369
1370         /**
1371          * Removes control point and attempts to reconnect next/prev
1372          * 
1373          * If this point is size change (PipeRuns are different on both sides), then reconnection cannot be made.
1374          */
1375         public void remove() {
1376                 PipeControlPoint currentPrev = previous;
1377                 PipeControlPoint currentNext = next;
1378                 _remove();
1379                 try {
1380                         if (currentNext != null)
1381                             if (!currentNext.checkRemove())
1382                                 PipingRules.requestUpdate(currentNext);
1383                         if (currentPrev != null)
1384                             if (!currentPrev.checkRemove())
1385                                 PipingRules.requestUpdate(currentPrev);
1386                 } catch (Exception e) {
1387                         e.printStackTrace();
1388                 }
1389         }
1390         
1391         
1392         /**
1393          * Removes control point without attempting to reconnect next/prev.
1394          * This usually leads to creation of another PipeRun for the control points after this point. 
1395          */
1396         public void removeAndSplit() {
1397         PipeControlPoint currentPrev = previous;
1398         PipeControlPoint currentNext = next;
1399         
1400         if (next != null && previous != null) {
1401             P3DRootNode root = (P3DRootNode)getPipelineComponent().getRootNode();
1402             PipeRun nextPipeRun = new PipeRun();
1403             nextPipeRun.setName(root.getUniqueName("PipeRun"));
1404             root.addChild(nextPipeRun);
1405             
1406             PipeRun previousRun = previous.getPipeRun();
1407             nextPipeRun.setPipeDiameter(previousRun.getPipeDiameter());
1408             nextPipeRun.setTurnRadiusArray(previousRun.getTurnRadiusArray());
1409             
1410             PipelineComponent n = next.getPipelineComponent();
1411             while (n != null) {
1412                 if (n.getPipeRun() != previousRun)
1413                     break;
1414                 if (! (n instanceof Nozzle)) {
1415                     n.deattach();
1416                     nextPipeRun.addChild(n);
1417                 } else
1418                     n.setPipeRun(nextPipeRun);
1419                 n = n.getNext();
1420             }
1421         }
1422         _remove(false);
1423         try {
1424             if (currentNext != null)
1425                 if (!currentNext.checkRemove())
1426                     PipingRules.requestUpdate(currentNext);
1427             if (currentPrev != null)
1428                 if (!currentPrev.checkRemove())
1429                     PipingRules.requestUpdate(currentPrev);
1430         } catch (Exception e) {
1431             e.printStackTrace();
1432         }
1433     }
1434         
1435         /**
1436          * This is called when adjacent control point is removed.
1437          * 
1438          * This call should remove the give point, if the point cannot exist alone. 
1439          * At the moment there is one such case: branch.
1440          * 
1441          * @return
1442          */
1443         protected boolean checkRemove() {
1444             if (getParentPoint() != null) {
1445                 return getParentPoint().checkRemove();
1446             } else {
1447                 if (getPipelineComponent() == null)
1448                     return true; // already removed
1449             if (getPipelineComponent().getType().equals("Plant3D.URIs.Builtin_BranchSplitComponent")) {
1450                 if (getChildPoints().get(0).getNext() == null && getChildPoints().get(0).getPrevious() == null) {
1451                         remove();
1452                         return true;
1453                 }
1454             }
1455             return checkRemove(getPipeRun());
1456             }
1457     }
1458
1459         private boolean checkRemove(PipeRun pipeRun) {
1460             if (pipeRun == null)
1461                 return false;
1462                 Collection<PipeControlPoint> points = pipeRun.getControlPoints();
1463                 if (points.size() == 0) {
1464                         pipeRun.remove();
1465                         return true;
1466                 } else if (points.size() == 1) {
1467                         PipeControlPoint pcp = points.iterator().next();
1468                         if (pcp.isDeletable() && pcp.getNext() == null && pcp.getPrevious() == null) {
1469                                 pcp._remove(); // This call will recursively call also this method...
1470                                 return true;
1471                         }
1472                 } else if (points.size() == 2) {
1473                     
1474                 }
1475                 return false;
1476         }
1477
1478         private void removeSubPoints() {
1479                 for (PipeControlPoint p : children) {
1480                         p.parent = null;
1481                         p.component = null;
1482                         //p._remove();
1483                         PipeControlPoint currentNext = p.getNext();
1484                         PipeControlPoint currentPrev = p.getPrevious();
1485                         p._setNext(null);
1486                         p._setPrevious(null);
1487                         PipeRun run = p.getPipeRun();
1488                         if (run != null) {
1489                             run.remChild(p);
1490                             checkRemove(run);
1491                         }
1492                         if (currentNext != null)
1493                 if (!currentNext.checkRemove())
1494                     PipingRules.requestUpdate(currentNext);
1495             if (currentPrev != null)
1496                 if (!currentPrev.checkRemove())
1497                     PipingRules.requestUpdate(currentPrev);
1498             
1499                 }
1500                 children.clear();
1501         }
1502
1503         private void removeParentPoint() {
1504                 throw new RuntimeException("Child points cannot be removed directly");
1505         }
1506         
1507         public boolean isRemoved() {
1508             return component == null;
1509         }
1510
1511         private void removeComponent() {
1512                 if (component == null)
1513                         return;
1514                 PipelineComponent next = component.getNext();
1515                 PipelineComponent prev = component.getPrevious();
1516                 PipelineComponent br0 = component.getBranch0();
1517                 component.setNext(null);
1518                 component.setPrevious(null);
1519                 component.setBranch0(null);
1520                 if (next != null) {
1521                         if (next.getNext() == component)
1522                                 next.setNext(null);
1523                         else if (next.getPrevious() == component)
1524                                 next.setPrevious(null);
1525                         else if (next.getBranch0() == component)
1526                                 next.setBranch0(null);
1527                 }
1528                 if (prev != null) {
1529                         if (prev.getNext() == component)
1530                                 prev.setNext(null);
1531                         else if (prev.getPrevious() == component)
1532                                 prev.setPrevious(null);
1533                         else if (prev.getBranch0() == component)
1534                                 prev.setBranch0(null);
1535                 }
1536                 if (br0 != null) {
1537                         if (br0.getNext() == component)
1538                                 br0.setNext(null);
1539                         else if (br0.getPrevious() == component)
1540                                 br0.setPrevious(null);
1541                         else if (br0.getBranch0() == component)
1542                                 br0.setBranch0(null);
1543                 }
1544                 PipelineComponent comp = component;
1545                 component = null;
1546
1547                 comp.remove();
1548         }
1549
1550         @Override
1551         public void setOrientation(Quat4d orientation) {
1552                 if (MathTools.equals(orientation, getOrientation()))
1553                         return;
1554                 if (getPipelineComponent() != null && (getPipelineComponent() instanceof Nozzle))
1555                     System.out.println();
1556                 super.setOrientation(orientation);
1557                 if (getParentPoint() == null && component != null)
1558                         component._setWorldOrientation(getWorldOrientation());
1559                 updateSubPoint();
1560         }
1561
1562         @Override
1563         public void setPosition(Vector3d position) {
1564                 if (MathTools.equals(position, getPosition()))
1565                         return;
1566                 if (Double.isNaN(position.x) || Double.isNaN(position.y) || Double.isNaN(position.z))
1567                         throw new IllegalArgumentException("NaN is not supported");
1568                 super.setPosition(position);
1569                 if (getParentPoint() == null && component != null)
1570                         component._setWorldPosition(getWorldPosition());
1571                 updateSubPoint();
1572         }
1573
1574         private void updateSubPoint() {
1575                 if (isOffset()) {
1576                         if (next == null && previous == null) {
1577                                 for (PipeControlPoint sub : getChildPoints()) {
1578                                         sub.setWorldPosition(getWorldPosition());
1579                                         sub.setWorldOrientation(getWorldOrientation());
1580                                 }
1581                                 return;
1582                         }
1583                         for (PipeControlPoint sub : getChildPoints()) {
1584                                 Vector3d wp = getWorldPosition();
1585                                 wp.add(getSizeChangeOffsetVector());
1586                                 sub.setWorldPosition(wp);
1587                                 sub.setWorldOrientation(getWorldOrientation());
1588                         }
1589                 } else {
1590                         for (PipeControlPoint sub : getChildPoints()) {
1591                                 sub.setWorldPosition(getWorldPosition());
1592                                 sub.setWorldOrientation(getWorldOrientation());
1593                         }
1594                 }
1595         }
1596
1597
1598         public void _setWorldPosition(Vector3d position) {
1599                 Vector3d localPos = getLocalPosition(position);
1600                 super.setPosition(localPos);
1601                 updateSubPoint();
1602         }
1603
1604         public void _setWorldOrientation(Quat4d orientation) {
1605                 Quat4d localOr = getLocalOrientation(orientation);
1606                 super.setOrientation(localOr);
1607                 updateSubPoint();
1608         }
1609
1610         @Override
1611         public String toString() {
1612                 return getClass().getName() + "@" + Integer.toHexString(hashCode());
1613         }
1614
1615 }