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 @CompoundGetPropertyValue(name="Parameters",tabId="Parameters",value="parameters")
97 public Map<String, Object> getParameterMap() {
98 return super.getParameterMap();
102 @CompoundRelatedSetValue(Plant3D.URIs.hasParameter)
103 public void setParameterMap(Map<String, Object> parameters) {
104 super.setParameterMap(parameters);
107 @CompoundGetPropertyValue(name="Parameters",tabId="Parameters",value="parameters")
108 public Map<String,Object> getParameterMapUI() {
109 // TODO : how to filter parameters that are calculated by geometry provider?
110 Map<String,Object> map = new HashMap<String, Object>(getParameterMap());
111 map.remove("radius");
115 @CompoundSetPropertyValue(value="parameters")
116 public void setParameterMapUI(Map<String, Object> parameters) {
117 Map<String, Object> curr = getParameterMap();
118 for (Entry<String, Object> entry : curr.entrySet()) {
119 if (!parameters.containsKey(entry.getKey()))
120 parameters.put(entry.getKey(), entry.getValue());
122 setParameterMap(parameters);
125 public abstract void setType(String typeURI) throws Exception;
127 @RelatedGetObj(Plant3D.URIs.HasNext)
128 public PipelineComponent getNext() {
132 @RelatedSetObj(Plant3D.URIs.HasNext)
133 public void setNext(PipelineComponent comp) {
136 if (this.next != null)
137 this.next._removeRef(this);
139 this.syncnext = false;
140 if (DEBUG) System.out.println(this + " next " + comp);
142 firePropertyChanged(Plant3D.URIs.HasNext);
149 @RelatedGetObj(Plant3D.URIs.HasPrevious)
150 public PipelineComponent getPrevious() {
154 @RelatedSetObj(Plant3D.URIs.HasPrevious)
155 public void setPrevious(PipelineComponent comp) {
156 if (previous == comp)
158 if (this.previous != null)
159 this.previous._removeRef(this);
160 this.previous = comp;
161 this.syncprev = false;
162 if (DEBUG) System.out.println(this + " prev " + comp);
164 firePropertyChanged(Plant3D.URIs.HasPrevious);
169 private PipelineComponent branch0;
171 @RelatedGetObj(Plant3D.URIs.HasBranch0)
172 public PipelineComponent getBranch0() {
176 @RelatedSetObj(Plant3D.URIs.HasBranch0)
177 public void setBranch0(PipelineComponent comp) {
180 if (this.branch0 != null)
181 this.branch0._removeRef(this);
183 this.syncbr0 = false;
184 if (DEBUG) System.out.println(this + " br0 " + comp);
186 firePropertyChanged(Plant3D.URIs.HasBranch0);
191 @GetPropertyValue(name="Previous",tabId="Debug",value=Plant3D.URIs.HasPrevious)
192 public String getPreviousDebug() {
193 if (previous == null)
195 return previous.getName();
198 @GetPropertyValue(name="Next",tabId="Debug",value=Plant3D.URIs.HasNext)
199 public String getNextDebug() {
202 return next.getName();
205 @GetPropertyValue(name="Branch0",tabId="Debug",value=Plant3D.URIs.HasBranch0)
206 public String getBR0Debug() {
209 return branch0.getName();
214 private PipeControlPoint getBranchPoint() {
215 PipeControlPoint branchPoint;
216 if (getControlPoint().getSubPoint().size() > 0) {
217 branchPoint = getControlPoint().getSubPoint().get(0);
219 if (branch0.getPipeRun() == null)
221 branchPoint = new PipeControlPoint(this,branch0.getPipeRun());
222 branchPoint.setFixed(false);
223 branchPoint.setType(PointType.END);
224 branchPoint.parent = getControlPoint();
225 getControlPoint().children.add(branchPoint);
226 branchPoint.setWorldOrientation(getControlPoint().getWorldOrientation());
227 branchPoint.setWorldPosition(getControlPoint().getWorldPosition());
232 private boolean _connectNext(PipeControlPoint pcp, PipeControlPoint nextPCP) {
235 if (pcp.getNext() != nextPCP) {
236 pcp.setNext(nextPCP);
238 if (pcp.isDualInline()) {
239 PipeControlPoint sub = pcp.getSubPoint().get(0);
240 if (sub.getNext() != nextPCP)
241 sub.setNext(nextPCP);
246 private boolean _connectPrev(PipeControlPoint pcp, PipeControlPoint prevPCP) {
249 if (prevPCP.isDualInline())
250 prevPCP = prevPCP.getSubPoint().get(0);
251 if (pcp.getPrevious() != prevPCP) {
252 pcp.setPrevious(prevPCP);
254 if (pcp.isDualInline()) {
255 PipeControlPoint sub = pcp.getSubPoint().get(0);
256 if (sub.getPrevious() != prevPCP)
257 sub.setPrevious(prevPCP);
262 // When link to a component is removed, also link to the other direction must be removed at the same time, or
263 // Control point structure is left into illegal state.
264 private void _removeRef(PipelineComponent comp) {
268 firePropertyChanged(Plant3D.URIs.HasNext);
270 } else if (previous == comp) {
273 firePropertyChanged(Plant3D.URIs.HasPrevious);
275 } else if (branch0 == comp) {
278 firePropertyChanged(Plant3D.URIs.HasBranch0);
283 boolean syncnext = false;
284 private void syncNext() {
287 syncnext = _syncNext();
291 private boolean _syncNext() {
292 PipeControlPoint pcp = getControlPoint();
296 if (next.getControlPoint() != null) {
298 // TODO, relying that the other direction is connected.
299 boolean nxt = next.getPrevious() == this;
300 boolean br0 = next.getBranch0() == this;
302 return _connectNext(pcp, next.getControlPoint());
304 return _connectNext(pcp, next.getBranchPoint());
312 } else if (pcp.getNext() != null) {
322 boolean syncprev = false;
323 private void syncPrevious() {
326 syncprev = _syncPrevious();
329 private boolean _syncPrevious() {
330 PipeControlPoint pcp = getControlPoint();
332 if (previous != null ) {
333 if (previous.getControlPoint() != null) {
335 // TODO, relying that the other direction is connected.
336 boolean prev = previous.getNext() == this;
337 boolean br0 = previous.getBranch0() == this;
339 return _connectPrev(pcp, previous.getControlPoint());
341 return _connectPrev(pcp, previous.getBranchPoint());
349 } else if (pcp.getPrevious() != null) {
350 pcp.setPrevious(null);
359 boolean syncbr0 = false;
360 private void syncBranch0() {
363 syncbr0 = _syncBranch0();
366 private boolean _syncBranch0() {
367 if (getControlPoint() != null) {
368 if (getControlPoint().isDualInline()) {
372 if (branch0 != null) {
373 if (branch0.getControlPoint() != null) {
374 PipeControlPoint branchPoint = getBranchPoint();
375 if (branchPoint == null)
377 PipeControlPoint pcp = branch0.getControlPoint();
378 // TODO, relying that the other direction is connected.
379 boolean next = branch0.getPrevious() == this; // this --> branch0
380 boolean prev = branch0.getNext() == this;
382 _connectNext(branchPoint, pcp);
384 _connectPrev(branchPoint, pcp);
393 } else if (getControlPoint().getSubPoint().size() > 0) { // TODO : this may cause problems? (Removes branch point, before branch has been set?)
394 //getControlPoint().getSubPoint().get(0).remove();
395 //getControlPoint().children.clear();
410 public void sync2() {
411 // if (getControlPoint().isDualInline()) {
412 // PipeControlPoint sub = getControlPoint().getSubPoint().get(0);
413 // next.getControlPoint().getPipeRun().addChild(sub);
415 getControlPoint()._setWorldOrientation(getWorldOrientation());
416 getControlPoint()._setWorldPosition(getWorldPosition());
419 public Map<String,Object> updateParameterMap() {
420 return Collections.EMPTY_MAP;
423 public abstract String getType();
424 public abstract PipeControlPoint getControlPoint();
427 public void remove() {
428 if (DEBUG) System.out.println(this + " remove");
429 PipeControlPoint pcp = getControlPoint();
430 // Second check is needed, when remove process is initiated from control point.
431 if (pcp != null && pcp.getPipelineComponent() != null) {
437 public void removeAndSplit() {
438 PipeControlPoint pcp = getControlPoint();
439 // Second check is needed, when remove process is initiated from control point.
440 if (pcp != null && pcp.getPipelineComponent() != null) {
441 pcp.removeAndSplit();
447 protected double[] getColor() {
448 if (getControlPoint() == null || !getControlPoint().isFixed())
449 return new double[]{0.7,0.7,0.7};
451 return new double[]{1.0,0.0,0.0};
455 protected double[] getSelectedColor() {
456 return new double[]{0.5,0,0.5};
460 public void setOrientation(Quat4d orientation) {
461 if (MathTools.equals(orientation, getOrientation()))
463 super.setOrientation(orientation);
464 if (getControlPoint() != null) {
465 getControlPoint()._setWorldOrientation(getWorldOrientation());
467 PipingRules.requestUpdate(getControlPoint());
468 } catch (Exception e) {
469 // TODO Auto-generated catch block
476 public void setPosition(Vector3d position) {
477 if (MathTools.equals(position, getPosition()))
479 super.setPosition(position);
480 if (getControlPoint() != null) {
481 getControlPoint()._setWorldPosition(getWorldPosition());
483 PipingRules.requestUpdate(getControlPoint());
484 } catch (Exception e) {
485 // TODO Auto-generated catch block
492 public void _setWorldPosition(Vector3d position) {
493 Vector3d localPos = getLocalPosition(position);
494 super.setPosition(localPos);
497 public void _setWorldOrientation(Quat4d orientation) {
498 Quat4d localOr = getLocalOrientation(orientation);
499 super.setOrientation(localOr);
502 @GetPropertyValue(name="Flow Length", value="flowlength", tabId = "Default")
503 public Double getFlowLength() {
504 PipeControlPoint pcp = getControlPoint();
507 switch (pcp.getType()) {
509 return pcp.getLength();
513 double r = getPipeRun().getTurnRadius();
514 double a = pcp.getTurnAngle();
522 public void getEnds(Tuple3d p1, Tuple3d p2) {
523 getControlPoint().getControlPointEnds(p1, p2);
526 public void getEndDirections(Tuple3d v1, Tuple3d v2) {
527 getControlPoint().getEndDirections(v1, v2);
530 public void getCentroid(Tuple3d p) {
531 PipeControlPoint pcp = getControlPoint();
533 throw new IllegalStateException("No centroid defined");
535 switch (pcp.getType()) {
538 // Just return the world location
539 if (!pcp.isSizeChange()) {
540 p.set(pcp.getWorldPosition());
544 // Calculate center of mass for the frustum
545 double r1 = getPipeRun().getPipeDiameter();
546 double r2 = getAlternativePipeRun().getPipeDiameter();
548 Vector3d p1 = new Vector3d(), p2 = new Vector3d();
549 pcp.getInlineControlPointEnds(p1, p2);
551 // Squared sum of radii
552 double r12 = r1 + r2;
555 // The larger of the radii form the base of a frustum
556 double rmax = Math.max(r1, r2);
558 // Relative distance from the base of the frustum
559 double h = (r12 + 2*rmax*rmax) / (4 * (r12 - r1*r2));
561 // Relative distance from p1 to p2
571 Vector3d loc = pcp.getRealPosition(PositionType.PREVIOUS);
573 double r = getPipeRun().getTurnRadius();
574 double a = pcp.getTurnAngle();
575 double pipeRadius = pcp.getPipeRun().getPipeDiameter() / 2;
577 // Unit vector in inlet flow direction
578 Vector3d inletDir = pcp.getPathLegDirection(Direction.PREVIOUS);
579 inletDir.scale(-1.0);
580 inletDir.normalize();
582 // Normal to both inletDir and turn axis in world coordinates
583 Vector3d outletDir = pcp.getPathLegDirection(Direction.NEXT);
584 Vector3d normal = new Vector3d(inletDir);
585 normal.scaleAdd(-inletDir.dot(outletDir), outletDir);
588 // Location of turn axis
589 Vector3d center = new Vector3d(normal);
590 center.scaleAdd(r, loc);
592 // Add vector components from axis to centroid
593 double c = r + pipeRadius * pipeRadius / (4 * r);
594 double c1 = c * Math.sin(a) / a;
595 double c2 = c * (1 - Math.cos(a)) / a;
599 center.add(inletDir);
606 throw new IllegalStateException("No centroid defined");
610 public double getVolume() {
611 PipeControlPoint pcp = getControlPoint();
613 throw new IllegalStateException("No centroid defined");
615 double pipeRadius = getPipeRun().getPipeDiameter() / 2;
617 switch (pcp.getType()) {
620 if (!pcp.isSizeChange()) {
621 // Just return the cylinder volume
622 return pcp.getLength() * Math.PI * pipeRadius * pipeRadius;
625 // Calculate center of mass for the frustum
626 double r1 = pipeRadius;
627 double r2 = getAlternativePipeRun().getPipeDiameter() / 2;
628 return pcp.getLength() * Math.PI * (r1*r1 + r1*r2 + r2*r2) / 4;
630 double r = getPipeRun().getTurnRadius();
631 double a = pcp.getTurnAngle();
632 return r * a * Math.PI * pipeRadius * pipeRadius;
635 throw new IllegalStateException("No centroid defined");