1 package org.simantics.plant3d.scenegraph;
3 import java.util.Collections;
6 import javax.vecmath.Quat4d;
7 import javax.vecmath.Tuple3d;
8 import javax.vecmath.Vector3d;
10 import org.simantics.g3d.math.MathTools;
11 import org.simantics.g3d.property.annotations.GetPropertyValue;
12 import org.simantics.g3d.property.annotations.PropertyContributor;
13 import org.simantics.objmap.graph.annotations.RelatedGetObj;
14 import org.simantics.objmap.graph.annotations.RelatedSetObj;
15 import org.simantics.plant3d.ontology.Plant3D;
16 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
17 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Type;
18 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
22 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
26 public abstract class PipelineComponent extends GeometryNode {
29 private PipeRun pipeRun;
30 private PipeRun alternativePipeRun;
31 private PipelineComponent next;
32 private PipelineComponent previous;
37 * With in-line,turn, and end components, the pipe run is the parent object in the scene-graph.
38 * With nozzles, the pipe run setting is explicit (nozzle has to be linked to the piperun, since the parent object is equipment).
39 * With size change components (in-line), there is also alternative pipe run, which must match the next component's pipe run.
43 public void setPipeRun(PipeRun pipeRun) {
44 if (pipeRun == this.pipeRun)
46 this.pipeRun = pipeRun;
47 if (getControlPoint() != null) {
48 getControlPoint().deattach();
49 if (pipeRun != null) {
50 pipeRun.addChild(getControlPoint());
56 @RelatedGetObj(Plant3D.URIs.HasAlternativePipeRun)
57 public PipeRun getAlternativePipeRun() {
58 return alternativePipeRun;
61 @RelatedSetObj(Plant3D.URIs.HasAlternativePipeRun)
62 public void setAlternativePipeRun(PipeRun pipeRun) {
63 if (this.alternativePipeRun == pipeRun)
65 this.alternativePipeRun = pipeRun;
66 if (getControlPoint().isDualInline()) {
67 PipeControlPoint sub = getControlPoint().getSubPoint().get(0);
68 if (sub.getParent() != this.alternativePipeRun)
69 this.alternativePipeRun.addChild(sub);
71 firePropertyChanged(Plant3D.URIs.HasAlternativePipeRun);
75 public void updateParameters() {
76 setParameterMap(updateParameterMap());
77 super.updateParameters();
80 public abstract void setType(String typeURI) throws Exception;
82 @RelatedGetObj(Plant3D.URIs.HasNext)
83 public PipelineComponent getNext() {
87 @RelatedSetObj(Plant3D.URIs.HasNext)
88 public void setNext(PipelineComponent comp) {
93 firePropertyChanged(Plant3D.URIs.HasNext);
96 // System.out.println(this + " next " + comp);
100 @RelatedGetObj(Plant3D.URIs.HasPrevious)
101 public PipelineComponent getPrevious() {
105 @RelatedSetObj(Plant3D.URIs.HasPrevious)
106 public void setPrevious(PipelineComponent comp) {
107 if (previous == comp)
109 this.previous = comp;
111 firePropertyChanged(Plant3D.URIs.HasPrevious);
114 // System.out.println(this + " prev " + comp);
116 private PipelineComponent branch0;
118 @RelatedGetObj(Plant3D.URIs.HasBranch0)
119 public PipelineComponent getBranch0() {
123 @RelatedSetObj(Plant3D.URIs.HasBranch0)
124 public void setBranch0(PipelineComponent comp) {
129 firePropertyChanged(Plant3D.URIs.HasBranch0);
132 // System.out.println(this + " next " + comp);
137 private PipeControlPoint getBranchPoint() {
138 PipeControlPoint branchPoint;
139 if (getControlPoint().getSubPoint().size() > 0) {
140 branchPoint = getControlPoint().getSubPoint().get(0);
142 if (branch0.getPipeRun() == null)
144 branchPoint = new PipeControlPoint(this,branch0.getPipeRun());
145 branchPoint.setFixed(false);
146 branchPoint.setType(Type.END);
147 branchPoint.parent = getControlPoint();
148 getControlPoint().children.add(branchPoint);
149 branchPoint.setWorldOrientation(getControlPoint().getWorldOrientation());
150 branchPoint.setWorldPosition(getControlPoint().getWorldPosition());
155 private boolean _connectNext(PipeControlPoint pcp, PipeControlPoint nextPCP) {
158 if (pcp.getNext() != nextPCP) {
159 pcp.setNext(nextPCP);
161 if (pcp.isDualInline()) {
162 PipeControlPoint sub = pcp.getSubPoint().get(0);
163 if (sub.getNext() != nextPCP)
164 sub.setNext(nextPCP);
169 private boolean _connectPrev(PipeControlPoint pcp, PipeControlPoint prevPCP) {
172 if (prevPCP.isDualInline())
173 prevPCP = prevPCP.getSubPoint().get(0);
174 if (pcp.getPrevious() != prevPCP) {
175 pcp.setPrevious(prevPCP);
177 if (pcp.isDualInline()) {
178 PipeControlPoint sub = pcp.getSubPoint().get(0);
179 if (sub.getPrevious() != prevPCP)
180 sub.setPrevious(prevPCP);
187 private boolean syncNext() {
189 if (getControlPoint() != null) {
191 if (next.getControlPoint() != null && next.getPipeRun() != null) {
193 // TODO, relying that the other direction is connected.
194 boolean nxt = next.getPrevious() == this;
195 boolean br0 = next.getBranch0() == this;
197 return _connectNext(getControlPoint(), next.getControlPoint());
199 return _connectNext(getControlPoint(), next.getBranchPoint());
205 } else if (getControlPoint().getPrevious() != null) {
206 getControlPoint().setNext(null);
214 private boolean syncPrevious() {
216 if (getControlPoint() != null) {
217 if (previous != null ) {
218 if (previous.getControlPoint() != null && previous.getPipeRun() != null) {
220 // TODO, relying that the other direction is connected.
221 boolean prev = previous.getNext() == this;
222 boolean br0 = previous.getBranch0() == this;
224 return _connectPrev(getControlPoint(), previous.getControlPoint());
226 return _connectPrev(getControlPoint(), previous.getBranchPoint());
232 } else if (getControlPoint().getPrevious() != null) {
233 getControlPoint().setPrevious(null);
241 private boolean syncBranch0() {
242 if (getControlPoint() != null) {
243 if (getControlPoint().isDualInline()) {
247 if (branch0 != null) {
248 if (branch0.getControlPoint() != null && branch0.getPipeRun() != null) {
249 PipeControlPoint branchPoint = getBranchPoint();
250 PipeControlPoint pcp = branch0.getControlPoint();
251 // TODO, relying that the other direction is connected.
252 boolean next = branch0.getPrevious() == this; // this --> branch0
253 boolean prev = branch0.getNext() == this;
255 _connectNext(branchPoint, pcp);
257 _connectPrev(branchPoint, pcp);
263 } else if (getControlPoint().getSubPoint().size() > 0) { // TODO : this may cause problems? (Removes branch point, before branch has been set?)
264 getControlPoint().getSubPoint().get(0).remove();
265 getControlPoint().children.clear();
279 public void sync2() {
280 // if (getControlPoint().isDualInline()) {
281 // PipeControlPoint sub = getControlPoint().getSubPoint().get(0);
282 // next.getControlPoint().getPipeRun().addChild(sub);
284 getControlPoint()._setWorldOrientation(getWorldOrientation());
285 getControlPoint()._setWorldPosition(getWorldPosition());
288 public Map<String,Object> updateParameterMap() {
289 return Collections.EMPTY_MAP;
292 public PipeRun getPipeRun() {
296 public abstract String getType();
297 public abstract PipeControlPoint getControlPoint();
300 public void remove() {
301 PipeControlPoint pcp = getControlPoint();
309 protected double[] getColor() {
310 if (getControlPoint() == null || !getControlPoint().isFixed())
311 return new double[]{0.7,0.7,0.7};
313 return new double[]{1.0,0.0,0.0};
317 protected double[] getSelectedColor() {
318 return new double[]{0.5,0,0.5};
322 public void setOrientation(Quat4d orientation) {
323 if (MathTools.equals(orientation, getOrientation()))
325 super.setOrientation(orientation);
326 if (getControlPoint() != null) {
327 getControlPoint()._setWorldOrientation(getWorldOrientation());
329 PipingRules.requestUpdate(getControlPoint());
330 } catch (Exception e) {
331 // TODO Auto-generated catch block
338 public void setPosition(Vector3d position) {
339 if (MathTools.equals(position, getPosition()))
341 super.setPosition(position);
342 if (getControlPoint() != null) {
343 getControlPoint()._setWorldPosition(getWorldPosition());
345 PipingRules.requestUpdate(getControlPoint());
346 } catch (Exception e) {
347 // TODO Auto-generated catch block
354 public void _setWorldPosition(Vector3d position) {
355 Vector3d localPos = getLocalPosition(position);
356 super.setPosition(localPos);
359 public void _setWorldOrientation(Quat4d orientation) {
360 Quat4d localOr = getLocalOrientation(orientation);
361 super.setOrientation(localOr);
364 @GetPropertyValue(name="Flow Length", value="flowlength", tabId = "Default")
365 public Double getFlowLength() {
366 PipeControlPoint pcp = getControlPoint();
369 switch (pcp.getType()) {
371 return pcp.getLength();
375 double r = getPipeRun().getTurnRadius();
376 double a = pcp.getTurnAngle();
384 public void getControlPointEnds(Tuple3d p1, Tuple3d p2) {
385 getControlPoint().getControlPointEnds(p1, p2);
388 public Vector3d getNormal() {
389 Vector3d v = new Vector3d();
390 MathTools.rotate(getWorldOrientation(), MathTools.Z_AXIS, v);