Difference between revisions of "VTK/Examples/Cxx/PolyData/PolyDataExtractNormals"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
Line 1: Line 1:
 
This demo looks for multiple types of normals in a VTP file and generates them if they do not exist.
 
This demo looks for multiple types of normals in a VTP file and generates them if they do not exist.
 
Fix: Add CMakeLists.txt
 
  
 
<source lang="cpp">
 
<source lang="cpp">
Line 17: Line 15:
 
int main(int argc, char *argv[])
 
int main(int argc, char *argv[])
 
{
 
{
  //Read File
+
//Read File
  vtkstd::string filename = argv[1]; //first command line argument
+
vtkstd::string filename = argv[1]; //first command line argument
 
 
  vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
+
vtkSmartPointer<vtkXMLPolyDataReader> reader =  
  vtkstd::cout << "Reading " << filename << vtkstd::endl;
+
      vtkSmartPointer<vtkXMLPolyDataReader>::New();
  reader->SetFileName(filename.c_str());
+
cout << "Reading " << filename << endl;
  reader->Update();
+
reader->SetFileName(filename.c_str());
  vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();
+
reader->Update();
 +
vtkPolyData* polydata = reader->GetOutput();
 
 
  // Try to read normals directly
+
// Try to read normals directly
  bool hasNormals = ReadNormals(polydata);
+
bool HasNormals = ReadNormals(polydata);
 
+
  if(hasNormals)
+
if(HasNormals)
 
     {
 
     {
    return 0;
+
return EXIT_FAILURE;
 
     }
 
     }
 
 
  // generate normals
+
// generate normals
  vtkSmartPointer<vtkPolyDataNormals> normalGenerator = vtkSmartPointer<vtkPolyDataNormals>::New();
+
vtkSmartPointer<vtkPolyDataNormals> normalGenerator =  
 +
      vtkSmartPointer<vtkPolyDataNormals>::New();
  
  normalGenerator->SetInput(polydata);
+
normalGenerator->SetInput(polydata);
  normalGenerator->Update();
+
normalGenerator->Update();
  polydata = normalGenerator->GetOutput();
+
polydata = normalGenerator->GetOutput();
 
 
  /*
+
/*
  //optional settings
+
//optional settings
  normalGenerator->SetFeatureAngle(0.1);
+
normalGenerator->SetFeatureAngle(0.1);
  normalGenerator->SetSplitting(1);
+
normalGenerator->SetSplitting(1);
  normalGenerator->SetConsistency(0);
+
normalGenerator->SetConsistency(0);
  normalGenerator->SetAutoOrientNormals(0);
+
normalGenerator->SetAutoOrientNormals(0);
  normalGenerator->SetComputePointNormals(1);
+
normalGenerator->SetComputePointNormals(1);
  normalGenerator->SetComputeCellNormals(0);
+
normalGenerator->SetComputeCellNormals(0);
  normalGenerator->SetFlipNormals(0);
+
normalGenerator->SetFlipNormals(0);
  normalGenerator->SetNonManifoldTraversal(1);
+
normalGenerator->SetNonManifoldTraversal(1);
  */
+
*/
 
 
  // Try to read normals again
+
// Try to read normals again
  ReadNormals(polydata);
+
ReadNormals(polydata);
 
 
  return 0;
+
return EXIT_SUCCESS;
 
}
 
}
  
 
bool ReadNormals(vtkPolyData* polydata)
 
bool ReadNormals(vtkPolyData* polydata)
 
{
 
{
  vtkstd::cout << "Reading normals..." << vtkstd::endl;
+
cout << "Reading normals..." << endl;
 
 
  //count points
+
//count points
  vtkIdType numberOfPoints = polydata->GetNumberOfPoints();
+
vtkIdType NumPoints = polydata->GetNumberOfPoints();
 
 
  //count triangles
+
//count triangles
  vtkIdType numberOfPolys = polydata->GetNumberOfPolys();
+
vtkIdType NumPolys = polydata->GetNumberOfPolys();
 
 
  ////////////////////////////////////////////////////////////////
+
////////////////////////////////////////////////////////////////
  //normals in an array
+
//normals in an array
  vtkSmartPointer<vtkDoubleArray> normalData1 =  
+
vtkSmartPointer<vtkDoubleArray> NormalData1 = vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetArray("Normals"));
    vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetArray("Normals"));
