Skip to content

PolyDataCellNormals

Repository source: PolyDataCellNormals

Description

This example shows how to add normals to vertices. This is identical to adding normals to any type of cells. The only difference from [Normals to/from a Polydata]]([Add/Get) is

polydata->GetPointData()->SetNormals(PointNormalsArray);

vtkSmartPointer<vtkDoubleArray> PointNormalsRetrieved = vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetNormals());

are changed to

polydata->GetCellData()->SetNormals(CellNormalsArray);

and

vtkSmartPointer<vtkDoubleArray> CellNormalsRetrieved = vtkDoubleArray::SafeDownCast(polydata->GetCellData()->GetNormals());

Question

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

Code

PolyDataCellNormals.cxx

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

int main(int, char*[])
{

  ///////// Set Point Normals ///////////

  // Setup point coordinates
  double x[3] = {1.0, 0.0, 0.0};
  double y[3] = {0.0, 0.0, 1.0};
  double z[3] = {0.0, 0.0, 0.0};

  // create 3 points and add a vertex at each point
  vtkNew<vtkPoints> points;
  vtkNew<vtkCellArray> vertices;

  for (unsigned int i = 0; i < 3; ++i)
  {
    // Declare a variable to store the index of the point that gets added. This
    // behaves just like an unsigned int.
    vtkIdType pid[1];

    // Add a point to the polydata and save its index, which we will use to
    // create the vertex on that point.
    pid[0] = points->InsertNextPoint(x[i], y[i], z[i]);

    // create a vertex cell on the point that was just added.
    vertices->InsertNextCell(1, pid);
  }

  // add the points and vertices to a polydata
  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);
  polydata->SetVerts(vertices);

  // set vertex (cell) normals
  vtkNew<vtkDoubleArray> normalsArray;
  normalsArray->SetNumberOfComponents(3); // 3d normals (ie x,y,z)
  normalsArray->SetNumberOfTuples(polydata->GetNumberOfPoints());

  // construct the normal vectors
  double cN1[3] = {1.0, 0.0, 0.0};
  double cN2[3] = {0.0, 1.0, 0.0};
  double cN3[3] = {0.0, 0.0, 1.0};

  // add the data to the normals array
  normalsArray->SetTuple(0, cN1);
  normalsArray->SetTuple(1, cN2);
  normalsArray->SetTuple(2, cN3);

  // add the normals to the cells in the polydata
  polydata->GetCellData()->SetNormals(normalsArray);

  ///////// Get cell normals ///////////
  // vtkSmartPointer<vtkDoubleArray> cellNormalsRetrieved =
  //    dynamic_cast<vtkDoubleArray*>(polydata->GetCellData()->GetNormals());
  auto cellNormalsRetrieved =
      dynamic_cast<vtkDoubleArray*>(polydata->GetCellData()->GetNormals());
  if (cellNormalsRetrieved)
  {
    cout << "There are " << cellNormalsRetrieved->GetNumberOfTuples()
         << " cell normals." << endl;

    for (vtkIdType i = 0; i < cellNormalsRetrieved->GetNumberOfTuples(); i++)
    {
      double cN[3];
      cellNormalsRetrieved->GetTuple(i, cN);
      cout << "Cell normal " << i << ": " << cN[0] << " " << cN[1] << " "
           << cN[2] << endl;
    }
  }
  else
  {
    cout << "No cell normals." << endl;
  }

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(PolyDataCellNormals)

find_package(VTK COMPONENTS 
  CommonCore
  CommonDataModel
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "PolyDataCellNormals: 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(PolyDataCellNormals MACOSX_BUNDLE PolyDataCellNormals.cxx )
  target_link_libraries(PolyDataCellNormals PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS PolyDataCellNormals
  MODULES ${VTK_LIBRARIES}
)

Download and Build PolyDataCellNormals

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

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

./PolyDataCellNormals

WINDOWS USERS

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