Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/marlin/Helpers.java
38918 views
/*1* Copyright (c) 2007, 2015, 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;2627import static java.lang.Math.PI;28import static java.lang.Math.cos;29import static java.lang.Math.sqrt;30import static java.lang.Math.cbrt;31import static java.lang.Math.acos;3233final class Helpers implements MarlinConst {3435private Helpers() {36throw new Error("This is a non instantiable class");37}3839static boolean within(final float x, final float y, final float err) {40final float d = y - x;41return (d <= err && d >= -err);42}4344static boolean within(final double x, final double y, final double err) {45final double d = y - x;46return (d <= err && d >= -err);47}4849static int quadraticRoots(final float a, final float b,50final float c, float[] zeroes, final int off)51{52int ret = off;53float t;54if (a != 0f) {55final float dis = b*b - 4*a*c;56if (dis > 0f) {57final float sqrtDis = (float)Math.sqrt(dis);58// depending on the sign of b we use a slightly different59// algorithm than the traditional one to find one of the roots60// so we can avoid adding numbers of different signs (which61// might result in loss of precision).62if (b >= 0f) {63zeroes[ret++] = (2f * c) / (-b - sqrtDis);64zeroes[ret++] = (-b - sqrtDis) / (2f * a);65} else {66zeroes[ret++] = (-b + sqrtDis) / (2f * a);67zeroes[ret++] = (2f * c) / (-b + sqrtDis);68}69} else if (dis == 0f) {70t = (-b) / (2f * a);71zeroes[ret++] = t;72}73} else {74if (b != 0f) {75t = (-c) / b;76zeroes[ret++] = t;77}78}79return ret - off;80}8182// find the roots of g(t) = d*t^3 + a*t^2 + b*t + c in [A,B)83static int cubicRootsInAB(float d, float a, float b, float c,84float[] pts, final int off,85final float A, final float B)86{87if (d == 0f) {88int num = quadraticRoots(a, b, c, pts, off);89return filterOutNotInAB(pts, off, num, A, B) - off;90}91// From Graphics Gems:92// http://tog.acm.org/resources/GraphicsGems/gems/Roots3And4.c93// (also from awt.geom.CubicCurve2D. But here we don't need as94// much accuracy and we don't want to create arrays so we use95// our own customized version).9697// normal form: x^3 + ax^2 + bx + c = 098a /= d;99b /= d;100c /= d;101102// substitute x = y - A/3 to eliminate quadratic term:103// x^3 +Px + Q = 0104//105// Since we actually need P/3 and Q/2 for all of the106// calculations that follow, we will calculate107// p = P/3108// q = Q/2109// instead and use those values for simplicity of the code.110double sq_A = a * a;111double p = (1.0/3.0) * ((-1.0/3.0) * sq_A + b);112double q = (1.0/2.0) * ((2.0/27.0) * a * sq_A - (1.0/3.0) * a * b + c);113114// use Cardano's formula115116double cb_p = p * p * p;117double D = q * q + cb_p;118119int num;120if (D < 0.0) {121// see: http://en.wikipedia.org/wiki/Cubic_function#Trigonometric_.28and_hyperbolic.29_method122final double phi = (1.0/3.0) * acos(-q / sqrt(-cb_p));123final double t = 2.0 * sqrt(-p);124125pts[ off+0 ] = (float)( t * cos(phi));126pts[ off+1 ] = (float)(-t * cos(phi + (PI / 3.0)));127pts[ off+2 ] = (float)(-t * cos(phi - (PI / 3.0)));128num = 3;129} else {130final double sqrt_D = sqrt(D);131final double u = cbrt(sqrt_D - q);132final double v = - cbrt(sqrt_D + q);133134pts[ off ] = (float)(u + v);135num = 1;136137if (within(D, 0.0, 1e-8)) {138pts[off+1] = -(pts[off] / 2f);139num = 2;140}141}142143final float sub = (1f/3f) * a;144145for (int i = 0; i < num; ++i) {146pts[ off+i ] -= sub;147}148149return filterOutNotInAB(pts, off, num, A, B) - off;150}151152static float evalCubic(final float a, final float b,153final float c, final float d,154final float t)155{156return t * (t * (t * a + b) + c) + d;157}158159static float evalQuad(final float a, final float b,160final float c, final float t)161{162return t * (t * a + b) + c;163}164165// returns the index 1 past the last valid element remaining after filtering166static int filterOutNotInAB(float[] nums, final int off, final int len,167final float a, final float b)168{169int ret = off;170for (int i = off, end = off + len; i < end; i++) {171if (nums[i] >= a && nums[i] < b) {172nums[ret++] = nums[i];173}174}175return ret;176}177178static float polyLineLength(float[] poly, final int off, final int nCoords) {179assert nCoords % 2 == 0 && poly.length >= off + nCoords : "";180float acc = 0;181for (int i = off + 2; i < off + nCoords; i += 2) {182acc += linelen(poly[i], poly[i+1], poly[i-2], poly[i-1]);183}184return acc;185}186187static float linelen(float x1, float y1, float x2, float y2) {188final float dx = x2 - x1;189final float dy = y2 - y1;190return (float)Math.sqrt(dx*dx + dy*dy);191}192193static void subdivide(float[] src, int srcoff, float[] left, int leftoff,194float[] right, int rightoff, int type)195{196switch(type) {197case 6:198Helpers.subdivideQuad(src, srcoff, left, leftoff, right, rightoff);199return;200case 8:201Helpers.subdivideCubic(src, srcoff, left, leftoff, right, rightoff);202return;203default:204throw new InternalError("Unsupported curve type");205}206}207208static void isort(float[] a, int off, int len) {209for (int i = off + 1, end = off + len; i < end; i++) {210float ai = a[i];211int j = i - 1;212for (; j >= off && a[j] > ai; j--) {213a[j+1] = a[j];214}215a[j+1] = ai;216}217}218219// Most of these are copied from classes in java.awt.geom because we need220// float versions of these functions, and Line2D, CubicCurve2D,221// QuadCurve2D don't provide them.222/**223* Subdivides the cubic curve specified by the coordinates224* stored in the <code>src</code> array at indices <code>srcoff</code>225* through (<code>srcoff</code> + 7) and stores the226* resulting two subdivided curves into the two result arrays at the227* corresponding indices.228* Either or both of the <code>left</code> and <code>right</code>229* arrays may be <code>null</code> or a reference to the same array230* as the <code>src</code> array.231* Note that the last point in the first subdivided curve is the232* same as the first point in the second subdivided curve. Thus,233* it is possible to pass the same array for <code>left</code>234* and <code>right</code> and to use offsets, such as <code>rightoff</code>235* equals (<code>leftoff</code> + 6), in order236* to avoid allocating extra storage for this common point.237* @param src the array holding the coordinates for the source curve238* @param srcoff the offset into the array of the beginning of the239* the 6 source coordinates240* @param left the array for storing the coordinates for the first241* half of the subdivided curve242* @param leftoff the offset into the array of the beginning of the243* the 6 left coordinates244* @param right the array for storing the coordinates for the second245* half of the subdivided curve246* @param rightoff the offset into the array of the beginning of the247* the 6 right coordinates248* @since 1.7249*/250static void subdivideCubic(float src[], int srcoff,251float left[], int leftoff,252float right[], int rightoff)253{254float x1 = src[srcoff + 0];255float y1 = src[srcoff + 1];256float ctrlx1 = src[srcoff + 2];257float ctrly1 = src[srcoff + 3];258float ctrlx2 = src[srcoff + 4];259float ctrly2 = src[srcoff + 5];260float x2 = src[srcoff + 6];261float y2 = src[srcoff + 7];262if (left != null) {263left[leftoff + 0] = x1;264left[leftoff + 1] = y1;265}266if (right != null) {267right[rightoff + 6] = x2;268right[rightoff + 7] = y2;269}270x1 = (x1 + ctrlx1) / 2f;271y1 = (y1 + ctrly1) / 2f;272x2 = (x2 + ctrlx2) / 2f;273y2 = (y2 + ctrly2) / 2f;274float centerx = (ctrlx1 + ctrlx2) / 2f;275float centery = (ctrly1 + ctrly2) / 2f;276ctrlx1 = (x1 + centerx) / 2f;277ctrly1 = (y1 + centery) / 2f;278ctrlx2 = (x2 + centerx) / 2f;279ctrly2 = (y2 + centery) / 2f;280centerx = (ctrlx1 + ctrlx2) / 2f;281centery = (ctrly1 + ctrly2) / 2f;282if (left != null) {283left[leftoff + 2] = x1;284left[leftoff + 3] = y1;285left[leftoff + 4] = ctrlx1;286left[leftoff + 5] = ctrly1;287left[leftoff + 6] = centerx;288left[leftoff + 7] = centery;289}290if (right != null) {291right[rightoff + 0] = centerx;292right[rightoff + 1] = centery;293right[rightoff + 2] = ctrlx2;294right[rightoff + 3] = ctrly2;295right[rightoff + 4] = x2;296right[rightoff + 5] = y2;297}298}299300301static void subdivideCubicAt(float t, float src[], int srcoff,302float left[], int leftoff,303float right[], int rightoff)304{305float x1 = src[srcoff + 0];306float y1 = src[srcoff + 1];307float ctrlx1 = src[srcoff + 2];308float ctrly1 = src[srcoff + 3];309float ctrlx2 = src[srcoff + 4];310float ctrly2 = src[srcoff + 5];311float x2 = src[srcoff + 6];312float y2 = src[srcoff + 7];313if (left != null) {314left[leftoff + 0] = x1;315left[leftoff + 1] = y1;316}317if (right != null) {318right[rightoff + 6] = x2;319right[rightoff + 7] = y2;320}321x1 = x1 + t * (ctrlx1 - x1);322y1 = y1 + t * (ctrly1 - y1);323x2 = ctrlx2 + t * (x2 - ctrlx2);324y2 = ctrly2 + t * (y2 - ctrly2);325float centerx = ctrlx1 + t * (ctrlx2 - ctrlx1);326float centery = ctrly1 + t * (ctrly2 - ctrly1);327ctrlx1 = x1 + t * (centerx - x1);328ctrly1 = y1 + t * (centery - y1);329ctrlx2 = centerx + t * (x2 - centerx);330ctrly2 = centery + t * (y2 - centery);331centerx = ctrlx1 + t * (ctrlx2 - ctrlx1);332centery = ctrly1 + t * (ctrly2 - ctrly1);333if (left != null) {334left[leftoff + 2] = x1;335left[leftoff + 3] = y1;336left[leftoff + 4] = ctrlx1;337left[leftoff + 5] = ctrly1;338left[leftoff + 6] = centerx;339left[leftoff + 7] = centery;340}341if (right != null) {342right[rightoff + 0] = centerx;343right[rightoff + 1] = centery;344right[rightoff + 2] = ctrlx2;345right[rightoff + 3] = ctrly2;346right[rightoff + 4] = x2;347right[rightoff + 5] = y2;348}349}350351static void subdivideQuad(float src[], int srcoff,352float left[], int leftoff,353float right[], int rightoff)354{355float x1 = src[srcoff + 0];356float y1 = src[srcoff + 1];357float ctrlx = src[srcoff + 2];358float ctrly = src[srcoff + 3];359float x2 = src[srcoff + 4];360float y2 = src[srcoff + 5];361if (left != null) {362left[leftoff + 0] = x1;363left[leftoff + 1] = y1;364}365if (right != null) {366right[rightoff + 4] = x2;367right[rightoff + 5] = y2;368}369x1 = (x1 + ctrlx) / 2f;370y1 = (y1 + ctrly) / 2f;371x2 = (x2 + ctrlx) / 2f;372y2 = (y2 + ctrly) / 2f;373ctrlx = (x1 + x2) / 2f;374ctrly = (y1 + y2) / 2f;375if (left != null) {376left[leftoff + 2] = x1;377left[leftoff + 3] = y1;378left[leftoff + 4] = ctrlx;379left[leftoff + 5] = ctrly;380}381if (right != null) {382right[rightoff + 0] = ctrlx;383right[rightoff + 1] = ctrly;384right[rightoff + 2] = x2;385right[rightoff + 3] = y2;386}387}388389static void subdivideQuadAt(float t, float src[], int srcoff,390float left[], int leftoff,391float right[], int rightoff)392{393float x1 = src[srcoff + 0];394float y1 = src[srcoff + 1];395float ctrlx = src[srcoff + 2];396float ctrly = src[srcoff + 3];397float x2 = src[srcoff + 4];398float y2 = src[srcoff + 5];399if (left != null) {400left[leftoff + 0] = x1;401left[leftoff + 1] = y1;402}403if (right != null) {404right[rightoff + 4] = x2;405right[rightoff + 5] = y2;406}407x1 = x1 + t * (ctrlx - x1);408y1 = y1 + t * (ctrly - y1);409x2 = ctrlx + t * (x2 - ctrlx);410y2 = ctrly + t * (y2 - ctrly);411ctrlx = x1 + t * (x2 - x1);412ctrly = y1 + t * (y2 - y1);413if (left != null) {414left[leftoff + 2] = x1;415left[leftoff + 3] = y1;416left[leftoff + 4] = ctrlx;417left[leftoff + 5] = ctrly;418}419if (right != null) {420right[rightoff + 0] = ctrlx;421right[rightoff + 1] = ctrly;422right[rightoff + 2] = x2;423right[rightoff + 3] = y2;424}425}426427static void subdivideAt(float t, float src[], int srcoff,428float left[], int leftoff,429float right[], int rightoff, int size)430{431switch(size) {432case 8:433subdivideCubicAt(t, src, srcoff, left, leftoff, right, rightoff);434return;435case 6:436subdivideQuadAt(t, src, srcoff, left, leftoff, right, rightoff);437return;438}439}440}441442443