I am developing a 3d application using VTK java bindings. I was used to use Java3D and I found VTK pretty different.<br><br>I have written a small program that plot spheres with a a label next to them.<br><br>The problem is that if I draw too many sphere (~/1000) the 3d becomes very slow.<br>
<br>I though that all the speheres may share the same Mapper but I do not know if it is the right thing to do.<br><br>Can anyone help me. I &#39;ve copy-pasted a very simple example<br><br><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"></span><span style="font-family: courier new,monospace;">import java.awt.BorderLayout;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import java.awt.Color;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import java.awt.Dimension;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import java.awt.HeadlessException;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import java.util.ArrayList;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import java.util.List;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import java.util.Random;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import java.util.concurrent.Executors;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import javax.swing.JFrame;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import vtk.vtkActor;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import vtk.vtkCamera;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import vtk.vtkCanvas;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import vtk.vtkFollower;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import vtk.vtkPolyDataMapper;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import vtk.vtkRenderer;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import vtk.vtkSphereSource;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">import vtk.vtkTransform;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import vtk.vtkVectorText;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">/**</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> *</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> * @author Enrico Scantamburlo &lt;scantamburlo at <a href="http://streamsim.com">streamsim.com</a>&gt;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> */</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">public class SmallTest extends JFrame {</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    public SmallTest() throws HeadlessException {</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        setLayout(new BorderLayout());</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        setSize(new Dimension(400, 400));</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        final vtkCanvas panel3D = new vtkCanvas();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        getContentPane().add(panel3D, BorderLayout.CENTER);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        panel3D.GetRenderer().SetBackground(1, 1, 1);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">//</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        panel3D.GetRenderer().DrawOff();</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        List&lt;double[]&gt; points = createPoint();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        int i = 0;</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        final vtkRenderer rend = panel3D.GetRenderer();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        vtkCamera camera = rend.GetActiveCamera();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        for (double[] ds : points) {</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            vtkActor sphere = createSphere(ds[0], ds[1], ds[2], Color.blue, rootPaneCheckingEnabled);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">            rend.AddActor(sphere);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            vtkFollower textActor = createText(i++, camera, ds);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">            rend.AddActor(textActor);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        panel3D.GetRenderer().DrawOn();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        Runnable runnable = new Runnable() {</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            public void run() {</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                panel3D.Render();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                rend.ResetCamera();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">                panel3D.Render();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        };</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        Executors.newSingleThreadExecutor().submit(runnable);</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    final double ray = 0.1;</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    private vtkActor createSphere(double x, double y, double z, Color color, boolean transparecy) {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        vtkSphereSource sphereg = new vtkSphereSource();</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        sphereg.SetRadius(ray);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        sphereg.SetThetaResolution(18);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        sphereg.SetPhiResolution(18);</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        // map to graphics objects</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        vtkPolyDataMapper smap = new vtkPolyDataMapper();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        smap.SetInput(sphereg.GetOutput());</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        // actor coordinates geometry, properties, transformation</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        vtkActor aSphere = new vtkActor();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        aSphere.SetMapper(smap);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        aSphere.GetProperty().SetColor(color.getRed() / 256d, color.getGreen() / 256d, color.getBlue() / 256d);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        vtkTransform tran = new vtkTransform();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        tran.Translate(x, y, z);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        aSphere.SetUserTransform(tran);</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        aSphere.PickableOn();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        return aSphere;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    }</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    public static void main(String[] args) {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        java.awt.EventQueue.invokeLater(new Runnable() {</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">            public void run() {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                SmallTest frame = new SmallTest();</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">                frame.setLocationRelativeTo(null);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">                frame.setVisible(true);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">            }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        });</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">    }</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    private static final int MAX = 1000;</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    private List&lt;double[]&gt; createPoint() {</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        ArrayList&lt;double[]&gt; values = new ArrayList&lt;double[]&gt;(MAX);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        Random rand = new Random(System.currentTimeMillis());</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        for (int i = 0; i &lt; MAX; i++) {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">            values.add(new double[]{rand.nextDouble(), rand.nextDouble(), rand.nextDouble()});</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        return values;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    }</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    private vtkFollower createText(int i, vtkCamera camera, double[] ds) {</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        vtkVectorText text = new vtkVectorText();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        text.SetText(&quot;dp&quot; + i);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        vtkFollower textActor = new vtkFollower();</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        textActor.SetCamera(camera);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        textActor.GetProperty().SetLighting(false);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        textActor.GetProperty().SetColor(0, 0, 0);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        vtkPolyDataMapper mapper = new vtkPolyDataMapper();</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        mapper.SetInputConnection(text.GetOutputPort());</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        textActor.SetMapper(mapper);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        textActor.SetPosition(ds[0] + ray, ds[1] + ray, ds[2] + ray);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        final double textScale = 1 / 9.0;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">        textActor.SetScale(textScale);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">        return textActor;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">    }</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">}</span><br><br>