]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/scenegraph/structural/G3DStructuralParentNode.java
Compiler warning elimination
[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 \r
16 import javax.vecmath.Quat4d;\r
17 import javax.vecmath.Vector3d;\r
18 \r
19 import org.simantics.g3d.math.MathTools;\r
20 import org.simantics.g3d.ontology.G3D;\r
21 import org.simantics.g3d.property.annotations.GetPropertyValue;\r
22 import org.simantics.g3d.property.annotations.SetPropertyValue;\r
23 import org.simantics.g3d.scenegraph.IG3DNode;\r
24 import org.simantics.g3d.scenegraph.base.NodeException;\r
25 import org.simantics.g3d.tools.NodeTools;\r
26 import org.simantics.objmap.graph.annotations.RelatedGetValue;\r
27 import org.simantics.objmap.graph.annotations.RelatedSetValue;\r
28 \r
29 public abstract class G3DStructuralParentNode<T extends IStructuralNode> extends StructuralParentNode<T> implements IG3DNode{\r
30         private Vector3d position = new Vector3d();\r
31         private Quat4d orientation = MathTools.getIdentityQuat();\r
32         \r
33 \r
34         @GetPropertyValue(value = G3D.URIs.hasOrientation, tabId = "Transform", name = "Orientation")\r
35         public Quat4d getOrientation() {\r
36                 if (getParent() == null)\r
37                         return MathTools.getIdentityQuat();\r
38                 return orientation;\r
39         };\r
40         \r
41         @Override\r
42         @GetPropertyValue(value = G3D.URIs.hasPosition, tabId = "Transform", name = "Position")\r
43         public Vector3d getPosition() {\r
44                 if (getParent() == null)\r
45                         return MathTools.ORIGIN;\r
46                 return position;\r
47         }\r
48         \r
49         @Override\r
50         @GetPropertyValue(value = G3D.URIs.hasWorldOrientation, tabId = "Transform", name = "World Orientation")\r
51         public Quat4d getWorldOrientation() {\r
52                 if (getParent() == null)\r
53                         return MathTools.getIdentityQuat();\r
54                 return getWorldOrientation(orientation);\r
55         }\r
56         \r
57         @Override\r
58         @GetPropertyValue(value = G3D.URIs.hasWorldPosition, tabId = "Transform", name = "World Position")\r
59         public Vector3d getWorldPosition() {\r
60                 IG3DNode parent = (IG3DNode)getParent();\r
61                 if (parent == null)\r
62                         return MathTools.ORIGIN;\r
63                 return NodeTools.getWorldPosition(parent, new Vector3d(position));\r
64         }\r
65         \r
66         @Override\r
67         public Quat4d getWorldOrientation(Quat4d localOrientation) {\r
68                 IG3DNode parent = (IG3DNode)getParent();\r
69                 if (parent == null)\r
70                         return localOrientation;\r
71                 return NodeTools.getWorldOrientation(parent, localOrientation);\r
72         }\r
73         \r
74         \r
75         public Vector3d getWorldPosition(Vector3d localPosition) {\r
76                 return NodeTools.getWorldPosition(this,localPosition);\r
77         }\r
78         \r
79         @Override\r
80         public Quat4d getLocalOrientation(Quat4d worldOrientation) {\r
81                 IG3DNode parent = (IG3DNode)getParent();\r
82                 if (parent == null)\r
83                         return worldOrientation;\r
84                 return NodeTools.getLocalOrientation(parent, new Quat4d(worldOrientation));\r
85         }\r
86         \r
87         @Override\r
88         public Vector3d getLocalPosition(Vector3d worldPosition) {\r
89                 IG3DNode parent = (IG3DNode)getParent();\r
90                 if (parent == null)\r
91                         return worldPosition;\r
92                 return NodeTools.getLocalPosition(parent,new Vector3d(worldPosition));\r
93         }\r
94         \r
95         @Override\r
96         @SetPropertyValue(G3D.URIs.hasPosition)\r
97         public void setPosition(Vector3d position) {\r
98                 this.position = position;\r
99                 firePropertyChanged(G3D.URIs.hasPosition);\r
100         }\r
101         \r
102         @Override\r
103         @SetPropertyValue(G3D.URIs.hasOrientation)\r
104         public void setOrientation(Quat4d orientation) {\r
105                 this.orientation = orientation;\r
106                 firePropertyChanged(G3D.URIs.hasOrientation);\r
107         }\r
108         \r
109         @Override\r
110         @SetPropertyValue(G3D.URIs.hasWorldOrientation)\r
111         public void setWorldOrientation(Quat4d orientation) {\r
112                 if (getParent() == null)\r
113                         throw new NodeException("Cannot set root node orientation");\r
114                 Quat4d localOr = getLocalOrientation(orientation);\r
115                 setOrientation(localOr);\r
116         }\r
117         \r
118         @Override\r
119         @SetPropertyValue(G3D.URIs.hasWorldPosition)\r
120         public void setWorldPosition(Vector3d position) {\r
121                 if (getParent() == null)\r
122                         throw new NodeException("Cannot set root node position");\r
123                 Vector3d localPos = getLocalPosition(position);\r
124                 setPosition(localPos);\r
125         }\r
126         \r
127         \r
128         \r
129         @RelatedGetValue(G3D.URIs.hasOrientation)\r
130         public double[] getOrientationArr() {\r
131                 double arr[] = new double[4];\r
132                 orientation.get(arr);\r
133                 return arr;\r
134                 \r
135         }\r
136         \r
137         @RelatedGetValue(G3D.URIs.hasPosition)\r
138         public double[] getPositionArr() {\r
139                 double arr[] = new double[3];\r
140                 position.get(arr);\r
141                 return arr;\r
142         }\r
143         \r
144         @RelatedSetValue(G3D.URIs.hasOrientation)\r
145         public void setOrientation(double[] arr) {\r
146                 if (arr == null)\r
147                         return;\r
148                 setOrientation(new Quat4d(arr));\r
149         }\r
150         \r
151         @RelatedSetValue(G3D.URIs.hasPosition)\r
152         public void setPosition(double[] arr) {\r
153                 if (arr == null)\r
154                         return;\r
155                 setPosition(new Vector3d(arr));\r
156         }\r
157         \r
158         \r
159         protected void _addStrNode(String id, T child) {\r
160                 addNode(id+"/str", child);\r
161         }\r
162         \r
163         protected boolean _removeStrNode(String id, T child) {\r
164                 return removeNode(id+"/str", child);\r
165         }\r
166         \r
167         protected Collection<T> _getStrNodes(String id) {\r
168                 return getNodes(id+"/str");\r
169         }\r
170 \r
171         @Override\r
172         public <U> U getAdapter(Class<U> adapter) {\r
173                 if (adapter.isInstance(this))\r
174                         return adapter.cast(this);\r
175                 return null;\r
176         }\r
177         \r
178 }\r