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().getSubPoint().get(0);
82 if (sub.getParent() != this.alternativePipeRun)
83 this.alternativePipeRun.addChild(sub);
85 firePropertyChanged(Plant3D.URIs.HasAlternativePipeRun);
89 public void updateParameters() {
90 setParameterMap(updateParameterMap());
91 super.updateParameters();
95 @CompoundRelatedGetValue(objRelation=Plant3D.URIs.hasParameter,objType=Plant3D.URIs.Parameter,valRelation=Plant3D.URIs.hasParameterValue)
96 public Map<String, Object> getParameterMap() {
97 return super.getParameterMap();
101 @CompoundRelatedSetValue(Plant3D.URIs.hasParameter)
102 public void setParameterMap(Map<String, Object> parameters) {
103 super.setParameterMap(parameters);
106 @CompoundGetPropertyValue(name="Parameters",tabId="Parameters",value="parameters")
107 public Map<String,Object> getParameterMapUI() {
108 // TODO : how to filter parameters that are calculated by geometry provider?
109 Map<String,Object> map = new HashMap<String, Object>(getParameterMap());
110 map.remove("radius");
111 map.remove("radius2");
112 map.remove("offset");
116 @CompoundSetPropertyValue(value="parameters")
117 public void setParameterMapUI(Map<String, Object> parameters) {
118 Map<String, Object> curr = getParameterMap();
119 for (Entry<String, Object> entry : curr.entrySet()) {
120 if (!parameters.containsKey(entry.getKey()))
121 parameters.put(entry.getKey(), entry.getValue());
123 setParameterMap(parameters);
126 public abstract void setType(String typeURI) throws Exception;
128 @RelatedGetObj(Plant3D.URIs.HasNext)
129 public PipelineComponent getNext() {
133 @RelatedSetObj(Plant3D.URIs.HasNext)
134 public void setNext(PipelineComponent comp) {
137 if (this.next != null)
138 this.next._removeRef(this);
140 this.syncnext = false;
141 if (DEBUG) System.out.println(this + " next " + comp);
143 firePropertyChanged(Plant3D.URIs.HasNext);
148 protected void _setNext(PipelineComponent comp) {
153 @RelatedGetObj(Plant3D.URIs.HasPrevious)
154 public PipelineComponent getPrevious() {
158 @RelatedSetObj(Plant3D.URIs.HasPrevious)
159 public void setPrevious(PipelineComponent comp) {
160 if (previous == comp)
162 if (this.previous != null)
163 this.previous._removeRef(this);
165 this.syncprev = false;
166 if (DEBUG) System.out.println(this + " prev " + comp);
168 firePropertyChanged(Plant3D.URIs.HasPrevious);
173 protected void _setPrevious(PipelineComponent comp) {
174 this.previous = comp;
177 private PipelineComponent branch0;
179 @RelatedGetObj(Plant3D.URIs.HasBranch0)
180 public PipelineComponent getBranch0() {
184 @RelatedSetObj(Plant3D.URIs.HasBranch0)
185 public void setBranch0(PipelineComponent comp) {
188 if (this.branch0 != null)
189 this.branch0._removeRef(this);
191 this.syncbr0 = false;
192 if (DEBUG) System.out.println(this + " br0 " + comp);
194 firePropertyChanged(Plant3D.URIs.HasBranch0);
199 @GetPropertyValue(name="Previous",tabId="Debug",value=Plant3D.URIs.HasPrevious)
200 public String getPreviousDebug() {
201 if (previous == null)
203 return previous.getName();
206 @GetPropertyValue(name="Next",tabId="Debug",value=Plant3D.URIs.HasNext)
207 public String getNextDebug() {
210 return next.getName();
213 @GetPropertyValue(name="Branch0",tabId="Debug",value=Plant3D.URIs.HasBranch0)
214 public String getBR0Debug() {
217 return branch0.getName();
222 private PipeControlPoint getBranchPoint() {
223 PipeControlPoint branchPoint;
224 if (getControlPoint().getSubPoint().size() > 0) {
225 branchPoint = getControlPoint().getSubPoint().get(0);
227 if (branch0.getPipeRun() == null)
229 branchPoint = new PipeControlPoint(this,branch0.getPipeRun());
230 branchPoint.setFixed(false);
231 branchPoint.setType(PointType.END);
232 branchPoint.parent = getControlPoint();
233 getControlPoint().children.add(branchPoint);
234 branchPoint.setWorldOrientation(getControlPoint().getWorldOrientation());
235 branchPoint.setWorldPosition(getControlPoint().getWorldPosition());
240 private boolean _connectNext(PipeControlPoint pcp, PipeControlPoint nextPCP) {
243 if (pcp.getNext() != nextPCP) {
244 pcp.setNext(nextPCP);
246 if (pcp.isDualInline()) {
247 PipeControlPoint sub = pcp.getSubPoint().get(0);
248 if (sub.getNext() != nextPCP)
249 sub.setNext(nextPCP);
254 private boolean _connectPrev(PipeControlPoint pcp, PipeControlPoint prevPCP) {
257 if (prevPCP.isDualInline())
258 prevPCP = prevPCP.getSubPoint().get(0);
259 if (pcp.getPrevious() != prevPCP) {
260 pcp.setPrevious(prevPCP);
262 if (pcp.isDualInline()) {
263 PipeControlPoint sub = pcp.getSubPoint().get(0);
264 if (sub.getPrevious() != prevPCP)
265 sub.setPrevious(prevPCP);
270 // When link to a component is removed, also link to the other direction must be removed at the same time, or
271 // Control point structure is left into illegal state.
272 private void _removeRef(PipelineComponent comp) {
276 if (DEBUG) System.out.println(this + " remove next " + comp);
277 firePropertyChanged(Plant3D.URIs.HasNext);
279 } else if (previous == comp) {
282 if (DEBUG) System.out.println(this + " remove prev " + comp);
283 firePropertyChanged(Plant3D.URIs.HasPrevious);
285 } else if (branch0 == comp) {
288 if (DEBUG) System.out.println(this + " remove br0 " + comp);
289 firePropertyChanged(Plant3D.URIs.HasBranch0);
294 boolean syncnext = false;
295 private void syncNext() {
298 syncnext = _syncNext();
302 private boolean _syncNext() {
303 PipeControlPoint pcp = getControlPoint();
307 if (next.getControlPoint() != null) {
309 // TODO, relying that the other direction is connected.
310 boolean nxt = next.getPrevious() == this;
311 boolean br0 = next.getBranch0() == this;
313 return _connectNext(pcp, next.getControlPoint());
315 return _connectNext(pcp, next.getBranchPoint());
323 } else if (pcp.getNext() != null) {
333 boolean syncprev = false;
334 private void syncPrevious() {
337 syncprev = _syncPrevious();
340 private boolean _syncPrevious() {
341 PipeControlPoint pcp = getControlPoint();
343 if (previous != null ) {
344 if (previous.getControlPoint() != null) {
346 // TODO, relying that the other direction is connected.
347 boolean prev = previous.getNext() == this;
348 boolean br0 = previous.getBranch0() == this;
350 return _connectPrev(pcp, previous.getControlPoint());
352 return _connectPrev(pcp, previous.getBranchPoint());
360 } else if (pcp.getPrevious() != null) {
361 pcp.setPrevious(null);
370 boolean syncbr0 = false;
371 private void syncBranch0() {
374 syncbr0 = _syncBranch0();
377 private boolean _syncBranch0() {
378 if (getControlPoint() != null) {
379 if (getControlPoint().isDualInline()) {
383 if (branch0 != null) {
384 if (branch0.getControlPoint() != null) {
385 PipeControlPoint branchPoint = getBranchPoint();
386 if (branchPoint == null)
388 PipeControlPoint pcp = branch0.getControlPoint();
389 // TODO, relying that the other direction is connected.
390 boolean next = branch0.getPrevious() == this; // this --> branch0
391 boolean prev = branch0.getNext() == this;
393 _connectNext(branchPoint, pcp);
395 _connectPrev(branchPoint, pcp);
404 } else if (getControlPoint().getSubPoint().size() > 0) { // TODO : this may cause problems? (Removes branch point, before branch has been set?)
405 //getControlPoint().getSubPoint().get(0).remove();
406 //getControlPoint().children.clear();
421 public void sync2() {
422 // if (getControlPoint().isDualInline()) {
423 // PipeControlPoint sub = getControlPoint().getSubPoint().get(0);
424 // next.getControlPoint().getPipeRun().addChild(sub);
426 getControlPoint()._setWorldOrientation(getWorldOrientation());
427 getControlPoint()._setWorldPosition(getWorldPosition());
430 public Map<String,Object> updateParameterMap() {
431 return Collections.EMPTY_MAP;
434 public abstract String getType();
435 public abstract PipeControlPoint getControlPoint();
438 public void remove() {
439 if (DEBUG) System.out.println(this + " remove");
440 PipeControlPoint pcp = getControlPoint();
441 // Second check is needed, when remove process is initiated from control point.
442 if (pcp != null && pcp.getPipelineComponent() != null) {
448 public void removeAndSplit() {
449 PipeControlPoint pcp = getControlPoint();
450 // Second check is needed, when remove process is initiated from control point.
451 if (pcp != null && pcp.getPipelineComponent() != null) {
452 pcp.removeAndSplit();
458 protected double[] getColor() {
459 if (getControlPoint() == null || !getControlPoint().isFixed())
460 return new double[]{0.7,0.7,0.7};
462 return new double[]{1.0,0.0,0.0};
466 protected double[] getSelectedColor() {
467 return new double[]{0.5,0,0.5};
471 public void setOrientation(Quat4d orientation) {
472 if (MathTools.equals(orientation, getOrientation()))
474 super.setOrientation(orientation);
475 if (getControlPoint() != null) {
476 getControlPoint()._setWorldOrientation(getWorldOrientation());
477 PipingRules.requestUpdate(getControlPoint());
482 public void setPosition(Vector3d position) {
483 if (MathTools.equals(position, getPosition()))
485 super.setPosition(position);
486 if (getControlPoint() != null) {
487 getControlPoint()._setWorldPosition(getWorldPosition());
488 PipingRules.requestUpdate(getControlPoint());
493 public void _setWorldPosition(Vector3d position) {
494 Vector3d localPos = getLocalPosition(position);
495 super.setPosition(localPos);
498 public void _setWorldOrientation(Quat4d orientation) {
499 Quat4d localOr = getLocalOrientation(orientation);
500 super.setOrientation(localOr);
503 @GetPropertyValue(name="Flow Length", value="flowlength", tabId = "Default")
504 public Double getFlowLength() {
505 PipeControlPoint pcp = getControlPoint();
508 switch (pcp.getType()) {
510 return pcp.getLength();
514 double r = ((TurnComponent)this).getTurnRadius();
515 double a = pcp.getTurnAngle();
524 * Returns diameter of the pipe
527 public Double getDiameter() {
528 return getPipeRun().getPipeDiameter();
532 * Returns secondary diameter of the pipe for size change components
535 public Double getDiameter2() {
536 if (getAlternativePipeRun() == null)
538 return getAlternativePipeRun().getPipeDiameter();
541 public void getEnds(Tuple3d p1, Tuple3d p2) {
542 getControlPoint().getControlPointEnds(p1, p2);
545 public void getEndDirections(Tuple3d v1, Tuple3d v2) {
546 getControlPoint().getEndDirections(v1, v2);
549 public void getCentroid(Tuple3d p) {
550 PipeControlPoint pcp = getControlPoint();
552 throw new IllegalStateException("No centroid defined");
554 switch (pcp.getType()) {
557 // Just return the world location
558 if (!pcp.isSizeChange()) {
559 p.set(pcp.getWorldPosition());
563 // Calculate center of mass for the frustum
564 double r1 = getPipeRun().getPipeDiameter();
565 double r2 = getAlternativePipeRun().getPipeDiameter();
567 Vector3d p1 = new Vector3d(), p2 = new Vector3d();
568 pcp.getInlineControlPointEnds(p1, p2);
570 // Squared sum of radii
571 double r12 = r1 + r2;
574 // The larger of the radii form the base of a frustum
575 double rmax = Math.max(r1, r2);
577 // Relative distance from the base of the frustum
578 double h = (r12 + 2*rmax*rmax) / (4 * (r12 - r1*r2));
580 // Relative distance from p1 to p2
590 Vector3d loc = pcp.getRealPosition(PositionType.PREVIOUS);
592 double r = ((TurnComponent)this).getTurnRadius();
593 double a = pcp.getTurnAngle();
594 double pipeRadius = pcp.getPipeRun().getPipeDiameter() / 2;
596 // Unit vector in inlet flow direction
597 Vector3d inletDir = pcp.getPathLegDirection(Direction.PREVIOUS);
598 inletDir.scale(-1.0);
599 inletDir.normalize();
601 // Normal to both inletDir and turn axis in world coordinates
602 Vector3d outletDir = pcp.getPathLegDirection(Direction.NEXT);
603 Vector3d normal = new Vector3d(inletDir);
604 normal.scaleAdd(-inletDir.dot(outletDir), outletDir);
607 // Location of turn axis
608 Vector3d center = new Vector3d(normal);
609 center.scaleAdd(r, loc);
611 // Add vector components from axis to centroid
612 double c = r + pipeRadius * pipeRadius / (4 * r);
613 double c1 = c * Math.sin(a) / a;
614 double c2 = c * (1 - Math.cos(a)) / a;
618 center.add(inletDir);
625 throw new IllegalStateException("No centroid defined");
629 public double getVolume() {
630 PipeControlPoint pcp = getControlPoint();
632 throw new IllegalStateException("No centroid defined");
634 double pipeRadius = getPipeRun().getPipeDiameter() / 2;
636 switch (pcp.getType()) {
639 if (!pcp.isSizeChange()) {
640 // Just return the cylinder volume
641 return pcp.getLength() * Math.PI * pipeRadius * pipeRadius;
644 // Calculate center of mass for the frustum
645 double r1 = pipeRadius;
646 double r2 = getAlternativePipeRun().getPipeDiameter() / 2;
647 return pcp.getLength() * Math.PI * (r1*r1 + r1*r2 + r2*r2) / 4;
649 double r = ((TurnComponent)this).getTurnRadius();
650 double a = pcp.getTurnAngle();
651 return r * a * Math.PI * pipeRadius * pipeRadius;
654 throw new IllegalStateException("No centroid defined");