+
  
  if(normalData1)
+
if(NormalData1)
    {  
+
  {  
    int nc = NormalData1->GetNumberOfTuples();
+
int nc = NormalData1->GetNumberOfTuples();
    vtkstd::cout << "There are " << nc << " components in NormalData1 (double Point \"Normals\")" << vtkstd::endl;
+
cout << "There are " << nc << " components in NormalData1 (double Point \"Normals\")" << endl;
    return true;
+
return true;
    }
+
  }
 
 
  ////////////////////////////////////////////////////////////////
+
////////////////////////////////////////////////////////////////
  //double point normals
+
//double point normals
  vtkSmartPointer<vtkDoubleArray> normalData2 =  
+
vtkSmartPointer<vtkDoubleArray> NormalData2 = vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetNormals());
    vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetNormals());
+
 
 
  if(normalData2)
+
if(NormalData2)
    {  
+
  {  
    vtkstd::cout << "There are " << normalData2->GetNumberOfComponents() << " components in NormalData2 (double Point Normals)" << vtkstd::endl;
+
cout << "There are " << NormalData2->GetNumberOfComponents() << " components in NormalData2 (double Point Normals)" << endl;
    return true;
+
return true;
    }
+
  }
 
 
 
 
  /////////////////////////////////////////////////////////////////////
+
/////////////////////////////////////////////////////////////////////
  //double generic (double) point normals
+
//double generic (double) point normals
  vtkSmartPointer<vtkDataArray> normalData3 = polydata->GetPointData()->GetNormals(); //works
+
vtkSmartPointer<vtkDataArray> NormalData3 = polydata->GetPointData()->GetNormals(); //works
  if(normalData3)
+
if(NormalData3)
    {  
+
  {  
    vtkstd::cout << "There are " << NormalData3->GetNumberOfTuples() << " normals in NormalData3 (float Point Normals)" << vtkstd::endl;
+
cout << "There are " << NormalData3->GetNumberOfTuples() << " normals in NormalData3 (float Point Normals)" << endl;
 +
 +
//works
 +
double TestDouble[3];
 +
NormalData3->GetTuple(0, TestDouble);
  
    //works
+
cout << "Double: " << TestDouble[0] << " " << TestDouble[1] << " " << TestDouble[2] << endl;
    double testDouble[3];
+
    normalData3->GetTuple(0, testDouble);
+
    vtkstd::cout << "Double: " << testDouble[0] << " " << testDouble[1] << " " << testDouble[2] << vtkstd::endl;
+
 
 
    return true;
+
return true;
    }
+
  }
  
  ///////////////////////////////////////////////////////////////////////////////////
+
///////////////////////////////////////////////////////////////////////////////////
  //double cell normals
+
//double cell normals
  vtkSmartPointer<vtkDoubleArray> normalData4 =  
+
vtkSmartPointer<vtkDoubleArray> NormalData4 = vtkDoubleArray::SafeDownCast(polydata->GetCellData()->GetNormals("cellNormals"));
    vtkDoubleArray::SafeDownCast(polydata->GetCellData()->GetNormals("cellNormals"));
+
 
 
  if(normalData4)
+
if(NormalData4)
    {  
+
  {  
    vtkstd::cout << "There are " << normalData4->GetNumberOfComponents() << " components in NormalData4 (double Cell \"Cell Normals\")" << vtkstd::endl;
+
cout << "There are " << NormalData4->GetNumberOfComponents() << " components in NormalData4 (double Cell \"Cell Normals\")" << endl;
    return true;
+
return true;
    }
+
  }
 
 
  //if the function has not yet quit, there were none of these types of normals
