Difference between revisions of "VTK/Examples/Cxx/Developers/PolyDataAlgorithmReader"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
(vtkTestReader.cxx)
Line 128: Line 128:
 
</source>
 
</source>
  
==CMakeLists.txt==
+
{{VTKCMakeLists|{{SUBPAGENAME}}}}
<source lang="cmake">
 
cmake_minimum_required(VERSION 2.6)
 
PROJECT(vtkPolyDataAlgorithmDemo)
 
 
 
FIND_PACKAGE(VTK REQUIRED)
 
INCLUDE(${VTK_USE_FILE})
 
 
 
ADD_EXECUTABLE(ReaderExample ReaderExample.cxx vtkTestReader.cxx)
 
TARGET_LINK_LIBRARIES(ReaderExample vtkHybrid)
 
 
 
</source>
 

Revision as of 19:14, 9 December 2012

This example demonstrates a reader that takes nothing as input and produces a vtkPolyData as output.

ReaderExample.cxx

#include <vtkSmartPointer.h>
#include <vtkPolyData.h>

#include "vtkTestReader.h"

int main (int, char *[])
{
  vtkSmartPointer<vtkTestReader> reader =
    vtkSmartPointer<vtkTestReader>::New();
  reader->Update();
  
  vtkPolyData* polydata = reader->GetOutput();
  polydata->Print(std::cout);

  return EXIT_SUCCESS;
}

vtkTestReader.h

#ifndef __vtkTestReader_h
#define __vtkTestReader_h

#include "vtkPolyDataAlgorithm.h"

class vtkTestReader : public vtkPolyDataAlgorithm 
{
public:
  vtkTypeMacro(vtkTestReader,vtkPolyDataAlgorithm);
  void PrintSelf(ostream& os, vtkIndent indent);

  static vtkTestReader *New();
	
protected:
  vtkTestReader();
  ~vtkTestReader();
  
  int FillOutputPortInformation( int port, vtkInformation* info );
  int RequestData(vtkInformation *, vtkInformationVector **, vtkInformationVector *);

private:
  vtkTestReader(const vtkTestReader&);  // Not implemented.
  void operator=(const vtkTestReader&);  // Not implemented.

  char* FileName;
};

#endif

vtkTestReader.cxx

#include "vtkTestReader.h"

#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkDataObject.h"
#include "vtkSmartPointer.h"

vtkStandardNewMacro(vtkTestReader);

vtkTestReader::vtkTestReader()
{
  this->FileName = NULL;
  this->SetNumberOfInputPorts(0);
  this->SetNumberOfOutputPorts(1);
}

vtkTestReader::~vtkTestReader()
{

}

int vtkTestReader::FillOutputPortInformation( int port, vtkInformation* info )
{
  if ( port == 0 )
  {
    info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPolyData" );
    
    return 1;
  }

  return 0;
}

int vtkTestReader::RequestData(
  vtkInformation *vtkNotUsed(request),
  vtkInformationVector **vtkNotUsed(inputVector),
  vtkInformationVector *outputVector)
{
  
  // get the info object
  vtkInformation *outInfo = outputVector->GetInformationObject(0);
  
  // get the ouptut
   vtkPolyData *output = vtkPolyData::SafeDownCast(
            outInfo->Get(vtkDataObject::DATA_OBJECT()));
  
  vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint(0.0, 0.0, 0.0);
  polydata->SetPoints(points);
  
  //output = polydata; //doesn't work
  output->ShallowCopy(polydata);
    
  return 1;
}


//----------------------------------------------------------------------------
void vtkTestReader::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os,indent);

  os << indent << "File Name: " 
      << (this->FileName ? this->FileName : "(none)") << "\n";	
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(PolyDataAlgorithmReader)

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

add_executable(PolyDataAlgorithmReader MACOSX_BUNDLE PolyDataAlgorithmReader.cxx)

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

Download and Build PolyDataAlgorithmReader

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

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

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

./PolyDataAlgorithmReader

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.