CombinePolyData
vtk-examples/Cxx/Filtering/CombinePolyData
Description¶
This example reads two .vtp files (or produces them if not specified as command line arguments), combines them, and displays the result to the screen.
Other languages
See (Python)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
CombinePolyData.cxx
#include <vtkActor.h>
#include <vtkAppendPolyData.h>
#include <vtkCamera.h>
#include <vtkCleanPolyData.h>
#include <vtkConeSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkPolyData> input1;
vtkNew<vtkPolyData> input2;
if (argc == 1) // command line arguments not specified
{
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetCenter(5, 0, 0);
sphereSource->Update();
input1->ShallowCopy(sphereSource->GetOutput());
vtkNew<vtkConeSource> coneSource;
coneSource->Update();
input2->ShallowCopy(coneSource->GetOutput());
}
else
{
if (argc != 3)
{
std::cout << "argc = " << argc << std::endl;
std::cout << "Required arguments: File1 File2" << std::endl;
return EXIT_FAILURE;
}
std::string inputFilename1 = argv[1];
std::string inputFilename2 = argv[2];
vtkNew<vtkXMLPolyDataReader> reader1;
reader1->SetFileName(inputFilename1.c_str());
reader1->Update();
input1->ShallowCopy(reader1->GetOutput());
vtkNew<vtkXMLPolyDataReader> reader2;
reader2->SetFileName(inputFilename2.c_str());
reader2->Update();
input2->ShallowCopy(reader2->GetOutput());
}
// Append the two meshes
vtkNew<vtkAppendPolyData> appendFilter;
appendFilter->AddInputData(input1);
appendFilter->AddInputData(input2);
// Remove any duplicate points.
vtkNew<vtkCleanPolyData> cleanFilter;
cleanFilter->SetInputConnection(appendFilter->GetOutputPort());
cleanFilter->Update();
// Create a mapper and actor
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(cleanFilter->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
// Create a renderer, render window, and interactor
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actors to the scene
renderer->AddActor(actor);
// Render and interact
renderWindowInteractor->Initialize();
renderWindow->Render();
renderer->SetBackground(colors->GetColor3d("deep_ochre").GetData());
renderer->GetActiveCamera()->Zoom(0.9);
renderWindow->SetWindowName("CombinePolyData");
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(CombinePolyData)
find_package(VTK COMPONENTS
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "CombinePolyData: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(CombinePolyData MACOSX_BUNDLE CombinePolyData.cxx )
target_link_libraries(CombinePolyData PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS CombinePolyData
MODULES ${VTK_LIBRARIES}
)
Download and Build CombinePolyData¶
Click here to download CombinePolyData and its CMakeLists.txt file. Once the tarball CombinePolyData.tar has been downloaded and extracted,
cd CombinePolyData/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./CombinePolyData
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.