[vtkusers] Adding points at the begining of a polyline

Joachim Pouderoux joachim.pouderoux at kitware.com
Tue Nov 22 08:54:03 EST 2016


Hello,

You are right, vtkCellArray allows to append data but not insert in sense
of shifting data.
Here you are building a set of segments so there is no need to shift
anything (but you can do you manually by creating new arrays if you want).
Append your points and append your cells, the order does no matter, just
remember what is first point (resp. last point to appending).

  int firstPointId = 0;  // Initial start point
  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);

    int pid = points.InsertNextPoint(new_x, new_y, new_z);
    points.Modified();

    lines.InsertNextCell(2);
    lines.InsertCellPoint(firstPointId);
    lines.InsertCellPoint(i);
    firstPointId = i;   // Newly insterted point is new line head point
    lines.Modified();
    canvas.repaint();
  }

Hope it helps.

Best regards,

*Joachim Pouderoux*, PhD

*Technical Expert - Scientific Computing Team*
*Kitware SAS <http://www.kitware.fr>*


2016-11-10 9:36 GMT-04:00 Jean-Max Redonnet <jmax.red at gmail.com>:

> 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();
>         }
>     }
> }
>
> _______________________________________________
> 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
>
> Search the list archives at: http://markmail.org/search/?q=vtkusers
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/vtkusers
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20161122/9f43c155/attachment.html>


More information about the vtkusers mailing list