[vtkusers] Newbie user simple example

Enrico Scantamburlo scantamburlo at streamsim.com
Tue Sep 21 19:31:25 EDT 2010


I am developing a 3d application using VTK java bindings. I was used to use
Java3D and I found VTK pretty different.

I have written a small program that plot spheres with a a label next to
them.

The problem is that if I draw too many sphere (~/1000) the 3d becomes very
slow.

I though that all the speheres may share the same Mapper but I do not know
if it is the right thing to do.

Can anyone help me. I 've copy-pasted a very simple example


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;
import javax.swing.JFrame;
import vtk.vtkActor;
import vtk.vtkCamera;
import vtk.vtkCanvas;
import vtk.vtkFollower;
import vtk.vtkPolyDataMapper;
import vtk.vtkRenderer;
import vtk.vtkSphereSource;
import vtk.vtkTransform;
import vtk.vtkVectorText;

/**
 *
 * @author Enrico Scantamburlo <scantamburlo at streamsim.com>
 */
public class SmallTest extends JFrame {

    public SmallTest() throws HeadlessException {

        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(new Dimension(400, 400));

        final vtkCanvas panel3D = new vtkCanvas();
        getContentPane().add(panel3D, BorderLayout.CENTER);

        panel3D.GetRenderer().SetBackground(1, 1, 1);
//
        panel3D.GetRenderer().DrawOff();

        List<double[]> points = createPoint();
        int i = 0;
        final vtkRenderer rend = panel3D.GetRenderer();
        vtkCamera camera = rend.GetActiveCamera();
        for (double[] ds : points) {

            vtkActor sphere = createSphere(ds[0], ds[1], ds[2], Color.blue,
rootPaneCheckingEnabled);
            rend.AddActor(sphere);

            vtkFollower textActor = createText(i++, camera, ds);
            rend.AddActor(textActor);
        }
        panel3D.GetRenderer().DrawOn();
        Runnable runnable = new Runnable() {

            public void run() {


                panel3D.Render();
                rend.ResetCamera();
                panel3D.Render();
            }
        };

        Executors.newSingleThreadExecutor().submit(runnable);



    }
    final double ray = 0.1;

    private vtkActor createSphere(double x, double y, double z, Color color,
boolean transparecy) {
        vtkSphereSource sphereg = new vtkSphereSource();

        sphereg.SetRadius(ray);
        sphereg.SetThetaResolution(18);
        sphereg.SetPhiResolution(18);

        // map to graphics objects
        vtkPolyDataMapper smap = new vtkPolyDataMapper();
        smap.SetInput(sphereg.GetOutput());

        // actor coordinates geometry, properties, transformation
        vtkActor aSphere = new vtkActor();
        aSphere.SetMapper(smap);
        aSphere.GetProperty().SetColor(color.getRed() / 256d,
color.getGreen() / 256d, color.getBlue() / 256d);


        vtkTransform tran = new vtkTransform();
        tran.Translate(x, y, z);
        aSphere.SetUserTransform(tran);

        aSphere.PickableOn();
        return aSphere;
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                SmallTest frame = new SmallTest();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
    private static final int MAX = 1000;

    private List<double[]> createPoint() {
        ArrayList<double[]> values = new ArrayList<double[]>(MAX);
        Random rand = new Random(System.currentTimeMillis());
        for (int i = 0; i < MAX; i++) {
            values.add(new double[]{rand.nextDouble(), rand.nextDouble(),
rand.nextDouble()});
        }
        return values;
    }

    private vtkFollower createText(int i, vtkCamera camera, double[] ds) {
        vtkVectorText text = new vtkVectorText();
        text.SetText("dp" + i);
        vtkFollower textActor = new vtkFollower();

        textActor.SetCamera(camera);
        textActor.GetProperty().SetLighting(false);
        textActor.GetProperty().SetColor(0, 0, 0);
        vtkPolyDataMapper mapper = new vtkPolyDataMapper();
        mapper.SetInputConnection(text.GetOutputPort());
        textActor.SetMapper(mapper);
        textActor.SetPosition(ds[0] + ray, ds[1] + ray, ds[2] + ray);
        final double textScale = 1 / 9.0;
        textActor.SetScale(textScale);
        return textActor;
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20100922/41bc9ef1/attachment.htm>


More information about the vtkusers mailing list