[vtkusers] Contours over unstructured grids

Mikko Lyly Mikko.Lyly at csc.fi
Thu Oct 9 14:29:21 EDT 2008


Dear all,

I'm working on an application which should draw contours of scalar fields over unstructured grids.
The contours should be colored according to a second scalar field (interpolated over the same grid).

I have successfully managed to generate and display contours without colors from the second field,
but coloring the contours by using n-tuples in vtkFloatArrays seems to result in broken manifolds (see
the attached figure).

I have tried to reduce the problem to the code below. The code sets up a point set in a deterministic
way in the unit cube, generates a Delaunay tessellation over them, and sets up point data for three
scalar fields. The output should be one flat contour for x=0 colored accoring to the z-coordinate.
The code perfors the Delaunay tetrahedralization just for demontration purposes, the "true" code is
related to external finite element meshes, which are placed in vtkUnstructuredGrid.

I would appreciate it very much, if someone could point me to the right direction, how to deal with the
problem of broken contours. I'm having the feeling that I'm working with the scalars in vtkFloatArrays
in a somehow wrong way.

Thank you in advance,
Mikko


#include "vtkUnstructuredGrid.h"
#include "vtkPoints.h"
#include "vtkDelaunay3D.h"
#include "vtkDataSetMapper.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkActor.h"
#include "vtkFloatArray.h"
#include "vtkContourFilter.h"
#include "vtkPointData.h"
#include "vtkPolyDataNormals.h"

#define NOF_POINTS 100

int main()
{
  double x[3];

  // Initialize:
  //-------------
  vtkRenderer *renderer = vtkRenderer::New();
  vtkRenderWindow *renderWindow = vtkRenderWindow::New();
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  renderWindow->AddRenderer(renderer);
  iren->SetRenderWindow(renderWindow);

  // Points:
  //---------
  vtkPoints *points = vtkPoints::New();
  points->SetNumberOfPoints(NOF_POINTS);
  for(int i = 0; i < NOF_POINTS; i++) {
    x[0] = sin(1.0 * (double)i); // x
    x[1] = sin(2.0 * (double)i); // y
    x[2] = sin(3.0 * (double)i); // z
    points->InsertPoint(i, x);
  }

  // Grid:
  //-------
  vtkUnstructuredGrid *grid = vtkUnstructuredGrid::New();
  grid->SetPoints(points);

  // Scalar fields (use x, y, and z):
  //----------------------------------
  vtkFloatArray *dataArray = vtkFloatArray::New();
  dataArray->SetNumberOfComponents(3);
  dataArray->SetNumberOfTuples(NOF_POINTS);
  dataArray->SetName("dataArray");
  for(int i = 0; i < NOF_POINTS; i++) {
    x[0] = sin(1.0 * (double)i); // x
    x[1] = sin(2.0 * (double)i); // y
    x[2] = sin(3.0 * (double)i); // z
    dataArray->SetTuple(i, x);
  }
  grid->GetPointData()->SetScalars(dataArray);

  // Tetras:
  //---------
  vtkDelaunay3D *delaunay = vtkDelaunay3D::New();
  delaunay->SetInput(grid);

  // Contours:
  //-----------
  vtkContourFilter *contour = vtkContourFilter::New();
  contour->SetInputConnection(delaunay->GetOutputPort());
  contour->SetValue(0, 0.0); // contour for x = 0.0

  // Mapper:
  //---------
  vtkDataSetMapper *mapper = vtkDataSetMapper::New();
  mapper->SetInputConnection(contour->GetOutputPort());
  mapper->ScalarVisibilityOn();
  mapper->ColorByArrayComponent("dataArray", 2); // color = z
  mapper->SetScalarRange(-1.0, 1.0);

  // Actor:
  //--------
  vtkActor *actor = vtkActor::New();
  actor->SetMapper(mapper);
  renderer->AddActor(actor);

  // Render:
  //---------
  renderer->ResetCamera();
  renderWindow->SetSize(500, 500);
  renderWindow->Render();
  iren->Start();

  // Bye:
  //-----
  return 0;
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot.png
Type: image/png
Size: 3355 bytes
Desc: Screenshot.png
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20081009/8964f7cc/attachment.png>


More information about the vtkusers mailing list