1 /*******************************************************************************
\r
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.proconf.g3d.scenegraph;
\r
13 import java.util.ArrayList;
\r
14 import java.util.Collection;
\r
16 import javax.vecmath.AxisAngle4d;
\r
17 import javax.vecmath.AxisAngle4f;
\r
18 import javax.vecmath.Matrix3d;
\r
19 import javax.vecmath.Quat4d;
\r
20 import javax.vecmath.Vector3f;
\r
22 import org.simantics.db.Graph;
\r
23 import org.simantics.db.Resource;
\r
24 import org.simantics.proconf.g3d.base.G3DTools;
\r
25 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;
\r
26 import org.simantics.proconf.g3d.base.VecmathJmeTools;
\r
27 import org.simantics.proconf.g3d.stubs.G3DNode;
\r
29 import com.jme.scene.Node;
\r
32 public abstract class AbstractGraphicsNode implements ISelectableNode {
\r
34 protected ThreeDimensionalEditorBase editor;
\r
36 protected IGraphicsNode parent = null;
\r
38 protected Resource shapeResource = null;
\r
40 protected boolean selected;
\r
42 protected Node parentGroup = null;
\r
43 protected Node transform = null;
\r
44 // protected Node center = null;
\r
46 protected String id;
\r
48 private ArrayList<IGraphicsNode> children = new ArrayList<IGraphicsNode>();
\r
50 public AbstractGraphicsNode(ThreeDimensionalEditorBase editor, IGraphicsNode parent, Graph graph, Resource shapeResource) {
\r
51 assert (parent != null);
\r
52 this.editor = editor;
\r
53 this.parent = parent;
\r
54 this.parentGroup = parent.getGroup();
\r
55 this.shapeResource = shapeResource;
\r
56 this.id = editor.getScenegraphAdapter().getNodeUID(shapeResource);
\r
58 updateTransform(graph);
\r
59 parent.addChild(this);
\r
63 public String getID() {
\r
67 public void setID(String id) {
\r
73 * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getParent()
\r
75 public IGraphicsNode getParent() {
\r
79 public Collection<IGraphicsNode> getChildren() {
\r
83 private void createGroups() {
\r
84 transform = new Node(); // TODO : uid
\r
85 parentGroup.attachChild(transform);
\r
88 public void addChild(IGraphicsNode node) {
\r
92 public void removeChild(IGraphicsNode node) {
\r
93 children.remove(node);
\r
97 * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getGroup()
\r
99 public Node getGroup() {
\r
106 * Updates rotation and translation of the shape without recalculating
\r
109 public void updateTransform(Graph graph) {
\r
110 G3DNode shape = getG3DNode(graph);
\r
111 if (shape.getLocalPosition() != null)
\r
112 transform.setLocalTranslation(VecmathJmeTools.get(G3DTools.getVectorFloat(shape.getLocalPosition())));
\r
113 if (shape.getLocalOrientation() != null)
\r
114 transform.setLocalRotation(VecmathJmeTools.get(G3DTools.getOrientationFloat(shape.getLocalOrientation())));
\r
115 // if (GraphicsNodeTools.hasCenter(shape)) {
\r
116 // center.setLocalTranslation(VecmathJmeTools.get(GraphicsNodeTools.getCenterFloat(shape)));
\r
118 // FIXME : typically transforms are updated once per frame (root as initiator) but with threaded access transformation may be read wrong.
\r
119 transform.updateWorldVectors();
\r
120 editor.getScenegraphAdapter().setChanged(true);
\r
125 protected void update(Matrix3d aa) {
\r
126 transform.setLocalRotation(VecmathJmeTools.get(aa));
\r
128 protected void update(AxisAngle4f aa) {
\r
129 transform.setLocalRotation(VecmathJmeTools.get(aa));
\r
132 protected void update(AxisAngle4d aa) {
\r
133 transform.setLocalRotation(VecmathJmeTools.get(aa));
\r
136 protected void update(Quat4d q) {
\r
137 transform.setLocalRotation(VecmathJmeTools.get(q));
\r
140 protected void update(Vector3f v) {
\r
141 transform.setLocalTranslation(VecmathJmeTools.get(v));
\r
145 public void setSelected(boolean selected) {
\r
146 if (this.selected == selected)
\r
148 this.selected = selected;
\r
151 public boolean isSelected() {
\r
156 * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#getResource()
\r
158 public Resource getResource() {
\r
159 return shapeResource;
\r
162 public G3DNode getG3DNode(Graph graph) {
\r
163 return new G3DNode(graph,shapeResource);
\r
167 * @see fi.vtt.proconf.shapeeditor.geometry.IGraphicsNode#dispose()
\r
169 public void dispose() {
\r
170 // if (children.size() != 0) {
\r
171 // System.out.print(getResource() + " contains children: ");
\r
172 // ArrayList<IGraphicsNode> c = new ArrayList<IGraphicsNode>(children);
\r
173 // for (IGraphicsNode n : c) {
\r
174 // System.out.print(n.getResource() + " ");
\r
176 // System.out.println();
\r
179 assert (children.size() == 0);
\r
181 transform.removeFromParent();
\r
182 transform.dispose();
\r
183 if (parent != null)
\r
184 parent.removeChild(this);
\r
188 public abstract void setPickable(boolean pickable);
\r
190 public String toString() {
\r
191 return this.getClass().toString();
\r
195 public int hashCode() {
\r
196 return shapeResource.hashCode();
\r