]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.csg/src/org/simantics/g3d/csg/scenegraph2/CSGrootNode.java
d88b42ed66adbc77863f986c094c93315e3a7d6e
[simantics/3d.git] / org.simantics.g3d.csg / src / org / simantics / g3d / csg / scenegraph2 / CSGrootNode.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g3d.csg.scenegraph2;
13
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Set;
17 import java.util.Stack;
18
19 import javax.vecmath.Quat4d;
20 import javax.vecmath.Vector3d;
21
22 import org.simantics.g3d.csg.editor.CSGNodeMap;
23 import org.simantics.g3d.csg.ontology.CSG;
24 import org.simantics.g3d.math.MathTools;
25 import org.simantics.g3d.scenegraph.IG3DNode;
26 import org.simantics.g3d.scenegraph.NodeMap;
27 import org.simantics.g3d.scenegraph.NodeMapProvider;
28 import org.simantics.g3d.scenegraph.base.NodeException;
29 import org.simantics.g3d.scenegraph.base.ParentNode;
30 import org.simantics.objmap.graph.annotations.GraphType;
31 import org.simantics.objmap.graph.annotations.RelatedElementsAdd;
32 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
33 import org.simantics.objmap.graph.annotations.RelatedElementsRem;
34
35 import vtk.vtkProp;
36
37
38 @GraphType(CSG.URIs.Model)
39 public class CSGrootNode extends ParentNode<ICSGnode> implements IG3DNode, NodeMapProvider<vtkProp, ICSGnode> {
40         
41         
42         private CSGNodeMap nodeMap;
43         
44         public void setNodeMap(CSGNodeMap nodeMap) {
45                 this.nodeMap = nodeMap;
46         }
47         
48         @Override
49         public NodeMap<vtkProp, ICSGnode> getNodeMap() {
50                 return nodeMap;
51         }
52         
53         @Override
54         public ParentNode<?> getParent() {
55                 return null;
56         }
57         
58         @Override
59         public ParentNode<?> getRootNode() {
60                 return this;
61         }
62         
63         @RelatedElementsAdd(CSG.URIs.hasChildShape)
64         public void addChild(ICSGnode node) {
65                 addNode("child",node);
66         }
67         
68         @RelatedElementsGet(CSG.URIs.hasChildShape)
69         public Collection<ICSGnode> getChild() {
70                 return getNodes("child");
71         }
72         
73         @RelatedElementsRem(CSG.URIs.hasChildShape)
74         public void remChild(ICSGnode node) {
75                 removeNode("child", node);
76         }
77         
78         public javax.vecmath.Quat4d getOrientation() {
79                 return MathTools.getIdentityQuat();
80         };
81         
82         @Override
83         public Vector3d getPosition() {
84                 return new Vector3d();
85         }
86         
87         @Override
88         public Quat4d getWorldOrientation() {
89                 return MathTools.getIdentityQuat();
90         }
91         
92         @Override
93         public Vector3d getWorldPosition() {
94                 return new Vector3d();
95         }
96         
97         @Override
98         public Quat4d getWorldOrientation(Quat4d localOrientation) {
99                 return localOrientation;
100         }
101         
102         @Override
103         public Vector3d getWorldPosition(Vector3d localPosition) {
104                 return localPosition;
105         }
106         
107         @Override
108         public Quat4d getLocalOrientation(Quat4d worldOrientation) {
109                 return worldOrientation;
110         }
111         
112         @Override
113         public Vector3d getLocalPosition(Vector3d worldPosition) {
114                 return worldPosition;
115         }
116         
117         @Override
118         public void setPosition(Vector3d position) {
119                 throw new NodeException("Cannot set root node position");
120         }
121         
122         @Override
123         public void setOrientation(Quat4d orientation) {
124                 throw new NodeException("Cannot set root node orientation");
125         }
126         
127         @Override
128         public void setWorldOrientation(Quat4d orientation) {
129                 throw new NodeException("Cannot set root node orientation");
130         }
131         
132         @Override
133         public void setWorldPosition(Vector3d position) {
134                 throw new NodeException("Cannot set root node orientation");
135         }
136         
137         public String getUniqueName(String prefix) {
138                 Set<String> names = new HashSet<String>();
139                 Stack<ICSGnode> nodes = new Stack<ICSGnode>();
140                 nodes.addAll(getChild());
141                 while (!nodes.isEmpty()) {
142                         ICSGnode n = nodes.pop();
143                         names.add(((ICSGnode)n).getName());
144                         if (n instanceof CSGparentNode) {
145                                 nodes.addAll(((CSGparentNode)n).getChild());
146                                 nodes.addAll(((CSGparentNode)n).getPrimaryChild());
147                                 nodes.addAll(((CSGparentNode)n).getSecondaryChild());
148                         }
149                 }
150                 int i = 1;
151                 while (true) {
152                         String genName = prefix + "_" + i;
153                         if (!names.contains(genName))
154                                 return genName;
155                         i++;
156                 }
157         }
158         
159
160         @Override
161         public Object getAdapter(Class adapter) {
162                 if (NodeMap.class == adapter)
163                         return nodeMap;
164                 return null;
165         }
166         
167
168 }