VTK/Examples/Cxx/PolyData/PolyDataExtractNormals

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Revision as of 18:10, 12 December 2009 by Daviddoria (Talk | contribs)

Jump to: navigation, search

This demo looks for multiple types of normals in a VTP file and generates them if they do not exist.

Fix: Add CMakeLists.txt

#include <vtkCellData.h>
#include <vtkDoubleArray.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkSmartPointer.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkPolyDataNormals.h>
 
bool ReadNormals(vtkPolyData* polydata);
 
int main(int argc, char *argv[])
{
  //Read File
  vtkstd::string filename = argv[1]; //first command line argument
 
  vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
  vtkstd::cout << "Reading " << filename << vtkstd::endl;
  reader->SetFileName(filename.c_str());
  reader->Update();
  vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();
 
  // Try to read normals directly
  bool hasNormals = ReadNormals(polydata);
 
  if(hasNormals)
    {
    return 0;
    }
 
  // generate normals
  vtkSmartPointer<vtkPolyDataNormals> normalGenerator = vtkSmartPointer<vtkPolyDataNormals>::New();
 
  normalGenerator->SetInput(polydata);
  normalGenerator->Update();
  polydata = normalGenerator->GetOutput();
 
  /*
  //optional settings
  normalGenerator->SetFeatureAngle(0.1);
  normalGenerator->SetSplitting(1);
  normalGenerator->SetConsistency(0);
  normalGenerator->SetAutoOrientNormals(0);
  normalGenerator->SetComputePointNormals(1);
  normalGenerator->SetComputeCellNormals(0);
  normalGenerator->SetFlipNormals(0);
  normalGenerator->SetNonManifoldTraversal(1);
  */
 
  // Try to read normals again
  ReadNormals(polydata);
 
  return 0;
}
 
bool ReadNormals(vtkPolyData* polydata)
{
  vtkstd::cout << "Reading normals..." << vtkstd::endl;
 
  //count points
  vtkIdType numberOfPoints = polydata->GetNumberOfPoints();
 
  //count triangles
  vtkIdType numberOfPolys = polydata->GetNumberOfPolys();
 
  ////////////////////////////////////////////////////////////////
  //normals in an array
  vtkSmartPointer<vtkDoubleArray> normalData1 = 
    vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetArray("Normals"));
 
  if(normalData1)
    { 
    int nc = NormalData1->GetNumberOfTuples();
    vtkstd::cout << "There are " << nc << " components in NormalData1 (double Point \"Normals\")" << vtkstd::endl;
    return true;
    }
 
  ////////////////////////////////////////////////////////////////
  //double point normals
  vtkSmartPointer<vtkDoubleArray> normalData2 = 
    vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetNormals());
 
  if(normalData2)
    { 
    vtkstd::cout << "There are " << normalData2->GetNumberOfComponents() << " components in NormalData2 (double Point Normals)" << vtkstd::endl;
    return true;
    }
 
 
  /////////////////////////////////////////////////////////////////////
  //double generic (double) point normals
  vtkSmartPointer<vtkDataArray> normalData3 = polydata->GetPointData()->GetNormals(); //works
  if(normalData3)
    { 
    vtkstd::cout << "There are " << NormalData3->GetNumberOfTuples() << " normals in NormalData3 (float Point Normals)" << vtkstd::endl;
 
    //works
    double testDouble[3];
    normalData3->GetTuple(0, testDouble);
    vtkstd::cout << "Double: " << testDouble[0] << " " << testDouble[1] << " " << testDouble[2] << vtkstd::endl;
 
    return true;
    }
 
  ///////////////////////////////////////////////////////////////////////////////////
  //double cell normals
  vtkSmartPointer<vtkDoubleArray> normalData4 = 
    vtkDoubleArray::SafeDownCast(polydata->GetCellData()->GetNormals("cellNormals"));
 
  if(normalData4)
    { 
    vtkstd::cout << "There are " << normalData4->GetNumberOfComponents() << " components in NormalData4 (double Cell \"Cell Normals\")" << vtkstd::endl;
    return true;
    }
 
  //if the function has not yet quit, there were none of these types of normals
  vtkstd::cout << "Normals not found!" << vtkstd::endl;
  return false;
 
}

CMakeLists.txt