]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/TurnComponent.java
Disabling branch creation prevented connections
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / TurnComponent.java
1 package org.simantics.plant3d.scenegraph;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.vecmath.Vector3d;
7
8 import org.simantics.g3d.math.MathTools;
9 import org.simantics.g3d.property.annotations.GetPropertyValue;
10 import org.simantics.g3d.property.annotations.SetPropertyValue;
11 import org.simantics.g3d.scenegraph.base.ParentNode;
12 import org.simantics.g3d.tools.NodeTools;
13 import org.simantics.objmap.graph.annotations.DynamicGraphType;
14 import org.simantics.objmap.graph.annotations.GetType;
15 import org.simantics.objmap.graph.annotations.RelatedGetValue;
16 import org.simantics.objmap.graph.annotations.RelatedSetValue;
17 import org.simantics.objmap.graph.annotations.SetType;
18 import org.simantics.plant3d.ontology.Plant3D;
19 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
20 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
21 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
22
23 @DynamicGraphType(Plant3D.URIs.TurnComponent)
24 public class TurnComponent extends PipelineComponent {
25
26         
27         private String type;
28         private PipeControlPoint controlPoint;
29         private Integer turnRadiusIndex;
30         
31         @GetType(Plant3D.URIs.TurnComponent)
32         public String getType() {
33                 return type;
34         }
35         
36         @SetType(Plant3D.URIs.TurnComponent)
37         public void setType(String type) throws Exception{
38                 this.type = type;
39                 controlPoint = ControlPointFactory.create(this);
40         }
41         
42         @Override
43         public PipeControlPoint getControlPoint() {
44                 return controlPoint;
45         }
46         
47         @Override
48         public void setParent(ParentNode<?> parent, String name) {
49                 super.setParent(parent, name);
50                 setPipeRun((PipeRun)parent);
51         }
52         
53         @Override
54         public Map<String, Object> updateParameterMap() {
55                 Map<String,Object> map = new HashMap<String, Object>();
56                 
57                 if (getPipeRun() != null) {
58                 map.put("turnRadius", getTurnRadius());
59                 map.put("radius", getDiameter() * 0.5);
60                 }
61                 if (controlPoint != null && controlPoint.getTurnAngle() != null  && !Double.isNaN(controlPoint.getTurnAngle())) {
62                         map.put("turnAngle", controlPoint.getTurnAngle());
63                 }
64                 return map;
65         }
66         
67         public boolean isVariableAngle() {
68                 return !controlPoint.isFixed();
69         }
70         
71         @Override
72         public void updateParameters() {
73                 super.updateParameters();
74                 if (controlPoint.asFixedAngle()) {
75                         Map<String,Object> calculated = getCalculatedParameters();
76                         if (calculated.containsKey("length")) {
77                                 controlPoint.setLength((Double)calculated.get("length"));
78                         }
79                         PipingRules.requestUpdate(getControlPoint());
80                 }
81         }
82         
83         public Double getTurnAngle() {
84             return getControlPoint().getTurnAngle();
85         }
86         
87         @RelatedGetValue(Plant3D.URIs.HasTurnAngle)
88         public Double _getTurnAngle() {
89             if (!getControlPoint().asFixedAngle())
90                 return null;
91                 return getControlPoint().getTurnAngle();
92         }
93         
94         @RelatedSetValue(Plant3D.URIs.HasTurnAngle)
95         public void setTurnAngle(Double a) {
96                 if (!getControlPoint().asFixedAngle())
97                         return;
98                 getControlPoint().setTurnAngle(a);            
99         }
100         
101         @GetPropertyValue(name="Turn Angle", value="turn angle", tabId = "Default")
102         public Double getTurnAngleDeg() {
103                 Double d = getControlPoint().getTurnAngle();
104                 if (d == null)
105                         return null;
106                 return MathTools.radToDeg(d);
107         }
108         
109         public Vector3d getTurnAxis() {
110                 return getControlPoint().getTurnAxis();
111         }
112         
113         @GetPropertyValue(name="Turn Radius", value="TurnRadius", tabId = "Default")
114         public Double getTurnRadius() {
115             if (turnRadiusIndex != null)
116                 return getPipeRun().getTurnRadiusArray()[turnRadiusIndex];
117             return getPipeRun().getTurnRadiusArray()[0];
118         }
119         
120         @RelatedGetValue(Plant3D.URIs.HasTurnRadiusIndex)
121         @GetPropertyValue(name="Turn Radius Index", value=Plant3D.URIs.HasTurnRadiusIndex, tabId = "Default")
122         public Integer getTurnRadiusIndex() 
123         {
124         // TODO: For backwards compatibility, we do not accept null values. 
125         //       One development path would allow null index, and setting custom turn radius for the component.
126         if (turnRadiusIndex == null)
127                 return 0;
128             return turnRadiusIndex;
129         }
130         
131         @RelatedSetValue(Plant3D.URIs.HasTurnRadiusIndex)
132         @SetPropertyValue(value=Plant3D.URIs.HasTurnRadiusIndex)
133     public void setTurnRadiusIndex(Integer turnRadiusIndex) {
134             if (this.turnRadiusIndex == turnRadiusIndex)
135                 return;
136             if (turnRadiusIndex == null)
137                 return;
138             if (turnRadiusIndex != null && getPipeRun() != null) {
139                 if (getPipeRun().getTurnRadiusArray().length <= turnRadiusIndex)
140                     return;
141             }
142             this.turnRadiusIndex = turnRadiusIndex;
143         firePropertyChanged(Plant3D.URIs.HasTurnRadiusIndex);
144         PipingRules.requestUpdate(getControlPoint());
145     }
146         
147         @RelatedGetValue(Plant3D.URIs.HasRotationAngle)
148         @GetPropertyValue(name="Rotation Angle", value=Plant3D.URIs.HasRotationAngle, tabId = "Default")
149         public Double getRotationAngle() {
150                 if (!controlPoint.asFixedAngle())
151                         return null;
152                 Double d = controlPoint.getRotationAngle();
153                 if (d == null)
154                         return 0.0;
155                 return MathTools.radToDeg(d);
156         }
157         @RelatedSetValue(Plant3D.URIs.HasRotationAngle)
158         @SetPropertyValue(value=Plant3D.URIs.HasRotationAngle)
159         public void setRotationAngle(Double angle) {
160                 if (!controlPoint.asFixedAngle())
161                         return;
162                 
163                 if (angle == null || Double.isInfinite(angle) || Double.isNaN(angle)) {
164                         return;
165                 }
166                 angle = MathTools.degToRad(angle);
167                 if (controlPoint.getRotationAngle() != null && Math.abs(controlPoint.getRotationAngle()-angle) < MathTools.NEAR_ZERO)
168                         return;
169                 controlPoint.setRotationAngle(angle);
170                 PipingRules.requestUpdate(getControlPoint());
171         }
172         
173         @RelatedGetValue(Plant3D.URIs.IsReversed)
174         @GetPropertyValue(name="Reverse", value=Plant3D.URIs.IsReversed, tabId = "Default")
175         public Boolean isReversed() {
176                 if (!controlPoint.asFixedAngle())
177                         return null;
178                 Boolean d = controlPoint._getReversed();
179                 return d;
180         }
181         @RelatedSetValue(Plant3D.URIs.IsReversed)
182         public void setReversed(Boolean reverse) {
183                 if (!controlPoint.asFixedAngle())
184                         return;
185                 
186                 if (reverse == null) {
187                         return;
188                 }
189                 controlPoint.setReversed(reverse);
190                 PipingRules.requestUpdate(getControlPoint()); 
191         }
192         
193         @Override
194         protected double[] getColor() {
195                 if (getControlPoint() == null || !getControlPoint().isFixed())
196                         return new double[]{0.6,0.6,0.6};
197                 else
198                         return new double[]{1.0,0.0,0.0};
199         }
200         
201         /**
202          * Turn is a section of a circle; this method returns center of that circle.
203          * @return
204          */
205         public Vector3d getCenterPosition() {
206             // getPosition() is control point position, located outside of turn
207         // we need to calculate position in the middle of the turn
208         double t = Math.tan((Math.PI - getControlPoint().getTurnAngle()) * 0.5);
209         double tr = getTurnRadius();
210         double R = 0.0;
211         if (t > MathTools.NEAR_ZERO)
212             R = tr / t;
213         Vector3d localC = new Vector3d(-R, 0.0, -tr);
214         // worldC is center of a circle
215         Vector3d worldC = NodeTools.getWorldPosition(this,localC);
216         return worldC;
217         }
218         
219         /**
220          * Returns position in the middle of turn component
221          * @return
222          */
223         public Vector3d getMiddlePosition() {
224             Vector3d pos = getWorldPosition();
225             Vector3d worldC = getCenterPosition();
226             
227             // calculate vector from center to edge, which is middle of the turn
228         pos.sub(worldC);
229         pos.normalize();
230         pos.scale(getTurnRadius());
231         pos.add(worldC);
232         return pos;
233         }
234 }