jdk/src/share/classes/sun/java2d/pisces/Curve.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8131 e2932d8114cb
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8131
diff changeset
     2
 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
     4
 *
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    10
 *
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    15
 * accompanied this code).
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    16
 *
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    20
 *
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    23
 * questions.
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    24
 */
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    25
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    26
package sun.java2d.pisces;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    27
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    28
import java.util.Iterator;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    29
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
    30
final class Curve {
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    31
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    32
    float ax, ay, bx, by, cx, cy, dx, dy;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    33
    float dax, day, dbx, dby;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    34
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    35
    Curve() {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    36
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    37
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    38
    void set(float[] points, int type) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    39
        switch(type) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    40
        case 8:
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    41
            set(points[0], points[1],
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    42
                points[2], points[3],
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    43
                points[4], points[5],
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    44
                points[6], points[7]);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    45
            break;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    46
        case 6:
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    47
            set(points[0], points[1],
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    48
                points[2], points[3],
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    49
                points[4], points[5]);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    50
            break;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    51
        default:
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    52
            throw new InternalError("Curves can only be cubic or quadratic");
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    53
        }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    54
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    55
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    56
    void set(float x1, float y1,
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    57
             float x2, float y2,
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    58
             float x3, float y3,
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    59
             float x4, float y4)
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    60
    {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    61
        ax = 3 * (x2 - x3) + x4 - x1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    62
        ay = 3 * (y2 - y3) + y4 - y1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    63
        bx = 3 * (x1 - 2 * x2 + x3);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    64
        by = 3 * (y1 - 2 * y2 + y3);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    65
        cx = 3 * (x2 - x1);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    66
        cy = 3 * (y2 - y1);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    67
        dx = x1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    68
        dy = y1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    69
        dax = 3 * ax; day = 3 * ay;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    70
        dbx = 2 * bx; dby = 2 * by;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    71
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    72
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    73
    void set(float x1, float y1,
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    74
             float x2, float y2,
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    75
             float x3, float y3)
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    76
    {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    77
        ax = ay = 0f;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    78
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    79
        bx = x1 - 2 * x2 + x3;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    80
        by = y1 - 2 * y2 + y3;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    81
        cx = 2 * (x2 - x1);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    82
        cy = 2 * (y2 - y1);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    83
        dx = x1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    84
        dy = y1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    85
        dax = 0; day = 0;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    86
        dbx = 2 * bx; dby = 2 * by;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    87
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    88
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    89
    float xat(float t) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    90
        return t * (t * (t * ax + bx) + cx) + dx;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    91
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    92
    float yat(float t) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    93
        return t * (t * (t * ay + by) + cy) + dy;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    94
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    95
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    96
    float dxat(float t) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    97
        return t * (t * dax + dbx) + cx;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    98
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
    99
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   100
    float dyat(float t) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   101
        return t * (t * day + dby) + cy;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   102
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   103
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   104
    int dxRoots(float[] roots, int off) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   105
        return Helpers.quadraticRoots(dax, dbx, cx, roots, off);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   106
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   107
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   108
    int dyRoots(float[] roots, int off) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   109
        return Helpers.quadraticRoots(day, dby, cy, roots, off);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   110
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   111
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   112
    int infPoints(float[] pts, int off) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   113
        // inflection point at t if -f'(t)x*f''(t)y + f'(t)y*f''(t)x == 0
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   114
        // Fortunately, this turns out to be quadratic, so there are at
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   115
        // most 2 inflection points.
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   116
        final float a = dax * dby - dbx * day;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   117
        final float b = 2 * (cy * dax - day * cx);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   118
        final float c = cy * dbx - cx * dby;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   119
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   120
        return Helpers.quadraticRoots(a, b, c, pts, off);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   121
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   122
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   123
    // finds points where the first and second derivative are
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   124
    // perpendicular. This happens when g(t) = f'(t)*f''(t) == 0 (where
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   125
    // * is a dot product). Unfortunately, we have to solve a cubic.
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   126
    private int perpendiculardfddf(float[] pts, int off) {
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   127
        assert pts.length >= off + 4;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   128
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   129
        // these are the coefficients of some multiple of g(t) (not g(t),
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   130
        // because the roots of a polynomial are not changed after multiplication
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   131
        // by a constant, and this way we save a few multiplications).
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   132
        final float a = 2*(dax*dax + day*day);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   133
        final float b = 3*(dax*dbx + day*dby);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   134
        final float c = 2*(dax*cx + day*cy) + dbx*dbx + dby*dby;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   135
        final float d = dbx*cx + dby*cy;
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   136
        return Helpers.cubicRootsInAB(a, b, c, d, pts, off, 0f, 1f);
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   137
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   138
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   139
    // Tries to find the roots of the function ROC(t)-w in [0, 1). It uses
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   140
    // a variant of the false position algorithm to find the roots. False
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   141
    // position requires that 2 initial values x0,x1 be given, and that the
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   142
    // function must have opposite signs at those values. To find such
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   143
    // values, we need the local extrema of the ROC function, for which we
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   144
    // need the roots of its derivative; however, it's harder to find the
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   145
    // roots of the derivative in this case than it is to find the roots
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   146
    // of the original function. So, we find all points where this curve's
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   147
    // first and second derivative are perpendicular, and we pretend these
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   148
    // are our local extrema. There are at most 3 of these, so we will check
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   149
    // at most 4 sub-intervals of (0,1). ROC has asymptotes at inflection
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   150
    // points, so roc-w can have at least 6 roots. This shouldn't be a
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   151
    // problem for what we're trying to do (draw a nice looking curve).
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   152
    int rootsOfROCMinusW(float[] roots, int off, final float w, final float err) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   153
        // no OOB exception, because by now off<=6, and roots.length >= 10
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   154
        assert off <= 6 && roots.length >= 10;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   155
        int ret = off;
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   156
        int numPerpdfddf = perpendiculardfddf(roots, off);
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   157
        float t0 = 0, ft0 = ROCsq(t0) - w*w;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   158
        roots[off + numPerpdfddf] = 1f; // always check interval end points
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   159
        numPerpdfddf++;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   160
        for (int i = off; i < off + numPerpdfddf; i++) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   161
            float t1 = roots[i], ft1 = ROCsq(t1) - w*w;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   162
            if (ft0 == 0f) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   163
                roots[ret++] = t0;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   164
            } else if (ft1 * ft0 < 0f) { // have opposite signs
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   165
                // (ROC(t)^2 == w^2) == (ROC(t) == w) is true because
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   166
                // ROC(t) >= 0 for all t.
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   167
                roots[ret++] = falsePositionROCsqMinusX(t0, t1, w*w, err);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   168
            }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   169
            t0 = t1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   170
            ft0 = ft1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   171
        }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   172
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   173
        return ret - off;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   174
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   175
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   176
    private static float eliminateInf(float x) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   177
        return (x == Float.POSITIVE_INFINITY ? Float.MAX_VALUE :
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   178
            (x == Float.NEGATIVE_INFINITY ? Float.MIN_VALUE : x));
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   179
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   180
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   181
    // A slight modification of the false position algorithm on wikipedia.
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   182
    // This only works for the ROCsq-x functions. It might be nice to have
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   183
    // the function as an argument, but that would be awkward in java6.
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   184
    // TODO: It is something to consider for java8 (or whenever lambda
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   185
    // expressions make it into the language), depending on how closures
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   186
    // and turn out. Same goes for the newton's method
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   187
    // algorithm in Helpers.java
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   188
    private float falsePositionROCsqMinusX(float x0, float x1,
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   189
                                           final float x, final float err)
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   190
    {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   191
        final int iterLimit = 100;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   192
        int side = 0;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   193
        float t = x1, ft = eliminateInf(ROCsq(t) - x);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   194
        float s = x0, fs = eliminateInf(ROCsq(s) - x);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   195
        float r = s, fr;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   196
        for (int i = 0; i < iterLimit && Math.abs(t - s) > err * Math.abs(t + s); i++) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   197
            r = (fs * t - ft * s) / (fs - ft);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   198
            fr = ROCsq(r) - x;
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   199
            if (sameSign(fr, ft)) {
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   200
                ft = fr; t = r;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   201
                if (side < 0) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   202
                    fs /= (1 << (-side));
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   203
                    side--;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   204
                } else {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   205
                    side = -1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   206
                }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   207
            } else if (fr * fs > 0) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   208
                fs = fr; s = r;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   209
                if (side > 0) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   210
                    ft /= (1 << side);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   211
                    side++;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   212
                } else {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   213
                    side = 1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   214
                }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   215
            } else {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   216
                break;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   217
            }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   218
        }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   219
        return r;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   220
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   221
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   222
    private static boolean sameSign(double x, double y) {
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   223
        // another way is to test if x*y > 0. This is bad for small x, y.
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   224
        return (x < 0 && y < 0) || (x > 0 && y > 0);
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   225
    }
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   226
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   227
    // returns the radius of curvature squared at t of this curve
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   228
    // see http://en.wikipedia.org/wiki/Radius_of_curvature_(applications)
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   229
    private float ROCsq(final float t) {
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   230
        // dx=xat(t) and dy=yat(t). These calls have been inlined for efficiency
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   231
        final float dx = t * (t * dax + dbx) + cx;
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   232
        final float dy = t * (t * day + dby) + cy;
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   233
        final float ddx = 2 * dax * t + dbx;
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   234
        final float ddy = 2 * day * t + dby;
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   235
        final float dx2dy2 = dx*dx + dy*dy;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   236
        final float ddx2ddy2 = ddx*ddx + ddy*ddy;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   237
        final float ddxdxddydy = ddx*dx + ddy*dy;
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   238
        return dx2dy2*((dx2dy2*dx2dy2) / (dx2dy2 * ddx2ddy2 - ddxdxddydy*ddxdxddydy));
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   239
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   240
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   241
    // curve to be broken should be in pts
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   242
    // this will change the contents of pts but not Ts
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   243
    // TODO: There's no reason for Ts to be an array. All we need is a sequence
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   244
    // of t values at which to subdivide. An array statisfies this condition,
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   245
    // but is unnecessarily restrictive. Ts should be an Iterator<Float> instead.
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   246
    // Doing this will also make dashing easier, since we could easily make
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   247
    // LengthIterator an Iterator<Float> and feed it to this function to simplify
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   248
    // the loop in Dasher.somethingTo.
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   249
    static Iterator<Integer> breakPtsAtTs(final float[] pts, final int type,
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   250
                                          final float[] Ts, final int numTs)
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   251
    {
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   252
        assert pts.length >= 2*type && numTs <= Ts.length;
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   253
        return new Iterator<Integer>() {
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   254
            // these prevent object creation and destruction during autoboxing.
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   255
            // Because of this, the compiler should be able to completely
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   256
            // eliminate the boxing costs.
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   257
            final Integer i0 = 0;
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   258
            final Integer itype = type;
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   259
            int nextCurveIdx = 0;
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   260
            Integer curCurveOff = i0;
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   261
            float prevT = 0;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   262
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   263
            @Override public boolean hasNext() {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   264
                return nextCurveIdx < numTs + 1;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   265
            }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   266
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   267
            @Override public Integer next() {
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   268
                Integer ret;
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   269
                if (nextCurveIdx < numTs) {
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   270
                    float curT = Ts[nextCurveIdx];
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   271
                    float splitT = (curT - prevT) / (1 - prevT);
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   272
                    Helpers.subdivideAt(splitT,
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   273
                                        pts, curCurveOff,
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   274
                                        pts, 0,
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   275
                                        pts, type, type);
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   276
                    prevT = curT;
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   277
                    ret = i0;
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   278
                    curCurveOff = itype;
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   279
                } else {
8131
e2932d8114cb 7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents: 7668
diff changeset
   280
                    ret = curCurveOff;
6997
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   281
                }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   282
                nextCurveIdx++;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   283
                return ret;
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   284
            }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   285
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   286
            @Override public void remove() {}
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   287
        };
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   288
    }
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   289
}
3642614e2282 6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
diff changeset
   290