]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/TurnComponent.java
e25fd8d4ad206fbb2faae13cf9cc8a301af27676
[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.objmap.graph.annotations.DynamicGraphType;
13 import org.simantics.objmap.graph.annotations.GetType;
14 import org.simantics.objmap.graph.annotations.RelatedGetValue;
15 import org.simantics.objmap.graph.annotations.RelatedSetValue;
16 import org.simantics.objmap.graph.annotations.SetType;
17 import org.simantics.plant3d.ontology.Plant3D;
18 import org.simantics.plant3d.scenegraph.controlpoint.ControlPointFactory;
19 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
20 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
21
22 @DynamicGraphType(Plant3D.URIs.TurnComponent)
23 public class TurnComponent extends PipelineComponent {
24
25         
26         private String type;
27         private PipeControlPoint controlPoint;
28         private Integer turnRadiusIndex;
29         
30         @GetType(Plant3D.URIs.TurnComponent)
31         public String getType() {
32                 return type;
33         }
34         
35         @SetType(Plant3D.URIs.TurnComponent)
36         public void setType(String type) throws Exception{
37                 this.type = type;
38                 controlPoint = ControlPointFactory.create(this);
39         }
40         
41         @Override
42         public PipeControlPoint getControlPoint() {
43                 return controlPoint;
44         }
45         
46         @Override
47         public void setParent(ParentNode<?> parent, String name) {
48                 super.setParent(parent, name);
49                 setPipeRun((PipeRun)parent);
50         }
51         
52         @Override
53         public Map<String, Object> updateParameterMap() {
54                 Map<String,Object> map = new HashMap<String, Object>();
55                 
56                 if (getPipeRun() != null) {
57                 map.put("turnRadius", getTurnRadius());
58                 map.put("radius", getDiameter() * 0.5);
59                 }
60                 if (controlPoint != null && controlPoint.getTurnAngle() != null  && !Double.isNaN(controlPoint.getTurnAngle())) {
61                         map.put("turnAngle", controlPoint.getTurnAngle());
62                 }
63                 return map;
64         }
65         
66         public boolean isVariableAngle() {
67                 return !controlPoint.isFixed();
68         }
69         
70         @Override
71         public void updateParameters() {
72                 super.updateParameters();
73                 if (!isVariableAngle()) {
74                         Map<String,Object> calculated = getCalculatedParameters();
75                         if (calculated.containsKey("length")) {
76                                 controlPoint.setLength((Double)calculated.get("length"));
77                         }
78                         PipingRules.requestUpdate(getControlPoint());
79                 }
80         }
81         
82         @RelatedGetValue(Plant3D.URIs.HasTurnAngle)
83         public Double getTurnAngle() {
84                 return getControlPoint().getTurnAngle();
85         }
86         
87         @RelatedSetValue(Plant3D.URIs.HasTurnAngle)
88         public void setTurnAngle(Double a) {
89                 if (!getControlPoint().isFixed())
90                         return;
91                 getControlPoint().setTurnAngle(a);            
92         }
93         
94         @GetPropertyValue(name="Turn Angle", value="turn angle", tabId = "Default")
95         public Double getTurnAngleDeg() {
96                 Double d = getControlPoint().getTurnAngle();
97                 if (d == null)
98                         return null;
99                 return MathTools.radToDeg(d);
100         }
101         
102         public Vector3d getTurnAxis() {
103                 return getControlPoint().getTurnAxis();
104         }
105         
106         @GetPropertyValue(name="Turn Radius", value=Plant3D.URIs.HasTurnRadius, tabId = "Default")
107         public Double getTurnRadius() {
108             if (turnRadiusIndex != null)
109                 return getPipeRun().getTurnRadiusArray()[turnRadiusIndex];
110             return getPipeRun().getTurnRadiusArray()[0];
111         }
112         
113         @RelatedGetValue(Plant3D.URIs.HasTurnRadiusIndex)
114         @GetPropertyValue(name="Turn Radius Index", value=Plant3D.URIs.HasTurnRadiusIndex, tabId = "Default")
115         public Integer getTurnRadiusIndex() 
116         {
117         // TODO: For backwards compatibility, we do not accept null values. 
118         //       One development path would allow null index, and setting custom turn radius for the component.
119         if (turnRadiusIndex == null)
120                 return 0;
121             return turnRadiusIndex;
122         }
123         
124         @RelatedSetValue(Plant3D.URIs.HasTurnRadiusIndex)
125         @SetPropertyValue(value=Plant3D.URIs.HasTurnRadiusIndex)
126     public void setTurnRadiusIndex(Integer turnRadiusIndex) {
127             if (this.turnRadiusIndex == turnRadiusIndex)
128                 return;
129             if (turnRadiusIndex == null)
130                 return;
131             if (turnRadiusIndex != null) {
132                 if (getPipeRun().getTurnRadiusArray().length <= turnRadiusIndex)
133                     return;
134             }
135             this.turnRadiusIndex = turnRadiusIndex;
136         firePropertyChanged(Plant3D.URIs.HasTurnRadiusIndex);
137         PipingRules.requestUpdate(getControlPoint());
138     }
139         
140         @RelatedGetValue(Plant3D.URIs.HasRotationAngle)
141         @GetPropertyValue(name="Rotation Angle", value=Plant3D.URIs.HasRotationAngle, tabId = "Default")
142         public Double getRotationAngle() {
143                 if (!controlPoint.isFixed())
144                         return null;
145                 Double d = controlPoint.getRotationAngle();
146                 if (d == null)
147                         return 0.0;
148                 return MathTools.radToDeg(d);
149         }
150         @RelatedSetValue(Plant3D.URIs.HasRotationAngle)
151         @SetPropertyValue(value=Plant3D.URIs.HasRotationAngle)
152         public void setRotationAngle(Double angle) {
153                 if (!controlPoint.isFixed())
154                         return;
155                 
156                 if (angle == null || Double.isInfinite(angle) || Double.isNaN(angle)) {
157                         return;
158                 }
159                 angle = MathTools.degToRad(angle);
160                 if (controlPoint.getRotationAngle() != null && Math.abs(controlPoint.getRotationAngle()-angle) < MathTools.NEAR_ZERO)
161                         return;
162                 controlPoint.setRotationAngle(angle);
163                 PipingRules.requestUpdate(getControlPoint());
164         }
165         
166         @RelatedGetValue(Plant3D.URIs.IsReversed)
167         @GetPropertyValue(name="Reverse", value=Plant3D.URIs.IsReversed, tabId = "Default")
168         public Boolean isReversed() {
169                 if (!controlPoint.isFixed())
170                         return null;
171                 Boolean d = controlPoint._getReversed();
172                 return d;
173         }
174         @RelatedSetValue(Plant3D.URIs.IsReversed)
175         public void setReversed(Boolean reverse) {
176                 if (!controlPoint.isFixed())
177                         return;
178                 
179                 if (reverse == null) {
180                         return;
181                 }
182                 controlPoint.setReversed(reverse);
183                 PipingRules.requestUpdate(getControlPoint()); 
184         }
185         
186         @Override
187         protected double[] getColor() {
188                 if (getControlPoint() == null || !getControlPoint().isFixed())
189                         return new double[]{0.6,0.6,0.6};
190                 else
191                         return new double[]{1.0,0.0,0.0};
192         }
193 }