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 import static java.lang.Math.PI; 29 import static java.lang.Math.cos; 30 import static java.lang.Math.sqrt; 31 import static java.lang.Math.cbrt; 32 import static java.lang.Math.acos; 33 34 final class Helpers implements MarlinConst { 35 36 private Helpers() { 37 throw new Error("This is a non instantiable class"); 38 } 39 40 static boolean within(final float x, final float y, final float err) { 41 final float d = y - x; 42 return (d <= err && d >= -err); 43 } 44 45 static boolean within(final double x, final double y, final double err) { 46 final double d = y - x; 47 return (d <= err && d >= -err); 48 } 49 50 static int quadraticRoots(final float a, final float b, 51 final float c, float[] zeroes, final int off) 52 { 103 // substitute x = y - A/3 to eliminate quadratic term: 104 // x^3 +Px + Q = 0 105 // 106 // Since we actually need P/3 and Q/2 for all of the 107 // calculations that follow, we will calculate 108 // p = P/3 109 // q = Q/2 110 // instead and use those values for simplicity of the code. 111 double sq_A = a * a; 112 double p = (1.0d/3.0d) * ((-1.0d/3.0d) * sq_A + b); 113 double q = (1.0d/2.0d) * ((2.0d/27.0d) * a * sq_A - (1.0d/3.0d) * a * b + c); 114 115 // use Cardano's formula 116 117 double cb_p = p * p * p; 118 double D = q * q + cb_p; 119 120 int num; 121 if (D < 0.0d) { 122 // see: http://en.wikipedia.org/wiki/Cubic_function#Trigonometric_.28and_hyperbolic.29_method 123 final double phi = (1.0d/3.0d) * acos(-q / sqrt(-cb_p)); 124 final double t = 2.0d * sqrt(-p); 125 126 pts[ off+0 ] = (float) ( t * cos(phi)); 127 pts[ off+1 ] = (float) (-t * cos(phi + (PI / 3.0d))); 128 pts[ off+2 ] = (float) (-t * cos(phi - (PI / 3.0d))); 129 num = 3; 130 } else { 131 final double sqrt_D = sqrt(D); 132 final double u = cbrt(sqrt_D - q); 133 final double v = - cbrt(sqrt_D + q); 134 135 pts[ off ] = (float) (u + v); 136 num = 1; 137 138 if (within(D, 0.0d, 1e-8d)) { 139 pts[off+1] = -(pts[off] / 2.0f); 140 num = 2; 141 } 142 } 143 144 final float sub = (1.0f/3.0f) * a; 145 146 for (int i = 0; i < num; ++i) { 147 pts[ off+i ] -= sub; 148 } 149 150 return filterOutNotInAB(pts, off, num, A, B) - off; 151 } 152 153 static float evalCubic(final float a, final float b, 159 160 static float evalQuad(final float a, final float b, 161 final float c, final float t) 162 { 163 return t * (t * a + b) + c; 164 } 165 166 // returns the index 1 past the last valid element remaining after filtering 167 static int filterOutNotInAB(float[] nums, final int off, final int len, 168 final float a, final float b) 169 { 170 int ret = off; 171 for (int i = off, end = off + len; i < end; i++) { 172 if (nums[i] >= a && nums[i] < b) { 173 nums[ret++] = nums[i]; 174 } 175 } 176 return ret; 177 } 178 179 static float polyLineLength(float[] poly, final int off, final int nCoords) { 180 assert nCoords % 2 == 0 && poly.length >= off + nCoords : ""; 181 float acc = 0.0f; 182 for (int i = off + 2; i < off + nCoords; i += 2) { 183 acc += linelen(poly[i], poly[i+1], poly[i-2], poly[i-1]); 184 } 185 return acc; 186 } 187 188 static float linelen(float x1, float y1, float x2, float y2) { 189 final float dx = x2 - x1; 190 final float dy = y2 - y1; 191 return (float) Math.sqrt(dx*dx + dy*dy); 192 } 193 194 static void subdivide(float[] src, int srcoff, float[] left, int leftoff, 195 float[] right, int rightoff, int type) 196 { 197 switch(type) { 198 case 6: 199 Helpers.subdivideQuad(src, srcoff, left, leftoff, right, rightoff); 200 return; 201 case 8: 202 Helpers.subdivideCubic(src, srcoff, left, leftoff, right, rightoff); 203 return; 204 default: 205 throw new InternalError("Unsupported curve type"); 206 } 207 } 419 } 420 if (right != null) { 421 right[rightoff + 0] = ctrlx; 422 right[rightoff + 1] = ctrly; 423 right[rightoff + 2] = x2; 424 right[rightoff + 3] = y2; 425 } 426 } 427 428 static void subdivideAt(float t, float[] src, int srcoff, 429 float[] left, int leftoff, 430 float[] right, int rightoff, int size) 431 { 432 switch(size) { 433 case 8: 434 subdivideCubicAt(t, src, srcoff, left, leftoff, right, rightoff); 435 return; 436 case 6: 437 subdivideQuadAt(t, src, srcoff, left, leftoff, right, rightoff); 438 return; 439 } 440 } 441 } | 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 import static java.lang.Math.PI; 29 import java.util.Arrays; 30 import sun.awt.geom.PathConsumer2D; 31 import static sun.java2d.marlin.MarlinConst.INITIAL_EDGES_COUNT; 32 import sun.java2d.marlin.stats.Histogram; 33 import sun.java2d.marlin.stats.StatLong; 34 35 final class Helpers implements MarlinConst { 36 37 private Helpers() { 38 throw new Error("This is a non instantiable class"); 39 } 40 41 static boolean within(final float x, final float y, final float err) { 42 final float d = y - x; 43 return (d <= err && d >= -err); 44 } 45 46 static boolean within(final double x, final double y, final double err) { 47 final double d = y - x; 48 return (d <= err && d >= -err); 49 } 50 51 static int quadraticRoots(final float a, final float b, 52 final float c, float[] zeroes, final int off) 53 { 104 // substitute x = y - A/3 to eliminate quadratic term: 105 // x^3 +Px + Q = 0 106 // 107 // Since we actually need P/3 and Q/2 for all of the 108 // calculations that follow, we will calculate 109 // p = P/3 110 // q = Q/2 111 // instead and use those values for simplicity of the code. 112 double sq_A = a * a; 113 double p = (1.0d/3.0d) * ((-1.0d/3.0d) * sq_A + b); 114 double q = (1.0d/2.0d) * ((2.0d/27.0d) * a * sq_A - (1.0d/3.0d) * a * b + c); 115 116 // use Cardano's formula 117 118 double cb_p = p * p * p; 119 double D = q * q + cb_p; 120 121 int num; 122 if (D < 0.0d) { 123 // see: http://en.wikipedia.org/wiki/Cubic_function#Trigonometric_.28and_hyperbolic.29_method 124 final double phi = (1.0d/3.0d) * Math.acos(-q / Math.sqrt(-cb_p)); 125 final double t = 2.0d * Math.sqrt(-p); 126 127 pts[ off+0 ] = (float) ( t * Math.cos(phi)); 128 pts[ off+1 ] = (float) (-t * Math.cos(phi + (PI / 3.0d))); 129 pts[ off+2 ] = (float) (-t * Math.cos(phi - (PI / 3.0d))); 130 num = 3; 131 } else { 132 final double sqrt_D = Math.sqrt(D); 133 final double u = Math.cbrt(sqrt_D - q); 134 final double v = - Math.cbrt(sqrt_D + q); 135 136 pts[ off ] = (float) (u + v); 137 num = 1; 138 139 if (within(D, 0.0d, 1e-8d)) { 140 pts[off+1] = -(pts[off] / 2.0f); 141 num = 2; 142 } 143 } 144 145 final float sub = (1.0f/3.0f) * a; 146 147 for (int i = 0; i < num; ++i) { 148 pts[ off+i ] -= sub; 149 } 150 151 return filterOutNotInAB(pts, off, num, A, B) - off; 152 } 153 154 static float evalCubic(final float a, final float b, 160 161 static float evalQuad(final float a, final float b, 162 final float c, final float t) 163 { 164 return t * (t * a + b) + c; 165 } 166 167 // returns the index 1 past the last valid element remaining after filtering 168 static int filterOutNotInAB(float[] nums, final int off, final int len, 169 final float a, final float b) 170 { 171 int ret = off; 172 for (int i = off, end = off + len; i < end; i++) { 173 if (nums[i] >= a && nums[i] < b) { 174 nums[ret++] = nums[i]; 175 } 176 } 177 return ret; 178 } 179 180 static float linelen(float x1, float y1, float x2, float y2) { 181 final float dx = x2 - x1; 182 final float dy = y2 - y1; 183 return (float) Math.sqrt(dx*dx + dy*dy); 184 } 185 186 static void subdivide(float[] src, int srcoff, float[] left, int leftoff, 187 float[] right, int rightoff, int type) 188 { 189 switch(type) { 190 case 6: 191 Helpers.subdivideQuad(src, srcoff, left, leftoff, right, rightoff); 192 return; 193 case 8: 194 Helpers.subdivideCubic(src, srcoff, left, leftoff, right, rightoff); 195 return; 196 default: 197 throw new InternalError("Unsupported curve type"); 198 } 199 } 411 } 412 if (right != null) { 413 right[rightoff + 0] = ctrlx; 414 right[rightoff + 1] = ctrly; 415 right[rightoff + 2] = x2; 416 right[rightoff + 3] = y2; 417 } 418 } 419 420 static void subdivideAt(float t, float[] src, int srcoff, 421 float[] left, int leftoff, 422 float[] right, int rightoff, int size) 423 { 424 switch(size) { 425 case 8: 426 subdivideCubicAt(t, src, srcoff, left, leftoff, right, rightoff); 427 return; 428 case 6: 429 subdivideQuadAt(t, src, srcoff, left, leftoff, right, rightoff); 430 return; 431 } 432 } 433 434 // From sun.java2d.loops.GeneralRenderer: 435 436 static int outcode(final float x, final float y, 437 final float[] clipRect) 438 { 439 int code; 440 if (y < clipRect[0]) { 441 code = OUTCODE_TOP; 442 } else if (y >= clipRect[1]) { 443 code = OUTCODE_BOTTOM; 444 } else { 445 code = 0; 446 } 447 if (x < clipRect[2]) { 448 code |= OUTCODE_LEFT; 449 } else if (x >= clipRect[3]) { 450 code |= OUTCODE_RIGHT; 451 } 452 return code; 453 } 454 455 // a stack of polynomial curves where each curve shares endpoints with 456 // adjacent ones. 457 static final class PolyStack { 458 private static final byte TYPE_LINETO = (byte) 0; 459 private static final byte TYPE_QUADTO = (byte) 1; 460 private static final byte TYPE_CUBICTO = (byte) 2; 461 462 // curves capacity = edges count (8192) = edges x 2 (coords) 463 private static final int INITIAL_CURVES_COUNT = INITIAL_EDGES_COUNT << 1; 464 465 // types capacity = edges count (4096) 466 private static final int INITIAL_TYPES_COUNT = INITIAL_EDGES_COUNT; 467 468 float[] curves; 469 int end; 470 byte[] curveTypes; 471 int numCurves; 472 473 // curves ref (dirty) 474 final FloatArrayCache.Reference curves_ref; 475 // curveTypes ref (dirty) 476 final ByteArrayCache.Reference curveTypes_ref; 477 478 // used marks (stats only) 479 int curveTypesUseMark; 480 int curvesUseMark; 481 482 private final StatLong stat_polystack_types; 483 private final StatLong stat_polystack_curves; 484 private final Histogram hist_polystack_curves; 485 private final StatLong stat_array_polystack_curves; 486 private final StatLong stat_array_polystack_curveTypes; 487 488 PolyStack(final RendererContext rdrCtx) { 489 this(rdrCtx, null, null, null, null, null); 490 } 491 492 PolyStack(final RendererContext rdrCtx, 493 final StatLong stat_polystack_types, 494 final StatLong stat_polystack_curves, 495 final Histogram hist_polystack_curves, 496 final StatLong stat_array_polystack_curves, 497 final StatLong stat_array_polystack_curveTypes) 498 { 499 curves_ref = rdrCtx.newDirtyFloatArrayRef(INITIAL_CURVES_COUNT); // 32K 500 curves = curves_ref.initial; 501 502 curveTypes_ref = rdrCtx.newDirtyByteArrayRef(INITIAL_TYPES_COUNT); // 4K 503 curveTypes = curveTypes_ref.initial; 504 numCurves = 0; 505 end = 0; 506 507 if (DO_STATS) { 508 curveTypesUseMark = 0; 509 curvesUseMark = 0; 510 } 511 this.stat_polystack_types = stat_polystack_types; 512 this.stat_polystack_curves = stat_polystack_curves; 513 this.hist_polystack_curves = hist_polystack_curves; 514 this.stat_array_polystack_curves = stat_array_polystack_curves; 515 this.stat_array_polystack_curveTypes = stat_array_polystack_curveTypes; 516 } 517 518 /** 519 * Disposes this PolyStack: 520 * clean up before reusing this instance 521 */ 522 void dispose() { 523 end = 0; 524 numCurves = 0; 525 526 if (DO_STATS) { 527 stat_polystack_types.add(curveTypesUseMark); 528 stat_polystack_curves.add(curvesUseMark); 529 hist_polystack_curves.add(curvesUseMark); 530 531 // reset marks 532 curveTypesUseMark = 0; 533 curvesUseMark = 0; 534 } 535 536 // Return arrays: 537 // curves and curveTypes are kept dirty 538 curves = curves_ref.putArray(curves); 539 curveTypes = curveTypes_ref.putArray(curveTypes); 540 } 541 542 private void ensureSpace(final int n) { 543 // use substraction to avoid integer overflow: 544 if (curves.length - end < n) { 545 if (DO_STATS) { 546 stat_array_polystack_curves.add(end + n); 547 } 548 curves = curves_ref.widenArray(curves, end, end + n); 549 } 550 if (curveTypes.length <= numCurves) { 551 if (DO_STATS) { 552 stat_array_polystack_curveTypes.add(numCurves + 1); 553 } 554 curveTypes = curveTypes_ref.widenArray(curveTypes, 555 numCurves, 556 numCurves + 1); 557 } 558 } 559 560 void pushCubic(float x0, float y0, 561 float x1, float y1, 562 float x2, float y2) 563 { 564 ensureSpace(6); 565 curveTypes[numCurves++] = TYPE_CUBICTO; 566 // we reverse the coordinate order to make popping easier 567 final float[] _curves = curves; 568 int e = end; 569 _curves[e++] = x2; _curves[e++] = y2; 570 _curves[e++] = x1; _curves[e++] = y1; 571 _curves[e++] = x0; _curves[e++] = y0; 572 end = e; 573 } 574 575 void pushQuad(float x0, float y0, 576 float x1, float y1) 577 { 578 ensureSpace(4); 579 curveTypes[numCurves++] = TYPE_QUADTO; 580 final float[] _curves = curves; 581 int e = end; 582 _curves[e++] = x1; _curves[e++] = y1; 583 _curves[e++] = x0; _curves[e++] = y0; 584 end = e; 585 } 586 587 void pushLine(float x, float y) { 588 ensureSpace(2); 589 curveTypes[numCurves++] = TYPE_LINETO; 590 curves[end++] = x; curves[end++] = y; 591 } 592 593 void pullAll(final PathConsumer2D io) { 594 final int nc = numCurves; 595 if (nc == 0) { 596 return; 597 } 598 if (DO_STATS) { 599 // update used marks: 600 if (numCurves > curveTypesUseMark) { 601 curveTypesUseMark = numCurves; 602 } 603 if (end > curvesUseMark) { 604 curvesUseMark = end; 605 } 606 } 607 final byte[] _curveTypes = curveTypes; 608 final float[] _curves = curves; 609 int e = 0; 610 611 for (int i = 0; i < nc; i++) { 612 switch(_curveTypes[i]) { 613 case TYPE_LINETO: 614 io.lineTo(_curves[e], _curves[e+1]); 615 e += 2; 616 continue; 617 case TYPE_QUADTO: 618 io.quadTo(_curves[e+0], _curves[e+1], 619 _curves[e+2], _curves[e+3]); 620 e += 4; 621 continue; 622 case TYPE_CUBICTO: 623 io.curveTo(_curves[e+0], _curves[e+1], 624 _curves[e+2], _curves[e+3], 625 _curves[e+4], _curves[e+5]); 626 e += 6; 627 continue; 628 default: 629 } 630 } 631 numCurves = 0; 632 end = 0; 633 } 634 635 void popAll(final PathConsumer2D io) { 636 int nc = numCurves; 637 if (nc == 0) { 638 return; 639 } 640 if (DO_STATS) { 641 // update used marks: 642 if (numCurves > curveTypesUseMark) { 643 curveTypesUseMark = numCurves; 644 } 645 if (end > curvesUseMark) { 646 curvesUseMark = end; 647 } 648 } 649 final byte[] _curveTypes = curveTypes; 650 final float[] _curves = curves; 651 int e = end; 652 653 while (nc != 0) { 654 switch(_curveTypes[--nc]) { 655 case TYPE_LINETO: 656 e -= 2; 657 io.lineTo(_curves[e], _curves[e+1]); 658 continue; 659 case TYPE_QUADTO: 660 e -= 4; 661 io.quadTo(_curves[e+0], _curves[e+1], 662 _curves[e+2], _curves[e+3]); 663 continue; 664 case TYPE_CUBICTO: 665 e -= 6; 666 io.curveTo(_curves[e+0], _curves[e+1], 667 _curves[e+2], _curves[e+3], 668 _curves[e+4], _curves[e+5]); 669 continue; 670 default: 671 } 672 } 673 numCurves = 0; 674 end = 0; 675 } 676 677 @Override 678 public String toString() { 679 String ret = ""; 680 int nc = numCurves; 681 int last = end; 682 int len; 683 while (nc != 0) { 684 switch(curveTypes[--nc]) { 685 case TYPE_LINETO: 686 len = 2; 687 ret += "line: "; 688 break; 689 case TYPE_QUADTO: 690 len = 4; 691 ret += "quad: "; 692 break; 693 case TYPE_CUBICTO: 694 len = 6; 695 ret += "cubic: "; 696 break; 697 default: 698 len = 0; 699 } 700 last -= len; 701 ret += Arrays.toString(Arrays.copyOfRange(curves, last, last+len)) 702 + "\n"; 703 } 704 return ret; 705 } 706 } 707 708 // a stack of integer indices 709 static final class IndexStack { 710 711 // integer capacity = edges count / 4 ~ 1024 712 private static final int INITIAL_COUNT = INITIAL_EDGES_COUNT >> 2; 713 714 private int end; 715 private int[] indices; 716 717 // indices ref (dirty) 718 private final IntArrayCache.Reference indices_ref; 719 720 // used marks (stats only) 721 private int indicesUseMark; 722 723 private final StatLong stat_idxstack_indices; 724 private final Histogram hist_idxstack_indices; 725 private final StatLong stat_array_idxstack_indices; 726 727 IndexStack(final RendererContext rdrCtx) { 728 this(rdrCtx, null, null, null); 729 } 730 731 IndexStack(final RendererContext rdrCtx, 732 final StatLong stat_idxstack_indices, 733 final Histogram hist_idxstack_indices, 734 final StatLong stat_array_idxstack_indices) 735 { 736 indices_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_COUNT); // 4K 737 indices = indices_ref.initial; 738 end = 0; 739 740 if (DO_STATS) { 741 indicesUseMark = 0; 742 } 743 this.stat_idxstack_indices = stat_idxstack_indices; 744 this.hist_idxstack_indices = hist_idxstack_indices; 745 this.stat_array_idxstack_indices = stat_array_idxstack_indices; 746 } 747 748 /** 749 * Disposes this PolyStack: 750 * clean up before reusing this instance 751 */ 752 void dispose() { 753 end = 0; 754 755 if (DO_STATS) { 756 stat_idxstack_indices.add(indicesUseMark); 757 hist_idxstack_indices.add(indicesUseMark); 758 759 // reset marks 760 indicesUseMark = 0; 761 } 762 763 // Return arrays: 764 // values is kept dirty 765 indices = indices_ref.putArray(indices); 766 } 767 768 boolean isEmpty() { 769 return (end == 0); 770 } 771 772 void reset() { 773 end = 0; 774 } 775 776 void push(final int v) { 777 // remove redundant values (reverse order): 778 int[] _values = indices; 779 final int nc = end; 780 if (nc != 0) { 781 if (_values[nc - 1] == v) { 782 // remove both duplicated values: 783 end--; 784 return; 785 } 786 } 787 if (_values.length <= nc) { 788 if (DO_STATS) { 789 stat_array_idxstack_indices.add(nc + 1); 790 } 791 indices = _values = indices_ref.widenArray(_values, nc, nc + 1); 792 } 793 _values[end++] = v; 794 795 if (DO_STATS) { 796 // update used marks: 797 if (end > indicesUseMark) { 798 indicesUseMark = end; 799 } 800 } 801 } 802 803 void pullAll(final float[] points, final PathConsumer2D io) { 804 final int nc = end; 805 if (nc == 0) { 806 return; 807 } 808 final int[] _values = indices; 809 810 for (int i = 0, j; i < nc; i++) { 811 j = _values[i] << 1; 812 io.lineTo(points[j], points[j + 1]); 813 } 814 end = 0; 815 } 816 } 817 } |