Ubuntu VTK Java项目构建和运行

魔法师LQ

Ubuntu VTK Java项目构建和运行

准备工作

在自己建立的VTK build的文件夹(这里名称为VTK-bin,见前文),找到vtk.jar,这里在VTK-bin/lib下。

新建工程

使用JetBrains的IDEA新建一个工程,将样例代码(见附录A)添加到工程中的src中,

添加vtk.jar

make之后生成的VTK-bin/lib路径下的vtk.jar路径添加到Project Structure->Modules->Dependencies中。

添加库

Ubuntu中.so文件相当于windows下的.dll动态链接库文件,

make之后生成的VTK-bin/lib路径加入到环境变量中,

1
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/[你自己的路径]/VTK-bin/lib

在IDEA中,File->Project Structure->Libraries(或者Ctrl+Shift+Alt+s),

编译运行

若出现类似vtkxxxx not loaded,请查看Ubuntu VTK java lang UnsatisfiedLinkError no vtkxxx in java library path

参考

[1] https://www.particleincell.com/2011/vtk-java-visualization/

附录

附录A. 一个vtk 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
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JToggleButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import vtk.vtkNativeLibrary;
import vtk.vtkPanel;
import vtk.vtkActor;
import vtk.vtkSphere;
import vtk.vtkSphereSource;
import vtk.vtkSampleFunction;
import vtk.vtkContourFilter;
import vtk.vtkPlane;
import vtk.vtkCutter;
import vtk.vtkLookupTable;
import vtk.vtkPolyDataMapper;

/* ************************************************************
* Demo applications showcasing how to use VTK with Java
*
* Based on SimpleVTK.java example distributed with VTK
*
* For more information see:
* http://www.particleincell.com/2011/vtk-java-visualization
*
* Information about VTK can be found at:
* http://vtk.org/
*
* ***********************************************************/

