]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DParentNode.java
08c289da72b4d4e6469177e0d6fe75d4bb704eee
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / P3DParentNode.java
1 package org.simantics.plant3d.scenegraph;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import javax.vecmath.Quat4d;
7 import javax.vecmath.Vector3d;
8
9 import org.simantics.g3d.math.MathTools;
10 import org.simantics.g3d.ontology.G3D;
11 import org.simantics.g3d.property.annotations.GetPropertyValue;
12 import org.simantics.g3d.property.annotations.PropertyContributor;
13 import org.simantics.g3d.property.annotations.SetPropertyValue;
14 import org.simantics.g3d.scenegraph.IG3DNode;
15 import org.simantics.g3d.scenegraph.base.ParentNode;
16 import org.simantics.g3d.tools.NodeTools;
17 import org.simantics.layer0.Layer0;
18 import org.simantics.objmap.graph.annotations.RelatedGetValue;
19 import org.simantics.objmap.graph.annotations.RelatedSetValue;
20
21 @PropertyContributor
22 public abstract class P3DParentNode<T extends IP3DNode> extends ParentNode<T> implements IP3DVisualNode {
23         
24         private String name;
25         
26
27         
28         
29         @RelatedGetValue(Layer0.URIs.HasName)
30         @GetPropertyValue(value = Layer0.URIs.HasName, tabId = "Default", name = "Name")
31         public String getName() {
32                 return name;
33         }
34         
35         @RelatedSetValue(Layer0.URIs.HasName)
36         @SetPropertyValue(Layer0.URIs.HasName)
37         public void setName(String name) {
38                 if (name == null)
39                         return;
40                 this.name = name;
41                 firePropertyChanged(Layer0.URIs.HasName);
42         }
43         
44         @Override
45         public String toString() {
46                 return getName();
47         }
48         
49         private Vector3d position = new Vector3d();
50         private Quat4d orientation = MathTools.getIdentityQuat();
51         
52         @Override
53         @GetPropertyValue(value = G3D.URIs.hasOrientation, tabId = "Transform", name = "Orientation")
54         public Quat4d getOrientation() {
55                 return orientation;
56         }
57         
58         @RelatedGetValue(G3D.URIs.hasOrientation)
59         public double[] getOrientationArr() {
60                 double arr[] = new double[4];
61                 orientation.get(arr);
62                 return arr;
63                 
64         }
65         
66         @Override
67         @GetPropertyValue(value = G3D.URIs.hasPosition, tabId = "Transform", name = "Position")
68         public Vector3d getPosition() {
69                 return position;
70         }
71         
72         @RelatedGetValue(G3D.URIs.hasPosition)
73         public double[] getPositionArr() {
74                 double arr[] = new double[3];
75                 position.get(arr);
76                 return arr;
77         }
78         
79         @Override
80         @SetPropertyValue(G3D.URIs.hasOrientation)
81         public void setOrientation(Quat4d orientation) {
82                 assert(orientation != null);
83                 this.orientation = orientation;
84                 
85                 firePropertyChanged(G3D.URIs.hasOrientation);
86         }
87         
88         @Override
89         @SetPropertyValue(G3D.URIs.hasPosition)
90         public void setPosition(Vector3d position) {
91                 assert(position != null);
92                 this.position = position;
93                 
94                 firePropertyChanged(G3D.URIs.hasPosition);
95         }
96         
97         @RelatedSetValue(G3D.URIs.hasOrientation)
98         public void setOrientation(double[] arr) {
99                 if (arr == null)
100                         return;
101                 setOrientation(new Quat4d(arr));
102         }
103         
104         @RelatedSetValue(G3D.URIs.hasPosition)
105         public void setPosition(double[] arr) {
106                 if (arr == null)
107                         return;
108                 setPosition(new Vector3d(arr));
109         }
110         
111         @Override
112         @GetPropertyValue(value = G3D.URIs.hasWorldPosition, tabId = "Transform", name = "World Position")
113         public Vector3d getWorldPosition() {
114                 IG3DNode parent = (IG3DNode)getParent();
115                 if (parent == null)
116                         return position;
117                 return NodeTools.getWorldPosition(parent, new Vector3d(position));
118         }
119         
120         
121         public Vector3d getWorldPosition(Vector3d localPosition) {
122                 return NodeTools.getWorldPosition(this,localPosition);
123         }
124
125         
126         @Override
127         @GetPropertyValue(value = G3D.URIs.hasWorldOrientation, tabId = "Transform", name = "World Orientation")
128         public Quat4d getWorldOrientation() {
129                 return getWorldOrientation(new Quat4d(orientation));
130         }
131         
132         public Quat4d getWorldOrientation(Quat4d localOrientation) {
133                 IG3DNode parent = (IG3DNode)getParent();
134                 if (parent == null)
135                         return localOrientation;
136                 return NodeTools.getWorldOrientation(parent, localOrientation);
137         }
138         
139         @Override
140         public Vector3d getLocalPosition(Vector3d worldPosition) {
141                 IG3DNode parent = (IG3DNode)getParent();
142                 if (parent == null)
143                         return worldPosition;
144                 return NodeTools.getLocalPosition(parent,new Vector3d(worldPosition));
145         }
146         
147         @Override
148         public Quat4d getLocalOrientation(Quat4d worldOrientation) {
149                 IG3DNode parent = (IG3DNode)getParent();
150                 if (parent == null)
151                         return worldOrientation;
152                 return NodeTools.getLocalOrientation(parent, new Quat4d(worldOrientation));
153         }
154         
155         @Override
156         @SetPropertyValue(G3D.URIs.hasWorldPosition)
157         public void setWorldPosition(Vector3d position) {
158                 Vector3d localPos = getLocalPosition(position);
159                 setPosition(localPos);
160         }
161         
162         @Override
163         @SetPropertyValue(G3D.URIs.hasWorldOrientation)
164         public void setWorldOrientation(Quat4d orientation) {
165                 Quat4d localOr = getLocalOrientation(orientation);
166                 setOrientation(localOr);
167         }
168         
169         @Override
170         public Object getAdapter(Class adapter) {
171                 if (IG3DNode.class == adapter)
172                         return this;
173                 return null;
174         }
175         
176         public String getUniqueName(String prefix) {
177                 Set<String> names = new HashSet<String>();
178                 for (IP3DNode node : getNodes()) {
179                         if (!(node instanceof IP3DVisualNode))
180                                 continue;
181                         IP3DVisualNode n = (IP3DVisualNode)node;
182                         names.add(n.getName());
183                 }
184                 int i = 1;
185                 while (true) {
186                         String genName = prefix + "_" + i;
187                         if (!names.contains(genName))
188                                 return genName;
189                         i++;
190                 }
191         }
192
193     
194 }