1 package org.simantics.plant3d.editor;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.HashSet;
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;
33 public class P3DNodeMap extends AbstractVTKNodeMap<INode> {
35 private static final boolean DEBUG = false;
37 public P3DNodeMap(Session session, IMapping mapping, InteractiveVtkPanel panel, P3DRootNode rootNode) {
38 super(session, mapping, panel, rootNode);
39 rootNode.setNodeMap(this);
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();
54 IP3DVisualNode node = (IP3DVisualNode)n;
57 System.out.print("P3DNodeMap update " + node);
59 System.out.print(" " + s);
63 if (ids.contains(Plant3D.URIs.hasGeometry)) {
64 node.visualize(panel);
65 updateRenderObjectsFor(node);
66 updateTransform(node);
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);
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);
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);
96 private void updateTransform(IP3DNode node) {
97 if (DEBUG) System.out.println("P3DNodeMap update Transform " + node);
99 node.update(panel.GetRenderer());
101 if (node instanceof ParentNode<?>) {
102 ParentNode<IP3DNode> p = (ParentNode<IP3DNode>)node;
103 for (IP3DNode n : p.getNodes())
109 protected Collection<vtkProp> getActors(INode n) {
110 List<vtkProp> props = new ArrayList<vtkProp>();
111 if (!(n instanceof IP3DVisualNode))
113 IP3DVisualNode node = (IP3DVisualNode)n;
114 for (vtkProp3D p : ((IP3DVisualNode)node).getActors())
121 protected void removeActor(INode n) {
122 if (DEBUG) System.out.println("P3DNodeMap.removeActor " + n);
123 if (!(n instanceof IP3DVisualNode))
125 IP3DVisualNode node = (IP3DVisualNode)n;
128 if (node instanceof P3DParentNode<?>) {
129 for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
130 if (n2 instanceof IP3DVisualNode)
131 removeActor((IP3DVisualNode)n2);
136 protected void addActor(INode n) {
137 if (DEBUG) System.out.println("P3DNodeMap.addActor " + n);
138 if (!(n instanceof IP3DVisualNode))
140 IP3DVisualNode node = (IP3DVisualNode)n;
144 if (Thread.currentThread() != AWTThread.getThreadAccess().getThread())
145 throw new RuntimeException("Illegal thread.");
149 node.visualize(panel);
151 for (vtkProp3D act : node.getActors()) {
152 nodeToActor.add(node, act);
153 actorToNode.put(act, node);
156 if (node instanceof P3DParentNode<?>) {
157 for (IP3DNode n2 : ((P3DParentNode<?>)node).getNodes())
158 if (n2 instanceof IP3DVisualNode)
159 addActor((IP3DVisualNode)n2);
162 updateTransform(node);
170 private boolean hasActor(IP3DVisualNode node) {
171 List<vtkProp> list = nodeToActor.getValues(node);
172 if (list == null || list.size() == 0)
177 private void remActor(IP3DVisualNode node) {
178 if (Thread.currentThread() != AWTThread.getThreadAccess().getThread())
179 throw new RuntimeException("Illegal thread.");
181 List<vtkProp> list = nodeToActor.getValues(node);
183 for (vtkProp obj : list) {
184 actorToNode.remove(obj);
186 nodeToActor.remove(node);
189 node.stopVisualize();
196 protected void update(ReadGraph graph) throws DatabaseException {
198 // System.out.println("Graph updates");
204 public void commit() {
206 // System.out.println("Graph commit");
211 protected void doCommit() {
212 // System.out.println("Do commit");
217 private void validate() {
218 for (INode node : rootNode.getNodes()) {
219 if (node instanceof PipeRun)
220 PipingRules.validate((PipeRun)node);
225 public synchronized void preRender() {
226 // System.out.println("P3DNodeMap preRender");
227 // super.preRender();
229 // boolean b = false;
230 // synchronized (syncMutex) {
231 // b = PipingRules.update();
234 // super.preRender();
238 b = PipingRules.update();
240 } catch (Exception e) {