]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.csg/src/org/simantics/g3d/csg/scenegraph2/CSGparentNode.java
Perform view direction switching via vtkCameraAndSelectorAction
[simantics/3d.git] / org.simantics.g3d.csg / src / org / simantics / g3d / csg / scenegraph2 / CSGparentNode.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.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;
41
42 import vtk.vtkProp3D;
43
44 @PropertyContributor
45 public abstract class CSGparentNode extends ParentNode<ICSGnode> implements ICSGnode {
46
47         private String name;
48         
49
50         @RelatedGetValue(Layer0.URIs.HasName)
51         @GetPropertyValue(value = Layer0.URIs.HasName, tabId = "Default", name = "Name")
52         public String getName() {
53                 return name;
54         }
55         
56         @RelatedSetValue(Layer0.URIs.HasName)
57         @SetPropertyValue(Layer0.URIs.HasName)
58         public void setName(String name) {
59                 if (name == null)
60                         return;
61                 this.name = name;
62                 firePropertyChanged(Layer0.URIs.HasName);
63         }
64         
65         @Override
66         public String toString() {
67                 return getName();
68         }
69         
70         private Vector3d position = new Vector3d();
71         private Quat4d orientation = MathTools.getIdentityQuat();
72         
73         @Override
74         @GetPropertyValue(value = G3D.URIs.hasOrientation, tabId = "Transform", name = "Orientation")
75         public Quat4d getOrientation() {
76                 return orientation;
77         }
78         
79         @RelatedGetValue(G3D.URIs.hasOrientation)
80         public double[] getOrientationArr() {
81                 double arr[] = new double[4];
82                 orientation.get(arr);
83                 return arr;
84                 
85         }
86         
87         @Override
88         @GetPropertyValue(value = G3D.URIs.hasPosition, tabId = "Transform", name = "Position")
89         public Vector3d getPosition() {
90                 return position;
91         }
92         
93         @RelatedGetValue(G3D.URIs.hasPosition)
94         public double[] getPositionArr() {
95                 double arr[] = new double[3];
96                 position.get(arr);
97                 return arr;
98         }
99         
100         @RelatedElementsAdd(CSG.URIs.hasPrimaryShape)
101         public void addPrimaryChild(ICSGnode node) {
102                 addNode("primary",node);
103         }
104         
105         @RelatedElementsGet(CSG.URIs.hasPrimaryShape)
106         public Collection<ICSGnode> getPrimaryChild() {
107                 return getNodes("primary");
108         }
109         
110         @RelatedElementsRem(CSG.URIs.hasPrimaryShape)
111         public void remPrimaryChild(ICSGnode node) {
112                 removeNode("primary", node);
113         }
114         
115         @RelatedElementsAdd(CSG.URIs.hasSecondaryShape)
116         public void addSecondaryChild(ICSGnode node) {
117                 addNode("secondary",node);
118         }
119         
120         @RelatedElementsGet(CSG.URIs.hasSecondaryShape)
121         public Collection<ICSGnode> getSecondaryChild() {
122                 return getNodes("secondary");
123         }
124         
125         @RelatedElementsRem(CSG.URIs.hasSecondaryShape)
126         public void remSecondaryChild(ICSGnode node) {
127                 removeNode("secondary", node);
128         }
129         
130
131         @RelatedElementsAdd(CSG.URIs.hasChildShape)
132         public void addChild(ICSGnode node) {
133                 addNode("child",node);
134         }
135         
136         @RelatedElementsGet(CSG.URIs.hasChildShape)
137         public Collection<ICSGnode> getChild() {
138                 return getNodes("child");
139         }
140         
141         @RelatedElementsRem(CSG.URIs.hasChildShape)
142         public void remChild(ICSGnode node) {
143                 removeNode("child", node);
144         }
145         
146         
147         
148         protected TopoDS_Shape getPrimary() {
149                 for (ICSGnode node : getNodes("primary"))
150                         return node.getGeometry();
151                 return null;
152         }
153         
154         protected TopoDS_Shape getSecondary() {
155                 for (ICSGnode node : getNodes("secondary"))
156                         return node.getGeometry();
157                 return null;
158         }
159         
160         @Override
161         public TopoDS_Shape getGeometry() {
162                 TopoDS_Shape shape = getBaseGeometry();
163                 if (shape == null)
164                         return null;
165                 Quat4d q = getOrientation();
166                 AxisAngle4d r = new AxisAngle4d();
167                 r.set(q);
168                 TopoDS_Shape tshape = OccTriangulator.makeRotation(shape, new double[] { 0.0, 0.0, 0.0, r.x, r.y, r.z }, r.angle);
169                 shape.delete();
170                 shape = tshape;
171                 Vector3d p = getPosition();
172                 tshape = OccTriangulator.makeTranslation(shape, p.x, p.y, p.z);
173                 shape.delete();
174                 return tshape;
175                         
176         }
177         
178
179         
180         @Override
181         @SetPropertyValue(G3D.URIs.hasOrientation)
182         public void setOrientation(Quat4d orientation) {
183                 assert(orientation != null);
184                 this.orientation = orientation;
185                 
186                 firePropertyChanged(G3D.URIs.hasOrientation);
187         }
188         
189         @Override
190         @SetPropertyValue(G3D.URIs.hasPosition)
191         public void setPosition(Vector3d position) {
192                 assert(position != null);
193                 this.position = position;
194                 
195                 firePropertyChanged(G3D.URIs.hasPosition);
196         }
197         
198         @RelatedSetValue(G3D.URIs.hasOrientation)
199         public void setOrientation(double[] arr) {
200                 if (arr == null)
201                         return;
202                 setOrientation(new Quat4d(arr));
203         }
204         
205         @RelatedSetValue(G3D.URIs.hasPosition)
206         public void setPosition(double[] arr) {
207                 if (arr == null)
208                         return;
209                 setPosition(new Vector3d(arr));
210         }
211         
212         @Override
213         @GetPropertyValue(value = G3D.URIs.hasWorldPosition, tabId = "Transform", name = "World Position")
214         public Vector3d getWorldPosition() {
215                 IG3DNode parent = (IG3DNode) getParent();
216                 if (parent == null)
217                         return position;
218                 return NodeTools.getWorldPosition(parent, new Vector3d(position));
219         }
220
221         public Vector3d getWorldPosition(Vector3d localPosition) {
222                 return NodeTools.getWorldPosition(this, localPosition);
223         }
224
225         
226         @Override
227         @GetPropertyValue(value = G3D.URIs.hasWorldOrientation, tabId = "Transform", name = "World Orientation")
228         public Quat4d getWorldOrientation() {
229                 return getWorldOrientation(new Quat4d(orientation));
230         }
231         
232         public Quat4d getWorldOrientation(Quat4d localOrientation) {
233                 IG3DNode parent = (IG3DNode)getParent();
234                 if (parent == null)
235                         return localOrientation;
236                 return NodeTools.getWorldOrientation(parent, localOrientation);
237         }
238         
239         @Override
240         public Vector3d getLocalPosition(Vector3d worldPosition) {
241                 IG3DNode parent = (IG3DNode)getParent();
242                 if (parent == null)
243                         return worldPosition;
244                 return NodeTools.getLocalPosition(parent,new Vector3d(worldPosition));
245         }
246         
247         @Override
248         public Quat4d getLocalOrientation(Quat4d worldOrientation) {
249                 IG3DNode parent = (IG3DNode)getParent();
250                 if (parent == null)
251                         return worldOrientation;
252                 return NodeTools.getLocalOrientation(parent, new Quat4d(worldOrientation));
253         }
254         
255         @Override
256         @SetPropertyValue(G3D.URIs.hasWorldPosition)
257         public void setWorldPosition(Vector3d position) {
258                 Vector3d localPos = getLocalPosition(position);
259                 setPosition(localPos);
260         }
261         
262         @Override
263         @SetPropertyValue(G3D.URIs.hasWorldOrientation)
264         public void setWorldOrientation(Quat4d orientation) {
265                 Quat4d localOr = getLocalOrientation(orientation);
266                 setOrientation(localOr);
267         }
268
269
270         private vtkSolidObject solidObject;
271         
272         
273         public void visualize(VtkView panel) {
274                 if (solidObject != null) {
275                         solidObject.delete();
276                         solidObject = null;
277                 }
278                 TopoDS_Shape shape = getGeometry();
279                 if (shape == null)
280                         return;
281                 solidObject = new vtkSolidObject(panel, shape);
282                 solidObject.visualizeSolid(true, false);
283         }
284         
285         @SuppressWarnings("unchecked")
286         public Collection<vtkProp3D> getActors() {
287                 if (solidObject == null)
288                         return Collections.EMPTY_LIST;
289                 return solidObject.getActors();
290         }
291         
292         public void stopVisualize() {
293                 if (solidObject != null) {
294                         if (Thread.currentThread() == AWTThread.getThreadAccess().getThread())
295                                 solidObject.delete();
296                         else
297                                 solidObject.dispose();
298                         solidObject = null;
299                 }
300         }
301         
302         @Override
303         public void cleanup() {
304                 stopVisualize();
305                 super.cleanup();
306         }
307         
308         
309         @Override
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.
312                 super.remove();
313         }
314
315         @Override
316         public <C> C getAdapter(Class<C> adapter) {
317                 return null;
318         }
319 }