# Is there a way to programmatically mark/select cells for extraction?

**URL:** https://discourse.vtk.org/t/is-there-a-way-to-programmatically-mark-select-cells-for-extraction/14379
**Category:** Support
**Created:** [August 9, 2024, 1:43pm UTC](https://discourse.vtk.org/t/is-there-a-way-to-programmatically-mark-select-cells-for-extraction/14379 "2024-08-09T13:43:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [August 9, 2024, 1:43pm UTC](https://discourse.vtk.org/t/is-there-a-way-to-programmatically-mark-select-cells-for-extraction/14379/1 "2024-08-09T13:43:45Z")

</div>

Perhaps there is a more elegant way of doing this, but I wanted to select cells where either the normal had a positive dot product with a given unit vector, or that had a positive dot product with a direction to a point in space. (Like directional light and point light in a graphics sense).

Right now I am doing it with something like this (and the selection is frustratingly not-quite-hemispherical):

```python
import numpy as np
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.util.numpy_support import vtk_to_numpy
from vtkmodules.vtkFiltersCore import vtkPolyDataNormals
from vtkmodules.vtkFiltersExtraction import vtkExtractSelection
from vtkmodules.vtkFiltersGeneral import vtkShrinkFilter
from vtkmodules.vtkFiltersSources import vtkSelectionSource, vtkSphereSource
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleTrackballCamera
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkDataSetMapper,
    vtkRenderer,
    vtkRenderWindow,
    vtkRenderWindowInteractor)

s = vtkSphereSource()
s.SetPhiResolution(20)
s.SetThetaResolution(20)

nf = vtkPolyDataNormals()
nf.ComputeCellNormalsOn()
nf.SetInputConnection(s.GetOutputPort())
nf.Update()
pd = nf.GetOutput()

normals = vtk_to_numpy(pd.GetCellData().GetNormals())
direction = np.array((1, 1, 0))
direction = direction/np.sqrt(np.dot(direction, direction))

ss = vtkSelectionSource()
ss.SetContentType(3) # supposed to be SelectionContent::INDICES ... python equiv?
ss.SetFieldType(0) # supposed to be SelectionField::CELL ... python equiv?

ci = pd.NewCellIterator()
ci.InitTraversal()

while True:
    ci.GoToNextCell()
    if ci.IsDoneWithTraversal():
        break
    idx = ci.GetCellId()
    cell_normal = normals[idx, :]
    if np.dot(cell_normal, direction) >= 0:
        ss.AddID(-1, idx)

es = vtkExtractSelection()
es.SetInputConnection(0, nf.GetOutputPort())
es.SetInputConnection(1, ss.GetOutputPort())
es.Update()

sf = vtkShrinkFilter()
sf.SetInputConnection(es.GetOutputPort())
sf.SetShrinkFactor(0.9)

mapper = vtkDataSetMapper()
mapper.SetInputConnection(sf.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)

ren = vtkRenderer()
ren_win = vtkRenderWindow()
ren_win.SetWindowName("Selected Sphere")
ren_win.SetSize(600, 600)
ren_win.AddRenderer(ren)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(ren_win)
style = vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style)

ren.AddActor(actor)
ren.ResetCamera()

ren_win.Render()
iren.Start()

```

---

<div class="post-metadata">

### Author: ![Jens\_Munk\_Hansen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jens_munk_hansen/32/1215_2.png) [@Jens\_Munk\_Hansen](https://discourse.vtk.org/u/Jens_Munk_Hansen)
#### Post date: [August 10, 2024, 2:21pm UTC](https://discourse.vtk.org/t/is-there-a-way-to-programmatically-mark-select-cells-for-extraction/14379/2 "2024-08-10T14:21:32Z")

</div>

Instead of a selection source, I have often done this. Don’t know if this is better?

```auto
   polydataConnect = vtkPolyDataConnectivityFilter()
   polydataConnect.SetExtractionModeToCellSeededRegions()
   polydataConnect.InitializeSeedList()
   for i in range(nCells):
      // Here your criteria on the dot product
      polydataConnect.AddSeed(i)
  polydataConnect.SetInputData(pd);
  polydataConnect.Update()
```

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [August 12, 2024, 12:47pm UTC](https://discourse.vtk.org/t/is-there-a-way-to-programmatically-mark-select-cells-for-extraction/14379/3 "2024-08-12T12:47:24Z")

</div>

That’s an interesting class, but for my particular use case there isn’t a topological way of extracting the region (I don’t think there is …).

---

<div class="post-metadata">

### Author: ![mau\_igna\_06](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mau_igna_06/32/8064_2.png) [@mau\_igna\_06](https://discourse.vtk.org/u/mau_igna_06)
#### Post date: [August 12, 2024, 4:24pm UTC](https://discourse.vtk.org/t/is-there-a-way-to-programmatically-mark-select-cells-for-extraction/14379/4 "2024-08-12T16:24:02Z")

</div>

I think you’ll need to use [VTK: vtkThreshold Class Reference](https://vtk.org/doc/nightly/html/classvtkThreshold.html)

---

<div class="post-metadata">

### Author: ![rexthor](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rexthor/32/6481_2.png) [@rexthor](https://discourse.vtk.org/u/rexthor)
#### Post date: [August 12, 2024, 6:27pm UTC](https://discourse.vtk.org/t/is-there-a-way-to-programmatically-mark-select-cells-for-extraction/14379/5 "2024-08-12T18:27:39Z")

</div>

Oh, I like that. So, for instance, I would define a node/cell data array, assign integers to it (inlet pressure = 1, wall = 2, etc.) and then threshold:

(swiped from [here](https://examples.vtk.org/site/Python/Medical/GenerateCubesFromLabels/)):

```python
    selector = vtkThreshold()
    selector.SetInputArrayToProcess(0, 0, 0, vtkDataObject().FIELD_ASSOCIATION_CELLS,
                                    vtkDataSetAttributes().SCALARS)
    selector.SetInputConnection(pad.GetOutputPort())
    selector.SetLowerThreshold(start_label)
    selector.SetUpperThreshold(end_label)
    selector.Update()

```