public class DemoJavaVTK extends JPanel implements ActionListener
{
private static final long serialVersionUID = 1L;
private vtkPanel renWin;
private vtkActor cutActor;
private vtkActor isoActor;

private JPanel buttons;
private JToggleButton slicesButton;
private JToggleButton isoButton;
private JButton exitButton;

/* Load VTK shared librarires (.dll) on startup, print message if not found */
static
{
if (!vtkNativeLibrary.LoadAllNativeLibraries())
{
for (vtkNativeLibrary lib : vtkNativeLibrary.values())
{
if (!lib.IsLoaded())
System.out.println(lib.GetLibraryName() + " not loaded");
}

System.out.println("Make sure the search path is correct: ");
System.out.println(System.getProperty("java.library.path"));
}
vtkNativeLibrary.DisableOutputWindow(null);
}

/* Constructor - generates visualization pipeline and adds actors*/
public DemoJavaVTK()
{
super(new BorderLayout()); /* large center and small border areas*/

double radius = 0.8; /*sphere radius*/

/**** 1) INPUT DATA: Sphere Implicit Function ****/
vtkSphere sphere = new vtkSphere();
sphere.SetRadius(radius);

vtkSampleFunction sample = new vtkSampleFunction();
sample.SetSampleDimensions(50,50,50);
sample.SetImplicitFunction(sphere);

/**** 2) PIPELINE 1: Isosurface Actor ****/

/* contour filter - will generate isosurfaces from 3D data*/
vtkContourFilter contour = new vtkContourFilter();
contour.SetInputConnection(sample.GetOutputPort());
contour.GenerateValues(3,0,1);

/* mapper, translates polygonal representation to graphics primitives */
vtkPolyDataMapper isoMapper = new vtkPolyDataMapper();
isoMapper.SetInputConnection(contour.GetOutputPort());

/*isosurface actor*/
isoActor = new vtkActor();
isoActor.SetMapper(isoMapper);

/**** 3) PIPELINE 2: Cutting Plane Actor ****/

/* define a plane in x-y plane and passing through the origin*/
vtkPlane plane = new vtkPlane();
plane.SetOrigin(0,0,0);
plane.SetNormal(0,0,1);

/* cutter, basically interpolates source data onto the plane */
vtkCutter planeCut = new vtkCutter();
planeCut.SetInputConnection(sample.GetOutputPort());
planeCut.SetCutFunction(plane);
/*this will actually create 3 planes at the subspace where the implicit
* function evaluates to -0.7, 0, 0.7 (0 would be original plane). In
* our case this will create three x-y planes passing through
* z=-0.7, z=0, and z=+0.7*/
planeCut.GenerateValues(3,-0.7,0.7);

/* look up table, we want to reduce number of values to get discrete bands */
vtkLookupTable lut = new vtkLookupTable();
lut.SetNumberOfTableValues(5);

/* mapper, using our custom LUT */
vtkPolyDataMapper cutMapper = new vtkPolyDataMapper();
cutMapper.SetInputConnection(planeCut.GetOutputPort());
cutMapper.SetLookupTable(lut);

/* cutting plane actor, looks much better with flat shading */
cutActor = new vtkActor();
cutActor.SetMapper(cutMapper);
cutActor.GetProperty().SetInterpolationToFlat();

/**** 4) PIPELINE 3: Surface Geometry Actor ****/

/* create polygonal representation of a sphere */
vtkSphereSource surf = new vtkSphereSource();
surf.SetRadius(radius);

/* another mapper*/
vtkPolyDataMapper surfMapper = new vtkPolyDataMapper();
surfMapper.SetInputConnection(surf.GetOutputPort());

/* surface geometry actor, turn on edges and apply flat shading*/
vtkActor surfActor = new vtkActor();
surfActor.SetMapper(surfMapper);
surfActor.GetProperty().EdgeVisibilityOn();
surfActor.GetProperty().SetEdgeColor(0.2,0.2,0.2);
surfActor.GetProperty().SetInterpolationToFlat();

/**** 5) RENDER WINDOW ****/

/* vtkPanel - this is the interface between Java and VTK */
renWin = new vtkPanel();

/* add the surface geometry plus the isosurface */
renWin.GetRenderer().AddActor(surfActor);
renWin.GetRenderer().AddActor(isoActor);

/* the default zoom is whacky, zoom out to see the whole domain */
renWin.GetRenderer().GetActiveCamera().Dolly(0.2);
renWin.GetRenderer().SetBackground(1, 1, 1);

/**** 6) CREATE PANEL FOR BUTTONS ****/
buttons = new JPanel();
buttons.setLayout(new GridLayout(1,0));

/* isosurface button, clicked by default */
isoButton = new JToggleButton("Isosurfaces",true);
isoButton.addActionListener(this);

/* cutting planes button */
slicesButton = new JToggleButton("Slices");
slicesButton.addActionListener(this);

/* exit button */
exitButton = new JButton("Exit");
exitButton.addActionListener(this);

/* add buttons to the panel */
buttons.add(isoButton);
buttons.add(slicesButton);
buttons.add(exitButton);

/**** 7) POPULATE MAIN PANEL ****/
add(renWin, BorderLayout.CENTER);
add(buttons, BorderLayout.SOUTH);
}

/* ActionListener that responds to button clicks
* Toggling iso/slices buttons results in addition or removal
* of the corresponding actor */
public void actionPerformed(ActionEvent e)
{
/*cutting planes button, add or remove cutActor */
if (e.getSource().equals(slicesButton))
{
if (slicesButton.isSelected())
renWin.GetRenderer().AddActor(cutActor);
else
renWin.GetRenderer().RemoveActor(cutActor);

renWin.Render();
}
/*isosurface button, add or remove isoActor */
else if (e.getSource().equals(isoButton))
{
if (isoButton.isSelected())
renWin.GetRenderer().AddActor(isoActor);
else
renWin.GetRenderer().RemoveActor(isoActor);
renWin.Render();
}
/*exit button, end application */
else if (e.getSource().equals(exitButton))
{
System.exit(0);
}
}

/* main, creates a new JFrame and populates it with the DemoJavaVTK panel */
public static void main(String s[])
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
JFrame frame = new JFrame("Java and VTK Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(new DemoJavaVTK(), BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}