/******************************************************************************* * Copyright (c) 2012, 2013 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.g3d.scenegraph.structural; import java.util.Collection; import javax.vecmath.Quat4d; import javax.vecmath.Vector3d; import org.simantics.g3d.math.MathTools; import org.simantics.g3d.ontology.G3D; import org.simantics.g3d.property.annotations.GetPropertyValue; import org.simantics.g3d.property.annotations.SetPropertyValue; import org.simantics.g3d.scenegraph.IG3DNode; import org.simantics.g3d.scenegraph.base.INode; import org.simantics.g3d.scenegraph.base.NodeException; import org.simantics.g3d.tools.NodeTools; import org.simantics.objmap.graph.annotations.RelatedGetValue; import org.simantics.objmap.graph.annotations.RelatedSetValue; public abstract class G3DStructuralParentNode extends StructuralParentNode implements IG3DNode{ private Vector3d position = new Vector3d(); private Quat4d orientation = MathTools.getIdentityQuat(); @GetPropertyValue(value = G3D.URIs.hasOrientation, tabId = "Transform", name = "Orientation") public Quat4d getOrientation() { if (getParent() == null) return MathTools.getIdentityQuat(); return orientation; }; @Override @GetPropertyValue(value = G3D.URIs.hasPosition, tabId = "Transform", name = "Position") public Vector3d getPosition() { if (getParent() == null) return MathTools.ORIGIN; return position; } @Override @GetPropertyValue(value = G3D.URIs.hasWorldOrientation, tabId = "Transform", name = "World Orientation") public Quat4d getWorldOrientation() { if (getParent() == null) return MathTools.getIdentityQuat(); return getWorldOrientation(orientation); } @Override @GetPropertyValue(value = G3D.URIs.hasWorldPosition, tabId = "Transform", name = "World Position") public Vector3d getWorldPosition() { IG3DNode parent = (IG3DNode)getParent(); if (parent == null) return MathTools.ORIGIN; return NodeTools.getWorldPosition(parent, new Vector3d(position)); } @Override public Quat4d getWorldOrientation(Quat4d localOrientation) { IG3DNode parent = (IG3DNode)getParent(); if (parent == null) return localOrientation; return NodeTools.getWorldOrientation(parent, localOrientation); } public Vector3d getWorldPosition(Vector3d localPosition) { return NodeTools.getWorldPosition(this,localPosition); } @Override public Quat4d getLocalOrientation(Quat4d worldOrientation) { IG3DNode parent = (IG3DNode)getParent(); if (parent == null) return worldOrientation; return NodeTools.getLocalOrientation(parent, new Quat4d(worldOrientation)); } @Override public Vector3d getLocalPosition(Vector3d worldPosition) { IG3DNode parent = (IG3DNode)getParent(); if (parent == null) return worldPosition; return NodeTools.getLocalPosition(parent,new Vector3d(worldPosition)); } @Override @SetPropertyValue(G3D.URIs.hasPosition) public void setPosition(Vector3d position) { this.position = position; firePropertyChanged(G3D.URIs.hasPosition); } @Override @SetPropertyValue(G3D.URIs.hasOrientation) public void setOrientation(Quat4d orientation) { this.orientation = orientation; firePropertyChanged(G3D.URIs.hasOrientation); } @Override @SetPropertyValue(G3D.URIs.hasWorldOrientation) public void setWorldOrientation(Quat4d orientation) { if (getParent() == null) throw new NodeException("Cannot set root node orientation"); Quat4d localOr = getLocalOrientation(orientation); setOrientation(localOr); } @Override @SetPropertyValue(G3D.URIs.hasWorldPosition) public void setWorldPosition(Vector3d position) { if (getParent() == null) throw new NodeException("Cannot set root node position"); Vector3d localPos = getLocalPosition(position); setPosition(localPos); } @RelatedGetValue(G3D.URIs.hasOrientation) public double[] getOrientationArr() { double arr[] = new double[4]; orientation.get(arr); return arr; } @RelatedGetValue(G3D.URIs.hasPosition) public double[] getPositionArr() { double arr[] = new double[3]; position.get(arr); return arr; } @RelatedSetValue(G3D.URIs.hasOrientation) public void setOrientation(double[] arr) { if (arr == null) return; setOrientation(new Quat4d(arr)); } @RelatedSetValue(G3D.URIs.hasPosition) public void setPosition(double[] arr) { if (arr == null) return; setPosition(new Vector3d(arr)); } protected void _addStrNode(String id, T child) { addNode(id+"/str", child); } protected boolean _removeStrNode(String id, T child) { return removeNode(id+"/str", child); } protected Collection _getStrNodes(String id) { return getNodes(id+"/str"); } public Object getAdapter(Class adapter) { if (INode.class == adapter) return this; if (IG3DNode.class == adapter) return this; return null; } }