]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java
Use unique names at the model scope instead of pipe run
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / P3DRootNode.java
1 package org.simantics.plant3d.scenegraph;
2
3 import java.util.Collection;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import javax.vecmath.Quat4d;
8 import javax.vecmath.Vector3d;
9
10 import org.simantics.db.Resource;
11 import org.simantics.g3d.math.MathTools;
12 import org.simantics.g3d.scenegraph.IG3DNode;
13 import org.simantics.g3d.scenegraph.NodeMap;
14 import org.simantics.g3d.scenegraph.NodeMapProvider;
15 import org.simantics.g3d.scenegraph.base.INode;
16 import org.simantics.g3d.scenegraph.base.NodeException;
17 import org.simantics.g3d.scenegraph.base.ParentNode;
18 import org.simantics.objmap.graph.IMapping;
19 import org.simantics.objmap.graph.annotations.GraphType;
20 import org.simantics.objmap.graph.annotations.RelatedElementsAdd;
21 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
22 import org.simantics.objmap.graph.annotations.RelatedElementsRem;
23 import org.simantics.plant3d.ontology.Plant3D;
24
25 import vtk.vtkProp;
26
27 @GraphType(Plant3D.URIs.Plant)
28 public class P3DRootNode extends ParentNode<INode> implements IG3DNode, NodeMapProvider<Resource, vtkProp, INode> {
29         
30         // Vertical direction that determines the interpretation of rotation angle origin
31         protected Vector3d upVector = new Vector3d(0.0, 1.0, 0.0);
32
33         @RelatedElementsAdd(Plant3D.URIs.children)
34         public void addChild(INode node) {
35         //public void addChild(IP3DVisualNode node) {
36                 addNode(Plant3D.URIs.children,node);
37         }
38         
39         @RelatedElementsGet(Plant3D.URIs.children)
40         public Collection<INode> getChild() {
41                 return getNodes(Plant3D.URIs.children);
42         }
43         
44         @RelatedElementsRem(Plant3D.URIs.children)
45         public void remChild(INode node) {
46         //public void remChild(IP3DNode node) {
47                 removeNode(Plant3D.URIs.children, node);
48         }
49         
50         private NodeMap<Resource, vtkProp, INode> nodeMap;
51         private IMapping<Resource, INode> mapping;
52         
53         public void setNodeMap(NodeMap<Resource, vtkProp, INode> nodeMap) {
54                 this.nodeMap = nodeMap;
55                 this.mapping = nodeMap.getMapping();
56         }
57         
58         public void setMapping(IMapping<Resource, INode> mapping) {
59                 this.mapping = mapping;
60         }
61         
62         @Override
63         public NodeMap<Resource, vtkProp, INode> getNodeMap() {
64                 return nodeMap;
65         }
66         
67         public Resource getNodeResource(INode node) {
68                 return mapping.inverseGet(node);
69         }
70         
71         public INode getResourceNode(Resource r) {
72                 return mapping.get(r);
73         }
74         
75         @Override
76         public ParentNode<?> getParent() {
77                 return null;
78         }
79         
80         @Override
81         public ParentNode<?> getRootNode() {
82                 return this;
83         }
84         
85         public javax.vecmath.Quat4d getOrientation() {
86                 return MathTools.getIdentityQuat();
87         };
88         
89         @Override
90         public Vector3d getPosition() {
91                 return new Vector3d();
92         }
93         
94         @Override
95         public Quat4d getWorldOrientation() {
96                 return MathTools.getIdentityQuat();
97         }
98         
99         @Override
100         public Vector3d getWorldPosition() {
101                 return new Vector3d();
102         }
103         
104         @Override
105         public Quat4d getWorldOrientation(Quat4d localOrientation) {
106                 return localOrientation;
107         }
108         
109         @Override
110         public Vector3d getWorldPosition(Vector3d localPosition) {
111                 return localPosition;
112         }
113         
114         @Override
115         public Quat4d getLocalOrientation(Quat4d worldOrientation) {
116                 return worldOrientation;
117         }
118         
119         @Override
120         public Vector3d getLocalPosition(Vector3d worldPosition) {
121                 return worldPosition;
122         }
123         
124         @Override
125         public void setPosition(Vector3d position) {
126                 throw new NodeException("Cannot set root node position");
127         }
128         
129         @Override
130         public void setOrientation(Quat4d orientation) {
131                 throw new NodeException("Cannot set root node orientation");
132         }
133         
134         @Override
135         public void setWorldOrientation(Quat4d orientation) {
136                 throw new NodeException("Cannot set root node orientation");
137         }
138         
139         @Override
140         public void setWorldPosition(Vector3d position) {
141                 throw new NodeException("Cannot set root node orientation");
142         }
143         
144         /**
145          * Get a unique name in the Plant3D model with a given prefix followed by
146          * an underscore and number.
147          */
148         public String getUniqueName(String prefix) {
149                 Set<String> names = new HashSet<String>();
150                 for (INode node : getChild()) {
151                         if (!(node instanceof IP3DVisualNode))
152                                 continue;
153                         IP3DVisualNode n = (IP3DVisualNode)node;
154                         names.add(n.getName());
155                         if (node instanceof P3DParentNode)
156                                 ((P3DParentNode<?>) node).getComponentNames(names);
157                 }
158                 int i = 1;
159                 while (true) {
160                         String genName = prefix + "_" + i;
161                         if (!names.contains(genName))
162                                 return genName;
163                         i++;
164                 }
165         }
166
167         
168         @Override
169         public <T> T getAdapter(Class<T> adapter) {
170                 if (adapter.isAssignableFrom(NodeMap.class))
171                         return adapter.cast(nodeMap);
172                 return null;
173         }
174         
175         public Nozzle createNozzle() {
176                 return new Nozzle();
177         }
178         
179         public Equipment createEquipment() {
180                 return new Equipment();
181         }
182         
183         public InlineComponent createInline() {
184                 return new InlineComponent();
185         }
186         
187         public EndComponent createEnd() {
188                 return new EndComponent();
189         }
190         
191         public TurnComponent createTurn() {
192                 return new TurnComponent();
193         }
194
195         public Vector3d getUpVector() {
196                 return upVector;
197         }
198 }