VTK/Examples/Cxx/Broken/VectorFieldNonZeroExtraction

From KitwarePublic

Jump to: navigation, search

VectorFieldNonZeroExtraction.cxx

#include <vtkSmartPointer.h>
#include <vtkPolyData.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkPointData.h>
#include <vtkImageData.h>
#include <vtkFloatArray.h>
#include <vtkThresholdPoints.h>
#include <vtkIntArray.h>
#include <vtkDataArray.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkImageMagnitude.h>
 
void CreateVectorField(vtkImageData* image);
 
int main(int argc, char **argv)
{ 
  // Create an image
  vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
  CreateVectorField(image);
 
  vtkSmartPointer<vtkImageMagnitude> magnitudeFilter = vtkSmartPointer<vtkImageMagnitude>::New();
  magnitudeFilter->SetInputConnection(image->GetProducerPort());
  magnitudeFilter->Update(); // This filter produces a vtkImageData with an array named "Magnitude"
 
  image->GetPointData()->AddArray(magnitudeFilter->GetOutput()->GetPointData()->GetScalars());
  image->GetPointData()->SetActiveScalars("Magnitude");
 
  vtkSmartPointer<vtkThresholdPoints> thresholdPoints = vtkSmartPointer<vtkThresholdPoints>::New();
  //thresholdPoints->SetInputConnection(magnitudeFilter->GetOutputPort());
  thresholdPoints->SetInputConnection(image->GetProducerPort());
  thresholdPoints->ThresholdByUpper(.05);
  thresholdPoints->Update();
 
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName("output.vtp");
  writer->SetInputConnection(thresholdPoints->GetOutputPort());
  writer->Write();
 
  return EXIT_SUCCESS;
}
 
void CreateVectorField(vtkImageData* image)
{
  // Specify the size of the image data
  image->SetDimensions(50,50,1);
  image->SetNumberOfScalarComponents(3);
  image->SetScalarTypeToFloat();
  image->AllocateScalars();
 
  int* dims = image->GetDimensions();
 
  // Zero the image
  for (int y = 0; y < dims[1]; y++)
    {
    for (int x = 0; x < dims[0]; x++)
      {
      float* pixel = static_cast<float*>(image->GetScalarPointer(x,y,0));
      pixel[0] = 0.0;
      pixel[1] = 0.0;
      pixel[2] = 0.0;
      }
    }
 
  // Set two of the pixels to non zero values
  float* pixel = static_cast<float*>(image->GetScalarPointer(20,20,0));
  pixel[0] = -10.0;
  pixel[1] = 5.0;
  pixel[2] = 0.0;
 
  pixel = static_cast<float*>(image->GetScalarPointer(30,30,0));
  pixel[0] = 10.0;
  pixel[1] = 10.0;
  pixel[2] = 0.0;
 
  image->GetPointData()->SetActiveVectors("ImageScalars");
 
  image->Modified();  
}


CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
PROJECT(VectorFieldNonZeroExtraction)
 
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
 
if (APPLE)
  add_executable(VectorFieldNonZeroExtraction MACOSX_BUNDLE VectorFieldNonZeroExtraction.cxx)
else()
  add_executable(VectorFieldNonZeroExtraction VectorFieldNonZeroExtraction.cxx)
endif()
 
if(VTK_LIBRARIES)
  target_link_libraries(VectorFieldNonZeroExtraction ${VTK_LIBRARIES})
else()
  target_link_libraries(VectorFieldNonZeroExtraction vtkHybrid )
endif()
Personal tools