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.tools;
\r
14 import javax.vecmath.Matrix4d;
\r
15 import javax.vecmath.Quat4d;
\r
16 import javax.vecmath.Vector3d;
\r
18 import org.simantics.g3d.math.MathTools;
\r
19 import org.simantics.g3d.scenegraph.IG3DNode;
\r
21 public class NodeTools {
\r
23 public static Vector3d getWorldPosition(IG3DNode node, Vector3d localPosition) {
\r
24 Vector3d v = new Vector3d(localPosition);
\r
25 MathTools.rotate(node.getOrientation(), v, v);
\r
26 v.add(node.getPosition());
\r
28 IG3DNode parent = (IG3DNode)node.getParent();
\r
31 return getWorldPosition(parent,v);
\r
34 public static Vector3d getWorldPosition(IG3DNode node) {
\r
35 IG3DNode parent = (IG3DNode)node.getParent();
\r
37 return node.getPosition();
\r
38 return NodeTools.getWorldPosition(parent,new Vector3d(node.getPosition()));
\r
41 public static Quat4d getWorldOrientation(IG3DNode node, Quat4d localOrientation) {
\r
42 Quat4d q = new Quat4d();
\r
43 q.set(node.getOrientation());
\r
44 Quat4d q2 = new Quat4d();
\r
45 q2.set(localOrientation);
\r
48 IG3DNode parent = (IG3DNode)node.getParent();
\r
51 return getWorldOrientation(parent,q);
\r
54 public static Quat4d getWorldOrientation(IG3DNode node) {
\r
55 IG3DNode parent = (IG3DNode)node.getParent();
\r
57 return node.getOrientation();
\r
58 return NodeTools.getWorldOrientation(parent, node.getOrientation());
\r
62 public static Vector3d getLocalPosition(IG3DNode node, Vector3d worldCoord) {
\r
64 IG3DNode parent = (IG3DNode)node.getParent();
\r
65 if (parent == null) {// this is a root node ( has no transformation)
\r
69 Vector3d local = getLocalPosition(parent,worldCoord);
\r
70 local.sub(node.getPosition());
\r
72 Quat4d q = new Quat4d();
\r
73 q.set(node.getOrientation());
\r
75 MathTools.rotate(q, local, local);
\r
80 public static Quat4d getLocalOrientation(IG3DNode node, Quat4d worldRot) {
\r
82 IG3DNode parent = (IG3DNode) node.getParent();
\r
83 if (parent == null) // this is a rootnode ( has no transformation)
\r
85 Quat4d local = getLocalOrientation(parent, worldRot);
\r
86 Quat4d q = new Quat4d();
\r
87 q.set(node.getOrientation());
\r
89 Quat4d q2 = new Quat4d();
\r
97 public static Matrix4d getWorldTransformation(IG3DNode node) {
\r
98 Vector3d pos = node.getWorldPosition();
\r
99 Quat4d q = node.getWorldOrientation();
\r
101 Matrix4d m1 = new Matrix4d();
\r
102 Matrix4d m2 = new Matrix4d();
\r
109 public static Matrix4d getWorldOrientationMat(IG3DNode node) {
\r
110 Quat4d q = node.getWorldOrientation();
\r
112 Matrix4d m2 = new Matrix4d();
\r
117 public static Vector3d getPosition(IG3DNode relative, IG3DNode node) {
\r
118 Vector3d wp = getWorldPosition(node);
\r
119 return getLocalPosition(relative, wp);
\r
122 public static Quat4d getOrientation(IG3DNode relative, IG3DNode node) {
\r
123 Quat4d wo = getWorldOrientation(node);
\r
124 return getLocalOrientation(relative, wo);
\r
127 public static void setPosition(IG3DNode relative, IG3DNode node, Vector3d position) {
\r
128 Vector3d wp = getWorldPosition(relative,position);
\r
129 node.setWorldPosition(wp);
\r
132 public static void setOrientation(IG3DNode relative, IG3DNode node, Quat4d orientation) {
\r
133 Quat4d wo = getWorldOrientation(relative,orientation);
\r
134 node.setWorldOrientation(wo);
\r