VTK/Examples/Cxx/ImageData/GetCellCenter
From KitwarePublic
Unfortunately this function is not built in directly, but rather quite convoluted to call. This is an easy to use wrapper.
GetCellCenter.cxx
#include <vtkSmartPointer.h> #include <vtkCell.h> #include <vtkImageData.h> static void GetCellCenter(vtkImageData* imageData, const unsigned int cellId, double center[3]); int main(int, char *[]) { // Create an image data vtkSmartPointer<vtkImageData> imageData = vtkSmartPointer<vtkImageData>::New(); // 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(unsigned int 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; } 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); }
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) PROJECT(CellIdFromGridCoordinates) find_package(VTK REQUIRED) include(${VTK_USE_FILE}) if (APPLE) add_executable(CellIdFromGridCoordinates MACOSX_BUNDLE CellIdFromGridCoordinates.cxx) else() add_executable(CellIdFromGridCoordinates CellIdFromGridCoordinates.cxx) endif() if(VTK_LIBRARIES) target_link_libraries(CellIdFromGridCoordinates ${VTK_LIBRARIES}) else() target_link_libraries(CellIdFromGridCoordinates vtkHybrid ) endif()