Quick count of weighted number of points inside grid cells

Hi All,

I have a set of random points in 3D space and each one of them has a variable which describes its importance.

I use the FastUniformGrid and Transform filter to generate a bounding grid for the points as below:

NOTE the grid in the Y direction is just one cell

I would like to have on the grid cells the number of points they contain. Furthermore I would like to know the weighted number of points based on the importance variable.

Is there a quick way to do this with combination of filters?


Using the vtkCellLocator in the Programmable filter, you can count the number of points contained in the grid’s cells. The procedure is as follows:

  1. In the Pipeline Browser, select the grid dataset.
  2. Hold down the Crtl key and select the Point dataset.
  3. Call the Programmable Filter.
  4. Write the following in Script and Apply.
grid = self.GetInputDataObject(0, 0)
pnts = self.GetInputDataObject(0, 1)

import paraview.vtk as vtk
import numpy as np

locator = vtk.vtkCellLocator()

num_cells = output.GetNumberOfCells()
np_counts = np.zeros(num_cells, dtype=int)
num_pnts = pnts.GetNumberOfPoints()
for i in range(num_pnts):
    pt = pnts.GetPoint(i)
    cell_id = locator.FindCell(pt)
    np_counts[cell_id] += 1

output.CellData.append(np_counts, 'count')

Upload the State file for the above.
count_pnts.pvsm (524.9 KB)

The weighted version can be achieved with a few modifications.

Thanks! perfect!