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

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
Line 8: Line 8:
 
#include <vtkDoubleArray.h>
 
#include <vtkDoubleArray.h>
 
#include <vtkIntArray.h>
 
#include <vtkIntArray.h>
 +
 +
void ManualMethod(vtkPolyData* input);
 +
void AutomaticMethod(vtkPolyData* input);
  
 
int main(int, char *[])
 
int main(int, char *[])
Line 18: Line 21:
  
 
   vtkSmartPointer<vtkDoubleArray> doubles =  
 
   vtkSmartPointer<vtkDoubleArray> doubles =  
      vtkSmartPointer<vtkDoubleArray>::New();
+
    vtkSmartPointer<vtkDoubleArray>::New();
 
   doubles->SetName("Doubles");
 
   doubles->SetName("Doubles");
 
   doubles->SetNumberOfTuples(polydata->GetNumberOfPoints());
 
   doubles->SetNumberOfTuples(polydata->GetNumberOfPoints());
Line 25: Line 28:
 
    
 
    
 
   vtkSmartPointer<vtkIntArray> ints =  
 
   vtkSmartPointer<vtkIntArray> ints =  
      vtkSmartPointer<vtkIntArray>::New();
+
    vtkSmartPointer<vtkIntArray>::New();
 
   ints->SetName("Ints");
 
   ints->SetName("Ints");
 
   ints->SetNumberOfTuples(polydata->GetNumberOfPoints());
 
   ints->SetNumberOfTuples(polydata->GetNumberOfPoints());
 
    
 
    
 
   polydata->GetPointData()->AddArray(ints);
 
   polydata->GetPointData()->AddArray(ints);
 +
 +
  ManualMethod(polydata);
 +
  AutomaticMethod(polydata);
 +
 +
 
 +
  return EXIT_SUCCESS;
 +
}
 +
 +
void ManualMethod(vtkPolyData* input)
 +
{
 
    
 
    
   unsigned int numberOfArrays = polydata->GetPointData()->GetNumberOfArrays();
+
   unsigned int numberOfArrays = input->GetPointData()->GetNumberOfArrays();
   cout << "There are " << numberOfArrays << " arrays." << endl;
+
   std::cout << "There are " << numberOfArrays << " arrays." << std::endl;
 
    
 
    
 
   vtkSmartPointer<vtkPolyData> newPolyData =  
 
   vtkSmartPointer<vtkPolyData> newPolyData =  
Line 39: Line 52:
 
   for(unsigned int i = 0; i < numberOfArrays; i++)
 
   for(unsigned int i = 0; i < numberOfArrays; i++)
 
     {
 
     {
     cout << "array " << i << ":" << endl;
+
     std::cout << "array " << i << ":" << std::endl;
     cout << "name: " << polydata->GetPointData()->GetArrayName(i) << endl;
+
     std::cout << "name: " << input->GetPointData()->GetArrayName(i) << std::endl;
     cout << "type: " << polydata->GetPointData()->GetArray(i)->GetDataType() << endl;
+
     std::cout << "type: " << input->GetPointData()->GetArray(i)->GetDataType() << std::endl;
     cout << endl;
+
     std::cout << std::endl;
 
      
 
      
     newPolyData->GetPointData()->AddArray(polydata->GetPointData()->GetArray(i));
+
     newPolyData->GetPointData()->AddArray(input->GetPointData()->GetArray(i));
 
     }
 
     }
 
      
 
      
   cout << "new polydata: " << endl;
+
   std::cout << "new polydata: " << std::endl;
 
    
 
    
 
   for(unsigned int i = 0; i < numberOfArrays; i++)
 
   for(unsigned int i = 0; i < numberOfArrays; i++)
 
     {
 
     {
     cout << "array " << i << ":" << endl;
+
     std::cout << "array " << i << ":" << std::endl;
     cout << "name: " << newPolyData->GetPointData()->GetArrayName(i) << endl;
+
     std::cout << "name: " << newPolyData->GetPointData()->GetArrayName(i) << std::endl;
     cout << "type: " << newPolyData->GetPointData()->GetArray(i)->GetDataType() << endl;
+
     std::cout << "type: " << newPolyData->GetPointData()->GetArray(i)->GetDataType() << std::endl;
     cout << endl;
+
     std::cout << std::endl;
 
     }
 
     }
  
  return EXIT_SUCCESS;
 
 
}
 
}
  
 +
void AutomaticMethod(vtkPolyData* input)
 +
{
 +
  unsigned int numberOfArrays = input->GetPointData()->GetNumberOfArrays();
 +
  std::cout << "There are " << numberOfArrays << " arrays." << std::endl;
 +
 
 +
  vtkSmartPointer<vtkPolyData> newPolyData =
 +
      vtkSmartPointer<vtkPolyData>::New();
 +
 
 +
  newPolyData->GetPointData()->PassData(input->GetPointData());
 +
 
 +
  for(unsigned int i = 0; i < numberOfArrays; i++)
 +
    {
 +
    std::cout << "array " << i << ":" << std::endl;
 +
    std::cout << "name: " << input->GetPointData()->GetArrayName(i) << std::endl;
 +
    std::cout << "type: " << input->GetPointData()->GetArray(i)->GetDataType() << std::endl;
 +
    std::cout << std::endl;
 +
    }
 +
   
 +
  std::cout << "new polydata: " << std::endl;
 +
 
 +
  for(unsigned int i = 0; i < numberOfArrays; i++)
 +
    {
 +
    std::cout << "array " << i << ":" << std::endl;
 +
    std::cout << "name: " << newPolyData->GetPointData()->GetArrayName(i) << std::endl;
 +
    std::cout << "type: " << newPolyData->GetPointData()->GetArray(i)->GetDataType() << std::endl;
 +
    std::cout << std::endl;
 +
    }
 +
   
 +
}
 
