<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;<br>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> * In this example vtkClipPolyData is used to cut a polygonal model<br> * of a cow in half. In addition, the open clip is closed by triangulating<br> * the resulting complex polygons.<br> */<br>public class ClipCow extends JPanel {
</p>
<p> vtkPanel renWin;<br> vtkClipPolyData clipper;<br> vtkCutter cutEdges;<br> vtkStripper cutStrips;<br> vtkPolyData cutPoly;<br> vtkPolyDataMapper cutMapper;</p>
<p> static final double[] TOMATO = {1.0000, 0.3882, 0.2784};<br> public static final double[] PEACOCK = {0.2000, 0.6300, 0.7900};</p>
<p> public ClipCow() {</p>
<p> // Prepare render window<br> setLayout(new BorderLayout());<br> renWin = new vtkPanel();</p>
<p> // First start by reading a cow model. We also generate surface normals for<br> // prettier rendering.<br> vtkBYUReader cow = new vtkBYUReader();<br>// cow.SetGeometryFileName(VtkUtil.getVtkDataRoot
() + "/Data/Viewpoint/cow.g");<br> cow.SetGeometryFileName("c:/user/VTK/Data/Viewpoint/cow.g");<br> vtkPolyDataNormals cowNormals = new vtkPolyDataNormals();<br> cowNormals.SetInput
(cow.GetOutput());</p>
<p> // We clip with an implicit function. Here we use a plane positioned near<br> // the center of the cow model and oriented at an arbitrary angle.<br> vtkPlane plane = new vtkPlane();<br> plane.SetOrigin
(0.25, 0, 0);<br> plane.SetNormal(-1, -1, 0);</p>
<p> // vtkClipPolyData requires an implicit function to define what it is to<br> // clip with. Any implicit function, including complex boolean combinations<br> // can be used. Notice that we can specify the value of the implicit function
<br> // with the SetValue method.<br> clipper = new vtkClipPolyData();<br> clipper.SetInput(cowNormals.GetOutput());<br> clipper.SetClipFunction(plane);<br> clipper.GenerateClipScalarsOn
();<br> clipper.GenerateClippedOutputOn();<br> clipper.SetValue(0.5);<br> vtkPolyDataMapper clipMapper = new vtkPolyDataMapper();<br> clipMapper.SetInput(clipper.GetOutput());<br> clipMapper.ScalarVisibilityOff
();<br> vtkProperty backProp = new vtkProperty();<br> backProp.SetDiffuseColor(TOMATO);<br> vtkActor clipActor = new vtkActor();<br> clipActor.SetMapper(clipMapper);<br> clipActor.GetProperty
().SetColor(PEACOCK);<br> clipActor.SetBackfaceProperty(backProp);</p>
<p> // Here we are cutting the cow. Cutting creates lines where the cut<br> // function intersects the model. (Clipping removes a portion of the<br> // model but the dimension of the data does not change.)
<br> //<br> // The reason we are cutting is to generate a closed polygon at the<br> // boundary of the clipping process.The cutter generates line<br> // segments, the stripper then puts them together into
polylines.We<br> // then pull a trick and define polygons using the closed line<br> // segements that the stripper created.<br> cutEdges = new vtkCutter();<br> cutEdges.SetInput(cowNormals.GetOutput
());<br> cutEdges.SetCutFunction(plane);<br> cutEdges.GenerateCutScalarsOn();<br> cutEdges.SetValue(0, 0.5);<br> cutStrips = new vtkStripper();<br> cutStrips.SetInput(cutEdges.GetOutput());
<br> cutStrips.Update();<br> cutPoly = new vtkPolyData();<br> cutPoly.SetPoints(cutStrips.GetOutput().GetPoints());<br> cutPoly.SetPolys(cutStrips.GetOutput().GetLines());</p>
<p> // Triangle filter is robust enough to ignore the duplicate point at<br> // the beginning and end of the polygons and triangulate them.<br> vtkTriangleFilter cutTriangles = new vtkTriangleFilter();
<br> cutTriangles.SetInput(cutPoly);<br> cutMapper = new vtkPolyDataMapper();<br> cutMapper.SetInput(cutPoly);<br> cutMapper.SetInput(cutTriangles.GetOutput());<br> vtkActor cutActor = new vtkActor();
<br> cutActor.SetMapper(cutMapper);<br> cutActor.GetProperty().SetColor(PEACOCK);</p>
<p> // The clipped part ofthe cow is rendered wireframe.<br> vtkPolyDataMapper restMapper = new vtkPolyDataMapper();<br> restMapper.SetInput(clipper.GetClippedOutput());<br> restMapper.ScalarVisibilityOff
();<br> vtkActor restActor = new vtkActor();<br> restActor.SetMapper(restMapper);<br> restActor.GetProperty().SetRepresentationToWireframe();</p>
<p> // Add the actors to the renderer, set the background and size<br> vtkRenderer ren = renWin.GetRenderer();<br> ren.AddActor(clipActor);<br> ren.AddActor(cutActor);<br> ren.AddActor(restActor);
<br> ren.SetBackground(1, 1, 1);<br> ren.GetActiveCamera().Azimuth(30);<br> ren.GetActiveCamera().Elevation(30);<br> ren.GetActiveCamera().Dolly(1.5);<br> ren.ResetCameraClippingRange();
</p>
<p>// renWin.setSize(300, 300);</p>
<p> // Palce render window in the center of this panel<br> add(renWin, BorderLayout.CENTER);<br> }</p>
<p><br> /**<br> * Lets you move the cut plane back and forth by invoking the function<br> * Cut with the appropriate plane value (essentially a distance from<br> * the original plane). This is not used in this code but should give
<br> * you an idea of how to define a function to do this.<br> */<br> private void cut(double v) {<br> clipper.SetValue(v);<br> cutEdges.SetValue(0, v);<br> cutStrips.Update();<br>
cutPoly.SetPoints(cutStrips.GetOutput().GetPoints());<br> cutPoly.SetPolys(cutStrips.GetOutput().GetLines());<br> cutMapper.Update();<br> renWin.Render();<br> }</p>
<p> /**<br> *<br> */<br> public static void main(String s[]) {</p>
<p> ClipCow panel = new ClipCow();</p>
<p> JFrame frame = new JFrame("ClipCow");<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> }<br>}</p>
<p> </p></div>