05-19-2021, 12:13 PM
(05-19-2021, 07:46 AM)Ottia Tuota Wrote: (I am not quite sure if I understand your comments quite right.)
No I have not thought about P(t)=K. But I may some day. And yes there is that fact that the stroke consists of several small arcs, and we cannot tell even on which arc the most distant point K is.
I have the feeling that when searching for an approximate Bezier arc it is important to choose K somewhere close to the middle of the stroke, and I think that the most distant point works well. Also, the splitting is good to do at the same point so that the stroke will never be split anywhere close to either end point but somewhere in the middle. That is why the point K is computed rather carefully.
Finding K is not done by running along the stroke. It is (usually) done by solving the points where the tangent is parallel to the chord. That comes down to solving equations of degree 2. So it should be fast enough.
I did not know about getPointAtDist and I don't find how to call it. But I found now that a stroke has a method get_point_at_dist(). It is the same I suppose.
Yes that error measure is the trouble spot. It should be made faster before even thinking to try to make the code faster elsewhere. But we are measuring distance between something like a stroke and its candidate approximate Bezier arc. There is no guarantee that the curves are close to each other anywhere else but the three points p0,p3,K. The two curves may not run side by side, and the stroke can contain small and large bends. The measure must take into account the whole lengths of the curves somehow; one point is not enough. But anyhow, I am sure the code could be made better with some better idea. This version is working all right, and for an experimental plugin it will do so far.
As a small entertainment I mention that some years ago my Parametric curves plugin (some old version) used as the error measure the area between two curves. The area was computed by summing a large number of narrow triangles. Well, no sense in going back to that solution...
My long practice of programming says that the best algorithm is one that work fast in the most frequent cases, but doesn't fail (or has a fallback) for the pathological cases even if that makes it run much slower for these. So any shortcut is OK if you can evaluate how good/bad the result is.