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
15 import java.util.Objects;
\r
17 import javax.vecmath.Quat4d;
\r
18 import javax.vecmath.Vector3d;
\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
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
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
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
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
59 @GetPropertyValue(value = G3D.URIs.hasWorldPosition, tabId = "Transform", name = "World Position")
\r
60 public Vector3d getWorldPosition() {
\r
61 IG3DNode parent = (IG3DNode)getParent();
\r
63 return MathTools.ORIGIN;
\r
64 return NodeTools.getWorldPosition(parent, new Vector3d(position));
\r
68 public Quat4d getWorldOrientation(Quat4d localOrientation) {
\r
69 IG3DNode parent = (IG3DNode)getParent();
\r
71 return localOrientation;
\r
72 return NodeTools.getWorldOrientation(parent, localOrientation);
\r
76 public Vector3d getWorldPosition(Vector3d localPosition) {
\r
77 return NodeTools.getWorldPosition(this,localPosition);
\r
81 public Quat4d getLocalOrientation(Quat4d worldOrientation) {
\r
82 IG3DNode parent = (IG3DNode)getParent();
\r
84 return worldOrientation;
\r
85 return NodeTools.getLocalOrientation(parent, new Quat4d(worldOrientation));
\r
89 public Vector3d getLocalPosition(Vector3d worldPosition) {
\r
90 IG3DNode parent = (IG3DNode)getParent();
\r
92 return worldPosition;
\r
93 return NodeTools.getLocalPosition(parent,new Vector3d(worldPosition));
\r
97 @SetPropertyValue(G3D.URIs.hasPosition)
\r
98 public void setPosition(Vector3d position) {
\r
99 if (Objects.equals(this.position, position))
\r
102 this.position = position;
\r
103 firePropertyChanged(G3D.URIs.hasPosition);
\r
107 @SetPropertyValue(G3D.URIs.hasOrientation)
\r
108 public void setOrientation(Quat4d orientation) {
\r
109 if (Objects.equals(this.orientation, orientation))
\r
112 this.orientation = orientation;
\r
113 firePropertyChanged(G3D.URIs.hasOrientation);
\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
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
136 @RelatedGetValue(G3D.URIs.hasOrientation)
\r
137 public double[] getOrientationArr() {
\r
138 double arr[] = new double[4];
\r
139 orientation.get(arr);
\r
144 @RelatedGetValue(G3D.URIs.hasPosition)
\r
145 public double[] getPositionArr() {
\r
146 double arr[] = new double[3];
\r
151 @RelatedSetValue(G3D.URIs.hasOrientation)
\r
152 public void setOrientation(double[] arr) {
\r
155 setOrientation(new Quat4d(arr));
\r
158 @RelatedSetValue(G3D.URIs.hasPosition)
\r
159 public void setPosition(double[] arr) {
\r
162 setPosition(new Vector3d(arr));
\r
166 protected void _addStrNode(String id, T child) {
\r
167 addNode(id+"/str", child);
\r
170 protected boolean _removeStrNode(String id, T child) {
\r
171 return removeNode(id+"/str", child);
\r
174 protected Collection<T> _getStrNodes(String id) {
\r
175 return getNodes(id+"/str");
\r
179 public <U> U getAdapter(Class<U> adapter) {
\r
180 if (adapter.isInstance(this))
\r
181 return adapter.cast(this);
\r