# Quick count of weighted number of points inside grid cells

**URL:** https://discourse.paraview.org/t/quick-count-of-weighted-number-of-points-inside-grid-cells/8187
**Category:** ParaView Support
**Created:** [October 14, 2021, 11:15am UTC](https://discourse.paraview.org/t/quick-count-of-weighted-number-of-points-inside-grid-cells/8187 "2021-10-14T11:15:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Fotos\_Stylianou](https://discourse.paraview.org/user_avatar/discourse.paraview.org/fotos_stylianou/32/7759_2.png) [@Fotos\_Stylianou](https://discourse.paraview.org/u/Fotos_Stylianou)
#### Post date: [October 14, 2021, 11:15am UTC](https://discourse.paraview.org/t/quick-count-of-weighted-number-of-points-inside-grid-cells/8187/1 "2021-10-14T11:15:05Z")

</div>

Hi All,

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

 ![points](https://discourse.paraview.org/uploads/default/original/2X/2/27a77d54f74b58db1d60f57192804473ef916bd6.png)

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

 ![grid](https://discourse.paraview.org/uploads/default/original/2X/6/6e1d3f8413b84ee1a8502d6036a73e0296891687.jpeg)

NOTE the grid in the Y direction is just one cell

 ![Grid in Y](https://discourse.paraview.org/uploads/default/original/2X/6/6d21e14e7065019a8fc3a65b464659b1ffb4c8c6.jpeg)

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?

Thanks  
Fotos

---

<div class="post-metadata">

### Author: ![Kenichiro-Yoshimi](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/k/ecc23a/32.png) [@Kenichiro-Yoshimi](https://discourse.paraview.org/u/Kenichiro-Yoshimi)
#### Post date: [October 17, 2021, 12:30pm UTC](https://discourse.paraview.org/t/quick-count-of-weighted-number-of-points-inside-grid-cells/8187/2 "2021-10-17T12:30:00Z")

</div>

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** _.

```python
grid = self.GetInputDataObject(0, 0)
pnts = self.GetInputDataObject(0, 1)
output.CopyStructure(grid)

import paraview.vtk as vtk
import numpy as np

locator = vtk.vtkCellLocator()
locator.SetDataSet(grid)
locator.BuildLocator()

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')

```

 ![count_pnts](https://discourse.paraview.org/uploads/default/original/2X/5/5fd8ab228ed657f97b63f47dc902566555ae0d37.jpeg)

Upload the State file for the above.  
[count\_pnts.pvsm](https://discourse.paraview.org/uploads/short-url/m0mlxgNEkEeLcvcvYq6w6CZYD7Z.pvsm) (524.9 KB)

The weighted version can be achieved with a few modifications.

---

<div class="post-metadata">

### Author: ![Fotos\_Stylianou](https://discourse.paraview.org/user_avatar/discourse.paraview.org/fotos_stylianou/32/7759_2.png) [@Fotos\_Stylianou](https://discourse.paraview.org/u/Fotos_Stylianou)
#### Post date: [October 27, 2021, 4:58pm UTC](https://discourse.paraview.org/t/quick-count-of-weighted-number-of-points-inside-grid-cells/8187/3 "2021-10-27T16:58:47Z")

</div>

Thanks! perfect!
