# distance along poly line (spline)

**URL:** https://discourse.vtk.org/t/distance-along-poly-line-spline/1153
**Category:** Support
**Created:** [June 15, 2019, 5:18pm UTC](https://discourse.vtk.org/t/distance-along-poly-line-spline/1153 "2019-06-15T17:18:42Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [June 15, 2019, 5:18pm UTC](https://discourse.vtk.org/t/distance-along-poly-line-spline/1153/1 "2019-06-15T17:18:42Z")

</div>

When you have a spline (many points, one poly line cell), is it possible to compute the distance along the spline for each point?

If this is possible when creating the spline, that’d be best but I have attached an example spline here: [spline.vtp](https://discourse.vtk.org/uploads/default/original/1X/dd54b03f7465fcc855c292ba82bb8600b3d430ed.vtp) (15.6 KB)

I created this with (pseudocode)

```python
spline_function = vtk.vtkParametricSpline()
spline_function.SetPoints(vtk_points)

para_source = vtk.vtkParametricFunctionSource()
para_source.SetParametricFunction(parametric_function)
para_source.SetUResolution(n_points - 1)
para_source.SetVResolution(100)
para_source.SetWResolution(100)
para_source.Update()

spline = para_source.GetOutput()

```

Please note that this originated in [this PyVista issue](https://github.com/pyvista/pyvista/issues/252)

---

<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: [June 15, 2019, 6:24pm UTC](https://discourse.vtk.org/t/distance-along-poly-line-spline/1153/2 "2019-06-15T18:24:56Z")

</div>

You can compute the distance along the curve once and store the distance values in point data. You could then find a particular distance value in the array very quickly (as the distance values are inherently sorted).

However, we found that simple on-the-fly computation of sum of Euclidean distances between curve points are fast enough for interactive use (e.g., for sliding a marker point along the curve by a specific distance). We usually have less than 1000 points in the curve - probably if we had hundreds of thousands of points then we would need to precompute, store, and look up distances.

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [June 19, 2019, 4:08pm UTC](https://discourse.vtk.org/t/distance-along-poly-line-spline/1153/3 "2019-06-19T16:08:39Z")

</div>

So I’d have to compute the euclidean distances between each point and consecutively sum them to get the distance. This isn’t exactly what I was hoping for but I suppose it will approximate the distance.For some reason, I was thinking that `vtk.vtkParametricFunctionSource` would have an implicit function that could represent a smooth version of the line in 3D space for me to compute distances on… now realizing it doesn’t have that.

---

<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: [June 19, 2019, 4:32pm UTC](https://discourse.vtk.org/t/distance-along-poly-line-spline/1153/4 "2019-06-19T16:32:16Z")

</div>

Computing distance from linear segments is not necessarily a limitation but often preferable, because it guarantees that the curve that the user can see (which is always made up of linear segments, as required by the renderer) is the same model that is used for computations.

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [June 19, 2019, 4:33pm UTC](https://discourse.vtk.org/t/distance-along-poly-line-spline/1153/5 "2019-06-19T16:33:55Z")

</div>

Ah, I’m realizing this now… I don’t work with poly lines often so I didn’t realize they were always linearly segmented. I had been working with very high resolution lines so my eyes couldn’t tell

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)
#### Post date: [June 19, 2019, 4:54pm UTC](https://discourse.vtk.org/t/distance-along-poly-line-spline/1153/6 "2019-06-19T16:54:07Z")

</div>

@banesullivan you may find [vtkAppendArcLength](https://vtk.org/doc/nightly/html/classvtkAppendArcLength.html) helpful for this task.

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [June 19, 2019, 6:03pm UTC](https://discourse.vtk.org/t/distance-along-poly-line-spline/1153/7 "2019-06-19T18:03:22Z")

</div>

Ah thanks, @cory.quammen! `vtkAppendArcLength` is **exactly** what I needed!
