Flaw in RPolyline.getDistancesFromStart(p)

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
CVH
Premier Member
Posts: 5136
Joined: Wed Sep 27, 2017 4:17 pm

Flaw in RPolyline.getDistancesFromStart(p)

Post by CVH » Thu Jan 29, 2026 6:50 pm

Andrew,
I think I unveiled a flaw in RPolyline::getDistancesFromStart(p)

For a point near a line segment it uses RLine::getDistanceFromStart(p)

For both it is not stated but I suspect that especially the RLine method assumes that the point is on the shape.

While the RPolyline method intentionally allows a point to be within 0.0001 from any segment for some unknown purpose.
Assumed to be a fix for general cases that don't require more than a 3-4 digits accuracy.

Code: Select all

if (segment->getDistanceTo(p)<0.0001) {.. add partial length ..}
Then RLine::getDistanceFromStart(p) calculates the direct distance in 3D from the segment start.

Code: Select all

double ret = startPoint.getDistanceTo(p);
And negates that if it doesn't points somehow in the same direction as towards a projected point on the segment.

In essence the hypotenuse of the triangle: start - point p - projection of p on the unlimited line shape.
:arrow: Not really the distance along the line shape :!: :?:

Because the distance point p - projection is not larger than 0.0001 here ...
... The differences are small but there are some peculiarities.

When point p is a little in front of the first RLine (P4), a positive value is returned meaning that it is beyond the start and on the shape.
In front in any direction because getVectorTo() in limited mode returns a direct vector towards the nearest endpoint.

Same happens beyond the segment (P3) adding more than the segment length as partial length.
meaning that it is beyond the end of the segment and thus on the next segment.

A second distance for the next segment can even be shorter than the former.
e.g. [72.11106081405715 , 72.11092560927979] expecting 72.11102550927978 or the corner vertex.

Visualized it looks like this:
getDistancesFromStart.png
getDistancesFromStart.png (25.38 KiB) Viewed 9662 times

Green are the 0.0001 tolerance areas, exaggerated.
The accounted for partial distance ? along ? the polyline segment are the yellow dotted lines for each points that is near enough.
P2, P3 and P5 are considered to be near both segments.


Also, there are several other pitfalls for this RPolyline method.
Mostly concerning bulging or Arc-like segments or a mix of.

Regards,
CVH

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”