Difference between revisions of "VTK/Examples/Cxx/Filtering/VectorFieldNonZeroExtraction"
From KitwarePublic
Jump to navigationJump to searchDaviddoria (talk | contribs) (Rewrite for VTK 6. It was marked as broken, but it seems to work.) |
|||
Line 12: | Line 12: | ||
#include <vtkDataArray.h> | #include <vtkDataArray.h> | ||
#include <vtkXMLPolyDataWriter.h> | #include <vtkXMLPolyDataWriter.h> | ||
+ | #include <vtkXMLImageDataWriter.h> | ||
#include <vtkXMLUnstructuredGridWriter.h> | #include <vtkXMLUnstructuredGridWriter.h> | ||
#include <vtkXMLPolyDataWriter.h> | #include <vtkXMLPolyDataWriter.h> | ||
#include <vtkImageMagnitude.h> | #include <vtkImageMagnitude.h> | ||
+ | #include <vtkVersion.h> | ||
− | void CreateVectorField(vtkImageData* image); | + | static void CreateVectorField(vtkImageData* image); |
+ | static void WriteImage(vtkImageData* image, const std::string fileName); | ||
+ | static void WriteVectorField(vtkPolyData* vectorField, const std::string fileName); | ||
− | int main(int | + | int main(int , char **) |
{ | { | ||
// Create an image | // Create an image | ||
Line 25: | Line 29: | ||
vtkSmartPointer<vtkImageMagnitude> magnitudeFilter = vtkSmartPointer<vtkImageMagnitude>::New(); | vtkSmartPointer<vtkImageMagnitude> magnitudeFilter = vtkSmartPointer<vtkImageMagnitude>::New(); | ||
+ | |||
+ | #if VTK_MAJOR_VERSION <= 5 | ||
magnitudeFilter->SetInputConnection(image->GetProducerPort()); | magnitudeFilter->SetInputConnection(image->GetProducerPort()); | ||
+ | #else | ||
+ | magnitudeFilter->SetInputData(image); | ||
+ | #endif | ||
magnitudeFilter->Update(); // This filter produces a vtkImageData with an array named "Magnitude" | magnitudeFilter->Update(); // This filter produces a vtkImageData with an array named "Magnitude" | ||
Line 33: | Line 42: | ||
vtkSmartPointer<vtkThresholdPoints> thresholdPoints = vtkSmartPointer<vtkThresholdPoints>::New(); | vtkSmartPointer<vtkThresholdPoints> thresholdPoints = vtkSmartPointer<vtkThresholdPoints>::New(); | ||
//thresholdPoints->SetInputConnection(magnitudeFilter->GetOutputPort()); | //thresholdPoints->SetInputConnection(magnitudeFilter->GetOutputPort()); | ||
+ | #if VTK_MAJOR_VERSION <= 5 | ||
thresholdPoints->SetInputConnection(image->GetProducerPort()); | thresholdPoints->SetInputConnection(image->GetProducerPort()); | ||
+ | #else | ||
+ | thresholdPoints->SetInputData(image); | ||
+ | #endif | ||
+ | |||
thresholdPoints->ThresholdByUpper(.05); | thresholdPoints->ThresholdByUpper(.05); | ||
thresholdPoints->Update(); | thresholdPoints->Update(); | ||
− | + | WriteImage(image, "input.vti"); | |
− | + | WriteVectorField(thresholdPoints->GetOutput(), "output.vtp"); | |
− | + | ||
− | |||
− | |||
return EXIT_SUCCESS; | return EXIT_SUCCESS; | ||
} | } | ||
Line 49: | Line 61: | ||
// Specify the size of the image data | // Specify the size of the image data | ||
image->SetDimensions(50,50,1); | image->SetDimensions(50,50,1); | ||
+ | #if VTK_MAJOR_VERSION <= 5 | ||
image->SetNumberOfScalarComponents(3); | image->SetNumberOfScalarComponents(3); | ||
image->SetScalarTypeToFloat(); | image->SetScalarTypeToFloat(); | ||
image->AllocateScalars(); | image->AllocateScalars(); | ||
+ | #else | ||
+ | image->AllocateScalars(VTK_FLOAT, 3); | ||
+ | #endif | ||
int* dims = image->GetDimensions(); | int* dims = image->GetDimensions(); | ||
Line 81: | Line 97: | ||
image->Modified(); | image->Modified(); | ||
+ | } | ||
+ | |||
+ | static void WriteVectorField(vtkPolyData* vectorField, const std::string fileName) | ||
+ | { | ||
+ | vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New(); | ||
+ | writer->SetFileName(fileName.c_str()); | ||
+ | #if VTK_MAJOR_VERSION <= 5 | ||
+ | writer->SetInput(vectorField); | ||
+ | #else | ||
+ | writer->SetInputData(vectorField); | ||
+ | #endif | ||
+ | writer->Write(); | ||
+ | } | ||
+ | |||
+ | static void WriteImage(vtkImageData* image, const std::string fileName) | ||
+ | { | ||
+ | vtkSmartPointer<vtkXMLImageDataWriter> writer = vtkSmartPointer<vtkXMLImageDataWriter>::New(); | ||
+ | writer->SetFileName(fileName.c_str()); | ||
+ | #if VTK_MAJOR_VERSION <= 5 | ||
+ | writer->SetInput(image); | ||
+ | #else | ||
+ | writer->SetInputData(image); | ||
+ | #endif | ||
+ | writer->Write(); | ||
} | } | ||
Revision as of 11:39, 30 December 2013
Contents
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 <vtkXMLImageDataWriter.h>
#include <vtkXMLUnstructuredGridWriter.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkImageMagnitude.h>
#include <vtkVersion.h>
static void CreateVectorField(vtkImageData* image);
static void WriteImage(vtkImageData* image, const std::string fileName);
static void WriteVectorField(vtkPolyData* vectorField, const std::string fileName);
int main(int , char **)
{
// Create an image
vtkSmartPointer<vtkImageData> image = vtkSmartPointer<vtkImageData>::New();
CreateVectorField(image);
vtkSmartPointer<vtkImageMagnitude> magnitudeFilter = vtkSmartPointer<vtkImageMagnitude>::New();
#if VTK_MAJOR_VERSION <= 5
magnitudeFilter->SetInputConnection(image->GetProducerPort());
#else
magnitudeFilter->SetInputData(image);
#endif
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());
#if VTK_MAJOR_VERSION <= 5
thresholdPoints->SetInputConnection(image->GetProducerPort());
#else
thresholdPoints->SetInputData(image);
#endif
thresholdPoints->ThresholdByUpper(.05);
thresholdPoints->Update();
WriteImage(image, "input.vti");
WriteVectorField(thresholdPoints->GetOutput(), "output.vtp");
return EXIT_SUCCESS;
}
void CreateVectorField(vtkImageData* image)
{
// Specify the size of the image data
image->SetDimensions(50,50,1);
#if VTK_MAJOR_VERSION <= 5
image->SetNumberOfScalarComponents(3);
image->SetScalarTypeToFloat();
image->AllocateScalars();
#else
image->AllocateScalars(VTK_FLOAT, 3);
#endif
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();
}
static void WriteVectorField(vtkPolyData* vectorField, const std::string fileName)
{
vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
writer->SetFileName(fileName.c_str());
#if VTK_MAJOR_VERSION <= 5
writer->SetInput(vectorField);
#else
writer->SetInputData(vectorField);
#endif
writer->Write();
}
static void WriteImage(vtkImageData* image, const std::string fileName)
{
vtkSmartPointer<vtkXMLImageDataWriter> writer = vtkSmartPointer<vtkXMLImageDataWriter>::New();
writer->SetFileName(fileName.c_str());
#if VTK_MAJOR_VERSION <= 5
writer->SetInput(image);
#else
writer->SetInputData(image);
#endif
writer->Write();
}
Please try the new VTKExamples website.
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
PROJECT(VectorFieldNonZeroExtraction)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(VectorFieldNonZeroExtraction MACOSX_BUNDLE VectorFieldNonZeroExtraction.cxx)
if(VTK_LIBRARIES)
target_link_libraries(VectorFieldNonZeroExtraction ${VTK_LIBRARIES})
else()
target_link_libraries(VectorFieldNonZeroExtraction vtkHybrid vtkWidgets)
endif()
Download and Build VectorFieldNonZeroExtraction
Click here to download VectorFieldNonZeroExtraction. and its CMakeLists.txt file.
Once the tarball VectorFieldNonZeroExtraction.tar has been downloaded and extracted,
cd VectorFieldNonZeroExtraction/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:
./VectorFieldNonZeroExtraction
WINDOWS USERS PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.