]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/scenegraph/structural/G3DStructuralParentNode.java
Copyrights
[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.INode;\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                 this.position = position;\r
100                 firePropertyChanged(G3D.URIs.hasPosition);\r
101         }\r
102         \r
103         @Override\r
104         @SetPropertyValue(G3D.URIs.hasOrientation)\r
105         public void setOrientation(Quat4d orientation) {\r
106                 this.orientation = orientation;\r
107                 firePropertyChanged(G3D.URIs.hasOrientation);\r
108         }\r
109         \r
110         @Override\r
111         @SetPropertyValue(G3D.URIs.hasWorldOrientation)\r
112         public void setWorldOrientation(Quat4d orientation) {\r
113                 if (getParent() == null)\r
114                         throw new NodeException("Cannot set root node orientation");\r
115                 Quat4d localOr = getLocalOrientation(orientation);\r
116                 setOrientation(localOr);\r
117         }\r
118         \r
119         @Override\r
120         @SetPropertyValue(G3D.URIs.hasWorldPosition)\r
121         public void setWorldPosition(Vector3d position) {\r
122                 if (getParent() == null)\r
123                         throw new NodeException("Cannot set root node position");\r
124                 Vector3d localPos = getLocalPosition(position);\r
125                 setPosition(localPos);\r
126         }\r
127         \r
128         \r
129         \r
130         @RelatedGetValue(G3D.URIs.hasOrientation)\r
131         public double[] getOrientationArr() {\r
132                 double arr[] = new double[4];\r
133                 orientation.get(arr);\r
134                 return arr;\r
135                 \r
136         }\r
137         \r
138         @RelatedGetValue(G3D.URIs.hasPosition)\r
139         public double[] getPositionArr() {\r
140                 double arr[] = new double[3];\r
141                 position.get(arr);\r
142                 return arr;\r
143         }\r
144         \r
145         @RelatedSetValue(G3D.URIs.hasOrientation)\r
146         public void setOrientation(double[] arr) {\r
147                 if (arr == null)\r
148                         return;\r
149                 setOrientation(new Quat4d(arr));\r
150         }\r
151         \r
152         @RelatedSetValue(G3D.URIs.hasPosition)\r
153         public void setPosition(double[] arr) {\r
154                 if (arr == null)\r
155                         return;\r
156                 setPosition(new Vector3d(arr));\r
157         }\r
158         \r
159         \r
160         protected void _addStrNode(String id, T child) {\r
161                 addNode(id+"/str", child);\r
162         }\r
163         \r
164         protected boolean _removeStrNode(String id, T child) {\r
165                 return removeNode(id+"/str", child);\r
166         }\r
167         \r
168         protected Collection<T> _getStrNodes(String id) {\r
169                 return getNodes(id+"/str");\r
170         }\r
171         \r
172         public Object getAdapter(Class adapter) {\r
173                 if (INode.class == adapter)\r
174                         return this;\r
175                 if (IG3DNode.class == adapter)\r
176                         return this;\r
177                 return null;\r
178         }\r
179         \r
180         \r
181         \r
182 }\r