]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.plant3d/src/org/simantics/plant3d/editor/P3DNodeMap.java
Allow adding adjustable length components in the middle of a pipe
[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.WriteGraph;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.g3d.ontology.G3D;
15 import org.simantics.g3d.scenegraph.base.INode;
16 import org.simantics.g3d.scenegraph.base.ParentNode;
17 import org.simantics.g3d.vtk.common.AbstractVTKNodeMap;
18 import org.simantics.g3d.vtk.common.VtkView;
19 import org.simantics.objmap.graph.IMapping;
20 import org.simantics.plant3d.ontology.Plant3D;
21 import org.simantics.plant3d.scenegraph.IP3DNode;
22 import org.simantics.plant3d.scenegraph.IP3DVisualNode;
23 import org.simantics.plant3d.scenegraph.P3DParentNode;
24 import org.simantics.plant3d.scenegraph.P3DRootNode;
25 import org.simantics.plant3d.scenegraph.ParameterizedNode;
26 import org.simantics.plant3d.scenegraph.PipeRun;
27 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint;
28 import org.simantics.plant3d.scenegraph.controlpoint.PipingRules;
29
30 import vtk.vtkProp;
31 import vtk.vtkProp3D;
32
33 public class P3DNodeMap extends AbstractVTKNodeMap<Resource,INode> {
34         
35         private static final boolean DEBUG = false;
36
37         public P3DNodeMap(Session session, IMapping<Resource,INode> mapping, VtkView 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 " + debugString(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 " + debugString(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(view);
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(view);
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                 if (ids.contains(Plant3D.URIs.HasTurnRadiusIndex)) {
95                     node.visualize(view);
96                     updateRenderObjectsFor(node);
97                 }
98         }
99         
100         private void updateTransform(IP3DNode node) {
101                 if (DEBUG) System.out.println("P3DNodeMap update Transform " + debugString(node));
102
103                 node.update(view.getRenderer());
104                 
105                 if (node instanceof ParentNode<?>) {
106                         @SuppressWarnings("unchecked")
107                         ParentNode<IP3DNode> p = (ParentNode<IP3DNode>)node;
108                         for (IP3DNode n : p.getNodes())
109                                 updateTransform(n);
110                 }
111         }
112
113         @Override
114         protected Collection<vtkProp> getActors(INode n) {
115                 List<vtkProp> props = new ArrayList<vtkProp>();
116                 if (!(n instanceof IP3DVisualNode))
117                         return props;
118                 IP3DVisualNode node = (IP3DVisualNode)n;
119                 for (vtkProp3D p : ((IP3DVisualNode)node).getActors())
120                         props.add(p);
121                 
122                 return props;
123         }
124         
125         @Override
126         protected void removeActor(INode n) {
127                 if (DEBUG) System.out.println("P3DNodeMap.removeActor " + debugString(n));
128                 if (!(n instanceof IP3DVisualNode))
129                         return;
130                 IP3DVisualNode node = (IP3DVisualNode)n;
131                 remActor(node);
132                 
133                 if (node instanceof P3DParentNode<?>) {
134                         for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
135                                 if (n2 instanceof IP3DVisualNode)
136                                         removeActor((IP3DVisualNode)n2);
137                 }
138         }
139         
140         @Override
141         protected void addActor(INode n) {
142                 if (DEBUG) System.out.println("P3DNodeMap.addActor " + debugString(n));
143                 if (!(n instanceof IP3DVisualNode))
144                         return;
145                 IP3DVisualNode node = (IP3DVisualNode)n;
146                 
147                 if (hasActor(node)) {
148                     if (DEBUG) System.out.println("Node already has an actor");
149                         return;
150                 }
151                 if (Thread.currentThread() != view.getThreadQueue().getThread())
152                         throw new RuntimeException("Illegal thread.");
153                 
154                 view.lock();
155                 
156                 node.visualize(view);
157
158                 map(node, node.getActors());
159                 
160                 if (DEBUG) System.out.println("Added " + node.getActors().size() + " actors");
161                 
162                 if (node instanceof P3DParentNode<?>) {
163                         for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
164                                 if (n2 instanceof IP3DVisualNode)
165                                         addActor((IP3DVisualNode)n2);
166                 }
167                 
168                 updateTransform(node);
169
170                 view.unlock();
171
172         }
173         
174         
175         
176         private boolean hasActor(IP3DVisualNode node) {
177                 Collection<vtkProp> list = getRenderObjects(node);
178                 if (list == null || list.size() == 0)
179                         return false;
180                 return true;
181         }
182         
183         private void remActor(IP3DVisualNode node) {
184                 if (Thread.currentThread() != view.getThreadQueue().getThread())
185                         throw new RuntimeException("Illegal thread.");
186
187                 Collection<vtkProp> list = getRenderObjects(node);
188                 if (list.size() > 0) {
189                     removeMap(node);
190                     view.lock();
191                     node.stopVisualize();
192                     view.unlock();
193                 }
194         }
195         
196         @Override
197         protected void update(ReadGraph graph) throws DatabaseException {
198                 validate();
199 //              System.out.println("Graph updates");
200                 super.update(graph);
201                 validate();
202         }
203         
204         @Override
205         public void commit(String commitMessage) {
206                 validate();
207 //              System.out.println("Graph commit");
208                 super.commit(commitMessage);
209                 
210         }
211         
212         @Override
213         protected void commit(WriteGraph graph) throws DatabaseException {
214             super.commit(graph);
215             validateGraph(graph);
216         }
217         @Override
218         protected void doCommit() {
219 //              System.out.println("Do commit");
220                 validate();
221                 super.doCommit();
222         }
223         
224         private void validate() {
225                 for (INode node : rootNode.getNodes()) {
226                         if (node instanceof PipeRun)
227                                 PipingRules.validate((PipeRun)node);
228                 }
229         }
230         
231         @Override
232         public synchronized void preRender() {
233 //              System.out.println("P3DNodeMap preRender");
234 //              super.preRender();
235                 try {
236 //                      boolean b = false;
237 //                      synchronized (syncMutex) {
238 //                              b = PipingRules.update();
239 //                      }
240 //                      if (b)
241 //                              super.preRender();
242                         boolean b = true;
243                         while (b) {
244                                 updateCycle();
245                                 b = PipingRules.update();
246                         }
247                 } catch (Exception e) {
248                         e.printStackTrace();
249                 }
250                 
251         }
252         
253         protected void validateGraph(ReadGraph graph) throws DatabaseException {
254             Plant3D P3D = Plant3D.getInstance(graph);
255             Resource root = (Resource)mapping.inverseGet(rootNode);
256             if (!graph.isInstanceOf(root, P3D.Plant))
257                 throw new DatabaseException("Root is not a Plant");
258             Set<Resource> equipment = new HashSet<>();
259             Set<Resource> pipeRuns = new HashSet<>();
260             for (Resource r : graph.getObjects(root, P3D.children)) {
261                 if (graph.isInstanceOf(r, P3D.Equipment)) {
262                     equipment.add(r);
263                 } else if (graph.isInstanceOf(r, P3D.PipeRun)) {
264                     pipeRuns.add(r);
265                 } else {
266                     throw new DatabaseException("Unknown resource " + r+ " " + mapping.get(r));
267                 }
268             }
269             Set<Resource> nozzles = new HashSet<>();
270             for (Resource e : equipment) {
271                 for (Resource n : graph.getObjects(e, P3D.HasNozzle)) {
272                     if (graph.isInstanceOf(n, P3D.Nozzle)) {
273                         nozzles.add(n);
274                     } else {
275                         throw new DatabaseException("Unknown nozzle resource " + n+ " " + mapping.get(n));
276                     }
277                 }
278             }
279             
280             Set<Resource> components = new HashSet<>();
281         for (Resource run : pipeRuns) {
282             for (Resource c : graph.getObjects(run, P3D.children)) {
283                 if (graph.isInstanceOf(c, P3D.PipelineComponent)) {
284                     components.add(c);
285                 } else {
286                     throw new DatabaseException("Unknown component resource " + c + " " + mapping.get(c));
287                 }
288             }
289         }
290         
291         for (Resource c : components) {
292             for (Resource connected : graph.getObjects(c, P3D.Connects)) {
293                 if (!components.contains(connected) && !nozzles.contains(connected)) {
294                     throw new DatabaseException("Unbrowsable component resource " + connected + " " + mapping.get(connected) +" connected to " + c + " " + mapping.get(c));
295                 }
296                 Collection<Resource> connectedConnected = graph.getObjects(connected, P3D.Connects);
297                 if (!connectedConnected.contains(c)) {
298                     throw new DatabaseException("Component resource " + c + " " + mapping.get(c) + " is connected to " + connected + " " + mapping.get(connected) +", but its has no connection back.");
299                 }
300             }
301         }
302         }
303
304 }