Overview

VTK an open source, freely available software system for 3D computer graphics, image processing, and visualization. It has its own file format for storing datasets in files with extension .vtk.
mayavi a scientific data visualizer. It can read, display, and manipulate datasets stored in vtk-files.
vtk a java package for creating vtk-files.

example

import vtk.*;

public class Polygonal {
  public static void main(String[] argv) {
    PolygonalDataset pd=new PolygonalDataset("data", 5);
    // geometry
    //           index  x    y    z
    pd.setPoint( 0,     1.5, 0  , 0  );  
    pd.setPoint( 1,     0  , 1.5, 0  );  
    pd.setPoint( 2,     1.5, 1.5, 0  );  
    pd.setPoint( 3,     0  , 0  , 0  );  
    pd.setPoint( 4,    -1.5,-1.5, 0  );  
   
    // topology
    pd.addPolygon(new int[] {0, 1, 2, 0});
    pd.addLines  (new int[] {3, 4});
    
    // fields (here only one)
    ColorField cf = new ColorField("lut", 5,1,1);
    // set colour of points 
    //          index   r g b  alpha
    cf.setColor(0,0,0, 1,0,0,   1);
    cf.setColor(1,0,0, 0,1,0,   1);
    cf.setColor(2,0,0, 0,0,1,   1);
    cf.setColor(3,0,0, 0,0,0,   1);
    cf.setColor(4,0,0, 0,0,0,   1);
               
    // add colour field to this Polygonal Dataset
    pd.add(cf);
               
    pd.write("polygonal.vtk");
  }
}
# vtk DataFile Version 2.0
data
ASCII
DATASET POLYDATA
POINTS 5 float
 1.5  0.0  0.0 
 0.0  1.5  0.0 
 1.5  1.5  0.0 
 0.0  0.0  0.0 
-1.5 -1.5  0.0 
LINES 1 3
2 3 4 
POLYGONS 1 5
4 0 1 2 0 
POINT_DATA 5
COLOR_SCALARS lut 4
1.0  0.0  0.0  1.0
0.0  1.0  0.0  1.0
0.0  0.0  1.0  1.0
0.0  0.0  0.0  1.0
0.0  0.0  0.0  1.0

Datasets

the components of a dataset
geometrya set of 3-dimensional points
topologya structure defined on these points, e.g. a grid or a set of polygons
one or more fieldsmappings that map each point to either a scalar, a vector, or a color

vtk.Field

A field is defined on a discrete nx x ny x nz grid.
For each point of this grid a value can be defined.
Values are scalars, vectors, or colors.
abstract class Field {
   private String name;
   private int nx, ny, nz; // dimensions

   public String getName();
   public int[/*3*/] getDimensions();
}

vtk.ScalarField

A field that can handle scalars: for each grid point (i,j,k) a scalar can be set.
class ScalarField extends Field {
   public ScalarField(String name, int nx, int ny, int nz);
   public double getScalar(int i, int j, int k);
   public void setScalar(int i, int j, int k, double value);
}

vtk.ColorField

A field that can handle colors: for each grid point (i,j,k) a color value can be set. A color consists of 4 doubles in the interval [0,1]: red, green, blue and alpha.
class ColorField extends Field {
   public ColorField(String name, int nx, int ny, int nz);
   public double[] getColor(int i, int j, int k);
   public void setColor(int i, int j, int k, 
                        double r, double g, double b, double alpha);
}

vtk.VectorField

A field that can handle vectors: for each grid point (i,j,k) a vector value can be set.
class VectorField extends Field {
   public VectorField(String name, int nx, int ny, int nz);
   public double[] getVector(int i, int j, int k);
   public void setVector(int i, int j, int k, 
                        double x, double y, double z);
}

vtk.Dataset

A dataset is a collection of points combined with a collection of fields.
abstract class Dataset {
  private String name; ...
  
  public String getName();
  public int getNumberOfPoints();
  public void add(ScalarField sf);
  public void add(VectorField vf);
  public void add(ColorField cf);
  public void write(String filename);
  ...
}

vtk.PolygonalDatatset

A dataset with vertices, lines and polygons. Fields associated with this data set should have dimensions (n,1,1). Where n is the number of points of the dataset.
class PolygonalDataset extends Dataset {
   public PolygonalDataset(String name, int n);
   public void addVertices(int[] pi);
   public void addLines(int[] pi);
   public void addPolygon(int[] pi);
   public void setPoint(int i, double x, double y, double z);
   ...
}

