vtkDecimatePolylineFilter not reducing the number of points

Using vtk for python, when using vtkDecimatePolylineFilter, the output has the same number of points (and lines) as the input.
Here below a minimal example supposed decimate from 30 to 10 points (target reduction of 0.667). But there is still 30 points in the output.

Am I using this filter wrong?
Thanks

import vtk
import numpy as np

# Create example data with 30 points and 30 lines

n = 30
n_down = 10

points = vtk.vtkPoints()
for i in range(int(n/2)):
    t = (4*i-n)/n
    points.InsertNextPoint(t, np.cos(t), 0) 
for i in range(int(n/2)):
    t = -(4*i-n)/n
    points.InsertNextPoint(t, -np.cos(t), 0) 

lines = vtk.vtkCellArray()
for i in range(n-1):
    line = vtk.vtkLine()
    line.GetPointIds().SetId(0, i)
    line.GetPointIds().SetId(1, i + 1)
    lines.InsertNextCell(line)
line.GetPointIds().SetId(0, i + 1)
line.GetPointIds().SetId(1, 0)
lines.InsertNextCell(line)

poly = vtk.vtkPolyData()
poly.SetPoints(points)
poly.SetLines(lines)

# Decimation
down_factor = (n - n_down) / n
decimator = vtk.vtkDecimatePolylineFilter()
decimator.SetInputData(poly)
decimator.SetTargetReduction(down_factor)
decimator.Update()
poly_down = decimator.GetOutput()

# Check number of points and lines in the downsampled poly
print(f"Original Number of Points: {poly.GetNumberOfPoints()}") 
print(f"Original Number of Lines: {poly.GetNumberOfLines()}")     
print(f"Downsampled Number of Points: {poly_down.GetNumberOfPoints()}") 
print(f"Downsampled Number of Lines: {poly_down.GetNumberOfLines()}")  
Original Number of Points: 30
Original Number of Lines: 30
Downsampled Number of Points: 30
Downsampled Number of Lines: 30
1 Like

I suspect that the filter is reducing the size of the polyline cells, and leaving the points untouched. Whether this is a feature, bug, or laziness on the part of the developer (probably me :slight_smile: ) is hard to say, certainly a mode could be added to discard unused points. In the mean time, if you use vtkCleanPolyData or vtkStaticCleanPolyData with RemoveUnusedPoints enabled, you should be able to remove the unused points.

1 Like

But aren’t the number of lines supposed to change at least? I am facing a similar problem. I don’t see any reduction in the cells.

The lines are assumed connected to form polylines… otherwise nothing changes

For an example of what Will is describing see how to create polylines here https://examples.vtk.org/site/PythonicAPI/PolyData/DecimatePolyline/