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