MiscCellData
Repository source: 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 output.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.12 FATAL_ERROR)
project(MiscCellData)
find_package(VTK COMPONENTS
CommonCore
CommonDataModel
IOXML
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "MiscCellData: 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(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}
)
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.