]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d/src/org/simantics/proconf/g3d/scenegraph/AbstractGraphicsNode.java
git-svn-id: https://www.simantics.org/svn/simantics/3d/trunk@22280 ac1ea38d-2e2b...
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / proconf / g3d / scenegraph / AbstractGraphicsNode.java
diff --git a/org.simantics.g3d/src/org/simantics/proconf/g3d/scenegraph/AbstractGraphicsNode.java b/org.simantics.g3d/src/org/simantics/proconf/g3d/scenegraph/AbstractGraphicsNode.java
new file mode 100644 (file)
index 0000000..b704969
--- /dev/null
@@ -0,0 +1,200 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.proconf.g3d.scenegraph;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import javax.vecmath.AxisAngle4d;\r
+import javax.vecmath.AxisAngle4f;\r
+import javax.vecmath.Matrix3d;\r
+import javax.vecmath.Quat4d;\r
+import javax.vecmath.Vector3f;\r
+\r
+import org.simantics.db.Graph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.proconf.g3d.base.G3DTools;\r
+import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
+import org.simantics.proconf.g3d.base.VecmathJmeTools;\r
+import org.simantics.proconf.g3d.stubs.G3DNode;\r
+\r
+import com.jme.scene.Node;\r
+\r
+\r
+public abstract class AbstractGraphicsNode implements ISelectableNode {\r
+\r
+       protected ThreeDimensionalEditorBase editor;\r
+       \r
+    protected IGraphicsNode parent = null;\r
+\r
+    protected Resource shapeResource = null;\r
+  \r
+    protected boolean selected;\r
+\r
+    protected Node parentGroup = null;\r
+    protected Node transform = null;\r
+   // protected Node center = null;\r
+    \r
+    protected String id;\r
\r
+    private ArrayList<IGraphicsNode> children = new ArrayList<IGraphicsNode>();\r
+\r
+    public AbstractGraphicsNode(ThreeDimensionalEditorBase editor, IGraphicsNode parent, Graph graph, Resource shapeResource) {\r
+        assert (parent != null);\r
+        this.editor = editor;\r
+        this.parent = parent;\r
+        this.parentGroup = parent.getGroup();\r
+        this.shapeResource = shapeResource;\r
+        this.id = editor.getScenegraphAdapter().getNodeUID(shapeResource);\r
+        createGroups();\r
+        updateTransform(graph);\r
+        parent.addChild(this);\r
+    }\r
+    \r
+    @Override\r
+    public String getID() {\r
+       return id;\r
+    }\r
+    \r
+    public void setID(String id) {\r
+       this.id = id;\r
+    }\r
+\r
+    \r
+    /* (non-Javadoc)\r
+     * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getParent()\r
+     */\r
+    public IGraphicsNode getParent() {\r
+        return parent;\r
+    }\r
+    \r
+    public Collection<IGraphicsNode> getChildren() {\r
+        return children;\r
+    }\r
+\r
+    private void createGroups() {\r
+       transform = new Node(); // TODO : uid\r
+       parentGroup.attachChild(transform);\r
+    }\r
+    \r
+    public void addChild(IGraphicsNode node) {\r
+        children.add(node);\r
+    }\r
+    \r
+    public void removeChild(IGraphicsNode node) {\r
+        children.remove(node);\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getGroup()\r
+     */\r
+    public Node getGroup() {\r
+       //return center;\r
+       \r
+       return transform;\r
+    }\r
+\r
+    /**\r
+     * Updates rotation and translation of the shape without recalculating\r
+     * geometry\r
+     */\r
+    public void updateTransform(Graph graph) {\r
+       G3DNode shape = getG3DNode(graph);\r
+       if (shape.getLocalPosition() != null)\r
+            transform.setLocalTranslation(VecmathJmeTools.get(G3DTools.getVectorFloat(shape.getLocalPosition())));\r
+        if (shape.getLocalOrientation() != null) \r
+            transform.setLocalRotation(VecmathJmeTools.get(G3DTools.getOrientationFloat(shape.getLocalOrientation())));\r
+//        if (GraphicsNodeTools.hasCenter(shape)) {\r
+//             center.setLocalTranslation(VecmathJmeTools.get(GraphicsNodeTools.getCenterFloat(shape)));\r
+//        }\r
+        // FIXME : typically transforms are updated once per frame (root as initiator) but with threaded access transformation may be read wrong.\r
+        transform.updateWorldVectors();\r
+        editor.getScenegraphAdapter().setChanged(true);\r
+    }\r
+\r
+\r
+    \r
+    protected void update(Matrix3d aa) {\r
+        transform.setLocalRotation(VecmathJmeTools.get(aa));\r
+    }\r
+    protected void update(AxisAngle4f aa) {\r
+        transform.setLocalRotation(VecmathJmeTools.get(aa));\r
+    }\r
+    \r
+    protected void update(AxisAngle4d aa) {\r
+        transform.setLocalRotation(VecmathJmeTools.get(aa));\r
+    }\r
+    \r
+    protected void update(Quat4d q) {\r
+        transform.setLocalRotation(VecmathJmeTools.get(q));\r
+    }\r
+    \r
+    protected void update(Vector3f v) {\r
+        transform.setLocalTranslation(VecmathJmeTools.get(v));\r
+    }\r
+\r
+\r
+    public void setSelected(boolean selected) {\r
+        if (this.selected == selected)\r
+            return;\r
+        this.selected = selected;\r
+    }\r
+\r
+    public boolean isSelected() {\r
+        return selected;\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getResource()\r
+     */\r
+    public Resource getResource() {\r
+        return shapeResource;\r
+    }\r
+\r
+    public G3DNode getG3DNode(Graph graph) {\r
+        return new G3DNode(graph,shapeResource);\r
+    }\r
+\r
+    /* (non-Javadoc)\r
+     * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#dispose()\r
+     */\r
+    public void dispose() {\r
+//        if (children.size() != 0) {\r
+//            System.out.print(getResource() + " contains children: ");\r
+//            ArrayList<IGraphicsNode> c = new ArrayList<IGraphicsNode>(children);\r
+//            for (IGraphicsNode n : c) {\r
+//              System.out.print(n.getResource() + " ");\r
+//            }\r
+//            System.out.println();\r
+//            return;\r
+//        }\r
+        assert (children.size() == 0);\r
+\r
+        transform.removeFromParent();\r
+        transform.dispose();\r
+        if (parent != null)\r
+            parent.removeChild(this);\r
+    }\r
+    \r
+    \r
+    public abstract void setPickable(boolean pickable);\r
+    \r
+    public String toString() {\r
+        return  this.getClass().toString();\r
+    }\r
+    \r
+    @Override\r
+    public int hashCode() {\r
+       return shapeResource.hashCode();\r
+    }\r
+\r
+\r
+}
\ No newline at end of file