1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.vtk.common;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.SelectionChangedEvent;
21 import org.simantics.g3d.scenegraph.IG3DNode;
22 import org.simantics.g3d.scenegraph.NodeHighlighter;
23 import org.simantics.g3d.scenegraph.NodeHighlighter.HighlightEventType;
24 import org.simantics.g3d.scenegraph.NodeHighlighter.HighlightObjectType;
25 import org.simantics.g3d.scenegraph.base.INode;
26 import org.simantics.g3d.tools.AdaptationUtils;
27 import org.simantics.utils.threads.AWTThread;
28 import org.simantics.utils.threads.ThreadUtils;
31 import vtk.vtkAlgorithm;
32 import vtk.vtkAlgorithmOutput;
33 import vtk.vtkFeatureEdges;
36 import vtk.vtkProperty;
38 public class SelectionHighlighter<DBObject, E extends INode> implements ISelectionChangedListener{
42 protected VtkView panel;
43 protected VTKNodeMap<DBObject,E> nodeMap;
45 protected List<IG3DNode> selectedNodes = new ArrayList<IG3DNode>();
46 protected List<vtkActor> selectedActors = new ArrayList<vtkActor>();
48 protected HighlightObjectType type = HighlightObjectType.Node;
50 public SelectionHighlighter(VtkView panel, VTKNodeMap<DBObject,E> nodeMap) {
52 this.nodeMap = nodeMap;
56 public void selectionChanged(SelectionChangedEvent event) {
57 final ISelection s = event.getSelection();
59 if (Thread.currentThread().equals(panel.getThreadQueue().getThread()))
62 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
66 //System.out.println(this.getClass().getName() + " highlight ");
75 protected void hilight(E node, HighlightEventType type) {
76 if (node instanceof NodeHighlighter) {
77 ((NodeHighlighter)node).highlight(type);
80 if (type == HighlightEventType.Selection) {
81 setSelectedColor(node);
82 } else if (type == HighlightEventType.ClearSelection) {
83 setDefaultColor(node);
87 protected void hilight(vtkActor actor, HighlightEventType type) {
88 if (type == HighlightEventType.Selection) {
89 setColor(actor,false,new double[]{1,0,0});
90 setColor(actor,true,new double[]{1,0,1});
91 } else if (type == HighlightEventType.ClearSelection) {
92 setColor(actor,false,new double[]{1,1,0});
93 setColor(actor,true,new double[]{0,0,0});
97 protected void highlight(ISelection s) {
98 highlight(s, HighlightEventType.Selection, HighlightEventType.ClearSelection);
101 protected void highlight(ISelection s, HighlightEventType apply, HighlightEventType clear) {
103 boolean changed = false;
104 if (type == HighlightObjectType.Node) {
105 Collection<IG3DNode> currentSelectedNodes = AdaptationUtils.adaptToCollection(s,IG3DNode.class);//getSelectedNodes(currentSelectedActors);
106 if (currentSelectedNodes.size() == 0) {
107 Collection<vtkProp> currentSelectedActors = AdaptationUtils.adaptToCollection(s, vtkProp.class);
108 currentSelectedNodes = getSelectedNodes(currentSelectedActors);
110 for (IG3DNode node : selectedNodes) {
111 if (!currentSelectedNodes.contains(node)) {
112 hilight((E)node, clear);
116 for (IG3DNode node : currentSelectedNodes) {
117 if (!selectedNodes.contains(node)) {
118 hilight((E)node, apply);
122 selectedNodes.clear();
123 selectedNodes.addAll(currentSelectedNodes);
124 //selectedNodes = currentSelectedNodes;
128 Collection<vtkActor> currentSelectedActors = AdaptationUtils.adaptToCollection(s, vtkActor.class);
130 for (vtkActor act : selectedActors) {
131 if (!currentSelectedActors.contains(act)) {
136 for (vtkActor act : currentSelectedActors) {
137 if (!selectedActors.contains(act)) {
142 selectedActors.clear();
143 selectedActors.addAll(currentSelectedActors);
150 protected List<IG3DNode> getSelectedNodes(Collection<vtkProp> selectedActors) {
151 List<IG3DNode> currentSelectedNodes = new ArrayList<IG3DNode>();
153 for (vtkProp a : selectedActors) {
154 INode node = nodeMap.getNode((vtkProp)a);
155 if (node == null || !(node instanceof IG3DNode))
157 if (!currentSelectedNodes.contains(node))
158 currentSelectedNodes.add((IG3DNode)node);
160 return currentSelectedNodes;
163 protected void setDefaultColor(E node) {
164 double color[] = new double[]{1,1,0};
165 setColor(node, false, color);
168 protected void setSelectedColor(E node) {
169 double color[] = new double[]{1,0,0};
170 setColor(node, false, color);
174 public void setColor(E node, boolean edge, double color[]) {
175 for (vtkProp prop : nodeMap.getRenderObjects(node)) {
176 if (prop instanceof vtkActor) {
177 vtkActor act = (vtkActor)prop;
178 setColor(act, edge, color);
183 protected void setColor(vtkActor act, boolean edge, double color[]) {
185 vtkMapper mapper = act.GetMapper();
186 vtkAlgorithmOutput out = mapper.GetInputConnection(0, 0);
187 vtkAlgorithm producer = out.GetProducer();
188 boolean isEdge = (producer instanceof vtkFeatureEdges);
190 if (isEdge == edge) {
191 vtkProperty property = act.GetProperty();
192 property.SetColor(color);