On Fri, 5 Nov 2021 04:41:34 GMT, Jeremy <d...@openjdk.java.net> wrote:
>> This removes code that relied on consulting the Bezier control points to >> calculate the Rectangle2D bounding box. Instead it's pretty straight-forward >> to convert the Bezier control points into the x & y parametric equations. At >> their most complex these equations are cubic polynomials, so calculating >> their extrema is just a matter of applying the quadratic formula to >> calculate their extrema. (Or in path segments that are >> quadratic/linear/constant: we do even less work.) >> >> The bug writeup indicated they wanted Path2D#getBounds2D() to be more >> accurate/concise. They didn't explicitly say they wanted CubicCurve2D and >> QuadCurve2D to become more accurate too. But a preexisting unit test failed >> when Path2D#getBounds2D() was updated and those other classes weren't. At >> this point I considered either: >> A. Updating CubicCurve2D and QuadCurve2D to use the new more accurate >> getBounds2D() or >> B. Updating the unit test to forgive the discrepancy. >> >> I chose A. Which might technically be seen as scope creep, but it feels like >> a more holistic/better approach. >> >> Other shapes in java.awt.geom should not require updating, because they >> already identify concise bounds. >> >> This also includes a new unit test (in Path2D/UnitTest.java) that fails >> without the changes in this commit. > > Jeremy has updated the pull request incrementally with one additional commit > since the last revision: > > 8176501: Method Shape.getBounds2D() incorrectly includes Bezier control > points in bounding box > > Addressing some of Laurent's code review recommendations/comments: > > 1. use the convention t for the parametric variable x(t),y(t) > 2. solve the quadratic equation using QuadCurve2d.solveQuadratic() or like > Helpers.quadraticRoots() > 3. always use braces for x = (a < b) ? ... > 4. always use double-precision constants in math or logical operations: (2 > * x => 2.0 * x) and (coefficients[3] != 0) => (coefficients[3] != 0.0) > > (There are two additional recommendations not in this commit that I'll ask > about shortly.) > > See https://github.com/openjdk/jdk/pull/6227#issuecomment-959757954 First comments, I will review carefully the code changes later: A. You should compute directly the curve derivatives ie da, db and const coefficients only, like my curve impl. Then the function findExtrema() becomes useless as QuadCurve2d.solveQuadratic() handles all cases (2nd & 1th order polynoms). So your algo is: - check endpoint bounds - if line: done - If cubic/quad: - get dx dy coeffs - get roots - check roots in ]0 - 1 [, then compute point (cubic or quad), check point bounds B. As you pointed out, degenerated cases are causing problems as computer precision is limited ~ 10^-15 x magnitude in double-precision ! If magnitude is 10^20, the ulp ~ 100 000 ! Solving roots and computing coeffs and points may give numerical errors ~ few ulps, so it may be necessary to refine roots... I will check later numerical accuracy issues and how to minimize them. ------------- PR: https://git.openjdk.java.net/jdk/pull/6227