]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/TurnComponent.java
Calculating turn specific coordinates
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / TurnComponent.java
index e8779985ae6b44622b62dd166fb260d61200a376..3d5fb5bf7fc27130d078414dde5da2a72bbd17c9 100644 (file)
@@ -9,6 +9,7 @@ import org.simantics.g3d.math.MathTools;
 import org.simantics.g3d.property.annotations.GetPropertyValue;
 import org.simantics.g3d.property.annotations.SetPropertyValue;
 import org.simantics.g3d.scenegraph.base.ParentNode;
+import org.simantics.g3d.tools.NodeTools;
 import org.simantics.objmap.graph.annotations.DynamicGraphType;
 import org.simantics.objmap.graph.annotations.GetType;
 import org.simantics.objmap.graph.annotations.RelatedGetValue;
@@ -25,6 +26,7 @@ public class TurnComponent extends PipelineComponent {
        
        private String type;
        private PipeControlPoint controlPoint;
+       private Integer turnRadiusIndex;
        
        @GetType(Plant3D.URIs.TurnComponent)
        public String getType() {
@@ -52,10 +54,9 @@ public class TurnComponent extends PipelineComponent {
        public Map<String, Object> updateParameterMap() {
                Map<String,Object> map = new HashMap<String, Object>();
                
-               PipeRun pipeRun = getPipeRun();
-               if (pipeRun != null) {
-                       map.put("turnRadius", pipeRun.getTurnRadius());
-                       map.put("radius", pipeRun.getPipeDiameter() * 0.5);
+               if (getPipeRun() != null) {
+               map.put("turnRadius", getTurnRadius());
+               map.put("radius", getDiameter() * 0.5);
                }
                if (controlPoint != null && controlPoint.getTurnAngle() != null  && !Double.isNaN(controlPoint.getTurnAngle())) {
                        map.put("turnAngle", controlPoint.getTurnAngle());
@@ -64,30 +65,33 @@ public class TurnComponent extends PipelineComponent {
        }
        
        public boolean isVariableAngle() {
-        return !controlPoint.isFixed();
-    }
+               return !controlPoint.isFixed();
+       }
        
        @Override
        public void updateParameters() {
-           super.updateParameters();
-           if (!isVariableAngle()) {
-            Map<String,Object> calculated = getCalculatedParameters();
-            if (calculated.containsKey("length")) {
-                controlPoint.setLength((Double)calculated.get("length"));
-            }
-        }
+               super.updateParameters();
+               if (controlPoint.asFixedAngle()) {
+                       Map<String,Object> calculated = getCalculatedParameters();
+                       if (calculated.containsKey("length")) {
+                               controlPoint.setLength((Double)calculated.get("length"));
+                       }
+                       PipingRules.requestUpdate(getControlPoint());
+               }
        }
        
        @RelatedGetValue(Plant3D.URIs.HasTurnAngle)
        public Double getTurnAngle() {
+           if (!getControlPoint().asFixedAngle())
+               return null;
                return getControlPoint().getTurnAngle();
        }
        
        @RelatedSetValue(Plant3D.URIs.HasTurnAngle)
        public void setTurnAngle(Double a) {
-           if (!getControlPoint().isFixed())
-               return;
-           getControlPoint().setTurnAngle(a);            
+               if (!getControlPoint().asFixedAngle())
+                       return;
+               getControlPoint().setTurnAngle(a);            
        }
        
        @GetPropertyValue(name="Turn Angle", value="turn angle", tabId = "Default")
@@ -102,37 +106,86 @@ public class TurnComponent extends PipelineComponent {
                return getControlPoint().getTurnAxis();
        }
        
-       @RelatedGetValue(Plant3D.URIs.HasRotationAngle)
-    @GetPropertyValue(name="Rotation Angle", value=Plant3D.URIs.HasRotationAngle, tabId = "Default")
-    public Double getRotationAngle() {
-        if (!controlPoint.isFixed())
-            return null;
-        Double d = controlPoint.getRotationAngle();
-        if (d == null)
-            return 0.0;
-        return MathTools.radToDeg(d);
-    }
-    @RelatedSetValue(Plant3D.URIs.HasRotationAngle)
-    @SetPropertyValue(value=Plant3D.URIs.HasRotationAngle)
-    public void setRotationAngle(Double angle) {
-        if (!controlPoint.isFixed())
-            return;
-        
-        if (angle == null || Double.isInfinite(angle) || Double.isNaN(angle)) {
-            return;
-        }
-        angle = MathTools.degToRad(angle);
-        if (controlPoint.getRotationAngle() != null && Math.abs(controlPoint.getRotationAngle()-angle) < MathTools.NEAR_ZERO)
-            return;
-        controlPoint.setRotationAngle(angle);
-        try {
-            PipingRules.requestUpdate(getControlPoint());
-        } catch (Exception e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
-        }   
+       @GetPropertyValue(name="Turn Radius", value="TurnRadius", tabId = "Default")
+       public Double getTurnRadius() {
+           if (turnRadiusIndex != null)
+               return getPipeRun().getTurnRadiusArray()[turnRadiusIndex];
+           return getPipeRun().getTurnRadiusArray()[0];
+       }
+       
+       @RelatedGetValue(Plant3D.URIs.HasTurnRadiusIndex)
+       @GetPropertyValue(name="Turn Radius Index", value=Plant3D.URIs.HasTurnRadiusIndex, tabId = "Default")
+       public Integer getTurnRadiusIndex() 
+       {
+        // TODO: For backwards compatibility, we do not accept null values. 
+        //       One development path would allow null index, and setting custom turn radius for the component.
+        if (turnRadiusIndex == null)
+               return 0;
+           return turnRadiusIndex;
+       }
+       
+       @RelatedSetValue(Plant3D.URIs.HasTurnRadiusIndex)
+       @SetPropertyValue(value=Plant3D.URIs.HasTurnRadiusIndex)
+    public void setTurnRadiusIndex(Integer turnRadiusIndex) {
+           if (this.turnRadiusIndex == turnRadiusIndex)
+               return;
+           if (turnRadiusIndex == null)
+               return;
+           if (turnRadiusIndex != null && getPipeRun() != null) {
+               if (getPipeRun().getTurnRadiusArray().length <= turnRadiusIndex)
+                   return;
+           }
+           this.turnRadiusIndex = turnRadiusIndex;
+        firePropertyChanged(Plant3D.URIs.HasTurnRadiusIndex);
+        PipingRules.requestUpdate(getControlPoint());
     }
        
+       @RelatedGetValue(Plant3D.URIs.HasRotationAngle)
+       @GetPropertyValue(name="Rotation Angle", value=Plant3D.URIs.HasRotationAngle, tabId = "Default")
+       public Double getRotationAngle() {
+               if (!controlPoint.asFixedAngle())
+                       return null;
+               Double d = controlPoint.getRotationAngle();
+               if (d == null)
+                       return 0.0;
+               return MathTools.radToDeg(d);
+       }
+       @RelatedSetValue(Plant3D.URIs.HasRotationAngle)
+       @SetPropertyValue(value=Plant3D.URIs.HasRotationAngle)
+       public void setRotationAngle(Double angle) {
+               if (!controlPoint.asFixedAngle())
+                       return;
+               
+               if (angle == null || Double.isInfinite(angle) || Double.isNaN(angle)) {
+                       return;
+               }
+               angle = MathTools.degToRad(angle);
+               if (controlPoint.getRotationAngle() != null && Math.abs(controlPoint.getRotationAngle()-angle) < MathTools.NEAR_ZERO)
+                       return;
+               controlPoint.setRotationAngle(angle);
+               PipingRules.requestUpdate(getControlPoint());
+       }
+       
+       @RelatedGetValue(Plant3D.URIs.IsReversed)
+       @GetPropertyValue(name="Reverse", value=Plant3D.URIs.IsReversed, tabId = "Default")
+       public Boolean isReversed() {
+               if (!controlPoint.asFixedAngle())
+                       return null;
+               Boolean d = controlPoint._getReversed();
+               return d;
+       }
+       @RelatedSetValue(Plant3D.URIs.IsReversed)
+       public void setReversed(Boolean reverse) {
+               if (!controlPoint.asFixedAngle())
+                       return;
+               
+               if (reverse == null) {
+                       return;
+               }
+               controlPoint.setReversed(reverse);
+               PipingRules.requestUpdate(getControlPoint()); 
+       }
+       
        @Override
        protected double[] getColor() {
                if (getControlPoint() == null || !getControlPoint().isFixed())
@@ -140,4 +193,38 @@ public class TurnComponent extends PipelineComponent {
                else
                        return new double[]{1.0,0.0,0.0};
        }
+       
+       /**
+        * Turn is a section of a circle; this method returns center of that circle.
+        * @return
+        */
+       public Vector3d getCenterPosition() {
+           // getPosition() is control point position, located outside of turn
+        // we need to calculate position in the middle of the turn
+        double t = Math.tan((Math.PI - getControlPoint().getTurnAngle()) * 0.5);
+        double tr = getTurnRadius();
+        double R = 0.0;
+        if (t > MathTools.NEAR_ZERO)
+            R = tr / t;
+        Vector3d localC = new Vector3d(-R, 0.0, -tr);
+        // worldC is center of a circle
+        Vector3d worldC = NodeTools.getWorldPosition(this,localC);
+        return worldC;
+       }
+       
+       /**
+        * Returns position in the middle of turn component
+        * @return
+        */
+       public Vector3d getMiddlePosition() {
+           Vector3d pos = getWorldPosition();
+           Vector3d worldC = getCenterPosition();
+           
+           // calculate vector from center to edge, which is middle of the turn
+        pos.sub(worldC);
+        pos.normalize();
+        pos.scale(getTurnRadius());
+        pos.add(worldC);
+        return pos;
+       }
 }