Yury
Yury

Reputation: 1259

3D contour plot C++ example using VTK?

I tried to use VTK library from C++.

Is there any example of using VTK just to plot simple 3D contour graph like the one shown below?

The official VTK Wiki has dozens of code examples, but I cannot find any useful example of this simple task.

a 3d plot

Upvotes: 4

Views: 13545

Answers (4)

kaikuchn
kaikuchn

Reputation: 795

A while back I wanted to do the very same thing and did some extensive searching. It didn't turn up much so I want to share with you my solution. It contains some superfluous code but it works. http://pastie.org/4721543

I just rediscovered this question because VTK 6.0 will have a plotsurface function. Which most likely will be far more performant than what my code does. I will try it later and update this post.

A little explanation to my code might be helpful though. It does the following:

  1. generate a plane the size of your 2D matrix that holds your z values (cols & rows represent the x & y values).
  2. create xyz-points from the input matrix
  3. store the z values as scalars for each point
  4. warp that plane according to the z values of the matrix and apply colors (mapper->SetScalarRange(..)
  5. draw your plot

It also adds a timer and an update command, it outputs rendering statistics, etc. as I said there's much you can leave out. My blog post elaborates a little bit more on this, but not much.

Upvotes: 2

user3592999
user3592999

Reputation: 11

In my system, the code worked when SetInput() is changed to SetInputData() for 'axes' and 'warp'.

Upvotes: 0

Madz
Madz

Reputation: 1279

With the help of Kaikuchn's code I was able to come up with a simple surface plot. Hope this will help someone who's looking for a similar solution.

#include "stdafx.h"


#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkPoints.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkDataSetMapper.h>
#include <vtkProperty.h>
#include <vtkCubeAxesActor2D.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkInteractorStyleTrackball.h>
#include <vtkSimplePointsReader.h>
#include <vtkWarpScalar.h>
#include <vtkAxisActor2D.h>


int mains(int, char *[])
{


// Read the file
  vtkSmartPointer<vtkSimplePointsReader> reader =vtkSmartPointer<vtkSimplePointsReader>::New();
  reader->SetFileName ( "simple.xyz" );
  reader->Update();

  vtkSmartPointer<vtkPolyData> inputPolyData = vtkSmartPointer<vtkPolyData>::New();
  inputPolyData ->CopyStructure(reader->GetOutput());


  // warp plane
  vtkSmartPointer<vtkWarpScalar> warp = vtkSmartPointer<vtkWarpScalar>::New();
  warp->SetInput(inputPolyData);
  warp->SetScaleFactor(0.0);

  // Visualize
  vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New();
  mapper->SetInputConnection(warp->GetOutputPort());



  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
  actor->GetProperty()->SetPointSize(4);
  actor->SetMapper(mapper);

  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->SetBackground(.3, .6, .3);
  renderWindow->Render();

  vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
  renderWindowInteractor->SetInteractorStyle(style);

  // add & render CubeAxes
  vtkSmartPointer<vtkCubeAxesActor2D> axes = vtkSmartPointer<vtkCubeAxesActor2D>::New();
  axes->SetInput(warp->GetOutput());
  axes->SetFontFactor(3.0);
  axes->SetFlyModeToNone();
  axes->SetCamera(renderer->GetActiveCamera());

  vtkSmartPointer<vtkAxisActor2D> xAxis = axes->GetXAxisActor2D();
  xAxis->SetAdjustLabels(1);

  renderer->AddViewProp(axes);
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

The simple.xyz;

0.0 0.0 3.0
1.0 4.0 0.0
0.0 1.0 0.0
4.0 0.0 3.0
1.0 4.0 7.0
0.0 6.0 0.0

Upvotes: 4

g.stevo
g.stevo

Reputation: 740

I believe a rough sketch of a VTK pipeline that may work for you is as follows. If you create some x and y points as vtkPoints and then move them to the StructuredGrid datatype and then add the z-axis using the GeometryFilter you should produce a surface that by going through vtkWarpScalar and the usual Mapper and Actor pipeline.

The following example code may help /Examples/DataManipulation/Cxx/SGrid.cxx in your vtk build. Surface plotting could also be performed if you had a complete volume and extracted and isosurface. If that's not the case and you are plotting a 3D function, probably populating a structured grid is the way to go.

Upvotes: 3

Related Questions