[vtkusers] mesh decimation

Agata Krasoń agatakrason at gmail.com
Tue Sep 19 15:11:34 EDT 2017


Yes,
I attached a file with source code.

Agata

2017-09-19 19:35 GMT+02:00 Chiranjib Sur <sur.chiranjib at gmail.com>:

> Could you please share your sample code in this thread?
>
> Thanks,
> Chiranjib
>
> On Tue, Sep 19, 2017 at 2:25 PM, Agata Krasoń <agatakrason at gmail.com>
> wrote:
>
>> Hello,
>>
>> Thanks for your reply.
>> Hmmm ... Yes, I know about that filter. I have already done this example.
>> I got the same number of triangle in each mesh. But the same number of
>> triangle doesn't equal the same number of points. That's my main problem  :
>> (
>>
>> Best regards,
>> Agata
>>
>> 2017-09-18 19:55 GMT+02:00 Chiranjib Sur <sur.chiranjib at gmail.com>:
>>
>>> Hi,
>>> The decimation filter works on percentage of the mesh that you need to
>>> decimate
>>>
>>> Look at this line of the example @ wiki (https://www.vtk.org/Wiki/VTK/
>>> Examples/Cxx/Meshes/Decimation)
>>>
>>> decimate->SetTargetReduction(.10); //10% reduction (if there was 100 triangles, now there will be 90)
>>>
>>>
>>> Hope that answers your query.
>>> Thanks,
>>> Chiranjib
>>>
>>> On Mon, Sep 18, 2017 at 8:14 PM, agatte <agatakrason at gmail.com> wrote:
>>>
>>>> Hello vtk users,
>>>>
>>>> I would like to decimate mesh (triangle meshes) polydata to constant
>>>> number
>>>> of points.
>>>> I have already tried vtkDecimationPro filter but I could only decimate
>>>> to
>>>> fix number of triangles.
>>>> I don't know how can I decimate a mesh to fix number of points ?
>>>>
>>>> I would appreciate for any help/advice.
>>>>
>>>>
>>>> Best :)
>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
>>>> _______________________________________________
>>>> Powered by www.kitware.com
>>>>
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Please keep messages on-topic and check the VTK FAQ at:
>>>> http://www.vtk.org/Wiki/VTK_FAQ
>>>>
>>>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://public.kitware.com/mailman/listinfo/vtkusers
>>>>
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170919/8566e2ad/attachment.html>
-------------- next part --------------

cmake_minimum_required(VERSION 2.8)

PROJECT(Decimation)

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

add_executable(Decimation MACOSX_BUNDLE Decimation.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(Decimation ${VTK_LIBRARIES})
else()
  target_link_libraries(Decimation vtkHybrid vtkWidgets)
endif()
-------------- next part --------------
#include <vtkVersion.h>
#include <vtkPolyData.h>
#include <vtkSphereSource.h>
#include <vtkDecimatePro.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkPolyDataReader.h>
#include <vtkPolyDataWriter.h>



int main(int, char *[])
{

  //vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();
  //sphereSource->Update();

	vtkSmartPointer<vtkPolyDataReader> reader = vtkSmartPointer<vtkPolyDataReader>::New();
	reader->SetFileName("2proj__005.vtk");
	reader->Update();

  vtkSmartPointer<vtkPolyData> input = vtkSmartPointer<vtkPolyData>::New();
  input->ShallowCopy(reader->GetOutput());
  
  std::cout << "Before decimation" << std::endl << "------------" << std::endl;
  std::cout << "There are " << input->GetNumberOfPoints() << " points." << std::endl;
  std::cout << "There are " << input->GetNumberOfPolys() << " polygons." << std::endl;

  vtkSmartPointer<vtkDecimatePro> decimate =
    vtkSmartPointer<vtkDecimatePro>::New();
#if VTK_MAJOR_VERSION <= 5
  decimate->SetInputConnection(input->GetProducerPort());
#else
  decimate->SetInputData(input);
#endif
  //decimate->SetTargetReduction(.9); //99% reduction (if there was 100 triangles, now there will be 1)
  decimate->SetTargetReduction(.20); //10% reduction (if there was 100 triangles, now there will be 90)
  //decimate->
  decimate->Update();

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

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

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


    vtkSmartPointer<vtkPolyDataWriter> writer = vtkPolyDataWriter::New(); 
	 writer->SetFileName("dec_seg001test.vtk");
     writer->SetInput(decimated);
     writer->Write();



  vtkSmartPointer<vtkPolyDataMapper> inputMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
  inputMapper->SetInputConnection(input->GetProducerPort());
#else
  inputMapper->SetInputData(input);
#endif
  vtkSmartPointer<vtkActor> inputActor =
    vtkSmartPointer<vtkActor>::New();
  inputActor->SetMapper(inputMapper);

  vtkSmartPointer<vtkPolyDataMapper> decimatedMapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
  decimatedMapper->SetInputConnection(decimated->GetProducerPort());
#else
  decimatedMapper->SetInputData(decimated);
#endif
  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);

  leftRenderer->ResetCamera();

  rightRenderer->ResetCamera();

  renderWindow->Render();
  interactor->Start();
  
  return EXIT_SUCCESS;
}


More information about the vtkusers mailing list