[vtkusers] plotting vector field from vtu file in java

christian.poliwoda christian.poliwoda at gcsc.uni-frankfurt.de
Fri Aug 3 10:19:26 EDT 2012


Finally it works.
I overlooked to set: 
image.GetPointData().SetVectors(image.GetPointData().GetArray(elementInFile));

In case that somebody want to see the whole code it is attached.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- - - - - - - - -

package edu.gcsc.vrl.vtk;

import java.io.File;
import java.io.Serializable;
import vtk.vtkActor;
import vtk.vtkArrowSource;
import vtk.vtkCubeSource;
import vtk.vtkGlyph3D;
import vtk.vtkPolyData;
import vtk.vtkPolyDataMapper;
import vtk.vtkThresholdPoints;
import vtk.vtkUnstructuredGrid;
import vtk.vtkXMLUnstructuredGridReader;

public class VectorFieldExample implements Serializable {

     private static final long serialVersionUID = 1L;

     public Visualization VectorFieldNonZeroExtraction(
             File file,
             int elementInFile,
             double threshold,
             double scaleFactor) {

         Visualization vis = new Visualization();

         vtkXMLUnstructuredGridReader reader = new 
vtkXMLUnstructuredGridReader();

         reader.SetFileName(file.getAbsolutePath());
         reader.Update();
         vtkUnstructuredGrid image = reader.GetOutput();
image.GetPointData().SetVectors(image.GetPointData().GetArray(elementInFile));


         vtkThresholdPoints thresholdVector = new vtkThresholdPoints();
         thresholdVector.SetInput(image);
         thresholdVector.SetInputArrayToProcess(
                 elementInFile,
                 image.GetInformation());

         thresholdVector.ThresholdByUpper(threshold);
         thresholdVector.Update();


         // represent vector field
         vtkGlyph3D vectorGlyph = new vtkGlyph3D();
         vtkArrowSource arrowSource = new vtkArrowSource();
         vtkPolyDataMapper vectorGlyphMapper = new vtkPolyDataMapper();

         int n = image.GetPointData().GetNumberOfArrays();
         for (int i = 0; i < n; i++) {
             System.out.println("name of array[" + i + "]: " + 
image.GetPointData().GetArrayName(i));
         }

         vtkPolyData tmp = thresholdVector.GetOutput();
         System.out.println("number of thresholded points: " + 
tmp.GetNumberOfPoints());

         vectorGlyph.SetInputConnection(image.GetProducerPort());
vectorGlyph.SetSourceConnection(arrowSource.GetOutputPort());
         vectorGlyph.SetScaleModeToScaleByVector();
         vectorGlyph.SetVectorModeToUseVector();
         vectorGlyph.ScalingOn();
         vectorGlyph.OrientOn();
         vectorGlyph.SetInputArrayToProcess(
                 elementInFile,
                 image.GetInformation());

         vectorGlyph.SetScaleFactor(scaleFactor);

         vectorGlyph.Update();

vectorGlyphMapper.SetInputConnection(vectorGlyph.GetOutputPort());
         vectorGlyphMapper.Update();

         vtkActor vectorActor = new vtkActor();
         vectorActor.SetMapper(vectorGlyphMapper);

         vis.addActor(vectorActor);
         return vis;
     }
}




More information about the vtkusers mailing list