I am trying to visualize vector data using VTK. From some example I managed to write a code as follows:
#include <vtkActor.h>
#include <vtkDataSetMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkDataset.h>
#include <vtkCellData.h>
// #include <vtkPointData.h>
#include <vtkLookupTable.h>
#include <vtkUnstructuredGrid.h>
#include <vtkPointData.h>
#include <vtkDataArray.h>
#include <vtkScalarBarActor.h>
#include <vtkCameraOrientationWidget.h>
#include <vtkGlyph3D.h>
#include <vtkArrowSource.h>
#include <vtkConeSource.h>int main(int argc, char *argv)
{
// parse command line arguments
if (argc < 2)
{
std::cerr << “Usage: " << argv[0] << " Filename(.vtu) e.g. tetra.vtu”
<< std::endl;
return EXIT_FAILURE;
}std::string filename = argv[1];
// std::string filename =“D:\Study\VTK\VTK-9.2.6-Data\Testing\Data\test.vtu”;
// read all the data from the file
vtkNew reader;
reader->SetFileName(filename.c_str());
reader->Update();vtkNew colors;
// Create a mapper and actor
vtkNew mapper;
vtkDataSet *dataSet = reader->GetOutputAsDataSet();
dataSet->Print(std::cout);
mapper->SetInputConnection(reader->GetOutputPort());
mapper->ScalarVisibilityOn();
mapper->SetColorModeToMapScalars();
mapper->UseLookupTableScalarRangeOn();
vtkNew actor;
actor->SetMapper(mapper);
actor->GetProperty()->EdgeVisibilityOn();
actor->GetProperty()->SetLineWidth(2.0);
actor->GetProperty()->SetOpacity(0.5);
actor->GetProperty()->SetColor(colors->GetColor3d(“Tomato”).GetData());//try to generate cell vector:
vtkNew glyph3D;
vtkNew arrowSource;
glyph3D->SetSourceConnection(arrowSource->GetOutputPort());
glyph3D->SetVectorModeToUseVector();
glyph3D->SetInputConnection(reader->GetOutputPort());
glyph3D->SetVectorModeToUseVector();
glyph3D->SetScaleFactor(.8);
glyph3D->Update();vtkNew glyphMapper;
glyphMapper->SetInputConnection(glyph3D->GetOutputPort());vtkNew glyphActor;
glyphActor->SetMapper(glyphMapper);// Create a renderer, render window, and interactor
vtkNew renderer;
vtkNew renderWindow;
renderWindow->AddRenderer(renderer);
vtkNew renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);// Add the actor to the scene
renderer->AddActor(actor);
renderer->AddActor(glyphActor);
renderer->SetBackground(colors->GetColor3d(“Wheat”).GetData());//add a xyz
vtkNew widgetCam;
widgetCam->SetParentRenderer(renderer);
widgetCam->On();// Render and interact
renderWindow->SetSize(640, 480);renderWindow->Render();
renderWindowInteractor->Start();return EXIT_SUCCESS;
}
However, the result seemed not as expected:
as we can see, all vectors are directed at X direction.
By the way I am wondering how to watch the inner data of a vtkDataSet? I tried to watch it by visual studio but could not find any valuable information.
I appreciate your explation, thank you.
test.7z (2.7 MB)