[vtkusers] Adding points at the begining of a polyline

Jean-Max Redonnet jmax.red at gmail.com
Thu Nov 10 08:36:15 EST 2016


Hi  ! First, I would like to thanks people who works on VTK. This is a
great piece of software, very fast, very efficient !

I'm currently working on a viewer that is supposed to display dynamically
the polylines calculated by my algorithms. To do that, I relied on this
post :
http://public.kitware.com/pipermail/vtkusers/2010-July/061781.html

This works fine to append points to the polyline, but due to my algorithms,
I need also to prepend points at the beginning of the polyline. I can't
figure out how to do that. Any help would be gracefully appreciated.

jMax

===
Here is my test code. The first loop works fine, but the second one is a
mess.

package vtktest;

import java.awt.BorderLayout;

public class PolyLineTest extends JFrame {
    private static final long serialVersionUID = 1L;

    static {
        if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
        }
        vtkNativeLibrary.DisableOutputWindow(null);
    }

    private Random random = new Random();
    private vtkRenderWindowPanel canvas;
    private vtkRenderer renderer;
    private vtkRenderWindowInteractor iren;
    private JPanel mainPanel;

    public static void main(String[] args) {
        new PolyLineTest();
    }

    public PolyLineTest() {
        this.canvas = new vtkRenderWindowPanel();
        this.renderer = canvas.GetRenderer();
        this.iren = canvas.getRenderWindowInteractor();
        this.setTitle("VTK Test");

        // First we'll create an initial point to build on
        vtkPoints points = new vtkPoints();
        points.InsertPoint(0, 0.0, 0.0, 0.0);

        // The cell array can be thought of as a connectivity list. Here we
        // specify the number of points followed by that number of point
        // ids. This can be repeated as many times as there are primitives
in
        // the list.
        // This first one is just a point to connect to as we add more
points
        vtkCellArray lines = new vtkCellArray();
        lines.InsertNextCell(1); // number of points
        lines.InsertCellPoint(0);

        vtkPolyData track = new vtkPolyData();
        track.SetPoints(points);
        track.SetLines(lines);

        vtkPolyDataMapper mapper = new vtkPolyDataMapper();
        mapper.SetInputData(track);

        vtkActor actor = new vtkActor();
        actor.SetMapper(mapper);
        actor.GetProperty().SetColor(0.3800, 0.7000, 0.1600);

        renderer.AddActor(actor);
        renderer.ResetCamera();

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(canvas, BorderLayout.CENTER);

        this.getContentPane().add(mainPanel);
        this.setSize(800, 800);
        this.setLocationRelativeTo(null); // Center on desktop

        this.setVisible(true);

        // Loop to add points to line
        System.out.println("Appending");
        for (int i = 1; i < 10; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            double[] prev_pt = points.GetPoint(i - 1);
            double new_x = prev_pt[0] + (2.0 * random.nextDouble() - 1.0);
            double new_y = prev_pt[1] + (2.0 * random.nextDouble() - 1.0);
            double new_z = prev_pt[2] + (2.0 * random.nextDouble() - 1.0);

            // Insert the new point
            // then tell VTK that the data has been modified so the Render()
            // call will update the view with the new data
            points.InsertPoint(i, new_x, new_y, new_z);
            points.Modified();

            // To get lines we need a beginning and end point in the
            // connectivity list,
            // then again tell VTK that the data has been modified
            lines.InsertNextCell(2);
            lines.InsertCellPoint(i - 1);
            lines.InsertCellPoint(i);
            lines.Modified();
            canvas.repaint();
        }

        System.out.println("Prepending");
        for (int i = 1; i < 10; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            double[] prev_pt = points.GetPoint(0);
            double new_x = prev_pt[0] + (2.0 * random.nextDouble() - 1.0);
            double new_y = prev_pt[1] + (2.0 * random.nextDouble() - 1.0);
            double new_z = prev_pt[2] + (2.0 * random.nextDouble() - 1.0);

            points.InsertPoint(0, new_x, new_y, new_z);
            points.Modified();

            lines.InsertNextCell(2);
            lines.UpdateCellCount (2);
            lines.InsertCellPoint(0);
            lines.InsertCellPoint(1);
            lines.Modified();
            canvas.repaint();
        }
    }
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20161110/8709e768/attachment.html>


More information about the vtkusers mailing list