vtk.StructuredDataset

A dataset based on a grid of size nx x ny x nz. Fields associated with this dataset should have dimensions (nx,ny,nz).
abstract class StructuredDataset extends Dataset {
   private int nx, ny, nz; // dimensions
   public int[/*3*/] getDimensions();
}

vtk.StructuredPoints

A grid with a regular geometry and a regular topology.
class StructuredPoints extends StructuredDataset {
   public StructuredPoints(String name, int nx, int ny, int nz);
   public void setOrigin(double x, double y, double z);
   public void setSpacing(double sx, double sy, double sz);
   ...
}

vtk.StructuredGrid

A grid with an irregular geometry and a regular topology.
class StructuredGrid extends StructuredDataset {
   public StructuredGrid(String name, int nx, int ny, int nz);
   public void setPoint(int i, int j, int k, 
                        double x, double y, double z);
   ...
}

package vtk example 1

import vtk.*;

public class Polygonal {
  public static void main(String[] argv) {
    PolygonalDataset pd=new PolygonalDataset("data", 5);
    // geometry
    //           index  x    y    z
    pd.setPoint( 0,     1.5, 0  , 0  );  
    pd.setPoint( 1,     0  , 1.5, 0  );  
    pd.setPoint( 2,     1.5, 1.5, 0  );  
    pd.setPoint( 3,     0  , 0  , 0  );  
    pd.setPoint( 4,    -1.5,-1.5, 0  );  
   
    // topology
    pd.addPolygon(new int[] {0, 1, 2, 0});
    pd.addLines  (new int[] {3, 4});
    
    // fields (here only one)
    ColorField cf = new ColorField("lut", 5,1,1);
    // set colour of points 
    //          index   r g b  alpha
    cf.setColor(0,0,0, 1,0,0,   1);
    cf.setColor(1,0,0, 0,1,0,   1);
    cf.setColor(2,0,0, 0,0,1,   1);
    cf.setColor(3,0,0, 0,0,0,   1);
    cf.setColor(4,0,0, 0,0,0,   1);
               
    // add colour field to this Polygonal Dataset
    pd.add(cf);
               
    pd.write("polygonal.vtk");
  }
}




















package vtk example 2

import vtk.*;

public class StructPoints {
  public static void main(String[] argv) {
   int[] dim = new int[] { 20, 10 , 5 }; // dimensions
     
   // create a Structured Points Dataset       
   StructuredPoints sp = new StructuredPoints("structured points", 
                                          dim[0], dim[1], dim[2]);
   sp.setOrigin(0,0,0);
   sp.setSpacing(0.1,0.1,0.1);
        
   ScalarField sf = new ScalarField("data",dim[0],dim[1],dim[2]);
   for(int i = 0; i < dim[0]; i = i+1)
      for(int j = 0; j < dim[1]; j = j+1)
         for(int k = 0; k < dim[2]; k=k+1) {
            double dx=i-(dim[0]-1)/2d;
            double dy=j-(dim[1]-1)/2d;
            double dz=k-(dim[2]-1)/2d;
            sf.setScalar(i, j, k, dx*dx + dy*dy + dz*dz);
         }
   sp.add(sf);
   sp.write("points.vtk");
  }
}

package vtk example 3

import vtk.*;

public class StructGrid {
  public static void main(String[] argv) {
   int[] dim = new int[] { 20, 20 , 1 }; // dimensions
        StructuredGrid sg = new StructuredGrid("structured grid", 
                                          dim[0], dim[1], dim[2]);
        
        for(int i = 0; i < dim[0]; i = i+1)
            for(int j = 0; j < dim[1]; j = j+1)
                for(int k = 0; k < dim[2]; k=k+1) {
                    double x=Math.log(10*i+1);
                    double y=Math.log(10*j+1);
                    double z=0;
                    sg.setPoint(i , j, k, x, y, z);
                }

        VectorField vf = new VectorField("data",dim[0],dim[1],dim[2]);
        for(int i = 0; i < dim[0]; i = i+1)
            for(int j = 0; j < dim[1]; j = j+1)
                for(int k = 0; k < dim[2]; k=k+1) {
                    double x=0;
                    double y=0;
                    double z=i+j;
                    vf.setVector(i,j,k,x,y,z);
                }
        sg.add(vf);
        sg.write("grid.vtk");
        
    }
}

links

Jaspis: https://www.win.tue.nl/jaspis
2Z860 : https://www.win.tue.nl/~wstahw/2Z860