# contour interpolation/resampling

**URL:** https://discourse.vtk.org/t/contour-interpolation-resampling/1000
**Category:** Support
**Created:** [May 21, 2019, 5:11pm UTC](https://discourse.vtk.org/t/contour-interpolation-resampling/1000 "2019-05-21T17:11:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sbucc](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@sbucc](https://discourse.vtk.org/u/sbucc)
#### Post date: [May 21, 2019, 5:11pm UTC](https://discourse.vtk.org/t/contour-interpolation-resampling/1000/1 "2019-05-21T17:11:27Z")

</div>

I’m completely new to vtk so bear with me if what I ask turns out to be incredibly easy for the expert user. Believe me, I’ve searched everywhere but I can’t find a way to do what I need. So here’s my problem:

I have a mesh, and I need to extract contours cutting it with planes at regular intervals. I’m using vtkCutter + vtkStripper + vtkCleanPolyData to obtain a list of points. So far so good, however I need these contours to be resampled/interpolated so that each contour is returned as a fixed number of points, equally spaced. Is there a way to do this in vtk: options to set on vtkCutter, other filters that I can add?

I also have another question: on some meshes the contours run clockwise, on others counter-clockwise, why? Of course I’m using the same planes, always oriented in the direction of the positive X axis.

Thanks

* * *

my code:

vtkSmartPointer intersection(vtkPlane& plane, vtkSmartPointer polydata) {  
auto cutter = vtkSmartPointer::New();  
cutter-\>SetCutFunction(&plane);  
cutter-\>SetInputData(polydata);  
cutter-\>Update();

```
auto cutStripper = vtkSmartPointer<vtkStripper>::New();
cutStripper->SetInputConnection(cutter->GetOutputPort());
cutStripper->Update();

auto cleanPolyData = vtkSmartPointer<vtkCleanPolyData>::New();
cleanPolyData->SetInputData(cutStripper->GetOutput());
cleanPolyData->Update();

vtkSmartPointer<vtkPolyData> contour = cleanPolyData->GetOutput();
return contour;

```

}

---

<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: [May 21, 2019, 5:22pm UTC](https://discourse.vtk.org/t/contour-interpolation-resampling/1000/2 "2019-05-21T17:22:38Z")

</div>

> [@sbucc](#):
>
> Is there a way to do this in vtk: options to set on vtkCutter, other filters that I can add?

I don’t think there is a polyline resampling filter in VTK. You can take [ResamplePoints](https://github.com/Slicer/Slicer/blob/a3fce8013ef8b63e101eb0afcc635f585ef45c3e/Modules/Loadable/Markups/MRML/vtkMRMLMarkupsCurveNode.cxx#L165-L252) function from 3D Slicer (it only uses VTK).

> [@sbucc](#):
>
> I also have another question: on some meshes the contours run clockwise, on others counter-clockwise, why?

You find and append line segments in random order, so polygon winding is random. It would require extra computation to determine winding and reverse the list of point indices, so it is not done in the cutter or stripper. If you don’t find a method in VTK for getting winding of a polygon then do a web search, you’ll find many implementations.

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [May 21, 2019, 8:39pm UTC](https://discourse.vtk.org/t/contour-interpolation-resampling/1000/3 "2019-05-21T20:39:43Z")

</div>

Look at this example [FitSplineToCutterOutput](https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/FitSplineToCutterOutput/) from the [VTKExamplesProject](https://lorensen.github.io/VTKExamples/site/).

---

<div class="post-metadata">

### Author: ![sbucc](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@sbucc](https://discourse.vtk.org/u/sbucc)
#### Post date: [May 22, 2019, 5:51pm UTC](https://discourse.vtk.org/t/contour-interpolation-resampling/1000/4 "2019-05-22T17:51:20Z")

</div>

Thank you both for the useful info. @lorensen, I’m trying to make that example work for me, and failing. All the examples are geared towards rendering, with mapper and actor, but what I need is points, sampled from the spline at regular intervals. I have tried this code, but the resulting contour has 0 points:

```
auto spline = vtkSmartPointer<vtkKochanekSpline>::New();
spline->SetDefaultTension(.5);
auto sf = vtkSmartPointer<vtkSplineFilter>::New();
sf->SetInputConnection(cutStripper->GetOutputPort());
sf->SetSubdivideToSpecified();
sf->SetNumberOfSubdivisions(50);
sf->SetSpline(spline);
sf->GetSpline()->ClosedOn();
auto contour = vtkSmartPointer<vtkPolyData>::New();
contour->SetPoints(sf->GetOutput()->GetPoints());
auto nl = contour->GetNumberOfLines(); // -> 0
auto np = contour->GetNumberOfPoints(); // -> 0
auto npi = contour->GetNumberOfPieces(); // -> 1
auto nv = contour->GetNumberOfVerts(); // -> 0

```

My problem is that I still don’t get how vtk works, I’m just copying the examples and hoping it works…  
If I can’t get my points using vtk calls, I’m thinking of doing linear interpolation of the points from cutStripper or cleanPolyData, but I imagine whatever I can come up with will be less efficient.

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [May 22, 2019, 6:30pm UTC](https://discourse.vtk.org/t/contour-interpolation-resampling/1000/5 "2019-05-22T18:30:09Z")

</div>

VTK is a demand-driven pipeline. See: [Visualization Pipeline](https://lorensen.github.io/VTKExamples/site/VTKBook/04Chapter4/).

That said, just do a  
sf-\>Update();  
and you should see the data you are asking for.

For future reference, the entire VTK Text Book is available in two forms:

- [VTK Markdown](https://lorensen.github.io/VTKExamples/site/VTKBook/01Chapter1/), good for mobile and interactive viewing.

- [VTK Latex](https://raw.githubusercontent.com/lorensen/VTKExamples/master/src/VTKBookLaTeX/VTKTextBook.pdf), good as a down loadable reference

Enjoy VTK,

Bill

---

<div class="post-metadata">

### Author: ![sbucc](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@sbucc](https://discourse.vtk.org/u/sbucc)
#### Post date: [May 22, 2019, 7:14pm UTC](https://discourse.vtk.org/t/contour-interpolation-resampling/1000/6 "2019-05-22T19:14:09Z")

</div>

…d’oh! It wasn’t in the example, but I should’ve realized it was necessary. Thanks Bill.

I still have a couple of problems:

- more than 50 points per spline, the number is always 1 + n\*(50+1)
- some points are overlapping and not equally spaced

My feeling is that the problem is the previous steps: vtkStripper + vtkCleanPolyData are generating the points in more than one section, with overlappings. I guess I have to do myself the unwinding as @lassoan was suggesting.

---

<div class="post-metadata">

### Author: ![sbucc](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/f475e1/32.png) [@sbucc](https://discourse.vtk.org/u/sbucc)
#### Post date: [May 28, 2019, 4:03pm UTC](https://discourse.vtk.org/t/contour-interpolation-resampling/1000/7 "2019-05-28T16:03:34Z")

</div>

Just a final comment about my remaining issues:

- multiple lines from vtkStripper: for whatever reason vtkStripper was always returning one good line, containing the expected contour, plus a couple of spurious bits containing 1-3 points each. The simple solution is to just keep the longest line.
- some contours are clockwise, some anti-clockwise: it just happens, even within the same mesh. If the direction is important, just check afterwards and reverse if necessary.
