< prev index next >

src/java.desktop/share/classes/sun/java2d/marlin/Curve.java

Print this page




  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.marlin;
  27 
  28 final class Curve {
  29 
  30     float ax, ay, bx, by, cx, cy, dx, dy;
  31     float dax, day, dbx, dby;
  32     // shared iterator instance
  33     private final BreakPtrIterator iterator = new BreakPtrIterator();
  34 
  35     Curve() {
  36     }
  37 
  38     void set(float[] points, int type) {
  39         switch(type) {
  40         case 8:
  41             set(points[0], points[1],
  42                 points[2], points[3],
  43                 points[4], points[5],
  44                 points[6], points[7]);
  45             return;
  46         case 6:
  47             set(points[0], points[1],
  48                 points[2], points[3],
  49                 points[4], points[5]);
  50             return;
  51         default:
  52             throw new InternalError("Curves can only be cubic or quadratic");
  53         }
  54     }
  55 
  56     void set(float x1, float y1,
  57              float x2, float y2,
  58              float x3, float y3,
  59              float x4, float y4)
  60     {
  61         ax = 3f * (x2 - x3) + x4 - x1;
  62         ay = 3f * (y2 - y3) + y4 - y1;
  63         bx = 3f * (x1 - 2f * x2 + x3);
  64         by = 3f * (y1 - 2f * y2 + y3);
  65         cx = 3f * (x2 - x1);
  66         cy = 3f * (y2 - y1);
  67         dx = x1;
  68         dy = y1;
  69         dax = 3f * ax; day = 3f * ay;
  70         dbx = 2f * bx; dby = 2f * by;
  71     }
  72 
  73     void set(float x1, float y1,
  74              float x2, float y2,
  75              float x3, float y3)
  76     {
  77         ax = 0f; ay = 0f;
  78         bx = x1 - 2f * x2 + x3;
  79         by = y1 - 2f * y2 + y3;
  80         cx = 2f * (x2 - x1);
  81         cy = 2f * (y2 - y1);
  82         dx = x1;
  83         dy = y1;
  84         dax = 0f; day = 0f;
  85         dbx = 2f * bx; dby = 2f * by;
  86     }
  87 
  88     float xat(float t) {
  89         return t * (t * (t * ax + bx) + cx) + dx;
  90     }
  91     float yat(float t) {
  92         return t * (t * (t * ay + by) + cy) + dy;
  93     }
  94 
  95     float dxat(float t) {
  96         return t * (t * dax + dbx) + cx;
  97     }
  98 
  99     float dyat(float t) {
 100         return t * (t * day + dby) + cy;
 101     }
 102 
 103     int dxRoots(float[] roots, int off) {
 104         return Helpers.quadraticRoots(dax, dbx, cx, roots, off);
 105     }
 106 
 107     int dyRoots(float[] roots, int off) {
 108         return Helpers.quadraticRoots(day, dby, cy, roots, off);
 109     }
 110 
 111     int infPoints(float[] pts, int off) {
 112         // inflection point at t if -f'(t)x*f''(t)y + f'(t)y*f''(t)x == 0
 113         // Fortunately, this turns out to be quadratic, so there are at
 114         // most 2 inflection points.
 115         final float a = dax * dby - dbx * day;
 116         final float b = 2f * (cy * dax - day * cx);
 117         final float c = cy * dbx - cx * dby;
 118 
 119         return Helpers.quadraticRoots(a, b, c, pts, off);
 120     }
 121 
 122     // finds points where the first and second derivative are
 123     // perpendicular. This happens when g(t) = f'(t)*f''(t) == 0 (where
 124     // * is a dot product). Unfortunately, we have to solve a cubic.
 125     private int perpendiculardfddf(float[] pts, int off) {
 126         assert pts.length >= off + 4;
 127 
 128         // these are the coefficients of some multiple of g(t) (not g(t),
 129         // because the roots of a polynomial are not changed after multiplication
 130         // by a constant, and this way we save a few multiplications).
 131         final float a = 2f * (dax*dax + day*day);
 132         final float b = 3f * (dax*dbx + day*dby);
 133         final float c = 2f * (dax*cx + day*cy) + dbx*dbx + dby*dby;
 134         final float d = dbx*cx + dby*cy;
 135         return Helpers.cubicRootsInAB(a, b, c, d, pts, off, 0f, 1f);
 136     }
 137 
 138     // Tries to find the roots of the function ROC(t)-w in [0, 1). It uses
 139     // a variant of the false position algorithm to find the roots. False
 140     // position requires that 2 initial values x0,x1 be given, and that the
 141     // function must have opposite signs at those values. To find such
 142     // values, we need the local extrema of the ROC function, for which we
 143     // need the roots of its derivative; however, it's harder to find the
 144     // roots of the derivative in this case than it is to find the roots
 145     // of the original function. So, we find all points where this curve's
 146     // first and second derivative are perpendicular, and we pretend these
 147     // are our local extrema. There are at most 3 of these, so we will check
 148     // at most 4 sub-intervals of (0,1). ROC has asymptotes at inflection
 149     // points, so roc-w can have at least 6 roots. This shouldn't be a
 150     // problem for what we're trying to do (draw a nice looking curve).
 151     int rootsOfROCMinusW(float[] roots, int off, final float w, final float err) {
 152         // no OOB exception, because by now off<=6, and roots.length >= 10
 153         assert off <= 6 && roots.length >= 10;
 154         int ret = off;
 155         int numPerpdfddf = perpendiculardfddf(roots, off);
 156         float t0 = 0, ft0 = ROCsq(t0) - w*w;
 157         roots[off + numPerpdfddf] = 1f; // always check interval end points
 158         numPerpdfddf++;
 159         for (int i = off; i < off + numPerpdfddf; i++) {
 160             float t1 = roots[i], ft1 = ROCsq(t1) - w*w;
 161             if (ft0 == 0f) {
 162                 roots[ret++] = t0;
 163             } else if (ft1 * ft0 < 0f) { // have opposite signs
 164                 // (ROC(t)^2 == w^2) == (ROC(t) == w) is true because
 165                 // ROC(t) >= 0 for all t.
 166                 roots[ret++] = falsePositionROCsqMinusX(t0, t1, w*w, err);
 167             }
 168             t0 = t1;
 169             ft0 = ft1;
 170         }
 171 
 172         return ret - off;
 173     }
 174 
 175     private static float eliminateInf(float x) {
 176         return (x == Float.POSITIVE_INFINITY ? Float.MAX_VALUE :
 177             (x == Float.NEGATIVE_INFINITY ? Float.MIN_VALUE : x));
 178     }
 179 
 180     // A slight modification of the false position algorithm on wikipedia.
 181     // This only works for the ROCsq-x functions. It might be nice to have
 182     // the function as an argument, but that would be awkward in java6.
 183     // TODO: It is something to consider for java8 (or whenever lambda


 203                 } else {
 204                     side = -1;
 205                 }
 206             } else if (fr * fs > 0) {
 207                 fs = fr; s = r;
 208                 if (side > 0) {
 209                     ft /= (1 << side);
 210                     side++;
 211                 } else {
 212                     side = 1;
 213                 }
 214             } else {
 215                 break;
 216             }
 217         }
 218         return r;
 219     }
 220 
 221     private static boolean sameSign(float x, float y) {
 222         // another way is to test if x*y > 0. This is bad for small x, y.
 223         return (x < 0f && y < 0f) || (x > 0f && y > 0f);
 224     }
 225 
 226     // returns the radius of curvature squared at t of this curve
 227     // see http://en.wikipedia.org/wiki/Radius_of_curvature_(applications)
 228     private float ROCsq(final float t) {
 229         // dx=xat(t) and dy=yat(t). These calls have been inlined for efficiency
 230         final float dx = t * (t * dax + dbx) + cx;
 231         final float dy = t * (t * day + dby) + cy;
 232         final float ddx = 2f * dax * t + dbx;
 233         final float ddy = 2f * day * t + dby;
 234         final float dx2dy2 = dx*dx + dy*dy;
 235         final float ddx2ddy2 = ddx*ddx + ddy*ddy;
 236         final float ddxdxddydy = ddx*dx + ddy*dy;
 237         return dx2dy2*((dx2dy2*dx2dy2) / (dx2dy2 * ddx2ddy2 - ddxdxddydy*ddxdxddydy));
 238     }
 239 
 240     // curve to be broken should be in pts
 241     // this will change the contents of pts but not Ts
 242     // TODO: There's no reason for Ts to be an array. All we need is a sequence
 243     // of t values at which to subdivide. An array statisfies this condition,
 244     // but is unnecessarily restrictive. Ts should be an Iterator<Float> instead.
 245     // Doing this will also make dashing easier, since we could easily make
 246     // LengthIterator an Iterator<Float> and feed it to this function to simplify
 247     // the loop in Dasher.somethingTo.
 248     BreakPtrIterator breakPtsAtTs(final float[] pts, final int type,
 249                                   final float[] Ts, final int numTs)
 250     {
 251         assert pts.length >= 2*type && numTs <= Ts.length;
 252 
 253         // initialize shared iterator:
 254         iterator.init(pts, type, Ts, numTs);
 255 
 256         return iterator;
 257     }
 258 
 259     static final class BreakPtrIterator {
 260         private int nextCurveIdx;
 261         private int curCurveOff;
 262         private float prevT;
 263         private float[] pts;
 264         private int type;
 265         private float[] ts;
 266         private int numTs;
 267 
 268         void init(final float[] pts, final int type,
 269                   final float[] ts, final int numTs) {
 270             this.pts = pts;
 271             this.type = type;
 272             this.ts = ts;
 273             this.numTs = numTs;
 274 
 275             nextCurveIdx = 0;
 276             curCurveOff = 0;
 277             prevT = 0f;
 278         }
 279 
 280         public boolean hasNext() {
 281             return nextCurveIdx <= numTs;
 282         }
 283 
 284         public int next() {
 285             int ret;
 286             if (nextCurveIdx < numTs) {
 287                 float curT = ts[nextCurveIdx];
 288                 float splitT = (curT - prevT) / (1f - prevT);
 289                 Helpers.subdivideAt(splitT,
 290                                     pts, curCurveOff,
 291                                     pts, 0,
 292                                     pts, type, type);
 293                 prevT = curT;
 294                 ret = 0;
 295                 curCurveOff = type;
 296             } else {
 297                 ret = curCurveOff;
 298             }
 299             nextCurveIdx++;
 300             return ret;
 301         }
 302     }
 303 }
 304 


  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.marlin;
  27 
  28 final class Curve {
  29 
  30     float ax, ay, bx, by, cx, cy, dx, dy;
  31     float dax, day, dbx, dby;


  32 
  33     Curve() {
  34     }
  35 
  36     void set(float[] points, int type) {
  37         switch(type) {
  38         case 8:
  39             set(points[0], points[1],
  40                 points[2], points[3],
  41                 points[4], points[5],
  42                 points[6], points[7]);
  43             return;
  44         case 6:
  45             set(points[0], points[1],
  46                 points[2], points[3],
  47                 points[4], points[5]);
  48             return;
  49         default:
  50             throw new InternalError("Curves can only be cubic or quadratic");
  51         }
  52     }
  53 
  54     void set(float x1, float y1,
  55              float x2, float y2,
  56              float x3, float y3,
  57              float x4, float y4)
  58     {
  59         ax = 3.0f * (x2 - x3) + x4 - x1;
  60         ay = 3.0f * (y2 - y3) + y4 - y1;
  61         bx = 3.0f * (x1 - 2.0f * x2 + x3);
  62         by = 3.0f * (y1 - 2.0f * y2 + y3);
  63         cx = 3.0f * (x2 - x1);
  64         cy = 3.0f * (y2 - y1);
  65         dx = x1;
  66         dy = y1;
  67         dax = 3.0f * ax; day = 3.0f * ay;
  68         dbx = 2.0f * bx; dby = 2.0f * by;
  69     }
  70 
  71     void set(float x1, float y1,
  72              float x2, float y2,
  73              float x3, float y3)
  74     {
  75         ax = 0.0f; ay = 0.0f;
  76         bx = x1 - 2.0f * x2 + x3;
  77         by = y1 - 2.0f * y2 + y3;
  78         cx = 2.0f * (x2 - x1);
  79         cy = 2.0f * (y2 - y1);
  80         dx = x1;
  81         dy = y1;
  82         dax = 0.0f; day = 0.0f;
  83         dbx = 2.0f * bx; dby = 2.0f * by;
  84     }
  85 
  86     float xat(float t) {
  87         return t * (t * (t * ax + bx) + cx) + dx;
  88     }
  89     float yat(float t) {
  90         return t * (t * (t * ay + by) + cy) + dy;
  91     }
  92 
  93     float dxat(float t) {
  94         return t * (t * dax + dbx) + cx;
  95     }
  96 
  97     float dyat(float t) {
  98         return t * (t * day + dby) + cy;
  99     }
 100 
 101     int dxRoots(float[] roots, int off) {
 102         return Helpers.quadraticRoots(dax, dbx, cx, roots, off);
 103     }
 104 
 105     int dyRoots(float[] roots, int off) {
 106         return Helpers.quadraticRoots(day, dby, cy, roots, off);
 107     }
 108 
 109     int infPoints(float[] pts, int off) {
 110         // inflection point at t if -f'(t)x*f''(t)y + f'(t)y*f''(t)x == 0
 111         // Fortunately, this turns out to be quadratic, so there are at
 112         // most 2 inflection points.
 113         final float a = dax * dby - dbx * day;
 114         final float b = 2.0f * (cy * dax - day * cx);
 115         final float c = cy * dbx - cx * dby;
 116 
 117         return Helpers.quadraticRoots(a, b, c, pts, off);
 118     }
 119 
 120     // finds points where the first and second derivative are
 121     // perpendicular. This happens when g(t) = f'(t)*f''(t) == 0 (where
 122     // * is a dot product). Unfortunately, we have to solve a cubic.
 123     private int perpendiculardfddf(float[] pts, int off) {
 124         assert pts.length >= off + 4;
 125 
 126         // these are the coefficients of some multiple of g(t) (not g(t),
 127         // because the roots of a polynomial are not changed after multiplication
 128         // by a constant, and this way we save a few multiplications).
 129         final float a = 2.0f * (dax*dax + day*day);
 130         final float b = 3.0f * (dax*dbx + day*dby);
 131         final float c = 2.0f * (dax*cx + day*cy) + dbx*dbx + dby*dby;
 132         final float d = dbx*cx + dby*cy;
 133         return Helpers.cubicRootsInAB(a, b, c, d, pts, off, 0.0f, 1.0f);
 134     }
 135 
 136     // Tries to find the roots of the function ROC(t)-w in [0, 1). It uses
 137     // a variant of the false position algorithm to find the roots. False
 138     // position requires that 2 initial values x0,x1 be given, and that the
 139     // function must have opposite signs at those values. To find such
 140     // values, we need the local extrema of the ROC function, for which we
 141     // need the roots of its derivative; however, it's harder to find the
 142     // roots of the derivative in this case than it is to find the roots
 143     // of the original function. So, we find all points where this curve's
 144     // first and second derivative are perpendicular, and we pretend these
 145     // are our local extrema. There are at most 3 of these, so we will check
 146     // at most 4 sub-intervals of (0,1). ROC has asymptotes at inflection
 147     // points, so roc-w can have at least 6 roots. This shouldn't be a
 148     // problem for what we're trying to do (draw a nice looking curve).
 149     int rootsOfROCMinusW(float[] roots, int off, final float w, final float err) {
 150         // no OOB exception, because by now off<=6, and roots.length >= 10
 151         assert off <= 6 && roots.length >= 10;
 152         int ret = off;
 153         int numPerpdfddf = perpendiculardfddf(roots, off);
 154         float t0 = 0.0f, ft0 = ROCsq(t0) - w*w;
 155         roots[off + numPerpdfddf] = 1.0f; // always check interval end points
 156         numPerpdfddf++;
 157         for (int i = off; i < off + numPerpdfddf; i++) {
 158             float t1 = roots[i], ft1 = ROCsq(t1) - w*w;
 159             if (ft0 == 0.0f) {
 160                 roots[ret++] = t0;
 161             } else if (ft1 * ft0 < 0.0f) { // have opposite signs
 162                 // (ROC(t)^2 == w^2) == (ROC(t) == w) is true because
 163                 // ROC(t) >= 0 for all t.
 164                 roots[ret++] = falsePositionROCsqMinusX(t0, t1, w*w, err);
 165             }
 166             t0 = t1;
 167             ft0 = ft1;
 168         }
 169 
 170         return ret - off;
 171     }
 172 
 173     private static float eliminateInf(float x) {
 174         return (x == Float.POSITIVE_INFINITY ? Float.MAX_VALUE :
 175             (x == Float.NEGATIVE_INFINITY ? Float.MIN_VALUE : x));
 176     }
 177 
 178     // A slight modification of the false position algorithm on wikipedia.
 179     // This only works for the ROCsq-x functions. It might be nice to have
 180     // the function as an argument, but that would be awkward in java6.
 181     // TODO: It is something to consider for java8 (or whenever lambda


 201                 } else {
 202                     side = -1;
 203                 }
 204             } else if (fr * fs > 0) {
 205                 fs = fr; s = r;
 206                 if (side > 0) {
 207                     ft /= (1 << side);
 208                     side++;
 209                 } else {
 210                     side = 1;
 211                 }
 212             } else {
 213                 break;
 214             }
 215         }
 216         return r;
 217     }
 218 
 219     private static boolean sameSign(float x, float y) {
 220         // another way is to test if x*y > 0. This is bad for small x, y.
 221         return (x < 0.0f && y < 0.0f) || (x > 0.0f && y > 0.0f);
 222     }
 223 
 224     // returns the radius of curvature squared at t of this curve
 225     // see http://en.wikipedia.org/wiki/Radius_of_curvature_(applications)
 226     private float ROCsq(final float t) {
 227         // dx=xat(t) and dy=yat(t). These calls have been inlined for efficiency
 228         final float dx = t * (t * dax + dbx) + cx;
 229         final float dy = t * (t * day + dby) + cy;
 230         final float ddx = 2.0f * dax * t + dbx;
 231         final float ddy = 2.0f * day * t + dby;
 232         final float dx2dy2 = dx*dx + dy*dy;
 233         final float ddx2ddy2 = ddx*ddx + ddy*ddy;
 234         final float ddxdxddydy = ddx*dx + ddy*dy;
 235         return dx2dy2*((dx2dy2*dx2dy2) / (dx2dy2 * ddx2ddy2 - ddxdxddydy*ddxdxddydy));
































































 236     }
 237 }
 238 
< prev index next >