1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.csg.scenegraph2;
14 import java.util.Collection;
15 import java.util.Collections;
17 import javax.vecmath.AxisAngle4d;
18 import javax.vecmath.Quat4d;
19 import javax.vecmath.Vector3d;
21 import org.jcae.opencascade.jni.TopoDS_Shape;
22 import org.simantics.g3d.csg.ontology.CSG;
23 import org.simantics.g3d.math.MathTools;
24 import org.simantics.g3d.ontology.G3D;
25 import org.simantics.g3d.property.annotations.GetPropertyValue;
26 import org.simantics.g3d.property.annotations.PropertyContributor;
27 import org.simantics.g3d.property.annotations.SetPropertyValue;
28 import org.simantics.g3d.scenegraph.IG3DNode;
29 import org.simantics.g3d.scenegraph.base.ParentNode;
30 import org.simantics.g3d.tools.NodeTools;
31 import org.simantics.g3d.vtk.common.VtkView;
32 import org.simantics.layer0.Layer0;
33 import org.simantics.objmap.graph.annotations.RelatedElementsAdd;
34 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
35 import org.simantics.objmap.graph.annotations.RelatedElementsRem;
36 import org.simantics.objmap.graph.annotations.RelatedGetValue;
37 import org.simantics.objmap.graph.annotations.RelatedSetValue;
38 import org.simantics.opencascade.OccTriangulator;
39 import org.simantics.opencascade.vtk.vtkSolidObject;
40 import org.simantics.utils.threads.AWTThread;
45 public abstract class CSGparentNode extends ParentNode<ICSGnode> implements ICSGnode {
50 @RelatedGetValue(Layer0.URIs.HasName)
51 @GetPropertyValue(value = Layer0.URIs.HasName, tabId = "Default", name = "Name")
52 public String getName() {
56 @RelatedSetValue(Layer0.URIs.HasName)
57 @SetPropertyValue(Layer0.URIs.HasName)
58 public void setName(String name) {
62 firePropertyChanged(Layer0.URIs.HasName);
66 public String toString() {
70 private Vector3d position = new Vector3d();
71 private Quat4d orientation = MathTools.getIdentityQuat();
74 @GetPropertyValue(value = G3D.URIs.hasOrientation, tabId = "Transform", name = "Orientation")
75 public Quat4d getOrientation() {
79 @RelatedGetValue(G3D.URIs.hasOrientation)
80 public double[] getOrientationArr() {
81 double arr[] = new double[4];
88 @GetPropertyValue(value = G3D.URIs.hasPosition, tabId = "Transform", name = "Position")
89 public Vector3d getPosition() {
93 @RelatedGetValue(G3D.URIs.hasPosition)
94 public double[] getPositionArr() {
95 double arr[] = new double[3];
100 @RelatedElementsAdd(CSG.URIs.hasPrimaryShape)
101 public void addPrimaryChild(ICSGnode node) {
102 addNode("primary",node);
105 @RelatedElementsGet(CSG.URIs.hasPrimaryShape)
106 public Collection<ICSGnode> getPrimaryChild() {
107 return getNodes("primary");
110 @RelatedElementsRem(CSG.URIs.hasPrimaryShape)
111 public void remPrimaryChild(ICSGnode node) {
112 removeNode("primary", node);
115 @RelatedElementsAdd(CSG.URIs.hasSecondaryShape)
116 public void addSecondaryChild(ICSGnode node) {
117 addNode("secondary",node);
120 @RelatedElementsGet(CSG.URIs.hasSecondaryShape)
121 public Collection<ICSGnode> getSecondaryChild() {
122 return getNodes("secondary");
125 @RelatedElementsRem(CSG.URIs.hasSecondaryShape)
126 public void remSecondaryChild(ICSGnode node) {
127 removeNode("secondary", node);
131 @RelatedElementsAdd(CSG.URIs.hasChildShape)
132 public void addChild(ICSGnode node) {
133 addNode("child",node);
136 @RelatedElementsGet(CSG.URIs.hasChildShape)
137 public Collection<ICSGnode> getChild() {
138 return getNodes("child");
141 @RelatedElementsRem(CSG.URIs.hasChildShape)
142 public void remChild(ICSGnode node) {
143 removeNode("child", node);
148 protected TopoDS_Shape getPrimary() {
149 for (ICSGnode node : getNodes("primary"))
150 return node.getGeometry();
154 protected TopoDS_Shape getSecondary() {
155 for (ICSGnode node : getNodes("secondary"))
156 return node.getGeometry();
161 public TopoDS_Shape getGeometry() {
162 TopoDS_Shape shape = getBaseGeometry();
165 Quat4d q = getOrientation();
166 AxisAngle4d r = new AxisAngle4d();
168 TopoDS_Shape tshape = OccTriangulator.makeRotation(shape, new double[] { 0.0, 0.0, 0.0, r.x, r.y, r.z }, r.angle);
171 Vector3d p = getPosition();
172 tshape = OccTriangulator.makeTranslation(shape, p.x, p.y, p.z);
181 @SetPropertyValue(G3D.URIs.hasOrientation)
182 public void setOrientation(Quat4d orientation) {
183 assert(orientation != null);
184 this.orientation = orientation;
186 firePropertyChanged(G3D.URIs.hasOrientation);
190 @SetPropertyValue(G3D.URIs.hasPosition)
191 public void setPosition(Vector3d position) {
192 assert(position != null);
193 this.position = position;
195 firePropertyChanged(G3D.URIs.hasPosition);
198 @RelatedSetValue(G3D.URIs.hasOrientation)
199 public void setOrientation(double[] arr) {
202 setOrientation(new Quat4d(arr));
205 @RelatedSetValue(G3D.URIs.hasPosition)
206 public void setPosition(double[] arr) {
209 setPosition(new Vector3d(arr));
213 @GetPropertyValue(value = G3D.URIs.hasWorldPosition, tabId = "Transform", name = "World Position")
214 public Vector3d getWorldPosition() {
215 IG3DNode parent = (IG3DNode) getParent();
218 return NodeTools.getWorldPosition(parent, new Vector3d(position));
221 public Vector3d getWorldPosition(Vector3d localPosition) {
222 return NodeTools.getWorldPosition(this, localPosition);
227 @GetPropertyValue(value = G3D.URIs.hasWorldOrientation, tabId = "Transform", name = "World Orientation")
228 public Quat4d getWorldOrientation() {
229 return getWorldOrientation(new Quat4d(orientation));
232 public Quat4d getWorldOrientation(Quat4d localOrientation) {
233 IG3DNode parent = (IG3DNode)getParent();
235 return localOrientation;
236 return NodeTools.getWorldOrientation(parent, localOrientation);
240 public Vector3d getLocalPosition(Vector3d worldPosition) {
241 IG3DNode parent = (IG3DNode)getParent();
243 return worldPosition;
244 return NodeTools.getLocalPosition(parent,new Vector3d(worldPosition));
248 public Quat4d getLocalOrientation(Quat4d worldOrientation) {
249 IG3DNode parent = (IG3DNode)getParent();
251 return worldOrientation;
252 return NodeTools.getLocalOrientation(parent, new Quat4d(worldOrientation));
256 @SetPropertyValue(G3D.URIs.hasWorldPosition)
257 public void setWorldPosition(Vector3d position) {
258 Vector3d localPos = getLocalPosition(position);
259 setPosition(localPos);
263 @SetPropertyValue(G3D.URIs.hasWorldOrientation)
264 public void setWorldOrientation(Quat4d orientation) {
265 Quat4d localOr = getLocalOrientation(orientation);
266 setOrientation(localOr);
270 private vtkSolidObject solidObject;
273 public void visualize(VtkView panel) {
274 if (solidObject != null) {
275 solidObject.delete();
278 TopoDS_Shape shape = getGeometry();
281 solidObject = new vtkSolidObject(panel, shape);
282 solidObject.visualizeSolid(true, false);
285 @SuppressWarnings("unchecked")
286 public Collection<vtkProp3D> getActors() {
287 if (solidObject == null)
288 return Collections.EMPTY_LIST;
289 return solidObject.getActors();
292 public void stopVisualize() {
293 if (solidObject != null) {
294 if (Thread.currentThread() == AWTThread.getThreadAccess().getThread())
295 solidObject.delete();
297 solidObject.dispose();
303 public void cleanup() {
310 public void remove() {
311 //FIXME: creating boolean shapes (removing nodes from parent and attaching under boolean shape would destroy the existing hierarchy, if default implementation is used.
316 public <C> C getAdapter(Class<C> adapter) {