Repository navigation
Some bézier clipping algorithm improvements - #427
Conversation
…ithm. The main improvements here are: * greatly reduced failure rates when one curve reduces to a point (that is, we now return many more actual intersections that we used to discard); * improvements on the rate of occurrence of large distances between curves sampled at returned intersection pairs.
|
A reminder of where I left off last time: I noticed that we were getting a lot of bézier clipping reducing one of the curves to a point, meaning there's a potential intersection at that point, and that we were throwing away a lot of those potential intersections because the way I handled that case was to sample distances from the point to the other curve: if the smallest sample was small enough we concluded intersection, otherwise we returned no intersection. On a run of one million pairs of random curves, we were throwing away ~20_000 actual intersections if the inputs were f64s, and ~300_000 actual intersections if the inputs were f32s (I think in both cases the random coefficients were between -100_000 and 100_000 for those numbers). (Oh yeah: why are so many curves reducing to a point? From what I recall, what I was seeing was that the fat line wasn't clipping us down to a point (though that can definitely happen), but it was clipping down to a small interval of 0..1, and then that small interval was translated to what was maybe already quite a small domain for a subcurve of the original curve, at which point rounding reduced the small subdomain of the small subdomain to a single point, particularly for f32s.) I made two changes from there.
That has the added benefit that we can now accept points in the api, i.e. we can return whether or not a point lies on a cubic curve. [Honestly though I'd be okay with not exposing that functionality if you don't think there's a real need for it. Right now that code is very general: for example it has to handle the case where the point is on a self intersection of the curve (in which case it has to return two or three parameters), but in the bézier clipping case the curve we're looking at is almost always (but not always) pretty close to linear and basically never self intersects or does anything goofy. So down the road there might be room for more optimizations for the clipping case if we don't also have to handle the general case at the same time.] That seems to work well: we still have to make a judgement on whether a potential intersection we find samples close enough to be considered a real intersection, but of the cases I looked at where we discard for being too far away, all were legit non-intersections: in all those cases we reduced to a point because some part of the previous curve (either an endpoint or a control point) landed on the fat line of the other curve (which is why it clipped to a point), but not on the other curve itself (which is why it's not an actual intersection). (That scenario will come up again later.) |
|
Epsilons I mentioned trying epsilon = 1e-7 for f32s last time, but then I noted that f32.EPSILON = 1.19209290e-07_f32 (the "machine epsilon"), so asking for two numbers between 0.0 and 1.0 to be less than 1e-7 seems like a bad idea. So I tried epsilon = 1e-6, epsilon = 5e-6, and epsilon = 1e-5. Each gave a different rate at which we got the bogus non-intersections that were actually intersections - the other thing it changed is the precision of our returned intersection pairs: the larger you allow the subcurve domains to be when you declare intersection, the more latitude you have for getting the exact location of the intersection parameters wrong. I checked for that error by looking at At f32:1e-5, 9_465 intersections with d >= 1 (2 with d > 10, max 512), 79 intersections missed due to roundoff errors [The max d's there are a little misleading: I later did some work to improve those, so they should be smaller now - I'll say more about that later.] I have less to say about the choice of epsilon for f64s because the f64 behavior has always seemed much more stable (which I guess isn't a surprise) - the f32 case has more known errors, breaks its epsilon bounds more readily, seems fussier and less stable, has more perfectly reasonable looking curves that fail for some weird reason, etc. I wrote a benchmark to try to give some idea of a) how much speed improvement we get with using f32s, and b) how much I've "regressed" (I use quotes because my previous submission was kinda broken already) by adding more processing. [Bench was just the first rust thing that came up when I looked for this stuff - if there's a better way to do what I'm describing here, I'd be happy to hear about it; also I can put my bench code somewhere if you'd like to see it.] The bench takes a fixed sequence of 10_000 curve pairs with random coefficients between -100_000 and 100_000, and just runs the bézier clipping algorithm on each pair, once as f32s, once as f64s (the random numbers were generated as f32s). Here's the diff between what I submitted last time and what I'm submitting this time: |
|
More known issues Fat line intersections missed due to rounding and other errors There are several ways in which the fat line intersection check can fail:
[Interestingly if I use LineEquation::signed_distance_to_point instead of Line::signed_distance_to_point, even more intersections disappear. signed_distance_to_point is also an issue with f64s, so it might be worth looking into whether its accuracy can be improved (note that it's used in the fat line bounds values as well, so is the source of both errors in the third point above).] f64 So for f64s, there are probably less than ~three of these errors per million pair curves (I got two such errors out of 2 million pairs with coefficients in +/-100_000). Intersection samples distance errors sqrt(1/2) ~ 0.707 is the max size of the error we allow when we conclude an intersection from a point/curve - all d's larger than that had to come from the case where both subcurve domains reduced to non-points of length < epsilon (i.e. where the algorithm completed normally, as originally designed, with neither curve reducing to a point). As noted, none of those large distances indicated an error, it was just a consequence of the inaccuracy of the algorithm at the given epsilon (such as when we're trying to find the exact intersection parameters of two curves that intersect very shallowly (though that doesn't seem to always be the cause)). f64 Also see Another Interesting Example below for another source of known error - it seems to occur at a rate of about 1 per million random f32 pair curves, but could be more prevalent for curves of a certain shape relation. |
|
Stability part two |
|
Odds and ends
|
|
r? @nical |
|
FYI: I'm once again a day slow on this... but: I ran a test where I computed intersections for a million curve pairs first as f32s, and then as f64s, and compared the intersections counts: With the previous version of the algorithm, I get 129_280 mismatches with 502 where more f32 intersections were found than f64 intersections. [I expect in this case that there are more where both the f32 and the f64 count were low, so were wrong even if they didn't show up in this test as a mismatch (and of course there could still be more of those in the current code as well!).] So this PR still seems like a big improvement, but there are still some types of error that I'm either not aware of or was previously miscounting. |
We can change the naming later, I'm not entirely sure whether I prefer
Nice, if we can avoid the need for a precision parameter it's great. It's a bit of a last resort solution when there isn't a good default for the algorithm.
Yes indeed, nice work! |
|
Another no-action-taken report:
This was in reference to how we handle the case where one of the curves reduces to a point - how do we tell if the point actually intersects the other curve? Previously we were sampling the curve to see if it was close to the point; this PR added new processing as described in the quote. I tried two changes to that: 1) I got rid of the sampling all together. That turned out a bit slower (according to my bench), so I also 2) got rid of the last check in I then ran 5_000_000 curve pairs with random coefficients between -100_000 and 100_000 and compared the number of intersections for each pair of curves with and without those changes. The code found exactly the same number of intersections with and without those changes, for both f32 and f64 curves (which suggests that the new point processing is working pretty well, at least for the types of curves seen deep in the clipping algorithm). The bench showed a slight slowdown for f32 curves - from 24,493,001 to 24,754,269 - and a slight speedup for f64 curves - from 28,575,400 to 28,363,437. Since the only reason we're keeping the f32 processing is for the speedup vs. f64 (and since sampling is a pretty simple straightforward operation), I guess it makes since to just keep the sampling for now. Let me know if you have other thoughts though :-) |
Actually thinking about it for another second, in general the clipping algorithm reaches that point very rarely (usually, but not always, when a curve reduces to a point, you've found an intersection) - I checked and for the 10_000 pairs of curves in my bench, that code is never reached. Getting rid of the check would mostly just speed up the case where we're testing a point that doesn't lie on a curve. |



No description provided.