1 package org.simantics.plant3d.scenegraph;
3 import java.util.HashSet;
4 import java.util.Objects;
7 import javax.vecmath.Quat4d;
8 import javax.vecmath.Vector3d;
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;
23 public abstract class P3DParentNode<T extends IP3DNode> extends ParentNode<T> implements IP3DVisualNode {
30 @RelatedGetValue(Layer0.URIs.HasName)
31 @GetPropertyValue(value = Layer0.URIs.HasName, tabId = "Default", name = "Name")
32 public String getName() {
36 @RelatedSetValue(Layer0.URIs.HasName)
37 @SetPropertyValue(Layer0.URIs.HasName)
38 public void setName(String name) {
39 if (name == null || name.equals(this.name))
42 firePropertyChanged(Layer0.URIs.HasName);
46 public String toString() {
50 private Vector3d position = new Vector3d();
51 private Quat4d orientation = MathTools.getIdentityQuat();
54 @GetPropertyValue(value = G3D.URIs.hasOrientation, tabId = "Transform", name = "Orientation")
55 public Quat4d getOrientation() {
59 @RelatedGetValue(G3D.URIs.hasOrientation)
60 public double[] getOrientationArr() {
61 double arr[] = new double[4];
68 @GetPropertyValue(value = G3D.URIs.hasPosition, tabId = "Transform", name = "Position")
69 public Vector3d getPosition() {
73 @RelatedGetValue(G3D.URIs.hasPosition)
74 public double[] getPositionArr() {
75 double arr[] = new double[3];
81 @SetPropertyValue(G3D.URIs.hasOrientation)
82 public void setOrientation(Quat4d orientation) {
83 assert(orientation != null);
84 if (Objects.equals(this.orientation, orientation))
87 this.orientation = orientation;
89 firePropertyChanged(G3D.URIs.hasOrientation);
93 @SetPropertyValue(G3D.URIs.hasPosition)
94 public void setPosition(Vector3d position) {
95 assert(position != null);
96 if (Objects.equals(this.position, position))
99 this.position = position;
101 firePropertyChanged(G3D.URIs.hasPosition);
104 @RelatedSetValue(G3D.URIs.hasOrientation)
105 public void setOrientation(double[] arr) {
108 setOrientation(new Quat4d(arr));
111 @RelatedSetValue(G3D.URIs.hasPosition)
112 public void setPosition(double[] arr) {
115 setPosition(new Vector3d(arr));
119 @GetPropertyValue(value = G3D.URIs.hasWorldPosition, tabId = "Transform", name = "World Position")
120 public Vector3d getWorldPosition() {
121 IG3DNode parent = (IG3DNode)getParent();
124 return NodeTools.getWorldPosition(parent, new Vector3d(position));
128 public Vector3d getWorldPosition(Vector3d localPosition) {
129 return NodeTools.getWorldPosition(this,localPosition);
134 @GetPropertyValue(value = G3D.URIs.hasWorldOrientation, tabId = "Transform", name = "World Orientation")
135 public Quat4d getWorldOrientation() {
136 return getWorldOrientation(new Quat4d(orientation));
139 public Quat4d getWorldOrientation(Quat4d localOrientation) {
140 IG3DNode parent = (IG3DNode)getParent();
142 return localOrientation;
143 return NodeTools.getWorldOrientation(parent, localOrientation);
147 public Vector3d getLocalPosition(Vector3d worldPosition) {
148 IG3DNode parent = (IG3DNode)getParent();
150 return worldPosition;
151 return NodeTools.getLocalPosition(parent,new Vector3d(worldPosition));
155 public Quat4d getLocalOrientation(Quat4d worldOrientation) {
156 IG3DNode parent = (IG3DNode)getParent();
158 return worldOrientation;
159 return NodeTools.getLocalOrientation(parent, new Quat4d(worldOrientation));
163 @SetPropertyValue(G3D.URIs.hasWorldPosition)
164 public void setWorldPosition(Vector3d position) {
165 Vector3d localPos = getLocalPosition(position);
166 setPosition(localPos);
170 @SetPropertyValue(G3D.URIs.hasWorldOrientation)
171 public void setWorldOrientation(Quat4d orientation) {
172 Quat4d localOr = getLocalOrientation(orientation);
173 setOrientation(localOr);
177 public <C> C getAdapter(Class<C> adapter) {
178 if (adapter.isInstance(this))
179 return adapter.cast(this);
183 public String getUniqueName(String prefix) {
184 Set<String> names = new HashSet<String>();
185 for (IP3DNode node : getNodes()) {
186 if (!(node instanceof IP3DVisualNode))
188 IP3DVisualNode n = (IP3DVisualNode)node;
189 names.add(n.getName());
193 String genName = prefix + "_" + i;
194 if (!names.contains(genName))