CombineImportedActors
Repository source: CombineImportedActors
Other languages
See (PythonicAPI)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
CombineImportedActors.cxx
// Importers
#include <vtk3DSImporter.h>
#include <vtkGLTFImporter.h>
#include <vtkOBJImporter.h>
#include <vtkVRMLImporter.h>
// Rendering
#include <vtkActorCollection.h>
#include <vtkCamera.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkVersion.h>
// Pipeline
#include <vtkAppendPolyData.h>
#include <vtkCellData.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkUnsignedCharArray.h>
#if VTK_BUILD_VERSION >= 20240630
#define USE_IMPORTER_UPDATE
#endif
#include <algorithm> // For transform()
#include <cctype> // For to_lower
#include <string> // For find_last_of()
int main(int argc, char* argv[])
{
vtkSmartPointer<vtkRenderWindow> importRenderWindow;
vtkSmartPointer<vtkRenderer> importRenderer;
if (argc < 2)
{
std::cerr << "Need a file e.g. iflamingo.3ds" << std::endl;
return EXIT_FAILURE;
}
std::string fileName = argv[1];
std::string extension = "";
// int filePrefixArgOffset = 0; // Depends on importer.
if (fileName.find_last_of(".") != std::string::npos)
{
extension = fileName.substr(fileName.find_last_of(".") + 1);
}
// Make the extension lowercase.
std::transform(extension.begin(), extension.end(), extension.begin(),
::tolower);
if (extension == "wrl")
{
vtkNew<vtkVRMLImporter> importer;
importer->SetFileName(argv[1]);
importer->SetRenderWindow(importRenderWindow);
importRenderWindow = importer->GetRenderWindow();
#ifdef USE_IMPORTER_UPDATE
importer->Update();
#else
importer->Read();
#endif
importRenderer = importer->GetRenderer();
// filePrefixArgOffset = 2;
}
else if (extension == "3ds")
{
vtkNew<vtk3DSImporter> importer;
importer->SetFileName(argv[1]);
importer->SetRenderWindow(importRenderWindow);
importRenderWindow = importer->GetRenderWindow();
#ifdef USE_IMPORTER_UPDATE
importer->Update();
#else
importer->Read();
#endif
importRenderer = importer->GetRenderer();
// filePrefixArgOffset = 2;
}
else if (extension == "gltf" || extension == "glb")
{
vtkNew<vtkGLTFImporter> importer;
importer->SetFileName(argv[1]);
importer->SetRenderWindow(importRenderWindow);
importRenderWindow = importer->GetRenderWindow();
#ifdef USE_IMPORTER_UPDATE
importer->Update();
#else
importer->Read();
#endif
importRenderer = importer->GetRenderer();
// filePrefixArgOffset = 2;
}
else if (extension == "obj")
{
vtkNew<vtkOBJImporter> importer;
importer->SetFileName(argv[1]);
importer->SetFileNameMTL(argv[2]);
importer->SetTexturePath(argv[3]);
importer->SetRenderWindow(importRenderWindow);
importRenderWindow = importer->GetRenderWindow();
#ifdef USE_IMPORTER_UPDATE
importer->Update();
#else
importer->Read();
#endif
importRenderer = importer->GetRenderer();
// filePrefixArgOffset = 4;
}
vtkNew<vtkAppendPolyData> append;
vtkActorCollection* allActors = importRenderer->GetActors();
vtkCollectionSimpleIterator actorsIt;
vtkActor* anActor = nullptr;
;
allActors->InitTraversal(actorsIt);
while ((anActor = allActors->GetNextActor(actorsIt)))
{
vtkNew<vtkPolyData> appendPD;
anActor->GetMapper()->Update();
if (anActor->GetUserMatrix() != nullptr)
{
vtkNew<vtkTransform> transform;
transform->SetMatrix(anActor->GetUserMatrix());
vtkNew<vtkTransformPolyDataFilter> transformPD;
transformPD->SetTransform(transform);
transformPD->SetInputData(
dynamic_cast<vtkPolyData*>(anActor->GetMapper()->GetInput()));
transformPD->Update();
appendPD->DeepCopy(transformPD->GetOutput());
}
else
{
auto pd = dynamic_cast<vtkPolyData*>(anActor->GetMapper()->GetInput());
appendPD->DeepCopy(pd);
}
vtkNew<vtkUnsignedCharArray> cellData;
cellData->SetNumberOfComponents(3);
cellData->SetNumberOfTuples(appendPD->GetNumberOfCells());
for (int i = 0; i < appendPD->GetNumberOfCells(); i++)
{
double rgb[4];
anActor->GetProperty()->GetDiffuseColor(rgb);
for (int j = 0; j < 4; ++j)
{
rgb[j] *= 255.0;
}
cellData->InsertTuple(i, rgb);
}
appendPD->GetCellData()->SetScalars(cellData);
append->AddInputData(appendPD);
}
append->Update();
vtkNew<vtkNamedColors> colors;
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(append->GetOutput());
mapper->SetScalarModeToUseCellData();
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Banana").GetData());
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(640, 480);
renderWindow->SetWindowName("CombineImportedActors");
vtkNew<vtkRenderer> renderer;
renderer->UseHiddenLineRemovalOn();
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
// renderer->SetActiveCamera(importRenderer->GetActiveCamera());
renderWindow->AddRenderer(renderer);
renderer->AddActor(actor);
auto camera = renderer->GetActiveCamera();
camera->SetPosition(-7, 34, 2);
camera->SetViewUp(0, 0, 1);
renderer->ResetCamera();
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(CombineImportedActors)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
CommonTransforms
FiltersCore
FiltersGeneral
IOImport
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "CombineImportedActors: 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(CombineImportedActors MACOSX_BUNDLE CombineImportedActors.cxx )
target_link_libraries(CombineImportedActors PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS CombineImportedActors
MODULES ${VTK_LIBRARIES}
)
Download and Build CombineImportedActors¶
Click here to download CombineImportedActors and its CMakeLists.txt file. Once the tarball CombineImportedActors.tar has been downloaded and extracted,
cd CombineImportedActors/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:
./CombineImportedActors
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.