hypar-io / Elements

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

Polygon.TransformAtNormalized returns nonsense result #1032

Open andrewheumann opened 11 months ago

andrewheumann commented 11 months ago

To reproduce:

var rectangle = Polygon.Rectangle(4, 10);
var transformsNormalized = Enumerable.Range(0, 20).SelectMany((i) => {
    return rectangle.TransformAtNormalized(i / 20.0).ToModelCurves();
});

produces:

image

The positions are incorrect; there should be some along the shorter edges. The axes are also incorrect — many Y and Z appear to be zero-length.

Note that the code for PointAtNormalized finds the correct positions:

var rectLength = rectangle.Length();
var lines = Enumerable.Range(0, 20).Select((i) => {
    return new ModelCurve(new Line(rectangle.PointAtNormalized(i / 20.0), rectangle.Center()));
});

image

DmytroMuravskyi commented 11 months ago

TransformAt is working wrong for polyline. At one point it does var d = this.Length() * u; which is wrong since u is no longer from 0 to 1. Since it's known that that each segment has domain of 1, much simpler approach can be used. Also, you can see that corner points have rotated transformation, but only two. This is because averaging is used on every vertex except first and last vertices. But for Polygon every vertex should be averaged, in my opinion. Third issue I have found is not related but would be nice to clarify. IndexedPolycurve.CreateVerticesAndCurveIndices produces unique indexes. For two lines and arc in between it would be [0, 1], [2, 3, 4], [5, 6]. It's not only in contrast with CreateCurveIndices but also changes indices when polyline is copies. If the TransformedPolycurve is called on polycurve with indices [0, 1], [1, 2, 3], [3, 4], new polycurve will have indices as described above: [0, 1], [2, 3, 4], [5, 6] indices that out of range of verticies.

DmytroMuravskyi commented 11 months ago

image The result I got

wynged commented 11 months ago

@andrewheumann