forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Focus.java
239 lines (207 loc) · 8.19 KB
/
Focus.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.filechooser.FileNameExtensionFilter;
import vtk.*;
public class Focus extends JFrame {
private vtkRenderWindowPanel mainPanel = new vtkRenderWindowPanel();
private vtkRenderWindowPanel focusPanel = new vtkRenderWindowPanel();
private vtkRenderedGraphRepresentation mainRep = new vtkRenderedGraphRepresentation();
private vtkRenderedGraphRepresentation focusRep = new vtkRenderedGraphRepresentation();
private vtkDelimitedTextReader reader = new vtkDelimitedTextReader();
private vtkTableToGraph tableToGraph = new vtkTableToGraph();
private vtkBoostBreadthFirstSearch mainBFS = new vtkBoostBreadthFirstSearch();
private vtkBoostBreadthFirstSearch bfs = new vtkBoostBreadthFirstSearch();
private vtkExtractSelectedGraph extract = new vtkExtractSelectedGraph();
private vtkGraphLayout layout = new vtkGraphLayout();
private vtkGraphLayoutView mainView = new vtkGraphLayoutView();
private vtkGraphLayoutView focusView = new vtkGraphLayoutView();
private vtkAnnotationLink link = new vtkAnnotationLink();
private vtkDoubleArray thresh = new vtkDoubleArray();
public Focus() {
// Use a simple ViewChangedObserver to render all views
// when the selection in one view changes.
ViewChangedObserver obs = new ViewChangedObserver();
this.link.AddObserver("SelectionChangedEvent", obs, "SelectionChanged");
this.reader.SetFileName("../../../../VTKData/Data/Infovis/classes.csv");
this.tableToGraph.SetInputConnection(this.reader.GetOutputPort());
this.tableToGraph.AddLinkEdge("Field 0", "Field 1");
vtkSimple2DLayoutStrategy strategy = new vtkSimple2DLayoutStrategy();
this.layout.SetLayoutStrategy(strategy);
this.layout.SetInputConnection(this.tableToGraph.GetOutputPort());
this.mainBFS.SetInputConnection(this.layout.GetOutputPort());
this.bfs.SetInputConnection(this.mainBFS.GetOutputPort());
this.extract.SetInputConnection(0, this.bfs.GetOutputPort());
vtkSelection select = new vtkSelection();
vtkSelectionNode node = new vtkSelectionNode();
node.SetContentType(7); // 7 == vtkSelection.THRESHOLDS
node.SetFieldType(3); // 3 == vtkSelection.VERTEX
this.thresh.SetName("BFS");
this.thresh.InsertNextValue(0);
this.thresh.InsertNextValue(1);
node.SetSelectionList(this.thresh);
select.AddNode(node);
this.extract.SetInputData(1, select);
this.mainRep.SetInputConnection(this.bfs.GetOutputPort());
this.focusRep.SetInputConnection(this.extract.GetOutputPort());
this.mainView.AddRepresentation(this.mainRep);
this.mainView.SetVertexLabelArrayName("label");
this.mainView.VertexLabelVisibilityOn();
this.mainView.SetLayoutStrategyToPassThrough();
this.mainView.SetVertexColorArrayName("BFS");
this.mainView.ColorVerticesOn();
this.focusView.AddRepresentation(this.focusRep);
this.focusView.SetVertexLabelArrayName("label");
this.focusView.VertexLabelVisibilityOn();
this.focusView.SetVertexColorArrayName("BFS");
this.focusView.ColorVerticesOn();
vtkViewTheme t = new vtkViewTheme();
vtkViewTheme theme = t.CreateMellowTheme();
theme.SetPointHueRange(0, 0);
theme.SetPointSaturationRange(1, 0);
theme.SetPointValueRange(1, 0);
this.mainView.ApplyViewTheme(theme);
this.focusView.ApplyViewTheme(theme);
this.mainPanel = new vtkRenderWindowPanel(this.mainView.GetRenderWindow());
this.focusPanel = new vtkRenderWindowPanel(this.focusView.GetRenderWindow());
// Views should produce pedigree id selections.
// 2 == vtkSelection.PEDIGREEIDS
this.mainRep.SetSelectionType(2);
this.focusRep.SetSelectionType(2);
this.mainPanel.setSize(500, 500);
this.focusPanel.setSize(500, 500);
//this.mainView.SetLayoutStrategyToSimple2D();
//this.focusView.SetLayoutStrategyToSimple2D();
this.mainRep.SetAnnotationLink(this.link);
this.focusRep.SetAnnotationLink(this.link);
// Arrange the views in a grid.
GridLayout layout = new GridLayout(1, 2);
layout.setHgap(5);
layout.setVgap(5);
JPanel panel = new JPanel();
panel.setLayout(layout);
panel.add(mainPanel);
panel.add(focusPanel);
// Create menu
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
// Create the menu bar.
menuBar = new JMenuBar();
// Build the first menu.
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menuBar.add(menu);
// A JMenuItem
menuItem = new JMenuItem("Open Edge List...", KeyEvent.VK_X);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
ActionEvent.CTRL_MASK));
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Create a file chooser
final JFileChooser fc = new JFileChooser();
// In response to a button click:
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = fc.getSelectedFile();
reader.SetFileName(f.getAbsolutePath());
ViewChangedObserver obs = new ViewChangedObserver();
obs.SelectionChanged();
}
}});
menu.add(menuItem);
JToolBar toolbar = new JToolBar();
// A JButton
JButton increase = new JButton("Increase Focus");
increase.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
thresh.SetValue(1, thresh.GetValue(1) + 1);
extract.Modified();
update();
}});
toolbar.add(increase);
// A JButton
JButton decrease = new JButton("Decrease Focus");
decrease.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (thresh.GetValue(1) > 0) {
thresh.SetValue(1, thresh.GetValue(1) - 1);
extract.Modified();
update();
}
}});
toolbar.add(decrease);
this.setJMenuBar(menuBar);
this.add(toolbar, BorderLayout.NORTH);
this.add(panel);
}
void update() {
this.mainPanel.Render();
this.focusPanel.Render();
this.mainView.ResetCamera();
this.focusView.ResetCamera();
this.mainView.Render();
this.focusView.Render();
}
private class ViewChangedObserver {
// When a selection changes, render all views.
void SelectionChanged() {
vtkSelection currentSel = link.GetCurrentSelection();
vtkConvertSelection convert = new vtkConvertSelection();
vtkSelection indexSel =
convert.ToIndexSelection(currentSel, tableToGraph.GetOutput());
if (currentSel.GetNumberOfNodes() > 0) {
vtkIdTypeArray ids = (vtkIdTypeArray)indexSel.GetNode(0).
GetSelectionList();
if (ids != null && ids.GetNumberOfTuples() == 1) {
bfs.SetOriginVertex(ids.GetValue(0));
bfs.Modified();
focusPanel.Render();
focusView.GetRenderer().ResetCamera();
}
}
mainPanel.Render();
focusPanel.Render();
}
}
// Load VTK library and print which library was not properly loaded
static {
if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
if (!lib.IsLoaded()) {
System.out.println(lib.GetLibraryName() + " not loaded");
}
}
}
vtkNativeLibrary.DisableOutputWindow(null);
}
public static void main(String args[]) {
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
final Focus app = new Focus();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
app.setTitle("Focus");
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.pack();
app.setVisible(true);
app.update();
}});
}
}