DecimatePolyline
Repository source: DecimatePolyline
Description¶
This example shows how to decimate a polyline.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
DecimatePolyline.cxx
#include <vtkActor.h>
#include <vtkCellArray.h>
#include <vtkDecimatePolylineFilter.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
int main(int, char*[])
{
const unsigned int numberofpoints = 100;
vtkNew<vtkPolyData> circle;
vtkNew<vtkPoints> points;
vtkNew<vtkCellArray> lines;
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);
vtkNew<vtkPolyDataMapper> c_mapper;
c_mapper->SetInputData(circle);
vtkNew<vtkNamedColors> colors;
vtkNew<vtkActor> c_actor;
c_actor->SetMapper(c_mapper);
c_actor->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData());
c_actor->GetProperty()->SetLineWidth(3);
vtkNew<vtkDecimatePolylineFilter> decimate;
decimate->SetInputData(circle);
decimate->SetTargetReduction(0.95);
decimate->Update();
vtkNew<vtkPolyDataMapper> d_mapper;
d_mapper->SetInputConnection(decimate->GetOutputPort());
vtkNew<vtkActor> d_actor;
d_actor->SetMapper(d_mapper);
d_actor->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
d_actor->GetProperty()->SetLineWidth(3);
vtkNew<vtkRenderer> ren;
ren->AddActor(c_actor);
ren->AddActor(d_actor);
ren->SetBackground(colors->GetColor3d("SlateGray").GetData());
vtkNew<vtkRenderWindow> renwin;
renwin->AddRenderer(ren);
renwin->SetSize(640, 480);
renwin->SetWindowName("DecimatePolyline");
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renwin);
renwin->Render();
iren->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(DecimatePolyline)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
FiltersCore
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "DecimatePolyline: Unable to find the VTK build folder.")
endif()
# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(DecimatePolyline MACOSX_BUNDLE DecimatePolyline.cxx )
target_link_libraries(DecimatePolyline PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS DecimatePolyline
MODULES ${VTK_LIBRARIES}
)
Download and Build DecimatePolyline¶
Click here to download DecimatePolyline and its CMakeLists.txt file. Once the tarball DecimatePolyline.tar has been downloaded and extracted,
cd DecimatePolyline/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:
./DecimatePolyline
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.