[vtkusers] Java example for ClipCow

Todd Simons todd.a.simons at gmail.com
Wed Dec 20 10:20:58 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;

/**
 *  In this example vtkClipPolyData is used to cut a polygonal model
 *  of a cow in half. In addition, the open clip is closed by triangulating
 *  the resulting complex polygons.
 */
public class ClipCow extends JPanel {

    vtkPanel renWin;
    vtkClipPolyData clipper;
    vtkCutter cutEdges;
    vtkStripper cutStrips;
    vtkPolyData cutPoly;
    vtkPolyDataMapper cutMapper;

    static final double[] TOMATO = {1.0000, 0.3882, 0.2784};
    public static final double[] PEACOCK = {0.2000, 0.6300, 0.7900};

    public ClipCow() {

        // Prepare render window
        setLayout(new BorderLayout());
        renWin = new vtkPanel();

        // First start by reading a cow model. We also generate surface
normals for
        // prettier rendering.
        vtkBYUReader cow = new vtkBYUReader();
//      cow.SetGeometryFileName(VtkUtil.getVtkDataRoot() +
"/Data/Viewpoint/cow.g");
        cow.SetGeometryFileName("c:/user/VTK/Data/Viewpoint/cow.g");
        vtkPolyDataNormals cowNormals = new vtkPolyDataNormals();
        cowNormals.SetInput(cow.GetOutput());

        // We clip with an implicit function. Here we use a plane positioned
near
        // the center of the cow model and oriented at an arbitrary angle.
        vtkPlane plane = new vtkPlane();
        plane.SetOrigin(0.25, 0, 0);
        plane.SetNormal(-1, -1, 0);

        // vtkClipPolyData requires an implicit function to define what it
is to
        // clip with. Any implicit function, including complex boolean
combinations
        // can be used. Notice that we can specify the value of the implicit
function
        // with the  SetValue method.
        clipper = new vtkClipPolyData();
        clipper.SetInput(cowNormals.GetOutput());
        clipper.SetClipFunction(plane);
        clipper.GenerateClipScalarsOn();
        clipper.GenerateClippedOutputOn();
        clipper.SetValue(0.5);
        vtkPolyDataMapper clipMapper = new vtkPolyDataMapper();
        clipMapper.SetInput(clipper.GetOutput());
        clipMapper.ScalarVisibilityOff();
        vtkProperty backProp = new vtkProperty();
        backProp.SetDiffuseColor(TOMATO);
        vtkActor clipActor = new vtkActor();
        clipActor.SetMapper(clipMapper);
        clipActor.GetProperty().SetColor(PEACOCK);
        clipActor.SetBackfaceProperty(backProp);

        // Here we are cutting the cow. Cutting creates lines where the cut
        // function intersects the model. (Clipping removes a portion of the
        // model but the dimension of the data does not change.)
        //
        // The reason we are cutting is to generate a closed polygon at the
        // boundary of the clipping process.The cutter generates line
        // segments, the stripper then puts them together into polylines.We
        // then pull a trick and define polygons using the closed line
        // segements that the stripper created.
        cutEdges = new vtkCutter();
        cutEdges.SetInput(cowNormals.GetOutput());
        cutEdges.SetCutFunction(plane);
        cutEdges.GenerateCutScalarsOn();
        cutEdges.SetValue(0, 0.5);
        cutStrips = new vtkStripper();
        cutStrips.SetInput(cutEdges.GetOutput());
        cutStrips.Update();
        cutPoly = new vtkPolyData();
        cutPoly.SetPoints(cutStrips.GetOutput().GetPoints());
        cutPoly.SetPolys(cutStrips.GetOutput().GetLines());

        // Triangle filter is robust enough to ignore the duplicate point at
        // the beginning and end of the polygons and triangulate them.
        vtkTriangleFilter cutTriangles = new vtkTriangleFilter();
        cutTriangles.SetInput(cutPoly);
        cutMapper = new vtkPolyDataMapper();
        cutMapper.SetInput(cutPoly);
        cutMapper.SetInput(cutTriangles.GetOutput());
        vtkActor cutActor = new vtkActor();
        cutActor.SetMapper(cutMapper);
        cutActor.GetProperty().SetColor(PEACOCK);

        // The clipped part ofthe cow is rendered wireframe.
        vtkPolyDataMapper restMapper = new vtkPolyDataMapper();
        restMapper.SetInput(clipper.GetClippedOutput());
        restMapper.ScalarVisibilityOff();
        vtkActor restActor = new vtkActor();
        restActor.SetMapper(restMapper);
        restActor.GetProperty().SetRepresentationToWireframe();

        // Add the actors to the renderer, set the background and size
        vtkRenderer ren = renWin.GetRenderer();
        ren.AddActor(clipActor);
        ren.AddActor(cutActor);
        ren.AddActor(restActor);
        ren.SetBackground(1, 1, 1);
        ren.GetActiveCamera().Azimuth(30);
        ren.GetActiveCamera().Elevation(30);
        ren.GetActiveCamera().Dolly(1.5);
        ren.ResetCameraClippingRange();

//        renWin.setSize(300, 300);

        // Palce render window in the center of this panel
        add(renWin, BorderLayout.CENTER);
    }


    /**
     *  Lets you move the cut plane back and forth by invoking the function
     *  Cut with the appropriate plane value (essentially a distance from
     *  the original plane).  This is not used in this code but should give
     *  you an idea of how to define a function to do this.
     */
    private void cut(double v) {
        clipper.SetValue(v);
        cutEdges.SetValue(0, v);
        cutStrips.Update();
        cutPoly.SetPoints(cutStrips.GetOutput().GetPoints());
        cutPoly.SetPolys(cutStrips.GetOutput().GetLines());
        cutMapper.Update();
        renWin.Render();
    }

    /**
     *
     */
    public static void main(String s[]) {

        ClipCow panel = new ClipCow();

        JFrame frame = new JFrame("ClipCow");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.getContentPane().add("Center", panel);
        frame.pack();
        frame.setVisible(true);
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20061220/c80192c8/attachment.htm>


More information about the vtkusers mailing list