1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g3d.scenegraph.structural;
\r
14 import java.util.Collection;
\r
16 import javax.vecmath.Quat4d;
\r
17 import javax.vecmath.Vector3d;
\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
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
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
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
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
58 @GetPropertyValue(value = G3D.URIs.hasWorldPosition, tabId = "Transform", name = "World Position")
\r
59 public Vector3d getWorldPosition() {
\r
60 IG3DNode parent = (IG3DNode)getParent();
\r
62 return MathTools.ORIGIN;
\r
63 return NodeTools.getWorldPosition(parent, new Vector3d(position));
\r
67 public Quat4d getWorldOrientation(Quat4d localOrientation) {
\r
68 IG3DNode parent = (IG3DNode)getParent();
\r
70 return localOrientation;
\r
71 return NodeTools.getWorldOrientation(parent, localOrientation);
\r
75 public Vector3d getWorldPosition(Vector3d localPosition) {
\r
76 return NodeTools.getWorldPosition(this,localPosition);
\r
80 public Quat4d getLocalOrientation(Quat4d worldOrientation) {
\r
81 IG3DNode parent = (IG3DNode)getParent();
\r
83 return worldOrientation;
\r
84 return NodeTools.getLocalOrientation(parent, new Quat4d(worldOrientation));
\r
88 public Vector3d getLocalPosition(Vector3d worldPosition) {
\r
89 IG3DNode parent = (IG3DNode)getParent();
\r
91 return worldPosition;
\r
92 return NodeTools.getLocalPosition(parent,new Vector3d(worldPosition));
\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
103 @SetPropertyValue(G3D.URIs.hasOrientation)
\r
104 public void setOrientation(Quat4d orientation) {
\r
105 this.orientation = orientation;
\r
106 firePropertyChanged(G3D.URIs.hasOrientation);
\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
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
129 @RelatedGetValue(G3D.URIs.hasOrientation)
\r
130 public double[] getOrientationArr() {
\r
131 double arr[] = new double[4];
\r
132 orientation.get(arr);
\r
137 @RelatedGetValue(G3D.URIs.hasPosition)
\r
138 public double[] getPositionArr() {
\r
139 double arr[] = new double[3];
\r
144 @RelatedSetValue(G3D.URIs.hasOrientation)
\r
145 public void setOrientation(double[] arr) {
\r
148 setOrientation(new Quat4d(arr));
\r
151 @RelatedSetValue(G3D.URIs.hasPosition)
\r
152 public void setPosition(double[] arr) {
\r
155 setPosition(new Vector3d(arr));
\r
159 protected void _addStrNode(String id, T child) {
\r
160 addNode(id+"/str", child);
\r
163 protected boolean _removeStrNode(String id, T child) {
\r
164 return removeNode(id+"/str", child);
\r
167 protected Collection<T> _getStrNodes(String id) {
\r
168 return getNodes(id+"/str");
\r
172 public <U> U getAdapter(Class<U> adapter) {
\r
173 if (adapter.isInstance(this))
\r
174 return adapter.cast(this);
\r