[vtkusers] Newbie user simple example

Sebastien Jourdain sebastien.jourdain at kitware.com
Wed Sep 22 08:07:28 EDT 2010


Hi Enrico,

there is several way to deal with your problem. I will enumerate each
of them and give you some hint so you can give a try which one is more
suitable for you. But first of all, be aware that VTK can be slow when
it has to render a lot of actors.

1) As you said, use a single mapper with a bunch of actor.
To do so, just create and pipe the sphereSource and the mapper once.
And then create as many actor as you want and bind them to the same
mapper. Then just set the position of the sphere to each actor so they
won't show on the same location. If you want each actor can have its
own transform, so you can also scale the sphere too.
I did used this solution once, because I needed to be able to select
each actor independently in order to show or hide them.

2) Create a sphereSource for each sphere and append them using a
vtkAppendDataSet filter and then just pipe a single mapper and actor
to that filter.

3) In the same way than the solution 2, create a dataset that as a
vertex for each sphere location and use the glyph filter with a
sphereSource as glyph to produce the expected dataset and then just
pipe a single mapper and actor.

Hope you get the ideas,

Seb

On Tue, Sep 21, 2010 at 7:31 PM, Enrico Scantamburlo
<scantamburlo at streamsim.com> wrote:
> 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;
>     }
> }
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at:
> http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
>



More information about the vtkusers mailing list