1 package org.simantics.plant3d.scenegraph;
3 import java.util.HashMap;
6 import javax.vecmath.Vector3d;
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;
23 @DynamicGraphType(Plant3D.URIs.TurnComponent)
24 public class TurnComponent extends PipelineComponent {
28 private PipeControlPoint controlPoint;
29 private Integer turnRadiusIndex;
31 @GetType(Plant3D.URIs.TurnComponent)
32 public String getType() {
36 @SetType(Plant3D.URIs.TurnComponent)
37 public void setType(String type) throws Exception{
39 controlPoint = ControlPointFactory.create(this);
43 public PipeControlPoint getControlPoint() {
48 public void setParent(ParentNode<?> parent, String name) {
49 super.setParent(parent, name);
50 setPipeRun((PipeRun)parent);
54 public Map<String, Object> updateParameterMap() {
55 Map<String,Object> map = new HashMap<String, Object>();
57 if (getPipeRun() != null) {
58 map.put("turnRadius", getTurnRadius());
59 map.put("radius", getDiameter() * 0.5);
61 if (controlPoint != null && controlPoint.getTurnAngle() != null && !Double.isNaN(controlPoint.getTurnAngle())) {
62 map.put("turnAngle", controlPoint.getTurnAngle());
67 public boolean isVariableAngle() {
68 return !controlPoint.isFixed();
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"));
79 PipingRules.requestUpdate(getControlPoint());
83 public Double getTurnAngle() {
84 return getControlPoint().getTurnAngle();
87 @RelatedGetValue(Plant3D.URIs.HasTurnAngle)
88 public Double _getTurnAngle() {
89 if (!getControlPoint().asFixedAngle())
91 return getControlPoint().getTurnAngle();
94 @RelatedSetValue(Plant3D.URIs.HasTurnAngle)
95 public void setTurnAngle(Double a) {
96 if (!getControlPoint().asFixedAngle())
98 getControlPoint().setTurnAngle(a);
101 @GetPropertyValue(name="Turn Angle", value="turn angle", tabId = "Default")
102 public Double getTurnAngleDeg() {
103 Double d = getControlPoint().getTurnAngle();
106 return MathTools.radToDeg(d);
109 public Vector3d getTurnAxis() {
110 return getControlPoint().getTurnAxis();
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];
120 @RelatedGetValue(Plant3D.URIs.HasTurnRadiusIndex)
121 @GetPropertyValue(name="Turn Radius Index", value=Plant3D.URIs.HasTurnRadiusIndex, tabId = "Default")
122 public Integer getTurnRadiusIndex()
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)
128 return turnRadiusIndex;
131 @RelatedSetValue(Plant3D.URIs.HasTurnRadiusIndex)
132 @SetPropertyValue(value=Plant3D.URIs.HasTurnRadiusIndex)
133 public void setTurnRadiusIndex(Integer turnRadiusIndex) {
134 if (this.turnRadiusIndex == turnRadiusIndex)
136 if (turnRadiusIndex == null)
138 if (turnRadiusIndex != null && getPipeRun() != null) {
139 if (getPipeRun().getTurnRadiusArray().length <= turnRadiusIndex)
142 this.turnRadiusIndex = turnRadiusIndex;
143 firePropertyChanged(Plant3D.URIs.HasTurnRadiusIndex);
144 PipingRules.requestUpdate(getControlPoint());
147 @RelatedGetValue(Plant3D.URIs.HasRotationAngle)
148 @GetPropertyValue(name="Rotation Angle", value=Plant3D.URIs.HasRotationAngle, tabId = "Default")
149 public Double getRotationAngle() {
150 if (!controlPoint.asFixedAngle())
152 Double d = controlPoint.getRotationAngle();
155 return MathTools.radToDeg(d);
157 @RelatedSetValue(Plant3D.URIs.HasRotationAngle)
158 @SetPropertyValue(value=Plant3D.URIs.HasRotationAngle)
159 public void setRotationAngle(Double angle) {
160 if (!controlPoint.asFixedAngle())
163 if (angle == null || Double.isInfinite(angle) || Double.isNaN(angle)) {
166 angle = MathTools.degToRad(angle);
167 if (controlPoint.getRotationAngle() != null && Math.abs(controlPoint.getRotationAngle()-angle) < MathTools.NEAR_ZERO)
169 controlPoint.setRotationAngle(angle);
170 PipingRules.requestUpdate(getControlPoint());
173 @RelatedGetValue(Plant3D.URIs.IsReversed)
174 @GetPropertyValue(name="Reverse", value=Plant3D.URIs.IsReversed, tabId = "Default")
175 public Boolean isReversed() {
176 if (!controlPoint.asFixedAngle())
178 Boolean d = controlPoint._getReversed();
181 @RelatedSetValue(Plant3D.URIs.IsReversed)
182 public void setReversed(Boolean reverse) {
183 if (!controlPoint.asFixedAngle())
186 if (reverse == null) {
189 controlPoint.setReversed(reverse);
190 PipingRules.requestUpdate(getControlPoint());
194 protected double[] getColor() {
195 if (getControlPoint() == null || !getControlPoint().isFixed())
196 return new double[]{0.6,0.6,0.6};
198 return new double[]{1.0,0.0,0.0};
202 * Turn is a section of a circle; this method returns center of that circle.
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();
211 if (t > MathTools.NEAR_ZERO)
213 Vector3d localC = new Vector3d(-R, 0.0, -tr);
214 // worldC is center of a circle
215 Vector3d worldC = NodeTools.getWorldPosition(this,localC);
220 * Returns position in the middle of turn component
223 public Vector3d getMiddlePosition() {
224 Vector3d pos = getWorldPosition();
225 Vector3d worldC = getCenterPosition();
227 // calculate vector from center to edge, which is middle of the turn
230 pos.scale(getTurnRadius());