]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipeControlPoint.java
Merge "Refuse attempts to set turn radius index to a negative value."
[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                         if (dir.lengthSquared() > MathTools.NEAR_ZERO)
522                                 dir.normalize();
523                         return getControlPointOrientationQuat(dir, angle);
524                 } else {
525                         Vector3d dir = getPathLegDirection(Direction.PREVIOUS);
526                         dir.negate();
527                         if (dir.lengthSquared() > MathTools.NEAR_ZERO)
528                                 dir.normalize();
529                         return getControlPointOrientationQuat(dir, turnAxis, angle);
530                 }
531         }
532         
533         public Quat4d getControlPointOrientationQuat(Vector3d dir, double angle, boolean reversed) {
534             if (turnAxis == null) {
535             if (dir.lengthSquared() > MathTools.NEAR_ZERO)
536                 dir.normalize();
537             Quat4d q =  getControlPointOrientationQuat(dir, angle);
538             if (reversed) {
539                 Quat4d q2 = new Quat4d();
540                 q2.set(new AxisAngle4d(MathTools.Y_AXIS, Math.PI));
541                 q.mulInverse(q2);
542             }
543             return q;
544         } else {
545             if (dir.lengthSquared() > MathTools.NEAR_ZERO)
546                 dir.normalize();
547             return getControlPointOrientationQuat(dir, turnAxis, angle);
548         }
549         }
550
551         public Quat4d getControlPointOrientationQuat(double angle, boolean reversed) {
552
553                 if (turnAxis == null) {
554                         Vector3d dir = getPathLegDirection(Direction.NEXT);
555                         return getControlPointOrientationQuat(dir, angle, reversed);
556                 } else {
557                         Vector3d dir = getPathLegDirection(Direction.PREVIOUS);
558                         dir.negate();
559                         return getControlPointOrientationQuat(dir, angle, reversed);
560                 }
561         }
562
563
564
565         public static Quat4d getControlPointOrientationQuat(Vector3d dir, double angle) {
566                 if (dir.lengthSquared() < MathTools.NEAR_ZERO)
567                         return MathTools.getIdentityQuat();
568
569
570                 Vector3d up = new Vector3d(0.0, 1.0, 0.0);
571                 double a = up.angle(dir);
572                 if (a < 0.1 || (Math.PI - a) < 0.1) {
573                         up.set(1.0, 0.0, 0.0);
574                 }
575
576
577                 return getControlPointOrientationQuat(dir, up, angle);
578         }
579
580         public static Quat4d getControlPointOrientationQuat(Vector3d dir, Vector3d up,  double angle) {
581                 if (dir.lengthSquared() < MathTools.NEAR_ZERO)
582                         return MathTools.getIdentityQuat();
583
584                 final Vector3d front = new Vector3d(1.0,0.0,0.0);
585
586                 Quat4d q1 = new Quat4d();
587
588
589                 Vector3d right = new Vector3d();
590
591                 right.cross(dir, up);
592                 up.cross(right, dir);
593                 right.normalize();
594                 up.normalize();
595
596                 Matrix3d m = new Matrix3d();
597                 m.m00 = dir.x;
598                 m.m10 = dir.y;
599                 m.m20 = dir.z;
600                 m.m01 = up.x;
601                 m.m11 = up.y;
602                 m.m21 = up.z;
603                 m.m02 = right.x;
604                 m.m12 = right.y;
605                 m.m22 = right.z;
606
607                 //q1.set(m); MathTools contains more stable conversion
608                 MathTools.getQuat(m, q1);
609
610                 //                      if (DEBUG) System.out.println("PipingTools.getPipeComponentOrientationQuat() " + dir+ " " + up + " " + right);
611
612                 Quat4d q2 = new Quat4d();
613                 q2.set(new AxisAngle4d(front, angle));
614                 q1.mul(q2);
615                 return q1;
616         }
617
618         public void insert(PipeControlPoint previous, PipeControlPoint next) {
619                 // inserting an offsetpoint is error, 
620                 if (isDualSub())
621                         throw new RuntimeException("Dual sub points cannot be inserted.");
622                 // size change control point cannot be inserted this way, because it ends PipeRun
623                 if (isSizeChange())
624                         throw new RuntimeException("Size change points cannot be inserted.");
625                 PipeRun piperun = previous.getPipeRun();
626                 // and just to make sure that control point structure is not corrupted
627                 if (getPipeRun() != null) {
628                         if (piperun != getPipeRun() || piperun != next.getPipeRun())
629                                 throw new RuntimeException("All controls points must be located on the same pipe run");
630                 } else {
631                         piperun.addChild(this);
632                 }
633
634                 // insert new BranchControlPoint between straight's control points
635                 PipeControlPoint previousNext = previous.getNext();
636                 PipeControlPoint previousPrevious = previous.getPrevious();
637
638                 PipeControlPoint offsetCP = null;
639                 if (isOffset()) {
640                         offsetCP = getDualSub();
641                 }
642                 if (previousNext != null && previousNext == next) {
643                         if (previous.isDualInline()) {
644                                 throw new RuntimeException();
645                         }
646                         if (next.isDualSub()) {
647                                 throw new RuntimeException();
648                         }
649                         previous.setNext(this);
650                         this.setPrevious(previous);
651                         if (previous.isDualSub()) {
652                                 previous.getParentPoint().setNext(this);
653                         }
654                         this.setNext(next);
655
656                         if (offsetCP == null) {
657                                 next.setPrevious(this);
658                         } else {
659                                 next.setPrevious(offsetCP);
660                                 offsetCP.setNext(next);
661                                 offsetCP.setPrevious(previous);
662                         }
663
664                         if (next.isDualInline()) {
665                                 next.getDualSub().setPrevious(this);
666                         }
667                 } else if (previousPrevious != null && previousPrevious == next) {
668                         // control point were given in reverse order 
669                         if (next.isDualInline())
670                                 throw new RuntimeException();
671                         if (previous.isDualSub())
672                                 throw new RuntimeException();
673
674                         this.setNext(previous);
675                         if (offsetCP == null) {
676                                 previous.setNext(this);
677                         } else {
678                                 previous.setPrevious(offsetCP);
679                                 offsetCP.setNext(previous);
680                                 offsetCP.setPrevious(next);
681                         }
682                         if (previous.isDualInline()) {
683                                 previous.getDualSub().setPrevious(this);
684                         }
685                         this.setPrevious(next);
686                         next.setNext(this);
687                         if (next.isDualSub()) {
688                                 next.getParentPoint().setNext(this);
689                         }
690
691                 } else {
692                         throw new RuntimeException();
693                 }       
694
695                 PipingRules.validate(piperun);
696         }
697
698
699
700         public void insert(PipeControlPoint pcp, Direction direction) {
701                 if (isDualSub())
702                         throw new RuntimeException();
703                 if (direction == Direction.NEXT) {
704                         // if direction is next, user must have given OffsetPoint
705                         if (pcp.isDualInline())
706                                 throw new RuntimeException();
707                         // basic next/prev links
708                         pcp.setNext(this);
709                         this.setPrevious(pcp);
710                         // and last take care of sizechange / offset points
711                         if (pcp.isDualSub()) {
712                                 pcp.getParentPoint().setNext(this);
713                         }
714                         if (isDualInline()) {
715                             getDualSub().setPrevious(this);
716                         }
717                 } else {
718                         // if direction is previous, user must have given sizechange
719                         if (pcp.isDualSub())
720                                 throw new RuntimeException();
721                         // previous direction is more complicated, since if newCP is SizeChangeControlPoint,
722                         // we must link pcp to newCP's OffsetPoint
723                         PipeControlPoint nocp = null;
724                         if (isDualInline()) {
725                                 nocp = getDualSub();
726                                 nocp.setNext(pcp);
727                         }
728                         if (nocp == null) {
729                                 pcp.setPrevious(this);
730                         } else {
731                                 pcp.setPrevious(nocp);
732                         }
733                         this.setNext(pcp);
734                         if (pcp.isDualInline()) {
735                                 PipeControlPoint ocp = pcp.getDualSub();
736                                 if (nocp == null)
737                                         ocp.setPrevious(this);
738                                 else
739                                         ocp.setPrevious(nocp);
740                         }
741
742                 }
743                 PipingRules.validate(getPipeRun());
744         }
745
746         public Vector3d getDirectedControlPointDirection() {
747                 assert (isDirected());
748                 Vector3d dir = new Vector3d();
749                 MathTools.rotate(getWorldOrientation(), new Vector3d(1.0, 0.0, 0.0), dir);
750                 dir.normalize();
751                 return dir;
752         }
753         
754         /**
755          * Returns direction vector. 
756          * 
757          * For directed control points, always returns outwards pointing vector.
758          * 
759          * @param direction
760          * @return normalized vector, or null
761          */
762         public Vector3d getDirection(Direction direction) {
763         if (isDirected())
764             return getDirectedControlPointDirection();
765         if (isTurn() && asFixedAngle()) {
766             if (direction == Direction.NEXT) {
767                 if (previous != null) {
768                     PipeControlPoint pcp = this;
769                     Vector3d dir = new Vector3d();
770                     dir.sub(pcp.getWorldPosition(),previous.getWorldPosition());
771                     if (dir.lengthSquared() > MathTools.NEAR_ZERO)
772                         dir.normalize();
773                     else
774                         return null;
775                     Quat4d q = getControlPointOrientationQuat(dir, pcp.getRotationAngle() != null ? pcp.getRotationAngle() : 0.0);
776                     AxisAngle4d aa = new AxisAngle4d(MathTools.Y_AXIS,pcp.getTurnAngle() == null ? 0.0 : pcp.getTurnAngle());
777                     Quat4d q2 = MathTools.getQuat(aa);
778                     Vector3d v = new Vector3d(1.,0.,0.);
779                     Vector3d offset = new Vector3d();
780                     MathTools.rotate(q2, v, offset);
781                     MathTools.rotate(q, offset, dir);
782                     dir.normalize();
783                     return dir;
784                 }
785             } else {
786                 if (next != null) {
787                     PipeControlPoint pcp = this;
788                     Vector3d dir = new Vector3d();
789                     dir.sub(next.getWorldPosition(),pcp.getWorldPosition());
790                     if (dir.lengthSquared() > MathTools.NEAR_ZERO)
791                         dir.normalize();
792                     else
793                         return null;
794                     Quat4d q = getControlPointOrientationQuat(dir, pcp.getRotationAngle() != null ? pcp.getRotationAngle() : 0.0);
795                     AxisAngle4d aa = new AxisAngle4d(MathTools.Y_AXIS,pcp.getTurnAngle() == null ? 0.0 : pcp.getTurnAngle());
796                     Quat4d q2 = MathTools.getQuat(aa);
797                     Vector3d v = new Vector3d(1.,0.,0.);
798                     Vector3d offset = new Vector3d();
799                     MathTools.rotate(q2, v, offset);
800                     MathTools.rotate(q, offset, dir);
801                     dir.normalize();
802                     return dir;
803                 }
804             }
805         }
806         return null;
807     }
808
809         /**
810          * Returns path leg direction of the control point.
811          * 
812          * This method differs from getDirection by also returning inward pointing vectors for directed control points.
813          * 
814          * @param direction
815          * @return
816          */
817         public Vector3d getPathLegDirection(Direction direction) {
818                 if (direction == Direction.NEXT) {
819                         if (next != null) {
820                                 PipeControlPoint pcp = this;
821                                 if (pcp.isDualInline()) {
822                                         pcp = pcp.getDualSub();
823                                 }
824                                 Vector3d v = new Vector3d();
825                                 v.sub(next.getWorldPosition(),pcp.getWorldPosition());
826                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
827                     v.normalize();
828                 else
829                     return null;
830                                 return v;
831                         } else {
832                                 if (previous == null) {
833                                         if (!isDirected())
834                                                 throw new RuntimeException("Cannot calculate path leg direction for unconnected control point " + this);
835                                         return getDirectedControlPointDirection();
836
837                                 } else {
838                                         if (isVariableAngle() && !asFixedAngle())
839                                                 throw new RuntimeException("Cannot calculate path leg direction for unconnected variable angle control point " + this);
840                                         if (isInline()) {
841                                                 PipeControlPoint pcp = this;
842                                                 if (pcp.isDualSub()) {
843                                                         pcp = pcp.getParentPoint();
844                                                 }
845                                                 Vector3d v = new Vector3d();
846                                                 v.sub(pcp.getWorldPosition(),previous.getWorldPosition());
847                                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
848                                 v.normalize();
849                                                 else
850                                                     return null;
851                                                 return v;
852                                         } else if (isDirected()) {
853                                                 return getDirectedControlPointDirection();
854                                         } else if (isEnd()) {
855                                                 Vector3d v = new Vector3d();
856                                                 v.sub(getWorldPosition(),previous.getWorldPosition());
857                                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
858                             v.normalize();
859                         else
860                             return null;
861                                                 return v;
862                                         } else if (isTurn() && asFixedAngle() && !_getReversed()) {
863                                                 return getDirection(Direction.NEXT);
864                                         }
865                                         throw new RuntimeException("Missing implementation " + this);
866                                 }
867                         }
868                 } else {
869                         if (previous != null) {
870                                 PipeControlPoint pcp = this;
871                                 if (isDualSub()) 
872                                         pcp = getParentPoint();
873                                 Vector3d v = new Vector3d();
874                                 v.sub(previous.getWorldPosition(),pcp.getWorldPosition());
875                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
876                     v.normalize();
877                 else
878                     return null;
879                                 return v;
880                         } else {
881                                 if (next == null)  {
882                                         if (!isDirected())
883                                                 throw new RuntimeException("Cannot calculate path leg direction for unconnected control point " + this);
884                                         Vector3d v = getDirectedControlPointDirection();
885                                         v.negate();
886                                         return v;
887                                 } else {
888                                         if (isVariableAngle() && !asFixedAngle())
889                                                 throw new RuntimeException("Cannot calculate path leg direction for unconnected variable angle control point " + this);
890                                         if (isInline()) {
891                                                 PipeControlPoint pcp = this;
892                                                 if (pcp.isDualInline()) {
893                                                         pcp = pcp.getDualSub();
894                                                 }
895                                                 Vector3d v = new Vector3d();
896                                                 v.sub(pcp.getWorldPosition(),next.getWorldPosition());
897                                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
898                             v.normalize();
899                         else
900                             return null;
901                                                 return v;
902                                         } else if (isDirected()) {
903                                                 Vector3d v = getDirectedControlPointDirection();
904                                                 v.negate();
905                                                 return v;
906                                         } else if (isEnd()) {
907                                                 Vector3d v = new Vector3d();
908                                                 v.sub(getWorldPosition(),next.getWorldPosition());
909                                                 if (v.lengthSquared() > MathTools.NEAR_ZERO)
910                             v.normalize();
911                         else
912                             return null;
913                                                 return v;
914                                         } else if (isTurn() && asFixedAngle() && _getReversed()) {
915                                                 return getDirection(Direction.PREVIOUS);
916                                         }
917                                         throw new RuntimeException("Missing implementation " + this);
918                                 }
919                         }
920                 }
921         }
922
923         public void getInlineControlPointEnds(Tuple3d p1, Tuple3d p2) {
924                 assert (isInline());
925
926                 PipeControlPoint sub = isAxial() ? this : getDualSub();
927                 Vector3d pos = getWorldPosition(), pos2 = sub == this ? pos : sub.getWorldPosition();
928                 Vector3d dir = sub.getPathLegDirection(Direction.NEXT);
929                 
930                 dir.normalize();
931                 dir.scale(length * 0.5);
932                 p1.set(pos);
933                 p2.set(pos2);
934                 p1.sub(dir);
935                 p2.add(dir);
936         }
937
938         public void getControlPointEnds(Tuple3d p1, Tuple3d p2) {
939                 PipeControlPoint sub = isAxial() || isDirected() || isTurn() ? this : getChildPoints().get(0);
940                 Vector3d pos = getWorldPosition(), pos2 = sub == this ? pos : sub.getWorldPosition();
941                 
942                 Vector3d dir1 = getPathLegDirection(Direction.PREVIOUS);
943                 Vector3d dir2 = sub.getPathLegDirection(Direction.NEXT);
944                 if (isInline()) {
945                         dir1.scale(length * 0.5);
946                         dir2.scale(length * 0.5);
947                 } else {
948                         dir1.scale(length);
949                         dir2.scale(length);
950                 }
951                 p1.set(pos);
952                 p2.set(pos2);
953                 p1.add(dir1);
954                 p2.add(dir2);
955         }
956
957         public void getEndDirections(Tuple3d v1, Tuple3d v2) {
958                 PipeControlPoint sub = isAxial() ? this : getDualSub();
959                 
960                 Vector3d dir1 = getPathLegDirection(Direction.PREVIOUS);
961                 Vector3d dir2 = sub.getPathLegDirection(Direction.NEXT);
962                 v1.set(dir1);
963                 v2.set(dir2);
964         }
965
966         public void getInlineControlPointEnds(Tuple3d p1, Tuple3d p2, Vector3d dir) {
967                 assert (isInline());
968
969                 Vector3d pos = getWorldPosition();
970                 dir.set(getPathLegDirection(Direction.NEXT));
971                 dir.normalize();
972                 dir.scale(length * 0.5);
973                 p1.set(pos);
974                 p2.set(pos);
975                 p1.sub(dir);
976                 p2.add(dir);
977         }
978
979         public void getInlineControlPointEnds(Tuple3d center, Tuple3d p1, Tuple3d p2, Vector3d dir) {
980                 assert (isInline());
981
982                 Vector3d pos = getWorldPosition();
983                 center.set(pos);
984                 dir.set(getPathLegDirection(Direction.NEXT));
985                 dir.normalize();
986                 dir.scale(length * 0.5);
987                 p1.set(pos);
988                 p2.set(pos);
989                 p1.sub(dir);
990                 p2.add(dir);
991         }
992
993         public double getInlineLength() {
994                 if (type == PointType.TURN)
995                         return length;
996                 else if (type == PointType.INLINE)
997                         return length * 0.5;
998                 return 0;
999         }
1000
1001         public Vector3d getRealPosition(PositionType type) {
1002                 Vector3d pos = getWorldPosition();
1003                 switch (type) {
1004                 case NEXT: {
1005                         Vector3d dir = getPathLegDirection(Direction.NEXT);
1006                         double length = getInlineLength();
1007                         dir.normalize();
1008                         dir.scale(length);
1009                         pos.add(dir);
1010                         break;
1011                 }
1012                 case PREVIOUS: {
1013                         Vector3d dir = getPathLegDirection(Direction.PREVIOUS);
1014                         double length = getInlineLength();
1015                         dir.normalize();
1016                         dir.scale(length);
1017                         pos.add(dir);
1018                         break;
1019                 }
1020                 case PORT:
1021                         // IEntity portDir = pcp.getSingleRelatedObject(ProcessResource.plant3Dresource.HasDirection);
1022                         // TODO : how we calculated needed space for a port; does it has an offset from control point's position or not?
1023                         break;
1024                 case SPLIT:
1025                         // do nothing
1026                         break;
1027
1028                 }
1029                 return pos;
1030         }
1031
1032         public void getInlineMovement(Tuple3d start, Tuple3d end) {
1033                 // FIXME : check type of neighbor components and allow movement on top of variable length components,
1034                 //         find proper range for movement (pcp's position is not)
1035                 PipeControlPoint p = previous.getPrevious();
1036                 PipeControlPoint n = next.getNext();
1037                 start.set(p.getWorldPosition());
1038                 end.set(n.getWorldPosition());
1039         }
1040
1041         public PipeControlPoint findNextEnd() {
1042                 ArrayList<PipeControlPoint> t = new ArrayList<PipeControlPoint>();
1043                 return findNextEnd( t);
1044         }
1045
1046         public PipeControlPoint findPreviousEnd() {
1047                 ArrayList<PipeControlPoint> t = new ArrayList<PipeControlPoint>();
1048                 return findPreviousEnd(t);
1049         }
1050
1051         public PipeControlPoint findNextEnd(List<PipeControlPoint> nextList) {
1052                 while (true) {
1053                         PipeControlPoint pcp = null;
1054                         PipeControlPoint p = null;
1055                         if (nextList.size() == 0)
1056                                 p = this;
1057
1058                         else
1059                                 p = nextList.get(nextList.size() - 1);
1060
1061                         pcp = p.getNext();
1062                         if (pcp == null) {
1063                                 pcp = p;
1064                                 if (nextList.size() > 0)
1065                                         nextList.remove(nextList.size() - 1);
1066                                 //              if (DEBUG) System.out.println(" " + pcp.getResource() + " not full");
1067                                 return pcp;
1068                                 //break;
1069                         }
1070                         if (pcp.isPathLegEnd()) {
1071                                 //if (DEBUG) System.out.println(" " + pcp.getResource());
1072                                 return pcp;
1073                         } else {
1074                                 nextList.add(pcp);
1075                                 // if (DEBUG) System.out.print(" " + pcp.getResource());
1076                         }
1077                 }
1078         }
1079
1080         public PipeControlPoint findPreviousEnd(List<PipeControlPoint> prevList) {
1081                 while (true) {
1082                         PipeControlPoint pcp = null;
1083                         PipeControlPoint p = null;
1084                         if (prevList.size() == 0)
1085                                 p = this;
1086
1087                         else
1088                                 p = prevList.get(prevList.size() - 1);
1089
1090                         pcp = p.getPrevious();
1091                         if (pcp == null) {
1092                                 pcp = p;
1093                                 if (prevList.size() > 0)
1094                                         prevList.remove(prevList.size() - 1);
1095                                 //                              if (DEBUG) System.out.println(" " + pcp.getResource() + " not full");
1096                                 return pcp;
1097                         }
1098                         if (pcp.isPathLegEnd()) {
1099                                 //                              if (DEBUG)      System.out.println(" " + pcp.getResource());
1100                                 return pcp;
1101                         } else {
1102                                 prevList.add(pcp);
1103                                 //                              if (DEBUG)System.out.print(" " + pcp.getResource());
1104                         }
1105                 }
1106         }
1107         
1108         public void _remove() {
1109             _remove(true);
1110         }
1111         
1112         
1113         public PipeControlPoint getDualSub() {
1114             if (isDualInline())
1115                 return getChildPoints().get(0);
1116             else
1117                 throw new IllegalStateException("Current control point is not dual inline");
1118         }
1119         
1120
1121         public void _remove(boolean renconnect) {
1122             if (disposed)
1123                 return;
1124                 
1125             if (DEBUG) System.out.println(this + " Remove " + renconnect);
1126
1127                 if (getParentPoint() != null) {
1128                     getParentPoint()._remove(renconnect);
1129                     return;
1130                 }
1131                 PipeRun pipeRun = getPipeRun();
1132 //              PipeRUn removal has been changed, so pipeRun may be null.
1133 //              if (pipeRun == null)
1134 //                      return;
1135
1136                 PipeControlPoint additionalRemove = null;
1137                 if (!PipingRules.isEnabled()) {
1138                     component = null;
1139                         setPrevious(null);
1140                         setNext(null);
1141                 } else {
1142
1143                         PipeControlPoint currentPrev = previous;
1144                         PipeControlPoint currentNext = next;
1145                         if (currentNext == null && currentPrev == null) {
1146                                 removeComponent();
1147                                 if (pipeRun != null) {
1148                                     pipeRun.remChild(this);
1149                                     checkRemove(pipeRun);
1150                                 }
1151                                 return;
1152                         }
1153                         if (currentNext != null && currentPrev != null) {
1154                                 boolean link = renconnect;
1155                                 if (currentNext.isBranchEnd()) {
1156                                         link = false;
1157                                         currentNext.remove();
1158                                         currentNext = null;
1159                                         setNext(null);
1160                                 }
1161                                 if (currentPrev.isBranchEnd()) {
1162                                         link = false;
1163                                         currentPrev.remove();
1164                                         currentPrev = null;
1165                                         setPrevious(null);
1166                                 }
1167                                 if (link) {
1168                                     if (currentPrev.isDirected() && currentNext.isDirected())
1169                                         link = false;
1170                                     else if (this.isDualInline()) {
1171                                         link = false;
1172                                     } else if (this.isDualSub()) {
1173                                         throw new RuntimeException("_remove() is called for parent point, somehow got to child point. " + this);
1174                                     }
1175                                 }
1176                                 if (currentNext == null) {
1177                                         // Nothing to do
1178                                 } else if (currentNext.isDualInline()) {
1179                                         PipeControlPoint sccp = currentNext;
1180                                         PipeControlPoint ocp = currentNext.getDualSub();
1181                                         if (ocp == null) {
1182                                                 throw new RuntimeException("Removing PipeControlPoint " + this+ " structure damaged, no offset control point");
1183                                         }
1184                                         if (link) {
1185                                                 sccp.setPrevious(currentPrev);
1186                                                 //ocp.setPrevious(currentPrev);
1187                                                 assert(ocp.getPrevious() == currentPrev);
1188                                         } else {
1189                                                 sccp.setPrevious(null);
1190                                                 //ocp.setPrevious(null);
1191                                                 assert(ocp.getPrevious() == null);
1192                                         }
1193                                         setNext(null);
1194                                 } else if (currentNext.isDualSub()) {
1195                                         throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, next control point is offset control point");
1196                                 } else if (currentNext.previous == this) {
1197                                         if (link) {
1198                                                 currentNext.setPrevious(currentPrev);
1199                                         } else {
1200                                                 currentNext.setPrevious(null);
1201                                         }
1202                                         setNext(null);
1203                                 } else if (isDualInline()) {
1204                                     if (currentNext.previous != getDualSub()) {
1205                                         throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1206                                     }
1207                                     if (link) {
1208                         currentNext.setPrevious(currentPrev);
1209                     } else {
1210                         currentNext.setPrevious(null);
1211                     }
1212                     setNext(null);
1213                                 } else {
1214                                         throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1215                                 }
1216                                 if (currentPrev == null) {
1217                                         // Nothing to do
1218                                 } else if (currentPrev.isDualInline()) {
1219                                         throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, previous control point is size change control point");
1220                                 } else if (currentPrev.isDualSub()) {
1221                                         PipeControlPoint ocp = currentPrev;
1222                                         PipeControlPoint sccp = currentPrev.getParentPoint();
1223                                         if (sccp == null)
1224                                                 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, no size change control point");
1225                                         if (link) {
1226                                                 //ocp.setNext(currentNext);
1227                                                 sccp.setNext(currentNext);
1228                                                 assert(ocp.getNext() == currentNext);
1229                                         } else {
1230                                                 //ocp.setNext(null);
1231                                                 sccp.setNext(null);
1232                                                 assert(ocp.getNext() == null);
1233                                         }
1234                                         setPrevious(null);
1235                                 } else if (currentPrev.next == this) {
1236                                         if (link)
1237                                                 currentPrev.setNext(currentNext);
1238                                         else
1239                                                 currentPrev.setNext(null);
1240
1241                                         setPrevious(null);
1242                                 } else {
1243                                         throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged");
1244                                 }
1245                                 if (link) {
1246                                         if (currentNext.isVariableLength() && currentPrev.isVariableLength()) {
1247                                                 // we have to join them into single variable length component.
1248                                                 additionalRemove = currentPrev;
1249                                                 // combine lengths and set the location of remaining control point to the center.
1250                                                 Point3d ps = new Point3d();
1251                                                 Point3d pe = new Point3d();
1252                                                 Point3d ns = new Point3d();
1253                                                 Point3d ne = new Point3d();
1254                                                 currentPrev.getInlineControlPointEnds(ps, pe);
1255                                                 currentNext.getInlineControlPointEnds(ns, ne);
1256                                                 double l = currentPrev.getLength() + currentNext.getLength();
1257                                                 Vector3d cp = new Vector3d();
1258                                                 cp.add(ps, ne);
1259                                                 cp.scale(0.5);
1260                                                 currentNext.setLength(l);
1261                                                 currentNext.setWorldPosition(cp);
1262                                         }
1263                                 } else {
1264                                         // FIXME : pipe run must be split into two parts, since the control point structure is no more continuous. 
1265                                 }
1266                         } else if (currentNext != null) {
1267                                 if (currentNext.isDualInline()) {
1268                                         PipeControlPoint sccp = currentNext;
1269                                         PipeControlPoint ocp = currentNext.getDualSub();
1270                                         if (ocp == null) {
1271                                                 throw new RuntimeException("Removing PipeControlPoint " + this+ " structure damaged, no offset control point");
1272                                         }
1273                                         sccp.setPrevious(null);
1274                                         assert(ocp.getPrevious() == null);
1275                                         //ocp.setPrevious(null);
1276                                 } else if (currentNext.isDualSub()) {
1277                                         throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, next control point is offset control point");
1278                                 } else if (currentNext.previous == this) {
1279                                     currentNext.setPrevious(null);
1280                                 }  else if (isDualInline()) {
1281                     if (currentNext.previous != getDualSub()) {
1282                         throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1283                     }
1284                     currentNext.setPrevious(null);
1285                                 } else {
1286                                         throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1287                                 }
1288                                 setNext(null);
1289                         } else {  //(previous != null)
1290                                 if(currentPrev.isDualInline()) {
1291                                         throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, previous control point is size change control point");
1292                                 } else if (currentPrev.isDualSub()) {
1293                                         PipeControlPoint ocp = currentPrev;
1294                                         PipeControlPoint sccp = currentPrev.getParentPoint();
1295                                         if (sccp == null) {
1296                                                 throw new RuntimeException("Removing PipeControlPoint " + this + " structure damaged, no size change control point");
1297                                         }
1298                                         sccp.setNext(null);
1299                                         assert(ocp.getNext() == null);
1300                                 } else if (currentPrev.next == this) {
1301                                     currentPrev.setNext(null);
1302                                 } else {
1303                                         throw new RuntimeException("Removing PipeControlPoint "+ this+ " structure damaged");
1304                                 }
1305                                 setPrevious(null);
1306                         }
1307                         if (children.size() > 0 ) {
1308                                 removeSubPoints();
1309                         } else if (parent!= null) {
1310                                 removeParentPoint();
1311                         }
1312
1313                 }
1314
1315                 removeComponent();
1316                 if (pipeRun != null) {
1317                 pipeRun.remChild(this);
1318                 checkRemove(pipeRun);
1319                 if (PipingRules.isEnabled() && pipeRun.getParent() != null && pipeRun.getControlPoints().size() > 0)
1320                         PipingRules.validate(pipeRun);
1321                 }
1322                 if (additionalRemove != null)
1323                         additionalRemove.remove();
1324                 disposed = true;
1325         }
1326
1327         /**
1328          * Removes control point and attempts to reconnect next/prev
1329          * 
1330          * If this point is size change (PipeRuns are different on both sides), then reconnection cannot be made.
1331          */
1332         public void remove() {
1333                 PipeControlPoint currentPrev = previous;
1334                 PipeControlPoint currentNext = next;
1335                 _remove();
1336                 try {
1337                         if (currentNext != null)
1338                             if (!currentNext.checkRemove())
1339                                 PipingRules.requestUpdate(currentNext);
1340                         if (currentPrev != null)
1341                             if (!currentPrev.checkRemove())
1342                                 PipingRules.requestUpdate(currentPrev);
1343                 } catch (Exception e) {
1344                         e.printStackTrace();
1345                 }
1346         }
1347         
1348         
1349         /**
1350          * Removes control point without attempting to reconnect next/prev.
1351          * This usually leads to creation of another PipeRun for the control points after this point. 
1352          */
1353         public void removeAndSplit() {
1354         PipeControlPoint currentPrev = previous;
1355         PipeControlPoint currentNext = next;
1356         
1357         if (next != null && previous != null) {
1358             P3DRootNode root = (P3DRootNode)getPipelineComponent().getRootNode();
1359             PipeRun nextPipeRun = new PipeRun();
1360             nextPipeRun.setName(root.getUniqueName("PipeRun"));
1361             root.addChild(nextPipeRun);
1362             
1363             PipeRun previousRun = previous.getPipeRun();
1364             nextPipeRun.setPipeDiameter(previousRun.getPipeDiameter());
1365             nextPipeRun.setTurnRadiusArray(previousRun.getTurnRadiusArray());
1366             
1367             PipelineComponent n = next.getPipelineComponent();
1368             while (n != null) {
1369                 if (n.getPipeRun() != previousRun)
1370                     break;
1371                 if (! (n instanceof Nozzle)) {
1372                     n.deattach();
1373                     nextPipeRun.addChild(n);
1374                 } else
1375                     n.setPipeRun(nextPipeRun);
1376                 n = n.getNext();
1377             }
1378         }
1379         _remove(false);
1380         try {
1381             if (currentNext != null)
1382                 if (!currentNext.checkRemove())
1383                     PipingRules.requestUpdate(currentNext);
1384             if (currentPrev != null)
1385                 if (!currentPrev.checkRemove())
1386                     PipingRules.requestUpdate(currentPrev);
1387         } catch (Exception e) {
1388             e.printStackTrace();
1389         }
1390     }
1391         
1392         /**
1393          * This is called when adjacent control point is removed.
1394          * 
1395          * This call should remove the give point, if the point cannot exist alone. 
1396          * At the moment there is one such case: branch.
1397          * 
1398          * @return
1399          */
1400         protected boolean checkRemove() {
1401             if (getParentPoint() != null) {
1402                 return getParentPoint().checkRemove();
1403             } else {
1404                 if (getPipelineComponent() == null)
1405                     return true; // already removed
1406             if (getPipelineComponent().getType().equals("Plant3D.URIs.Builtin_BranchSplitComponent")) {
1407                 if (getChildPoints().get(0).getNext() == null && getChildPoints().get(0).getPrevious() == null) {
1408                         remove();
1409                         return true;
1410                 }
1411             }
1412             return checkRemove(getPipeRun());
1413             }
1414     }
1415
1416         private boolean checkRemove(PipeRun pipeRun) {
1417             if (pipeRun == null)
1418                 return false;
1419                 Collection<PipeControlPoint> points = pipeRun.getControlPoints();
1420                 if (points.size() == 0) {
1421                         pipeRun.remove();
1422                         return true;
1423                 } else if (points.size() == 1) {
1424                         PipeControlPoint pcp = points.iterator().next();
1425                         if (pcp.isDeletable() && pcp.getNext() == null && pcp.getPrevious() == null) {
1426                                 pcp._remove(); // This call will recursively call also this method...
1427                                 return true;
1428                         }
1429                 } else if (points.size() == 2) {
1430                     
1431                 }
1432                 return false;
1433         }
1434
1435         private void removeSubPoints() {
1436                 for (PipeControlPoint p : children) {
1437                         p.parent = null;
1438                         p.component = null;
1439                         //p._remove();
1440                         PipeControlPoint currentNext = p.getNext();
1441                         PipeControlPoint currentPrev = p.getPrevious();
1442                         p._setNext(null);
1443                         p._setPrevious(null);
1444                         PipeRun run = p.getPipeRun();
1445                         if (run != null) {
1446                             run.remChild(p);
1447                             checkRemove(run);
1448                         }
1449                         if (currentNext != null)
1450                 if (!currentNext.checkRemove())
1451                     PipingRules.requestUpdate(currentNext);
1452             if (currentPrev != null)
1453                 if (!currentPrev.checkRemove())
1454                     PipingRules.requestUpdate(currentPrev);
1455             
1456                 }
1457                 children.clear();
1458         }
1459
1460         private void removeParentPoint() {
1461                 throw new RuntimeException("Child points cannot be removed directly");
1462         }
1463         
1464         public boolean isRemoved() {
1465             return component == null;
1466         }
1467
1468         private void removeComponent() {
1469                 if (component == null)
1470                         return;
1471                 PipelineComponent next = component.getNext();
1472                 PipelineComponent prev = component.getPrevious();
1473                 PipelineComponent br0 = component.getBranch0();
1474                 component.setNext(null);
1475                 component.setPrevious(null);
1476                 component.setBranch0(null);
1477                 if (next != null) {
1478                         if (next.getNext() == component)
1479                                 next.setNext(null);
1480                         else if (next.getPrevious() == component)
1481                                 next.setPrevious(null);
1482                         else if (next.getBranch0() == component)
1483                                 next.setBranch0(null);
1484                 }
1485                 if (prev != null) {
1486                         if (prev.getNext() == component)
1487                                 prev.setNext(null);
1488                         else if (prev.getPrevious() == component)
1489                                 prev.setPrevious(null);
1490                         else if (prev.getBranch0() == component)
1491                                 prev.setBranch0(null);
1492                 }
1493                 if (br0 != null) {
1494                         if (br0.getNext() == component)
1495                                 br0.setNext(null);
1496                         else if (br0.getPrevious() == component)
1497                                 br0.setPrevious(null);
1498                         else if (br0.getBranch0() == component)
1499                                 br0.setBranch0(null);
1500                 }
1501                 PipelineComponent comp = component;
1502                 component = null;
1503
1504                 comp.remove();
1505         }
1506
1507         @Override
1508         public void setOrientation(Quat4d orientation) {
1509                 if (MathTools.equals(orientation, getOrientation()))
1510                         return;
1511                 if (getPipelineComponent() != null && (getPipelineComponent() instanceof Nozzle))
1512                     System.out.println();
1513                 super.setOrientation(orientation);
1514                 if (getParentPoint() == null && component != null)
1515                         component._setWorldOrientation(getWorldOrientation());
1516                 updateSubPoint();
1517         }
1518
1519         @Override
1520         public void setPosition(Vector3d position) {
1521                 if (MathTools.equals(position, getPosition()))
1522                         return;
1523                 if (Double.isNaN(position.x) || Double.isNaN(position.y) || Double.isNaN(position.z))
1524                         throw new IllegalArgumentException("NaN is not supported");
1525                 super.setPosition(position);
1526                 if (getParentPoint() == null && component != null)
1527                         component._setWorldPosition(getWorldPosition());
1528                 updateSubPoint();
1529         }
1530
1531         private void updateSubPoint() {
1532                 if (isOffset()) {
1533                         if (next == null && previous == null) {
1534                                 for (PipeControlPoint sub : getChildPoints()) {
1535                                         sub.setWorldPosition(getWorldPosition());
1536                                         sub.setWorldOrientation(getWorldOrientation());
1537                                 }
1538                                 return;
1539                         }
1540                         for (PipeControlPoint sub : getChildPoints()) {
1541                                 Vector3d wp = getWorldPosition();
1542                                 wp.add(getSizeChangeOffsetVector());
1543                                 sub.setWorldPosition(wp);
1544                                 sub.setWorldOrientation(getWorldOrientation());
1545                         }
1546                 } else {
1547                         for (PipeControlPoint sub : getChildPoints()) {
1548                                 sub.setWorldPosition(getWorldPosition());
1549                                 sub.setWorldOrientation(getWorldOrientation());
1550                         }
1551                 }
1552         }
1553
1554
1555         public void _setWorldPosition(Vector3d position) {
1556                 Vector3d localPos = getLocalPosition(position);
1557                 super.setPosition(localPos);
1558                 updateSubPoint();
1559         }
1560
1561         public void _setWorldOrientation(Quat4d orientation) {
1562                 Quat4d localOr = getLocalOrientation(orientation);
1563                 super.setOrientation(localOr);
1564                 updateSubPoint();
1565         }
1566
1567         @Override
1568         public String toString() {
1569                 return getClass().getName() + "@" + Integer.toHexString(hashCode());
1570         }
1571
1572 }