RFC: should this method be generally available?

To developers.

I am thinking about proposing this function 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.

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:

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.

1 Like

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.

‘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 where this function could have a place.

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

Thank you again.

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.

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

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

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.