# Proposed change to curves resampling

**URL:** <https://discourse.slicer.org/t/proposed-change-to-curves-resampling/8471>\
**Category:** Development\
**Created:** [September 17, 2019, 7:30pm UTC](https://discourse.slicer.org/t/proposed-change-to-curves-resampling/8471 "2019-09-17T19:30:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [September 17, 2019, 7:30pm UTC](https://discourse.slicer.org/t/proposed-change-to-curves-resampling/8471/1 "2019-09-17T19:30:33Z")

</div>

I noticed some unexpected behavior while using [vtkMRMLMarkupsCurveNode::ResamplePoints](https://github.com/Slicer/Slicer/blob/1ba1f270ccb6e5227d27944b7968e00c688e78fd/Modules/Loadable/Markups/MRML/vtkMRMLMarkupsCurveNode.cxx#L193)

Due to small rounding differences when comparing the remaining segment to the sampling distance, the last point is sometimes not placed, as shown in the figure below. The resampled curve (blue) is plotted over the original curve (red).

 ![OriginalCurve](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/f/5fbb5749e7e9c86093b716f221b8eb8fa8ea19b1.png)

This issue can be replicated by creating an arbitrary 5 point curve and running:

```python
resampleNumber=10
curve=getNode("C")
currentPoints = curve.GetCurvePointsWorld()
newPoints = vtk.vtkPoints()
sampleDist = curve.GetCurveLengthWorld()/(resampleNumber-1)

closedCurveOption = 0
curve.ResamplePoints(currentPoints,newPoints,sampleDist,closedCurveOption)

vector=vtk.vtkVector3d()
pt=[0,0,0]
resampledCurve = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsCurveNode", "resampledCurveOrig")

for controlPoint in range(0,newPoints.GetNumberOfPoints()):
  newPoints.GetPoint(controlPoint,pt)
  vector[0]=pt[0]
  vector[1]=pt[1]
  vector[2]=pt[2]
  resampledCurve.AddControlPoint(vector)

```

I fixed this for myself by rounding the remaining segment length and sampling distance to 4 decimal places when they are compared. I was wondering if there’s interest in changing this in vtkMRMLMarkupsCurveNode::ResamplePoints too? I can submit a pull request if so. Thanks!

 ![UpdatedCurve](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/e/fe7e9804ff51ed62cc2f5581b0d202d503dfa816.png)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 17, 2019, 8:29pm UTC](https://discourse.slicer.org/t/proposed-change-to-curves-resampling/8471/2 "2019-09-17T20:29:21Z")

</div>

Yes, please send a pull request.

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [September 17, 2019, 11:04pm UTC](https://discourse.slicer.org/t/proposed-change-to-curves-resampling/8471/3 "2019-09-17T23:04:00Z")

</div>

Thanks @lassoan. I have one more question on this. In my module, I rounded to 4 decimal places to get rid of the differences on the order of ~1e-14 when comparing two doubles. I would like to use a more precise approach such as:

> double a, b;  
> if(fabs(a - b) \<= epsilon \* fabs(a))

Is there a convention or recommended approach for setting this epsilon in Slicer?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 17, 2019, 11:52pm UTC](https://discourse.slicer.org/t/proposed-change-to-curves-resampling/8471/4 "2019-09-17T23:52:57Z")

</div>

I’ve just found that the resample function already handled this case properly but only for closed curves. I’ve update the code to do the same for open curves, too.

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [September 18, 2019, 12:44am UTC](https://discourse.slicer.org/t/proposed-change-to-curves-resampling/8471/5 "2019-09-18T00:44:49Z")

</div>

Great! Thanks @lassoan!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 18, 2019, 4:21pm UTC](https://discourse.slicer.org/t/proposed-change-to-curves-resampling/8471/6 "2019-09-18T16:21:19Z")

</div>

Fix committed to master (Revision: 28508).

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [September 19, 2019, 6:29pm UTC](https://discourse.slicer.org/t/proposed-change-to-curves-resampling/8471/7 "2019-09-19T18:29:08Z")

</div>

Thanks @lassoan, this is working well for me in the preview version.
