[vtkusers] Java example for CSpline
Todd Simons
todd.a.simons at gmail.com
Wed Dec 20 10:24:16 EST 2006
Hello all,
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.
Best Regards,
Todd
package examples;
import vtk.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* This example demonstrates the use of vtkCardinalSpline.
* It creates random points and connects them with a spline
*/
public class CSpline extends JPanel {
//from vtk.util.colors import tomato, banana
public CSpline () {
setLayout(new BorderLayout());
vtkPanel renWin = new vtkPanel();
vtkRenderer renderer = renWin.GetRenderer();
int numberOfInputPoints = 20;
// One spline for each direction.
vtkCardinalSpline aSplineX, aSplineY, aSplineZ;
aSplineX = new vtkCardinalSpline();
aSplineY = new vtkCardinalSpline();
aSplineZ = new vtkCardinalSpline();
/* Generate random (pivot) points and add the corresponding
* coordinates to the splines.
* aSplineX will interpolate the x values of the points
* aSplineY will interpolate the y values of the points
* aSplineZ will interpolate the z values of the points */
vtkMath math=new vtkMath();
vtkPoints inputPoints = new vtkPoints();
for (int i=0; i<numberOfInputPoints; i++) {
double x = math.Random(0, 1);
double y = math.Random(0, 1);
double z = math.Random(0, 1);
aSplineX.AddPoint(i, x);
aSplineY.AddPoint(i, y);
aSplineZ.AddPoint(i, z);
inputPoints.InsertPoint(i, x, y, z);
} //i loop
// The following section will create glyphs for the pivot points
// in order to make the effect of the spline more clear.
// Create a polydata to be glyphed.
vtkPolyData inputData = new vtkPolyData();
inputData.SetPoints(inputPoints);
// Use sphere as glyph source.
vtkSphereSource balls = new vtkSphereSource();
balls.SetRadius(.01);
balls.SetPhiResolution(10);
balls.SetThetaResolution(10);
vtkGlyph3D glyphPoints = new vtkGlyph3D();
glyphPoints.SetInput(inputData);
glyphPoints.SetSource(balls.GetOutput());
vtkPolyDataMapper glyphMapper = new vtkPolyDataMapper();
glyphMapper.SetInputConnection(glyphPoints.GetOutputPort());
vtkActor glyph = new vtkActor();
glyph.SetMapper(glyphMapper);
glyph.GetProperty().SetDiffuseColor(0.0, 1.0, 0.0);
glyph.GetProperty().SetSpecular(.3);
glyph.GetProperty().SetSpecularPower(30);
// Generate the polyline for the spline.
vtkPoints points = new vtkPoints();
vtkPolyData profileData = new vtkPolyData();
// Number of points on the spline
int numberOfOutputPoints = 400;
// Interpolate x, y and z by using the three spline filters and
// create new points
double t;
for (int i=0; i<numberOfOutputPoints; i++) {
t =
(double)(numberOfInputPoints-1)/(double)(numberOfOutputPoints-1)*(double)i;
points.InsertPoint(i, aSplineX.Evaluate(t), aSplineY.Evaluate(t),
aSplineZ.Evaluate(t));
}//i loop
// Create the polyline.
vtkCellArray lines = new vtkCellArray();
lines.InsertNextCell(numberOfOutputPoints);
for (int i=0; i<numberOfOutputPoints; i++) lines.InsertCellPoint(i);
profileData.SetPoints(points);
profileData.SetLines(lines);
// Add thickness to the resulting line.
vtkTubeFilter profileTubes = new vtkTubeFilter();
profileTubes.SetNumberOfSides(8);
profileTubes.SetInput(profileData);
profileTubes.SetRadius(.005);
vtkPolyDataMapper profileMapper = new vtkPolyDataMapper();
profileMapper.SetInputConnection(profileTubes.GetOutputPort());
vtkActor profile = new vtkActor();
profile.SetMapper(profileMapper);
profile.GetProperty().SetDiffuseColor(1.0, 0.0, 0.0);
profile.GetProperty().SetSpecular(.3);
profile.GetProperty().SetSpecularPower(30);
renderer.AddActor(glyph);
renderer.AddActor(profile);
renderer.SetBackground(1,1,1);
add(renWin, BorderLayout.CENTER);
}//constructor
/**
* main method
*/
public static void main(String s[]) {
CSpline panel = new CSpline();
JFrame frame = new JFrame("CSpline");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add("Center", panel);
frame.pack();
frame.setVisible(true);
}//main
} //class CSpline
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20061220/c22021a6/attachment.htm>
More information about the vtkusers
mailing list