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