]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipeControlPoint.java
Very crude API for creating pipeline components
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / controlpoint / PipeControlPoint.java
index 22c58ddb160be1a1f43303b34e63d1dd3614bc17..8e3e6bbdc1c926d28270e3900f7d8648a7f01cf3 100644 (file)
@@ -7,6 +7,7 @@ import java.util.List;
 
 import javax.vecmath.AxisAngle4d;
 import javax.vecmath.Matrix3d;
+import javax.vecmath.Point3d;
 import javax.vecmath.Quat4d;
 import javax.vecmath.Tuple3d;
 import javax.vecmath.Vector3d;
@@ -22,14 +23,16 @@ import vtk.vtkRenderer;
 
 
 public class PipeControlPoint extends G3DNode implements IP3DNode {
+    
+    private static boolean DEBUG = false;
        
-       public enum Type{INLINE,TURN,END};
+       public enum PointType{INLINE,TURN,END};
        public enum Direction{NEXT,PREVIOUS};
        public enum PositionType {SPLIT,NEXT,PREVIOUS,PORT}
        
        private PipelineComponent component;
        
-       private Type type;
+       private PointType type;
        private boolean fixed = true;
        private boolean rotate = false;
        private boolean reverse = false;
@@ -66,11 +69,11 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
                return component;
        }
        
