/******************************************************************************* * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. * 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.proconf.g3d.scenegraph; import java.util.ArrayList; import java.util.Collection; import javax.vecmath.AxisAngle4d; import javax.vecmath.AxisAngle4f; import javax.vecmath.Matrix3d; import javax.vecmath.Quat4d; import javax.vecmath.Vector3f; import org.simantics.db.Graph; import org.simantics.db.Resource; import org.simantics.proconf.g3d.base.G3DTools; import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase; import org.simantics.proconf.g3d.base.VecmathJmeTools; import org.simantics.proconf.g3d.stubs.G3DNode; import com.jme.scene.Node; public abstract class AbstractGraphicsNode implements ISelectableNode { protected ThreeDimensionalEditorBase editor; protected IGraphicsNode parent = null; protected Resource shapeResource = null; protected boolean selected; protected Node parentGroup = null; protected Node transform = null; // protected Node center = null; protected String id; private ArrayList children = new ArrayList(); public AbstractGraphicsNode(ThreeDimensionalEditorBase editor, IGraphicsNode parent, Graph graph, Resource shapeResource) { assert (parent != null); this.editor = editor; this.parent = parent; this.parentGroup = parent.getGroup(); this.shapeResource = shapeResource; this.id = editor.getScenegraphAdapter().getNodeUID(shapeResource); createGroups(); updateTransform(graph); parent.addChild(this); } @Override public String getID() { return id; } public void setID(String id) { this.id = id; } /* (non-Javadoc) * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getParent() */ public IGraphicsNode getParent() { return parent; } public Collection getChildren() { return children; } private void createGroups() { transform = new Node(); // TODO : uid parentGroup.attachChild(transform); } public void addChild(IGraphicsNode node) { children.add(node); } public void removeChild(IGraphicsNode node) { children.remove(node); } /* (non-Javadoc) * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getGroup() */ public Node getGroup() { //return center; return transform; } /** * Updates rotation and translation of the shape without recalculating * geometry */ public void updateTransform(Graph graph) { G3DNode shape = getG3DNode(graph); if (shape.getLocalPosition() != null) transform.setLocalTranslation(VecmathJmeTools.get(G3DTools.getVectorFloat(shape.getLocalPosition()))); if (shape.getLocalOrientation() != null) transform.setLocalRotation(VecmathJmeTools.get(G3DTools.getOrientationFloat(shape.getLocalOrientation()))); // if (GraphicsNodeTools.hasCenter(shape)) { // center.setLocalTranslation(VecmathJmeTools.get(GraphicsNodeTools.getCenterFloat(shape))); // } // FIXME : typically transforms are updated once per frame (root as initiator) but with threaded access transformation may be read wrong. transform.updateWorldVectors(); editor.getScenegraphAdapter().setChanged(true); } protected void update(Matrix3d aa) { transform.setLocalRotation(VecmathJmeTools.get(aa)); } protected void update(AxisAngle4f aa) { transform.setLocalRotation(VecmathJmeTools.get(aa)); } protected void update(AxisAngle4d aa) { transform.setLocalRotation(VecmathJmeTools.get(aa)); } protected void update(Quat4d q) { transform.setLocalRotation(VecmathJmeTools.get(q)); } protected void update(Vector3f v) { transform.setLocalTranslation(VecmathJmeTools.get(v)); } public void setSelected(boolean selected) { if (this.selected == selected) return; this.selected = selected; } public boolean isSelected() { return selected; } /* (non-Javadoc) * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getResource() */ public Resource getResource() { return shapeResource; } public G3DNode getG3DNode(Graph graph) { return new G3DNode(graph,shapeResource); } /* (non-Javadoc) * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#dispose() */ public void dispose() { // if (children.size() != 0) { // System.out.print(getResource() + " contains children: "); // ArrayList c = new ArrayList(children); // for (IGraphicsNode n : c) { // System.out.print(n.getResource() + " "); // } // System.out.println(); // return; // } assert (children.size() == 0); transform.removeFromParent(); transform.dispose(); if (parent != null) parent.removeChild(this); } public abstract void setPickable(boolean pickable); public String toString() { return this.getClass().toString(); } @Override public int hashCode() { return shapeResource.hashCode(); } }