Path: blob/master/src/java.desktop/share/classes/sun/java2d/marlin/Curve.java
41159 views
/*1* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.marlin;2627final class Curve {2829double ax, ay, bx, by, cx, cy, dx, dy;30double dax, day, dbx, dby;3132Curve() {33}3435void set(final double[] points, final int type) {36// if instead of switch (perf + most probable cases first)37if (type == 8) {38set(points[0], points[1],39points[2], points[3],40points[4], points[5],41points[6], points[7]);42} else if (type == 4) {43set(points[0], points[1],44points[2], points[3]);45} else {46set(points[0], points[1],47points[2], points[3],48points[4], points[5]);49}50}5152void set(final double x1, final double y1,53final double x2, final double y2,54final double x3, final double y3,55final double x4, final double y4)56{57final double dx32 = 3.0d * (x3 - x2);58final double dy32 = 3.0d * (y3 - y2);59final double dx21 = 3.0d * (x2 - x1);60final double dy21 = 3.0d * (y2 - y1);61ax = (x4 - x1) - dx32; // A = P3 - P0 - 3 (P2 - P1) = (P3 - P0) + 3 (P1 - P2)62ay = (y4 - y1) - dy32;63bx = (dx32 - dx21); // B = 3 (P2 - P1) - 3(P1 - P0) = 3 (P2 + P0) - 6 P164by = (dy32 - dy21);65cx = dx21; // C = 3 (P1 - P0)66cy = dy21;67dx = x1; // D = P068dy = y1;69dax = 3.0d * ax;70day = 3.0d * ay;71dbx = 2.0d * bx;72dby = 2.0d * by;73}7475void set(final double x1, final double y1,76final double x2, final double y2,77final double x3, final double y3)78{79final double dx21 = (x2 - x1);80final double dy21 = (y2 - y1);81ax = 0.0d; // A = 082ay = 0.0d;83bx = (x3 - x2) - dx21; // B = P3 - P0 - 2 P284by = (y3 - y2) - dy21;85cx = 2.0d * dx21; // C = 2 (P2 - P1)86cy = 2.0d * dy21;87dx = x1; // D = P188dy = y1;89dax = 0.0d;90day = 0.0d;91dbx = 2.0d * bx;92dby = 2.0d * by;93}9495void set(final double x1, final double y1,96final double x2, final double y2)97{98final double dx21 = (x2 - x1);99final double dy21 = (y2 - y1);100ax = 0.0d; // A = 0101ay = 0.0d;102bx = 0.0d; // B = 0103by = 0.0d;104cx = dx21; // C = (P2 - P1)105cy = dy21;106dx = x1; // D = P1107dy = y1;108dax = 0.0d;109day = 0.0d;110dbx = 0.0d;111dby = 0.0d;112}113114int dxRoots(final double[] roots, final int off) {115return Helpers.quadraticRoots(dax, dbx, cx, roots, off);116}117118int dyRoots(final double[] roots, final int off) {119return Helpers.quadraticRoots(day, dby, cy, roots, off);120}121122int infPoints(final double[] pts, final int off) {123// inflection point at t if -f'(t)x*f''(t)y + f'(t)y*f''(t)x == 0124// Fortunately, this turns out to be quadratic, so there are at125// most 2 inflection points.126final double a = dax * dby - dbx * day;127final double b = 2.0d * (cy * dax - day * cx);128final double c = cy * dbx - cx * dby;129130return Helpers.quadraticRoots(a, b, c, pts, off);131}132133int xPoints(final double[] ts, final int off, final double x)134{135return Helpers.cubicRootsInAB(ax, bx, cx, dx - x, ts, off, 0.0d, 1.0d);136}137138int yPoints(final double[] ts, final int off, final double y)139{140return Helpers.cubicRootsInAB(ay, by, cy, dy - y, ts, off, 0.0d, 1.0d);141}142143// finds points where the first and second derivative are144// perpendicular. This happens when g(t) = f'(t)*f''(t) == 0 (where145// * is a dot product). Unfortunately, we have to solve a cubic.146private int perpendiculardfddf(final double[] pts, final int off) {147assert pts.length >= off + 4;148149// these are the coefficients of some multiple of g(t) (not g(t),150// because the roots of a polynomial are not changed after multiplication151// by a constant, and this way we save a few multiplications).152final double a = 2.0d * (dax * dax + day * day);153final double b = 3.0d * (dax * dbx + day * dby);154final double c = 2.0d * (dax * cx + day * cy) + dbx * dbx + dby * dby;155final double d = dbx * cx + dby * cy;156157return Helpers.cubicRootsInAB(a, b, c, d, pts, off, 0.0d, 1.0d);158}159160// Tries to find the roots of the function ROC(t)-w in [0, 1). It uses161// a variant of the false position algorithm to find the roots. False162// position requires that 2 initial values x0,x1 be given, and that the163// function must have opposite signs at those values. To find such164// values, we need the local extrema of the ROC function, for which we165// need the roots of its derivative; however, it's harder to find the166// roots of the derivative in this case than it is to find the roots167// of the original function. So, we find all points where this curve's168// first and second derivative are perpendicular, and we pretend these169// are our local extrema. There are at most 3 of these, so we will check170// at most 4 sub-intervals of (0,1). ROC has asymptotes at inflection171// points, so roc-w can have at least 6 roots. This shouldn't be a172// problem for what we're trying to do (draw a nice looking curve).173int rootsOfROCMinusW(final double[] roots, final int off, final double w2, final double err) {174// no OOB exception, because by now off<=6, and roots.length >= 10175assert off <= 6 && roots.length >= 10;176177int ret = off;178final int end = off + perpendiculardfddf(roots, off);179roots[end] = 1.0d; // always check interval end points180181double t0 = 0.0d, ft0 = ROCsq(t0) - w2;182183for (int i = off; i <= end; i++) {184double t1 = roots[i], ft1 = ROCsq(t1) - w2;185if (ft0 == 0.0d) {186roots[ret++] = t0;187} else if (ft1 * ft0 < 0.0d) { // have opposite signs188// (ROC(t)^2 == w^2) == (ROC(t) == w) is true because189// ROC(t) >= 0 for all t.190roots[ret++] = falsePositionROCsqMinusX(t0, t1, w2, err);191}192t0 = t1;193ft0 = ft1;194}195196return ret - off;197}198199private static double eliminateInf(final double x) {200return (x == Double.POSITIVE_INFINITY ? Double.MAX_VALUE :201(x == Double.NEGATIVE_INFINITY ? Double.MIN_VALUE : x));202}203204// A slight modification of the false position algorithm on wikipedia.205// This only works for the ROCsq-x functions. It might be nice to have206// the function as an argument, but that would be awkward in java6.207// TODO: It is something to consider for java8 (or whenever lambda208// expressions make it into the language), depending on how closures209// and turn out. Same goes for the newton's method210// algorithm in Helpers.java211private double falsePositionROCsqMinusX(final double t0, final double t1,212final double w2, final double err)213{214final int iterLimit = 100;215int side = 0;216double t = t1, ft = eliminateInf(ROCsq(t) - w2);217double s = t0, fs = eliminateInf(ROCsq(s) - w2);218double r = s, fr;219220for (int i = 0; i < iterLimit && Math.abs(t - s) > err * Math.abs(t + s); i++) {221r = (fs * t - ft * s) / (fs - ft);222fr = ROCsq(r) - w2;223if (sameSign(fr, ft)) {224ft = fr; t = r;225if (side < 0) {226fs /= (1 << (-side));227side--;228} else {229side = -1;230}231} else if (fr * fs > 0.0d) {232fs = fr; s = r;233if (side > 0) {234ft /= (1 << side);235side++;236} else {237side = 1;238}239} else {240break;241}242}243return r;244}245246private static boolean sameSign(final double x, final double y) {247// another way is to test if x*y > 0. This is bad for small x, y.248return (x < 0.0d && y < 0.0d) || (x > 0.0d && y > 0.0d);249}250251// returns the radius of curvature squared at t of this curve252// see http://en.wikipedia.org/wiki/Radius_of_curvature_(applications)253private double ROCsq(final double t) {254final double dx = t * (t * dax + dbx) + cx;255final double dy = t * (t * day + dby) + cy;256final double ddx = 2.0d * dax * t + dbx;257final double ddy = 2.0d * day * t + dby;258final double dx2dy2 = dx * dx + dy * dy;259final double ddx2ddy2 = ddx * ddx + ddy * ddy;260final double ddxdxddydy = ddx * dx + ddy * dy;261return dx2dy2 * ((dx2dy2 * dx2dy2) / (dx2dy2 * ddx2ddy2 - ddxdxddydy * ddxdxddydy));262}263}264265266