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;
16 import java.util.Objects;
18 import javax.vecmath.AxisAngle4d;
19 import javax.vecmath.Quat4d;
20 import javax.vecmath.Vector3d;
22 import org.jcae.opencascade.jni.TopoDS_Shape;
23 import org.simantics.g3d.csg.ontology.CSG;
24 import org.simantics.g3d.math.MathTools;
25 import org.simantics.g3d.ontology.G3D;
26 import org.simantics.g3d.property.annotations.GetPropertyValue;
27 import org.simantics.g3d.property.annotations.PropertyContributor;
28 import org.simantics.g3d.property.annotations.SetPropertyValue;
29 import org.simantics.g3d.scenegraph.IG3DNode;
30 import org.simantics.g3d.scenegraph.base.ParentNode;
31 import org.simantics.g3d.tools.NodeTools;
32 import org.simantics.g3d.vtk.common.VtkView;
33 import org.simantics.layer0.Layer0;
34 import org.simantics.objmap.graph.annotations.RelatedElementsAdd;
35 import org.simantics.objmap.graph.annotations.RelatedElementsGet;
36 import org.simantics.objmap.graph.annotations.RelatedElementsRem;
37 import org.simantics.objmap.graph.annotations.RelatedGetValue;
38 import org.simantics.objmap.graph.annotations.RelatedSetValue;
39 import org.simantics.opencascade.OccTriangulator;
40 import org.simantics.opencascade.vtk.vtkSolidObject;
41 import org.simantics.utils.threads.AWTThread;
46 public abstract class CSGparentNode extends ParentNode<ICSGnode> implements ICSGnode {
51 @RelatedGetValue(Layer0.URIs.HasName)
52 @GetPropertyValue(value = Layer0.URIs.HasName, tabId = "Default", name = "Name")
53 public String getName() {
57 @RelatedSetValue(Layer0.URIs.HasName)
58 @SetPropertyValue(Layer0.URIs.HasName)
59 public void setName(String name) {
60 if (name == null || name.equals(this.name))
63 firePropertyChanged(Layer0.URIs.HasName);
67 public String toString() {
71 private Vector3d position = new Vector3d();
72 private Quat4d orientation = MathTools.getIdentityQuat();
75 @GetPropertyValue(value = G3D.URIs.hasOrientation, tabId = "Transform", name = "Orientation")
76 public Quat4d getOrientation() {
80 @RelatedGetValue(G3D.URIs.hasOrientation)
81 public double[] getOrientationArr() {
82 double arr[] = new double[4];
89 @GetPropertyValue(value = G3D.URIs.hasPosition, tabId = "Transform", name = "Position")
90 public Vector3d getPosition() {
94 @RelatedGetValue(G3D.URIs.hasPosition)
95 public double[] getPositionArr() {
96 double arr[] = new double[3];
101 @RelatedElementsAdd(CSG.URIs.hasPrimaryShape)
102 public void addPrimaryChild(ICSGnode node) {
103 addNode("primary",node);
106 @RelatedElementsGet(CSG.URIs.hasPrimaryShape)
107 public Collection<ICSGnode> getPrimaryChild() {
108 return getNodes("primary");
111 @RelatedElementsRem(CSG.URIs.hasPrimaryShape)
112 public void remPrimaryChild(ICSGnode node) {
113 removeNode("primary", node);
116 @RelatedElementsAdd(CSG.URIs.hasSecondaryShape)
117 public void addSecondaryChild(ICSGnode node) {
118 addNode("secondary",node);
121 @RelatedElementsGet(CSG.URIs.hasSecondaryShape)
122 public Collection<ICSGnode> getSecondaryChild() {
123 return getNodes("secondary");
126 @RelatedElementsRem(CSG.URIs.hasSecondaryShape)
127 public void remSecondaryChild(ICSGnode node) {
128 removeNode("secondary", node);
132 @RelatedElementsAdd(CSG.URIs.hasChildShape)
133 public void addChild(ICSGnode node) {
134 addNode("child",node);
137 @RelatedElementsGet(CSG.URIs.hasChildShape)
138 public Collection<ICSGnode> getChild() {
139 return getNodes("child");
142 @RelatedElementsRem(CSG.URIs.hasChildShape)
143 public void remChild(ICSGnode node) {
144 removeNode("child", node);
149 protected TopoDS_Shape getPrimary() {
150 for (ICSGnode node : getNodes("primary"))
151 return node.getGeometry();
155 protected TopoDS_Shape getSecondary() {
156 for (ICSGnode node : getNodes("secondary"))
157 return node.getGeometry();
162 public TopoDS_Shape getGeometry() {
163 TopoDS_Shape shape = getBaseGeometry();
166 Quat4d q = getOrientation();
167 AxisAngle4d r = new AxisAngle4d();
169 TopoDS_Shape tshape = OccTriangulator.makeRotation(shape, new double[] { 0.0, 0.0, 0.0, r.x, r.y, r.z }, r.angle);
172 Vector3d p = getPosition();
173 tshape = OccTriangulator.makeTranslation(shape, p.x, p.y, p.z);
182 @SetPropertyValue(G3D.URIs.hasOrientation)
183 public void setOrientation(Quat4d orientation) {
184 assert(orientation != null);
185 if (Objects.equals(this.orientation, orientation))
188 this.orientation = orientation;
190 firePropertyChanged(G3D.URIs.hasOrientation);
194 @SetPropertyValue(G3D.URIs.hasPosition)
195 public void setPosition(Vector3d position) {
196 assert(position != null);
197 if (Objects.equals(this.position, position))
200 this.position = position;
202 firePropertyChanged(G3D.URIs.hasPosition);
205 @RelatedSetValue(G3D.URIs.hasOrientation)
206 public void setOrientation(double[] arr) {
209 setOrientation(new Quat4d(arr));
212 @RelatedSetValue(G3D.URIs.hasPosition)
213 public void setPosition(double[] arr) {
216 setPosition(new Vector3d(arr));
220 @GetPropertyValue(value = G3D.URIs.hasWorldPosition, tabId = "Transform", name = "World Position")
221 public Vector3d getWorldPosition() {
222 IG3DNode parent = (IG3DNode) getParent();
225 return NodeTools.getWorldPosition(parent, new Vector3d(position));
228 public Vector3d getWorldPosition(Vector3d localPosition) {
229 return NodeTools.getWorldPosition(this, localPosition);
234 @GetPropertyValue(value = G3D.URIs.hasWorldOrientation, tabId = "Transform", name = "World Orientation")
235 public Quat4d getWorldOrientation() {
236 return getWorldOrientation(new Quat4d(orientation));
239 public Quat4d getWorldOrientation(Quat4d localOrientation) {
240 IG3DNode parent = (IG3DNode)getParent();
242 return localOrientation;
243 return NodeTools.getWorldOrientation(parent, localOrientation);
247 public Vector3d getLocalPosition(Vector3d worldPosition) {
248 IG3DNode parent = (IG3DNode)getParent();
250 return worldPosition;
251 return NodeTools.getLocalPosition(parent,new Vector3d(worldPosition));
255 public Quat4d getLocalOrientation(Quat4d worldOrientation) {
256 IG3DNode parent = (IG3DNode)getParent();
258 return worldOrientation;
259 return NodeTools.getLocalOrientation(parent, new Quat4d(worldOrientation));
263 @SetPropertyValue(G3D.URIs.hasWorldPosition)
264 public void setWorldPosition(Vector3d position) {
265 Vector3d localPos = getLocalPosition(position);
266 setPosition(localPos);
270 @SetPropertyValue(G3D.URIs.hasWorldOrientation)
271 public void setWorldOrientation(Quat4d orientation) {
272 Quat4d localOr = getLocalOrientation(orientation);
273 setOrientation(localOr);
277 private vtkSolidObject solidObject;
280 public void visualize(VtkView panel) {
281 if (solidObject != null) {
282 solidObject.delete();
285 TopoDS_Shape shape = getGeometry();
288 solidObject = new vtkSolidObject(panel, shape);
289 solidObject.visualizeSolid(true, false);
292 @SuppressWarnings("unchecked")
293 public Collection<vtkProp3D> getActors() {
294 if (solidObject == null)
295 return Collections.EMPTY_LIST;
296 return solidObject.getActors();
299 public void stopVisualize() {
300 if (solidObject != null) {
301 if (Thread.currentThread() == AWTThread.getThreadAccess().getThread())
302 solidObject.delete();
304 solidObject.dispose();
310 public void cleanup() {
317 public void remove() {
318 //FIXME: creating boolean shapes (removing nodes from parent and attaching under boolean shape would destroy the existing hierarchy, if default implementation is used.
323 public <C> C getAdapter(Class<C> adapter) {