Difference between revisions of "VTK/Examples/Cxx/Meshes/QuadricDecimation"

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to: navigation, search
(Created page with "==QuadricDecimation.cxx== <source lang="cpp"> #include <vtkPolyData.h> #include <vtkSphereSource.h> #include <vtkQuadricDecimation.h> #include <vtkSmartPointer.h> #include <vtkPo...")
 
(Now optionally accepts an input polydata file. Shared camera.)
Line 4: Line 4:
 
#include <vtkSphereSource.h>
 
#include <vtkSphereSource.h>
 
#include <vtkQuadricDecimation.h>
 
#include <vtkQuadricDecimation.h>
 +
#include <vtkXMLPolyDataReader.h>
 
#include <vtkSmartPointer.h>
 
#include <vtkSmartPointer.h>
 
#include <vtkPolyDataMapper.h>
 
#include <vtkPolyDataMapper.h>
Line 10: Line 11:
 
#include <vtkRenderWindowInteractor.h>
 
#include <vtkRenderWindowInteractor.h>
 
#include <vtkRenderer.h>
 
#include <vtkRenderer.h>
 +
#include <vtkCamera.h>
  
int main(int, char *[])
+
int main(int argc, char *argv[])
 
{
 
{
   vtkSmartPointer<vtkSphereSource> sphereSource =
+
   vtkSmartPointer<vtkPolyData> inputPolyData;
    vtkSmartPointer<vtkSphereSource>::New();
+
  if(argc > 1)
  sphereSource->Update();
+
    {
 
+
    vtkSmartPointer<vtkXMLPolyDataReader> reader =
   vtkSmartPointer<vtkPolyData> input =
+
      vtkSmartPointer<vtkXMLPolyDataReader>::New();
    vtkSmartPointer<vtkPolyData>::New();
+
    reader->SetFileName(argv[1]);
  input->ShallowCopy(sphereSource->GetOutput());
+
    reader->Update();
 +
    inputPolyData = reader->GetOutput();
 +
    }
 +
   else
 +
    {
 +
    vtkSmartPointer<vtkSphereSource> sphereSource =
 +
      vtkSmartPointer<vtkSphereSource>::New();
 +
    sphereSource->SetThetaResolution(30);
 +
    sphereSource->SetPhiResolution(15);
 +
    sphereSource->Update();
 +
    inputPolyData = sphereSource->GetOutput();
 +
    }
  
 
   std::cout << "Before decimation" << std::endl << "------------" << std::endl;
 
   std::cout << "Before decimation" << std::endl << "------------" << std::endl;
   std::cout << "There are " << input->GetNumberOfPoints() << " points." << std::endl;
+
   std::cout << "There are "
   std::cout << "There are " << input->GetNumberOfPolys() << " polygons." << std::endl;
+
            << inputPolyData->GetNumberOfPoints() << " points." << std::endl;
 +
   std::cout << "There are "
 +
            << inputPolyData->GetNumberOfPolys() << " polygons." << std::endl;
  
 
   vtkSmartPointer<vtkQuadricDecimation> decimate =
 
   vtkSmartPointer<vtkQuadricDecimation> decimate =
 
     vtkSmartPointer<vtkQuadricDecimation>::New();
 
     vtkSmartPointer<vtkQuadricDecimation>::New();
   decimate->SetInputConnection(input->GetProducerPort());
+
   decimate->SetInputConnection(inputPolyData->GetProducerPort());
 
   decimate->Update();
 
   decimate->Update();
  
Line 36: Line 51:
 
   std::cout << "After decimation" << vtkstd::endl << "------------" << std::endl;
 
   std::cout << "After decimation" << vtkstd::endl << "------------" << std::endl;
  
   std::cout << "There are " << decimated->GetNumberOfPoints() << " points." << std::endl;
+
   std::cout << "There are "
   std::cout << "There are " << decimated->GetNumberOfPolys() << " polygons." << std::endl;
+
            << decimated->GetNumberOfPoints() << " points." << std::endl;
 +
   std::cout << "There are "
 +
            << decimated->GetNumberOfPolys() << " polygons." << std::endl;
  
 
   vtkSmartPointer<vtkPolyDataMapper> inputMapper =
 
   vtkSmartPointer<vtkPolyDataMapper> inputMapper =
 
     vtkSmartPointer<vtkPolyDataMapper>::New();
 
     vtkSmartPointer<vtkPolyDataMapper>::New();
   inputMapper->SetInputConnection(input->GetProducerPort());
+
   inputMapper->SetInputConnection(inputPolyData->GetProducerPort());
 
   vtkSmartPointer<vtkActor> inputActor =
 
   vtkSmartPointer<vtkActor> inputActor =
 
     vtkSmartPointer<vtkActor>::New();
 
     vtkSmartPointer<vtkActor>::New();
 
   inputActor->SetMapper(inputMapper);
 
   inputActor->SetMapper(inputMapper);
 +
  inputActor->GetProperty()->SetInterpolationToFlat();
  
 
   vtkSmartPointer<vtkPolyDataMapper> decimatedMapper =
 
   vtkSmartPointer<vtkPolyDataMapper> decimatedMapper =
Line 84: Line 102:
 
   leftRenderer->AddActor(inputActor);
 
   leftRenderer->AddActor(inputActor);
 
   rightRenderer->AddActor(decimatedActor);
 
   rightRenderer->AddActor(decimatedActor);
 +
 +
  vtkSmartPointer<vtkCamera> camera =
 +
    vtkSmartPointer<vtkCamera>::New();
 +
  leftRenderer->SetActiveCamera(camera);
 +
  rightRenderer->SetActiveCamera(camera);
  
 
   leftRenderer->ResetCamera();
 
   leftRenderer->ResetCamera();
 
  rightRenderer->ResetCamera();
 
  
 
   renderWindow->Render();
 
   renderWindow->Render();
Line 94: Line 115:
 
   return EXIT_SUCCESS;
 
   return EXIT_SUCCESS;
 
}
 
}
 
 
</source>
 
