# PolyDataCellNormals

VTKExamples/Cxx/PolyData/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());

### Code

PolyDataCellNormals.cxx

    #include <vtkCellData.h>
    #include <vtkCellArray.h>
    #include <vtkDoubleArray.h>
    #include <vtkPoints.h>
    #include <vtkPolyData.h>
    #include <vtkPointData.h>
    #include <vtkSmartPointer.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
      vtkSmartPointer<vtkPoints> points =
          vtkSmartPointer<vtkPoints>::New();
      vtkSmartPointer<vtkCellArray> vertices =
          vtkSmartPointer<vtkCellArray>::New();

      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
      vtkSmartPointer<vtkPolyData> polydata =
          vtkSmartPointer<vtkPolyData>::New();
      polydata->SetPoints(points);
      polydata->SetVerts(vertices);

      //set vertex (cell) normals
      vtkSmartPointer<vtkDoubleArray> normalsArray =
          vtkSmartPointer<vtkDoubleArray>::New();
      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 =
          vtkDoubleArray::SafeDownCast(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.3 FATAL_ERROR)

    project(PolyDataCellNormals)

    find_package(VTK COMPONENTS
      vtkCommonCore
      vtkCommonDataModel QUIET)
    if (NOT VTK_FOUND)
      message("Skipping PolyDataCellNormals: ${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(PolyDataCellNormals MACOSX_BUNDLE PolyDataCellNormals.cxx )
      target_link_libraries(PolyDataCellNormals PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      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}
        )
    endif ()

### 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.