1 package org.simantics.plant3d.utils;
3 import java.util.ArrayList;
4 import java.util.HashMap;
8 import javax.vecmath.Vector3d;
10 import org.simantics.Simantics;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.request.ReadRequest;
14 import org.simantics.db.common.utils.NameUtils;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.g3d.math.MathTools;
17 import org.simantics.g3d.scenegraph.GeometryProvider;
18 import org.simantics.g3d.scenegraph.ParametricGeometryProvider;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.plant3d.geometry.ParameterRead;
21 import org.simantics.plant3d.ontology.Plant3D;
22 import org.simantics.plant3d.scenegraph.EndComponent;
23 import org.simantics.plant3d.scenegraph.Equipment;
24 import org.simantics.plant3d.scenegraph.InlineComponent;
25 import org.simantics.plant3d.scenegraph.Nozzle;
26 import org.simantics.plant3d.scenegraph.P3DRootNode;
27 import org.simantics.plant3d.scenegraph.PipeRun;
28 import org.simantics.plant3d.scenegraph.PipelineComponent;
29 import org.simantics.plant3d.scenegraph.TurnComponent;
30 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
31 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
32 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.PositionType;
33 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
35 public class ComponentUtils {
38 private static Map<String,Class<? extends PipelineComponent>> clazzes = new HashMap<String, Class<? extends PipelineComponent>>();
39 private static Map<String,GeometryProvider> providers = new HashMap<String,GeometryProvider>();
40 private static Map<String,String> names = new HashMap<String,String>();
42 public static void preloadCache() {
43 Simantics.getSession().asyncRequest(new ReadRequest() {
46 public void run(ReadGraph graph) throws DatabaseException {
47 List<String> types = new ArrayList<String>();
48 types.add(Plant3D.URIs.Builtin_Straight);
49 types.add(Plant3D.URIs.Builtin_Elbow);
50 types.add(Plant3D.URIs.Builtin_ConcentricReducer);
51 types.add(Plant3D.URIs.Builtin_BranchSplitComponent);
52 types.add(Plant3D.URIs.Builtin_EccentricReducer);
53 types.add(Plant3D.URIs.Builtin_Elbow45);
54 types.add(Plant3D.URIs.Builtin_Elbow90);
56 for (String typeURI : types) {
63 private static GeometryProvider getProvider(ReadGraph graph, Resource type) throws DatabaseException {
65 Layer0 l0 = Layer0.getInstance(graph);
66 Plant3D p3d = Plant3D.getInstance(graph);
67 Resource geom = graph.getPossibleObject(type,p3d.hasGeometry);
69 for (Resource a : graph.getObjects(type, l0.Asserts)) {
70 if (p3d.hasGeometry.equals(graph.getPossibleObject(a, l0.HasPredicate))) {
71 geom = graph.getPossibleObject(a, l0.HasObject);
77 GeometryProvider provider = graph.adapt(geom, GeometryProvider.class);
78 if (provider instanceof ParametricGeometryProvider) {
79 Map<String,Object> params = graph.syncRequest(new ParameterRead(type));
80 if (params.size() > 0)
81 ((ParametricGeometryProvider)provider).setProperties(params);
88 private static Class<? extends PipelineComponent> getClazz(ReadGraph graph, Resource type) throws DatabaseException {
89 Plant3D p3d = Plant3D.getInstance(graph);
90 if (graph.isInheritedFrom(type, p3d.InlineComponent))
91 return InlineComponent.class;
92 if (graph.isInheritedFrom(type, p3d.TurnComponent))
93 return TurnComponent.class;
94 if (graph.isInheritedFrom(type, p3d.EndComponent))
95 return EndComponent.class;
96 if (graph.isInheritedFrom(type, p3d.Nozzle))
101 private static void load(ReadGraph graph, String typeURI) throws DatabaseException {
102 Plant3D p3d = Plant3D.getInstance(graph);
103 Resource type = graph.getResource(typeURI);
105 GeometryProvider provider = getProvider(graph, type);
106 if (provider != null || graph.hasStatement(type,p3d.NonVisibleComponent)) {
107 providers.put(typeURI, provider);
108 if (graph.isInheritedFrom(type, p3d.PipelineComponent))
109 clazzes.put(typeURI,getClazz(graph, type));
110 names.put(typeURI, NameUtils.getSafeName(graph, type));
113 throw new DatabaseException("Cannot find component for " + typeURI);
116 private static void load(final String typeURI) throws DatabaseException {
117 Simantics.getSession().syncRequest(new ReadRequest() {
120 public void run(ReadGraph graph) throws DatabaseException {
127 * Creates a component
129 * Does not set the name or add the component to a piperun.
135 public static PipelineComponent createComponent(P3DRootNode root, String typeURI) throws Exception {
136 Class<? extends PipelineComponent> type = clazzes.get(typeURI);
137 GeometryProvider provider = providers.get(typeURI);
138 if (type == null || provider == null) {
140 type = clazzes.get(typeURI);
141 provider = providers.get(typeURI);
143 //PipelineComponent component = type.newInstance();
144 PipelineComponent component = null;
145 if (type == InlineComponent.class) {
146 component = root.createInline();
147 } else if (type == TurnComponent.class) {
148 component = root.createTurn();
149 } else if (type == EndComponent.class) {
150 component = root.createTurn();
151 } else if (type == Nozzle.class) {
152 component = root.createNozzle();
154 component.setType(typeURI);
155 component.setGeometry(provider);
160 * Creates a equipment
162 * Does not set the name
170 public static Equipment createEquipment(P3DRootNode root, String typeURI) throws Exception {
171 GeometryProvider provider = providers.get(typeURI);
172 if (provider == null) {
174 provider = providers.get(typeURI);
176 Equipment equipment = root.createEquipment();
177 equipment.setType(typeURI);
178 equipment.setGeometry(provider);
179 root.addChild(equipment);
183 public static InlineComponent createStraight(P3DRootNode root) throws Exception{
184 InlineComponent component = root.createInline();
185 component.setType(Plant3D.URIs.Builtin_Straight);
186 component.setGeometry(providers.get(Plant3D.URIs.Builtin_Straight));
190 public static TurnComponent createTurn(P3DRootNode root) throws Exception {
191 TurnComponent elbow = root.createTurn();
192 elbow.setType(Plant3D.URIs.Builtin_Elbow);
193 elbow.setGeometry(providers.get(Plant3D.URIs.Builtin_Elbow));
197 public static InlineComponent createReducer(P3DRootNode root) throws Exception {
198 InlineComponent component = root.createInline();
199 component.setType(Plant3D.URIs.Builtin_ConcentricReducer);
200 component.setGeometry(providers.get(Plant3D.URIs.Builtin_ConcentricReducer));
204 public static InlineComponent createBranchSplit(P3DRootNode root) throws Exception {
205 InlineComponent component = root.createInline();
206 component.setType(Plant3D.URIs.Builtin_BranchSplitComponent);
210 public static Equipment createEquipment(P3DRootNode root, Item equipmentType) throws Exception {
211 Equipment equipment = createEquipment(root, equipmentType.getUri());
212 String n = root.getUniqueName(equipmentType.getName());
213 equipment.setName(n);
219 public static Nozzle createDefaultNozzle(P3DRootNode root, Equipment equipment) throws Exception {
220 return createNozzle(root, equipment, new Item(Plant3D.URIs.Builtin_Nozzle, "Nozzle"));
223 public static Nozzle createNozzle(P3DRootNode root, Equipment equipment, Item nozzleType) throws Exception {
224 Nozzle nozzle = root.createNozzle();
225 nozzle.setType(nozzleType.getUri());
226 String n = root.getUniqueName(nozzleType.getName());
228 PipeRun pipeRun = new PipeRun();
229 n = root.getUniqueName("PipeRun");
231 nozzle.setPipeRun(pipeRun);
233 equipment.addChild(nozzle);
234 root.addChild(pipeRun);
235 // root.getNodeMap().commit("Add nozzle " + n);
239 public static class InsertInstruction {
240 public String typeUri;
242 public PositionType position = PositionType.NEXT;
243 public PositionType insertPosition = PositionType.NEXT;
245 // Reducer requires pipe specs
246 public Double diameter;
247 public Double turnRadius;
250 public Double length;
255 public String getTypeUri() {
259 public void setTypeUri(String typeUri) {
260 this.typeUri = typeUri;
263 public PositionType getPosition() {
267 public void setPosition(PositionType position) {
268 this.position = position;
271 public PositionType getInsertPosition() {
272 return insertPosition;
275 public void setInsertPosition(PositionType insertPosition) {
276 this.insertPosition = insertPosition;
279 public Double getDiameter() {
283 public void setDiameter(Double diameter) {
284 this.diameter = diameter;
287 public Double getTurnRadius() {
291 public void setTurnRadius(Double turnRadius) {
292 this.turnRadius = turnRadius;
295 public Double getLength() {
299 public void setLength(Double length) {
300 this.length = length;
303 public Double getAngle() {
307 public void setAngle(Double angle) {
313 public static PipelineComponent addComponent(P3DRootNode root, PipelineComponent component, InsertInstruction inst) throws Exception {
315 PipelineComponent newComponent = ComponentUtils.createComponent(root, inst.typeUri);
316 PipeControlPoint newPcp = newComponent.getControlPoint();
318 PipeControlPoint toPcp = component.getControlPoint();
319 PipeRun pipeRun = toPcp.getPipeRun();
321 String typeName = names.get(inst.typeUri);
322 if (typeName == null)
323 typeName = "Component";
328 PositionType position = inst.position;
329 PositionType insertPosition = inst.insertPosition;
330 boolean lengthAdjustable = false;
331 if (newComponent instanceof InlineComponent) {
332 lengthAdjustable = ((InlineComponent)newComponent).isVariableLength() || ((InlineComponent)newComponent).isModifialble();
334 boolean insertAdjustable = false;
335 if (component instanceof InlineComponent) {
336 insertAdjustable = ((InlineComponent)component).isVariableLength();
338 boolean sizeChange = false;
339 if (newComponent instanceof InlineComponent) {
340 sizeChange = ((InlineComponent)newComponent).isSizeChange();
343 if (toPcp.isInline()) {
346 if (toPcp.isDualInline()) {
347 toPcp = toPcp.getDualSub();
348 pipeRun = toPcp.getPipeRun();
353 if (toPcp.isDualSub()) {
354 toPcp = toPcp.parent;
355 pipeRun = toPcp.getPipeRun();
361 Vector3d start = new Vector3d();
362 Vector3d end = new Vector3d();
363 dir = new Vector3d();
364 toPcp.getInlineControlPointEnds(start, end, dir);
368 pos = new Vector3d(end);
371 pos = new Vector3d(start);
374 pos = new Vector3d(toPcp.getWorldPosition());
380 } else if (toPcp.isDirected()) {
381 dir = new Vector3d(toPcp.getDirection(Direction.NEXT));
382 pos = new Vector3d(toPcp.getWorldPosition());
383 } else if (toPcp.isTurn() && toPcp.asFixedAngle()) {
384 dir = new Vector3d(toPcp.getDirection(position == PositionType.NEXT ? Direction.NEXT : Direction.PREVIOUS));
385 pos = new Vector3d(toPcp.getWorldPosition());
386 if (!lengthAdjustable) {
387 Vector3d v = new Vector3d(dir);
388 v.scale(toPcp.getInlineLength());
391 if (insertPosition == PositionType.NEXT) {
392 Vector3d v = new Vector3d(dir);
393 v.scale(toPcp.getInlineLength());
395 } else if (insertPosition == PositionType.SPLIT) {
396 // scale 0.5*length so that we don't remove the length twice from the new component
397 Vector3d v = new Vector3d(dir);
398 v.scale(toPcp.getInlineLength()*0.5);
406 String name = component.getPipeRun().getUniqueName(typeName);
407 newComponent.setName(name);
409 pipeRun.addChild(newComponent);
411 if (newComponent instanceof InlineComponent && ((InlineComponent)newComponent).isVariableLength() ||((InlineComponent)newComponent).isModifialble()) {
412 newPcp.setLength(inst.length);
413 newComponent.setParameter("length", inst.length);
414 } else if (newComponent instanceof TurnComponent && ((TurnComponent)newComponent).isVariableAngle()) {
415 newPcp.setTurnAngle(inst.angle);
416 newComponent.setParameter("turnAngle", inst.angle);
419 newComponent.updateParameters();
421 Vector3d v = new Vector3d(dir);
422 if (insertAdjustable) {
423 if (insertPosition == PositionType.NEXT)
424 v.scale(newComponent.getControlPoint().getInlineLength());
425 else if (insertPosition == PositionType.SPLIT)
427 else if (insertPosition == PositionType.PREVIOUS)
428 v.scale(-newComponent.getControlPoint().getInlineLength());
430 v.scale(newComponent.getControlPoint().getInlineLength());
447 if (toPcp.isDualInline())
448 toPcp = toPcp.getDualSub();
449 newPcp.insert(toPcp, Direction.NEXT);
450 newPcp.setWorldPosition(pos);
453 if (toPcp.isDualSub())
454 toPcp = toPcp.parent;
455 newPcp.insert(toPcp, Direction.PREVIOUS);
456 newPcp.setWorldPosition(pos);
459 PipingRules.splitVariableLengthComponent(newComponent, (InlineComponent)component, true);
464 PipeRun other = new PipeRun();
465 String n = root.getUniqueName("PipeRun");
467 other.setPipeDiameter(inst.diameter);
468 other.setTurnRadius(inst.turnRadius);
469 root.addChild(other);
472 if (position == PositionType.NEXT) {
473 PipingRules.addSizeChange(false, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
474 } else if (position == PositionType.PREVIOUS){
475 PipingRules.addSizeChange(true, pipeRun, other, (InlineComponent)newComponent, toPcp, null);
477 newPcp.setWorldPosition(pos);
478 // TODO : chicken-egg problem
479 newComponent.updateParameters();
480 Vector3d v = new Vector3d(dir);
481 v.scale(newComponent.getControlPoint().getLength()*0.5);
494 newPcp.setWorldPosition(pos);
501 public static boolean connect(PipelineComponent current, PipelineComponent endTo) throws Exception {
502 return connect(current, endTo, null, null);
506 * Connects component to another component
514 public static boolean connect(PipelineComponent current, PipelineComponent endTo, PositionType endType, Vector3d position) throws Exception{
515 PipeControlPoint endCP = endTo.getControlPoint();
517 if (current.getNext() == null)
519 else if (current.getPrevious() == null)
524 PipeRun pipeRun = current.getPipeRun();
525 P3DRootNode root = (P3DRootNode)current.getRootNode();
526 PipeControlPoint currentCP = current.getControlPoint();
528 if (endType == null || endType == PositionType.NEXT || endType == PositionType.PREVIOUS) {
532 boolean requiresReverse = false;
533 if (!reversed && endCP.getPrevious() != null) {
534 if (endCP.getNext() != null)
536 requiresReverse = true;
537 } else if (reversed && endCP.getNext() != null) {
538 if (endCP.getPrevious() != null)
540 requiresReverse = true;
542 PipeRun other = endCP.getPipeRun();
543 boolean mergeRuns = other == null ? true : pipeRun.canMerge(other);
545 if (requiresReverse) {
546 // Pipe line must be traversible with next/previous relations without direction change.
547 // Now the component, where we are connecting the created pipeline is defined in different order.
548 PipingRules.reverse(other);
553 // Runs have compatible specs and must be merged
554 if (other != null && pipeRun != other)
555 pipeRun.merge(other);
556 else if (other == null) {
557 if (!(endTo instanceof Nozzle)) {
558 pipeRun.addChild(endTo);
560 endTo.setPipeRun(pipeRun);
564 currentCP.setNext(endCP);
565 endCP.setPrevious(currentCP);
567 currentCP.setPrevious(endCP);
568 endCP.setNext(currentCP);
571 // Runs do not have compatible specs, and a reducer must be attached in between.
572 InlineComponent reducer = ComponentUtils.createReducer(root);
573 PipeControlPoint pcp = reducer.getControlPoint();
575 Vector3d endPos = endCP.getWorldPosition();
576 Vector3d currentPos = currentCP.getWorldPosition();
577 Vector3d v = new Vector3d(endPos);
582 PipingRules.addSizeChange(reversed, pipeRun, other, reducer, currentCP, endCP);
584 pcp.setWorldPosition(v);
585 reducer.updateParameters();
587 PipingRules.positionUpdate(endCP);
590 } else if (endType == PositionType.SPLIT) {
591 InlineComponent branchSplit = createBranchSplit((InlineComponent)endTo, position);
592 if (branchSplit == null)
594 PipeControlPoint branchSplitCP = branchSplit.getControlPoint();
595 PipeControlPoint pcp = new PipeControlPoint(branchSplit,pipeRun);
596 branchSplitCP.children.add(pcp);
597 pcp.parent = branchSplitCP;
598 pcp.setWorldOrientation(branchSplitCP.getWorldOrientation());
599 pcp.setWorldPosition(branchSplitCP.getWorldPosition());
603 pcp.setPrevious(currentCP);
604 currentCP.setNext(pcp);
606 pcp.setNext(currentCP);
607 currentCP.setPrevious(pcp);
609 PipingRules.positionUpdate(endCP);
615 public static InlineComponent createBranchSplit(InlineComponent component, Vector3d pos) throws Exception{
616 if (!component.isVariableLength())
618 PipeRun pipeRun = component.getPipeRun();
619 Vector3d sStart = new Vector3d();
620 Vector3d sEnd = new Vector3d();
621 component.getControlPoint().getInlineControlPointEnds(sStart, sEnd);
623 if (MathTools.distance(sStart, sEnd) < (pipeRun.getPipeDiameter()*0.5))
627 Vector3d p = MathTools.closestPointOnEdge(new Vector3d(pos), sStart, sEnd);
629 Vector3d v = new Vector3d(sEnd);
632 v.scale(component.getPipeRun().getPipeDiameter()*0.5);
634 } else if (p == sEnd) {
635 Vector3d v = new Vector3d(sStart);
638 v.scale(component.getPipeRun().getPipeDiameter()*0.5);
642 P3DRootNode root = (P3DRootNode)component.getRootNode();
643 InlineComponent branchSplit = ComponentUtils.createBranchSplit(root);
644 String branchName = component.getPipeRun().getUniqueName("Branch");
645 branchSplit.setName(branchName);
646 component.getPipeRun().addChild(branchSplit);
647 PipeControlPoint branchSplitCP = branchSplit.getControlPoint();
648 branchSplitCP.setWorldPosition(p);
649 PipingRules.splitVariableLengthComponent(branchSplit, component, false);