]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java
7a5ce551a7c317c16df776455125398213378934
[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         public String getUniqueName(String prefix) {
145                 Set<String> names = new HashSet<String>();
146                 for (INode node : getChild()) {
147                         if (!(node instanceof IP3DVisualNode))
148                                 continue;
149                         IP3DVisualNode n = (IP3DVisualNode)node;
150                         names.add(n.getName());
151                 }
152                 int i = 1;
153                 while (true) {
154                         String genName = prefix + "_" + i;
155                         if (!names.contains(genName))
156                                 return genName;
157                         i++;
158                 }
159         }
160
161         
162         @Override
163         public <T> T getAdapter(Class<T> adapter) {
164                 if (adapter.isAssignableFrom(NodeMap.class))
165                         return adapter.cast(nodeMap);
166                 return null;
167         }
168         
169         public Nozzle createNozzle() {
170                 return new Nozzle();
171         }
172         
173         public Equipment createEquipment() {
174                 return new Equipment();
175         }
176         
177         public InlineComponent createInline() {
178                 return new InlineComponent();
179         }
180         
181         public EndComponent createEnd() {
182                 return new EndComponent();
183         }
184         
185         public TurnComponent createTurn() {
186                 return new TurnComponent();
187         }
188
189         public Vector3d getUpVector() {
190                 return upVector;
191         }
192 }