This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
ParaViewPlotRender.java
557 lines (497 loc) · 17.8 KB
/
ParaViewPlotRender.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package org.eclipse.ice.viz.service.paraview;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
import org.eclipse.ice.client.common.ActionTree;
import org.eclipse.ice.viz.service.connections.ConnectionPlotRender;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.json.JSONArray;
import org.json.JSONObject;
import com.kitware.vtk.web.VtkWebClient;
import com.kitware.vtk.web.util.InteractiveRenderPanel;
public class ParaViewPlotRender extends ConnectionPlotRender<VtkWebClient> {
// TODO Use a thread to throttle the resize events.
private List<ActionTree> actions;
private final ReadLock readLock;
private final WriteLock writeLock;
private String plotCategory;
private String plotType;
private String plotRepType;
private String repType;
private int viewId;
private int fileId;
private int repId;
/**
* The default constructor.
*
* @param parent
* The parent Composite that contains the plot render.
*
* @param plot
* The rendered ConnectionPlot. This cannot be changed.
*/
public ParaViewPlotRender(Composite parent, ParaViewPlot plot) {
this(parent, plot, -1, -1, -1);
}
/**
* The full constructor. <i>This should only be called once per plot so that
* the view created to read the file's contents is not wasted.</i>
*
* @param parent
* The parent Composite that contains the plot render.
*
* @param plot
* The rendered ConnectionPlot. This cannot be changed.
* @param viewId
* The ID of the view that was created to read the file's
* contents, or -1 to indicate that a view has not been created.
* @param repId
* The ID of the representation (i.e., the render properties) of
* the file.
*/
public ParaViewPlotRender(Composite parent, ParaViewPlot plot, int viewId,
int fileId, int repId) {
super(parent, plot);
ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
readLock = lock.readLock();
writeLock = lock.writeLock();
this.viewId = viewId;
this.fileId = fileId;
this.repId = repId;
return;
}
/**
* Overrides the default behavior so that the rendering widget is only
* updated if the category changes to a new value.
*/
@Override
public void setPlotCategory(String category) {
writeLock.lock();
try {
// if (category != null && !category.equals(getPlotCategory())) {
// plotChanged = true;
// super.setPlotCategory(category);
// }
super.setPlotCategory(category);
} finally {
writeLock.unlock();
}
return;
}
/**
* Overrides the default behavior so that the rendering widget is only
* updated if the type changes to a new value.
*/
@Override
public void setPlotType(String type) {
writeLock.lock();
try {
// if (type != null && !type.equals(getPlotType())) {
// plotChanged = true;
// super.setPlotType(type);
// }
super.setPlotType(type);
} finally {
writeLock.unlock();
}
return;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ice.viz.service.connections.ConnectionPlotRender#
* createPlotComposite(org.eclipse.swt.widgets.Composite, int,
* java.lang.Object)
*/
@Override
protected Composite createPlotComposite(Composite parent, int style,
VtkWebClient connection) throws Exception {
// Create a new view on the ParaView server if one does not already
// exist. We will need the corresponding view, file proxy, and
// representation proxy IDs.
if (viewId < 0 || fileId < 0 || repId < 0) {
String fullPath = ((ParaViewPlot) plot)
.getParaViewConnectionAdapter().findRelativePath(
plot.getDataSource().getPath());
// Create a new view from the file and get its associated IDs.
List<Object> args = new ArrayList<Object>();
JSONObject object;
args.add(fullPath);
object = connection.call("createView", args).get();
viewId = object.getInt("viewId");
fileId = object.getInt("proxyId");
repId = object.getInt("repId");
// Throw an exception if a view could not be created for the file.
if (viewId < 0 || fileId < 0 || repId < 0) {
throw new Exception("IPlot error: "
+ "The file could not be rendered with "
+ plot.getVizService().getName() + ".");
}
}
// Create a container to hold a ToolBar and the ParaView widget.
Composite plotContainer = new Composite(parent, style);
plotContainer.setBackground(parent.getBackground());
plotContainer.setFont(parent.getFont());
GridLayout gridLayout = new GridLayout();
// Get rid of the default margins (5 px on top, bottom, left, right).
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
plotContainer.setLayout(gridLayout);
// Create a ToolBar.
ToolBarManager toolBarManager = new ToolBarManager();
ToolBar toolBar = toolBarManager.createControl(plotContainer);
toolBar.setBackground(parent.getBackground());
toolBar.setFont(parent.getFont());
toolBar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
fillToolBar(toolBarManager, connection);
// Since the ParaView widget is built on AWT, we will need to use the
// SWT_AWT bridge below.
// Create the Composite that will contain the embedded ParaView widget.
final Composite composite = new Composite(plotContainer, SWT.EMBEDDED
| SWT.DOUBLE_BUFFERED);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
// Create the ParaView widget.
final InteractiveRenderPanel renderPanel = new InteractiveRenderPanel(
connection, viewId, 4, 80, 1);
// Create an AWT Frame to contain the ParaView widget.
Frame frame = SWT_AWT.new_Frame(composite);
frame.setLayout(new BorderLayout());
frame.add(renderPanel, BorderLayout.CENTER);
// When the Composite is resized, the ParaView widget will need to be
// refreshed.
composite.addControlListener(new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
renderPanel.dirty();
}
});
return plotContainer;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ice.viz.service.connections.ConnectionPlotRender#
* updatePlotComposite(org.eclipse.swt.widgets.Composite, java.lang.Object)
*/
@Override
protected void updatePlotComposite(Composite plotComposite,
VtkWebClient connection) throws Exception {
// FIXME Should this be done here on the UI thread, or elsewhere?
List<Object> args = new ArrayList<Object>();
JSONObject object;
boolean repChanged = false;
String repType = null;
readLock.lock();
try {
repType = this.repType;
if (repType != null && !repType.equals(plotRepType)) {
plotRepType = repType;
repChanged = true;
}
} finally {
readLock.unlock();
}
boolean plotTypeChanged = false;
String category = null;
String type = null;
readLock.lock();
try {
category = getPlotCategory();
type = getPlotType();
if (category != null && !category.equals(plotCategory)) {
plotCategory = category;
plotTypeChanged = true;
}
if (type != null && !type.equals(plotType)) {
plotType = type;
plotTypeChanged = true;
}
} finally {
readLock.unlock();
}
if (repChanged || plotTypeChanged) {
// Make sure this view is the active one.
args.clear();
args.add(viewId);
connection.call("activateView", args).get();
}
if (repChanged) {
// Set the representation proxy to show the mesh as a surface.
args.clear();
JSONArray updatedProperties = new JSONArray();
JSONObject repProperty = new JSONObject();
repProperty.put("id", Integer.toString(repId));
repProperty.put("name", "Representation");
repProperty.put("value", repType);
updatedProperties.put(repProperty);
// Update the properties that were configured.
args.add(updatedProperties);
object = connection.call("pv.proxy.manager.update", args).get();
if (!object.getBoolean("success")) {
System.out.println("Failed to set the representation: ");
JSONArray array = object.getJSONArray("errorList");
for (int i = 0; i < array.length(); i++) {
System.out.println(array.get(i));
}
}
}
if (plotTypeChanged) {
JSONArray updatedProperties = new JSONArray();
// Update the "status" of the mesh/material/cell/point arrays.
// For the silo file, we want to select Battery_/TemperatureP1.
JSONObject pointStatusProperty = new JSONObject();
pointStatusProperty.put("id", Integer.toString(fileId));
pointStatusProperty.put("name", "PointArrayStatus");
JSONArray pointArrays = new JSONArray();
pointArrays.put(type);
pointStatusProperty.put("value", pointArrays);
updatedProperties.put(pointStatusProperty);
// Update the properties that were configured.
args.clear();
args.add(updatedProperties);
object = connection.call("pv.proxy.manager.update", args).get();
if (!object.getBoolean("success")) {
System.out.println("Failed to set the representation: ");
JSONArray array = object.getJSONArray("errorList");
for (int i = 0; i < array.length(); i++) {
System.out.println(array.get(i));
}
}
// Set the representation to color based on the plot type.
// FIXME The documentation for ParaView is a little unclear about
// most of these parameters. The only obvious ones are the
// representation proxy ID and the variable name. The rescale option
// does not appear to work as expected.
args.clear();
args.add(Integer.toString(repId));
args.add("ARRAY"); // TODO Not sure when to use SOLID here.
args.add("POINTS"); // TODO Not sure when to use CELLS here.
args.add(type);
args.add("Magnitude"); // TODO Not sure when to use Component here.
args.add(0); // TODO Not sure when to use another value here.
args.add(true);
object = connection.call("pv.color.manager.color.by", args).get();
// Set the visibility of the legend to true.
args.clear();
JSONObject legendVisibilities = new JSONObject();
legendVisibilities.put(Integer.toString(fileId), true);
args.add(legendVisibilities);
object = connection.call(
"pv.color.manager.scalarbar.visibility.set", args).get();
System.out.println(object.toString(4));
// Auto-scale the color map to the data.
args.clear();
JSONObject scaleOptions = new JSONObject();
scaleOptions.put("type", "data");
scaleOptions.put("proxyId", fileId);
args.add(scaleOptions);
object = connection.call(
"pv.color.manager.rescale.transfer.function", args).get();
System.out.println(object.toString(4));
// // ---- Set the plot representation style. ---- //
// // Set the representation proxy to show the mesh as
// // "Surface With Edges".
// args.clear();
// JSONArray updatedProperties = new JSONArray();
// JSONObject repProperty = new JSONObject();
// repProperty.put("id", Integer.toString(repId));
// repProperty.put("name", "Representation");
// repProperty.put("value", "Surface");
// updatedProperties.put(repProperty);
//
// // Update the "status" of the mesh/material/cell/point arrays.
// // For the silo file, we want to select Battery_/TemperatureP1.
// JSONObject pointStatusProperty = new JSONObject();
// pointStatusProperty.put("id", Integer.toString(fileId));
// pointStatusProperty.put("name", "PointArrayStatus");
// JSONArray pointArrays = new JSONArray();
// pointArrays.put("Battery_/TemperatureP1");
// pointStatusProperty.put("value", pointArrays);
// updatedProperties.put(pointStatusProperty);
//
// // Update the properties that were configured.
// args.add(updatedProperties);
// object = connection.call("pv.proxy.manager.update", args).get();
// if (!object.getBoolean("success")) {
// System.out.println("Failed to set the representation: ");
// JSONArray array = object.getJSONArray("errorList");
// for (int i = 0; i < array.length(); i++) {
// System.out.println(array.get(i));
// }
// }
// // -------------------------------------------- //
//
// // ---- Set the plot type via the ColorManager. ---- //
// args.clear();
// args.add(Integer.toString(repId)); // The rep ID to update.
// args.add("ARRAY"); // The "color mode".
// args.add("POINTS"); // The "array location".
// args.add("Battery_/TemperatureP1"); // The name of the array to
// plot,
// in this case the
// // plot type. The category does not need to be
// // specified.
// args.add("Magnitude"); // The default "vector mode".
// args.add(0); // The default "vector component".
// args.add(true); // This is supposed to rescale the colors.
//
// // Send these arguments to update the data set that the view
// plots.
// connection.call("pv.color.manager.color.by", args).get();
//
// // Auto-scale the color map to the data.
// // args.clear();
// // JSONObject scaleOptions = new JSONObject();
// // scaleOptions.put("type", "data");
// // scaleOptions.put("proxyId", fileId);
// // args.add(scaleOptions);
// // connection.call("pv.color.manager.rescale.transfer.function",
// // args)
// // .get();
// // ------------------------------------------------- //
}
if (repChanged || plotTypeChanged) {
// Make sure this view is the active one.
args.clear();
args.add(viewId);
connection.call("activateView", args).get();
}
return;
}
/**
* Fills the specified {@code ToolBar} with actions that can be used to
* update the rendered plot.
* <p>
* <b>Note:</b> This method should only be called once, and should be called
* at plot creation time.
* </p>
*
* @param toolBar
* The {@code ToolBarManager} that will be populated.
*/
private void fillToolBar(ToolBarManager toolBar, VtkWebClient connection) {
actions = new ArrayList<ActionTree>();
ActionTree plotTypesTree = new ActionTree("Plot Types");
actions.add(plotTypesTree);
// Create an ActionTree for the available plot categories and types.
// Selecting one of the leaf nodes should set the category and type for
// the associated plot.
try {
Map<String, String[]> plotTypes = plot.getPlotTypes();
for (Entry<String, String[]> entry : plotTypes.entrySet()) {
// Create a tree for the category and add it to the main plot
// type tree. It should contain all of the available types for
// the category.
final String category = entry.getKey();
ActionTree categoryTree = new ActionTree(category);
plotTypesTree.add(categoryTree);
// For each plot type in this category, add an ActionTree that,
// when clicked, updates the plot category and type.
for (final String type : entry.getValue()) {
ActionTree typeTree = new ActionTree(new Action(type) {
@Override
public void run() {
try {
plot.draw(category, type, parent);
} catch (Exception e) {
System.err
.println("IPlot error: "
+ "Failed to update the plot type to category \""
+ category + "\" and type \""
+ type + "\".");
}
}
});
categoryTree.add(typeTree);
}
}
} catch (Exception e) {
plotTypesTree.setEnabled(false);
}
// Add widgets to change the representation.
ActionTree repTypesTree = new ActionTree("Representations");
actions.add(repTypesTree);
try {
List<Object> args = new ArrayList<Object>(1);
JSONObject object;
args.add(repId);
object = connection.call("pv.proxy.manager.get", args).get();
JSONArray array = object.getJSONArray("ui");
for (int i = 0; i < array.length(); i++) {
object = array.getJSONObject(i);
if ("Representation".equals(object.get("name"))) {
array = object.getJSONArray("values");
for (i = 0; i < array.length(); i++) {
final String representation = array.getString(i);
repTypesTree.add(new ActionTree(new Action(
representation) {
@Override
public void run() {
writeLock.lock();
try {
repType = representation;
} finally {
writeLock.unlock();
}
refresh();
}
}));
}
break;
}
}
} catch (Exception e) {
repTypesTree.setEnabled(false);
}
// Populate the ToolBarManager with the ActionTrees.
for (ActionTree tree : actions) {
toolBar.add(tree.getContributionItem());
}
toolBar.update(true);
return;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.ice.viz.service.PlotRender#disposePlotComposite(org.eclipse
* .swt.widgets.Composite)
*/
@Override
protected void disposePlotComposite(Composite plotComposite) {
// Dispose of the ActionTrees if necessary.
if (actions != null) {
for (ActionTree tree : actions) {
tree.dispose();
}
actions.clear();
}
return;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.ice.viz.service.connections.ConnectionPlotRender#
* getPreferenceNodeID()
*/
@Override
protected String getPreferenceNodeID() {
return "org.eclipse.ice.viz.service.paraview.preferences";
}
}