GetCellCenter
Repository source: GetCellCenter
Description¶
Unfortunately this function is not built in directly, but rather quite convoluted to call. This is an easy to use wrapper.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
GetCellCenter.cxx
#include <vtkCell.h>
#include <vtkImageData.h>
#include <vtkNew.h>
namespace {
void GetCellCenter(vtkImageData* imageData, const unsigned int cellId,
double center[3]);
}
int main(int, char*[])
{
// Create an image data
vtkNew<vtkImageData> imageData;
// Specify the size of the image data
imageData->SetDimensions(3, 3, 2); // This will cause 18 points and 4 cells
imageData->SetSpacing(1.0, 1.0, 1.0);
imageData->SetOrigin(0.0, 0.0, 0.0);
std::cout << "Number of points: " << imageData->GetNumberOfPoints()
<< std::endl;
std::cout << "Number of cells: " << imageData->GetNumberOfCells()
<< std::endl;
double center[3] = {0, 0, 0};
for (vtkIdType cellId = 0; cellId < imageData->GetNumberOfCells(); ++cellId)
{
GetCellCenter(imageData, cellId, center);
std::cout << "Cell " << cellId << " center: " << center[0] << " "
<< center[1] << " " << center[2] << std::endl;
}
return EXIT_SUCCESS;
}
namespace {
void GetCellCenter(vtkImageData* imageData, const unsigned int cellId,
double center[3])
{
double pcoords[3] = {0, 0, 0};
double* weights = new double[imageData->GetMaxCellSize()];
vtkCell* cell = imageData->GetCell(cellId);
int subId = cell->GetParametricCenter(pcoords);
cell->EvaluateLocation(subId, pcoords, center, weights);
}
} // namespace
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(GetCellCenter)
find_package(VTK COMPONENTS
CommonCore
CommonDataModel
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "GetCellCenter: 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(GetCellCenter MACOSX_BUNDLE GetCellCenter.cxx )
target_link_libraries(GetCellCenter PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS GetCellCenter
MODULES ${VTK_LIBRARIES}
)
Download and Build GetCellCenter¶
Click here to download GetCellCenter and its CMakeLists.txt file. Once the tarball GetCellCenter.tar has been downloaded and extracted,
cd GetCellCenter/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:
./GetCellCenter
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.