C ++ 3D outline example using VTK?

I tried using the VTK library from C ++.

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

the official VTK Wiki contains dozens of code examples, but I cannot find a useful example of this simple task.

a 3d plot

+3


source to share


4 answers


While back I wanted to do the same and did a lot of searching. It didn't really make a difference, so I want to share my solution with you. It contains some extra code, but it works. http://pastie.org/4721543

I just opened this question because VTK 6.0 will have a plotsurface feature. It will most likely be much more than my code. I will try this later and update this post.

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



  • create a plane sized by your 2D matrix that holds your z values โ€‹โ€‹(cols and rows represent the x and y values).
  • create xyz-points from input matrix
  • store z values โ€‹โ€‹as scalars for each point
  • flip this plane according to the z values โ€‹โ€‹of the matrix and apply colors (mapper->SetScalarRange(..)

  • draw your plot

It also adds a timer and refresh command, outputs rendering stats, etc., as I said a lot there that you can leave. My blog post goes into more detail on this, but not much.

+2


source


I believe a rough sketch of a VTK pipeline that might 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 a GeometryFilter, you should create a surface that goes through vtkWarpScalar and a regular Mapper and Actor path.



The following code example might help /Examples/DataManipulation/Cxx/SGrid.cxx in your vtk assembly. Surface rendering can also be performed if you have full volume and extract and isosurface. If that's not the case and you're planning a 3D feature, perhaps populating a structured mesh is the way to go.

+3


source


With the Kaikuchn code, I was able to come up with a simple surface patch. Hope this helps someone 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;
}

      

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

      

+1


source


On my system, the code worked when it SetInput()

was changed to SetInputData()

for "axes" and "warp".

-1


source







All Articles