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