# RFC: should this method be generally available?

**URL:** <https://discourse.slicer.org/t/rfc-should-this-method-be-generally-available/28332>\
**Category:** Development\
**Created:** [March 12, 2023, 5:49pm UTC](https://discourse.slicer.org/t/rfc-should-this-method-be-generally-available/28332 "2023-03-12T17:49:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [March 12, 2023, 5:49pm UTC](https://discourse.slicer.org/t/rfc-should-this-method-be-generally-available/28332/1 "2023-03-12T17:49:46Z")

</div>

To developers.

I am thinking about proposing this [function](https://github.com/chir-set/SlicerExtraMarkups/blob/0e1309b4d518d903d9d53ada237fcac56c87203a/Shape/MRML/vtkMRMLMarkupsShapeNode.cxx#L331) for inclusion in vtkMath.

It determines a point coordinate along a line defined by p1 and p2, given an offset relative to p2. Basically, it

- shifts p1 to origin,
- determines the resulting p2 (rp2) accounting for this shift,
- calculates a linear proportional scale based on lengths,
- applies the scale to get the expected coordinate component in each dimension,
- reverts the shift.

I found it very useful in many places and think that it might be of interest more generally.

Looking forward for your inputs.

Regards.

---

<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:** [March 12, 2023, 11:13pm UTC](https://discourse.slicer.org/t/rfc-should-this-method-be-generally-available/28332/2 "2023-03-12T23:13:50Z")

</div>

Thanks for considering contributing this to Slicer/vtkAddon. We usually create utility functions for frequent and/or complex operations.

The function you suggest is not a complex operation - it is a single-line operation in Python, single-line operation in C++ if you use VNL or Eigen, and even if you use VTK it is quite short and simple:

```auto
double directionVector[3] = { p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2] };
vtkMath::Normalize(directionVector);
result[0] = p2[0] + lineLength * directionVector[0];
result[1] = p2[1] + lineLength * directionVector[1];
result[2] = p2[2] + lineLength * directionVector[2];

```

Still, if this function was used very often then it would make sense to make it a widely available utility function. If you can find at least a few cases anywhere in Slicer code where this utility function would simplify the code (you can find candidates by searching for `vtkMath::Normalize(` in the Slicer source code) then I think it is a good justification to add it to vtkAddon (using a name like `GetPointAlongLine`). If it seems that it would not be used at all in Slicer’s relative large code base then it is probably enough to keep it in your module for now.

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [March 13, 2023, 10:11am UTC](https://discourse.slicer.org/t/rfc-should-this-method-be-generally-available/28332/3 "2023-03-13T10:11:37Z")

</div>

Thank you for considering.

Using ‘vtkMath::Normalize’ is indeed an elegant approach to a more _professional_ code style. In your code snippet, ‘linelength’ should nevertheless be replaced by ‘offset’ or ‘difference’ to obtain same results.

> [@lassoan](#):
>
> if this function was used very often

‘vtkMath::Normalize’ is used very often in the Slicer code base, I could not go to every place where it is called. I have found only one [instance](https://github.com/Slicer/Slicer/blob/main/Libs/MRML/DisplayableManager/vtkMRMLSliceIntersectionInteractionRepresentation.cxx#L267) where this function could have a place.

> [@lassoan](#):
>
> If you can find at least a few cases

A single case will not justify an addition to vtkAddonMathUtilities. So we may leave things as they are.

Thank you again.

---

<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:** [March 13, 2023, 12:19pm UTC](https://discourse.slicer.org/t/rfc-should-this-method-be-generally-available/28332/4 "2023-03-13T12:19:46Z")

</div>

Maybe what we could add is an `AddScaledVector` method. That would be less specialized and that could replace 3 lines by a single command at a number of places.

```python
double directionVector[3] = { p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2] };
vtkMath::Normalize(directionVector);
vtkAddonMath::AddScaledVector(result, p2, directionVector, lineLength);

```

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [March 13, 2023, 1:54pm UTC](https://discourse.slicer.org/t/rfc-should-this-method-be-generally-available/28332/5 "2023-03-13T13:54:03Z")

</div>

> [@lassoan](#):
>
> Maybe what we could add

Ok, I’ll bake something and continue in a PR.

---

<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:** [March 13, 2023, 2:11pm UTC](https://discourse.slicer.org/t/rfc-should-this-method-be-generally-available/28332/6 "2023-03-13T14:11:03Z")

</div>

OK. Submit it to vtkAddon, as we maintain general-purpose low-level vector/matrix computation utility functions there. Or maybe you can even try submitting to VTK.
