Skip to content

MiscCellData

vtk-examples/Cxx/PolyData/MiscCellData

Description

This demo attaches a double to a cell (triangle) in a polydata.

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

MiscCellData.cxx

#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkDoubleArray.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkTriangle.h>
#include <vtkXMLPolyDataWriter.h>

int main(int, char*[])
{
  // We will write the resulting file to Test.vtp so it can be inspected in
  // Paraview
  std::string outputFilename = "output.vtp";

  // Setup 3 points
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(1.0, 0.0, 0.0);
  points->InsertNextPoint(0.0, 0.0, 0.0);
  points->InsertNextPoint(0.0, 1.0, 0.0);

  // Create a triangle
  vtkNew<vtkTriangle> triangle;
  triangle->GetPointIds()->SetId(0, 0);
  triangle->GetPointIds()->SetId(1, 1);
  triangle->GetPointIds()->SetId(2, 2);

  // Add the triangle to a cell array
  vtkNew<vtkCellArray> triangles;
  triangles->InsertNextCell(triangle);

  // Setup data for the triangle. Attach a value of 1.45.
  // This can be anything you wish to store with it)
  vtkNew<vtkDoubleArray> triangleData;
  triangleData->SetNumberOfComponents(
      1); // we will have only 1 value associated with the triangle
  triangleData->SetName("TriangleData"); // set the name of the value
  triangleData->InsertNextValue(1.45);   // set the actual value

  // Create a polydata that contains the points,
  // the triangle on those points, and the data
  // array (value) we created for the triangle
  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);
  polydata->SetPolys(triangles);
  polydata->GetCellData()->AddArray(triangleData);

  // Write the file
  vtkNew<vtkXMLPolyDataWriter> writer;
  writer->SetInputData(polydata);
  writer->SetFileName(outputFilename.c_str());
  writer->Write();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(MiscCellData)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  vtkIOXML
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping MiscCellData: ${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(MiscCellData MACOSX_BUNDLE MiscCellData.cxx )
  target_link_libraries(MiscCellData PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(MiscCellData MACOSX_BUNDLE MiscCellData.cxx )
  target_link_libraries(MiscCellData PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS MiscCellData
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build MiscCellData

Click here to download MiscCellData and its CMakeLists.txt file. Once the tarball MiscCellData.tar has been downloaded and extracted,

cd MiscCellData/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:

./MiscCellData

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.