1 package org.simantics.plant3d.scenegraph;
3 import java.util.Collections;
4 import java.util.HashMap;
6 import java.util.Map.Entry;
8 import javax.vecmath.Quat4d;
9 import javax.vecmath.Tuple3d;
10 import javax.vecmath.Vector3d;
12 import org.simantics.g3d.math.MathTools;
13 import org.simantics.g3d.property.annotations.CompoundGetPropertyValue;
14 import org.simantics.g3d.property.annotations.CompoundSetPropertyValue;
15 import org.simantics.g3d.property.annotations.GetPropertyValue;
16 import org.simantics.g3d.property.annotations.PropertyContributor;
17 import org.simantics.objmap.graph.annotations.CompoundRelatedGetValue;
18 import org.simantics.objmap.graph.annotations.CompoundRelatedSetValue;
19 import org.simantics.objmap.graph.annotations.RelatedGetObj;
20 import org.simantics.objmap.graph.annotations.RelatedSetObj;
21 import org.simantics.plant3d.ontology.Plant3D;
22 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
23 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
24 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PointType;
25 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
26 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
30 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
34 public abstract class PipelineComponent extends GeometryNode {
36 private static boolean DEBUG = false;
39 private PipeRun pipeRun;
40 private PipeRun alternativePipeRun;
41 private PipelineComponent next;
42 private PipelineComponent previous;
44 public PipeRun getPipeRun() {
51 * With in-line,turn, and end components, the pipe run is the parent object in the scene-graph.
52 * With nozzles, the pipe run setting is explicit (nozzle has to be linked to the piperun, since the parent object is equipment).
53 * With size change components (in-line), there is also alternative pipe run, which must match the next component's pipe run.
57 public void setPipeRun(PipeRun pipeRun) {
58 if (pipeRun == this.pipeRun)
60 this.pipeRun = pipeRun;
61 if (getControlPoint() != null) {
62 getControlPoint().deattach();
63 if (pipeRun != null) {
64 pipeRun.addChild(getControlPoint());
70 @RelatedGetObj(Plant3D.URIs.HasAlternativePipeRun)
71 public PipeRun getAlternativePipeRun() {
72 return alternativePipeRun;
75 @RelatedSetObj(Plant3D.URIs.HasAlternativePipeRun)
76 public void setAlternativePipeRun(PipeRun pipeRun) {
77 if (this.alternativePipeRun == pipeRun)
79 this.alternativePipeRun = pipeRun;
80 if (getControlPoint().isDualInline()) {
81 PipeControlPoint sub = getControlPoint().getDualSub();
82 if (sub.getParent() != this.alternativePipeRun) {
83 if (this.alternativePipeRun != null) {
84 this.alternativePipeRun.addChild(sub);
85 } else if (sub.getPipeRun() != null) {
86 // FIXME : how to handle child point without proper run?
87 sub.getPipeRun().remChild(sub);
92 firePropertyChanged(Plant3D.URIs.HasAlternativePipeRun);
96 public void updateParameters() {
97 setParameterMap(updateParameterMap());
98 super.updateParameters();
102 @CompoundRelatedGetValue(objRelation=Plant3D.URIs.hasParameter,objType=Plant3D.URIs.Parameter,valRelation=Plant3D.URIs.hasParameterValue)
103 public Map<String, Object> getParameterMap() {
104 return super.getParameterMap();
108 @CompoundRelatedSetValue(Plant3D.URIs.hasParameter)
109 public void setParameterMap(Map<String, Object> parameters) {
110 super.setParameterMap(parameters);
113 @CompoundGetPropertyValue(name="Parameters",tabId="Parameters",value="parameters")
114 public Map<String,Object> getParameterMapUI() {
115 // TODO : how to filter parameters that are calculated by geometry provider?
116 Map<String,Object> map = new HashMap<String, Object>(getParameterMap());
117 map.remove("radius");
118 map.remove("radius2");
119 map.remove("offset");
123 @CompoundSetPropertyValue(value="parameters")
124 public void setParameterMapUI(Map<String, Object> parameters) {
125 Map<String, Object> curr = getParameterMap();
126 for (Entry<String, Object> entry : curr.entrySet()) {
127 if (!parameters.containsKey(entry.getKey()))
128 parameters.put(entry.getKey(), entry.getValue());
130 setParameterMap(parameters);
133 public abstract void setType(String typeURI) throws Exception;
135 @RelatedGetObj(Plant3D.URIs.HasNext)
136 public PipelineComponent getNext() {
140 @RelatedSetObj(Plant3D.URIs.HasNext)
141 public void setNext(PipelineComponent comp) {
144 if (this.next != null)
145 this.next._removeRef(this);
147 this.syncnext = false;
148 if (DEBUG) System.out.println(this + " next " + comp);
150 firePropertyChanged(Plant3D.URIs.HasNext);
155 protected void _setNext(PipelineComponent comp) {
160 @RelatedGetObj(Plant3D.URIs.HasPrevious)
161 public PipelineComponent getPrevious() {
165 @RelatedSetObj(Plant3D.URIs.HasPrevious)
166 public void setPrevious(PipelineComponent comp) {
167 if (previous == comp)
169 if (this.previous != null)
170 this.previous._removeRef(this);
172 this.syncprev = false;
173 if (DEBUG) System.out.println(this + " prev " + comp);
175 firePropertyChanged(Plant3D.URIs.HasPrevious);
180 protected void _setPrevious(PipelineComponent comp) {
181 this.previous = comp;
184 private PipelineComponent branch0;
186 @RelatedGetObj(Plant3D.URIs.HasBranch0)
187 public PipelineComponent getBranch0() {
191 @RelatedSetObj(Plant3D.URIs.HasBranch0)
192 public void setBranch0(PipelineComponent comp) {
195 if (this.branch0 != null)
196 this.branch0._removeRef(this);
198 this.syncbr0 = false;
199 if (DEBUG) System.out.println(this + " br0 " + comp);
201 firePropertyChanged(Plant3D.URIs.HasBranch0);
206 @GetPropertyValue(name="Previous",tabId="Debug",value=Plant3D.URIs.HasPrevious)
207 public String getPreviousDebug() {
208 if (previous == null)
210 return previous.getName();
213 @GetPropertyValue(name="Next",tabId="Debug",value=Plant3D.URIs.HasNext)
214 public String getNextDebug() {
217 return next.getName();
220 @GetPropertyValue(name="Branch0",tabId="Debug",value=Plant3D.URIs.HasBranch0)
221 public String getBR0Debug() {
224 return branch0.getName();
229 private PipeControlPoint getBranchPoint() {
230 PipeControlPoint branchPoint;
231 if (getControlPoint().getChildPoints().size() > 0) {
232 branchPoint = getControlPoint().getChildPoints().get(0);
234 if (branch0.getPipeRun() == null)
236 branchPoint = new PipeControlPoint(this,branch0.getPipeRun());
237 branchPoint.setFixed(false);
238 branchPoint.setType(PointType.END);
239 branchPoint.parent = getControlPoint();
240 getControlPoint().children.add(branchPoint);
241 branchPoint.setWorldOrientation(getControlPoint().getWorldOrientation());
242 branchPoint.setWorldPosition(getControlPoint().getWorldPosition());
247 private boolean _connectNext(PipeControlPoint pcp, PipeControlPoint nextPCP) {
250 if (pcp.getNext() != nextPCP) {
251 pcp.setNext(nextPCP);
253 // if (pcp.isDualInline()) {
254 // PipeControlPoint sub = pcp.getChildPoints().get(0);
255 // if (sub.getNext() != nextPCP)
256 // sub.setNext(nextPCP);
261 private boolean _connectPrev(PipeControlPoint pcp, PipeControlPoint prevPCP) {
264 // if (prevPCP.isDualInline())
265 // prevPCP = prevPCP.getChildPoints().get(0);
266 if (pcp.getPrevious() != prevPCP) {
267 pcp.setPrevious(prevPCP);
269 // if (pcp.isDualInline()) {
270 // PipeControlPoint sub = pcp.getChildPoints().get(0);
271 // if (sub.getPrevious() != prevPCP)
272 // sub.setPrevious(prevPCP);
277 // When link to a component is removed, also link to the other direction must be removed at the same time, or
278 // Control point structure is left into illegal state.
279 private void _removeRef(PipelineComponent comp) {
283 if (DEBUG) System.out.println(this + " remove next " + comp);
284 firePropertyChanged(Plant3D.URIs.HasNext);
286 } else if (previous == comp) {
289 if (DEBUG) System.out.println(this + " remove prev " + comp);
290 firePropertyChanged(Plant3D.URIs.HasPrevious);
292 } else if (branch0 == comp) {
295 if (DEBUG) System.out.println(this + " remove br0 " + comp);
296 firePropertyChanged(Plant3D.URIs.HasBranch0);
301 boolean syncnext = false;
302 private void syncNext() {
305 syncnext = _syncNext();
309 private boolean _syncNext() {
310 PipeControlPoint pcp = getControlPoint();
314 if (next.getControlPoint() != null) {
316 // TODO, relying that the other direction is connected.
317 boolean nxt = next.getPrevious() == this;
318 boolean br0 = next.getBranch0() == this;
320 return _connectNext(pcp, next.getControlPoint());
322 return _connectNext(pcp, next.getBranchPoint());
330 } else if (pcp.getNext() != null) {
340 boolean syncprev = false;
341 private void syncPrevious() {
344 syncprev = _syncPrevious();
347 private boolean _syncPrevious() {
348 PipeControlPoint pcp = getControlPoint();
350 if (previous != null ) {
351 if (previous.getControlPoint() != null) {
353 // TODO, relying that the other direction is connected.
354 boolean prev = previous.getNext() == this;
355 boolean br0 = previous.getBranch0() == this;
357 return _connectPrev(pcp, previous.getControlPoint());
359 return _connectPrev(pcp, previous.getBranchPoint());
367 } else if (pcp.getPrevious() != null) {
368 pcp.setPrevious(null);
377 boolean syncbr0 = false;
378 private void syncBranch0() {
381 syncbr0 = _syncBranch0();
384 private boolean _syncBranch0() {
385 if (getControlPoint() != null) {
386 if (getControlPoint().isDualInline()) {
390 if (branch0 != null) {
391 if (branch0.getControlPoint() != null) {
392 PipeControlPoint branchPoint = getBranchPoint();
393 if (branchPoint == null)
395 PipeControlPoint pcp = branch0.getControlPoint();
396 // TODO, relying that the other direction is connected.
397 boolean next = branch0.getPrevious() == this; // this --> branch0
398 boolean prev = branch0.getNext() == this;
400 _connectNext(branchPoint, pcp);
402 _connectPrev(branchPoint, pcp);
411 } else if (getControlPoint().getChildPoints().size() > 0) { // TODO : this may cause problems? (Removes branch point, before branch has been set?)
412 //getControlPoint().getSubPoint().get(0).remove();
413 //getControlPoint().children.clear();
428 public void sync2() {
429 // if (getControlPoint().isDualInline()) {
430 // PipeControlPoint sub = getControlPoint().getSubPoint().get(0);
431 // next.getControlPoint().getPipeRun().addChild(sub);
433 getControlPoint()._setWorldOrientation(getWorldOrientation());
434 getControlPoint()._setWorldPosition(getWorldPosition());
437 public Map<String,Object> updateParameterMap() {
438 return Collections.emptyMap();
441 public abstract String getType();
442 public abstract PipeControlPoint getControlPoint();
445 public void remove() {
446 if (DEBUG) System.out.println(this + " remove");
447 PipeControlPoint pcp = getControlPoint();
448 // Second check is needed, when remove process is initiated from control point.
449 if (pcp != null && pcp.getPipelineComponent() != null) {
455 public void removeAndSplit() {
456 PipeControlPoint pcp = getControlPoint();
457 // Second check is needed, when remove process is initiated from control point.
458 if (pcp != null && pcp.getPipelineComponent() != null) {
459 pcp.removeAndSplit();
465 protected double[] getColor() {
466 if (getControlPoint() == null || !getControlPoint().isFixed())
467 return new double[]{0.7,0.7,0.7};
469 return new double[]{1.0,0.0,0.0};
473 protected double[] getSelectedColor() {
474 return new double[]{0.5,0,0.5};
478 public void setOrientation(Quat4d orientation) {
479 if (MathTools.equals(orientation, getOrientation()))
481 super.setOrientation(orientation);
482 if (getControlPoint() != null) {
483 getControlPoint()._setWorldOrientation(getWorldOrientation());
484 PipingRules.requestUpdate(getControlPoint());
489 public void setPosition(Vector3d position) {
490 if (MathTools.equals(position, getPosition()))
492 super.setPosition(position);
493 if (getControlPoint() != null) {
494 getControlPoint()._setWorldPosition(getWorldPosition());
495 PipingRules.requestUpdate(getControlPoint());
500 public void _setWorldPosition(Vector3d position) {
501 Vector3d localPos = getLocalPosition(position);
502 super.setPosition(localPos);
505 public void _setWorldOrientation(Quat4d orientation) {
506 Quat4d localOr = getLocalOrientation(orientation);
507 super.setOrientation(localOr);
510 @GetPropertyValue(name="Flow Length", value="flowlength", tabId = "Default")
511 public Double getFlowLength() {
512 PipeControlPoint pcp = getControlPoint();
515 switch (pcp.getType()) {
517 return pcp.getLength();
521 double r = ((TurnComponent)this).getTurnRadius();
522 double a = pcp.getTurnAngle();
531 * Returns diameter of the pipe
534 public Double getDiameter() {
535 return getPipeRun().getPipeDiameter();
539 * Returns secondary diameter of the pipe for size change components
542 public Double getDiameter2() {
543 if (getAlternativePipeRun() == null)
545 return getAlternativePipeRun().getPipeDiameter();
548 public void getEnds(Tuple3d p1, Tuple3d p2) {
549 getControlPoint().getControlPointEnds(p1, p2);
552 public void getEndDirections(Tuple3d v1, Tuple3d v2) {
553 getControlPoint().getEndDirections(v1, v2);
556 public void getCentroid(Tuple3d p) {
557 PipeControlPoint pcp = getControlPoint();
559 throw new IllegalStateException("No centroid defined");
561 switch (pcp.getType()) {
564 // Just return the world location
565 if (!pcp.isSizeChange()) {
566 p.set(pcp.getWorldPosition());
570 // Calculate center of mass for the frustum
571 double r1 = getPipeRun().getPipeDiameter();
572 double r2 = getAlternativePipeRun().getPipeDiameter();
574 Vector3d p1 = new Vector3d(), p2 = new Vector3d();
575 pcp.getInlineControlPointEnds(p1, p2);
577 // Squared sum of radii
578 double r12 = r1 + r2;
581 // The larger of the radii form the base of a frustum
582 double rmax = Math.max(r1, r2);
584 // Relative distance from the base of the frustum
585 double h = (r12 + 2*rmax*rmax) / (4 * (r12 - r1*r2));
587 // Relative distance from p1 to p2
597 Vector3d loc = pcp.getRealPosition(PositionType.PREVIOUS);
599 double r = ((TurnComponent)this).getTurnRadius();
600 double a = pcp.getTurnAngle();
601 double pipeRadius = pcp.getPipeRun().getPipeDiameter() / 2;
603 // Unit vector in inlet flow direction
604 Vector3d inletDir = pcp.getPathLegDirection(Direction.PREVIOUS);
605 inletDir.scale(-1.0);
606 inletDir.normalize();
608 // Normal to both inletDir and turn axis in world coordinates
609 Vector3d outletDir = pcp.getPathLegDirection(Direction.NEXT);
610 Vector3d normal = new Vector3d(inletDir);
611 normal.scaleAdd(-inletDir.dot(outletDir), outletDir);
614 // Location of turn axis
615 Vector3d center = new Vector3d(normal);
616 center.scaleAdd(r, loc);
618 // Add vector components from axis to centroid
619 double c = r + pipeRadius * pipeRadius / (4 * r);
620 double c1 = c * Math.sin(a) / a;
621 double c2 = c * (1 - Math.cos(a)) / a;
625 center.add(inletDir);
632 throw new IllegalStateException("No centroid defined");
636 public double getVolume() {
637 PipeControlPoint pcp = getControlPoint();
639 throw new IllegalStateException("No centroid defined");
641 double pipeRadius = getPipeRun().getPipeDiameter() / 2;
643 switch (pcp.getType()) {
646 if (!pcp.isSizeChange()) {
647 // Just return the cylinder volume
648 return pcp.getLength() * Math.PI * pipeRadius * pipeRadius;
651 // Calculate center of mass for the frustum
652 double r1 = pipeRadius;
653 double r2 = getAlternativePipeRun().getPipeDiameter() / 2;
654 return pcp.getLength() * Math.PI * (r1*r1 + r1*r2 + r2*r2) / 4;
656 double r = ((TurnComponent)this).getTurnRadius();
657 double a = pcp.getTurnAngle();
658 return r * a * Math.PI * pipeRadius * pipeRadius;
661 throw new IllegalStateException("No centroid defined");