-       public Type getType() {
+       public PointType getType() {
                return type;
        }
        
-       public void setType(Type type) {
+       public void setType(PointType type) {
                this.type = type;
        }
        
@@ -116,19 +119,19 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
        }
        
        public boolean isPathLegEnd() {
-               return type != Type.INLINE;
+               return type != PointType.INLINE;
        }
        
        public boolean isEnd() {
-               return type == Type.END;
+               return type == PointType.END;
        }
        
        public boolean isTurn() {
-               return type == Type.TURN;
+               return type == PointType.TURN;
        }
        
        public boolean isInline() {
-               return type == Type.INLINE;
+               return type == PointType.INLINE;
        }
        
        public boolean isDirected() {
@@ -186,8 +189,9 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
        public void setNext(PipeControlPoint next) {
                if (isEnd() && previous != null && next != null)
                        throw new RuntimeException("End control points are allowed to have only one connection");
-//             if (next != null && getPipeRun() == null)
-//                     throw new RuntimeException("Cannot connect control point befor piperun has been set");
+               if (this.next == next)
+                   return;
+               if (DEBUG) System.out.println(this + " next " + next);
                this.next = next;
                if (component != null) {
                        if (parent == null || sub)
@@ -202,8 +206,9 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
        public void setPrevious(PipeControlPoint previous) {
                if (isEnd() && next != null && previous != null)
                        throw new RuntimeException("End control points are allowed to have only one connection");
-//             if (previous != null && getPipeRun() == null)
-//                     throw new RuntimeException("Cannot connect control point befor piperun has been set");
+               if (this.previous == previous)
+                   return;
+               if (DEBUG) System.out.println(this + " previous " + previous);
                this.previous = previous;
                if (component != null) {
                        if (parent == null || sub)
@@ -225,14 +230,8 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
        public PipeControlPoint getParentPoint() {
                return parent;
        }
-       
-       
-       
 
        
-       
-       
-       
        private double length;
        private Double turnAngle;
        private Vector3d turnAxis;
@@ -290,7 +289,7 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
     }
        
        public void setTurnAngle(Double turnAngle) {
-               if (Double.isInfinite(turnAngle) || Double.isNaN(turnAngle)) {
+               if (turnAngle == null || Double.isInfinite(turnAngle) || Double.isNaN(turnAngle)) {
                        return;
                }
                if (this.turnAngle != null && Math.abs(this.turnAngle-turnAngle) < MathTools.NEAR_ZERO)
@@ -300,7 +299,9 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
        }
        
        public void setTurnAxis(Vector3d turnAxis) {
-               this.turnAxis = turnAxis;
+           if (this.turnAxis != null && MathTools.equals(turnAxis, this.turnAxis))
+            return;
+           this.turnAxis = turnAxis;
                firePropertyChanged("turnAxis");
        }
        
@@ -483,6 +484,8 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
                     dir.sub(pcp.getWorldPosition(),previous.getWorldPosition());
                     if (dir.lengthSquared() > MathTools.NEAR_ZERO)
                          dir.normalize();
+                    else
+                        return null;
                     Quat4d q = getControlPointOrientationQuat(dir, pcp.getRotationAngle() != null ? pcp.getRotationAngle() : 0.0);
                     AxisAngle4d aa = new AxisAngle4d(MathTools.Y_AXIS,pcp.getTurnAngle() == null ? 0.0 : pcp.getTurnAngle());
                     Quat4d q2 = MathTools.getQuat(aa);
@@ -499,6 +502,8 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
                     dir.sub(next.getWorldPosition(),pcp.getWorldPosition());
                     if (dir.lengthSquared() > MathTools.NEAR_ZERO)
                          dir.normalize();
+                    else
+                        return null;
                     Quat4d q = getControlPointOrientationQuat(dir, pcp.getRotationAngle() != null ? pcp.getRotationAngle() : 0.0);
                     AxisAngle4d aa = new AxisAngle4d(MathTools.Y_AXIS,pcp.getTurnAngle() == null ? 0.0 : pcp.getTurnAngle());
                     Quat4d q2 = MathTools.getQuat(aa);
@@ -791,9 +796,9 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
        }
        
        public double getInlineLength() {
-               if (type == Type.TURN)
+               if (type == PointType.TURN)
                        return length;
-               else if (type == Type.INLINE)
+               else if (type == PointType.INLINE)
                        return length * 0.5;
                return 0;
        }
@@ -1009,7 +1014,19 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
                                        if (currentNext.isVariableLength() && currentPrev.isVariableLength()) {
                                                // we have to join them into single variable length component.
                                                additionalRemove = currentPrev;
-                                               //currentPrev.remove();
+                                               // combine lengths and set the location of remaining control point to the center.
+                                               Point3d ps = new Point3d();
+                                           Point3d pe = new Point3d();
+                                           Point3d ns = new Point3d();
+                        Point3d ne = new Point3d();
+                                               currentPrev.getInlineControlPointEnds(ps, pe);
+                                               currentNext.getInlineControlPointEnds(ns, ne);
+                                               double l = currentPrev.getLength() + currentNext.getLength();
+                                               Vector3d cp = new Vector3d();
+                                               cp.add(ps, ne);
+                                               cp.scale(0.5);
+                                               currentNext.setLength(l);
+                                               currentNext.setWorldPosition(cp);
                                        }
                                } else {
                                        // FIXME : pipe run must be split into two parts, since the control point structure is no more continuous. 
@@ -1149,18 +1166,34 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
                if (component == null)
                        return;
                PipelineComponent next = component.getNext();
-               PipelineComponent prev = component.getNext();
+               PipelineComponent prev = component.getPrevious();
+               PipelineComponent br0 = component.getBranch0();
+               component.setNext(null);
+               component.setPrevious(null);
+               component.setBranch0(null);
                if (next != null) {
                        if (next.getNext() == component)
                                next.setNext(null);
                        else if (next.getPrevious() == component)
                                next.setPrevious(null);
+                       else if (next.getBranch0() == component)
+                           next.setBranch0(null);
                }
                if (prev != null) {
                        if (prev.getNext() == component)
                                prev.setNext(null);
                        else if (prev.getPrevious() == component)
                                prev.setPrevious(null);
+                       else if (prev.getBranch0() == component)
+                           prev.setBranch0(null);
+               }
+               if (br0 != null) {
+                   if (br0.getNext() == component)
+                       prev.setNext(null);
+                   else if (br0.getPrevious() == component)
+                prev.setPrevious(null);
+                   else if (br0.getBranch0() == component)
+                       br0.setBranch0(null);
                }
                PipelineComponent comp = component;
                component = null;