[vtkusers] plotting vector field from vtu file in java

christian.poliwoda christian.poliwoda at gcsc.uni-frankfurt.de
Fri Aug 3 09:20:21 EDT 2012


I am trying for some days now to plot a vector field from a vtu file 
where some "arrays/data" are in.

My problem is similar to: 
http://vtk.1045678.n5.nabble.com/Visualizing-a-vector-field-from-a-vtkImageData-td4792007.html

I translated the following examples into java and they work.
http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/VectorFieldNonZeroExtraction
http://www.vtk.org/Wiki/VTK/Examples/Cxx/WishList/Visualization/VectorField

But if i try to visualize the field from the vtu file the vector are all 
paralell to the x-axis
and neither of the vectors in the file are parallel to the x-axis 
(checked with ParaView).

My Code is attached.

Any suggestions on how to get these glyphs to point in the direction 
specified by the data in the file?

Thanks in advance,
Christian

PS: Notice that the visualization class is a own written class that show 
the results.

  The console output:

name of array[0]: c1
name of array[1]: c2
name of array[2]: SomeVector
name of array[3]: c
name of array[4]: Linker
name of array[5]: UserFct
name of array[6]: ElemDiscValue
name of array[7]: LuaFct
name of array[8]: ConstData
number of thresholded points: 0

Value for the parameters:

file = "path/to/file.vtu"
elementInFile = 2
threshold = 0.0001

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
- - - - - - - - -
package edu.gcsc.vrl.vtk;

import eu.mihosoft.vrl.annotation.ComponentInfo;
import eu.mihosoft.vrl.annotation.ParamInfo;
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) {

         Visualization vis = new Visualization();


//        // Create an image
//        vtkImageData image = new vtkImageData();
//
//        CreateVectorField(image);

         vtkXMLUnstructuredGridReader reader = new 
vtkXMLUnstructuredGridReader();

//            System.out.println("-- reader = "+ reader);
         reader.SetFileName(file.getAbsolutePath());
         reader.Update();
         vtkUnstructuredGrid image = reader.GetOutput();


//        // This filter produces a vtkImageData with an array named 
"Magnitude"
//        vtkImageMagnitude magnitudeFilter = new vtkImageMagnitude();
// magnitudeFilter.SetInputConnection(image.GetProducerPort());
//        magnitudeFilter.Update();
//
// 
image.GetPointData().AddArray(magnitudeFilter.GetOutput().GetPointData().GetScalars());
//        image.GetPointData().SetActiveScalars("Magnitude");

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

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

         // in case you want to save imageData
         //vtkXMLPolyDataWriter writer = vtkXMLPolyDataWriter.New();
         //writer.SetFileName("output.vtp");
//writer.SetInputConnection(thresholdPoints.GetOutputPort());
         //writer.Write();

         // repesents the pixels
         vtkCubeSource cubeSource = new vtkCubeSource();
         cubeSource.SetXLength(0.01);
         cubeSource.SetYLength(0.01);
         cubeSource.SetZLength(0.01);

         vtkGlyph3D glyph = new vtkGlyph3D();
         glyph.SetInput(image);
         glyph.SetSourceConnection(cubeSource.GetOutputPort()); //show cubes
         // don't scale glyphs according to any scalar data
//        glyph.SetScaleModeToDataScalingOff();

         vtkPolyDataMapper glyphMapper = new vtkPolyDataMapper();
         glyphMapper.SetInputConnection(glyph.GetOutputPort());
         // don't color glyphs according to scalar data
//        glyphMapper.ScalarVisibilityOff();
//        glyphMapper.SetScalarModeToDefault();
         glyphMapper.SetScalarModeToUsePointData();

         vtkActor actor = new vtkActor();
         actor.SetMapper(glyphMapper);

          vis.addActor(actor);




         // 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(thresholdVector.GetOutputPort());
// vectorGlyph.SetInputConnection(thresholdVector.GetOutputPort());

//        vtkGlyph2D vectorfieldVector = new vtkGlyph2D();
//
//        vectorfieldVector.SetInput(image);
//        vectorfieldVector.SetInputArrayToProcess(
//                elementInFile,
//                image.GetInformation());
//
//
//        vectorfieldVector.Update();

         vectorGlyph.SetInputConnection(image.GetProducerPort());


         // in case you want the point glyphs to be oriented according to
         // scalar values in array "ImageScalars" uncomment the 
following line
//        image.GetPointData().SetActiveVectors("ImageScalars");

// 
image.GetPointData().SetActiveVectors(image.GetPointData().GetArrayName(elementInFile));

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

         vectorGlyph.SetScaleFactor(0.025);

         vectorGlyph.Update();

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

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


         vis.addActor(vectorActor);

         return vis;
     }

}


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120803/4bb40105/attachment.htm>


More information about the vtkusers mailing list