<div>Hello all,</div>
<div> </div>
<div>I'm a relatively new VTK developer. I am using Java as the gui for my VTK applications. I had a hard time getting started and finding examples written in Java. I have ported a dozen examples to Java to get familiar with it. I wanted to post my examples so other Java enthusiasts could get up to speed on VTK a bit more easily. I hope this helps.
</div>
<div> </div>
<div>Best Regards,</div>
<div>Todd</div>
<div> </div>
<div> </div>
<div> </div>
<div>
<p>package examples;</p>
<p>import vtk.*;</p>
<p>import javax.swing.*;<br>import java.awt.*;<br>import java.awt.event.WindowAdapter;<br>import java.awt.event.WindowEvent;</p>
<p>/**<br> * This example demonstrates the use of vtkCardinalSpline.<br> * It creates random points and connects them with a spline<br> */<br>public class CSpline extends JPanel {</p>
<p>//from vtk.util.colors import tomato, banana</p>
<p> public CSpline () {<br> setLayout(new BorderLayout());<br> vtkPanel renWin = new vtkPanel();<br> vtkRenderer renderer = renWin.GetRenderer();</p>
<p> int numberOfInputPoints = 20;</p>
<p> // One spline for each direction.<br> vtkCardinalSpline aSplineX, aSplineY, aSplineZ;<br> aSplineX = new vtkCardinalSpline();<br> aSplineY = new vtkCardinalSpline();<br> aSplineZ = new vtkCardinalSpline();
</p>
<p>/* Generate random (pivot) points and add the corresponding<br> * coordinates to the splines.<br> * aSplineX will interpolate the x values of the points<br> * aSplineY will interpolate the y values of the points<br>
* aSplineZ will interpolate the z values of the points */<br> vtkMath math=new vtkMath();<br> vtkPoints inputPoints = new vtkPoints();<br> for (int i=0; i<numberOfInputPoints; i++) {<br> double x = math.Random
(0, 1);<br> double y = math.Random(0, 1);<br> double z = math.Random(0, 1);<br> aSplineX.AddPoint(i, x);<br> aSplineY.AddPoint(i, y);<br> aSplineZ.AddPoint(i, z);<br> inputPoints.InsertPoint(i, x, y, z);
<br> } //i loop</p>
<p> // The following section will create glyphs for the pivot points<br> // in order to make the effect of the spline more clear.</p>
<p> // Create a polydata to be glyphed.<br> vtkPolyData inputData = new vtkPolyData();<br> inputData.SetPoints(inputPoints);</p>
<p> // Use sphere as glyph source.<br> vtkSphereSource balls = new vtkSphereSource();<br> balls.SetRadius(.01);<br> balls.SetPhiResolution(10);<br> balls.SetThetaResolution(10);</p>
<p> vtkGlyph3D glyphPoints = new vtkGlyph3D();<br> glyphPoints.SetInput(inputData);<br> glyphPoints.SetSource(balls.GetOutput());</p>
<p> vtkPolyDataMapper glyphMapper = new vtkPolyDataMapper();<br> glyphMapper.SetInputConnection(glyphPoints.GetOutputPort());</p>
<p> vtkActor glyph = new vtkActor();<br> glyph.SetMapper(glyphMapper);<br> glyph.GetProperty().SetDiffuseColor(0.0, 1.0, 0.0);<br> glyph.GetProperty().SetSpecular(.3);<br> glyph.GetProperty().SetSpecularPower(30);
</p>
<p> // Generate the polyline for the spline.<br> vtkPoints points = new vtkPoints();<br> vtkPolyData profileData = new vtkPolyData();</p>
<p> // Number of points on the spline<br> int numberOfOutputPoints = 400;</p>
<p> // Interpolate x, y and z by using the three spline filters and<br> // create new points<br> double t;<br> for (int i=0; i<numberOfOutputPoints; i++) {<br> t = (double)(numberOfInputPoints-1)/(double)(numberOfOutputPoints-1)*(double)i;
<br> points.InsertPoint(i, aSplineX.Evaluate(t), aSplineY.Evaluate(t),<br> aSplineZ.Evaluate(t));<br> }//i loop</p>
<p> // Create the polyline.<br> vtkCellArray lines = new vtkCellArray();<br> lines.InsertNextCell(numberOfOutputPoints);<br> for (int i=0; i<numberOfOutputPoints; i++) lines.InsertCellPoint(i);</p>
<p> profileData.SetPoints(points);<br> profileData.SetLines(lines);</p>
<p> // Add thickness to the resulting line.<br> vtkTubeFilter profileTubes = new vtkTubeFilter();<br> profileTubes.SetNumberOfSides(8);<br> profileTubes.SetInput(profileData);<br> profileTubes.SetRadius
(.005);</p>
<p> vtkPolyDataMapper profileMapper = new vtkPolyDataMapper();<br> profileMapper.SetInputConnection(profileTubes.GetOutputPort());</p>
<p> vtkActor profile = new vtkActor();<br> profile.SetMapper(profileMapper);<br> profile.GetProperty().SetDiffuseColor(1.0, 0.0, 0.0);<br> profile.GetProperty().SetSpecular(.3);<br> profile.GetProperty
().SetSpecularPower(30);</p>
<p> renderer.AddActor(glyph);<br> renderer.AddActor(profile);<br> renderer.SetBackground(1,1,1);</p>
<p> add(renWin, BorderLayout.CENTER);</p>
<p> }//constructor</p>
<p> /**<br> * main method<br> */<br> public static void main(String s[]) {</p>
<p> CSpline panel = new CSpline();</p>
<p> JFrame frame = new JFrame("CSpline");<br> frame.addWindowListener(new WindowAdapter() {<br> public void windowClosing(WindowEvent e) {<br> System.exit(0);<br> }
<br> });<br> frame.getContentPane().add("Center", panel);<br> frame.pack();<br> frame.setVisible(true);<br> }//main</p>
<p>} //class CSpline<br></p></div>