CellCentersDemo
VTKEx/Cxx/PolyData/CellCentersDemo
Question
If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
CellCentersDemo.cxx
#include <vtkSmartPointer.h>
#include <vtkCellCenters.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkGlyph3DMapper.h>
#include <vtkBYUReader.h>
#include <vtkOBJReader.h>
#include <vtkPLYReader.h>
#include <vtkPolyDataReader.h>
#include <vtkSTLReader.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkSphereSource.h>
#include <vtksys/SystemTools.hxx>
#include <vtkSphereSource.h>
#include <vtkNamedColors.h>
#include <vtkColor.h>
#include <vector>
#include <algorithm>
namespace
{
vtkSmartPointer<vtkPolyData> ReadPolyData(const char *fileName);
}
int main (int argc, char *argv[])
{
// Define the colors used in the example
vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();
vtkColor3d actorColor = colors->GetColor3d("Banana");
vtkColor3d pointActorColor = colors->GetColor3d("Peacock");
vtkColor3d backgroundColor = colors->GetColor3d("Silver");
// Read the polydata
vtkSmartPointer<vtkPolyData> polyData =
ReadPolyData(argc > 1 ? argv[1] : "");
std::vector<double> bounds(6);;
polyData->GetBounds(&bounds[0]);
std::vector<double> range(3);
for (int i = 0; i < 3; ++i)
{
range[i] = bounds[2*i + 1] - bounds[2*i];
}
std::cout << "Range: "
<< range[0] << ", "
<< range[1] << ", "
<< range[2] << std::endl;
vtkSmartPointer<vtkCellCenters> centers =
vtkSmartPointer<vtkCellCenters>::New();
centers->SetInputData(polyData);
auto maxRange = std::max_element(range.begin(), range.end());
double radius = *maxRange * .0025;
vtkSmartPointer<vtkSphereSource> sphere =
vtkSmartPointer<vtkSphereSource>::New();
sphere->SetPhiResolution(11);
sphere->SetThetaResolution(11);
sphere->SetRadius(radius);
vtkSmartPointer<vtkGlyph3DMapper> pointMapper =
vtkSmartPointer<vtkGlyph3DMapper>::New();
pointMapper->SetInputConnection(centers->GetOutputPort());
pointMapper->SetSourceConnection(sphere->GetOutputPort());
vtkSmartPointer<vtkActor> pointActor =
vtkSmartPointer<vtkActor>::New();
pointActor->SetMapper(pointMapper);
pointActor->GetProperty()->SetColor(pointActorColor.GetData());
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputData(polyData);
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(actorColor.GetData());
actor->GetProperty()->EdgeVisibilityOn();
actor->GetProperty()->SetInterpolationToFlat();
// Setup render window, renderer, and interactor
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->AddActor(pointActor);
renderer->ResetCamera();
renderer->GetActiveCamera()->Azimuth(30);
renderer->GetActiveCamera()->Elevation(30);
renderer->GetActiveCamera()->Dolly(1.5);
renderer->ResetCameraClippingRange();
renderer->SetBackground(backgroundColor.GetData());
renderWindow->SetSize(640, 480);
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
namespace
{
vtkSmartPointer<vtkPolyData> ReadPolyData(const char *fileName)
{
vtkSmartPointer<vtkPolyData> polyData;
std::string extension = vtksys::SystemTools::GetFilenameExtension(std::string(fileName));
if (extension == ".ply")
{
vtkSmartPointer<vtkPLYReader> reader =
vtkSmartPointer<vtkPLYReader>::New();
reader->SetFileName (fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".vtp")
{
vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName (fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".obj")
{
vtkSmartPointer<vtkOBJReader> reader =
vtkSmartPointer<vtkOBJReader>::New();
reader->SetFileName (fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".stl")
{
vtkSmartPointer<vtkSTLReader> reader =
vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName (fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".vtk")
{
vtkSmartPointer<vtkPolyDataReader> reader =
vtkSmartPointer<vtkPolyDataReader>::New();
reader->SetFileName (fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".g")
{
vtkSmartPointer<vtkBYUReader> reader =
vtkSmartPointer<vtkBYUReader>::New();
reader->SetGeometryFileName (fileName);
reader->Update();
polyData = reader->GetOutput();
}
else
{
vtkSmartPointer<vtkSphereSource> source =
vtkSmartPointer<vtkSphereSource>::New();
source->Update();
polyData = source->GetOutput();
}
return polyData;
}
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(CellCentersDemo)
find_package(VTK COMPONENTS
vtkvtkCommonColor
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkFiltersCore
vtkvtkFiltersSources
vtkvtkIOGeometry
vtkvtkIOLegacy
vtkvtkIOPLY
vtkvtkIOXML
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping CellCentersDemo: ${VTK_NOT_FOUND_MESSAGE}")
return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
# old system
include(${VTK_USE_FILE})
add_executable(CellCentersDemo MACOSX_BUNDLE CellCentersDemo.cxx )
target_link_libraries(CellCentersDemo PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(CellCentersDemo MACOSX_BUNDLE CellCentersDemo.cxx )
target_link_libraries(CellCentersDemo PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS CellCentersDemo
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build CellCentersDemo¶
Click here to download CellCentersDemo and its CMakeLists.txt file. Once the tarball CellCentersDemo.tar has been downloaded and extracted,
cd CellCentersDemo/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:
./CellCentersDemo
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.