]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/scenegraph/structural/G3DStructuralParentNode.java
Remove listener calls when property values not updated.
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / scenegraph / structural / G3DStructuralParentNode.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g3d.scenegraph.structural;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.Objects;\r
16 \r
17 import javax.vecmath.Quat4d;\r
18 import javax.vecmath.Vector3d;\r
19 \r
20 import org.simantics.g3d.math.MathTools;\r
21 import org.simantics.g3d.ontology.G3D;\r
22 import org.simantics.g3d.property.annotations.GetPropertyValue;\r
23 import org.simantics.g3d.property.annotations.SetPropertyValue;\r
24 import org.simantics.g3d.scenegraph.IG3DNode;\r
25 import org.simantics.g3d.scenegraph.base.NodeException;\r
26 import org.simantics.g3d.tools.NodeTools;\r
27 import org.simantics.objmap.graph.annotations.RelatedGetValue;\r
28 import org.simantics.objmap.graph.annotations.RelatedSetValue;\r
29 \r
30 public abstract class G3DStructuralParentNode<T extends IStructuralNode> extends StructuralParentNode<T> implements IG3DNode{\r
31         private Vector3d position = new Vector3d();\r
32         private Quat4d orientation = MathTools.getIdentityQuat();\r
33         \r
34 \r
35         @GetPropertyValue(value = G3D.URIs.hasOrientation, tabId = "Transform", name = "Orientation")\r
36         public Quat4d getOrientation() {\r
37                 if (getParent() == null)\r
38                         return MathTools.getIdentityQuat();\r
39                 return orientation;\r
40         };\r
41         \r
42         @Override\r
43         @GetPropertyValue(value = G3D.URIs.hasPosition, tabId = "Transform", name = "Position")\r
44         public Vector3d getPosition() {\r
45                 if (getParent() == null)\r
46                         return MathTools.ORIGIN;\r
47                 return position;\r
48         }\r
49         \r
50         @Override\r
51         @GetPropertyValue(value = G3D.URIs.hasWorldOrientation, tabId = "Transform", name = "World Orientation")\r
52         public Quat4d getWorldOrientation() {\r
53                 if (getParent() == null)\r
54                         return MathTools.getIdentityQuat();\r
55                 return getWorldOrientation(orientation);\r
56         }\r
57         \r
58         @Override\r
59         @GetPropertyValue(value = G3D.URIs.hasWorldPosition, tabId = "Transform", name = "World Position")\r
60         public Vector3d getWorldPosition() {\r
61                 IG3DNode parent = (IG3DNode)getParent();\r
62                 if (parent == null)\r
63                         return MathTools.ORIGIN;\r
64                 return NodeTools.getWorldPosition(parent, new Vector3d(position));\r
65         }\r
66         \r
67         @Override\r
68         public Quat4d getWorldOrientation(Quat4d localOrientation) {\r
69                 IG3DNode parent = (IG3DNode)getParent();\r
70                 if (parent == null)\r
71                         return localOrientation;\r
72                 return NodeTools.getWorldOrientation(parent, localOrientation);\r
73         }\r
74         \r
75         \r
76         public Vector3d getWorldPosition(Vector3d localPosition) {\r
77                 return NodeTools.getWorldPosition(this,localPosition);\r
78         }\r
79         \r
80         @Override\r
81         public Quat4d getLocalOrientation(Quat4d worldOrientation) {\r
82                 IG3DNode parent = (IG3DNode)getParent();\r
83                 if (parent == null)\r
84                         return worldOrientation;\r
85                 return NodeTools.getLocalOrientation(parent, new Quat4d(worldOrientation));\r
86         }\r
87         \r
88         @Override\r
89         public Vector3d getLocalPosition(Vector3d worldPosition) {\r
90                 IG3DNode parent = (IG3DNode)getParent();\r
91                 if (parent == null)\r
92                         return worldPosition;\r
93                 return NodeTools.getLocalPosition(parent,new Vector3d(worldPosition));\r
94         }\r
95         \r
96         @Override\r
97         @SetPropertyValue(G3D.URIs.hasPosition)\r
98         public void setPosition(Vector3d position) {\r
99                 if (Objects.equals(this.position, position))\r
100                         return;\r
101                 \r
102                 this.position = position;\r
103                 firePropertyChanged(G3D.URIs.hasPosition);\r
104         }\r
105         \r
106         @Override\r
107         @SetPropertyValue(G3D.URIs.hasOrientation)\r
108         public void setOrientation(Quat4d orientation) {\r
109                 if (Objects.equals(this.orientation, orientation))\r
110                         return;\r
111                 \r
112                 this.orientation = orientation;\r
113                 firePropertyChanged(G3D.URIs.hasOrientation);\r
114         }\r
115         \r
116         @Override\r
117         @SetPropertyValue(G3D.URIs.hasWorldOrientation)\r
118         public void setWorldOrientation(Quat4d orientation) {\r
119                 if (getParent() == null)\r
120                         throw new NodeException("Cannot set root node orientation");\r
121                 Quat4d localOr = getLocalOrientation(orientation);\r
122                 setOrientation(localOr);\r
123         }\r
124         \r
125         @Override\r
126         @SetPropertyValue(G3D.URIs.hasWorldPosition)\r
127         public void setWorldPosition(Vector3d position) {\r
128                 if (getParent() == null)\r
129                         throw new NodeException("Cannot set root node position");\r
130                 Vector3d localPos = getLocalPosition(position);\r
131                 setPosition(localPos);\r
132         }\r
133         \r
134         \r
135         \r
136         @RelatedGetValue(G3D.URIs.hasOrientation)\r
137         public double[] getOrientationArr() {\r
138                 double arr[] = new double[4];\r
139                 orientation.get(arr);\r
140                 return arr;\r
141                 \r
142         }\r
143         \r
144         @RelatedGetValue(G3D.URIs.hasPosition)\r
145         public double[] getPositionArr() {\r
146                 double arr[] = new double[3];\r
147                 position.get(arr);\r
148                 return arr;\r
149         }\r
150         \r
151         @RelatedSetValue(G3D.URIs.hasOrientation)\r
152         public void setOrientation(double[] arr) {\r
153                 if (arr == null)\r
154                         return;\r
155                 setOrientation(new Quat4d(arr));\r
156         }\r
157         \r
158         @RelatedSetValue(G3D.URIs.hasPosition)\r
159         public void setPosition(double[] arr) {\r
160                 if (arr == null)\r
161                         return;\r
162                 setPosition(new Vector3d(arr));\r
163         }\r
164         \r
165         \r
166         protected void _addStrNode(String id, T child) {\r
167                 addNode(id+"/str", child);\r
168         }\r
169         \r
170         protected boolean _removeStrNode(String id, T child) {\r
171                 return removeNode(id+"/str", child);\r
172         }\r
173         \r
174         protected Collection<T> _getStrNodes(String id) {\r
175                 return getNodes(id+"/str");\r
176         }\r
177 \r
178         @Override\r
179         public <U> U getAdapter(Class<U> adapter) {\r
180                 if (adapter.isInstance(this))\r
181                         return adapter.cast(this);\r
182                 return null;\r
183         }\r
184         \r
185 }\r