]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/editor/P3DNodeMap.java
b098951346a114f6b1566ee798a6c20d358d88b1
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / editor / P3DNodeMap.java
1 package org.simantics.plant3d.editor;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Set;
8
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.Session;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.g3d.ontology.G3D;
14 import org.simantics.g3d.scenegraph.base.INode;
15 import org.simantics.g3d.scenegraph.base.ParentNode;
16 import org.simantics.g3d.vtk.common.AbstractVTKNodeMap;
17 import org.simantics.g3d.vtk.common.InteractiveVtkPanel;
18 import org.simantics.objmap.graph.IMapping;
19 import org.simantics.plant3d.ontology.Plant3D;
20 import org.simantics.plant3d.scenegraph.IP3DNode;
21 import org.simantics.plant3d.scenegraph.IP3DVisualNode;
22 import org.simantics.plant3d.scenegraph.P3DParentNode;
23 import org.simantics.plant3d.scenegraph.P3DRootNode;
24 import org.simantics.plant3d.scenegraph.ParameterizedNode;
25 import org.simantics.plant3d.scenegraph.PipeRun;
26 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
27 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
28 import org.simantics.utils.threads.AWTThread;
29
30 import vtk.vtkProp;
31 import vtk.vtkProp3D;
32
33 public class P3DNodeMap extends AbstractVTKNodeMap<INode> {
34         
35         private static final boolean DEBUG = false;
36
37         public P3DNodeMap(Session session, IMapping mapping, InteractiveVtkPanel panel, P3DRootNode rootNode) {
38                 super(session, mapping, panel, rootNode);
39                 rootNode.setNodeMap(this);
40         }
41         @Override
42         protected void updateActor(INode n, Set<String> ids) {
43                 if (DEBUG) System.out.println("P3DNodeMap update " + n);
44                 if (!(n instanceof IP3DVisualNode)) {
45                         if (n instanceof PipeControlPoint) {
46                                 n = ((PipeControlPoint)n).getPipelineComponent();
47                                 if (n == null)
48                                         return;
49                         } else {
50                                 return;
51                         }
52                 }
53                 
54                 IP3DVisualNode node = (IP3DVisualNode)n;
55                 
56                 if (DEBUG) {
57                         System.out.print("P3DNodeMap update " + node);
58                         for (String s : ids)
59                                 System.out.print(" " + s);
60                         System.out.println();
61                 }
62                 
63                 if (ids.contains(Plant3D.URIs.hasGeometry)) {
64                         node.visualize(panel);
65                         updateRenderObjectsFor(node);
66                         updateTransform(node);
67                 } 
68                 if (n instanceof ParameterizedNode) {
69                         ParameterizedNode geom = (ParameterizedNode)n;
70                         for (String id : geom.getParameterMap().keySet()) {
71                                 if (ids.contains(id)) {
72                                         node.visualize(panel);
73                                         updateRenderObjectsFor(node);
74                                         updateTransform(node);
75                                         break;
76                                 }
77                         }
78                 } else if (n instanceof PipeRun) {
79                         // FIXME: may require rule based update!
80                         PipeRun run = (PipeRun)n;
81                         Set<String> ids2 = new HashSet<String>();
82                         ids2.add(Plant3D.URIs.hasGeometry);
83                         for (PipeControlPoint pcp : run.getControlPoints()) {
84                                 updateActor(pcp, ids2);
85                         }
86                 }
87                 
88                 if (ids.contains(G3D.URIs.hasPosition) || 
89                         ids.contains(G3D.URIs.hasOrientation) ||
90                         ids.contains(G3D.URIs.hasWorldPosition) ||
91                         ids.contains(G3D.URIs.hasWorldOrientation)) {
92                         updateTransform(node);
93                 }
94         }
95         
96         private void updateTransform(IP3DNode node) {
97                 if (DEBUG) System.out.println("P3DNodeMap update Transform " + node);
98
99                 node.update(panel.GetRenderer());
100                 
101                 if (node instanceof ParentNode<?>) {
102                         ParentNode<IP3DNode> p = (ParentNode<IP3DNode>)node;
103                         for (IP3DNode n : p.getNodes())
104                                 updateTransform(n);
105                 }
106         }
107
108         @Override
109         protected Collection<vtkProp> getActors(INode n) {
110                 List<vtkProp> props = new ArrayList<vtkProp>();
111                 if (!(n instanceof IP3DVisualNode))
112                         return props;
113                 IP3DVisualNode node = (IP3DVisualNode)n;
114                 for (vtkProp3D p : ((IP3DVisualNode)node).getActors())
115                         props.add(p);
116                 
117                 return props;
118         }
119         
120         @Override
121         protected void removeActor(INode n) {
122                 if (DEBUG) System.out.println("P3DNodeMap.removeActor " + n);
123                 if (!(n instanceof IP3DVisualNode))
124                         return;
125                 IP3DVisualNode node = (IP3DVisualNode)n;
126                 remActor(node);
127                 
128                 if (node instanceof P3DParentNode<?>) {
129                         for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
130                                 if (n2 instanceof IP3DVisualNode)
131                                         removeActor((IP3DVisualNode)n2);
132                 }
133         }
134         
135         @Override
136         protected void addActor(INode n) {
137                 if (DEBUG) System.out.println("P3DNodeMap.addActor " + n);
138                 if (!(n instanceof IP3DVisualNode))
139                         return;
140                 IP3DVisualNode node = (IP3DVisualNode)n;
141                 
142                 if (hasActor(node))
143                         return;
144                 if (Thread.currentThread() != AWTThread.getThreadAccess().getThread())
145                         throw new RuntimeException("Illegal thread.");
146                 
147                 panel.lock();
148                 
149                 node.visualize(panel);
150
151                 for (vtkProp3D act : node.getActors()) {
152                         nodeToActor.add(node, act);
153             actorToNode.put(act, node);
154                 }
155                 
156                 if (node instanceof P3DParentNode<?>) {
157                         for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
158                                 if (n2 instanceof IP3DVisualNode)
159                                         addActor((IP3DVisualNode)n2);
160                 }
161                 
162                 updateTransform(node);
163
164         panel.unlock();
165
166         }
167         
168         
169         
170         private boolean hasActor(IP3DVisualNode node) {
171                 List<vtkProp> list = nodeToActor.getValues(node);
172                 if (list == null || list.size() == 0)
173                         return false;
174                 return true;
175         }
176         
177         private void remActor(IP3DVisualNode node) {
178                 if (Thread.currentThread() != AWTThread.getThreadAccess().getThread())
179                         throw new RuntimeException("Illegal thread.");
180
181                 List<vtkProp> list = nodeToActor.getValues(node);
182                 if (list != null) {
183                         for (vtkProp obj : list) {
184                                 actorToNode.remove(obj);        
185                         }
186                         nodeToActor.remove(node);
187                         panel.lock();
188                         
189                         node.stopVisualize();
190                         
191                         panel.unlock();
192                 }
193         }
194         
195         @Override
196         protected void update(ReadGraph graph) throws DatabaseException {
197                 validate();
198 //              System.out.println("Graph updates");
199                 super.update(graph);
200                 validate();
201         }
202         
203         @Override
204         public void commit() {
205                 validate();
206 //              System.out.println("Graph commit");
207                 super.commit();
208                 
209         }
210         @Override
211         protected void doCommit() {
212 //              System.out.println("Do commit");
213                 validate();
214                 super.doCommit();
215         }
216         
217         private void validate() {
218                 for (INode node : rootNode.getNodes()) {
219                         if (node instanceof PipeRun)
220                                 PipingRules.validate((PipeRun)node);
221                 }
222         }
223         
224         @Override
225         public synchronized void preRender() {
226 //              System.out.println("P3DNodeMap preRender");
227 //              super.preRender();
228                 try {
229 //                      boolean b = false;
230 //                      synchronized (syncMutex) {
231 //                              b = PipingRules.update();
232 //                      }
233 //                      if (b)
234 //                              super.preRender();
235                         boolean b = true;
236                         while (b) {
237                                 updateCycle();
238                                 b = PipingRules.update();
239                         }
240                 } catch (Exception e) {
241                         e.printStackTrace();
242                 }
243                 
244         }
245
246 }