VTK/Examples/Broken/PCAAnalysisFilter

From KitwarePublic
< VTK‎ | Examples
Jump to navigationJump to search

The eigenvalues vector doesn't seem to be valid. It seems like the eigenvector corresponding to the smallest value is stored as the normal of the cell in the output - how do you get the other two eigen vectors?

PCAAnalysisFilter.cxx

#include <vtkSmartPointer.h>
#include <vtkFloatArray.h>
#include <vtkPolyData.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkPlaneSource.h>
#include <vtkPCAAnalysisFilter.h>

int main(int argc, char *argv[])
{
  //create a set of points
  vtkSmartPointer<vtkPlaneSource> planeSource = 
      vtkSmartPointer<vtkPlaneSource>::New();
  planeSource->SetNormal(1,2,3);
  planeSource->Update();
  
  {
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = 
    vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetInputConnection(planeSource->GetOutputPort());
  writer->SetFileName("plane.vtp");
  writer->Write();
  }
  vtkSmartPointer<vtkPCAAnalysisFilter> pcaAnalysis = 
      vtkSmartPointer<vtkPCAAnalysisFilter>::New();
  pcaAnalysis->SetNumberOfInputs(1);
  pcaAnalysis->SetInput(0, planeSource->GetOutput());
  pcaAnalysis->Update();
        
  vtkFloatArray* evals = pcaAnalysis->GetEvals();
  
  cout << "There are " << evals->GetNumberOfComponents() << " components." << endl;
  cout << "There are " << evals->GetNumberOfTuples() << " evals." << endl;
  
  for(vtkIdType i = 0; i < evals->GetNumberOfTuples(); i++)
    {
    float eval[1];
    evals->GetTupleValue(i, eval);
    cout << "Eval " << i << " : " << eval[0] << endl;
    }
    
  vtkSmartPointer<vtkPolyData> output = vtkPolyData::SafeDownCast(pcaAnalysis->GetOutput());
  
  //get the normals to get the evecs
  
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = 
      vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetInput(output);
  writer->SetFileName("output.vtp");
  writer->Write();
  
  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(PCAAnalysisFilter)

find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

add_executable(PCAAnalysisFilter MACOSX_BUNDLE PCAAnalysisFilter.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(PCAAnalysisFilter ${VTK_LIBRARIES})
else()
  target_link_libraries(PCAAnalysisFilter vtkHybrid vtkWidgets)
endif()

Download and Build PCAAnalysisFilter

Click here to download PCAAnalysisFilter. and its CMakeLists.txt file.

Once the tarball PCAAnalysisFilter.tar has been downloaded and extracted,

cd PCAAnalysisFilter/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:

./PCAAnalysisFilter

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.