# Intersect polydata with an implicit function

**URL:** https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706
**Category:** Support
**Created:** [April 8, 2019, 6:03pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706 "2019-04-08T18:03:55Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![lmainguy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/l/c0e974/32.png) [@lmainguy](https://discourse.vtk.org/u/lmainguy)
#### Post date: [April 8, 2019, 6:03pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706/1 "2019-04-08T18:03:55Z")

</div>

Hi all,  
There is [vtkIntersectionPolyDataFilter](https://vtk.org/doc/nightly/html/classvtkIntersectionPolyDataFilter.html) to get the intersection of two polydata but I didn’t find a filter that could that with a polydata and an implicit function.  
Of course, one can convert an implicit function to a polydata and use vtkIntersectionPolyDataFilter but it seems an unoptimized way to do it.  
Thanks for your help.  
Ludovic

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [April 8, 2019, 6:30pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706/2 "2019-04-08T18:30:07Z")

</div>

You can sample the implicit function with the polydata (probably using probe filter) then use threshold filter to cut the polydata. The advantage of this approach is that threshold filter always works, while vtkIntersectionPolyDataFilter randomly fails even for completely valid inputs.

---

<div class="post-metadata">

### Author: ![lmainguy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/l/c0e974/32.png) [@lmainguy](https://discourse.vtk.org/u/lmainguy)
#### Post date: [April 9, 2019, 5:48pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706/3 "2019-04-09T17:48:33Z")

</div>

Thanks Andras.  
I have a bit of difficulty to understand how these filters work.  
So far here is what I got:

> polyData = LoadMesh(objPath)

> source = vtk.vtkSphereSource()  
> source.SetCenter(center)  
> source.SetRadius(radius)  
> source.SetPhiResolution(100)  
> source.SetThetaResolution(100)

> probeFilter = vtk.vtkProbeFilter()  
> probeFilter.SetInputConnection( source.GetOutputPort())  
> probeFilter.SetSourceConnection(scanMesh.GetOutputPort())

> array = probeFilter.GetValidPoints()  
> tupleCount = array.GetNumberOfTuples()  
> print("tupleCount " + str(tupleCount))  
> componentCount = array.GetNumberOfComponents()  
> print("componentCount " + str(componentCount))

The tuple count is 0 and the component count is 1.

I’m not sure I am doing this right…

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [April 9, 2019, 6:12pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706/4 "2019-04-09T18:12:17Z")

</div>

The problem with the above code snippet that you try to use two polydata as input of probe filter. One of them should be the implicit function instead.

---

<div class="post-metadata">

### Author: ![lmainguy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/l/c0e974/32.png) [@lmainguy](https://discourse.vtk.org/u/lmainguy)
#### Post date: [April 9, 2019, 7:45pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706/5 "2019-04-09T19:45:43Z")

</div>

So I guess I have to convert a implicit function to a dataset.  
I tried to do that with a sample function like this:

> boxR = vtk.vtkCubeSource()  
> boxR.SetBounds(-10,10,-10,10,-10,10)

> radius = 4  
> center = 10,0,0

> sphere = vtk.vtkSphere()  
> sphere.SetCenter(center)  
> sphere.SetRadius(radius)

> sample = vtk.vtkSampleFunction()  
> sample.SetSampleDimensions(50,50,50)  
> sample.SetImplicitFunction(sphere)

> probeFilter = vtk.vtkProbeFilter()  
> probeFilter.SetInputConnection(sample.GetOutputPort())  
> probeFilter.SetSourceConnection(boxR.GetOutputPort())

But I still get what looks like an empty array.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [April 9, 2019, 9:09pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706/6 "2019-04-09T21:09:08Z")

</div>

@lmainguy as you have discovered, vtkProbeFilter cannot be used with implicit functions. And using vtkSampleFunction to convert an implicit function into a data set for use with vtkProbeFilter is horridly inefficient. Don’t use vtkProbeFilter for this.

The best approach is to use vtkClipPolyData, using SetClipFunction() to apply the implicit function.

The second-best approach is to use vtkSampleImplicitFunctionFilter, using SetImplicitFunction() to sample the function, followed by vtkClipPolyData.

---

<div class="post-metadata">

### Author: ![lmainguy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/l/c0e974/32.png) [@lmainguy](https://discourse.vtk.org/u/lmainguy)
#### Post date: [April 9, 2019, 9:15pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706/7 "2019-04-09T21:15:22Z")

</div>

Hi David,  
Thanks for your help!  
Ok the next step is to get the boundary edges of the clipped polydata.  
I guess vtkFeatureEdges is the correct tool to achieve that?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [April 9, 2019, 9:17pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706/8 "2019-04-09T21:17:53Z")

</div>

Yes indeed. But if all you need are the edges, then use vtkCutter instead of vtkClipPolyData. The cutter will give you the just the edges.

---

<div class="post-metadata">

### Author: ![lmainguy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/l/c0e974/32.png) [@lmainguy](https://discourse.vtk.org/u/lmainguy)
#### Post date: [April 9, 2019, 9:22pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706/9 "2019-04-09T21:22:47Z")

</div>

Ohhh so there is a class that does what I want. Nice!  
I was actually in the process to do that in Python, glad to know the solution already exists in C++.

Thanks David!

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [April 9, 2019, 9:24pm UTC](https://discourse.vtk.org/t/intersect-polydata-with-an-implicit-function/706/10 "2019-04-09T21:24:42Z")

</div>

Glad to help. One final note: if you are visualizing edges (or lines of any sort), you might find vtkTubeFilter useful. It turns all lines into tubes of a specified radius so that they are thicker and therefore easier to see. And it’s very fast.