+
//if the function has not yet quit, there were none of these types of normals
  vtkstd::cout << "Normals not found!" << vtkstd::endl;
+
cout << "Normals not found!" << endl;
  return false;
+
return false;
 
 
 
}
 
}
 +
 +
 
</source>
 
</source>
  
 
==CMakeLists.txt==
 
==CMakeLists.txt==
<source lang="text">
+
<source lang="cmake">
 +
cmake_minimum_required(VERSION 2.6)
 +
PROJECT(ReadNormals)
 +
 
 +
FIND_PACKAGE(VTK REQUIRED)
 +
INCLUDE(${VTK_USE_FILE})
 +
 
 +
ADD_EXECUTABLE(ReadNormals ReadNormals.cpp)
 +
TARGET_LINK_LIBRARIES(ReadNormals  vtkHybrid)
 +
 
 +
 
 
</source>
 
</source>

Revision as of 12:19, 1 February 2010

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

#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();
	cout << "Reading " << filename << endl;
	reader->SetFileName(filename.c_str());
	reader->Update();
	vtkPolyData* polydata = reader->GetOutput();
 
	// Try to read normals directly
	bool HasNormals = ReadNormals(polydata);
 
	if(HasNormals)
    {
		return EXIT_FAILURE;
    }
 
	// 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 EXIT_SUCCESS;
}
 
bool ReadNormals(vtkPolyData* polydata)
{
	cout << "Reading normals..." << endl;
 
	//count points
	vtkIdType NumPoints = polydata->GetNumberOfPoints();
 
	//count triangles
	vtkIdType NumPolys = polydata->GetNumberOfPolys();
 
	////////////////////////////////////////////////////////////////
	//normals in an array
	vtkSmartPointer<vtkDoubleArray> NormalData1 = vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetArray("Normals"));
 
	if(NormalData1)
	  { 
		int nc = NormalData1->GetNumberOfTuples();
		cout << "There are " << nc << " components in NormalData1 (double Point \"Normals\")" << endl;
		return true;
	  }
 
	////////////////////////////////////////////////////////////////
	//double point normals
	vtkSmartPointer<vtkDoubleArray> NormalData2 = vtkDoubleArray::SafeDownCast(polydata->GetPointData()->GetNormals());
 
	if(NormalData2)
	  { 
		cout << "There are " << NormalData2->GetNumberOfComponents() << " components in NormalData2 (double Point Normals)" << endl;
		return true;
	  }
 
 
	/////////////////////////////////////////////////////////////////////
	//double generic (double) point normals
	vtkSmartPointer<vtkDataArray> NormalData3 = polydata->GetPointData()->GetNormals(); //works
	if(NormalData3)
	  { 
		cout << "There are " << NormalData3->GetNumberOfTuples() << " normals in NormalData3 (float Point Normals)" << endl;
 
		//works
		double TestDouble[3];
		NormalData3->GetTuple(0, TestDouble);
 
		cout << "Double: " << TestDouble[0] << " " << TestDouble[1] << " " << TestDouble[2] << endl;
 
		return true;
	  }
 
	///////////////////////////////////////////////////////////////////////////////////
	//double cell normals
	vtkSmartPointer<vtkDoubleArray> NormalData4 = vtkDoubleArray::SafeDownCast(polydata->GetCellData()->GetNormals("cellNormals"));
 
	if(NormalData4)
	  { 
		cout << "There are " << NormalData4->GetNumberOfComponents() << " components in NormalData4 (double Cell \"Cell Normals\")" << endl;
		return true;
	  }
 
	//if the function has not yet quit, there were none of these types of normals
	cout << "Normals not found!" << endl;
	return false;
 
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
PROJECT(ReadNormals)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
ADD_EXECUTABLE(ReadNormals ReadNormals.cpp)
TARGET_LINK_LIBRARIES(ReadNormals  vtkHybrid)