1 /*
   2  * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  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
 184     // expressions make it into the language), depending on how closures
 185     // and turn out. Same goes for the newton's method
 186     // algorithm in Helpers.java
 187     private float falsePositionROCsqMinusX(float x0, float x1,
 188                                            final float x, final float err)
 189     {
 190         final int iterLimit = 100;
 191         int side = 0;
 192         float t = x1, ft = eliminateInf(ROCsq(t) - x);
 193         float s = x0, fs = eliminateInf(ROCsq(s) - x);
 194         float r = s, fr;
 195         for (int i = 0; i < iterLimit && Math.abs(t - s) > err * Math.abs(t + s); i++) {
 196             r = (fs * t - ft * s) / (fs - ft);
 197             fr = ROCsq(r) - x;
 198             if (sameSign(fr, ft)) {
 199                 ft = fr; t = r;
 200                 if (side < 0) {
 201                     fs /= (1 << (-side));
 202                     side--;
 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 
--- EOF ---