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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With