</source>
  

Revision as of 22:36, 10 September 2010

QuadricDecimation.cxx

#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkQuadricDecimation.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkCamera.h>

int main(int argc, char *argv[])
{
  vtkSmartPointer<vtkPolyData> inputPolyData;
  if(argc > 1)
    {
    vtkSmartPointer<vtkXMLPolyDataReader> reader =
      vtkSmartPointer<vtkXMLPolyDataReader>::New();
    reader->SetFileName(argv[1]);
    reader->Update();
    inputPolyData = reader->GetOutput();
    }
  else
    {
    vtkSmartPointer<vtkSphereSource> sphereSource =
      vtkSmartPointer<vtkSphereSource>::New();
    sphereSource->SetThetaResolution(30);
    sphereSource->SetPhiResolution(15);
    sphereSource->Update();
    inputPolyData = sphereSource->GetOutput();
    }

  std::cout << "Before decimation" << std::endl << "------------" << std::endl;
  std::cout << "There are "
            << inputPolyData->GetNumberOfPoints() << " points." << std::endl;
  std::cout << "There are "
            << inputPolyData->GetNumberOfPolys() << " polygons." << std::endl;

  vtkSmartPointer<vtkQuadricDecimation> decimate =
    vtkSmartPointer<vtkQuadricDecimation>::New();
  decimate->SetInputConnection(inputPolyData->GetProducerPort());
  decimate->Update();

  vtkSmartPointer<vtkPolyData> decimated =
    vtkSmartPointer<vtkPolyData>::New();
  decimated->ShallowCopy(decimate->GetOutput());

  std::cout << "After decimation" << vtkstd::endl << "------------" << std::endl;

  std::cout << "There are "
            << decimated->GetNumberOfPoints() << " points." << std::endl;
  std::cout << "There are "
            << decimated->GetNumberOfPolys() << " polygons." << std::endl;

  vtkSmartPointer<vtkPolyDataMapper> inputMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  inputMapper->SetInputConnection(inputPolyData->GetProducerPort());
  vtkSmartPointer<vtkActor> inputActor =
    vtkSmartPointer<vtkActor>::New();
  inputActor->SetMapper(inputMapper);
  inputActor->GetProperty()->SetInterpolationToFlat();

  vtkSmartPointer<vtkPolyDataMapper> decimatedMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  decimatedMapper->SetInputConnection(decimated->GetProducerPort());
  vtkSmartPointer<vtkActor> decimatedActor =
    vtkSmartPointer<vtkActor>::New();
  decimatedActor->SetMapper(decimatedMapper);

  // There will be one render window
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->SetSize(600, 300);

  // And one interactor
  vtkSmartPointer<vtkRenderWindowInteractor> interactor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(renderWindow);

  // Define viewport ranges
  // (xmin, ymin, xmax, ymax)
  double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
  double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};

  // Setup both renderers
  vtkSmartPointer<vtkRenderer> leftRenderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderWindow->AddRenderer(leftRenderer);
  leftRenderer->SetViewport(leftViewport);
  leftRenderer->SetBackground(.6, .5, .4);

  vtkSmartPointer<vtkRenderer> rightRenderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderWindow->AddRenderer(rightRenderer);
  rightRenderer->SetViewport(rightViewport);
  rightRenderer->SetBackground(.4, .5, .6);

  // Add the sphere to the left and the cube to the right
  leftRenderer->AddActor(inputActor);
  rightRenderer->AddActor(decimatedActor);

  vtkSmartPointer<vtkCamera> camera =
    vtkSmartPointer<vtkCamera>::New();
  leftRenderer->SetActiveCamera(camera);
  rightRenderer->SetActiveCamera(camera);

  leftRenderer->ResetCamera();

  renderWindow->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)

PROJECT(QuadricDecimation)

FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})

ADD_EXECUTABLE(QuadricDecimation QuadricDecimation.cxx)
TARGET_LINK_LIBRARIES(QuadricDecimation vtkHybrid)