</source>
 
</source>
  
Line 73: Line 114:
 
ADD_EXECUTABLE(CopyAllArrays CopyAllArrays.cxx)
 
ADD_EXECUTABLE(CopyAllArrays CopyAllArrays.cxx)
 
TARGET_LINK_LIBRARIES(CopyAllArrays vtkHybrid)
 
TARGET_LINK_LIBRARIES(CopyAllArrays vtkHybrid)
 +
</source>
  
 
+
[[Category:VTKNoVizRequired]]
</source>
 

Revision as of 13:18, 1 September 2010

CopyAllArrays.cxx

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyData.h>
#include <vtkPointData.h>
#include <vtkFloatArray.h>
#include <vtkDoubleArray.h>
#include <vtkIntArray.h>

void ManualMethod(vtkPolyData* input);
void AutomaticMethod(vtkPolyData* input);

int main(int, char *[])
{
  vtkSmartPointer<vtkSphereSource> sphereSource = 
      vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();
  
  vtkPolyData* polydata = sphereSource->GetOutput();

  vtkSmartPointer<vtkDoubleArray> doubles = 
    vtkSmartPointer<vtkDoubleArray>::New();
  doubles->SetName("Doubles");
  doubles->SetNumberOfTuples(polydata->GetNumberOfPoints());
  
  polydata->GetPointData()->AddArray(doubles);
  
  vtkSmartPointer<vtkIntArray> ints = 
    vtkSmartPointer<vtkIntArray>::New();
  ints->SetName("Ints");
  ints->SetNumberOfTuples(polydata->GetNumberOfPoints());
  
  polydata->GetPointData()->AddArray(ints);

  ManualMethod(polydata);
  AutomaticMethod(polydata);

  
  return EXIT_SUCCESS;
}

void ManualMethod(vtkPolyData* input)
{
  
  unsigned int numberOfArrays = input->GetPointData()->GetNumberOfArrays();
  std::cout << "There are " << numberOfArrays << " arrays." << std::endl;
  
  vtkSmartPointer<vtkPolyData> newPolyData = 
      vtkSmartPointer<vtkPolyData>::New();
  
  for(unsigned int i = 0; i < numberOfArrays; i++)
    {
    std::cout << "array " << i << ":" << std::endl;
    std::cout << "name: " << input->GetPointData()->GetArrayName(i) << std::endl;
    std::cout << "type: " << input->GetPointData()->GetArray(i)->GetDataType() << std::endl;
    std::cout << std::endl;
    
    newPolyData->GetPointData()->AddArray(input->GetPointData()->GetArray(i));
    }
    
  std::cout << "new polydata: " << std::endl;
  
  for(unsigned int i = 0; i < numberOfArrays; i++)
    {
    std::cout << "array " << i << ":" << std::endl;
    std::cout << "name: " << newPolyData->GetPointData()->GetArrayName(i) << std::endl;
    std::cout << "type: " << newPolyData->GetPointData()->GetArray(i)->GetDataType() << std::endl;
    std::cout << std::endl;
    }

}

void AutomaticMethod(vtkPolyData* input)
{
  unsigned int numberOfArrays = input->GetPointData()->GetNumberOfArrays();
  std::cout << "There are " << numberOfArrays << " arrays." << std::endl;
  
  vtkSmartPointer<vtkPolyData> newPolyData = 
      vtkSmartPointer<vtkPolyData>::New();
  
  newPolyData->GetPointData()->PassData(input->GetPointData()); 
  
  for(unsigned int i = 0; i < numberOfArrays; i++)
    {
    std::cout << "array " << i << ":" << std::endl;
    std::cout << "name: " << input->GetPointData()->GetArrayName(i) << std::endl;
    std::cout << "type: " << input->GetPointData()->GetArray(i)->GetDataType() << std::endl;
    std::cout << std::endl;
    }
    
  std::cout << "new polydata: " << std::endl;
  
  for(unsigned int i = 0; i < numberOfArrays; i++)
    {
    std::cout << "array " << i << ":" << std::endl;
    std::cout << "name: " << newPolyData->GetPointData()->GetArrayName(i) << std::endl;
    std::cout << "type: " << newPolyData->GetPointData()->GetArray(i)->GetDataType() << std::endl;
    std::cout << std::endl;
    }
     
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

PROJECT(CopyAllArrays)

FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})

ADD_EXECUTABLE(CopyAllArrays CopyAllArrays.cxx)
TARGET_LINK_LIBRARIES(CopyAllArrays vtkHybrid)