]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.csg/src/org/simantics/g3d/csg/scenegraph2/CSGnode.java
Using SWT thread with Plant3d
[simantics/3d.git] / org.simantics.g3d.csg / src / org / simantics / g3d / csg / scenegraph2 / CSGnode.java
1 /*******************************************************************************
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g3d.csg.scenegraph2;
13
14 import java.util.Collection;
15 import java.util.Collections;
16
17 import javax.vecmath.AxisAngle4d;
18 import javax.vecmath.Quat4d;
19 import javax.vecmath.Vector3d;
20
21 import org.jcae.opencascade.jni.TopoDS_Shape;
22 import org.simantics.g3d.property.annotations.GetPropertyValue;
23 import org.simantics.g3d.property.annotations.SetPropertyValue;
24 import org.simantics.g3d.scenegraph.G3DNode;
25 import org.simantics.g3d.vtk.common.VtkView;
26 import org.simantics.layer0.Layer0;
27 import org.simantics.objmap.graph.annotations.RelatedGetValue;
28 import org.simantics.objmap.graph.annotations.RelatedSetValue;
29 import org.simantics.opencascade.OccTriangulator;
30 import org.simantics.opencascade.vtk.vtkSolidObject;
31 import org.simantics.utils.threads.AWTThread;
32
33 import vtk.vtkProp3D;
34
35 public abstract class CSGnode extends G3DNode implements ICSGnode {
36
37         public static final double MIN_VALUE = 0.001;
38         
39         private String name;
40         
41
42         @RelatedGetValue(Layer0.URIs.HasName)
43         @GetPropertyValue(value = Layer0.URIs.HasName, tabId = "Default", name = "Name")
44         public String getName() {
45                 return name;
46         }
47         
48         @RelatedSetValue(Layer0.URIs.HasName)
49         @SetPropertyValue(Layer0.URIs.HasName)
50         public void setName(String name) {
51                 if (name == null)
52                         return;
53                 this.name = name;
54                 firePropertyChanged(Layer0.URIs.HasName);
55         }
56         
57         @Override
58         public String toString() {
59                 return getName();
60         }
61         
62         
63         private vtkSolidObject solidObject;
64         
65         @Override
66         public TopoDS_Shape getGeometry() {
67                 TopoDS_Shape shape = getBaseGeometry();
68                 if (shape == null)
69                         return null;
70                 Quat4d q = getOrientation();
71                 AxisAngle4d r = new AxisAngle4d();
72                 r.set(q);
73                 TopoDS_Shape tshape = OccTriangulator.makeRotation(shape, new double[] { 0.0, 0.0, 0.0, r.x, r.y, r.z }, r.angle);
74                 shape.delete();
75                 shape = tshape;
76                 Vector3d p = getPosition();
77                 tshape = OccTriangulator.makeTranslation(shape, p.x, p.y, p.z);
78                 shape.delete();
79                 return tshape;  
80         }
81         
82         public void visualize(VtkView panel) {
83                 if (solidObject != null) {
84                         solidObject.delete();
85                         solidObject = null;
86                 }
87                 TopoDS_Shape shape = getGeometry();
88                 if (shape == null)
89                         return;
90                 solidObject = new vtkSolidObject(panel, shape);
91                 solidObject.visualizeSolid(true, false);
92         }
93         
94         @SuppressWarnings("unchecked")
95         public Collection<vtkProp3D> getActors() {
96                 if (solidObject == null)
97                         return Collections.EMPTY_LIST;
98                 return solidObject.getActors();
99         }
100         
101         public void stopVisualize() {
102                 if (solidObject != null) {
103                         if (Thread.currentThread() == AWTThread.getThreadAccess().getThread())
104                                 solidObject.delete();
105                         else
106                                 solidObject.dispose();
107                         solidObject = null;
108                 }
109         }
110         
111         @Override
112         public void cleanup() {
113                 stopVisualize();
114                 super.cleanup();
115         }
116         
117         
118
119 }