]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d.csg/src/org/simantics/g3d/csg/editor/CSGNodeMap.java
b4eaa148903d71b793dd09335d2cb88caf178269
[simantics/3d.git] / org.simantics.g3d.csg / src / org / simantics / g3d / csg / editor / CSGNodeMap.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.editor;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.Set;
18
19 import org.simantics.db.Session;
20 import org.simantics.g3d.csg.scenegraph2.CSGrootNode;
21 import org.simantics.g3d.csg.scenegraph2.ICSGnode;
22 import org.simantics.g3d.scenegraph.base.ParentNode;
23 import org.simantics.g3d.vtk.awt.InteractiveVtkPanel;
24 import org.simantics.g3d.vtk.common.AbstractVTKNodeMap;
25 import org.simantics.objmap.graph.IMapping;
26
27 import vtk.vtkProp;
28 import vtk.vtkProp3D;
29
30 public class CSGNodeMap extends AbstractVTKNodeMap<ICSGnode> {
31         
32
33         
34         public CSGNodeMap(Session session, IMapping mapping, InteractiveVtkPanel panel, CSGrootNode rootNode) {
35                 super(session, mapping, panel, rootNode);
36                 rootNode.setNodeMap(this);
37         }
38         
39
40         @SuppressWarnings("unchecked")
41         protected void updateActor(ICSGnode node, Set<String> ids) {
42                 //System.out.println("CSGNodeMap.updateActor " + node);
43                 if (node.getParent() instanceof ICSGnode) {
44                         ICSGnode parent = (ICSGnode) node.getParent();
45                         if (!"child".equals(node.getParentRel())) {
46                                 updateActor(parent,null);
47                                 return;
48                         }
49                 }
50                 
51                 if (node instanceof ParentNode<?>) {
52                         ParentNode<ICSGnode> p = (ParentNode<ICSGnode>)node;
53                         for (ICSGnode n : p.getNodes())
54                                 remActor(n);
55                 }
56                 
57                 remActor(node);
58                 addActor(node);
59
60         }
61
62         @Override
63         protected Collection<vtkProp> getActors(ICSGnode node) {
64                 List<vtkProp> props = new ArrayList<vtkProp>();
65                 for (vtkProp3D p : ((ICSGnode)node).getActors())
66                         props.add(p);
67                 return props;
68         }
69         
70         protected void removeActor(ICSGnode node) {
71                 //System.out.println("CSGNodeMap.removeActor " + node);
72                 remActor(node);
73                 
74                 if (!"child".equals(node.getParentRel())) {
75                         if (node.getParent() instanceof ICSGnode)
76                                 updateActor((ICSGnode)node.getParent(),null);
77                 }
78         }
79         
80         protected void addActor(ICSGnode node) {
81                 //System.out.println("CSGNodeMap.addActor " + node);
82                 if (hasActor(node))
83                         return;
84                 if (Thread.currentThread() != view.getThreadQueue().getThread())
85                         throw new RuntimeException("Illegal thread.");
86                 
87                 view.lock();
88                 
89                 node.visualize(view);
90
91                 for (vtkProp3D act : node.getActors()) {
92                         nodeToActor.add(node, act);
93             actorToNode.put(act, node);
94                 }
95
96         view.unlock();
97
98         }
99         
100         
101         
102         private boolean hasActor(ICSGnode node) {
103                 List<vtkProp> list = nodeToActor.getValues(node);
104                 if (list == null || list.size() == 0)
105                         return false;
106                 return true;
107         }
108         
109         private void remActor(ICSGnode node) {
110                 if (Thread.currentThread() != view.getThreadQueue().getThread())
111                         throw new RuntimeException("Illegal thread.");
112
113                 List<vtkProp> list = nodeToActor.getValues(node);
114                 if (list != null) {
115                         for (vtkProp obj : list) {
116                                 actorToNode.remove(obj);        
117                         }
118                         nodeToActor.remove(node);
119                         view.lock();
120                         
121                         node.stopVisualize();
122                         
123                         view.unlock();
124                 }
125         }
126         
127
128 }