hypar-io / Elements

The smallest useful BIM.
https://www.hypar.io
MIT License
349 stars 74 forks source link

Unexpected behavior PointAtNormalized for polylines #1006

Open pederlh opened 1 year ago

pederlh commented 1 year ago

Describe the bug Starting with Hypar.Elements 2.0.0, polylines are parametrized 0 -> # vertices - 1, which changed the behavior of methods such as PointAt. In order to facilitate simple library updates, the convenience method PointAtNormalized using the old parametrization 0 -> 1 was introduced. However, even though this method deceptively appears to be linear, it is not. Should this behavior change or be clarified better?

To Reproduce An sample polyline with three vertices is initialized below:

Polyline perimeter = new(new List<Vector3>()
{
    new Vector3(x: 0.0, y: 0.0),
    new Vector3(x: 8.0,  y: 0.0),
    new Vector3(x: 10.0, y: 0.0), 
}
);

Vector3 point0 = perimeter.PointAtNormalized(0.25);
Vector3 point1 = perimeter.PointAtNormalized(0.50);

Expected behavior point0 is located at x=2.5 and point1 at x=5.0.

What happens point0 is located at x=4.0 and point1 at x=8.0.

Bonus Is there an elegant way of changing from old to new parametrization? The procedure seems to involve finding the length of each individual line + the total polyline length, and calculating the parametrization variable for each vertex.