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

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search
(Created page with '== Decimate one Polyline == This example shows how to decimate one polyline. Note that this example requires the git (master branch) version of vtk ==DecimatePolyline.cxx== <so…')
 
Line 16: Line 16:
 
#include <vtkRenderWindowInteractor.h>
 
#include <vtkRenderWindowInteractor.h>
 
#include <vtkProperty.h>
 
#include <vtkProperty.h>
 +
 +
#include <vtkSmartPointer.h>
  
 
int main(int, char *[])
 
int main(int, char *[])
Line 22: Line 24:
  
 
   vtkPolyData* circle = vtkPolyData::New();
 
   vtkPolyData* circle = vtkPolyData::New();
   vtkPointspoints = vtkPoints::New();
+
   vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
   vtkCellArray* lines = vtkCellArray::New();
+
   vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
 
   vtkIdType* lineIndices = new vtkIdType[numberofpoints+1];
 
   vtkIdType* lineIndices = new vtkIdType[numberofpoints+1];
  
Line 42: Line 44:
 
   circle->SetPoints( points );
 
   circle->SetPoints( points );
 
   circle->SetLines( lines );
 
   circle->SetLines( lines );
  points->Delete();
 
  lines->Delete();
 
  
   vtkPolyDataMapper* c_mapper = vtkPolyDataMapper::New();
+
   vtkSmartPointer<vtkPolyDataMapper> c_mapper =  
 +
    vtkSmartPointer<vtkPolyDataMapper>::New();
 
   c_mapper->SetInput( circle );
 
   c_mapper->SetInput( circle );
  
   vtkActor* c_actor = vtkActor::New();
+
   vtkSmartPointer<vtkActor> c_actor =  
 +
    vtkSmartPointer<vtkActor>::New();
 
   c_actor->SetMapper( c_mapper );
 
   c_actor->SetMapper( c_mapper );
  
   vtkDecimatePolylineFilter* decimate = vtkDecimatePolylineFilter::New();
+
   vtkSmartPointer<vtkDecimatePolylineFilter> decimate =  
 +
    vtkSmartPointer<vtkDecimatePolylineFilter>::New();
 
   decimate->SetInput( circle );
 
   decimate->SetInput( circle );
 
   decimate->SetTargetReduction( 0.95 );
 
   decimate->SetTargetReduction( 0.95 );
 
   decimate->Update();
 
   decimate->Update();
  
   vtkPolyDataMapper* d_mapper = vtkPolyDataMapper::New();
+
   vtkSmartPointer<vtkPolyDataMapper> d_mapper =  
 +
    vtkSmartPointer<vtkPolyDataMapper>::New();
 
   d_mapper->SetInput( decimate->GetOutput() );
 
   d_mapper->SetInput( decimate->GetOutput() );
  
   vtkActor* d_actor = vtkActor::New();
+
   vtkSmartPointer<vtkActor> d_actor =  
 +
    vtkSmartPointer<vtkActor>::New();
 
   d_actor->SetMapper( d_mapper );
 
   d_actor->SetMapper( d_mapper );
 
   d_actor->GetProperty()->SetColor( 1., 0. ,0. );
 
   d_actor->GetProperty()->SetColor( 1., 0. ,0. );
  
   vtkRenderer* ren = vtkRenderer::New();
+
   vtkSmartPointer<vtkRenderer> ren =  
 +
    vtkSmartPointer<vtkRenderer>::New();
 
   ren->AddActor( c_actor );
 
   ren->AddActor( c_actor );
 
   ren->AddActor( d_actor );
 
   ren->AddActor( d_actor );
  
   vtkRenderWindow* renwin = vtkRenderWindow::New();
+
   vtkSmartPointer<vtkRenderWindow> renwin =  
 +
    vtkSmartPointer<vtkRenderWindow>::New();
 
   renwin->AddRenderer( ren );
 
   renwin->AddRenderer( ren );
  
   vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
+
   vtkSmartPointer<vtkRenderWindowInteractor> iren =  
 +
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
 
   iren->SetRenderWindow( renwin );
 
   iren->SetRenderWindow( renwin );
  
Line 77: Line 85:
 
   iren->Start();
 
   iren->Start();
  
  iren->Delete();
 
  renwin->Delete();
 
  ren->Delete();
 
 
  d_actor->Delete();
 
  d_mapper->Delete();
 
 
  c_actor->Delete();
 
  c_mapper->Delete();
 
 
  decimate->Delete();
 
 
   circle->Delete();
 
   circle->Delete();
  

Revision as of 11:10, 5 August 2010

Decimate one Polyline

This example shows how to decimate one polyline.

Note that this example requires the git (master branch) version of vtk

DecimatePolyline.cxx

#include <vtkDecimatePolylineFilter.h>
#include <vtkMath.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>

#include <vtkSmartPointer.h>

int main(int, char *[])
{
  const unsigned int numberofpoints = 100;

  vtkPolyData* circle = vtkPolyData::New();
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  vtkSmartPointer<vtkCellArray> lines = vtkSmartPointer<vtkCellArray>::New();
  vtkIdType* lineIndices = new vtkIdType[numberofpoints+1];

  for( unsigned int i = 0; i < numberofpoints; i++ )
    {
    const double angle = 2.0 * vtkMath::Pi() * static_cast< double >( i ) /
      static_cast< double >( numberofpoints );
    points->InsertPoint( static_cast< vtkIdType >( i ),
                         cos( angle ),
                         sin( angle ),
                         0. );
    lineIndices[i] = static_cast< vtkIdType >( i );
    }
  lineIndices[numberofpoints] = 0;
  lines->InsertNextCell( numberofpoints+1, lineIndices );
  delete[] lineIndices;

  circle->SetPoints( points );
  circle->SetLines( lines );

  vtkSmartPointer<vtkPolyDataMapper> c_mapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
  c_mapper->SetInput( circle );

  vtkSmartPointer<vtkActor> c_actor = 
    vtkSmartPointer<vtkActor>::New();
  c_actor->SetMapper( c_mapper );

  vtkSmartPointer<vtkDecimatePolylineFilter> decimate = 
    vtkSmartPointer<vtkDecimatePolylineFilter>::New();
  decimate->SetInput( circle );
  decimate->SetTargetReduction( 0.95 );
  decimate->Update();

  vtkSmartPointer<vtkPolyDataMapper> d_mapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
  d_mapper->SetInput( decimate->GetOutput() );

  vtkSmartPointer<vtkActor> d_actor = 
    vtkSmartPointer<vtkActor>::New();
  d_actor->SetMapper( d_mapper );
  d_actor->GetProperty()->SetColor( 1., 0. ,0. );

  vtkSmartPointer<vtkRenderer> ren = 
    vtkSmartPointer<vtkRenderer>::New();
  ren->AddActor( c_actor );
  ren->AddActor( d_actor );

  vtkSmartPointer<vtkRenderWindow> renwin = 
    vtkSmartPointer<vtkRenderWindow>::New();
  renwin->AddRenderer( ren );

  vtkSmartPointer<vtkRenderWindowInteractor> iren = 
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  iren->SetRenderWindow( renwin );

  renwin->Render();

  iren->Start();

  circle->Delete();

  return EXIT_SUCCESS;
}


CMakeLists.txt

project( DecimatePolyline )
cmake_minimum_required( VERSION 2.6 )

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

add_executable( DecimatePolyline DecimatePolyline.cxx )
target_link_libraries( DecimatePolyline vtkHybrid )