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