]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/TurnComponent.java
Refuse attempts to set turn radius index to a negative value.
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / TurnComponent.java
index a1374def587c857a3b0327cba4510891896838be..794c53294a0197210674e6bb416aefa5b70401e2 100644 (file)
@@ -1,14 +1,20 @@
 package org.simantics.plant3d.scenegraph;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import javax.vecmath.Vector3d;
 
 import org.simantics.g3d.math.MathTools;
+import org.simantics.g3d.property.annotations.GetComboProperty;
+import org.simantics.g3d.property.annotations.GetComboPropertyValue;
 import org.simantics.g3d.property.annotations.GetPropertyValue;
+import org.simantics.g3d.property.annotations.SetComboPropertyValue;
 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;
@@ -36,6 +42,9 @@ public class TurnComponent extends PipelineComponent {
        public void setType(String type) throws Exception{
                this.type = type;
                controlPoint = ControlPointFactory.create(this);
+               syncNext();
+        syncPrevious();
+        syncBranch0();
        }
        
        @Override
@@ -79,8 +88,12 @@ public class TurnComponent extends PipelineComponent {
                }
        }
        
-       @RelatedGetValue(Plant3D.URIs.HasTurnAngle)
        public Double getTurnAngle() {
+           return getControlPoint().getTurnAngle();
+       }
+       
+       @RelatedGetValue(Plant3D.URIs.HasTurnAngle)
+       public Double _getTurnAngle() {
            if (!getControlPoint().asFixedAngle())
                return null;
                return getControlPoint().getTurnAngle();
@@ -105,15 +118,14 @@ public class TurnComponent extends PipelineComponent {
                return getControlPoint().getTurnAxis();
        }
        
-       @GetPropertyValue(name="Turn Radius", value="TurnRadius", tabId = "Default")
-       public Double getTurnRadius() {
+    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")
+       @GetComboPropertyValue(name="Turn Radius", value=Plant3D.URIs.HasTurnRadiusIndex, tabId = "Default")
        public Integer getTurnRadiusIndex() 
        {
         // TODO: For backwards compatibility, we do not accept null values. 
@@ -124,11 +136,11 @@ public class TurnComponent extends PipelineComponent {
        }
        
        @RelatedSetValue(Plant3D.URIs.HasTurnRadiusIndex)
-       @SetPropertyValue(value=Plant3D.URIs.HasTurnRadiusIndex)
+       @SetComboPropertyValue(value=Plant3D.URIs.HasTurnRadiusIndex)
     public void setTurnRadiusIndex(Integer turnRadiusIndex) {
            if (this.turnRadiusIndex == turnRadiusIndex)
                return;
-           if (turnRadiusIndex == null)
+           if (turnRadiusIndex == null || turnRadiusIndex < 0)
                return;
            if (turnRadiusIndex != null && getPipeRun() != null) {
                if (getPipeRun().getTurnRadiusArray().length <= turnRadiusIndex)
@@ -139,6 +151,15 @@ public class TurnComponent extends PipelineComponent {
         PipingRules.requestUpdate(getControlPoint());
     }
        
+       @GetComboProperty(value=Plant3D.URIs.HasTurnRadiusIndex)
+       public List<Double> _getTurnRadii() {
+           List<Double> values = new ArrayList<Double>();
+           for (double d : getPipeRun().getTurnRadiusArray())
+               values.add(d);
+           return values;
+       }
+       
+       
        @RelatedGetValue(Plant3D.URIs.HasRotationAngle)
        @GetPropertyValue(name="Rotation Angle", value=Plant3D.URIs.HasRotationAngle, tabId = "Default")
        public Double getRotationAngle() {
@@ -192,4 +213,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;
+       }
 }