Elements icon indicating copy to clipboard operation
Elements copied to clipboard

GetParameterAt reports -1 for some locations.

Open ikeough opened this issue 2 years ago • 1 comments

See https://github.com/tkahng/ElementsCurve.

ikeough avatar Feb 21 '23 22:02 ikeough

so i did some more digging and got it to work by using a custom extension method for Polygon.GetParameterAt. theres a few things ive noticed, but not sure if they are the culprit causing this behavior.

first the geometry in the rhino file is definitely not the best to test this on, since its a nurbs polycurve etc.

seems like there might be related to #945 , the closestPoint from DistanceTo does not seem to be always actually on the polygon. hence when using GetParameterAt using that point, it fails to find the segment the point lies on, returning -1.

GetParameterAt is not overriden on Polygon, so calls invoke the Polyline.Segments() instead of Polygon.Segments(), . maybe one less line in the return could be problematic.

heres a dirty workaround i used it make it work:

        public static double PolygonGetParameterAt(this Polygon polygon, Vector3 point)
        {
            var segments = polygon.Segments();
            var segment = segments.Select(s => new { line = s, dist = point.DistanceTo(point.ClosestPointOn(s)) }).OrderBy(p => p.dist).First().line;
            var segmentIndex = segments.ToList().IndexOf(segment);
            var segmentsLength = segments.Where((x, i) => i < segmentIndex).Sum(x => x.Length());
            var pointLength = segmentsLength + point.ClosestPointOn(segment).DistanceTo(segment.Start);
            return pointLength / polygon.Length();
        }

ill try to save the geometry as json so we can test without the rhino3dm dependency.

tkahng avatar Feb 25 '23 07:02 tkahng